Defining the Menus in XML
Figure 1 File menu.
Figure 2 Help menu.
To define the menus, create either an XML or JSON file. I'll use XML, as the JSON format can be a pain to write from scratch (its syntax is quite particular). An XML file is a plain-text document that can be created in any text editor or integrated development environment. The XML file should start with the standard opening:
<?xml version="1.0" encoding="utf-8"?>
Next, the XML file should have one root element, which I'll just call root:
To create the menu items, add sub-elements to the root element. To create File and Help menus, for example, the XML file would look like this:
<?xml version="1.0" encoding="utf-8"?> <root> <menu label="File"></menu> <menu label="Help"></menu> </root>
As you might expect, the label attribute is where you assign the text that will appear in the menu. The names of the elements can be anything that's allowed in XML, so use something meaningful and consistent.
For contextual menus, you'd likely use top-level menu items. For application and window menus, the structure is nested; each top-level menu item (File, Help, and so on) is just a label, with the specific functionality (Exit, Show Tips, and so on) defined as a submenu item. Let's flesh out the example:
<menu label="File"> <menuItem label="Exit" /> </menu> <menu label="Help"> <menuItem label="Show Tips" /> <menuItem label="Help Pages" /> <menuItem label="Check for Updates" /> <menuItem label="Visit Web Site" /> </menu>
For each of the submenu items, we use a condensed element format—that is, the element is closed using a space and a slash, rather than a formal closing tag. The labels correspond to those shown earlier in Figures 1 and 2. As with the top-level menus, the names of the elements can be anything valid, but take care to be consistent.