Adjusting Menus
At any given time during the execution of your application, it's likely that some of the commands in your menus will not be appropriate. For example, if the front window is a dialog window, then any menu commands that manipulate only document windows should be disabled. Similarly, if the desktop shows no windows belonging to your application, then the Close command in the File menu should be disabled. When a menu item is disabled, it is drawn in a dimmed text and is not highlighted when the cursor passes over it. This disabling prevents the user from choosing those commands.An easy way to achieve this effect is to call an application-defined routine that adjusts the menus according to the current application context just before you call either
MenuSelectorMenuKey. Listing 8-6 shows the version ofDoMenuAdjustused by the Venn Diagrammer application.
PROCEDURE DoMenuAdjust; VAR myWindow: WindowPtr; myMenu: MenuHandle; count: Integer; BEGIN myWindow := FrontWindow; IF myWindow = NIL THEN DisableMenuItem(GetMenuHandle(mFile), iClose) ELSE EnableMenuItem(GetMenuHandle(mFile), iClose); myMenu := GetMenuHandle(mVennD); IF IsAppWindow(myWindow) THEN FOR count := 1 TO kNumTools DO EnableMenuItem(myMenu, count) ELSE FOR count := 1 TO kNumTools DO DisableMenuItem(myMenu, count); IF IsDAccWindow(myWindow) THEN EnableMenuItem(GetMenuHandle(mEdit), 0) ELSE DisableMenuItem(GetMenuHandle(mEdit), 0); DrawMenuBar; END;TheDoMenuAdjustprocedure callsFrontWindowto get a pointer to the frontmost window belonging to the Venn Diagrammer application. If there is no window belonging to the Venn Diagrammer application,DoMenuAdjustdisables the Close menu command in the File menu. Conversely, if there is a window belonging to the application,DoMenuAdjustenables the Close command.If the front window is a document window, then
DoMenuAdjustenables all the document-specific commands in the Venn menu; otherwise, it disables all those commands. (DoMenuAdjustretrieves the menu handle by callingGetMenuHandleand passes that handle toEnableMenuItemorDisableMenuItem.)You can disable or enable an entire menu by passing
DisableMenuItemorEnableMenuItemthe value 0 in place of a menu item number. This is the strategy thatDoMenuAdjustfollows for the Edit menu. Venn Diagrammer does no editing of its own, soDoMenuAdjustmakes certain to enable the Edit menu only when a desk accessory window is frontmost. When you callDisableMenuItemorEnableMenuItemin this way, however, you also need to call the Menu Manager procedureDrawMenuBarto update the menu bar's appearance.