# MenuBar (Vala)

Tiffany Antopolski
tiffany.antopolski@gmail.com 2012
A widget which holds GtkMenuItem widgets

# MenuBar

A MenuBar created using XML and GtkBuilder.

# Create a MenuBar using XML

To create the menubar using XML:

Create menubar.ui using your favorite text editor.

Enter the following line at the top of the file:

<?xml version="1.0"? encoding="UTF-8"?>

We want to create the interface which will contain our menubar and its submenus. Our menubar will contain File, Edit, Choices and Help submenus. We add the following XML code to the file:

Now we will create the .vala file and use GtkBuilder to import the menubar.ui we just created.

# Add the MenuBar to the window using GtkBuilder

Now, compile the vala file, and run it. The application should look like the picture at the top of this page.

# Add items to the menus

We start off by adding 2 menuitems to the File menu: New and Quit. We do this by adding a `section` to the the `File` submenu with these items. The menubar.ui should look like this (lines 6 to 13 inclusive comprise the newly added section):

<?xml version="1.0" encoding="UTF-8"?> <interface> <menu id="menubar"> <submenu> <attribute name="label">File</attribute> <section> <item> <attribute name="label">New</attribute> </item> <item> <attribute name ="label">Quit</attribute> </item> </section> </submenu> <submenu> <attribute name="label">Edit</attribute> </submenu> <submenu> <attribute name="label">Choices</attribute> </submenu> <submenu> <attribute name="label">Help</attribute> </submenu> </menu> </interface>

Following this pattern, you can now add a `Copy` and a `Paste` item to the `Edit` submenu, and an `About` item to the `Help` submenu. We will hold off on adding items to the Choices submenu until further in the tutorial.

You do not need to recompile the vala program if you only made changes to the UI file. Just run your previously compiled application, and the UI changes will be reflected.

# Setup actions

This is a three step process.

First we create the ActionEntry array in the MyApplication class. An ActionEntry consists of:

the "action name" (mandatory)

the callback function to connect to the "activate" signal of the action (if applicable)

the type of the parameter that must be passed to the activate function for the action (if applicable)

the initial state for this action (if applicable)

the callback to connect to "change-state" signal (if applicable)

const ActionEntry[] actions = { { "new", new_cb }, // {"action name", callback_function} { "quit", quit_cb } };

Second, we create the callback functions the actions are connected to.

void new_cb (SimpleAction action, Variant? parameter) { print ("You clicked \"New\"\n"); //new MyWindow (this).show (); }

void quit_cb (SimpleAction action, Variant? parameter) { print ("You clicked \"Quit\"\n"); this.quit (); }

And lastly, we connect the menu items to the actions in the XML file by adding the "action" attribute:

<item> <attribute name="label">New</attribute> <attribute name="action">app.new</attribute> </item> <item> <attribute name="label">Quit</attribute> <attribute name="action">app.quit</attribute> </item>

# Choices submenu and items with state

Lines 30 to 80 inclusive of the demonstrate the XML code used to create the UI for Choices menu.

# Actions: Application or Window?

Above, we created the "new" and "open" actions as part of the MyApplication class. Actions which control the application itself, such as "quit" should be created similarly.

Some actions, such as "copy" and "paste" deal with the window, not the application. Window actions should be created as part of the window class.

The complete example files contain both application actions and window applications. The window actions are the ones usually included in the application menu also. It is not good practice to include window actions in the application menu. For demonstration purposes, the complete example files which follow include XML in the UI file which creates the application menu which includes a "New" and "Open" item, and these are hooked up to the same actions as the menubar items of the same name.

# Complete XML UI file for this example

# Complete Vala file for this example

# Mnemonics

Labels may contain mnemonics. Mnemonics are underlined characters in the label, used for keyboard navigation. Mnemonics are created by placing an underscore before the mnemonic character. For example "_File" instead of just "File" in the menubar.ui label attribute.

The mnemonics are visible when you press the Alt key. Pressing Alt F will open the File menu.

# Accelerators

Accelerators can be explicitly added in the UI definitions. For example, it is common to be able to quit an application by pressing Ctrl Q or to save a file by pressing Ctrl S. To add an accelerator to the UI definition, you simply need add an "accel" attribute to the item.

`<attribute name="accel">&lt;Primary&gt;q</attribute>` will create the Ctrl Q sequence when added to the `Quit` label item. Here, "Primary" refers to the Ctrl key on a PC or the ⌘ key on a Mac.

<item> <attribute name="label">_Quit</attribute> <attribute name="action">app.quit</attribute> <attribute name="accel">&lt;Primary&gt;q</attribute> </item>

# Translatable strings

Since GNOME applications are being translated into many languages, it is important that the strings in your application are translatable. To make a label translatable, simple set `translatable="yes"`:

`<attribute name="label" translatable="yes">Quit</attribute>`

# Relevant API documentation

In this sample we used the following:

Glib.ActionEntry

Gtk.Builder

# Exercises