home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / ColorSync SDK / Sample Code / CSDemo 2.1 / ShellSources / win.c < prev    next >
Encoding:
Text File  |  1997-06-13  |  16.2 KB  |  624 lines  |  [TEXT/CWIE]

  1. // Simple framework for Macintosh sample code
  2. //
  3. // David Hayward and Nick Thompson 
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyrite 1995, Apple Computer,Inc
  8. //
  9. // Access routines for the winHandle structure that is used this
  10. // simple application framework
  11. // 
  12. // 9/2/94    nick    first cut
  13. // 10/4/94    nick    add field for the doc FSSpec
  14. // 11/16/94    david    in the DocumentRecord structure
  15. //                        added fields dPicHandle, dPictPalette, dPictCTab
  16. //                        changed field dDocFsSp to dFSSpec
  17. //                        changed field dPageFormat to dGXFormat
  18. //                        changed field dDocumentJob to dGXJob
  19. //                    added several getters/setters routines
  20. //                    changed some code to use the new getters/setters
  21. //                    changed the #define SetWinumentHandleForWindowRefcon to a function SetWindowDocumentHandle
  22. //                    changed the #define GetWinumentHandleForWindowRefcon to a function GetWindowDocumentHandle
  23. //                    changed varaible names from "theDoc" to "doc" for brevity
  24. //                    added some lines to DisposeDocumentHandle so doc's palette and color table are disposed
  25. // 3/10/95    david    fixed bugs in FindWinHandle
  26. // 8/23/95    david    strengthened sanity checks in Get/SetWindowWinHandle
  27. //                     changed to use accessors in <Windows.h> v2.1 under STRICT_WINDOWS
  28. // 2/15/96    david    improved FindWinHandle to also seach subtype
  29.  
  30. #define OLDROUTINENAMES 1
  31.  
  32. #include <Errors.h>
  33. #include <Windows.h>
  34. #include <Files.h>
  35. #include <TextUtils.h>
  36.  
  37. #include "appErrors.h"
  38.  
  39. #include "win.h"
  40.  
  41. #ifndef PIGS_SHELL_NOPRINT
  42. #include "gxPrintUtils.h"
  43. #endif
  44.  
  45.  
  46. /**\
  47. |**| ==============================================================================
  48. |**| PRIVATE DEFINES
  49. |**| ==============================================================================
  50. \**/
  51. #define kWindowKind    123
  52.  
  53.  
  54. /**\
  55. |**| ==============================================================================
  56. |**| PRIVATE FUNCTION PROTOTYPES
  57. |**| ==============================================================================
  58. \**/
  59. Boolean    SameFSSpec    ( FSSpec *fs1, FSSpec *fs2 ) ;
  60.  
  61.  
  62. /**\
  63. |**| ==============================================================================
  64. |**| PUBLIC FUNCTIONS
  65. |**| ==============================================================================
  66. \**/
  67.  
  68.  
  69. /*------------------------------------------------------------------------------*\
  70.     GetWindowWinHandle
  71.  *------------------------------------------------------------------------------*
  72.         This routine gets the winHandle from the refcon field of a window
  73.         and returns it.
  74. \*------------------------------------------------------------------------------*/
  75. winHandle GetWindowWinHandle ( WindowRef w )
  76. {
  77.     winHandle    win;
  78.     short        kind;
  79.     
  80.     if (w==nil)
  81.         return nil ;
  82.  
  83.     kind = GetWindowKind( w ) ;
  84.     if (kind != kWindowKind)        // validate - check for our windowKind
  85.         return nil ;
  86.  
  87.     win = (winHandle) GetWRefCon( w ) ;
  88.     if (win == nil)                    // validate - check for nil
  89.         return nil ;
  90.     
  91.     // validate - check for the document creator 
  92.     // as the first long of the struct
  93.     if( GetWinCreator(win) != kDocumentCreator )
  94.         win = nil;
  95.         
  96.     return win ;                    // assert: this will either be valid or nil
  97. }
  98.  
  99.  
  100. /*------------------------------------------------------------------------------*\
  101.     GetWindowWinHandle
  102.  *------------------------------------------------------------------------------*
  103.         This routine puts the winHandle into the refcon field of a window.
  104. \*------------------------------------------------------------------------------*/
  105. void SetWindowWinHandle ( WindowRef w, winHandle win )
  106. {
  107.     SetWRefCon( w, (long)win ) ;
  108.     SetWindowKind( w, kWindowKind) ;
  109. }
  110.  
  111.  
  112. /*------------------------------------------------------------------------------*\
  113.     FrontWin/GetNextWin
  114.  *------------------------------------------------------------------------------*
  115.         This routinee are equivalent to FindWindow and GetNextWindow.
  116. \*------------------------------------------------------------------------------*/
  117. winHandle    GetFrontWindowWinHandle( void )
  118. {
  119.     return GetWindowWinHandle( FrontWindow() ) ;
  120. }
  121.  
  122. winHandle    FrontWin( void )
  123. {
  124.     winHandle    win = nil;
  125.     WindowRef    window ;
  126.     
  127.     window = FrontWindow() ;
  128.     while ( window != nil && win==nil ) {
  129.         win = GetWindowWinHandle( window ) ;
  130.         window = GetNextWindow( window ) ;
  131.     }
  132.     return win;
  133. }
  134.  
  135. winHandle    GetNextWin( winHandle win )
  136. {
  137.     winHandle    next = nil;
  138.     WindowRef    window ;
  139.     
  140.     window = GetWinWindow(win) ;
  141.     while ( window != nil && next==nil ) {
  142.         window = GetNextWindow( window ) ;
  143.         next = GetWindowWinHandle( window ) ;
  144.     }
  145.     return next;
  146. }
  147.  
  148. /*------------------------------------------------------------------------------*\
  149.     FindWinHandle
  150.  *------------------------------------------------------------------------------*
  151.         This routine searches through the active window list to locate those that
  152.         have winHandle's with a given FSSpec, type or subtype.  It can be used either 
  153.         to count the number of matches or to return the matches in the result buffer.
  154.         The logic is basically the following:
  155.           if result buffer is nil
  156.             return num of winHandles that match the specPtr, type or subtype parameters
  157.             the index and count fields are ignored
  158.           else
  159.             copy into result buffer up to count winHandles
  160.             that match the specPtr and/or type parameters
  161.             starting with the index-th match
  162.             return the the number of winHandles copied into the buffer
  163. \*------------------------------------------------------------------------------*/
  164. short    FindWinHandle ( FSSpec    *specPtr,
  165.                         OSType type,
  166.                         OSType subtype,
  167.                         short index,
  168.                         short count,
  169.                         winHandle *result )
  170. {
  171.     WindowRef        w;
  172.     FSSpec            docSpec ;
  173.     winHandle        win = nil;
  174.     short            n = 0;
  175.     short            stopCount, foundCount;
  176.     Boolean            sameSpec, sameType, sameSubtype;
  177.     
  178.     stopCount = count ;
  179.     foundCount = 0 ;
  180.  
  181.     w = FrontWindow() ;
  182.     if (w==nil) return 0 ;        // no window are open
  183.  
  184.     do {
  185.         win = GetWindowWinHandle( w ) ;
  186.         if ( win != nil )
  187.         {
  188.             sameSpec = true;
  189.             sameType = true;
  190.             sameSubtype = true;
  191.             
  192.             if (specPtr) docSpec = GetWinFSSpec( win ) ;
  193.             if (specPtr) sameSpec = SameFSSpec( &docSpec, specPtr) ;
  194.             if (type)    sameType = (type==GetWinType(win)) ;
  195.             if (subtype) sameSubtype = (subtype==GetWinSubtype(win)) ;
  196.             
  197.             if ( sameSpec && sameType && sameSubtype )
  198.             {
  199.                 if  ( result==nil )
  200.                     foundCount++ ;
  201.                 else
  202.                 {
  203.                     n++;
  204.                     if (n>=index)
  205.                     {
  206.                         foundCount++ ;
  207.                         result[foundCount-1] = win;
  208.                     }
  209.                 }
  210.             }
  211.         }
  212.         w = GetNextWindow( w ) ;
  213.  
  214.     } while ( (w!=nil)  &&  ((foundCount<stopCount) || (result==nil)) ) ;
  215.     
  216.     return foundCount;
  217. }
  218.  
  219.  
  220. /*------------------------------------------------------------------------------*\
  221.     NewWinHandle
  222.  *------------------------------------------------------------------------------*
  223.         This routine allocates a new winRecord, sets record's AllocProc, and
  224.         then calls that AllocProc to do the rest of the work.
  225. \*------------------------------------------------------------------------------*/
  226. OSErr    NewWinHandle ( winHandle *win, AllocProcPtr allocProc )
  227. {
  228.     OSErr        err = noErr ;
  229.     
  230.     *win = (winHandle)NewHandleClear( sizeof(winRecord) ) ;    
  231.     if( *win == nil )
  232.     {
  233.         err = MemError() ;
  234.         if (err==noErr)  err=mFulErr ;    // should only need this when memerror is noErr
  235.         return err ;
  236.     }
  237.  
  238.     // init the creator field
  239.     SetWinCreator( *win, kDocumentCreator ) ;
  240.  
  241.     // allocate any custom data for the win
  242.     SetWinAllocProc( *win, allocProc ) ;
  243.     err = CallWinAllocProc( *win ) ;
  244.     if ( err != noErr)
  245.         DisposeHandle( (Handle)(*win) ) ;
  246.         
  247.     return err ;
  248. }
  249.  
  250.  
  251. /*------------------------------------------------------------------------------*\
  252.     NewWinHandle
  253.  *------------------------------------------------------------------------------*
  254.         This routine calls the winHandle's DisposeProc, disposes of the structures
  255.         associated with the winHandle, and lastly, disposed of the winHandle 
  256.         itself.
  257. \*------------------------------------------------------------------------------*/
  258. OSErr    DisposeWinHandle ( winHandle win )
  259. {
  260.     OSErr    err = noErr ;
  261.     
  262.     // deallocate the fields…
  263.  
  264.     // dispose of any custom data for the win
  265.     CallWinDisposeProc( win ) ;
  266.  
  267.     // dispose of the window we used
  268.     if ( GetWinWindow(win) != nil )
  269.     {
  270.         DisposeWindow( GetWinWindow(win) ) ;
  271.         err =  QDError() ;
  272.     }
  273.  
  274. #ifndef PIGS_SHELL_NOPRINT
  275.     // get rid of the memory allocated for the document job
  276.     if ( GetWinGXJob(win) != nil ) 
  277.     {
  278.         if( GXPrinting_present()==noErr )
  279.             GXDisposeJob( GetWinGXJob(win) ) ;
  280.     }
  281.  
  282.     // get rid of the memory allocated for the print record
  283.     if ( GetWinPrintRec(win) != nil )
  284.     {
  285.         DisposeHandle((Handle)GetWinPrintRec(win)) ;
  286.         err =  MemError() ;
  287.     }
  288. #endif
  289.     
  290.     // get rid of the memory for the document handle  - DO THIS LAST
  291.     if ( err == noErr )
  292.     {
  293.         DisposeHandle( (Handle)win ) ;
  294.         err =  MemError() ;
  295.     }
  296.     
  297.     return    err ;
  298. }
  299.  
  300.  
  301. /*------------------------------------------------------------------------------*\
  302.     GetWinXxxx / SetWinXxxx
  303.  *------------------------------------------------------------------------------*
  304.         These routines get/set the data fields of a winHandle.
  305. \*------------------------------------------------------------------------------*/
  306.  
  307.  
  308. OSType        GetWinCreator ( winHandle win )
  309. {    return (win) ? ((**win).Creator) : (nil) ;
  310. }
  311. void        SetWinCreator ( winHandle win, OSType creator )
  312. {    if (win) (**win).Creator = creator ;
  313. }
  314.  
  315.  
  316. OSType        GetWinType ( winHandle win )
  317. {    return (win) ? ((**win).Type) : (nil) ;
  318. }
  319. void        SetWinType ( winHandle win, OSType type )
  320. {    if (win) (**win).Type = type ;
  321. }
  322.  
  323.  
  324. OSType        GetWinSubtype ( winHandle win )
  325. {    return (win) ? ((**win).Subtype) : (nil) ;
  326. }
  327. void        SetWinSubtype ( winHandle win, OSType type )
  328. {    if (win) (**win).Subtype = type ;
  329. }
  330.  
  331.  
  332. FSSpec        GetWinFSSpec ( winHandle win )
  333. {    return (**win).FSSpec ;
  334. }
  335. void        SetWinFSSpec ( winHandle win, FSSpec *theFSSpec )
  336. {    if (win) (**win).FSSpec = *theFSSpec ;
  337. }
  338.  
  339.  
  340. WindowRef    GetWinWindow ( winHandle win )
  341. {    return (win) ? ((**win).Window) : (nil) ;
  342. }
  343. void        SetWinWindow ( winHandle win, WindowRef theWindow )
  344. {    if (win) (**win).Window = theWindow ;
  345. }
  346.  
  347.  
  348. Rect        GetWinRect ( winHandle win )
  349. {    return (**win).Rect ;
  350. }
  351. void        SetWinRect ( winHandle win, Rect theRect )
  352. {    if (win) (**win).Rect = theRect ;
  353. }
  354.  
  355.  
  356. Rect        GetWinSizeRect ( winHandle win )
  357. {    return (**win).SizeRect ;
  358. }
  359. void        SetWinSizeRect ( winHandle win, Rect theRect )
  360. {    if (win) (**win).SizeRect = theRect ;
  361. }
  362.  
  363.  
  364. Boolean        GetWinDirty ( winHandle win )
  365. {    return (win) ? ((**win).Dirty) : (nil) ;
  366. }
  367. void        SetWinDirty ( winHandle win, Boolean dirty )
  368. {    if (win) (**win).Dirty = dirty ;
  369. }
  370.  
  371.  
  372. #ifndef PIGS_SHELL_NOPRINT
  373.  
  374. THPrint        GetWinPrintRec ( winHandle win )
  375. {    return (win) ? ((**win).PrintRec) : (nil) ;
  376. }
  377. void    SetWinPrintRec ( winHandle win, THPrint thePrintRec )
  378. {    if (win) (**win).PrintRec = thePrintRec ;
  379. }
  380.  
  381.  
  382. gxJob        GetWinGXJob ( winHandle win )
  383. {    return (win) ? ((**win).GXJob) : (nil) ;
  384. }
  385. void        SetWinGXJob ( winHandle win, gxJob theGXJob )
  386. {    if (win) (**win).GXJob = theGXJob ;
  387. }
  388.  
  389. #endif
  390.  
  391.  
  392. Handle        GetWinData ( winHandle win )
  393. {    return (win) ? ((**win).Data) : (nil) ;
  394. }
  395. void        SetWinData ( winHandle win, Handle h )
  396. {    if (win) (**win).Data = h ;
  397. }
  398.  
  399.  
  400. /*------------------------------------------------------------------------------*\
  401.     CallWinXxxx / SetWinXxxx
  402.  *------------------------------------------------------------------------------*
  403.         These routines call/set the procPtrs of a winHandle.
  404. \*------------------------------------------------------------------------------*/
  405.  
  406.  
  407. void    CallWinActivateProc (winHandle win, Boolean activating)
  408. {    if ( (win) && ((**win).ActivateProc) )
  409.             ((**win).ActivateProc)(win, activating) ;
  410. }
  411. void    SetWinActivateProc (winHandle win, ActivateProcPtr f)
  412. {    if (win) (**win).ActivateProc = f;
  413. }
  414.  
  415.  
  416. void    CallWinResumeProc (winHandle win, Boolean resuming)
  417. {    if ( (win) && ((**win).ResumeProc) )
  418.             ((**win).ResumeProc)(win, resuming) ;
  419. }
  420. void    SetWinResumeProc (winHandle win, ResumeProcPtr f)
  421. {    if (win) (**win).ResumeProc = f;
  422. }
  423.  
  424.  
  425. void    CallWinUpdateProc (winHandle win, EventRecord *e)
  426. {    if ( (win) && ((**win).UpdateProc) )
  427.         ((**win).UpdateProc)(win, e) ;
  428. }
  429. void    SetWinUpdateProc (winHandle win, UpdateProcPtr f)
  430. {    if (win) (**win).UpdateProc = f;
  431. }
  432.  
  433.  
  434. void    CallWinClickProc (winHandle win, EventRecord *e)
  435. {    if ( (win) && ((**win).ClickProc) )
  436.         ((**win).ClickProc)(win, e) ;
  437. }
  438. void    SetWinClickProc (winHandle win, ClickProcPtr f)
  439. {    if (win) (**win).ClickProc = f;
  440. }
  441.  
  442.  
  443. void    CallWinKeyProc (winHandle win, EventRecord *e)
  444. {    if ( (win) && ((**win).KeyProc) )
  445.         ((**win).KeyProc)(win, e) ;
  446. }
  447. void    SetWinKeyProc (winHandle win, KeyProcPtr f)
  448. {    if (win) (**win).KeyProc = f;
  449. }
  450.  
  451.  
  452. void    CallWinNullProc (winHandle win, EventRecord *e)
  453. {    if ( (win) && ((**win).NullProc) )
  454.         ((**win).NullProc)(win, e) ;
  455. }
  456. void    SetWinNullProc (winHandle win, NullProcPtr f)
  457. {    if (win) (**win).NullProc = f;
  458. }
  459.  
  460.  
  461. void        CallWinMenuProc (winHandle win, long m, Boolean *didit)
  462. {    if ( (win) && ((**win).MenuProc) )
  463.             ((**win).MenuProc)(win, m, didit) ;
  464. }
  465. void        SetWinMenuProc (winHandle win, MenuProcPtr f)
  466. {    if (win) (**win).MenuProc = f;
  467. }
  468.  
  469.  
  470. void        CallWinUpdateMenusProc (winHandle win)
  471. {    if ( (win) && ((**win).UpdateMenusProc) )
  472.         ((**win).UpdateMenusProc)(win) ;
  473. }
  474. void        SetWinUpdateMenusProc (winHandle win, UpdateMenusProcPtr f)
  475. {    if (win) (**win).UpdateMenusProc = f;
  476. }
  477.  
  478.  
  479. void        CallWinResizeProc (winHandle win)
  480. {    if ( (win) && ((**win).ResizeProc) )
  481.         ((**win).ResizeProc)(win) ;
  482. }
  483. void        SetWinResizeProc (winHandle win, ResizeProcPtr f)
  484. {    if (win) (**win).ResizeProc = f;
  485. }
  486.  
  487.  
  488. OSErr        CallWinAllocProc (winHandle win )
  489. {    if ( (win) && ((**win).AllocProc) )
  490.         return ((**win).AllocProc)(win) ;
  491.     else return eFatalNoProcPtr ;
  492. }
  493. void        SetWinAllocProc (winHandle win, AllocProcPtr f)
  494. {    if (win) (**win).AllocProc = f;
  495. }
  496.  
  497.  
  498. OSErr        CallWinNewProc (winHandle win )
  499. {    if ( (win) && ((**win).NewProc) )
  500.         return ((**win).NewProc)(win) ;
  501.     else return eFatalNoProcPtr ;
  502. }
  503. void        SetWinNewProc (winHandle win, NewProcPtr f)
  504. {    if (win) (**win).NewProc = f;
  505. }
  506.  
  507.  
  508. OSErr        CallWinOpenProc (winHandle win )
  509. {    if ( (win) && ((**win).OpenProc) )
  510.         return((**win).OpenProc)(win) ;
  511.     else return eFatalNoProcPtr ;
  512. }
  513. void        SetWinOpenProc (winHandle win, OpenProcPtr f)
  514. {    if (win) (**win).OpenProc = f;
  515. }
  516.  
  517.  
  518. void        CallWinCloseProc (winHandle win)
  519. {    if ( (win) && ((**win).CloseProc) )
  520.         ((**win).CloseProc)(win) ;
  521. }
  522. void        SetWinCloseProc (winHandle win, CloseProcPtr f)
  523. {    if (win) (**win).CloseProc = f;
  524. }
  525.  
  526.  
  527. void        CallWinDisposeProc    ( winHandle win )
  528. {    if ( (win) && ((**win).DisposeProc) )
  529.         ((**win).DisposeProc)(win) ;
  530. }
  531. void        SetWinDisposeProc    ( winHandle win, DisposeProcPtr f )
  532. {    if (win) (**win).DisposeProc = f;
  533. }
  534.  
  535.  
  536. #ifndef PIGS_SHELL_NOPRINT
  537.  
  538. OSErr        CallWinPageCountProc (winHandle win, short *pageCount)
  539. {    if ( (win) && ((**win).PageCountProc) )
  540.         return ((**win).PageCountProc)(win,pageCount) ;
  541.     else return eFatalNoProcPtr ;
  542. }
  543. void        SetWinPageCountProc (winHandle win, PageCountProcPtr f)
  544. {    if (win) (**win).PageCountProc = f;
  545. }
  546.  
  547.  
  548. OSErr        CallWinPagePrintProc (winHandle win, GrafPtr imagingPort, short pageNum)
  549. {    if ( (win) && ((**win).PagePrintProc) )
  550.         return ((**win).PagePrintProc)(win,imagingPort,pageNum) ;
  551.     else return eFatalNoProcPtr ;
  552. }
  553. void        SetWinPagePrintProc (winHandle win, PagePrintProcPtr f)
  554. {    if (win) (**win).PagePrintProc = f;
  555. }
  556.  
  557. #endif
  558.  
  559.  
  560.  
  561. OSErr        CallAllWinNullProcs( EventRecord *event )
  562. {
  563.     winHandle win ;
  564.     win = FrontWin() ;
  565.     while ( win ) {
  566.         CallWinNullProc( win, event ) ;
  567.         win = GetNextWin(win) ;
  568.     }
  569.     return noErr;
  570. }
  571.  
  572. OSErr        CallAllWinResumeProcs( Boolean resuming )
  573. {
  574.     winHandle win ;
  575.     win = FrontWin() ;
  576.     while ( win ) {
  577.         CallWinResumeProc( win, resuming ) ;
  578.         win = GetNextWin(win) ;
  579.     }
  580.     return noErr;
  581. }
  582.  
  583. OSErr        CallAllWinUpdateMenusProcs( void )
  584. {
  585.     winHandle win ;
  586.     win = FrontWin() ;
  587.     while ( win ) {
  588.         CallWinUpdateMenusProc( win ) ;
  589.         win = GetNextWin(win) ;
  590.     }
  591.     return noErr;
  592. }
  593.  
  594. OSErr        CallAllWinMenuProcs( long m, Boolean *didit )
  595. {
  596.     winHandle win ;
  597.     win = FrontWin() ;
  598.     while ( win && !(*didit) ) {
  599.         CallWinMenuProc( win, m, didit ) ;
  600.         if (!(*didit)) win = GetNextWin(win) ;
  601.     }
  602.     return noErr;
  603. }
  604.  
  605.  
  606. /**\
  607. |**| ==============================================================================
  608. |**| PRIVATE FUNCTIONS
  609. |**| ==============================================================================
  610. \**/
  611.  
  612.  
  613. /*------------------------------------------------------------------------------*\
  614.     SameFSSpec
  615.  *------------------------------------------------------------------------------*
  616.         This routine checks to FSSpec records to see if they are the same.
  617. \*------------------------------------------------------------------------------*/
  618. static Boolean    SameFSSpec ( FSSpec *fs1, FSSpec *fs2 )
  619. {
  620.     return ( (fs1->vRefNum == fs2->vRefNum) && 
  621.              (fs1->parID   == fs2->parID) && 
  622.                 (EqualString(fs1->name,fs2->name, false, true)) ) ;
  623. }
  624.