Gtk+ programing tutorial for beginner

First program for GTK+ beginner is just make a window widget and show on screen. GTK+ is graphical user interface library toolkit. It is a cross platform library which offers complete set of widgets and controls for creating a native user interface. It is also called GIMP toolkit and backbone of gnome desktop. gnome desktop is a package which provides desktop environment for linux based operating system like ubuntu, fedora, centos and many more. Now a days the light version of gnome desktop is used in smartphone os, the light version of gnome desktop is know as mobile gnome desktop.

For a gtk+ beginner programer this article will boost the starting interest. now let us create our first program and then we will see how to compile, run the program. And after running our first gui we will see “How it works”.

(1) Save the below code as mywindow.c

#include<gtk/gtk.h>

GtkWidget* window;

int main(int argc, char** argv)
{
     gtk_init(&argc, &argv);

     /* create window */
     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     /* set size of window */
     gtk_window_set_default_size(GTK_WINDOW(window),600,400);

    /* show window */
    gtk_widget_show_all(window);

    gtk_main();
}

(2) To compile mywindow.c , run below command on terminal.
$ gcc mywindow.c `pkg-config –cflags –libs gtk+-3.0` -o mywindow

Note: if you have not installed gtk library . Then from your terminal you can type below command to install gtk+ library on debian, ubuntu.

$ sudo apt-get install libgtk-3-dev

For other linux based os you can install gtk+  library from their respective package manager like for redHat or centos you can install by rmp package manager.

If your os is somewhat old then you can install gtk2 library:
$ sudo apt-get install libgtk2.0-dev

In this case your compilation command will be as below

$ gcc mywindow.c `pkg-config –cflags –libs gtk+-3.0` -o mywindow

(3) Now just run the output mywindow.

$./mywindow

First program for GTK+ beginnerFirst program for GTK+ beginner

This above window will popup on screen. But this is very basic and when you try to close this window ,the window will disappear from the desktop but you will not end up with your terminal.  This means that the program mywindow is still running on your system. Then “How will we quit our program”?

Let us first learn a little about Events, Signals and CallBacks in GTK

GTK+ has its own system of events and event listener. The Event ( like click,destroy ,resize etc ) is know as signals and the function which gets called after emitting these signals is called callbacks. GTk+ signals are emitted by gtk+ widget when user interacts with these widgets. we can connect these signals with a user defined function ( callbaks ).

The connection between a signal and a function ( callback ) is achieved by a gtk+ API.

g_signal_connect (GtkWidget* , signalname, G_CALLBACK (foo), gpointer data);

Let implement this in our program mywindow.c
(1) When a user will click on close icom in our window example gtk+ emits "destroy"
    signal,so the signal name will be "destro".
(2) To quit the program wee need to exit from the program. So we can make a function
    and call exit() from inside that function.
(3) The third argument in is gpointer which is equivalent to NULL in c. This is used
    to pass a argument to the callback function.
g_signal_connect (GtkWidget* , signalname, G_CALLBACK (foo), gpointer data);
 Let us see the implementation in below code:

#include<gtk/gtk.h>

GtkWidget* window;

void quit(GtkWidget* wid, gpointer* data)
{
  exit(0);
}    

int main(int argc, char** argv)
{
 gtk_init(&argc, &argv);

  /* create window */  
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   /* set size of window */
   gtk_window_set_default_size(GTK_WINDOW(window),600,400);

/* connecting destroy signal to function quit*/
g_signal_connect(G_OBJECT(window), "destroy",
    G_CALLBACK(quit), NULL); 

 /* show window */
 gtk_widget_show_all(window);
 gtk_main();
}


Note: In place of exit() call in quit function you can gtk_main_quit(), function 
call. it is gtk+ built function used to quit the gtk+ main program.like below
 quit(GtkWidget* wid, gpointer* data)
 {
   gtk_main_quit();
 }


Related Contents to follow