Legacy Maemo 5 Documentation/Graphical UI Tutorial/Menus

=Menus = The use of menus in Hildon applications follows a very different approach from the traditional application menus.

Hildon applications have no menu bar. Each window has an attached menu which activates when the user presses the window title area. Also, unlike typical menus in desktop applications, view menus do not follow a hierarchical structure. This menu, attached to a view, is called Touch View Menu.

Additionally Hildon provides Context menus, although those should be avoided when possible.

Touch view menu
The widget which should be used as a menu in the windows of a Hildon application is HildonAppmenu.

This widget opens at the top of the screen, obscuring the topmost window, and contains several entries organized in one or two columns, depending on screen orientation. Besides entries, application menus can also contain a group of filter buttons.

The entries are GtkButtons. Assign an action to the menu item for when they are activated.

Filters are toggle buttons that can be used for presentation/sorting purposes. For example, sorting alphabetically a list of contacts or changing the size of icons in a list.

To create a HildonAppMenu, use the following command:

kWidget  *  hildon_app_menu_new              (  void  ); 

Once the application menu is created, add entries to the menu by using the following functions:

	 void        hildon_app_menu_append           ( HildonAppMenu  * menu ,          	                                     GtkButton  * item ); void        <font color="#000000">hildon_app_menu_prepend          <font color="#990000">( HildonAppMenu <font color="#990000"> * menu <font color="#990000">,                  	                             GtkButton <font color="#990000"> * item <font color="#990000">); <font color="#009900">void        <font color="#000000">hildon_app_menu_insert           <font color="#990000">( HildonAppMenu <font color="#990000"> * menu <font color="#990000">,                          	                     GtkButton <font color="#990000"> * item <font color="#990000">,                                  	             gint position <font color="#990000">); </tt>

These functions allow you to append, prepend an entry or add it in a certain position (from 0 to N-1, where N is the number of menus).

It is important keep the UI simple. Try to keep the menu as short as possible and avoid adding unnecessary options. An application menu with more that 10 items is probably not displayed well.

To add filter buttons, use the following:

 <font color="#009900">void        <font color="#000000">hildon_app_menu_add_filter       <font color="#990000">( HildonAppMenu <font color="#990000"> * menu <font color="#990000">,                                		               GtkButton <font color="#990000"> * filter <font color="#990000">); </tt>

Again, be careful with the number of filters that you add to the application menu. More than 4 might not be well displayed, even less, depending on the length of the labels. Filters should be grouped and act as radio buttons, that is, for any filter, at least another filter with a different/opposite action should exist. Actually, GtkRadioButtons can be easily used to accomplish this by having them grouped (using  like the next example shows) and by not drawing their indicator - with the function gtk_toggle_button_set_mode.

Once the menu is properly created and filled up with entries and filters, add the menu to a HildonWindow. To set and retrieve a window's menu, use the following functions:

	 <font color="#009900">void        <font color="#000000">hildon_window_set_app_menu       <font color="#990000">( HildonWindow <font color="#990000"> * self <font color="#990000">,                  	                             HildonAppMenu <font color="#990000"> * menu <font color="#990000">); HildonAppMenu <font color="#990000"> *  <font color="#000000">hildon_window_get_app_menu    <font color="#990000">( HildonWindow <font color="#990000"> * self <font color="#990000">); </tt>

The following example shows how to create and set up an application menu.

'''Example 3.1. Example of a Hildon application menu'''

  <font color="#000080"> #include                                        <font color="#FF0000">&lt;hildon/hildon.h&gt;  <font color="#0000FF">static  <font color="#009900">void  <font color="#000000">menu_button_clicked                             <font color="#990000">( GtkButton <font color="#990000"> * button <font color="#990000">,                                                   GtkLabel <font color="#990000"> * label <font color="#990000">) <font color="#FF0000">{  <font color="#0000FF">const  <font color="#009900">char  <font color="#990000"> * buttontext <font color="#990000"> =   <font color="#000000">gtk_button_get_label   <font color="#990000">( button <font color="#990000">); <font color="#009900">char <font color="#990000"> * text <font color="#990000"> =   <font color="#000000">g_strdup_printf   <font color="#990000">(  <font color="#FF0000">"Last option selected:  <font color="#CC33CC">\n  <font color="#FF0000">%s"  <font color="#990000">, buttontext <font color="#990000">);  <font color="#000000">gtk_label_set_text  <font color="#990000">( label <font color="#990000">, text <font color="#990000">);  <font color="#000000">g_free  <font color="#990000">( text <font color="#990000">);  <font color="#000000">g_debug  <font color="#990000">(  <font color="#FF0000">"Button clicked: %s"  <font color="#990000">, buttontext <font color="#990000">); <font color="#FF0000">}  <font color="#0000FF">static  HildonAppMenu <font color="#990000"> *  <font color="#000000">create_menu                                     <font color="#990000">( GtkWidget     <font color="#990000"> * label <font color="#990000">) <font color="#FF0000">{ <font color="#009900">int i <font color="#990000"> ; gchar <font color="#990000"> * command_id <font color="#990000"> ; GtkWidget <font color="#990000"> * button <font color="#990000"> ; HildonAppMenu <font color="#990000"> * menu <font color="#990000"> =  <font color="#000000">HILDON_APP_MENU   <font color="#990000">(   <font color="#000000">hildon_app_menu_new   <font color="#990000">);  <font color="#0000FF">for  <font color="#990000">( i <font color="#990000"> =  <font color="#993399">1  <font color="#990000"> ; i <font color="#990000">&lt;  <font color="#993399">6  <font color="#990000"> ; i <font color="#990000">++)  <font color="#FF0000">{  <font color="#9A1900">/* Create menu entries */  button <font color="#990000"> =  <font color="#000000">hildon_gtk_button_new   <font color="#990000">( HILDON_SIZE_AUTO <font color="#990000">); command_id <font color="#990000"> =  <font color="#000000">g_strdup_printf   <font color="#990000">(  <font color="#FF0000">"Menu command %d"  <font color="#990000">, i <font color="#990000">);  <font color="#000000">gtk_button_set_label  <font color="#990000">(   <font color="#000000">GTK_BUTTON   <font color="#990000">( button <font color="#990000">), command_id <font color="#990000">);  <font color="#9A1900">/* Attach callback to clicked signal */   <font color="#000000">g_signal_connect_after  <font color="#990000">( button <font color="#990000">,  <font color="#FF0000">"clicked"  <font color="#990000">,                               <font color="#000000">G_CALLBACK   <font color="#990000">( menu_button_clicked <font color="#990000">),                              label <font color="#990000">);  <font color="#9A1900">/* Add entry to the view menu */   <font color="#000000">hildon_app_menu_append  <font color="#990000">( menu <font color="#990000">,   <font color="#000000">GTK_BUTTON   <font color="#990000">( button <font color="#990000">)); <font color="#FF0000">}  <font color="#9A1900">/* Create filters */  button <font color="#990000"> =  <font color="#000000">hildon_gtk_radio_button_new   <font color="#990000">( HILDON_SIZE_AUTO <font color="#990000">, NULL <font color="#990000">);  <font color="#000000">gtk_button_set_label  <font color="#990000">(   <font color="#000000">GTK_BUTTON   <font color="#990000">( button <font color="#990000">),  <font color="#FF0000">"filter one"  <font color="#990000">);  <font color="#000000">g_signal_connect_after  <font color="#990000">( button <font color="#990000">,  <font color="#FF0000">"clicked"  <font color="#990000">,   <font color="#000000">G_CALLBACK   <font color="#990000">( menu_button_clicked <font color="#990000">), label <font color="#990000">);  <font color="#000000">hildon_app_menu_add_filter  <font color="#990000">( menu <font color="#990000">,   <font color="#000000">GTK_BUTTON   <font color="#990000">( button <font color="#990000">));  <font color="#000000">gtk_toggle_button_set_mode  <font color="#990000">(   <font color="#000000">GTK_TOGGLE_BUTTON   <font color="#990000">( button <font color="#990000">), FALSE <font color="#990000">); button <font color="#990000"> =  <font color="#000000">hildon_gtk_radio_button_new_from_widget   <font color="#990000">( HILDON_SIZE_AUTO <font color="#990000">,                                                       <font color="#000000">GTK_RADIO_BUTTON   <font color="#990000">( button <font color="#990000">));  <font color="#000000">gtk_button_set_label  <font color="#990000">(   <font color="#000000">GTK_BUTTON   <font color="#990000">( button <font color="#990000">),  <font color="#FF0000">"filter two"  <font color="#990000">);  <font color="#000000">g_signal_connect_after  <font color="#990000">( button <font color="#990000">,  <font color="#FF0000">"clicked"  <font color="#990000">,   <font color="#000000">G_CALLBACK   <font color="#990000">( menu_button_clicked <font color="#990000">), label <font color="#990000">);  <font color="#000000">hildon_app_menu_add_filter  <font color="#990000">( menu <font color="#990000">,   <font color="#000000">GTK_BUTTON   <font color="#990000">( button <font color="#990000">));  <font color="#000000">gtk_toggle_button_set_mode  <font color="#990000">(   <font color="#000000">GTK_TOGGLE_BUTTON   <font color="#990000">( button <font color="#990000">), FALSE <font color="#990000">);  <font color="#000000">gtk_widget_show_all  <font color="#990000">(   <font color="#000000">GTK_WIDGET   <font color="#990000">( menu <font color="#990000">));  <font color="#0000FF">return  menu <font color="#990000"> ; <font color="#FF0000">} <font color="#009900">int  <font color="#000000">main                                            <font color="#990000">(  <font color="#009900">int argc <font color="#990000">,                                                   <font color="#009900">char  <font color="#990000"> ** argv <font color="#990000">) <font color="#FF0000">{ GtkWidget <font color="#990000"> * win <font color="#990000"> ; GtkWidget <font color="#990000"> * label <font color="#990000"> ; GtkWidget <font color="#990000"> * label2 <font color="#990000"> ; GtkBox <font color="#990000"> * vbox <font color="#990000"> ; HildonAppMenu <font color="#990000"> * menu <font color="#990000"> ;  <font color="#000000">hildon_gtk_init  <font color="#990000">(&amp; argc <font color="#990000">,  <font color="#990000">&amp; argv <font color="#990000">); win <font color="#990000"> =  <font color="#000000">hildon_stackable_window_new   <font color="#990000">;  <font color="#9A1900">/* Create and pack labels */  label <font color="#990000"> =  <font color="#000000">gtk_label_new   <font color="#990000">(  <font color="#FF0000">"This is an example of the  <font color="#CC33CC">\n  <font color="#FF0000">HildonAppMenu widget.  <font color="#CC33CC">\n\n  <font color="#FF0000">"                           <font color="#FF0000">"Click on the titlebar  <font color="#CC33CC">\n  <font color="#FF0000">to pop up the menu."  <font color="#990000">); label2 <font color="#990000"> =  <font color="#000000">gtk_label_new   <font color="#990000">(  <font color="#FF0000">"No menu option has been selected yet."  <font color="#990000">);  <font color="#000000">gtk_label_set_justify  <font color="#990000">(   <font color="#000000">GTK_LABEL   <font color="#990000">( label <font color="#990000">), GTK_JUSTIFY_CENTER <font color="#990000">);  <font color="#000000">gtk_label_set_justify  <font color="#990000">(   <font color="#000000">GTK_LABEL   <font color="#990000">( label2 <font color="#990000">), GTK_JUSTIFY_CENTER <font color="#990000">); vbox <font color="#990000"> =  <font color="#000000">GTK_BOX   <font color="#990000">(   <font color="#000000">gtk_vbox_new   <font color="#990000">( FALSE <font color="#990000">,  <font color="#993399">10  <font color="#990000">));  <font color="#000000">gtk_box_pack_start  <font color="#990000">( vbox <font color="#990000">, label <font color="#990000">, TRUE <font color="#990000">, TRUE <font color="#990000">,  <font color="#993399">0  <font color="#990000">);  <font color="#000000">gtk_box_pack_start  <font color="#990000">( vbox <font color="#990000">, label2 <font color="#990000">, TRUE <font color="#990000">, TRUE <font color="#990000">,  <font color="#993399">0  <font color="#990000">);  <font color="#9A1900">/* Create menu */  menu <font color="#990000"> =  <font color="#000000">create_menu   <font color="#990000">( label2 <font color="#990000">);  <font color="#9A1900">/* Attach menu to the window */   <font color="#000000">hildon_window_set_app_menu  <font color="#990000">(   <font color="#000000">HILDON_WINDOW   <font color="#990000">( win <font color="#990000">), menu <font color="#990000">);  <font color="#9A1900">/* Add label's box to window */   <font color="#000000">gtk_container_add  <font color="#990000">(   <font color="#000000">GTK_CONTAINER   <font color="#990000">( win <font color="#990000">),   <font color="#000000">GTK_WIDGET   <font color="#990000">( vbox <font color="#990000">));  <font color="#000000">g_signal_connect  <font color="#990000">( win <font color="#990000">,  <font color="#FF0000">"delete_event"  <font color="#990000">,   <font color="#000000">G_CALLBACK   <font color="#990000">( gtk_main_quit <font color="#990000">), NULL <font color="#990000">);  <font color="#000000">gtk_widget_show_all  <font color="#990000">( win <font color="#990000">);  <font color="#000000">gtk_main  <font color="#990000">;  <font color="#0000FF">return  <font color="#993399">0  <font color="#990000"> ; <font color="#FF0000">} </tt>

Each entry and filter button in this example is attached to a function that simply changes a label in the main window.

Note that the function used to attach handlers to the entries is. So the handler are called after the default handler of the signal "clicked". The default handler for entries and filters closes the menu.

Application menus and views
The previous example had only one view. In applications with several views, you can attach a different menu to each view and add only the options that are relevant to the displayed view.

The function allows to set a menu to an HildonWindow and its descendant HildonStackableWindow widget.

	  <font color="#000000">hildon_window_set_app_menu  <font color="#990000">(   <font color="#000000">HILDON_STACKABLE_WINDOW   <font color="#990000">( win <font color="#990000">), menu <font color="#990000">); </tt>

Note that submenus are not supported by view menus. Usually a menu item that would have suboptions in a desktop application is implemented as a new subview in a Hildon application.

A callback function for a complex menu entry could create a new HildonStackableWindow to accomplish the task that the option refers to. The new window could contain a different view menu to contain buttons to perform the action. The buttons could even reside in the window area.

Context menu
Context menu is usually invoked via long press over an item on the screen, like holding a finger over an image thumbnail. The menu should contain commands directly related to the chosen item.

Avoid using context menus, because they are a hidden and inconvenient way of interacting with the UI. Use HildonAppMenus instead.

To create a GtkMenu in a Hildon application you should use the following function instead of :

GtkWidget <font color="#990000"> *  <font color="#000000">hildon_gtk_menu_new              <font color="#990000">(  <font color="#009900">void  <font color="#990000">); </tt>

This function creates a GtkMenu that allows Hildon specific styling.

When you use a GtkMenu in your Hildon application, consider how many menu items you are going to use, because screen space is limited. Also consider the fact that in the interests of keeping the UI clear, submenus are not allowed.