3.16 Context menus

RapaGUI supports context menus for each of its widget classes. Context menus are assigned to the individual widgets by using the Area.ContextMenu attribute. As Area class is the super-class for all widgets, you can use this attribute with every MOAI object that creates a widget. If you've declared Area.ContextMenu for a widget, RapaGUI will automatically show the context menu whenever the user clicks the right mouse button while the cursor is over a widget that has a context menu attached.

Area.ContextMenu expects an object derived from Menu class as its argument so you have to create such a menu MOAI object for your context menu in XML first. It is very important to note that you have to declare your menus in the <application> scope because menus are global objects and are only attached to windows or widgets later on. That is why it is not allowed to declare menus inside a <window> XML scope.

Here is an example in which we add a cut, copy, and paste context menu to an object of type Texteditor class:

 
<menu title="Context menu" id="ctxtmenu">
   <item>Cut</item>
   <item>Copy</item>
   <item>Paste</item>
</menu>

<window>
...
   <texteditor contextmenu="ctxtmenu"/>
...
</window>

Note that when using Menu class to create context menus, the Menu.Title attribute is only used on AmigaOS and compatibles. Context menus on Windows, Linux, and macOS don't show a title.

Also note that context menu events will be delivered via the standard Menuitem.Selected mechanism and not through a special context menu event handler. Since you can use the same menu object as a context menu for several widgets, you need a way to find out the widget whose context menu triggered the event. To give you this information, the event message will contain an additional item named "Parent" which contains the ID of the context menu's hosting widget. This allows you to re-use the same menu object as a context menu for multiple parent widgets.

AmigaOS users please note that MUI doesn't allow context menu objects to be shared across windows. You are not allowed to use the same menu object with widgets in different windows. It's okay to re-use a menu object for several widgets in the same window but not for widgets in another window. All other platforms don't have this limitation, only AmigaOS is affected here.


Show TOC