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