Saturday, August 17, 2013

How to integrate Glade 3 UI into a C program

Here's a simple example on how to make a UI in Glade 3 and then use the output XML file in a C program.
The code in C should look like this:

1:  #include <gtk/gtk.h>  
2:    
3:  int main(int argc, char **argv) {  
4:    GtkBuilder *builder;  
5:    GtkWidget *window;  
6:    GError   *error = NULL;  
7:    
8:    /* Init GTK+ */  
9:    gtk_init( &argc, &argv );  
10:    
11:    /* Create new GtkBuilder object */  
12:    builder = gtk_builder_new();  
13:    /* Load UI from file. If error occurs, report it and quit application.  
14:     * Replace "tut.glade" with your saved project. */  
15:    if(!gtk_builder_add_from_file(builder, "tut.glade", &error)) {  
16:      g_warning( "%s", error->message );  
17:      g_free( error );  
18:      return( 1 );  
19:    }  
20:    
21:    /* Get main window pointer from UI */  
22:    window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));  
23:    
24:    /* Connect signals */  
25:    gtk_builder_connect_signals( builder, NULL );  
26:    
27:    /* Destroy builder, since we don't need it anymore */  
28:    g_object_unref( G_OBJECT( builder ) );  
29:    
30:    /* Show window. All other widgets are automatically shown by GtkBuilder */  
31:    gtk_widget_show( window );  
32:    
33:    /* Start main loop */  
34:    gtk_main();  
35:    
36:    return( 0 );  
37:  }  

For compiling the code you need to link gtk 3 libs.

gcc source.c `pkg-config --libs --cflags gtk+-3.0`

The code isn't mine, just sharing something that worked for me ;)

No comments:

Post a Comment