Menubar class is used to manage top-level menus that can be attached to windows via the Window.Menubar attribute. A menubar contains a number of children which are objects of Menu class, each of them describing exactly one menu.
In an XML file a menu tree is defined using the <menubar>
,
<menu>
and <item>
tags. Here is an example definition
of a simple menubar:
<menubar id="mymenubar"> <menu title="_File"> <item>_New...</item> <item>_Open...</item> <item/> <item>_Save</item> <item>S_ave as...</item> <item/> <item>_Quit</item> </menu> <menu title="Edit"> <item shortcut="Ctrl+X">_Cut</item> <item shortcut="Ctrl+C">C_opy</item> <item shortcut="Ctrl+V">_Paste</item> </menu> <menu title="?"> <item>Se_ttings...</item> <item/> <item>A_bout...</item> <item>About _RapaGUI...</item> </menu> </menubar> |
Note the use of the underscore character in the XML code above: You can use this character to automatically designate the next character as a shortcut key for the menu item. This is very useful because many people like to use the keyboard instead of the mouse, especially when it comes to repeating the same actions many times. Thus, it is always a good idea to set up keyboard shortcuts. See Keyboard shortcuts for details.
If you need more complex keyboard shortcuts, e.g. something like Ctrl+V
for paste, you can use the attribute Menuitem.Shortcut to set up
such a shortcut. Note that on some platforms (e.g. Windows), both types of shortcuts
can be specified at the same time: Shortcuts specified by using the underscore character
and shortcuts declared via the Menuitem.Shortcut. If a certain platform supports
only one type of shortcut, the one specified in Menuitem.Shortcut will take
precedence over the one specified using the underscore character.
Also note the empty <item/>
declarations: These will insert a
separator bar into the menu tree. Using separator bars makes your menu
more readable to the end-user. After you have written the XML declaration
above you can add the menubar to one of your windows by using the
Window.Menubar attribute as follows:
<window menubar="mymenubar"> ... </window> |
It is very important to note that you have to declare your menubars in
the <application>
scope because menubars are global objects
and are only attached to windows or widgets later on. That is why it
is not allowed to declare menubars inside a <window>
XML
scope.