home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / DTS.Draw / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-05  |  13.9 KB  |  547 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 __TREEOBJ2__
  50. #include "TreeObj2.h"
  51. #endif
  52.  
  53. #ifndef __UTILITIES__
  54. #include "Utilities.h"
  55. #endif
  56.  
  57.  
  58.  
  59. /*****************************************************************************/
  60.  
  61.  
  62.  
  63. extern Boolean    gQuitApplication;
  64. extern Boolean    gHasAppleEvents;
  65. extern OSType    gAppWindowType;
  66.  
  67. extern Boolean    gLowOnMem;
  68. extern short    gDialogErr;
  69.  
  70.  
  71.  
  72. /*****************************************************************************/
  73. /*****************************************************************************/
  74.  
  75.  
  76.  
  77. /* •• Called by DTS.Lib..framework. •• */
  78.  
  79. /* Adjust the menu items.  We allow the DTS.Lib framework to do most of the work
  80. ** for us.  It will walk the menubar, and for each menu in the menubar, it will
  81. ** disable all of the menu items and then call the application at either
  82. ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
  83. ** or DialogAdjustMenuItems() (for modal dialogs).  The application's job is to then
  84. ** turn on menu items that should be enabled to match the current application state.
  85. ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
  86. ** file menu, and DoAdjustEditMenu() for the edit menu.  Any other menus that may
  87. ** be added to Wannabe have all menu items enabled.  This allows menus to be added
  88. ** to the running version of Wannabe and allows them to actually do something.
  89. ** If the top-most window is a dialog, then all menus are disabled except for the
  90. ** edit menu.  Various items in the edit menu are enabled, depending on if there
  91. ** is an active TextEdit control, and what is in the clipboard. */
  92.  
  93. #pragma segment Menu
  94. void    DoAdjustMenus(void)
  95. {
  96.     if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
  97.         DrawMenuBar();
  98. }
  99.  
  100.  
  101.  
  102. /*****************************************************************************/
  103.  
  104.  
  105.  
  106. /* This is called when an item is chosen from the menu bar (after calling
  107. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  108. ** It is good to have both the result of MenuSelect and MenuKey go to one
  109. ** routine like this to keep everything organized. */
  110.  
  111. #pragma segment Menu
  112. Boolean    DoMenuCommand(short menuID, short menuItem)
  113. {
  114.     short        saveMode, daRefNum, i;
  115.     Str255        str;
  116.     FileRecHndl    frHndl, newFrHndl;
  117.     WindowPtr    window, clipWind;
  118.     TreeObjHndl    root, cobj;
  119.     OSErr        err;
  120.     Boolean        handled;
  121.  
  122.     handled = true;
  123.  
  124.     window = FrontWindowOfType(kwIsDocument, true);
  125.     if (window)
  126.         frHndl = (FileRecHndl)GetWRefCon(window);
  127.             /* frHndl is valid only if it is one of our windows. */
  128.  
  129.     switch (menuID) {
  130.  
  131.         case mApple:
  132.             switch (menuItem) {
  133.                 case kStdAbout:        /* Bring up alert for About. */
  134.                     NewDocumentWindow(nil, 'ABOT', false);
  135.                     break;
  136.                 default:            /* All non-About items in this menu are DAs. */
  137.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, str);
  138.                     daRefNum = OpenDeskAcc(str);
  139.                     break;
  140.             }
  141.             break;
  142.  
  143.         case mFile:
  144.             switch (menuItem) {
  145.                 case kStdNew:
  146.                     gDialogErr = NewDocumentWindow(&frHndl, gAppWindowType, true);
  147.                     if (gDialogErr)
  148.                         NewDocumentWindow(nil, 'ERR#', false);
  149.                     break;
  150.                 case kStdOpen:
  151.                     err = OpenDocumentWindow(&frHndl, nil, fsRdWrPerm);
  152.                     if ((err) && (err != userCanceledErr)) {
  153.                         gDialogErr = err;
  154.                         NewDocumentWindow(nil, 'ERR#', false);
  155.                     }
  156.                     break;
  157.                 case kStdClose:
  158.                     DisposeOneWindow(window, kClose);
  159.                     break;
  160.                 case kStdSave:
  161.                 case kStdSaveAs:
  162.                     saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
  163.                     if ((*frHndl)->fileState.refNum == kInvalRefNum)
  164.                         saveMode = kSaveAs;
  165.                     err = SaveDocument(frHndl, window, saveMode);
  166.                     if ((err) && (err != userCanceledErr)) {
  167.                         gDialogErr = err;
  168.                         NewDocumentWindow(nil, 'ERR#', false);
  169.                     }
  170.                     break;
  171.                 case kDuplicate:
  172.                     gDialogErr = DuplicateDocument(frHndl, &newFrHndl);
  173.                     if (!gDialogErr) {
  174.                         gDialogErr = DoNewWindow(newFrHndl, nil,
  175.                                                  FrontWindowOfType(kwIsDocument, true),
  176.                                                  (WindowPtr)-1);
  177.                         if (gDialogErr)
  178.                             DisposeDocument(newFrHndl);
  179.                     }
  180.                     if (gDialogErr)
  181.                         NewDocumentWindow(nil, 'ERR#', false);
  182.                     break;
  183.                 case kStdPageSetup:
  184.                     DoSetCursor(&qd.arrow);
  185.                     PresentStyleDialog(frHndl);
  186.                     break;
  187.                 case kStdPrint:
  188.                     DoSetCursor(&qd.arrow);
  189.                     err = noErr;
  190.                     if (!(*frHndl)->d.doc.fhInfo.printRecValid)
  191.                         err = PresentStyleDialog(frHndl);
  192.                     if (!err) {
  193.                         err = PrintDocument(frHndl, true, true);
  194.                         PrintDocument(nil, false, false);
  195.                     }
  196.                     if ((err) && (err != userCanceledErr)) {
  197.                         gDialogErr = err;
  198.                         NewDocumentWindow(nil, 'ERR#', false);
  199.                     }
  200.                     break;
  201.                 case kStdQuit:
  202.                     gQuitApplication = DisposeAllWindows();
  203.                     break;
  204.             }
  205.             break;
  206.  
  207.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  208.             if (!IsDAWindow(window)) {
  209.                 if (menuItem == kClipboard) {
  210.                     clipWind = GetNextWindow(nil, kClipboardFileType);
  211.                     if (clipWind) {
  212.                         ShowHide(clipWind, !((WindowPeek)clipWind)->visible);
  213.                         CleanSendInFront(clipWind, FrontWindowOfType(kwIsDocument, false));
  214.                     }
  215.                     break;
  216.                 }
  217. #if VH_VERSION
  218.                 if (menuItem == kStdViewHier) {
  219.                     handled = false;        /* Let framework automatically open the view hierarchy window. */
  220.                     break;
  221.                 }
  222. #endif
  223.                 switch ((*frHndl)->fileState.sfType) {
  224.                     case kDocFileType:
  225.                         switch (menuItem) {
  226.                             case kStdUndo:
  227.                             case kStdRedo:
  228.                                 DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
  229.                                 break;
  230.                             case kStdCut:
  231.                             case kStdCopy:
  232.                             case kStdPaste:
  233.                                 DoClipboard(frHndl, menuItem);
  234.                                 break;
  235.                             case kStdClear:
  236.                                 DoDelete(frHndl);
  237.                                 break;
  238.                             case kSelectAll:
  239.                                 BeginContent(window);
  240.                                 root = (*frHndl)->d.doc.root;
  241.                                 for (i = (*root)->numChildren; i;) {
  242.                                     cobj = GetChildHndl(root, --i);
  243.                                     DoTreeObjMethod(cobj, SETSELECTMESSAGE, SELECTON);
  244.                                 }
  245.                                 EndContent(window);
  246.                                 break;
  247.                             default:
  248.                                 handled = false;
  249.                         }
  250.                         break;
  251. #if VH_VERSION
  252.                     case kViewHierFileType:
  253.                         BeginContent(window);
  254.                         switch (menuItem) {
  255.                             case kStdUndo:
  256.                                 CTEUndo();
  257.                                 break;
  258.                             case kStdCut:
  259.                             case kStdCopy:
  260.                             case kStdPaste:
  261.                             case kStdClear:
  262.                                 CTEClipboard(menuItem - kStdCut + 2);
  263.                                 break;
  264.                         }
  265.                         EndContent(window);
  266.                         break;
  267. #endif
  268.                 }
  269.             }
  270.             else SystemEdit(menuItem - 1);
  271.             break;
  272.  
  273.         case mArrange:
  274.             DoArrange(frHndl, menuItem);
  275.             break;
  276.  
  277.         case mOther:
  278.             switch (menuItem) {
  279.                 case kPenSize:
  280.                     NewDocumentWindow(&frHndl, 'PENS', false);
  281.                     break;
  282.                 case kBorderColor:
  283.                 case kContentColor:
  284.                     ChangeColor(frHndl, window, menuItem);
  285.                     break;
  286.             }
  287.             break;
  288.  
  289.         case mToolPalette:
  290.             switch (menuItem) {
  291.                 case -1:
  292.                     handled = false;
  293.                     break;
  294.                 default:
  295.                     SetPaletteTool(menuItem - 1);
  296.                     break;
  297.             }
  298.  
  299.         default:
  300.             handled = false;
  301.             break;
  302.     }
  303.  
  304.     return(handled);
  305. }
  306.  
  307.  
  308.  
  309. /*****************************************************************************/
  310.  
  311.  
  312.  
  313. #pragma segment Menu
  314. Boolean    DoAdjustFileMenu(WindowPtr window)
  315. {
  316.     MenuHandle    menu;
  317.     FileRecHndl    frHndl;
  318.     short        enableItem;
  319.  
  320.     menu = GetMenuHandle(mFile);
  321.  
  322.     EnableItem(menu, UnmapMItem(mFile, kStdQuit));            /* Gotta be able to quit. */
  323.  
  324.     if (IsDAWindow(window)) {
  325.         EnableItem(menu, UnmapMItem(mFile, kStdClose));        /* Let DAs do a close from the menu. */
  326.         return(false);
  327.     }
  328.  
  329.     if (!gLowOnMem) {
  330.         EnableItem(menu, UnmapMItem(mFile, kStdNew));
  331.         EnableItem(menu, UnmapMItem(mFile, kStdOpen));
  332.     }
  333.  
  334.     window = FrontWindowOfType(kwIsDocument, true);
  335.     if (window) {
  336.         EnableItem(menu, UnmapMItem(mFile, kStdClose));
  337.         frHndl = (FileRecHndl)GetWRefCon(window);
  338.         if ((*frHndl)->fileState.sfType == kDocFileType) {
  339.             enableItem = GetWindowDirty(window);
  340.             if ((*frHndl)->fileState.refNum == kInvalRefNum)
  341.                 enableItem = true;
  342.             if (enableItem)
  343.                 EnableItem(menu, UnmapMItem(mFile, kStdSave));
  344.             EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
  345.             EnableItem(menu, UnmapMItem(mFile, kDuplicate));
  346.         }
  347.         EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
  348.         EnableItem(menu, UnmapMItem(mFile, kStdPrint));
  349.     }
  350.  
  351.     return(false);
  352. }
  353.  
  354.  
  355.  
  356. /*****************************************************************************/
  357.  
  358.  
  359.  
  360. #pragma segment Menu
  361. Boolean    DoAdjustEditMenu(WindowPtr window)
  362. {
  363.     MenuHandle        menu;
  364.     Boolean            menuEnabled;
  365.     FileRecHndl        frHndl, frClip;
  366.     WindowPtr        clipWind;
  367.     short            i, undoDepth, numUndos;
  368.     Boolean            haveDocument;
  369.     Str255            str;
  370.     StringPtr        cptr;
  371.     TreeObjHndl        root;
  372.     static Str32    clipboardText[3];
  373.  
  374.     menu = GetMenuHandle(mEdit);
  375.  
  376.     if (IsDAWindow(window)) {
  377.         EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
  378.         EnableItem(menu, UnmapMItem(mEdit, kStdCut));
  379.         EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
  380.         EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
  381.         EnableItem(menu, UnmapMItem(mEdit, kStdClear));
  382.         return(false);
  383.     }
  384.  
  385.     haveDocument = (window = FrontWindowOfType(kwIsDocument, true)) ? true : false;
  386.     frHndl = (haveDocument) ? (FileRecHndl)GetWRefCon(window) : nil;
  387.  
  388. #if VH_VERSION
  389.     if (haveDocument)
  390.         EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
  391. #endif
  392.  
  393.     EnableItem(menu, UnmapMItem(mEdit, kClipboard));
  394.     if (!clipboardText[0][0]) {
  395.         GetMenuItemText(menu, UnmapMItem(mEdit, kClipboard), str);
  396.         p2c(str);
  397.         for (i = 0; str[i]; ++i)
  398.             if (str[i] == ',')
  399.                 str[i] = 0;
  400.         for (cptr = str, i = 0; i < 3; ++i) {
  401.             c2p((char *)cptr);
  402.             pcpy(clipboardText[i], cptr);
  403.             cptr += (cptr[0] + 1);
  404.         }
  405.     }
  406.     clipWind = GetNextWindow(nil, kClipboardFileType);
  407.     if (clipWind) {
  408.         i = (((WindowPeek)clipWind)->visible) ? 1 : 0;
  409.         pcpy(str, clipboardText[i]);
  410.         pcat(str, clipboardText[2]);
  411.         SetMenuItemText(menu, UnmapMItem(mEdit, kClipboard), str);
  412.     }
  413.  
  414.     if (haveDocument) {
  415.         root = (*frHndl)->d.doc.root;
  416.         switch ((*frHndl)->fileState.sfType) {
  417.             case kDocFileType:
  418.                 GetUndoInfo(frHndl, &undoDepth, &numUndos);
  419.                 if (undoDepth)
  420.                     EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
  421.                 if (undoDepth < numUndos)
  422.                     EnableItem(menu, UnmapMItem(mEdit, kStdRedo));
  423.                 if (mDerefRoot(root)->numSelected) {
  424.                     EnableItem(menu, UnmapMItem(mEdit, kStdCut));
  425.                     EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
  426.                     EnableItem(menu, UnmapMItem(mEdit, kStdClear));
  427.                 }
  428.                 if (clipWind) {
  429.                     frClip = (FileRecHndl)GetWRefCon(clipWind);
  430.                     if ((*((*frClip)->d.doc.root))->numChildren)
  431.                         EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
  432.                 }
  433.                 if (mDerefRoot(root)->numSelected < (*root)->numChildren)
  434.                     EnableItem(menu, UnmapMItem(mEdit, kSelectAll));
  435.                 break;
  436. #if VH_VERSION
  437.             case kViewHierFileType:
  438.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  439.                                                  UnmapMItem(mEdit, kStdCut));
  440.                 break;
  441. #endif
  442.             default:
  443.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  444.                                                  UnmapMItem(mEdit, kStdCut));
  445.                 break;
  446.         }
  447.     }
  448.  
  449.     return(false);
  450. }
  451.  
  452.  
  453.  
  454. /*****************************************************************************/
  455.  
  456.  
  457.  
  458. #pragma segment Menu
  459. Boolean    DoAdjustArrangeMenu(WindowPtr window)
  460. {
  461.     MenuHandle        menu;
  462.     short            i, numChildren, numSelected, cnum, flag;
  463.     Boolean            haveDocument;
  464.     FileRecHndl        frHndl;
  465.     TreeObjHndl        root, chndl;
  466.  
  467.     if (IsDAWindow(window)) return(false);
  468.  
  469.     menu = GetMenuHandle(mArrange);
  470.  
  471.     haveDocument = (window = FrontWindowOfType(kwIsDocument, true)) ? true : false;
  472.     frHndl = (haveDocument) ? (FileRecHndl)GetWRefCon(window) : nil;
  473.  
  474.     for (i = kMoveForward; i <= kUngroup; ++i) CheckItem(menu, i, false);
  475.     if (haveDocument) {
  476.         if ((*frHndl)->fileState.sfType == kDocFileType) {
  477.             root        = (*frHndl)->d.doc.root;
  478.             numChildren = (*root)->numChildren;
  479.             numSelected = mDerefRoot(root)->numSelected;
  480.             if (numSelected) {
  481.                 for (cnum = (*root)->numChildren; cnum;) {
  482.                     chndl = GetChildHndl(root, --cnum);
  483.                     if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
  484.                         if ((*chndl)->type == GROUPOBJ)
  485.                             EnableItem(menu, kUngroup);
  486.                 }
  487.             }
  488.             if ((numSelected) && (numChildren > 1)) {
  489.                 if ((flag = numSelected) == 1) {
  490.                     chndl = GetChildHndl(root, 0);
  491.                     if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
  492.                         --flag;
  493.                 }
  494.                 if (flag) {
  495.                     EnableItem(menu, kMoveForward);
  496.                     EnableItem(menu, kMoveToFront);
  497.                 }
  498.                 if ((flag = numSelected) == 1) {
  499.                     chndl = GetChildHndl(root, -1);        /* Last child. */
  500.                     if (DoTreeObjMethod(chndl, GETSELECTMESSAGE, 0))
  501.                         --flag;
  502.                 }
  503.                 if (flag) {
  504.                     EnableItem(menu, kMoveBackward);
  505.                     EnableItem(menu, kMoveToBack);
  506.                 }
  507.             }
  508.             if (numSelected > 1)
  509.                 EnableItem(menu, kGroup);
  510.         }
  511.     }
  512.  
  513.     return(false);
  514. }
  515.  
  516.  
  517.  
  518. /*****************************************************************************/
  519.  
  520.  
  521.  
  522. #pragma segment Menu
  523. Boolean    DoAdjustOtherMenu(WindowPtr window)
  524. {
  525.     MenuHandle        menu;
  526.     Boolean            haveDocument;
  527.     FileRecHndl        frHndl;
  528.  
  529.     if (IsDAWindow(window)) return(false);
  530.  
  531.     menu = GetMenuHandle(mOther);
  532.  
  533.     haveDocument = (window = FrontWindowOfType(kwIsDocument, true)) ? true : false;
  534.     frHndl = (haveDocument) ? (FileRecHndl)GetWRefCon(window) : nil;
  535.  
  536.     if (haveDocument) {
  537.         EnableItem(menu, kPenSize);
  538.         EnableItem(menu, kBorderColor);
  539.         EnableItem(menu, kContentColor);
  540.     }
  541.  
  542.     return(false);
  543. }
  544.  
  545.  
  546.  
  547.