Archive

Posts Tagged ‘Linux Programming’

Glade 3 + GtkBuilder + Anjuta Example.

February 23rd, 2010 Jimmy No comments

Today I whipped up a simple GTK application using the Anjuta IDE, Glade 3, and the new GtkBuilder system. As some of you know, Glade 3 and GTK changed things up. First Glade stopped using generated code which required you to use libglade. Now the GTK developers created their own interface interpretor called GtkBuilder. Your whole entire Gtk+ application is now done using mainly signals, making it fairly easy to develop Gnome type applications.

Note: This is not an in-debth tutorial on using Anjuta however I did provide a few screenshots. This is just to give you a quick idea of how to grab widgets with GtkBuilder and how to process signals from a Glade 3 interface. I just happen to be using Anjuta as my IDE.

Here is my development setup:
Anjuta: 2.82.2
Glade: 3.6.7-1
GTK: 2.18.7-1

If you are running Arch Linux these are available through pacman.

Once you have the above installed you’ll want to create a GTK+ project. Select File -> New Project, Select C (not C++), then GTK+. I unchecked the option to support other languages, but you don’t have to if you want to make your app multi-lingual.

Once you have a project setup you should see a few source files and a UI file. The UI file contains a user interface file which you can edit in glade. The screenshot below shows a basic setup after a project is created. I use horizontal and vertical splitters to align all my GtkWidgets. I created two buttons and one entry box.

Now to put text in the textbox (entry1) when a user clicks on the “button” you’ll need to add a GtkEntry pointer to your callback.c file.

callback.c

#include <gtk/gtk.h>
GtkEntry *myEntry;
void destroy (GtkWidget *widget, gpointer data);

main.c

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <config.h>

#include <gtk/gtk.h>

#include "callbacks.h"

/* For testing propose use the local (not installed) ui file */
/* #define UI_FILE PACKAGE_DATA_DIR"/gtk_foobar/ui/gtk_foobar.ui" */
#define UI_FILE "src/gtk_foobar.ui"

GtkWidget* create_window (void)
{
	GtkWidget *window;
	GtkBuilder *builder;
	GError* error = NULL;

	builder = gtk_builder_new ();
	if (!gtk_builder_add_from_file (builder, UI_FILE, &error))
	{
		g_warning ("Couldn't load builder file: %s", error->message);
		g_error_free (error);
	}

	/* This is important */
	gtk_builder_connect_signals (builder, NULL);
	window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));

Now we need to populate the myEntry GtkEntry pointer in callback.c to our object that we made in the Glade interface builder. I named my entrybox “entry”. In the glade interface you can name your Widgets in the properties section. Remember, we defined GtkEntry *myEntry in callback.c.

    myEntry = GTK_ENTRY (gtk_builder_get_object (builder, "entry1"));

    gtk_entry_set_text (myEntry, "Hello World!");

	g_object_unref (builder);

	return window;
}

int main (int argc, char *argv[])
{
 	GtkWidget *window;

	gtk_set_locale ();
	gtk_init (&argc, &argv);

	window = create_window ();

	gtk_widget_show (window);

	gtk_main ();
	return 0;
}

void
button1_clicked_cb (GtkButton *self, gpointer user_data)
{
gtk_main_quit();
}

Here is the signal I created when creating my interface in Glade 3 from within Anjuta. Click on your Widget, then properties (lower left), then Signals (upper right, left side bar). If you don’t see a signals tab you might have to scroll over to see it using the scroll (left/right arrow looking things) buttons they built in.

void
btn1_Click (GtkButton *self, gpointer user_data)
{
    gtk_entry_set_text (myEntry, "Hello From Button Click!");
}

Now you can click build and execute your program! The program will populated your entry box when the application starts and then change the text when you click the “button” button.

Categories: Programming Tags: ,