Legacy Maemo 5 Documentation/Graphical UI Tutorial/Getting started
(→Getting started) |
(wikify slightly) |
||
(3 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
- | + | {{Legacy documentation}} | |
+ | |||
Before starting to develop your Hildon applications you need to get, install and properly configure the Maemo SDK. You can download and learn how to use the latest SDK in [http://maemo.org/development/sdks/ Maemo SDK Releases]. | Before starting to develop your Hildon applications you need to get, install and properly configure the Maemo SDK. You can download and learn how to use the latest SDK in [http://maemo.org/development/sdks/ 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. | + | To begin our introduction to Hildon, we start with the simplest program possible - <code>base.c</code>. 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''' | '''Example 1.1. Simple Hildon program''' | ||
- | + | <source lang="c"> | |
- | + | #include <hildon/hildon.h> | |
- | + | int main( int argc, | |
- | + | char *argv[] ) | |
- | + | { | |
- | + | GtkWidget *window; | |
- | + | hildon_gtk_init (&argc, &argv); | |
- | + | g_set_application_name ("Simplest example"); | |
- | + | window = hildon_window_new (); | |
- | + | gtk_widget_show (window); | |
- | + | gtk_main (); | |
- | + | return 0; | |
- | + | } | |
+ | </source> | ||
To compile the program, use the following command: | To compile the program, use the following command: | ||
+ | gcc base.c `pkg-config hildon-1 --cflags --libs` -o base | ||
- | + | All programs include <code>hildon/hildon.h</code> which declares the variables, functions, structures, and so on, that are used in your Hildon application. | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | All programs include hildon/hildon.h which declares the variables, functions, structures, and so on, that are used in your Hildon application. | + | |
The next line is necessary in every program: | The next line is necessary in every program: | ||
- | + | <source lang="c"> | |
+ | hildon_gtk_init (&argc, &argv); | ||
+ | </source> | ||
This function replaces a call to <code>gtk_init()</code> and also initializes the Hildon library. This function also performs a call to <code>gtk_init()</code>. For more information on GTK+ initialization and arguments that can be passed to your application on the command line, see [http://library.gnome.org/devel/gtk/unstable/gtk-General.html#gtk-init GTK+ Reference Manual]. The next two lines of code create and display a window. | This function replaces a call to <code>gtk_init()</code> and also initializes the Hildon library. This function also performs a call to <code>gtk_init()</code>. For more information on GTK+ initialization and arguments that can be passed to your application on the command line, see [http://library.gnome.org/devel/gtk/unstable/gtk-General.html#gtk-init GTK+ Reference Manual]. The next two lines of code create and display a window. | ||
- | + | <source lang="c"> | |
- | + | window = hildon_window_new (); | |
- | + | gtk_widget_show (window); | |
+ | </source> | ||
- | 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. | + | The <code>HildonWindow</code> used in this example represents a top-level window in the Hildon framework. It is derived from <code>GtkWindow</code> and provides additional commodities specific to the Hildon framework. |
- | In very simple applications, HildonWindow can be enough. However, in most of the applications, use a HildonStackableWindow. | + | In very simple applications, <code>HildonWindow</code> can be enough. However, in most of the applications, use a <code>HildonStackableWindow</code>. |
The <code>gtk_widget_show()</code> shows the widget (makes it visible) that would not be otherwise displayed. | The <code>gtk_widget_show()</code> shows the widget (makes it visible) that would not be otherwise displayed. | ||
Line 49: | Line 50: | ||
The last line enters the GTK+ main processing loop. | The last line enters the GTK+ main processing loop. | ||
- | + | <source lang="c"> | |
- | + | gtk_main (); | |
+ | </source> | ||
- | 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 | + | 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 I/O 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). | 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''' | '''Example 1.2. Hildon Hello World program''' | ||
- | + | <source lang="c"> | |
- | + | #include <hildon/hildon.h> | |
- | + | ||
- | + | /* This is a callback function. The data arguments are ignored | |
- | + | * in this example. More on callbacks . */ | |
- | + | static void hello (GtkWidget *widget, | |
- | + | gpointer data) | |
- | + | { | |
- | + | g_print ("Hello World!\n"); | |
- | + | } | |
- | + | ||
- | + | int | |
- | + | main (int argc, | |
- | + | char **argv) | |
- | + | { | |
- | + | HildonProgram *program; | |
- | + | GtkWidget *window; | |
- | + | GtkWidget *button; | |
- | + | ||
- | + | /* This is called in all Hildon applications. Arguments are parsed | |
- | + | * from the command line and are returned to the application. */ | |
- | + | hildon_gtk_init (&argc, &argv); | |
- | + | ||
- | + | /* Get an instance of HildonProgram. It is an object used to represent | |
- | + | * an application running in the Hildon framework. */ | |
- | + | program = hildon_program_get_instance (); | |
- | + | ||
- | + | /* create a new hildon window */ | |
- | + | window = hildon_window_new (); | |
- | + | ||
- | + | /* Registers a window as belonging to the program */ | |
- | + | hildon_program_add_window (program, HILDON_WINDOW (window)); | |
- | + | ||
- | + | /* When the window is given the "delete_event" signal (this is given | |
- | + | * by the window manager, usually by the "close" option, or on the | |
- | + | * titlebar), we ask it to call the delete_event () function | |
- | + | * as defined above. The data passed to the callback | |
- | + | * function is NULL and is ignored in the callback function. */ | |
- | + | g_signal_connect (G_OBJECT (window), "delete_event", | |
- | + | G_CALLBACK (gtk_main_quit), NULL); | |
- | + | ||
- | + | /* Create a new hildon button with its title label set to "Hello world!", | |
- | + | * also size and type of arrangement is set */ | |
- | + | button = hildon_button_new_with_text (HILDON_SIZE_AUTO, | |
- | + | HILDON_BUTTON_ARRANGEMENT_VERTICAL, | |
- | + | "Hello world!", | |
- | + | NULL); | |
- | + | ||
- | + | /* When the button is given the "clicked" signal, we ask it to call the | |
- | + | * hello () function as defined above. The data passed to the callback | |
- | + | * function is NULL and is ignored in the callback function. */ | |
- | + | g_signal_connect (G_OBJECT (button), "clicked", | |
- | + | G_CALLBACK (hello), NULL); | |
- | + | ||
- | + | /* This packs the button into the window (a GTK+ container). */ | |
- | + | gtk_container_add (GTK_CONTAINER (window), | |
- | + | button); | |
- | + | ||
- | + | /* The final step is to display this newly created widget | |
- | + | * and all widgets it contains. */ | |
- | + | gtk_widget_show_all (GTK_WIDGET (window)); | |
- | + | ||
- | + | /* All GTK+ applications must have a gtk_main(). Control ends here | |
- | + | * and waits for an event to occur (like a key press or | |
- | + | * mouse event). */ | |
- | + | gtk_main (); | |
- | + | ||
- | + | return 0; | |
- | + | } | |
+ | </source> | ||
As you can see in this simple example, writing Hildon applications is slightly different from writing standard GTK+ applications. We are going to review these differences through the next chapters. | As you can see in this simple example, writing Hildon applications is slightly different from writing standard GTK+ applications. We are going to review these differences through the next chapters. | ||
==Compiling Hello World == | ==Compiling Hello World == | ||
+ | |||
To compile, use the following command: | To compile, use the following command: | ||
- | + | gcc hello-world.c `pkg-config hildon-1 --cflags --libs` -o hello | |
- | + | ||
- | + | ||
This uses the program pkg-config, which can be obtained from [http://www.freedesktop.org/wiki/Home Freedesktop]. The program reads the .pc files which come with Hildon to determine what compiler switches are needed to compile programs that use it. 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 joined as a single instance because it is easier to understand. | This uses the program pkg-config, which can be obtained from [http://www.freedesktop.org/wiki/Home Freedesktop]. The program reads the .pc files which come with Hildon to determine what compiler switches are needed to compile programs that use it. 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 joined as a single instance because it is easier to understand. | ||
- | + | N.B. Note that the type of single quote (back-tick) used in the compile command above is significant. | |
- | N.B. Note that the type of single quote used in the compile command above is significant. | + | |
==Stepping through Hello World == | ==Stepping through Hello World == | ||
Line 151: | Line 151: | ||
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 handle events from them. | 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 handle events from them. | ||
- | + | <source lang="c"> | |
- | + | static void hello( GtkWidget *widget, | |
- | + | gpointer data ) | |
- | + | { | |
- | + | g_print ("Hello World!\n"); | |
- | + | } | |
+ | </source> | ||
The following is the definition of the main function, similar to the standard main function you would find in any C program. | The following is the definition of the main function, similar to the standard main function you would find in any C program. | ||
- | + | <source lang="c"> | |
- | + | int | |
- | + | main (int argc, char **argv){ | |
+ | </source> | ||
- | The next lines of code declare 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. | + | The next lines of code declare pointers to an object of type <code>HildonProgram</code> which represents an application running in the Hildon framework. Pointers to the widgets which show the application are also declared. |
- | + | <source lang="c"> | |
- | + | HildonProgram *program; | |
- | + | GtkWidget *window; | |
- | + | GtkWidget *button; | |
+ | </source> | ||
To use Hildon, initialize it first. Initialization connects to the window system display, and parses some standard command line arguments. | To use Hildon, initialize it first. Initialization connects to the window system display, and parses some standard command line arguments. | ||
- | + | <source lang="c"> | |
- | + | hildon_gtk_init (&argc, &argv); | |
+ | </source> | ||
- | Only one HildonProgram can be created per process. Use <code>hildon_program_get_instance()</code> to access it. | + | Only one <code>HildonProgram</code> can be created per process. Use <code>hildon_program_get_instance()</code> to access it. |
- | + | <source lang="c"> | |
- | + | program = hildon_program_get_instance (); | |
+ | </source> | ||
- | In this simple example, a new HildonWindow is created. In cases with nested views, use a HildonStackableWindow. | + | In this simple example, a new <code>HildonWindow</code> is created. In cases with nested views, use a <code>HildonStackableWindow</code>. |
- | + | <source lang="c"> | |
- | + | window = hildon_window_new (); | |
+ | </source> | ||
This call registers a window as belonging to the program. This allows applying program-wide settings to all the registered windows, such as assigning a common menu to all the registered windows by setting it to the program. | This call registers a window as belonging to the program. This allows applying program-wide settings to all the registered windows, such as assigning a common menu to all the registered windows by setting it to the program. | ||
- | + | <source lang="c"> | |
- | + | hildon_program_add_window (program, HILDON_WINDOW (window)); | |
+ | </source> | ||
- | The following code is an example of connecting a signal handler to an object, in this case, the window. The function <code>gtk_main_quit()</code> 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. | + | The following code is an example of connecting a signal handler to an object, in this case, the window. The function <code>gtk_main_quit()</code> is set as a handler to the "<code>delete_event</code>" signal. The function tells GTK+ that it must exit from <code>gtk_main</code> when control is returned to it, making the program terminate. |
- | + | <source lang="c"> | |
- | + | g_signal_connect (G_OBJECT (window), "delete_event", | |
- | + | G_CALLBACK (gtk_main_quit), NULL); | |
+ | </source> | ||
- | 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 do not need the additional features that Hildon provides. | + | This call creates a new <code>HildonButton</code>. 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 <code>GtkButton</code>'s in Hildon applications in case you do not need the additional features that Hildon provides. |
- | + | <source lang="c"> | |
- | + | button = hildon_button_new_with_text (HILDON_SIZE_AUTO, | |
- | + | HILDON_BUTTON_ARRANGEMENT_VERTICAL, | |
- | + | "Hello world!", | |
- | + | NULL); | |
+ | </source> | ||
- | Here, a signal handler is attached to the newly created button so that when it emits the "clicked" signal, our <code>hello()</code> function is called. The data is ignored, so we simply pass in NULL to the <code>hello()</code> callback function. Obviously, the "clicked" signal is emitted when the button is pressed. | + | Here, a signal handler is attached to the newly created button so that when it emits the "<code>clicked</code>" signal, our <code>hello()</code> function is called. The data is ignored, so we simply pass in <code>NULL</code> to the <code>hello()</code> callback function. Obviously, the "<code>clicked</code>" signal is emitted when the button is pressed. |
- | + | <source lang="c"> | |
- | + | g_signal_connect (G_OBJECT (button), "clicked", | |
- | + | G_CALLBACK (hello), NULL); | |
+ | </source> | ||
This packing call tells GTK+ to place the button in the window. For more information, see the Packing Widgets section of the [http://library.gnome.org/devel/gtk-tutorial/stable/ GTK+ 2.0] Tutorial. | This packing call tells GTK+ to place the button in the window. For more information, see the Packing Widgets section of the [http://library.gnome.org/devel/gtk-tutorial/stable/ GTK+ 2.0] Tutorial. | ||
- | + | <source lang="c"> | |
- | + | gtk_container_add (GTK_CONTAINER (window), | |
- | + | button); | |
+ | </source> | ||
When 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. | When 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. | ||
- | + | <source lang="c"> | |
- | + | gtk_widget_show_all (GTK_WIDGET (window)); | |
+ | </source> | ||
And of course, we call <code>gtk_main()</code> which waits for events to come from the X server and calls on the widgets to emit signals when these events come. | And of course, we call <code>gtk_main()</code> which waits for events to come from the X server and calls on the widgets to emit signals when these events come. | ||
- | + | <source lang="c"> | |
- | + | gtk_main (); | |
+ | </source> | ||
And the final return. Control returns here after <code>gtk_main_quit()</code> is called. | And the final return. Control returns here after <code>gtk_main_quit()</code> is called. | ||
- | + | <source lang="c"> | |
- | + | return 0; | |
+ | </source> | ||
+ | |||
+ | <hr/> | ||
+ | <table width=100%> | ||
+ | <tr> | ||
+ | <td width=30% align=left> | ||
+ | Prev: [[{{BASEPAGENAME}}/Introduction|Introduction]]</td> | ||
+ | <td align=center>Up: [[{{BASEPAGENAME}}|Table of Contents]]</td> | ||
+ | <td width=30% align=right> | ||
+ | Next: [[{{BASEPAGENAME}}/Windows_and_dialogs|Windows and Dialogs]]</td> | ||
+ | </tr></table> |
Latest revision as of 08:50, 15 October 2010
This article is legacy documentation, and is superseded by Forum Nokia documentation. The Forum Nokia documentation is available as the Hildon 2.2 UI style guide, Fremantle master layout guide and the Hildon 2.2 widget UI specification |
Before starting to develop your Hildon applications you need to get, install and properly configure the 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 <hildon/hildon.h> int main( int argc, char *argv[] ) { GtkWidget *window; hildon_gtk_init (&argc, &argv); g_set_application_name ("Simplest example"); window = hildon_window_new (); gtk_widget_show (window); gtk_main (); return 0; }
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, and so on, that are used in your Hildon application.
The next line is necessary in every program:
hildon_gtk_init (&argc, &argv);
This function replaces a call to gtk_init()
and also initializes the Hildon library. This function also performs a call to gtk_init()
. For more information on GTK+ initialization and arguments that can be passed to your application on the command line, see GTK+ Reference Manual. The next two lines of code create and display a window.
window = hildon_window_new (); gtk_widget_show (window);
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
can be enough. However, in most of the applications, use a HildonStackableWindow
.
The gtk_widget_show()
shows the widget (makes it visible) that would not be otherwise displayed.
The last line enters the GTK+ main processing loop.
gtk_main ();
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 I/O notifications to occur. In our simple example, however, events are ignored.
[edit] 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
#include <hildon/hildon.h> /* This is a callback function. The data arguments are ignored * in this example. More on callbacks . */ static void hello (GtkWidget *widget, gpointer data) { g_print ("Hello World!\n"); } int main (int argc, char **argv) { HildonProgram *program; GtkWidget *window; GtkWidget *button; /* This is called in all Hildon applications. Arguments are parsed * from the command line and are returned to the application. */ hildon_gtk_init (&argc, &argv); /* Get an instance of HildonProgram. It is an object used to represent * an application running in the Hildon framework. */ program = hildon_program_get_instance (); /* create a new hildon window */ window = hildon_window_new (); /* Registers a window as belonging to the program */ hildon_program_add_window (program, HILDON_WINDOW (window)); /* When the window is given the "delete_event" signal (this is given * by the window manager, usually by the "close" option, or on the * titlebar), we ask it to call the delete_event () function * as defined above. The data passed to the callback * function is NULL and is ignored in the callback function. */ g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL); /* Create a new hildon button with its title label set to "Hello world!", * also size and type of arrangement is set */ button = hildon_button_new_with_text (HILDON_SIZE_AUTO, HILDON_BUTTON_ARRANGEMENT_VERTICAL, "Hello world!", NULL); /* When the button is given the "clicked" signal, we ask it to call the * hello () function as defined above. The data passed to the callback * function is NULL and is ignored in the callback function. */ g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (hello), NULL); /* This packs the button into the window (a GTK+ container). */ gtk_container_add (GTK_CONTAINER (window), button); /* The final step is to display this newly created widget * and all widgets it contains. */ gtk_widget_show_all (GTK_WIDGET (window)); /* All GTK+ applications must have a gtk_main(). Control ends here * and waits for an event to occur (like a key press or * mouse event). */ gtk_main (); return 0; }
As you can see in this simple example, writing Hildon applications is slightly different from writing standard GTK+ applications. We are going to review these differences through the next chapters.
[edit] Compiling Hello World
To compile, use the following command:
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 files which come with Hildon to determine what compiler switches are needed to compile programs that use it. 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 joined as a single instance because it is easier to understand.
N.B. Note that the type of single quote (back-tick) used in the compile command above is significant.
[edit] 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 handle events from them.
static void hello( GtkWidget *widget, gpointer data ) { g_print ("Hello World!\n"); }
The following is the definition of the main function, similar to the standard main function you would find in any C program.
int main (int argc, char **argv){
The next lines of code declare 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.
HildonProgram *program; GtkWidget *window; GtkWidget *button;
To use Hildon, initialize it first. Initialization connects to the window system display, and parses some standard command line arguments.
hildon_gtk_init (&argc, &argv);
Only one HildonProgram
can be created per process. Use hildon_program_get_instance()
to access it.
program = hildon_program_get_instance ();
In this simple example, a new HildonWindow
is created. In cases with nested views, use a HildonStackableWindow
.
window = hildon_window_new ();
This call registers a window as belonging to the program. This allows applying program-wide settings to all the registered windows, such as assigning a common menu to all the registered windows by setting it to the program.
hildon_program_add_window (program, HILDON_WINDOW (window));
The following code is an example of connecting a signal handler to an object, in this case, the window. The function gtk_main_quit()
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.
g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
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 do not need the additional features that Hildon provides.
button = hildon_button_new_with_text (HILDON_SIZE_AUTO, HILDON_BUTTON_ARRANGEMENT_VERTICAL, "Hello world!", NULL);
Here, a signal handler is attached to the newly created button so that when it emits the "clicked
" signal, our hello()
function is called. The data is ignored, so we simply pass in NULL
to the hello()
callback function. Obviously, the "clicked
" signal is emitted when the button is pressed.
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (hello), NULL);
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.
gtk_container_add (GTK_CONTAINER (window), button);
When 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.
gtk_widget_show_all (GTK_WIDGET (window));
And of course, we call gtk_main()
which waits for events to come from the X server and calls on the widgets to emit signals when these events come.
gtk_main ();
And the final return. Control returns here after gtk_main_quit()
is called.
return 0;
Prev: Introduction | Up: Table of Contents | Next: Windows and Dialogs |
- This page was last modified on 15 October 2010, at 08:50.
- This page has been accessed 30,631 times.