home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / pbClock / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  10.0 KB  |  373 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Menu.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for application.            */
  28.  
  29. #ifndef __DESK__
  30. #include <Desk.h>
  31. #endif
  32.  
  33. #ifndef __ERRORS__
  34. #include <Errors.h>
  35. #endif
  36.  
  37. #ifndef __MEMORY__
  38. #include <Memory.h>
  39. #endif
  40.  
  41. #ifndef __MENUS__
  42. #include <Menus.h>
  43. #endif
  44.  
  45. #ifndef __TOOLUTILS__
  46. #include <ToolUtils.h>
  47. #endif
  48.  
  49. #ifndef __UTILITIES__
  50. #include "Utilities.h"
  51. #endif
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. extern Boolean    gQuitApplication;
  60. extern Boolean    gHasAppleEvents;
  61. extern OSType    gAppWindowType;
  62.  
  63. extern Boolean    gLowOnMem;
  64. extern short    gDialogErr;
  65.  
  66.  
  67.  
  68. /*****************************************************************************/
  69. /*****************************************************************************/
  70.  
  71.  
  72.  
  73. /* •• Called by DTS.Lib..framework. •• */
  74.  
  75. /* Adjust the menu items.  We allow the DTS.Lib framework to do most of the work
  76. ** for us.  It will walk the menubar, and for each menu in the menubar, it will
  77. ** disable all of the menu items and then call the application at either
  78. ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
  79. ** or DialogAdjustMenuItems() (for modal dialogs).  The application's job is to then
  80. ** turn on menu items that should be enabled to match the current application state.
  81. ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
  82. ** file menu, and DoAdjustEditMenu() for the edit menu.  Any other menus that may
  83. ** be added to Wannabe have all menu items enabled.  This allows menus to be added
  84. ** to the running version of Wannabe and allows them to actually do something.
  85. ** If the top-most window is a dialog, then all menus are disabled except for the
  86. ** edit menu.  Various items in the edit menu are enabled, depending on if there
  87. ** is an active TextEdit control, and what is in the clipboard. */
  88.  
  89. #pragma segment Menu
  90. void    DoAdjustMenus(void)
  91. {
  92.     if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
  93.         DrawMenuBar();
  94. }
  95.  
  96.  
  97.  
  98. /*****************************************************************************/
  99.  
  100.  
  101.  
  102. /* This is called when an item is chosen from the menu bar (after calling
  103. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  104. ** It is good to have both the result of MenuSelect and MenuKey go to one
  105. ** routine like this to keep everything organized. */
  106.  
  107. #pragma segment Menu
  108. Boolean    DoMenuCommand(short menuID, short menuItem)
  109. {
  110.     short        undoDepth, numUndos, saveMode, daRefNum;
  111.     Str255        daName;
  112.     FileRecHndl    frHndl;
  113.     WindowPtr    window;
  114.     TEHandle    te;
  115.     OSErr        err;
  116.     Boolean        handled;
  117.     RGBColor    oldc, newc;
  118.  
  119.     handled = true;
  120.  
  121.     window = FrontWindow();
  122.     if (window)
  123.         frHndl = (FileRecHndl)GetWRefCon(window);
  124.             /* frHndl is valid only if it is one of our windows. */
  125.  
  126.     switch (menuID) {
  127.  
  128.         case mApple:
  129.             switch (menuItem) {
  130.                 case kStdAbout:        /* Bring up alert for About. */
  131.                     NewDocumentWindow(nil, 'ABOT', false);
  132.                     break;
  133.                 default:            /* All non-About items in this menu are DAs. */
  134.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  135.                     daRefNum = OpenDeskAcc(daName);
  136.                     break;
  137.             }
  138.             break;
  139.  
  140.         case mFile:
  141.             switch (menuItem) {
  142.                 case kStdNew:
  143.                     gDialogErr = NewDocumentWindow(&frHndl, gAppWindowType, true);
  144.                     if (gDialogErr)
  145.                         NewDocumentWindow(nil, 'ERR#', false);
  146.                     break;
  147.                 case kStdOpen:
  148.                     err = OpenDocumentWindow(&frHndl, nil, fsRdWrPerm);
  149.                     if ((err) && (err != userCanceledErr)) {
  150.                         gDialogErr = err;
  151.                         NewDocumentWindow(nil, 'ERR#', false);
  152.                     }
  153.                     break;
  154.                 case kStdClose:
  155.                     if (IsAppWindow(window)) {
  156.                         window = FrontWindowOfType(kwIsDocument, true);
  157.                         if (window)
  158.                             DisposeOneWindow(window, kClose);
  159.                     }
  160.                     else
  161.                         DisposeOneWindow(window, kClose);        /* Dispose of DA window. */
  162.                     break;
  163.                 case kStdSave:
  164.                 case kStdSaveAs:
  165.                     saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
  166.                     if ((*frHndl)->fileState.refNum == kInvalRefNum)
  167.                         saveMode = kSaveAs;
  168.                     err = SaveDocument(frHndl, window, saveMode);
  169.                     if ((err) && (err != userCanceledErr)) {
  170.                         gDialogErr = err;
  171.                         NewDocumentWindow(nil, 'ERR#', false);
  172.                     }
  173.                     break;
  174.                 case kStdPageSetup:
  175.                     DoSetCursor(&qd.arrow);
  176.                     PresentStyleDialog(frHndl);
  177.                     break;
  178.                 case kStdPrint:
  179.                     DoSetCursor(&qd.arrow);
  180.                     err = noErr;
  181.                     if (!(*frHndl)->d.doc.fhInfo.printRecValid)
  182.                         err = PresentStyleDialog(frHndl);
  183.                     if (!err) {
  184.                         err = PrintDocument(frHndl, true, true);
  185.                         PrintDocument(nil, false, false);
  186.                     }
  187.                     if ((err) && (err != userCanceledErr)) {
  188.                         gDialogErr = err;
  189.                         NewDocumentWindow(nil, 'ERR#', false);
  190.                     }
  191.                     break;
  192.                 case kStdQuit:
  193.                     gQuitApplication = DisposeAllWindows();
  194.                     break;
  195.                 default:
  196.                     handled = false;
  197.                     break;
  198.             }
  199.             break;
  200.  
  201.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  202. #if VH_VERSION
  203.             if (menuItem == kStdViewHier) {
  204.                 handled = false;
  205.                 break;
  206.             }
  207. #endif
  208.             if (IsAppWindow(window)) {
  209.                 switch (menuItem) {
  210.                     case kStdUndo:
  211.                     case kStdRedo:
  212.                     case kStdCut:
  213.                     case kStdCopy:
  214.                     case kStdPaste:
  215.                     case kStdClear:
  216.                         switch ((*frHndl)->fileState.sfType) {
  217.                                 /* This is written with the assumption that document types
  218.                                 ** that demand specific code will be added.  The below “if”
  219.                                 ** illustrates how to handle the edit menu for windows that
  220.                                 ** have an active TextEdit control.  The “else” shows a typical
  221.                                 ** undo/redo scenario for applications that are using the
  222.                                 ** hierarchical document package.  The clipboard features
  223.                                 ** are of course document-dependent, so a sample hasn't been
  224.                                 ** implemented here.  For a sample, see DTS.Draw. */
  225.                             default:
  226.                                 te = CTEFindActive(window);
  227.                                 if (te) {
  228.                                     if (gQDVersion) {
  229.                                         GetBackColor(&oldc);
  230.                                         newc.red = newc.green = newc.blue = 0xFFFF;
  231.                                         RGBBackColor(&newc);
  232.                                             /* The window color is non-white, but
  233.                                             ** the control area containing TEControls
  234.                                             ** is white.  Therefore we have to set
  235.                                             ** the backColor to white before doing
  236.                                             ** TEControl operations, or else we
  237.                                             ** will get some weird results. */
  238.                                     }
  239.                                     if ((*te)->viewRect.left < -8192)
  240.                                         BeginFrame(window);
  241.                                     else
  242.                                         BeginContent(window);
  243.                                     if (menuItem == kStdUndo)
  244.                                         CTEUndo();
  245.                                     else
  246.                                         CTEClipboard(menuItem - kStdCut + 2);
  247.                                     EndContent(window);
  248.                                     if (gQDVersion)
  249.                                         RGBBackColor(&oldc);
  250.                                 }
  251.                                 else {
  252.                                     if (menuItem <= kStdRedo) {
  253.                                         if (!UnmapMItem(mEdit, kStdUndo)) {
  254.                                             GetUndoInfo(frHndl, &undoDepth, &numUndos);
  255.                                             DoUndoTask((*frHndl)->d.doc.root, 1 - undoDepth, true);
  256.                                         }
  257.                                         else DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
  258.                                     }
  259.                                     else {
  260.                                         /* Handle rest of edit menu here. */
  261.                                     }
  262.                                 }
  263.                                 break;
  264.                         }
  265.                         break;
  266.                 }
  267.             }
  268.             else SystemEdit(menuItem - 1);
  269.             break;
  270.  
  271.         default:
  272.             handled = false;
  273.             break;
  274.  
  275.     }
  276.  
  277.     return(handled);
  278. }
  279.  
  280.  
  281.  
  282. /*****************************************************************************/
  283.  
  284.  
  285.  
  286. #pragma segment Menu
  287. Boolean    DoAdjustFileMenu(WindowPtr window)
  288. {
  289.     MenuHandle    menu;
  290.     FileRecHndl    frHndl;
  291.     short        enableItem;
  292.  
  293.     menu = GetMenuHandle(mFile);
  294.     EnableItem(menu, UnmapMItem(mFile, kStdQuit));            /* Gotta be able to quit. */
  295.  
  296.     if (IsDAWindow(window)) {
  297.         EnableItem(menu, UnmapMItem(mFile, kStdClose));        /* Let DAs do a close from the menu. */
  298.         return(false);
  299.     }
  300.  
  301.     if (!gLowOnMem) {
  302.         EnableItem(menu, UnmapMItem(mFile, kStdNew));
  303.         EnableItem(menu, UnmapMItem(mFile, kStdOpen));
  304.     }
  305.  
  306.     window = FrontWindowOfType(kwIsDocument, true);
  307.     if (window) {
  308.         EnableItem(menu, UnmapMItem(mFile, kStdClose));
  309.         frHndl = (FileRecHndl)GetWRefCon(window);
  310.         if ((*frHndl)->fileState.sfType == kDocFileType) {
  311.             enableItem = GetWindowDirty(window);
  312.             if ((*frHndl)->fileState.refNum == kInvalRefNum)
  313.                 enableItem = true;
  314.             if (enableItem)
  315.                 EnableItem(menu, UnmapMItem(mFile, kStdSave));
  316.             EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
  317.         }
  318.         EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
  319.         EnableItem(menu, UnmapMItem(mFile, kStdPrint));
  320.     }
  321.  
  322.     return(false);
  323. }
  324.  
  325.  
  326.  
  327. /*****************************************************************************/
  328.  
  329.  
  330.  
  331. #pragma segment Menu
  332. Boolean    DoAdjustEditMenu(WindowPtr window)
  333. {
  334.     MenuHandle        menu;
  335.     Boolean            menuEnabled;
  336.     FileRecHndl        frHndl;
  337.  
  338.     menu = GetMenuHandle(mEdit);
  339.  
  340.     if (IsDAWindow(window)) {
  341.         EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
  342.         EnableItem(menu, UnmapMItem(mEdit, kStdCut));
  343.         EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
  344.         EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
  345.         EnableItem(menu, UnmapMItem(mEdit, kStdClear));
  346.         return(false);
  347.     }
  348.  
  349.     if (IsAppWindow(window)) {
  350.         frHndl = (FileRecHndl)GetWRefCon(window);
  351.         switch ((*frHndl)->fileState.sfType) {
  352. #if VH_VERSION
  353.             case kViewHierFileType:
  354.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  355.                                                  UnmapMItem(mEdit, kStdCut));
  356.                 break;
  357. #endif
  358.             default:
  359. #if VH_VERSION
  360.                 EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
  361. #endif
  362.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  363.                                                  UnmapMItem(mEdit, kStdCut));
  364.                 break;
  365.         }
  366.     }
  367.  
  368.     return(false);
  369. }
  370.  
  371.  
  372.  
  373.