Legacy Maemo 5 Documentation/Graphical UI Tutorial/Navigation

=Navigation = In previous examples only one widget was added to a window so we could simply use a  to "pack" it into the window. To pack more than one widget into a window, use container widgets.

To develop Hildon user interfaces, you can use any container widget provided by GTK+. For more information, see the GTK+ 2.0 Tutorial.

Apart from supporting those containers, the Hildon framework also provides a new container widget called HildonPannableArea, a scrolling widget designed for touch screens.

Pannable area
HildonPannableArea is used to create an area with another widget inside which is accessible regardless of the size using the touchscreen to scroll it. You may insert any type of widget into a pannable area and it is always accessible by using the fingers to pan, or scroll, in the area.

This widget can be "panned" (scrolled) in any direction using the touchscreen with the fingers. One remarkable characteristic is that the scrolling is "kinetic", meaning that the motion continues from the initial motion by gradually slowing down to stop.

The use of this widget is very similar to GTK+ scrolled windows. In fact, both widgets implement a similar concept and its APIs are very similar.

To create a new pannable area, choose one of the following functions:

GtkWidget  *  hildon_pannable_area_new         (  void  ); GtkWidget  *  hildon_pannable_area_new_full    ( gint mode ,                                               gboolean enabled ,                                               gdouble vel_min ,                                               gdouble vel_max ,                                               gdouble decel ,                                               guint sps ); 

The first one creates a new pannable area with the properties set to the default values. The second one allows you to set the value of the most important properties of this widget:


 * mode: Used to change the behaviour of the pannable area allowing to choose whether to use the "kinetic" effect described above.
 * enabled: Enable or disable finger-scroll.
 * vel_min, vel_max: Allows developers to adjust how many pixels the child widget moves per "frame".
 * decel: Value for the deceleration property.
 * sps: Amount of scroll events to generate per second.

The default values of these properties are right for most applications and so the simpler constructor is sufficient. For more information on the properties of the pannable area, see the Hildon reference manual.

Once the area is created, you can place your object into the pannable window using the following function.

 <font color="#009900">void        <font color="#000000">hildon_pannable_area_add_with_viewport   <font color="#990000">( HildonPannableArea <font color="#990000"> * area <font color="#990000">,                                                      GtkWidget <font color="#990000"> * child <font color="#990000">); </tt>

That is a convenience function used to add a child to a GtkViewport, and add the viewport to the pannable area.

Warning

Widgets with native scrolling should be added directly inside a pannable area. Widgets such as, for example, GtkTextView, GtkTreeView, GtkIconView and GtkLayout should be added by calling. Otherwise, panning could not work properly.

Pannable area example
Functions explained above are enough for a simple example. The following example packs a table with 100 toggle buttons into a pannable area.

@@IMAGE@@: examples/hildon_pannable_area_example.c output (code )

'''Example 5.1. Example of a pannable area'''

  <font color="#000080"> #include  <font color="#FF0000">&lt;stdio.h&gt;  <font color="#000080"> #include  <font color="#FF0000">&lt;hildon/hildon.h&gt;  <font color="#0000FF">static  GtkWidget <font color="#990000"> *  <font color="#000000">create_table   <font color="#990000"> <font color="#FF0000">{ GtkWidget <font color="#990000"> * hbox <font color="#990000"> ; GtkWidget <font color="#990000"> * table <font color="#990000"> ; GtkWidget <font color="#990000"> * button <font color="#990000"> ; <font color="#009900">char buffer <font color="#990000">[ <font color="#993399">32  <font color="#990000">]; <font color="#009900">int i <font color="#990000">, j <font color="#990000"> ; '' <font color="#9A1900">/* create a table of 10 by 10 squares. */ ''   table <font color="#990000"> =   <font color="#000000">gtk_table_new   <font color="#990000">(  <font color="#993399">10  <font color="#990000">,  <font color="#993399">10  <font color="#990000">, FALSE <font color="#990000">);  <font color="#9A1900">/* set the spacing to 10 on x and 10 on y */   <font color="#000000">gtk_table_set_row_spacings  <font color="#990000">(   <font color="#000000">GTK_TABLE   <font color="#990000">( table <font color="#990000">),  <font color="#993399">10  <font color="#990000">);  <font color="#000000">gtk_table_set_col_spacings  <font color="#990000">(   <font color="#000000">GTK_TABLE   <font color="#990000">( table <font color="#990000">),  <font color="#993399">10  <font color="#990000">);  <font color="#000000">gtk_widget_show  <font color="#990000">( table <font color="#990000">);  <font color="#9A1900">/* this simply creates a grid of toggle buttons on the table  '' <font color="#9A1900">  * to demonstrate the scrolled window. */ ''    <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">10  <font color="#990000"> ; i <font color="#990000">++)  <font color="#0000FF">for  <font color="#990000">( j <font color="#990000"> =  <font color="#993399">1  <font color="#990000"> ; j <font color="#990000">&lt;  <font color="#993399">10  <font color="#990000"> ; j <font color="#990000">++)  <font color="#FF0000">{  <font color="#000000">sprintf  <font color="#990000">( buffer <font color="#990000">,  <font color="#FF0000">"button (%d,%d)  <font color="#CC33CC">\n  <font color="#FF0000">"  <font color="#990000">, i <font color="#990000">, j <font color="#990000">); button <font color="#990000"> =  <font color="#000000">gtk_toggle_button_new_with_label   <font color="#990000">( buffer <font color="#990000">);  <font color="#000000">gtk_table_attach_defaults  <font color="#990000">(   <font color="#000000">GTK_TABLE   <font color="#990000">( table <font color="#990000">), button <font color="#990000">,                                   i <font color="#990000">, i <font color="#990000">+  <font color="#993399">1  <font color="#990000">, j <font color="#990000">, j <font color="#990000">+  <font color="#993399">1  <font color="#990000">); <font color="#FF0000">}  <font color="#0000FF">return  table <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="#990000">) <font color="#FF0000">{  <font color="#0000FF">static  GtkWidget <font color="#990000"> * window <font color="#990000"> ; GtkWidget <font color="#990000"> * button <font color="#990000"> ; GtkWidget <font color="#990000"> * pannable_area <font color="#990000"> ; GtkWidget <font color="#990000"> * table <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">); window <font color="#990000"> =  <font color="#000000">hildon_stackable_window_new   <font color="#990000">;  <font color="#000000">g_signal_connect  <font color="#990000">(   <font color="#000000">G_OBJECT   <font color="#990000">( window <font color="#990000">),  <font color="#FF0000">"destroy"  <font color="#990000">,                       <font color="#000000">G_CALLBACK   <font color="#990000">( gtk_widget_destroy <font color="#990000">), NULL <font color="#990000">); pannable_area <font color="#990000"> =  <font color="#000000">hildon_pannable_area_new   <font color="#990000">; table <font color="#990000"> =  <font color="#000000">create_table   <font color="#990000">;  <font color="#9A1900">/* pack the table into the scrolled window */   <font color="#000000">hildon_pannable_area_add_with_viewport  <font color="#990000">(       <font color="#000000">HILDON_PANNABLE_AREA   <font color="#990000">( pannable_area <font color="#990000">), table <font color="#990000">);  <font color="#9A1900">/* Add the box into the window*/   <font color="#000000">gtk_container_add  <font color="#990000">(   <font color="#000000">GTK_CONTAINER   <font color="#990000">( window <font color="#990000">), pannable_area <font color="#990000">);  <font color="#000000">gtk_widget_show_all  <font color="#990000">( window <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>

In the example above, two calls are enough to use a pannable area. The rest of the code in the example is no different from that used in a GTK+ application.

 '' <font color="#9A1900">/* Create a new pannable area. */ ''   pannable_area <font color="#990000"> =   <font color="#000000">hildon_pannable_area_new   <font color="#990000">;  <font color="#9A1900">/* Pack the table into the pannable area */   <font color="#000000">hildon_pannable_area_add_with_viewport  <font color="#990000">(   <font color="#000000">HILDON_PANNABLE_AREA   <font color="#990000">( pannable_area <font color="#990000">), table <font color="#990000">); </tt>

To see all the buttons, users can scroll with the fingers. In this example, horizontal and vertical panning are activated as that is needed to allow users to be able to interact both all the widgets. The property "mov-mode" controls if the area can scroll horizontally, vertically (default value) or both, using HILDON_MOVEMENT_MODE_HORIZ, HILDON_MOVEMENT_MODE_VERT or HILDON_MOVEMENT_MODE_BOTH, respectively.

Additional features
Pannable areas provide a set of convenience functions that make it easier to move to a certain element inside the area without users interaction.

These functions allow to scroll or jump to a position which ensures that a certain point or a certain child widget is visible for the user.

For example, the first of the functions changes the current position on the pannable area to ensure position (x,y) is visible. The movement is a quick jump. The second function performs a smoothly scroll towards the selected position.

 <font color="#009900">void        <font color="#000000">hildon_pannable_area_jump_to     <font color="#990000">( HildonPannableArea <font color="#990000"> * area <font color="#990000">,                                                <font color="#0000FF">const  gint x <font color="#990000">,                                                <font color="#0000FF">const  gint y <font color="#990000">); <font color="#009900">void        <font color="#000000">hildon_pannable_area_scroll_to   <font color="#990000">( HildonPannableArea <font color="#990000"> * area <font color="#990000">,                                                <font color="#0000FF">const  gint x <font color="#990000">,                                                <font color="#0000FF">const  gint y <font color="#990000">); </tt>

The following functions make it possible to jump or scroll to a certain descendent of the area. The argument should be a reference to a descendent widget.

 <font color="#009900">void        <font color="#000000">hildon_pannable_area_jump_to_child   <font color="#990000">( HildonPannableArea <font color="#990000"> * area <font color="#990000">,                                                  GtkWidget <font color="#990000"> * child <font color="#990000">); <font color="#009900">void       hildon_pannable_area_scroll_to_child <font color="#990000">( HildonPannableArea <font color="#990000"> * area <font color="#990000">,                                              GtkWidget <font color="#990000"> * child <font color="#990000">); </tt>

Here is a modified version of the previous example. The pannable area is packed into an GtkVBox and a new button is also added to navigate to the last clicked button.

'''Example 5.2. Example of a pannable area and a "jump-to" button'''

  <font color="#000080"> #include  <font color="#FF0000">&lt;stdio.h&gt;  <font color="#000080"> #include  <font color="#FF0000">&lt;hildon/hildon.h&gt; GtkWidget <font color="#990000"> * last_clicked_button <font color="#990000"> ; GtkWidget <font color="#990000"> * pannable_area <font color="#990000"> ;  <font color="#0000FF">static  <font color="#009900">void   <font color="#000000">destroy   <font color="#990000">( GtkWidget <font color="#990000"> * widget <font color="#990000">,                       gpointer   data <font color="#990000">) <font color="#FF0000">{  <font color="#000000">gtk_main_quit  <font color="#990000">; <font color="#FF0000">}  <font color="#0000FF">static  <font color="#009900">void   <font color="#000000">clicked   <font color="#990000">( GtkButton <font color="#990000"> * button <font color="#990000">,                       gpointer   user_data <font color="#990000">) <font color="#FF0000">{ last_clicked_button <font color="#990000"> =  <font color="#000000">GTK_WIDGET   <font color="#990000">( button <font color="#990000">); <font color="#FF0000">}  <font color="#0000FF">static  <font color="#009900">void   <font color="#000000">go_to_last_clicked   <font color="#990000">( GtkButton <font color="#990000"> * button <font color="#990000">,                                  gpointer   user_data <font color="#990000">) <font color="#FF0000">{  <font color="#9A1900">/*     hildon_pannable_area_jump_to_child (HILDON_PANNABLE_AREA (pannable_area), */    <font color="#9A1900">/*                                          last_clicked_button); */   <font color="#000000">hildon_pannable_area_jump_to_child    <font color="#990000">(   <font color="#000000">HILDON_PANNABLE_AREA   <font color="#990000">( pannable_area <font color="#990000">),                                           last_clicked_button <font color="#990000">); <font color="#FF0000">}  <font color="#0000FF">static  GtkWidget <font color="#990000"> *  <font color="#000000">create_table   <font color="#990000"> <font color="#FF0000">{ GtkWidget <font color="#990000"> * hbox <font color="#990000"> ; GtkWidget <font color="#990000"> * table <font color="#990000"> ; GtkWidget <font color="#990000"> * button <font color="#990000"> ; <font color="#009900">char buffer <font color="#990000">[ <font color="#993399">32  <font color="#990000">]; <font color="#009900">int i <font color="#990000">, j <font color="#990000"> ; '' <font color="#9A1900">/* create a table of 10 by 10 squares. */ ''     table <font color="#990000"> =   <font color="#000000">gtk_table_new   <font color="#990000">(  <font color="#993399">10  <font color="#990000">,  <font color="#993399">10  <font color="#990000">, FALSE <font color="#990000">);  <font color="#9A1900">/* set the spacing to 10 on x and 10 on y */   <font color="#000000">gtk_table_set_row_spacings  <font color="#990000">(   <font color="#000000">GTK_TABLE   <font color="#990000">( table <font color="#990000">),  <font color="#993399">10  <font color="#990000">);  <font color="#000000">gtk_table_set_col_spacings  <font color="#990000">(   <font color="#000000">GTK_TABLE   <font color="#990000">( table <font color="#990000">),  <font color="#993399">10  <font color="#990000">);  <font color="#000000">gtk_widget_show  <font color="#990000">( table <font color="#990000">);  <font color="#9A1900">/* this simply creates a grid of toggle buttons on the table  '' <font color="#9A1900">    * to demonstrate the scrolled window. */ ''      <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">10  <font color="#990000"> ; i <font color="#990000">++)  <font color="#0000FF">for  <font color="#990000">( j <font color="#990000"> =  <font color="#993399">1  <font color="#990000"> ; j <font color="#990000">&lt;  <font color="#993399">10  <font color="#990000"> ; j <font color="#990000">++)  <font color="#FF0000">{  <font color="#000000">sprintf  <font color="#990000">( buffer <font color="#990000">,  <font color="#FF0000">"button (%d,%d)  <font color="#CC33CC">\n  <font color="#FF0000">"  <font color="#990000">, i <font color="#990000">, j <font color="#990000">); button <font color="#990000"> =  <font color="#000000">gtk_toggle_button_new_with_label   <font color="#990000">( buffer <font color="#990000">);  <font color="#000000">g_signal_connect  <font color="#990000">(   <font color="#000000">G_OBJECT   <font color="#990000">( button <font color="#990000">),                              <font color="#FF0000">"clicked"  <font color="#990000">,                               <font color="#000000">G_CALLBACK   <font color="#990000">( clicked <font color="#990000">),                              NULL <font color="#990000">);  <font color="#000000">gtk_table_attach_defaults  <font color="#990000">(   <font color="#000000">GTK_TABLE   <font color="#990000">( table <font color="#990000">), button <font color="#990000">,  	                             i <font color="#990000">, i <font color="#990000">+  <font color="#993399">1  <font color="#990000">, j <font color="#990000">, j <font color="#990000">+  <font color="#993399">1  <font color="#990000">);  <font color="#000000">gtk_widget_show  <font color="#990000">( button <font color="#990000">); <font color="#FF0000">}  <font color="#0000FF">return  table <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="#990000">) <font color="#FF0000">{  <font color="#0000FF">static  GtkWidget <font color="#990000"> * window <font color="#990000"> ;  <font color="#9A1900">//   GtkWidget *pannable_area;  GtkWidget <font color="#990000"> * button <font color="#990000"> ; GtkWidget <font color="#990000"> * table <font color="#990000"> ; GtkWidget <font color="#990000"> * hbox <font color="#990000"> ;  <font color="#000000">gtk_init  <font color="#990000">(&amp; argc <font color="#990000">,  <font color="#990000">&amp; argv <font color="#990000">);  <font color="#9A1900">/* Create a new hildon window for the pannable area to be  '' <font color="#9A1900">    * packed into. */ ''     window <font color="#990000"> =   <font color="#000000">hildon_stackable_window_new   <font color="#990000">;  <font color="#000000">g_signal_connect  <font color="#990000">(   <font color="#000000">G_OBJECT   <font color="#990000">( window <font color="#990000">),  <font color="#FF0000">"destroy"  <font color="#990000">,  		       <font color="#000000">G_CALLBACK   <font color="#990000">( destroy <font color="#990000">), NULL <font color="#990000">);  <font color="#000000">gtk_window_set_title  <font color="#990000">(   <font color="#000000">GTK_WINDOW   <font color="#990000">( window <font color="#990000">),  <font color="#FF0000">"HildonPannableArea example"  <font color="#990000">);  <font color="#9A1900">/* Create a table which will be inside the pannable area */  table <font color="#990000"> =  <font color="#000000">create_table   <font color="#990000">; hbox <font color="#990000"> =  <font color="#000000">gtk_hbox_new   <font color="#990000">( FALSE <font color="#990000">,  <font color="#993399">0  <font color="#990000">);  <font color="#9A1900">/* Add a button to jump */   <font color="#9A1900">/* Button to go to last clicked */  button <font color="#990000"> =  <font color="#000000">gtk_button_new_with_label   <font color="#990000">(  <font color="#FF0000">"Go to last clicked"  <font color="#990000">);  <font color="#000000">gtk_widget_show  <font color="#990000">( button <font color="#990000">);  <font color="#000000">g_signal_connect  <font color="#990000">(   <font color="#000000">G_OBJECT   <font color="#990000">( button <font color="#990000">),                        <font color="#FF0000">"clicked"  <font color="#990000">,                         <font color="#000000">G_CALLBACK   <font color="#990000">( go_to_last_clicked <font color="#990000">),                        NULL <font color="#990000">);  <font color="#000000">gtk_box_pack_start  <font color="#990000">(   <font color="#000000">GTK_BOX   <font color="#990000">( hbox <font color="#990000">),                          button <font color="#990000">,                          TRUE <font color="#990000">,                          TRUE <font color="#990000">,                          <font color="#993399">0  <font color="#990000">); '' <font color="#9A1900">/* create a new pannable area. */ ''     pannable_area <font color="#990000"> =   <font color="#000000">hildon_pannable_area_new   <font color="#990000">;  <font color="#9A1900">/* pack the table into the scrolled window */   <font color="#000000">hildon_pannable_area_add_with_viewport  <font color="#990000">(                      <font color="#000000">HILDON_PANNABLE_AREA   <font color="#990000">( pannable_area <font color="#990000">), table <font color="#990000">);  <font color="#000000">gtk_box_pack_start  <font color="#990000">(   <font color="#000000">GTK_BOX   <font color="#990000">( hbox <font color="#990000">),                          pannable_area <font color="#990000">,                          TRUE <font color="#990000">,                          TRUE <font color="#990000">,                          <font color="#993399">0  <font color="#990000">);  <font color="#9A1900">//   gtk_container_add (GTK_CONTAINER (hbox), pannable_area);   <font color="#9A1900">//HACK THAT SHOULD DETROYED   <font color="#000000">g_object_set  <font color="#990000">(   <font color="#000000">G_OBJECT   <font color="#990000">( pannable_area <font color="#990000">),                    <font color="#FF0000">"mov-mode"  <font color="#990000">, HILDON_MOVEMENT_MODE_BOTH <font color="#990000">);  <font color="#9A1900">//   gtk_container_set_border_width (GTK_CONTAINER (pannable_area), 10);   <font color="#9A1900">/* Adding pannable area into window*/   <font color="#000000">gtk_container_add  <font color="#990000">(   <font color="#000000">GTK_CONTAINER   <font color="#990000">( window <font color="#990000">), pannable_area <font color="#990000">);  <font color="#000000">gtk_widget_show  <font color="#990000">( hbox <font color="#990000">);  <font color="#000000">gtk_widget_show_all  <font color="#990000">( window <font color="#990000">);  <font color="#000000">gtk_widget_show  <font color="#990000">( pannable_area <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>

The example used a global variable to store a reference to the last clicked button. This reference is used by the callback go_to_last_clicked to jump to it by calling one of the navigation functions. This function is used as a handler for the signal "clicked" of the button outside the pannable area.

To test the different navigation functions, just change the call in the callback.

When you use the navigation functions that allow to navigate to a certain child, the widget must be already realized. Use the GTK_WIDGET_REALIZED macro to check it. If you want to call it during the initialization process, use the navigation function inside a callback to the "realized" signal, connecting it with.