The Menu class represents a menu, which is a vertical list of menu items.
// 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(); Adds a menu or menu item specified by item to the menu.
Adds a separator to the menu.
The zero-based index of the submenu or menu item with a particular caption or -1 if a submenu or menu item with a caption does not exist.
Finds the location of a submenu or menu item with a particular caption in the menu and returns the location where the submenu or menu item is located. If a submenu or menu item with the caption is not found, then the function returns -1.
The text of the menu.
Returns the text of the menu.
The menu or menu item at the specified zero-based index in the menu. If index is less than 0, greater than the number of menu items in the menu or is unspecified, the function returns null.
Returns the submenu or menu item at the specified index.
The number of submenus and menu items in the menu.
Returns the number of submenus and menu items in the menu.
Inserts a menu or menu item specified by item into the menu at a given index. If no index is specified, the menu or menu item is inserted at the end of the menu.
Inserts a separator into the menu at a given index. If no index is specified, the separator is inserted at the end of the menu.
Creates a menu on a form at at a given point.
Removes a menu or a menu item from the menu. If the input is an integer, then the item at the position specified by index is removed. If the input is a menu or menu item, then the item is removed from the menu, wherever it is located.
Enables or disables a menu. If enabled isn't specified, the menu is enabled; otherwise the menu is enabled if enabled is true and disabled if enabled is false.
Sets the menu text specified by label.