Starting off in Glade/GTK+
Tutorial by Ishan Chattopadhyaya
Introduction
How often do we admire the work which the GNOME project has brought out and wish that we too could have created something which could be called "my software" on Linux? Well, if you are interested in developing Linux programs, the best tool that can come to your rescue is Glade.
Now, the question arises, "how do we learn Glade?". Glade is actually a tool that creates the interface, i.e. the windows, the dialogs, the widgets, and creates a framework for your code. It creates the necessary files in the source code where you have to do all the coding manually. Therefore it is quite easy building an interface with this tool. This means, you:
In this tutorial, you will see how to create an interface and how to link certain widgets with one another.
Creating the interface
The Glade Environment (Click for a larger view) |
Open Glade. You will see a number of component windows of Glade. They are Glade (Main), Palette, Properties, etc. (See figure above for an example environment) Proceed as follows:
[Note for other programmers: Using a fixed position container is easier for beginners rather than using vertical and horizontal boxes etc. Besides, all who have previous experience with Microsoft Visual Basic/C++ or Power Builder will find it more comfortable to adjust. Using vertical and horizontal box containers can be left for a later stage in the course for a beginner learning Glade.]
[Tip: You can move your mouse over the icons in the palette and wait for a second for the ‘tool tips’ to popup. Take a while exploring these tool tips so you are comfortable with the development environment.]
Adding the Signals (After Clicking "..." button) |
Adding the Signals (After Clicking Add button) |
Editing the code
Now that we have successfully created the interface and built the source for it, the time has come to edit the code. Since Glade is not an Integrated Development Environment (IDE) like Visual Basic, Visual C++ etc., you will need to edit the code using an external text editor. Most programmers use emacs as their favorite text editor for coding, but I prefer using gedit, since it has a multiple document interface (MDI). The choice of editor is up to you!
Whichever the text editor it might be, fire it up. Assuming
that during building your source you gave /root/Projects/Project1 as the directory
of your project, open the file /home/
Scroll down the file until you come across the function on_BT_EXIT_clicked. In the space that is provided within the function, add a function: gtk_main_quit(); Your function should now look like this:
{ gtk_main_quit(); } |
[Note: The gtk_main_quit() function causes the program to end. Another alternate function exit(0); can also be used for the same purpose.]
Do the same for the on_window1_destroy function. That is add gtk_main_quit() function there also. This ensures that when the window is closed using the 'X' button on the upper right, the program ends.
Similarly, add the following code to the on_BT_OK_clicked function:
{ /* INITIALIZATIONS START HERE */
/* COPYING THE TEXT ENTERED IN ENTRY1 TO THE LIST WIDGET */
/* COPYING THE TEXT ENTERED IN entry1 TO entry2 */
/* SHOWING A DIALOG BOX WITH THE TEXT OF entry1 IN IT */
} |
Now add the following code to the two radiobutton functions, like this:
{
}
{
} |
This piece of code is quite simple to understand. What the on_BT_OK_clicked function does is as follows:
Now that the coding part is done, now you have to proceed compiling your project.
Compiling the Project
Open a terminal window. Go to the project directory, i.e.
/home/
Overview of this tutorial
|
This small tutorial will let you understand simple interactions between widgets. This will also explain how to compile and run your projects. I hope you benefit from this tutorial. Please mail in your suggestions to me at ichattopadhyaya@yahoo.com. Please SIGN MY GUEST BOOK for your suggestions or visit my website: http://www.geocities.com/ichattopadhyaya. I am currently a 15 year old high school student and cannot devote all my time to Glade and GTK+ programming. But I hope to come up with a few more tutorials like this. Enjoy Glade! |
Download this tutorial
as PDF (116 KB)
|
|
Download
Tarball of the example (151 KB)
|
|
Please copy and use what you want from this site.
ichattopadhyaya@yahoo.com |