Up   Previous   Next  

Calling the Appropriate MLTE Function

When your application detects a menu command, it can call a function such as MyDoFileEditFontMenuCommands . This function can then call the appropriate MLTE function to handle the command.

It is a straightforward process for your application to figure out which MLTE function to call for all commands except those from the Font menu. Because the Font menu can be hierarchical or nonhierarchical, your application needs to check for both cases.

Listing 3-7 shows how your application can handle window-related menu events from within a MyDoFileEditFontMenuCommands function.

Listing 3-7 Processing menu commands from the File, Edit, and Font menus
void MyDoFileEditFontMenuCommands (long MenuResult) { short menuID, menuItem; OSStatus result = eventNotHandledErr; //failure by default; TXNObject textObject; TXNTypeAttributes typeAttr; WindowPtr theWindow; // Get the window reference. theWindow = FrontWindow (); // Use macros to extract the menu ID and menu item. menuID = HiWord (MenuResult); menuItem = LoWord (MenuResult); // Use your own function to get the text object associated // with the window. MyGetTextObject (theWindow, &textObject); // Choose an action based on the menu ID and menu item. switch (menuID) { case mFile: switch (menuItem) { case kClose: // Call a function similar to MyDisposeObject // to close the window and dispose of the text object. if (MyDisposeObject (theWindow)) result = noErr; break; case kSave: // Call your function to save the text object. // Your MyDoSave function should call the MLTE function // TXNSave to save the text object. result = MyDoSave (theWindow); break; case kSaveAs: // Call your function to save a copy of the text object. // Your MyDoSaveAs function should call the MLTE // function TXNSave to save the text object result = MyDoSaveAs (theWindow); break; case kPageSetup: // The MLTE function TXNPageSetup displays the // Page Setup dialog and handles all changes in // response to page layout settings your user makes. result = TXNPageSetup (textObject); break; case kPrint: // The MLTE function TXNPrint displays the Print // dialog, handles all changes in response to print // settings your user makes, then prints the text // text object. result = TXNPrint (textObject); break; } break; case mEdit: switch (menuItem) { case kUndo: // Call the MLTE function to undo // the last user action. TXNUndo (textObject); result = noErr; // report success break; case kRedo: // Call the MLTE function to redo // the last user action. TXNRedo (textObject); result = noErr; // report success break; case kCut: // Call the MLTE function to cut the selected text. result = TXNCut (textObject); // report result break; case kCopy: // Call the MLTE function to copy the selected text. result = TXNCopy (textObject); // report result break; case kPaste: // Call the MLTE function to paste the selected text. result = TXNPaste (textObject); // report result break; case kClear: // Call the MLTE function to clear the selected text. result = TXNClear (textObject); // report result break; case kSelectAll: // Call the MLTE function to select all text // in the text object. TXNSelectAll (textObject); result = noErr; // report success break; } break; case mFont: // This if for the case of a menu that is not // a hierarchical Font menu. if (gTXNFontMenuObject != NULL) status = TXNDoFontMenuSelection (textObject, gFontMenuObject, menuID, menuItem); break; default: // The command ID is none of the above, check to see // if it is from a hierarchical menu. if (menuID >= kStartHierMenuID) { // Then check to see if the menu reference // refers to a the Font menu. if (gTXNFontMenuObject != NULL) result = TXNDoFontMenuSelection (textObject, gFontMenuObject, menuID, menuItem); } break; } // unhighlight the menu title. HiliteMenu (0); return result; }

Copyright © 2001 Apple Computer, Inc. (Last Updated January 11, 2001)

Up   Previous   Next