GTK+ GUI Programming: Ori Idan Helicon Technologies
GTK+ GUI Programming: Ori Idan Helicon Technologies
Ori Idan
Helicon technologies
What is it?
● GUI library written in C with bindings for many
other languages
● LGPL License
● Part of the GNU project
● Initially developed for and used by the GIMP
● Used today as the basis of GNOME
● Portings to other operating systems including MS
Windows
Language bindings
● Language bindings enable you to write GTK+
programs using languages other then C.
● GTK+ was written with language bindings in
mind from the very beginning
● Officialy supported languages: C++, Java, Perl
● Other languages supported: PHP, Python, Ruby,
TCL, Eifel, C#, ADA, Lisp and many others.
Architecture and components
● Fully object oriented although written in C.
● Uses classes and callback functions implemented
as structures and pointers to functions
● Contains the following libraries:
– Glib – low level core library functions providing event
loop, threads, dynamic loading, object system, string and
list manipulation etc.
– Pango – Layout and rendering of text with emphasis on
I18N
– ATK – Accessibility toolkit supporting screen readers and
alternative input devices.
Show me the code...
int main(int argc, char *argv[]) {
GtkWidget *window, *button;
/* initialize library and parse command line arguments */
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window),"delete_event",
G_CALLBACK(delete_event), NULL);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
button = gtk_button_new_with_label("Hello world");
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main();
return 0;
}
Show me the code...
gboolean delete_event(GtkWidget *widget, GdkEvent *event,
gpointer data) {
printf("delete event occured\n");
return FALSE;
}
void destroy(GtkWidget *widget, GdkEvent *event,
gpointer data) {
gtk_main_quit();
}
How to compile
Use the pkgconfig utility
gcc o hello hello.c `pkgconfig cflags libs gtk+2.0`
pkgconfig will give all the flags needed to compile
using this library.
Signals
● Signals are used to relate actions to mouse clicks,
mouse moves, keyboard, timer etc.
● Each widget can have several signal handlers for
different signals or for same signal
● The g_signal_connect() function connects a signal
to a callback function to handle the signal.
● Type of callback function depends on the signal.
More code...
First we create a callback function to be called when
clicking the button:
void ButtonCallback(GtkWidget *button, gpointer
*data) {
printf("Button pressed\n");
gtk_main_quit();
}
Then we connect the function to the signal:
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(ButtonCallback), NULL);
Now when we run the program, pressing the button will
print a message and quit.
Packing widgets
● Widgets are packed in boxes and tables.
● There are two types of boxes VBOX and HBOX
● Table can be regarded as combination of HBOX and
VBOX
● Each cell may contain either a box or one widget
● Create a box using gtk_vbox_new or gtk_hbox_new
● Add the box using gtk_container_add
● Create a table using gtk_table_new
Packing widgets example
Add two buttons in a window:
●
After all this is what open source is about...
Thank you...
Questions ???
Ori Idan: [email protected]
http://www.helicontech.co.il