Tabular IQ

Overview

The MenuItem class represents items on a menu.

Constructor

MenuItem()
MenuItem(label : String, help : String)

Arguments

label
The text that appears on the menu item.
help
The help string associated with the item.

Events

MenuItem.click
Fired when the menu item is clicked.

Methods

MenuItem.getHelpString
Returns the help string associated with the menu item.
MenuItem.getLabel
Returns the text of the menu item.
MenuItem.setEnabled
Enables or disables a menu item.
MenuItem.setHelpString
Sets the help string for the menu item.
MenuItem.setLabel
Sets the label for a menu item.

Example

// A MenuItem is one clickable entry on a Menu.  setLabel() and
// setHelpString() work at any time; setEnabled() only bites once the
// item is on a menu, so add first, then disable.
//
// An item belongs to the menu it was added to -- adding it to a second
// menu does nothing.  For one command on two menus, create two items and
// connect both click events to the same handler.

class MyForm extends Form {
    menu = null;

    constructor() {
        super("MenuItem Example", 100, 100, 300, 120);

        this.menu = new Menu();

        // label and help string in the constructor; the host frame shows
        // the help string in its status bar as the item is highlighted
        const open_item = new MenuItem("Open", "Open the selected file");
        open_item.click.connect(this, this.onCommand);
        this.menu.add(open_item);

        // or set them afterwards, with the no-argument constructor
        const save_item = new MenuItem();
        save_item.setLabel("Save");
        save_item.setHelpString("Write the file back to disk");
        save_item.click.connect(this, this.onCommand);
        this.menu.add(save_item);

        // greyed out -- note setEnabled() comes after add()
        const locked_item = new MenuItem("Locked", "Not available yet");
        this.menu.add(locked_item);
        locked_item.setEnabled(false);

        this.menu.addSeparator();

        // items differing only in a value need one handler between them:
        // store the value on the item, read it back off 'sender'
        for (const size of [10, 12, 14]) {
            const item = new MenuItem(`${size} point`);
            item.point_size = size;
            item.click.connect(this, this.onFontSize);

            this.menu.add(item);
        }

        const button = new Button("Show Menu", 0, 0, 100, 24);
        button.click.connect(this, this.onShowMenu);

        const layout = new BoxLayout(Layout.Vertical);
        layout.add(button, 0, Layout.Center | Layout.All, 16);
        this.setLayout(layout);
    }

    onShowMenu(sender, event_args) {
        this.menu.popup(sender);
    }

    onCommand(sender, event_args) {
        alert(`${sender.getLabel()} -- ${sender.getHelpString()}`);
    }

    onFontSize(sender, event_args) {
        alert(`font size ${sender.point_size}`);
    }
}


const form = new MyForm();
form.show();
Application.run();