Tabular IQ

Overview

The Menu class represents a menu, which is a vertical list of menu items.

Constructor

Menu()
Menu(title : String)

Arguments

title
The title of the menu.

Methods

Menu.add
Adds a menu or a menu item to a menu.
Menu.addSeparator
Adds a separator to a menu.
Menu.findMenuItem
Finds the location of a submenu or menu item in the menu.
Menu.getLabel
Returns the text of the menu.
Menu.getMenuItem
Returns a menu or menu item that is in the menu.
Menu.getMenuItemCount
Returns the number of menu items in the menu.
Menu.insert
Inserts a menu or menu item into a menu.
Menu.insertSeparator
Inserts a separator into a menu.
Menu.popup
Creates a menu at a given position on a form.
Menu.remove
Removes a Menu or MenuItem from the menu.
Menu.setEnabled
Enables or disables a menu.
Menu.setLabel
Sets the label for a menu.

Example

// A Menu is a list of MenuItem objects.  The same object works as a
// context menu (popup) or as a drop-down on a MenuBar; a Menu added to
// another Menu becomes a submenu.

class MyForm extends Form {
    menu = null;

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

        this.menu = new Menu();
        this.menu.add(this.makeItem("Cut"));
        this.menu.add(this.makeItem("Copy"));
        this.menu.addSeparator();

        // the submenu's title is what the parent menu displays
        const recent = new Menu("Open Recent");
        recent.add(this.makeItem("results.report"));
        recent.add(this.makeItem("sales.ttx"));
        this.menu.add(recent);

        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);
    }

    makeItem(label) {
        const item = new MenuItem(label);
        item.click.connect(this, this.onItemClicked);

        return item;
    }

    onShowMenu(sender, event_args) {
        // updating item state just before the menu appears is the usual
        // pattern.  findMenuItem(label) returns an index into this menu,
        // or -1; getMenuItem(index) returns the MenuItem, or the Menu
        // when the slot holds a submenu.
        const index = this.menu.findMenuItem("Copy");
        this.menu.getMenuItem(index).setEnabled(false);

        // popup(control) shows the menu over that control, at the mouse;
        // pass a Point as a second argument to place it exactly
        this.menu.popup(sender);
    }

    onItemClicked(sender, event_args) {
        alert(`clicked ${sender.getLabel()}`);
    }
}


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