GNOME Developer's Tutorial.

Gnome Developers' Information
Prev |  | Next

# Chapter 3. GNOME Developer's Tutorial.

From the gnome-hello manual:

gnome-hello is a GNOME application which contains all the essential and common features of GNOME programs, such as initialization, event loops, configuration file parsing, internationalization and so forth.

gnome-hello is intended to be an example of the GNOME coding standards, as well as a fun and useless little program.

In this chapter we're going to use it to explain the basics features that an application must have to be considered gnome-compliant.

# Starting with GNOME

We'll start with a very basic application. It's only a window with a button which when clicked, close the window and prints "Hello GNOME". The code source for it is in `programs/gnome-hello/gnome-hello-0-basic.c`. [FIXME: Should i include the full code here?]

    	  #include <config.h>
    	  #include <gnome.h>

All the programs have to include `gnome.h` which gives all you need to use the Gtk+ and GNOME libraries.

    	int
    	main(int argc, char *argv[])
    	{

`gnome_init` is always called at the beginning of a program. It takes care of initializing both Gtk and GNOME.

    	  gnome_init (&argc, &argv);

Then we call `prepare_app` that do the real work and `gtk_main` to enter into the main processing loop.

    	  prepare_app ();

     	  gtk_main ();

    	  return 0;
    	}

Let's now go to the `prepare_app` code...

    	void
    	prepare_app()
    	{
    	  GtkWidget *button;

We first make the main window calling `gnome_app_new` and connect the signal `delete_event` to the callback `quit_cb`, so the user can close the app via the window manager:

    	  app = gnome_app_new ("hello", "Hello World Gnomified");
    	  gtk_widget_realize (app);
    	  gtk_signal_connect (GTK_OBJECT (app), "delete_event",
    	                      GTK_SIGNAL_FUNC (quit_cb), NULL);

Then, we create a button and set it to be the content of the main window.

    	  button = gtk_button_new_with_label ("Hello GNOME");
    	  gtk_signal_connect (GTK_OBJECT (button), "clicked",
    	                     GTK_SIGNAL_FUNC (hello_cb), NULL);
    	  gtk_container_border_width (GTK_CONTAINER (button), 60);
    	  gnome_app_set_contents ( GNOME_APP (app), button);

Finally, we show the widgets. It could be done in any order, but we display at last the main window so the whole window will pop up at once rather than seeing the window pop up, and then the button form inside of it.

    	  gtk_widget_show (button);
     	  gtk_widget_show (app);
    	}

The callbacks (`hello_cb` for the button and `quit_cb` for `delete_event`) are very simple. They just call `gtk_main_quit` to exit.

    	void
    	hello_cb (GtkWidget *widget, void *data)
    	{
      	  g_print ("Hello GNOME\n");
    	  gtk_main_quit ();
    	  return;
    	}

    	void
    	quit_cb (GtkWidget *widget, void *data)
    	{
    	  gtk_main_quit ();
    	  return;
    	}

Prev | Home | Next
Coding Standards. |  | Adding menus