Legacy Maemo 5 Documentation/Graphical UI Tutorial/Getting started

=Getting started= Before starting to develop your Hildon applications you need to get and properly install and configure Maemo SDK. You can download and learn how to use the latest SDK in Maemo SDK Releases

To begin our introduction to Hildon, we start with the simplest program possible - base.c. This program creates a window and has no way of exiting except to be killed by using the shell.

'''Example 1.1. Simple Hildon program'''

   #include  &lt;hildon/hildon.h&gt; int  main   (  int   argc ,            char   * argv []  ) { GtkWidget  * window  ;  hildon_gtk_init  <font color="#990000">(&amp; argc <font color="#990000">,  <font color="#990000">&amp; argv <font color="#990000">);  <font color="#000000">g_set_application_name  <font color="#990000">(  <font color="#FF0000">"Simplest example"  <font color="#990000">); window <font color="#990000"> =  <font color="#000000">hildon_window_new   <font color="#990000">;  <font color="#000000">gtk_widget_show  <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>

To compile the program, use the following command:

gcc base.c `pkg-config hildon-1 --cflags --libs` -o base

All programs include hildon/hildon.h which declares the variables, functions, structures, etc. that are used in your Hildon application.

The next line is necessary in every program:

<tt>  <font color="#000000">hildon_gtk_init  <font color="#990000">(&amp; argc <font color="#990000">,  <font color="#990000">&amp; argv <font color="#990000">); </tt>

This function replaces a call to  and also initializes the Hildon library. This function also performs a call to g. for more information on GTK+ initialization and arguments that can pass to your application on the command line, see GTK+ Reference Manual. The next two lines of code create and display a window.

<tt> window <font color="#990000"> =  <font color="#000000">hildon_window_new   <font color="#990000">;  <font color="#000000">gtk_widget_show  <font color="#990000">( window <font color="#990000">); </tt>

The HildonWindow used in this example represents a top-level window in the Hildon framework. It is derived from GtkWindow and provides additional commodities specific to the Hildon framework.

In very simple applications, HildonWindow could be enough. However, in most of the applications a HildonStackableWindow should be used.

The  shows the widget (makes it visible) that would not be otherwise displayed.

The last line enters the GTK+ main processing loop.

<tt>  <font color="#000000">gtk_widget_main  <font color="#990000">;    <font color="#0000FF">static   <font color="#009900">void   <font color="#000000">hello   <font color="#990000">( GtkWidget <font color="#990000"> * widget <font color="#990000">,                       gpointer   data <font color="#990000">) <font color="#FF0000">{  <font color="#000000">g_print  <font color="#990000">(  <font color="#FF0000">"Hello World!  <font color="#CC33CC">\n  <font color="#FF0000">"  <font color="#990000">); <font color="#FF0000">} </tt>

This call exists in every GTK+ application and therefore in every Hildon application. When control reaches this point, GTK+ sleeps waiting for X events (for example, button or key presses), timeouts, or file IO notifications to occur. In our simple example, however, events are ignored.

Hello World in Hildon
To begin our introduction to Hildon, we introduce the classic Hello World, Hildon style. This program creates a window with a widget (a button).

'''Example 1.2. Hildon Hello World program'''

<tt>  <font color="#000080"> #include                                        <font color="#FF0000">&lt;hildon/hildon.h&gt; '' <font color="#9A1900">/* This is a callback function. The data arguments are ignored '' '' <font color="#9A1900"> * in this example. More on callbacks. */ ''  <font color="#0000FF">static   <font color="#009900">void   <font color="#000000">hello   <font color="#990000">( GtkWidget <font color="#990000"> * widget <font color="#990000">,                     gpointer   data <font color="#990000">) <font color="#FF0000">{  <font color="#000000">g_print  <font color="#990000">(  <font color="#FF0000">"Hello World!  <font color="#CC33CC">\n  <font color="#FF0000">"  <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">{ HildonProgram <font color="#990000"> * program <font color="#990000"> ; GtkWidget <font color="#990000"> * window <font color="#990000"> ; GtkWidget <font color="#990000"> * button <font color="#990000"> ; '' <font color="#9A1900">/* This is called in all Hildon applications. Arguments are parsed '' '' <font color="#9A1900">  * from the command line and are returned to the application. */ ''    <font color="#000000">hildon_gtk_init   <font color="#990000">(&amp; argc <font color="#990000">,  <font color="#990000">&amp; argv <font color="#990000">); '' <font color="#9A1900">/* Get an instance of HildonProgram. It is an object used to represent '' '' <font color="#9A1900">  * an application running in the Hildon framework. */ ''   program <font color="#990000"> =   <font color="#000000">hildon_program_get_instance   <font color="#990000">;  <font color="#9A1900">/* create a new hildon window */  window <font color="#990000"> =  <font color="#000000">hildon_window_new   <font color="#990000">;  <font color="#9A1900">/* Registers a window as belonging to the program */   <font color="#000000">hildon_program_add_window  <font color="#990000">( program <font color="#990000">,   <font color="#000000">HILDON_WINDOW   <font color="#990000">( window <font color="#990000">));  <font color="#9A1900">/* When the window is given the "delete_event" signal (this is given   <font color="#9A1900">   * by the window manager, usually by the "close" option, or on the    <font color="#9A1900">   * titlebar), we ask it to call the delete_event  function  '' <font color="#9A1900">  * as defined above. The data passed to the callback '' '' <font color="#9A1900">  * function is NULL and is ignored in the callback function. */ ''    <font color="#000000">g_signal_connect   <font color="#990000">(   <font color="#000000">G_OBJECT   <font color="#990000">( window <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="#9A1900">/* Create a new hildon button with its title label set to "Hello world!",   <font color="#9A1900">  * also size and type of arrangement is set */  button <font color="#990000"> =  <font color="#000000">hildon_button_new_with_text   <font color="#990000">( HILDON_SIZE_AUTO <font color="#990000">,                                          HILDON_BUTTON_ARRANGEMENT_VERTICAL <font color="#990000">,                                          <font color="#FF0000">"Hello world!"  <font color="#990000">,                                          NULL <font color="#990000">);  <font color="#9A1900">/* When the button is given the "clicked" signal, we ask it to call the  '' <font color="#9A1900">  * hello  function as defined above. The data passed to the callback '' '' <font color="#9A1900">  * function is NULL and is ignored in the callback function. */ ''    <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">( hello <font color="#990000">), NULL <font color="#990000">); '' <font color="#9A1900">/* This packs the button into the window (a GTK+ container). */ ''    <font color="#000000">gtk_container_add   <font color="#990000">(   <font color="#000000">GTK_CONTAINER   <font color="#990000">( window <font color="#990000">),                       button <font color="#990000">);  <font color="#9A1900">/* The final step is to display this newly created widget  '' <font color="#9A1900">  * and all widgets it contains. */ ''    <font color="#000000">gtk_widget_show_all   <font color="#990000">(   <font color="#000000">GTK_WIDGET   <font color="#990000">( window <font color="#990000">)); '' <font color="#9A1900">/* All GTK+ applications must have a gtk_main. Control ends here ''  <font color="#9A1900">  * and waits for an event to occur (like a key press or   '' <font color="#9A1900">   * mouse event). */ ''    <font color="#000000">gtk_main   <font color="#990000">;  <font color="#0000FF">return  <font color="#993399">0  <font color="#990000"> ; <font color="#FF0000">} </tt>

As you can see in this simple example, programming Hildon application is slightly different from common GTK+ applications programming. We are going to review these differences through the next chapters.

Compiling Hello World
To compile use the following:

gcc hello-world.c `pkg-config hildon-1 --cflags --libs` -o hello

This uses the program pkg-config, which can be obtained from Freedesktop. The program reads the .pc which comes with Hildon to determine what compiler switches are needed to compile programs that use Hildon. pkg-config hildon-1 -cflags outputs a list of include directories for the compiler to look in, and pkg-config hildon-1 -libs outputs the list of libraries for the compiler to link with and the directories to find them in. Although they could be used separately as `pkg-config hildon-1 -cflags` and `pkg-config hildon-1 -libs`, in the above example they were joint as a single instance because it is easier to understand.

Note

Note that the type of single quote used in the compile command above is significant.

Stepping through Hello World
This section explains the Hello World example above step by step.

The following lines define the callback function that is called when the button is "clicked". We ignore both the widget and the data in this example, but usually developers would need to ???do things with them.

<tt>  <font color="#000000">gtk_widget_main  <font color="#990000">;    <font color="#0000FF">static   <font color="#009900">void   <font color="#000000">hello   <font color="#990000">( GtkWidget <font color="#990000"> * widget <font color="#990000">,                       gpointer   data <font color="#990000">) <font color="#FF0000">{  <font color="#000000">g_print  <font color="#990000">(  <font color="#FF0000">"Hello World!  <font color="#CC33CC">\n  <font color="#FF0000">"  <font color="#990000">); <font color="#FF0000">} </tt>

The following is the definition of the main function ???like it is usually done in programs written in C.

<tt>  <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">{ </tt>

Next code declares pointers to an object of type HildonProgram which represents an application running in the Hildon framework. Pointers to the widgets which show the application are also declared.

<tt> HildonProgram <font color="#990000"> * program <font color="#990000"> ; GtkWidget <font color="#990000"> * window <font color="#990000"> ; GtkWidget <font color="#990000"> * button <font color="#990000"> ; </tt>

To use Hildon, initialize it first. Initialization connects to the window system display, and parses some standard command line arguments.

<tt>  <font color="#000000">hildon_gtk_init  <font color="#990000">(&amp; argc <font color="#990000">,  <font color="#990000">&amp; argv <font color="#990000">); </tt>

Only one HildonProgram can be created per process. Use  to access it.

<tt>program <font color="#990000"> =  <font color="#000000">hildon_program_get_instance   <font color="#990000">; </tt>

In this simple example a new HildonWindow is created. In cases with nested views, a HildonStackableWindow should be used.

<tt>window <font color="#990000"> =  <font color="#000000">hildon_window_new   <font color="#990000">; </tt>

This call registers a window as belonging to the program. This allows applying program-wide settings to all the registered windows, such as, for example, assigning a common menu to all the registered windows by setting it to the program.

<tt>  <font color="#000000">hildon_program_add_window  <font color="#990000">( program <font color="#990000">,   <font color="#000000">HILDON_WINDOW   <font color="#990000">( window <font color="#990000">)) </tt>

The following code is an example of connecting a signal handler to an object, in this case, the window. The function  is set as a handler to the "delete_event" signal. The function tells GTK+ that it must exit from gtk_main when control is returned to it, making the program terminate.

<tt>   <font color="#000000">g_signal_connect   <font color="#990000">(   <font color="#000000">G_OBJECT   <font color="#990000">( window <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">); </tt>

This call creates a new HildonButton. This button allows to set two labels, one main label and another secondary one. You can also set the size of the button and the order of the labels. Notice that you can use GtkButton's in Hildon applications in case you don't need those additional features.

<tt>	button <font color="#990000"> =  <font color="#000000">hildon_button_new_with_text   <font color="#990000">( HILDON_SIZE_AUTO <font color="#990000">,                                          HILDON_BUTTON_ARRANGEMENT_VERTICAL <font color="#990000">,                                          <font color="#FF0000">"Hello world!"  <font color="#990000">,                                          NULL <font color="#990000">); </tt>

Here a signal handler is attached to the newly created button so that when it emits the "clicked" signal, our  function is called. The data is ignored, so we simply pass in NULL to the  callback function. Obviously, the "clicked" signal is emitted when the button is pressed.

<tt>  <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">( hello <font color="#990000">), NULL <font color="#990000">); </tt>

This packing call tells GTK+ to place the button in the window. For more information, see the Packing Widgets section of the GTK+ 2.0 Tutorial.

<tt>  <font color="#000000">gtk_container_add  <font color="#990000">(   <font color="#000000">GTK_CONTAINER   <font color="#990000">( window <font color="#990000">),                       button <font color="#990000">); </tt>

Once everything is set up, with all signal handlers in place and the button placed in the window, we ask GTK to "show" the widgets on the screen.

<tt>  <font color="#000000">gtk_widget_show_all  <font color="#990000">(   <font color="#000000">GTK_WIDGET   <font color="#990000">( window <font color="#990000">)); </tt>

And of course, we call  which waits for events to come from the X server and calls on the widgets to emit signals when these events come.

<tt>  <font color="#000000">gtk_main  <font color="#990000">; </tt>

And the final return. Control returns here after  is called.

<tt>  <font color="#0000FF">return  <font color="#993399">0  <font color="#990000"> ; </tt>