The MenuBar class represents a menu bar.
// 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(); Adds a menu bar specified by item to the menu.
The zero-based index of the menu with a particular caption or -1 if a menu with a caption does not exist.
Finds the location of a menu with a particular caption in the menu bar and returns the location where the menu is located. If a menu with the caption is not found, then the function returns -1.
The menu at the specified zero-based index in the menu bar. If index is less than 0, greater than the number of menus in the menu bar or is unspecified, the function returns null.
Returns the menu in the menu bar at the specified index.
The number of menus in the menu bar.
Returns the number of menus in the menu bar.
An array of menus and menu items that are in the menu.
Returns an array of menus and menu items that are in the menu.
An array of menus that are in the menu bar.
Returns an array of menus that are in the menu bar.
Inserts a menu specified by item into the menu bar at a given index. If no index is specified, the menu or menu item is inserted at the end of the menu.
Returns true if the menu is removed, and false otherwise.
Removes a menu from the menu bar that is explicitly denoted by item or else by an position given by index. The function returns true if the menu is removed, false otherwise.