1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
TITLE:: Menu
summary:: Context and application-level menus
categories:: GUI
related:: Classes/MenuAction, Classes/CustomViewAction, Classes/ToolBar, Classes/MainMenu
DESCRIPTION::
The Menu class allows the creation and control of context menus and application-level menus.
CODE::
(
~menu = Menu(
MenuAction("A", { "A selected".postln }),
MenuAction("B", { "B selected".postln }),
MenuAction("C", { "C selected".postln }),
).front;
)
::
CLASSMETHODS::
METHOD:: new
Create a new menu. A menu is populated with link::Classes/MenuAction::s
ARGUMENT::
One or more link::Classes/MenuAction##MenuActions:: or Menus to be displayed in the menu. Menu arguments will create sub-menus.
DISCUSSION::
CODE::
(
Menu(
MenuAction("main"),
MenuAction("menu"),
Menu(
MenuAction("sub"),
MenuAction("menu"),
).title_("submenu")
).front;
)
::
INSTANCEMETHODS::
SUBSECTION:: Control
METHOD:: front
Show the menu if it is not already shown. Note that unlike most Views, Menus are not destroyed when they are closed - front can be used to re-generate a single menu again and again.
ARGUMENT:: point
A link::Classes/Point:: at which to show the menu. Default to the current mouse cursor position.
ARGUMENT:: action
The action that should appear at the specified point coordinates. Should be an action contained in the menu.
METHOD:: title
The title of the menu.
ARGUMENT:: title
A link::Classes/String::.
METHOD:: tearOff
ARGUMENT::
If true, menu is / will be torn off from its parent and become a free-floating menu.
METHOD:: copy
Create an identical copy of the menu. The new menu will not be shown until code::.front:: is called.
SUBSECTION:: Events
Menus broadcast several kinds of events that can be registered for with the standard link::Classes/Object#-addDependant:: calls.
Events broadcast by menu:
LIST::
## code::\aboutToShow::
## code::\aboutToHide::
## code::\triggered:: (with the MenuAction that was triggered)
## code::\hovered:: (with action that was hovered over)
::
STRONG:: Events example ::
CODE::
(
m = Menu(MenuAction("Option A"), MenuAction("Option B")).title_("Event Example");
f = {
|menu, what, value|
"Menu '%' sent event %, value = %".format(menu.title, "\\" ++ what, value).postln;
if (what == \hovered) { "Hovering over: %".format(value).postln };
if (what == \triggered) { "Action triggered: %".format(value).postln; menu.destroy };
};
m.addDependant(f);
m.onClose_({ m.removeDependant(f) }).front;
)
::
SUBSECTION:: Context Menus
One or more MenuActions can be attached to any View object. The shortcut key combinations for these actions will become available, and the actions will appear in the context menu for that view.
See link::Classes/View#-setContextMenuActions::, link::Classes/View#-addMenuAction::, link::Classes/View#-removeMenuAction:: for more information.
CODE::
(
~view = View().layout_(HLayout(
~button = Button().states_([["Button"]]),
~number = NumberBox().minWidth_(100).value_(100)
)).front;
~button.setContextMenuActions(
MenuAction("Red", { ~button.states = [["Red", Color.red]] }),
MenuAction("Green", { ~button.states = [["Green", Color.green]] }),
MenuAction("Blue", { ~button.states = [["Blue", Color.blue]] })
);
~number.setContextMenuActions(
MenuAction("100", { ~number.value = 100 }),
MenuAction("200", { ~number.value = 200 }),
MenuAction("300", { ~number.value = 300 })
);
)
::
EXAMPLES::
STRONG:: A more complex menu. ::
CODE::
(
~image = Image(64, 64).draw({
Pen.fillColor = Color.blue;
Pen.fillOval(Rect(0, 0, 64, 64));
});
~menu = Menu(
MenuAction("checked", { "checked".postln })
.checked_(true),
MenuAction("disabled", { "disabled".postln })
.enabled_(false),
MenuAction("keyboard short", { "keyboard short".postln })
.shortcut_("Ctrl+Shift+N"),
MenuAction("icon", { "icon".postln })
.icon_(~image),
MenuAction("font", { "font".postln })
.font_(Font("Helvetica", 20, italic:true)),
MenuAction.separator.string_("other stuff"),
CustomViewAction(Slider().orientation_(\horizontal)).action_({ |v| v.value.postln }),
Menu(
"string.toAction",
{ "function.toAction".postln }
).title_("submenu")
).front;
)
::
|