home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MyHelpMenu.c
-
- Contains: Sample code to demonstrate how to append your own help menu items
- under the help menu.
-
- Written by: Jason Yeo(Mark Cookson contributed the sub menu code)
-
- Copyright: Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/16/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
-
- #include <Menus.h>
- #include <Dialogs.h>
- #include <Balloons.h>
- #include <Stdio.h>
- #include <Fonts.h>
- #include <ToolUtils.h>
- #include <Devices.h>
- #include <DiskInit.h>
-
- //------------------------------------------------------------------------------
-
- enum {
- mApple = 128,
- mFile = 129
- };
-
- enum {
- iAbout = 1
- };
- enum {
- iSample = 1,
- iQuit = 3
- };
-
- enum {
- iAboutDialog = 128,
- iHelp1Dialog = 129,
- iHelp2Dialog = 130,
- iSubHelp1Dialog = 131,
- iSubHelp2Dialog = 132
- };
-
- enum {
- iSubMenu1 = 1,
- iSubMenu2 = 2
- };
-
- //------------------------------------------------------------------------------
-
- #define mHelpSubMenu 252
-
- //------------------------------------------------------------------------------
-
- OSErr HandleHelpMenu (short item);
- OSErr HandleHelpSubMenu (short item);
- OSErr ShowSampleDialog(short resID);
-
- //------------------------------------------------------------------------------
-
- static Boolean pQuitFlag = false;
- static short myHelpMenuItem2;
-
- //------------------------------------------------------------------------------
-
- void InitToolbox(void);
- void MainEventLoop(void);
- void HandleKeyPress(EventRecord *event);
- void HandleMenuCommand(long menuResult);
-
- //------------------------------------------------------------------------------
-
- void main()
- {
- InitToolbox();
-
- MainEventLoop();
- }
-
- //------------------------------------------------------------------------------
-
- void InitToolbox()
- {
- Handle menuBar = nil;
- MenuHandle helpMenu;
- MenuHandle subMenu;
- OSErr err;
-
- InitGraf((Ptr)&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- pQuitFlag = false;
-
- menuBar = GetNewMBar(mApple);
-
- if (menuBar == nil)
- ExitToShell(); // if we dont have it then quit - your app
- // needs a dialog here
-
- SetMenuBar(menuBar);
- DisposeHandle(menuBar);
-
- AppendResMenu(GetMenuHandle(mApple), 'DRVR'); // Add DA names to Apple menu, ID 128
-
- err = HMGetHelpMenuHandle(&helpMenu); // Getting a handle to the Help menu
-
- // Make our help sub-menu
- subMenu = GetMenu (mHelpSubMenu);
- if (subMenu != nil) {
- InsertMenu (subMenu, -1);
- }
-
- // Appending your own Help menu
- if (helpMenu != nil) {
- short count;
-
- count = CountMItems (helpMenu);
- AppendMenu (helpMenu, "\pThis is my help1...");
- SetItemCmd (helpMenu, count + 1, 0x1B);
- SetItemMark (helpMenu, count + 1, mHelpSubMenu);
-
- AppendMenu (helpMenu, "\pThis is my help2...");
- myHelpMenuItem2 = CountMItems (helpMenu);
- }
-
- DrawMenuBar();
- }
-
- //------------------------------------------------------------------------------
-
- void HandleMenuCommand(long menuResult)
- {
- short menuID;
- short menuItem;
- Str255 daName;
- OSErr err = noErr;
-
- menuID = HiWord(menuResult);
- menuItem = LoWord(menuResult);
-
- if (menuResult != 0) {
- menuID = HiWord (menuResult);
- menuItem = LoWord (menuResult);
- } else {
- // If menuChoice is 0 then they selected something from a disabled menu or
- // from our Help Menu submenu. Need to use MenuChoice to determine what
- // they selected. If it wasn't from our help sub menu then bail out.
- long menuChoice;
-
- menuChoice = MenuChoice ();
-
- menuID = HiWord (menuChoice);
- menuItem = LoWord (menuChoice);
- if (menuID != mHelpSubMenu) {
- // They didn't select our help menu so don't process whatever they selected.
- menuID = 0;
- menuItem = 0;
- }
- }
-
- switch (menuID) {
-
- case kHMHelpMenuID:
- err = HandleHelpMenu(menuItem);
- break;
- case mHelpSubMenu:
- err = HandleHelpSubMenu(menuItem);
- break;
- case mApple:
- switch (menuItem) {
- case iAbout:
- err = ShowSampleDialog(iAboutDialog);
- break;
-
- default:
- GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
- (void) OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch (menuItem) {
- case iSample:
- err = ShowSampleDialog(iAboutDialog);
- break;
- case iQuit:
- pQuitFlag = true;
- break;
- }
- break;
-
- }
- HiliteMenu(0);
- }
-
- //------------------------------------------------------------------------------
-
- void MainEventLoop()
- {
- EventRecord event;
- WindowPtr window;
- short thePart;
- Point aPoint = {100, 100};
-
- while(!pQuitFlag)
- {
- if (WaitNextEvent(everyEvent, &event, 0, nil))
- {
- switch (event.what) {
- case mouseDown:
-
- thePart = FindWindow(event.where, &window);
-
- switch(thePart) {
- case inMenuBar:
- HandleMenuCommand(MenuSelect(event.where));
- break;
-
- case inDrag:
- break;
-
- case inContent:
- break;
-
- case inGoAway:
- break;
-
- default:
- break;
- }
- break;
-
-
- case updateEvt:
- break;
-
- case keyDown:
- case autoKey:
- HandleKeyPress(&event);
- break;
-
- case diskEvt:
- if (HiWord(event.message) != noErr)
- (void) DIBadMount(aPoint, event.message);
- break;
-
- case osEvt:
- case activateEvt:
- break;
-
-
- }
- }
- }
- }
-
- //------------------------------------------------------------------------------
-
- void HandleKeyPress(EventRecord *event)
- {
- char key;
-
- key = event->message & charCodeMask;
-
- // just check to see if we want to quit...
- if (event->modifiers & cmdKey) { /* Command key down? */
- HandleMenuCommand(MenuKey(key));
- }
- }
-
- //------------------------------------------------------------------------------
-
- // This puts up a sample dialog
- OSErr ShowSampleDialog(short resID)
- {
- DialogPtr theDialog;
- short itemHit;
- OSErr err = noErr;
-
- theDialog = GetNewDialog (resID, nil, (WindowPtr)-1);
- SetDialogDefaultItem(theDialog, 1);
-
- do {
- ModalDialog (nil, &itemHit);
- } while(itemHit != ok);
- DisposeDialog (theDialog);
- return err;
- }
-
- //------------------------------------------------------------------------------
-
- // This will handle menu items which are not sub-menus in the Help menu.
- OSErr HandleHelpMenu (short item) {
- OSErr err = noErr;
-
- if (item == myHelpMenuItem2) {
- err = ShowSampleDialog(iHelp2Dialog);
- }
- return err;
- }
-
- //------------------------------------------------------------------------------
-
- // This handles all menu items that in our help menu's sub-menu
- OSErr HandleHelpSubMenu (short item) {
- OSErr err = noErr;
- MenuHandle mHandle;
- long itemEnabled = 0;
-
- mHandle = GetMenu (mHelpSubMenu);
-
- if (mHandle != nil) {
- // Make sure the menu is not totally disabled.
- if ((**mHandle).enableFlags & 1) {
- // Check to see if the item chosen is disabled
- itemEnabled = (**mHandle).enableFlags & (1 << item);
- if (itemEnabled != 0) {
- // It's not disabled, so go to it.
- switch (item) {
- case iSubMenu1:
- err = ShowSampleDialog(iSubHelp1Dialog);
- break;
- case iSubMenu2:
- err = ShowSampleDialog(iSubHelp2Dialog);
- break;
- }
- }
- }
- }
-
- return err;
- }