home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Toolbox / MyHelpMenu / MyHelpMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-28  |  7.4 KB  |  345 lines  |  [TEXT/CWIE]

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