Menu Handling: Sample Code

The following code fragment constructs a menu that consists of two submenus and displays it, first saving any existing menu:

78  ID-NEW                 VALUE 101.
78  ID-OPEN                VALUE 102.
78  ID-SAVE                VALUE 103.
78  ID-SAVE-AS             VALUE 104.
78  ID-EXIT                VALUE 105.
78  ID-SAVE-SET            VALUE 201.
78  ID-PASSWORD            VALUE 202.
77  OLD-MENU               PIC S9(9) COMP-4.
77  MAIN-MENU              PIC S9(9) COMP-4.
77  SUBMENU                PIC S9(9) COMP-4.

BUILD-MENU.
* Create two empty menus and save their menu handles
    CALL "W$MENU" USING WMENU-NEW.
    MOVE RETURN-CODE TO MAIN-MENU.
    CALL "W$MENU" USING WMENU-NEW.
    MOVE RETURN-CODE TO SUBMENU.
    IF MAIN-MENU = ZERO OR SUBMENU = ZERO
        GO TO BUILD-MENU-EXIT.

* Build "File" submenu

   CALL "W$MENU" USING WMENU-ADD, SUBMENU, 0, 0,
      "&New", ID-NEW.
   CALL "W$MENU" USING WMENU-ADD, SUBMENU, 0, 0, 
      "&Open...",ID-OPEN.
   CALL "W$MENU" USING WMENU-ADD, SUBMENU, 0, W-DISABLED, 
      "&Save", ID-SAVE.
   CALL "W$MENU" USING WMENU-ADD, SUBMENU, 0, W-DISABLED, 
      "Save &As...", ID-SAVE-AS.
   CALL "W$MENU" USING WMENU-ADD, SUBMENU, 0, W-SEPARATOR.
   CALL "W$MENU" USING WMENU-ADD, SUBMENU, 0, 0, 
      "E&xit", ID-EXIT.

* Attach "File" submenu to main menu

   CALL "W$MENU" USING WMENU-ADD, MAIN-MENU, 0, 0, 
      "&File", 0, SUBMENU.

* When finished with the "File" submenu, make another 
* submenu and populate it.

   CALL "W$MENU" USING WMENU-NEW.
   MOVE RETURN-CODE TO SUBMENU.
   IF SUBMENU = ZERO
       GO TO BUILD-MENU-EXIT.
   CALL "W$MENU" USING WMENU-ADD, SUBMENU, 0, W-CHECKED, 
      "&Save Settings on Exit", ID-SAVE-SET.
   CALL "W$MENU" USING WMENU-ADD, SUBMENU, 0, 0, 
      "&Password Protect", ID-PASSWORD.

* Attach "Options" submenu

   CALL "W$MENU" USING WMENU-ADD, MAIN-MENU, 0, 0, 
      "&Options", 0, SUBMENU.

* Save current menu and display new one.

   CALL "W$MENU" USING WMENU-GET-MENU.
   MOVE RETURN-CODE TO OLD-MENU.
   CALL "W$MENU" USING WMENU-SHOW, MAIN-MENU.

Further on in the program, you may want to remove the check mark on the Save Settings on Exit menu item. The following line of code would do that:

    CALL "W$MENU" USING WMENU-CHECK, ID-SAVE-SET, 
       W-UNCHECKED.

When it came time to remove this menu and restore the old one, you could use the following code:

    CALL "W$MENU" USING WMENU-SHOW, OLD-MENU.
    CALL "W$MENU" USING WMENU-DESTROY, MAIN-MENU.