Tabular IQ

Overview

The MenuBar class represents a menu bar.

Constructor

MenuBar()

Methods

MenuBar.add
Adds a menu to a menu bar.
MenuBar.findMenu
Finds the location of a menu in the menu bar.
MenuBar.getMenu
Returns a menu in the menu bar.
MenuBar.getMenuCount
Returns the number of menus in the menu bar.
MenuBar.getMenuItems
Returns an array of menus and menu items that are in the menu.
MenuBar.getMenus
Returns an array of menus that are in the menu bar.
MenuBar.insert
Inserts a menu into a menu bar.
MenuBar.remove
Removes a menu from the menu bar.

Example

// A MenuBar is the strip of drop-downs across the top of a form.  It
// holds Menu objects; the MenuItem entries live on those menus.
//
// HostApp.getFrameMenu() returns a MenuBar wrapping the host
// application's own menu bar, for scripts that want to add to it.

class MyForm extends Form {
    menubar = null;

    constructor() {
        super("MenuBar Example", 100, 100, 360, 160);

        // each drop-down is a Menu whose title is the word on the bar
        const file_menu = new Menu("File");
        file_menu.add(this.makeItem("Open", "Open a document"));
        file_menu.addSeparator();
        file_menu.add(this.makeItem("Exit", "Close this example"));

        const edit_menu = new Menu("Edit");
        edit_menu.add(this.makeItem("Cut", "Move the selection"));
        edit_menu.add(this.makeItem("Copy", "Copy the selection"));

        this.menubar = new MenuBar();
        this.menubar.add(file_menu);
        this.menubar.add(edit_menu);

        // nothing appears until the form is told to use the bar
        this.setMenuBar(this.menubar);

        const button = new Button("Describe", 0, 0, 100, 24);
        button.click.connect(this, this.onDescribe);

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

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

        return item;
    }

    onDescribe(sender, event_args) {
        // getMenus() returns them all; getMenu(index) fetches one, and
        // findMenu(title) gives an index, or -1
        let s = `${this.menubar.getMenuCount()} menus\n`;

        for (const menu of this.menubar.getMenus()) {
            s += `  ${menu.getLabel()}: ${menu.getMenuItemCount()} entries\n`;
        }

        // remove(index) or remove(menu); the Menu keeps its items, so it
        // can be added straight back
        this.menubar.remove(this.menubar.findMenu("Edit"));

        alert(`${s}\nremoved the Edit menu`);
    }

    onItemClicked(sender, event_args) {
        if (sender.getLabel() === "Exit") {
            Application.exit();
        }

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


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