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 / appMenus.c < prev    next >
Encoding:
Text File  |  1997-06-13  |  8.4 KB  |  270 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 1994, Apple Computer,Inc
  8. // 
  9. // Application menu handling code such as dimming menu items and 
  10. // dispatching items.  The dispatcher first calls the the MenuProcPtr
  11. // of the frontmost window to see if it could handle the event.  Any
  12. // items not handled by the front windo are handled here.
  13. //
  14. // 9/13/94    nick    first cut
  15. // 9/13/94    nick    implement commands
  16. // 12/13/94    david    several modifications
  17. // 8/23/95    david    added generic prining to the framework
  18.  
  19.  
  20. #include <Types.h>
  21. #include <StandardFile.h>
  22. #include <SegLoad.h>
  23. #include <Devices.h>
  24.  
  25. #include "appGlobals.h"
  26. #include "appMain.h"
  27. #include "appMenus.h"
  28. #include "appErrors.h"
  29. #include "appPrint.h"
  30. #include "appAEvts.h"
  31.  
  32. #include "win.h"
  33. #include "winTables.h"
  34.  
  35.  
  36. /**\
  37. |**| ==============================================================================
  38. |**| PRIVATE FUNCTION PROTOTYPES
  39. |**| ==============================================================================
  40. \**/
  41. void DoAppNewCommand        ( void ) ;
  42. void DoAppOpenCommand        ( void ) ;
  43. void DoAppCloseCommand        ( winHandle win ) ;
  44. void DoAppQuitCommand        ( void ) ;
  45.  
  46.  
  47. /**\
  48. |**| ==============================================================================
  49. |**| PUBLIC FUNCTIONS
  50. |**| ==============================================================================
  51. \**/
  52.  
  53.  
  54. /*------------------------------------------------------------------------------*\
  55.     dimAllMenus
  56.  *------------------------------------------------------------------------------*
  57.         This routine dims all menus and menu items in the menu bar
  58.         It is called by:
  59.             InitMenuBar() which is called when the app is intited,
  60.             DoActivateEvent() which is called whenever a window is brought to front,
  61.             AdjustMenusForPrinting() which is called before GX print dialogs, and
  62.             CloseProcPtrs which are called whenever windows are closed 
  63. \*------------------------------------------------------------------------------*/
  64. void dimAllMenus ( void )
  65. {
  66.     short            menu, item, totalItems ;
  67.     MenuHandle        mhdl ;
  68.  
  69.     for ( menu=mFirstMenu; menu <= mLastSubMenu; menu++ )
  70.     {
  71.         mhdl = GetMenuHandle( menu ) ;
  72.         DisableItem( mhdl, kWholeMenu ) ;
  73.  
  74.         if (menu==mApple)
  75.             totalItems = 1 ;
  76.         else
  77.             totalItems = CountMItems(mhdl) ;
  78.  
  79.         for ( item=1; item<=totalItems; item++ )
  80.             DisableItem( mhdl, item ) ;
  81.     }
  82. }
  83.         
  84.         
  85. /*------------------------------------------------------------------------------*\
  86.     DoAppAdjustMenus
  87.  *------------------------------------------------------------------------------*
  88.         This routine enables any menus and menu items in the menu bar
  89.         which are able to be handled.
  90.         It is called by:
  91.             InitMenuBar() which is called when the app is intited,
  92.             DoActivateEvent() which is called whenever a window is brought to front, and
  93.             CloseProcPtrs which are called whenever windows are closed 
  94. \*------------------------------------------------------------------------------*/
  95. void DoAppAdjustMenus ( void ) 
  96. {
  97.     MenuHandle        theMenu ;
  98.     short            i, openableTypes ;
  99.  
  100.     dimAllMenus() ;
  101.  
  102.     // let the windows enable the items they can handle
  103.     (void) CallAllWinUpdateMenusProcs() ;
  104.  
  105.     // Call default UpdateMenusProc
  106.     gDefaulltUpdateMenusProc( nil ) ;
  107.  
  108.     // let the famework enable the items it can handle
  109.         for ( i=openableTypes=0; i<gAllocProcMapCount; i++ )
  110.             if (gAllocProcMap[i].type != nil )
  111.                     openableTypes++;
  112.  
  113.         theMenu = GetMenuHandle ( mFile ) ;
  114.         EnableItem ( theMenu, kWholeMenu ) ;
  115.         if (openableTypes) EnableItem ( theMenu, iOpen ) ;
  116.         EnableItem ( theMenu, iQuit ) ;
  117.     
  118.     DrawMenuBar() ;
  119. }
  120.  
  121.  
  122. /*------------------------------------------------------------------------------*\
  123.     HandleMenuCommand
  124.  *------------------------------------------------------------------------------*
  125.         This routine handles all commands generated by mouse event or
  126.         by command key equivalent.  This routine first calls the the MenuProcPtr
  127.         of the frontmost window to see if it could handle the event.  After that
  128.         it hadles any menu items which the application is responsible for.
  129.         It is called by:
  130.             DoMouseDownEvent() which is called in response to mouse events, and
  131.             DoKeyDownEvent() which is called in response to keyboard events
  132. \*------------------------------------------------------------------------------*/
  133. void HandleMenuCommand ( long menuResult )
  134. {
  135.     short            menuID ;
  136.     short            menuItem ;
  137.     Str255            daName ;
  138.     winHandle        win ;
  139.     Boolean            didit = false ;
  140.  
  141.     // see if any of the windows can handle the event
  142.     (void) CallAllWinMenuProcs( menuResult, &didit) ;
  143.     if (didit) return ; // check to see if a window handled it 
  144.  
  145.     // Call default MenuProc
  146.     gDefaulltMenuProc(nil, menuResult, &didit) ;
  147.     if (didit) return ; // check to see if the default handled it 
  148.  
  149.     // if that fails, let the famework do what it can
  150.     win = GetWindowWinHandle( FrontWindow() ) ;
  151.     menuID = HiWrd(menuResult) ;
  152.     menuItem = LoWrd(menuResult) ;
  153.     switch ( menuID )
  154.     {
  155.         case mApple:
  156.             GetMenuItemText(GetMenuHandle(mApple), menuItem, daName) ;
  157.             OpenDeskAcc(daName) ;
  158.             break;
  159.             
  160.         case mFile:
  161.             switch ( menuItem )
  162.             {
  163.                 case iOpen:                // open an existing file
  164.                     DoAppOpenCommand() ;
  165.                     break ;
  166.                     
  167.                 case iClose:            // close an open window/document
  168.                     DoAppCloseCommand( win ) ;
  169.                     break ;
  170.  
  171. #ifndef PIGS_SHELL_NOPRINT
  172.                 case iPageSetup:        // std page setup dialog
  173.                     DoAppPageSetupCommand( win ) ;
  174.                     break ;
  175.  
  176.                 case iPrint:            // std print job dialog
  177.                     DoAppPrintCommand( win ) ;
  178.                     break ;
  179.  
  180.                 case iPrintOne:            // std print job dialog
  181.                     DoAppPrintOneCommand( win ) ;
  182.                     break ;
  183. #endif
  184.                 case iQuit:
  185.                     DoAppQuitCommand() ;// send ourselves a quit event
  186.                     break ;
  187.             }
  188.             break ;            
  189.     }
  190.     HiliteMenu(0) ;        // Unhighlight whatever MenuSelect or MenuKey hilited
  191. }
  192.  
  193.  
  194. /**\
  195. |**| ==============================================================================
  196. |**| PRIVATE FUNCTIONS
  197. |**| ==============================================================================
  198. \**/
  199.  
  200.  
  201. /*------------------------------------------------------------------------------*\
  202.     DoAppOpenCommand
  203.  *------------------------------------------------------------------------------*
  204.         This routine handles the File:Open command for the application.
  205.         It calls StandardGetFile() and with the resulting FSSpec, calls
  206.         SendODOC() to force the app to send an ODOC event to itself. 
  207.         It is called by:
  208.             HandleMenuCommand() which handles all menu events.
  209. \*------------------------------------------------------------------------------*/
  210. static void DoAppOpenCommand ( void ) 
  211. {
  212.     SFTypeList            *myTypesPtr ;    
  213.     StandardFileReply    theSFReply ;    // Get the file name to open
  214.     short                i, count ;
  215.     OSErr                err ;
  216.  
  217.     myTypesPtr = (SFTypeList*) NewPtr( (Size)( sizeof(long) * gAllocProcMapCount) ) ;
  218.     err = MemError();
  219.     WarnIfErr( err ) ;
  220.     if (err) return ;
  221.  
  222.     for ( i=count=0; i<gAllocProcMapCount; i++ )
  223.         if (gAllocProcMap[i].type != nil )
  224.             (*myTypesPtr)[count++] = gAllocProcMap[i].type ;
  225.  
  226.     StandardGetFile( nil, count, *myTypesPtr, &theSFReply ) ;
  227.     
  228.     DisposePtr( (Ptr)myTypesPtr ) ;
  229.  
  230.     if(theSFReply.sfGood)                 // if the user did not cancel
  231.         SendODOC( &theSFReply.sfFile ) ;// send ODOC event to ourselves:
  232.  
  233.     return ;
  234. }
  235.  
  236.  
  237.  
  238. /*------------------------------------------------------------------------------*\
  239.     DoAppCloseCommand
  240.  *------------------------------------------------------------------------------*
  241.         This routine handles the File:Close command for the application.
  242.         It calls DisposeWinHandle() and also returns all the menus
  243.         to the default state by calling DoAppAdjustMenus().
  244.         This is needed because, if the window being closed is the only window,
  245.         then there is not another window to trigger an activate event. 
  246.         It is called by:
  247.             HandleMenuCommand() which handles all menu events.
  248. \*------------------------------------------------------------------------------*/
  249. static void DoAppCloseCommand ( winHandle win ) 
  250. {
  251.     CallWinCloseProc( win ) ;
  252.     DoAppAdjustMenus() ;
  253. }
  254.  
  255.  
  256. /*------------------------------------------------------------------------------*\
  257.     DoAppQuitCommand
  258.  *------------------------------------------------------------------------------*
  259.         This routine handles the File:Quit command for the application.
  260.         It calls StandardGetFile() and with the resulting FSSpec, calls
  261.         It calls SendQUIT() to force the app to send an QUIT event to itself. 
  262.         It is called by:
  263.             HandleMenuCommand() which handles all menu events.
  264. \*------------------------------------------------------------------------------*/
  265. static void DoAppQuitCommand ( void ) 
  266. {
  267.     SendQUIT() ;                // send ourselves a quit event
  268.     return ;
  269. }
  270.