Using CodeBlocks with Glade and GTK toolkit

With CodeBlocks, it’s possible to make a new project based on the GTK toolkit with the wizard, but the default main.c builds the UI from scratch. However, it can be easily converted to use a file (in GtkBuilder format) to generate the GUI.
This file can then be modified with Glade without modifying the source code. Still, callback must be added manually.

In CodeBlocks, modify the compiler and linker options:
Project -> Build Options -> Click on the project name in the left panel (not Debug or Release; you want to modify flags for all versions)
On the right panel, select compiler settings, other options;
add this at the end: -Wl,–export-dynamic
It should now look as: `pkg-config gtk+-2.0 –cflags` -Wl,–export-dynamic

Still on the right panel, select linker settings, other options;
add this at the end: -export-dynamic
It should now look as: `pkg-config gtk+-2.0 –libs` -export-dynamic

After that, replace the code in main.c with:
#include <stdlib.h>
#include <gtk/gtk.h>

GtkBuilder *builder;
GtkWidget *app;

G_MODULE_EXPORT
void on_app_destroy (void)
{
gtk_main_quit ();
}

G_MODULE_EXPORT
void on_menu_quit_activate (void)
{
gtk_main_quit ();
exit(EXIT_SUCCESS);
}

int main (int argc, char *argv[])
{
/* Initialize GTK+ */
g_log_set_handler (“Gtk”, G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false,        NULL);
gtk_init (&argc, &argv);
g_log_set_handler (“Gtk”, G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, “GTK_Glade.xml”, NULL);
app = GTK_WIDGET (gtk_builder_get_object (builder, “app”));
gtk_builder_connect_signals (builder, NULL);

g_object_unref (G_OBJECT (builder));

/* Enter the main loop */
gtk_widget_show (app);
gtk_main ();
return 0;
}

Advertisements
This entry was posted in Electronics and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s