Using GTK, Glade and Codeblocks together in Ubuntu - Level Beginner
Looks like you need to do a lot of things to properly set an environment where you can peacefully create UI files using Glade which uses GTK-3.0 version in the backend while using CodeBlocks!
The major steps for setting up environment are:
- Install latest stable version of CodeBlocks (for me, 16.01)
- Install GTK-3.0 related libraries
- Modify the Template Script in CodeBlocks
- Install Glade
The major steps for creating an UI are:
- Create design using Glade
- Connect it to a C code
Latest CodeBlocks!
Installing CodeBlocks from PPA
GTK-3.0 Libraries!
Installing GTK-3.0 libraries:
Modify Template Script in CodeBlocks
This is the tricky part. You see, the CodeBlocks still uses GTK+-2.0 libraries by default for the GTK+ projects in its template. You need to edit the template file and modify the parts where it will reference the GTK-3.0 libraries instead of the libraries from version 2.
start with this command:
See the output? It is showing the necessary files you need to successfully build a gtk based app. For me, it is:
Now, Open CodeBlocks. Right click on New > Project > GTK+ Project
, edit this script
. It may throw an error saying it can not find the file, making it in read-only mode. Hence, go to ~/.local/share/codeblocks/templates/wizard/gtk
and open the wizard.script
.
Replace the version values with the values you found from the pkg-config
part. For example,
will become
In case you are clueless, you can take a look at my wizard.script.
Installing Glade
Create Design Using Glade
Open glade, drag and drop a Window
top-level container. Add some more things if you want inside it. From the properties pane at the right side, go to general
tab, and write mywindow
in the ID.
The key things to remember here are:
- You got to have a Window ID, and it must be used in the C code
- You have to save it in GtkBuilder format (
file > save as
)
C code example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# C Code that includes a testGlade.glade file
# for designing UI
#include<gtk/gtk.h>
int main(int argc, char *argv[]){
GtkBuilder *gtkBuilder;
GtkWidget *window;
gtk_init(&argc, &argv);
gtkBuilder = gtk_builder_new();
gtk_builder_add_from_file(gtkBuilder, "testGlade.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "mywindow"));
g_object_unref(G_OBJECT(gtkBuilder));
gtk_widget_show(window);
gtk_main();
return 0;
}
Compile and run. Comment below if this works for you or if you got a feedback! share your screen shot of what you built :)