type=menubar factorys

The contents of a factorys of type=menubar specify the x::w::menubarfactory methods that get invoked when generating the contents of a menu bar.

uigenerator6.C gives an example of using a menu bar factory to create its menus from uigenerator6.xml:

<?xml version="1.0" encoding="utf-8"?>
<theme version="1"
       xmlns:xi="http://www.w3.org/2003/XInclude">

  <dim id="cell-padding">20</dim>

  <layout id="main-window-grid" type="grid">
    <append_row>
      <name>main-window-contents</name>
    </append_row>
  </layout>

  <factory id="main-window-contents" type="grid">
    <padding>
      <side>all</side>
      <padding>cell-padding</padding>
    </padding>

    <!-- Put an input field element in the window -->

    <element id="input-field">
      <input_field />
      <config>
	<columns>20</columns>
      </config>

      <!-- Right pointer button context popup menu for the input field -->

      <context>
	<menu>
	  <append_items>
	    <items>
	      <label>Reset</label>
	    </items>
	  </append_items>
	  <append_copy_cut_paste>
	    <parent>input-field</parent>
	  </append_copy_cut_paste>
	</menu>

	<!-- Context popup's optional keyboard shortcut -->

	<shortcut>Alt-I</shortcut>
      </context>
    </element>
  </factory>

  <!-- Start building the window's menu bar -->

  <layout type="menubar" id="main-window-menu">
    <append_menus>
      <name>main-window-menus</name>
    </append_menus>

    <append_right_menus>
      <name>main-window-right-menus</name>
    </append_right_menus>
  </layout>

  <factory type="menubar" id="main-window-menus">

    <!--
	File menu. The code finishes the menu bar setup by installing a
	callback that update()s the main menu's copy/cut/paste items whenever
	file_menu pops up.
    -->

    <add id="file_menu">
      <title>
	<element>
	  <label type="theme_text">${decoration:underline}F${decoration:none}ile</label>
	</element>
      </title>

      <!-- Optional keyboard shortcut for the menu -->
      <shortcut type="theme_text">Alt-F</shortcut>
      <menu>

	<!--
	    List layout manager methods. <name> labels each menu item
	    so that the code can find it and install the appropriate
	    callback that gets invoked for each menu item.
	-->

	<append_items>
	  <items>
	    <name>file_new</name>
	    <label type="theme_text">New</label>
	    <name>file_open</name>
	    <label type="theme_text">Open</label>
	    <name>file_close</name>
	    <label type="theme_text">Close</label>
	    <name>file_recent</name>

	    <!-- Submenu -->

	    <submenu>
	      <append_items>
		<items>
		  <name>file_recent_file_1</name>
		  <label type="theme_text">File 1</label>
		  <name>file_recent_file_2</name>
		  <label type="theme_text">File 2</label>
		</items>
	      </append_items>
	    </submenu>
	    <label type="theme_text">Recent</label>

	    <!-- Menu option followed by a separator -->

	    <name>file_automatic_close</name>
	    <menuoption />
	    <label type="theme_text">Automatic Close</label>
	    <separator />

	    <!-- A radio group of two menu options -->

	    <name>file_plain_format</name>
	    <menugroup>optiongroup@examples.w.libcxx.com</menugroup>
	    <selected /> <!-- Selected by default -->
	    <label type="theme_text">Plain</label>

	    <name>file_full_format</name>
	    <menugroup>optiongroup@examples.w.libcxx.com</menugroup>
	    <label type="theme_text">Full</label>

	  </items>
	</append_items>

	<!-- Append copy/cut/paste menu items, for the "main" window -->

	<append_copy_cut_paste>
	  <parent>main</parent>
	</append_copy_cut_paste>
      </menu>
    </add>
  </factory>

  <!-- "Help" menu on the right -->

  <factory type="menubar" id="main-window-right-menus">
    <add>
      <title>
	<element>
	  <label type="theme_text">${decoration:underline}H${decoration:none}elp</label>
	</element>
      </title>
      <!-- Optional keyboard shortcut for the menu -->
      <shortcut type="theme_text">Alt-H</shortcut>
      <menu>
	<append_items>
	  <items>
	    <status_change>help_about_callback</status_change>
	    <label type="theme_text">About...</label>
	  </items>
	</append_items>
      </menu>
    </add>
  </factory>
</theme>

Adding menus

add in a factorys of type=menubar creates a new menu, and must have a title and a menu element; in addition to the optional shortcut and appearance elements.

title sets the new menu's title, and contains any element that's valid in a generic factory. The traditional menu title is a label element.

The menu element contains type=list layout elements.

In the general case, it's typically a single append_items element, specifying a name of each menuitem. uigenerator6.xml provides an example of generate()ing a menu and then using get_list_itemhandle to retrive a x::w::listitemhandle for each named menu itema and installing a callback that gets invoked by selecting the menu item.

The optional shortcut element specifies the menu's keyboard shortcut The optional appearance element specifies the menu's appearance object.

The Help / About menu item gives an example of an alternative to using a x::w::listitemhandle: