home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Help Systems / MyHelpMenu / MyHelpMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.5 KB  |  350 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MyHelpMenu.c
  3.  
  4.     Contains:    Sample code to demonstrate how to append your own help menu items 
  5.                 under the help menu.
  6.  
  7.     Written by: Jason Yeo(Mark Cookson contributed the sub menu code)
  8.  
  9.     Copyright:    Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/16/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24.  
  25. #include <Menus.h>
  26. #include <Dialogs.h>
  27. #include <Balloons.h>
  28. #include <Stdio.h>
  29. #include <Fonts.h>
  30. #include <ToolUtils.h>
  31. #include <Devices.h>
  32. #include <DiskInit.h>
  33.  
  34. //------------------------------------------------------------------------------
  35.  
  36. enum {
  37.     mApple = 128,
  38.     mFile = 129
  39. };
  40.  
  41. enum {
  42.     iAbout = 1
  43. };
  44. enum {
  45.     iSample = 1,
  46.     iQuit = 3
  47. };
  48.  
  49. enum {
  50.     iAboutDialog = 128,
  51.     iHelp1Dialog = 129,
  52.     iHelp2Dialog = 130,
  53.     iSubHelp1Dialog = 131,
  54.     iSubHelp2Dialog = 132    
  55. };
  56.  
  57. enum {
  58.     iSubMenu1 = 1,
  59.     iSubMenu2 = 2
  60. };
  61.  
  62. //------------------------------------------------------------------------------
  63.  
  64. #define mHelpSubMenu 252
  65.  
  66. //------------------------------------------------------------------------------
  67.  
  68. OSErr HandleHelpMenu (short item);
  69. OSErr HandleHelpSubMenu (short item);
  70. OSErr ShowSampleDialog(short resID);
  71.  
  72. //------------------------------------------------------------------------------
  73.  
  74. static Boolean pQuitFlag = false;
  75. static short myHelpMenuItem2;
  76.  
  77. //------------------------------------------------------------------------------
  78.  
  79. void InitToolbox(void);
  80. void MainEventLoop(void);
  81. void HandleKeyPress(EventRecord *event); 
  82. void HandleMenuCommand(long menuResult);
  83.  
  84. //------------------------------------------------------------------------------
  85.  
  86. void main()
  87. {
  88.     InitToolbox();
  89.     
  90.     MainEventLoop();
  91. }
  92.  
  93. //------------------------------------------------------------------------------
  94.  
  95. void InitToolbox()
  96. {
  97.     Handle        menuBar = nil;
  98.     MenuHandle    helpMenu;
  99.     MenuHandle    subMenu;
  100.     OSErr        err;    
  101.     
  102.     InitGraf((Ptr)&qd.thePort);
  103.     InitFonts();
  104.     InitWindows();
  105.     InitMenus();
  106.     TEInit();
  107.     InitDialogs(0L);
  108.     InitCursor();
  109.     
  110.     pQuitFlag = false;
  111.     
  112.     menuBar = GetNewMBar(mApple);
  113.     
  114.     if (menuBar == nil)
  115.          ExitToShell();                // if we dont have it then quit - your app 
  116.                                      // needs a dialog here
  117.  
  118.     SetMenuBar(menuBar);            
  119.     DisposeHandle(menuBar);
  120.     
  121.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');    // Add DA names to Apple menu, ID 128
  122.  
  123.     err = HMGetHelpMenuHandle(&helpMenu);    // Getting a handle to the Help menu
  124.  
  125.     // Make our help sub-menu
  126.     subMenu = GetMenu (mHelpSubMenu);
  127.     if (subMenu != nil) {
  128.         InsertMenu (subMenu, -1);
  129.     }
  130.  
  131.     // Appending your own Help menu
  132.     if (helpMenu != nil) {
  133.         short        count;
  134.  
  135.         count = CountMItems (helpMenu);
  136.         AppendMenu (helpMenu, "\pThis is my help1...");
  137.         SetItemCmd (helpMenu, count + 1, 0x1B);
  138.         SetItemMark (helpMenu, count + 1, mHelpSubMenu);
  139.         
  140.         AppendMenu (helpMenu, "\pThis is my help2...");
  141.         myHelpMenuItem2 = CountMItems (helpMenu);    
  142.     }
  143.  
  144.     DrawMenuBar();
  145. }
  146.  
  147. //------------------------------------------------------------------------------
  148.  
  149. void HandleMenuCommand(long menuResult)
  150. {
  151.     short        menuID;
  152.     short        menuItem;
  153.     Str255        daName;
  154.     OSErr        err            = noErr;
  155.  
  156.     menuID = HiWord(menuResult);
  157.     menuItem = LoWord(menuResult);
  158.     
  159.     if (menuResult != 0) {
  160.         menuID = HiWord (menuResult);
  161.         menuItem = LoWord (menuResult);
  162.     } else {
  163.         // If menuChoice is 0 then they selected something from a disabled menu or
  164.         // from our Help Menu submenu.  Need to use MenuChoice to determine what
  165.         // they selected.  If it wasn't from our help sub menu then bail out.
  166.         long menuChoice;
  167.  
  168.         menuChoice = MenuChoice ();
  169.  
  170.         menuID = HiWord (menuChoice);
  171.         menuItem = LoWord (menuChoice);
  172.         if (menuID != mHelpSubMenu) {
  173.             // They didn't select our help menu so don't process whatever they selected.
  174.             menuID = 0;
  175.             menuItem = 0;
  176.         }
  177.     }    
  178.     
  179.     switch (menuID) {
  180.     
  181.         case kHMHelpMenuID:
  182.             err = HandleHelpMenu(menuItem);
  183.             break;
  184.         case mHelpSubMenu:        
  185.             err = HandleHelpSubMenu(menuItem);
  186.             break;            
  187.         case mApple:
  188.             switch (menuItem) {
  189.                 case iAbout:
  190.                     err = ShowSampleDialog(iAboutDialog);
  191.                     break;
  192.                             
  193.                 default:
  194.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  195.                     (void) OpenDeskAcc(daName);
  196.                     break;
  197.             }
  198.             break;
  199.         case mFile:
  200.             switch (menuItem) {
  201.                 case iSample:
  202.                     err = ShowSampleDialog(iAboutDialog);
  203.                     break;
  204.                 case iQuit:
  205.                     pQuitFlag = true;
  206.                     break;
  207.             }
  208.             break;
  209.             
  210.     }
  211.     HiliteMenu(0);
  212. }
  213.  
  214. //------------------------------------------------------------------------------
  215.  
  216. void MainEventLoop()
  217. {
  218.     EventRecord     event;
  219.     WindowPtr       window;
  220.     short           thePart;
  221.     Point            aPoint = {100, 100};
  222.  
  223.     while(!pQuitFlag)
  224.     {
  225.         if (WaitNextEvent(everyEvent, &event, 0, nil))
  226.         {
  227.             switch (event.what) {
  228.                 case mouseDown:
  229.                 
  230.                     thePart = FindWindow(event.where, &window);
  231.                     
  232.                     switch(thePart) {
  233.                         case inMenuBar: 
  234.                             HandleMenuCommand(MenuSelect(event.where));
  235.                             break;
  236.                         
  237.                         case inDrag:
  238.                             break;
  239.                     
  240.                         case inContent:
  241.                             break;
  242.                     
  243.                         case inGoAway:
  244.                             break;
  245.                             
  246.                         default:
  247.                             break;
  248.                     }
  249.                     break;
  250.                             
  251.                         
  252.                 case updateEvt:
  253.                     break;
  254.                     
  255.                 case keyDown:
  256.                 case autoKey:
  257.                     HandleKeyPress(&event);
  258.                     break;
  259.                     
  260.                 case diskEvt:
  261.                     if (HiWord(event.message) != noErr) 
  262.                         (void) DIBadMount(aPoint, event.message);
  263.                     break;
  264.                     
  265.                 case osEvt:
  266.                 case activateEvt:
  267.                     break;
  268.  
  269.  
  270.             }
  271.         }
  272.     }
  273. }
  274.  
  275. //------------------------------------------------------------------------------
  276.  
  277. void HandleKeyPress(EventRecord *event)
  278. {
  279.     char    key;
  280.  
  281.     key = event->message & charCodeMask;
  282.     
  283.     // just check to see if we want to quit...
  284.     if (event->modifiers & cmdKey) {        /* Command key down? */
  285.         HandleMenuCommand(MenuKey(key));
  286.     } 
  287. }
  288.  
  289. //------------------------------------------------------------------------------
  290.  
  291. // This puts up a sample dialog
  292. OSErr ShowSampleDialog(short resID)
  293. {
  294.     DialogPtr    theDialog; 
  295.     short        itemHit;
  296.     OSErr        err            = noErr;
  297.     
  298.     theDialog = GetNewDialog (resID, nil, (WindowPtr)-1);
  299.     SetDialogDefaultItem(theDialog, 1);
  300.  
  301.     do {
  302.         ModalDialog (nil, &itemHit);
  303.     } while(itemHit != ok);
  304.     DisposeDialog (theDialog);
  305.     return err;
  306. }
  307.  
  308. //------------------------------------------------------------------------------
  309.  
  310. // This will handle menu items which are not sub-menus in the Help menu.
  311. OSErr HandleHelpMenu (short item) {
  312.     OSErr        err            = noErr;
  313.  
  314.     if (item == myHelpMenuItem2) {
  315.         err = ShowSampleDialog(iHelp2Dialog);
  316.     }
  317.     return err;
  318. }
  319.  
  320. //------------------------------------------------------------------------------
  321.  
  322. // This handles all menu items that in our help menu's sub-menu
  323. OSErr HandleHelpSubMenu (short item) {
  324.     OSErr        err            = noErr;
  325.     MenuHandle    mHandle;
  326.     long        itemEnabled    = 0;
  327.  
  328.     mHandle = GetMenu (mHelpSubMenu);
  329.  
  330.     if (mHandle != nil) {
  331.         // Make sure the menu is not totally disabled.
  332.         if ((**mHandle).enableFlags & 1) {
  333.             // Check to see if the item chosen is disabled
  334.             itemEnabled = (**mHandle).enableFlags & (1 << item);
  335.             if (itemEnabled != 0) {
  336.                 // It's not disabled, so go to it.
  337.                 switch (item) {
  338.                 case iSubMenu1:
  339.                     err = ShowSampleDialog(iSubHelp1Dialog);
  340.                     break;
  341.                 case iSubMenu2:
  342.                     err = ShowSampleDialog(iSubHelp2Dialog);
  343.                     break;
  344.                 }
  345.             }
  346.         }
  347.     }
  348.  
  349.     return err;
  350. }