Sunday, November 18, 2007

GTK+ programming starters

Hey friends,

Bored of programming in CUI (character user interface), wanna do some GUI programming, want it to run in Linux as well as in the Windows environment. then GTK+ is for you.


GTK stands for GIMP Tool Kit.

GTK is a a multi-platform library for creating graphical user interfaces. It is designed to be small and efficient, but still flexible enough to allow the programmer freedom in the interfaces created. GTK allows the programmer to use a variety of standard user interface widgets such as push, radio and check buttons, menus, lists and frames. It also provides several "container" widgets which can be used to control the layout of the user interface elements.


To make life easier for you, GTK presents this flexibility in a uniform framework. Specifically, it implements its own support for object oriented programming that is well adapted to the purposes of a user interface toolkit and it aims at providing a reasonable sane and disciplined programming interface. This uniformity and discipline is intended to make it easy and reliable to access GTK from languages other than C


Ok , enough of the talk... lets start on some hands on coding for a better understanding of the library.



I would suggest you to download and install the Glade before starting.

Glade is a RAD tool to enable quick & easy development of user interfaces for the GTK+ toolkit and the GNOME desktop environment.

It is very similar to the Visual Basic development environment. With all drap and drop features you can come up with a proffessional looking software within minutes.

I would tell you how to make an application that would have 2 buttons - one to eject the CD tray, and the other to Load the CD tray.

I will use the Linux environment ( but it is entirely my choice, you may use Windows in case you want to)


OK. Here it comes ...





Step 1. Open Glade and Start a new project.





Choose Project -> New.





Step 2. Choose 'Window' from the palette. This is the main window, and we would build everything on it.





Add 'Vertical Boxes' and make 2 rows.





Add 2 buttons by choosing 'Buttons' from the Palette toolbar and add one to each row.


Step 3. Now Click on the Button1 and choose View -> Show Property Editor.





Put a proper name for the button1 as "Eject" and choose "Load" for button2.





Choosing button1 only, go to the Signals tab and choose 'clicked' in the signal. Click on Add button. This is the callback function for the signal. That is whenever there is an event of mouse click on the button1, the on_button1_clicked() function will be invoked.





Do the same with button2.


Step 4. Save the project and note the path it is saved.






The project should look a bit like this now !




Step 5. 'Build' the project. This creates all the .c files required for the project and automatically puts in barebone code in it too!!





Step 6. Here comes the hard core coding part, but do not fear !


In the Projects directory, go to the src folder. This is where the actually codes are saved. There should be a number of files in here, but the one of our concern is the callbacks.c file.


open it. you should see two empty functions -- void on_button1_clicked and void on_button2_clicked.





Now, add this code to the on_button1_clicked: system("eject");

and in the on_button2_clicked add: system("eject -t");





eject and eject -t are the shell commands to eject and load a CD tray respectively. We are passing this to system() function. The system function takes a string arguments and runs it in a shell. So what we effectively get is a CD tray opener !





Now save and close the file.





Step 7. In the parent directory, run the autogen.sh script from the terminal, by typing ./autogen after going to the parent directory. This should compile and build the project and create an executable file. Run the executable file and enjoy !!!








You have built a professional level GUI program. If you compile/ build it in windos environment, you would get an windows executable.



Simple, isn't it?




In case you have doubts or you want to know more, write comments here. I am also available at my email address given.

2 comments:

... said...

That is so cool. I still haven't gone through the whole. Whatever I read, I can say, it's really helpful for those who wants to learn GTK. Keep it up. And sharing your knowledge with the whole world is a noble work. I appreciate that.

Anonymous said...

yes... thank you for this thoughts.