AT&T Home | AT&T Labs | Research
AT&T Labs, Inc. - Research

The Yoix® Scripting Language

Home | What's New | Grammar | Documentation | Download | License | YDAT | YWAIT | Byzgraf | FAQs
MenuBar typedict
 
A MenuBar is the interface to Java's AWT MenuBar MenuComponent. Yoix programs normally interact with them by adding event handlers to the frame that owns the menubar, and by reading or writing the following MenuBar fields:
font The Font, or font name if it is a String, used to draw the text strings in the menubar and its submenus. Unfortunately Windows seems to ignore all font requests, and on other platforms menubars may have to be removed and reattached before font changes take effect.
getEnabled(Object pattern) A Builtin that finds the first menu item with a command that matches pattern, which can be a String or Regexp, and returns 1 if it is currently enabled (i.e., it will respond when selected by a user), 0 if not, and -1 if there is no match.
getState(Object pattern) A Builtin that finds the first checkbox menu item with a command that matches pattern, which can be a String or Regexp, and returns 1 if it is currently selected, 0 if not, and -1 if there is no match.
items A Menu array, organized in pairs, that completely describes the menubar. The first element in each pair must be a string that is used as a label on the menubar. The second element in each pair must be another Menu array that describes the submenu that is placed under its label on the menubar.
setEnabled(Object pattern, int state) A Builtin that finds menu items with commands that match pattern, which can be a String or Regexp, and enables them (i.e., they respond when selected by a user) when state is non-zero and disables them when state is zero. A return value of -1 means there were no matches.
setState(Object pattern, int state) A Builtin that finds checkbox menu items with commands that match pattern, which can be a String or Regexp, and selects them when state is non-zero and deselects them when state is zero. A return value of -1 means there were no matches.
visible An int that is 1 when the menubar is visible, and 0 otherwise. Writing immediately sets the menubar's visibility to the new state. Unfortunately, reading this field does not always return an accurate representation of the menubar's state.
Several permanent fields have not been documented and should not be used in Yoix applications.

A menubar can be attached to a frame by assigning it to the frame's menubar field. Only one frame can own a menubar, so attaching it to a frame automatically removes the menubar from the current owner, if there is one. Storing NULL in a frame's menubar field removes the current menubar. Selecting a regular menu item delivers an ActionEvent to the actionPerformed event handler defined in the frame that currently owns the menubar. Selecting a checkbox menu item delivers an ItemEvent to the itemStateChanged event handler defined in that same frame.
 
 Example:   The program,
import yoix.*.*;

MenuBar menubar = {
    Menu items = {
        "File", new Menu {
            "Open", new Dictionary {
                String command = "open";
                String accelerator = "ctrl O";
                int    enabled = FALSE;
            },
            "Close", new Dictionary {
                String command = "close";
                String accelerator = "ESCAPE";
            },
            "-", NULL,
            "Exit", "exit",
        },
        "Edit", new Menu {
            "Save", "save1", 1,
            "Save As", "save2",
        },
    };
};

Frame f = {
    MenuBar menubar = menubar;

    GridLayout layoutmanager = {
        int  rows = 1;
    };

    Array layout = {
        new Button {
            String text = "Toggle MenuBar";
            actionPerformed(e) {
                if (root.menubar == NULL)
                    root.menubar = global.menubar;
                else root.menubar = NULL;
            }
        },
        new Button {
            String text = "Show MenuBar";
            actionPerformed(e) {
                root.menubar = global.menubar;
            }
        },
        new Button {
            String text = "Hide MenuBar";
            actionPerformed(e) {
                root.menubar = NULL;
            }
        },
    };

    actionPerformed(e) {
        printf("Received: %O\n", e);
        if (strcmp(e.command, "exit") == 0)
            exit(0);
    }

    itemStateChanged(e) {
        printf("Received: %O\n", e);
    }
};

f.visible = TRUE;
adds and removes a menubar from a frame and shows how menu selections get back to the frame's actionPerformed and itemStateChanged event handlers.
 
 See Also:   Frame, Menu, PopupMenu

 

Yoix is a registered trademark of AT&T Intellectual Property.