home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Demos / Tools / AppMaker / Examples / pre-built AMReminder / Procedural / MainMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-04  |  4.0 KB  |  209 lines  |  [TEXT/MMCC]

  1. /* MainMenu.c */
  2. /* Created 01/01/95 12:01 PM by AppMaker */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Dialogs.h>
  8. #include <Events.h>
  9. #include <Lists.h>
  10. #include <LowMem.h>
  11. #include <Menus.h>
  12. #include <TextEdit.h>
  13. #include "Globals.h"
  14. #include "ResourceDefs.h"
  15. #include "Miscellany.h"
  16.  
  17. #include "FileM.h"
  18. #include "EditM.h"
  19. #include "RemindM.h"
  20.  
  21. #include <Desk.h>
  22. #include <ToolUtils.h>
  23. #include "MainMenu.h"
  24.  
  25.  
  26. static void Enable            (short            itemNr,
  27.                              Boolean        enabled);
  28. static void EnableTitle        (MenuHandle        menu,
  29.                              Boolean        enabled);
  30. static void DoApple            (short            itemNr);
  31.  
  32. /*----------*/
  33. void InitTitles (void)
  34. {
  35.     InitFileM ();
  36.     InitEditM ();
  37.     InitRemindM ();
  38.  
  39. } /*InitTitles*/
  40.  
  41. /*----------*/
  42. void LoadMenus (void)
  43. {
  44.     AppleMenu    = GetMenu (MENU_Apple);
  45.     FailNilResource ((Handle)AppleMenu);
  46.     AddResMenu (AppleMenu, 'DRVR');
  47.     FileMenu    = GetMenu (MENU_File);
  48.     EditMenu    = GetMenu (MENU_Edit);
  49.     RemindMenu    = GetMenu (MENU_Remind);
  50.     SoundMenu    = GetMenu (MENU_Sound);
  51.     AddResMenu (SoundMenu, 'snd ');
  52.  
  53.     InsertMenu (AppleMenu, 0);
  54.     InsertMenu (FileMenu, 0);
  55.     InsertMenu (EditMenu, 0);
  56.     InsertMenu (RemindMenu, 0);
  57.  
  58.     DrawMenuBar ();
  59. } /*LoadMenus*/
  60.  
  61. /*----------*/
  62. static void DoAbout (void)
  63. {
  64.     short            itemNr;
  65.  
  66.     itemNr = Alert (AboutID, nil);
  67. } /*DoAbout*/
  68.  
  69.  
  70. /*----------*/
  71. static void DoApple (short     itemNr)
  72. {
  73.     Str255            name;
  74.     short            refNum;
  75.  
  76.     switch (itemNr) {
  77.         case AppleAbout:
  78.                 DoAbout ();
  79.             break;
  80.  
  81.         default:
  82.                 GetItem (AppleMenu, itemNr, name);
  83.                 refNum = OpenDeskAcc (name);
  84.             break;
  85.     } /*switch*/
  86. } /*DoApple*/
  87.  
  88. /*----------*/
  89. void DoMenu (long        menuChoice)
  90. {
  91.     short            menuID;
  92.     short            itemNr;
  93.  
  94.     menuID = HiWord (menuChoice);
  95.     itemNr = LoWord (menuChoice);
  96.  
  97.     switch (menuID) {
  98.     case 0:
  99.             /*Do nothing*/;
  100.         break;
  101.     case MENU_Apple:
  102.             DoApple (itemNr);
  103.         break;
  104.     case MENU_File:
  105.             DoFile (itemNr);
  106.         break;
  107.     case MENU_Edit:
  108.             DoEdit (itemNr);
  109.         break;
  110.     case MENU_Remind:
  111.             DoRemind (itemNr);
  112.         break;
  113.  
  114.     } /*switch*/
  115.  
  116.     HiliteMenu (0);
  117. } /*DoMenu*/
  118.  
  119. /*----------*/
  120. MenuHandle        menu;
  121. Boolean            menuBarChanged;
  122.  
  123. /*----------*/
  124. static void Enable    (short        itemNr,
  125.                      Boolean    enabled)
  126. {
  127.     if (enabled) {
  128.         EnableItem  (menu, itemNr);
  129.     } else {
  130.         DisableItem (menu, itemNr);
  131.     }
  132. } /*Enable*/
  133.  
  134. /*----------*/
  135. static void EnableTitle    (MenuHandle        menu,
  136.                          Boolean        enabled)
  137. {
  138.     if (enabled != ((**menu).enableFlags & 1)) {
  139.         menuBarChanged = true;
  140.     }
  141.     if (enabled) {
  142.         EnableItem  (menu, 0);
  143.     } else {
  144.         DisableItem (menu, 0);
  145.     }
  146. } /*EnableTitle*/
  147.  
  148. /*----------*/
  149. void UpdateMenus (void)
  150. {
  151.     WindowPeek        frontPeek;
  152.     Boolean            isFront;            /*is there a front window?*/
  153.     Boolean            isCur;                /*is there a current window?*/
  154.     Boolean            isDirty;            /*is it dirty?*/
  155.     Boolean            hasFile;            /*does it have a file?*/
  156.     Boolean            isSelected;            /*is anything selected?*/
  157.     Boolean            isDesk;                /*is the front window a desk acc?*/
  158.     Boolean            isText;                /*is there a current text field?*/
  159.     Boolean            isScrap;            /*is there any scrap?*/
  160.     menuBarChanged = false;
  161.  
  162.     isFront        = (FrontWindow () != nil);
  163.     isCur        = (curWindow != nil);
  164.     isDirty        = false;
  165.     hasFile        = false;
  166.     isSelected    = false;
  167.     if (isCur) {
  168.         isDirty        =  cur->dirty;
  169.         hasFile        = (cur->fileNum != 0);
  170.     }
  171.  
  172.     isDesk = false;
  173.     if (isFront) {
  174.         frontPeek    = (WindowPeek) FrontWindow ();
  175.         isDesk        = (frontPeek->windowKind < 0);
  176.     }
  177.     isText        = isCur && (cur->text != nil);
  178.     isScrap        = false;
  179.     if (isText) {
  180.         isSelected    = ((**(cur->text)).selStart != (**(cur->text)).selEnd);
  181.         isScrap        = (TEGetScrapLen () > 0);
  182.     }
  183.  
  184.     menu = FileMenu;
  185.     Enable (FileClose,        isFront);
  186.     Enable (FileSave,        isDirty);
  187.     Enable (FileSaveAs,        isCur);
  188.     Enable (FileRevert,        isDirty);
  189.  
  190.     menu = EditMenu;
  191.     if (isFront) {
  192.         Enable (EditUndo,        isDesk);
  193.         Enable (EditCut,        isDesk || isSelected);
  194.         Enable (EditCopy,        isDesk || isSelected);
  195.         Enable (EditPaste,        isDesk || isScrap);
  196.         Enable (EditClear,        isDesk || isSelected);
  197.         Enable (EditSelectAll,    isText);
  198.  
  199.     }
  200.     EnableTitle (EditMenu,     isFront);
  201.  
  202.  
  203.     if (menuBarChanged) {
  204.         DrawMenuBar ();
  205.     }
  206. } /*UpdateMenus*/
  207.  
  208. /* MainMenu */
  209.