Gnome Programming: Getting Started with Anjuta/Glade
April 20th, 2005Note: This tutorial is no longer current. A new, more up-to-date and in-depth tutorial has been written: Tutorial: Simple Gnome Application Using libglade and C/GTK+
Introduction
Hello, and welcome to my first Gnome application. That's right, this was my first Gnome application. Keep that in mind. I'm not an expert on GTK+ or Gnome development whatsoever. I will do my best to keep this updated with corrections and provide the best information I can. You can learn to program for Gnome right along with me as I learn.
It's worth pointing out that although I don't have much experience with GTK and Linux programming, I am an experienced programmer and understand basic C, object oriented programming, data structures, and other concepts that are (I'm assuming) going to be critical. Also, you may want to become more familiar with straight GTK+ programming before moving into Gnome specific programming. There is a tutorial on the GTK website. Personally, I worked through the first 'Hello World' GTK program and am now moving on to Gnome specific GTK programming.
As I figure things out I'll pass the information on to you. Part of my reasons for writing this tutorial are due to the lack of up-to-date information on getting over that initial hump in learning to use Anjuta and Glade to develop Gnome applications. I'm not teaching you GTK, nor am I teaching you the C programming language. I'm showing you how I got started using Anjuta and Glad. I will be using the C language (although not much just yet!).
This tutorial will go over the creation of a Gnome application called "Hello World" which looks something like:

You'll notice that it's a real, Gnome looking application. It doesn't do much, but it look cool! When we click the 'File' -> 'New' menu item, another dialog box will be show that looks like:

Sources of Information
I did a bit of reading and browsing the internet before embarking on this new task of learning to develop gnome applications. Here are some of the resources that influenced this tutorial:
- Anjuta Tutorial - This tutorial is from Anjuta, however, it seems a little bit out of date. However, this was the basic guidline I followed.
- GTK+ Mailing List - This is the GTK+ mailing list in which I ask questions. We're going to be good friends.
- Tutorials by Ishan Chattopadhyaya - He has written several tutorials on GTK+/Glade programming.
- Below are 2 books I have and have been using as a reference. Both are a bit out-dated, so if you have some book reccomendations, please, by all means, post them in comments at the bottom of this page. I just got these 2 books mainly due to how cheap they are and knowing that they would still teach me the fundamentals of GTK and Gnome programming.
| This is a book by Havoc Pennington who plays a significant role in the development of gnome. I've been sort of casually reading through it to get an idea of what I'm up against. This book is from 1999 though, so you could imagine how many changes have occured since then. But, it gives me a good background on Gnome applications programming if nothing else. I find it quite valuable. | |
| Yet another book from 1999 and therefore highly likely to be a bit outdated, however, I've been using this as a reference for basic GTK+ programming concepts. |
The Tools - Anjuta and Glade
In order to go through this tutorial and begin developing Gnome applications, you should make sure you have the following installed for your distribution:
- Glade - Glade Interface Designer is an application that allows you to easily create the GUI for your application. It then generates the GTK+ code for that interface. It also interfaces quite nicely with Anjuta. I'm using Glade 2.6.0 installed from RPM for my Fedora Core 3 installation. If you don't already have it installed and use RPMs (it comes with many distros), you can probably obtain the RPM using YUM or apt-get.
- Anjuta - Integrated Developement Environment (IDE). I'm using version 1.2.2 which I believe came from the Fedora Core 1 RPM downloaded from teh Anjuta website.
That's it for now. Obviously you need GTK+ and gnome libraries etc. That should be covered in your endevours of installing the above said applications.
One other thing that stumpted me for a short period of time. The RPM version of Anjuta doesn't ensure gettext is installed. When I first tried to create a new project in Anjuta, I recieved a message like the one below:

For me, this was resolved by installing the gettext-devel RPM by issuing the command:
yum install gettext-devel
Creating a New Project in Anjuta
According to the Anjuta tutorial, the sequences that the new project wizard takes you through are very important. However, we're just sort of playing around here so we can afford to make mistakes. That's how we learn. So open up Anjuta (it should be under 'Programming' in your Gnome applications menu). Select 'New Project' from the 'File' menu to begin the New Project wizard. Fill in each screen similar to these screenshots:
When you click 'Apply', the autogen will start working. This takes a minute to run. When it's complete, there'll be a message that says "Auto Generation completed..............successful". You now have a project. By default, Anjuta's projects are created in the 'Projects' folder in your home folder. For this project , the code will reside in your_home_dir/Projects/hello. If for some reason you need to start over creating the project, first delete this folder (you can't create a project if one already exists with that name).
Building the Interface with Glade
Now we should be in Anjuta. We will now create our basic GUI. You can either select 'Edit Application GUI' from the 'Project' file menu in Anjuta or press ALT+G to launch Glade. The main window of Glade looks like the one below. Take note of the 'window1' that's currently listed. That's going to become significant later in this tutorial. But for now, click to highlight 'window1' and press the delete key to delete window1.

In the 'Pallette' window, click the 'Gnome' button and then the Gnome Application Window button which is the upper left icon. With the newly created 'app1' window open, you can modify it's properties in the 'Properties' window. Change the 'Title:' attribute to "Hello World!" as shown below.
![]() |
![]() |
|
What we just did, is we removed the window1 widget, which was a GtkWindow widget and replaced it with a GnomeApp widget, which is a subclass of the GtkWindow widget. It is simply a GtkWindow with the extras that are consistant in most Gnome applications so that we can have that good ol' Gnome look and feel. Specifically, it has the menu, toolbar, and statusbar built in.
Now, choose 'Save' from the 'File' menu and then 'Build' from the 'File' menu. This will generate the GTK code required to build your interface! Exit Glade and return to Anjuta.
Note: There are 2 methods when using the Glade interface designer. What we just did actually builds the C/GTK+ code. However, another method is to use the libglade library which takes Glades XML files to create the interface at runtime. From my readings, I've learned that this seems to be the advised method of experienced GTK programmers, especially with larger scale projects. However, since we're just learning here, and this is a tiny little application, using the Build method will do just fine.
Editing the Code in Anjuta
Back in anjuta, click the callbacks.c file in the project explorer of Anjuta (left pane). We're going to add a call to gtk_main_quit() in the function named on_quit1_activate to terminate the program when a user selects 'Quit' from our program's 'File' menu.:
<span style="color: #000099">void</span>
on_quit1_activate (<span style="color: #990000">GtkMenuItem</span> *menuitem,
<span style="color: #990000">gpointer</span> user_data)
{
gtk_main_quit();
}
And then one more, just for fun, still in the callbacks.c file, edit the function on_new1_activate as follows:
<span style="color: #000099">void</span>
on_new1_activate (<span style="color: #990000">GtkMenuItem</span> *menuitem,
<span style="color: #990000">gpointer</span> user_data)
{
GtkWidget *msgbox = gnome_app_new(<span style="color: #666666">"Hello World"</span>, <span style="color: #666666">"Hello World"</span>);
gnome_app_message (GNOME_APP(msgbox), <span style="color: #666666">"Hello World. You chose New!"</span>);
}
Those 2 lines will generate a little message box when the user chooses 'New' from our program's 'File' menu. Now, you'd think we're ready to build and test our project-- but there's one little problem. I left this problem in on purpose as It's a good learning experience. Remember when we opened up Glade and deleted 'window1' and replaced it with our Gnome Application Window called 'app2'? Well, take a look at the file main.c and look around line 36 for:
window1 = create_window1 (); gtk_widget_show (window1);
You see the problem? We no longer have a create_window1() function. It became create_app1() function (I found it in interface.c). However, it seems that once main.c is created, Glade will not modify it. Therefore, it didn't change this call. So we could either go back into Glade and rename our Gnome Application Window to 'window1', or, what I did was change that line as follows:
window1 = create_app1 (); gtk_widget_show (window1);
Now we are ready to build our project. Select 'Build' from the 'Build' menu. Hopefull you don't get any errors. And now, press F3 or choose 'Execute' from the 'Build' Menu to view our little application in action. Cool eh?
From Here...
From here we move on to Gnome Programming Tutorial 2 - Using LibGlade with Anjuta/Glade in which we'll create another new Gnome application, only this time we'll be using Glade with the libglade library to build the interface at runtime.
Categories
Popular Posts
RSS Feeds
Archives








May 8th, 2008 at 7:25 am
Thank you very much ,your message is quite useful to , are you also learning GNOME programming ? If you did ,I hope I can be your friend .
May 8th, 2008 at 7:26 am
I am sorry ,forget to tell you that, my E-mail is qeeken@gmail.com
May 8th, 2008 at 9:31 am
excuse me , I think what you written is quite useful to the new hand like us ,and I hope I can get your permission to translate this tutorial to my mother language share is with other people need it. Thank you and waiting for your E-mail , here is mine: qeeken@gmail.com