As Big As This

Alalalalala

Saying Hello World With GTK

So now we’ re going to start nice and easy by making a common, household Hello World application. Here I’ll lay out a bare-essentials GTK application which I will try to explain as breifly as I can so fire up your text editor and create a base.c file:

This tells your compiler to include the gtk header files which includes the variables, structures, and other neat stuff in the GTK toolkit.

#include <gtk/gtk.h>

Here we have your main application function declaration. It has two parameters, which are pretty much standard:

int main(int argc, char **argv)
{

Here we declare an instance of GtkWidget which is a storage type for widgets and we’ll name it window:

GtkWidget *window;

Next, we call gtk_init, which as the name implies initializes GTK, initializing such stuff like color space, libararies, default signal handlers, etc. Here we see argc and argv again, which contain the arguments passed from the command line. gtk_init parses these arguments for special reserved arguments and removes them from the argument list. That’ll be useful in our future programs, so take note:

gtk_init (&argc, argv);

This creates a new window:

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

And this one shows the window:

gtk_widget_show (window);

Last, but certainly not the least, calling this gets us into the main processing loop:

gtk_main ();

And, then we elegantly close off our function

return 0;
}

Ok, put that all together and compile using the following basic compile command:

gcc base.c -o base `pkg-config –cflags –libs gtk+-2.0`

After it compiles, you’ll have a program that you can either double-click, or in terminal type

./base

This will open up a window with the default 200×200 size because we didn’t specify a size. But wait, it say’s base on it’s title bar, not “Hello World”! Yes, that’s just the bare-essential base application we did. We need to add these right before gtk_window_show:

gtk_window_set_title (GTK_WINDOW (window), "Hello, World");

Compile it again, and we have our extremely easy Hello World program.

Hello World In Three Flavors

In one of my previous posts I mentioned that GTK is language-neutral, so you may be wondering why this code looks an awful lot like C. That’s because I prefer to code in C, but you can just as easily code this in C#, Ruby, Perl, or others by referring to the GTK Hello World In Six Different Languages article on which this tutorial was based on. Just scroll down and click on the link in my references below.

Next >> Undecided

References:

February 29, 2008 Posted by punongbisyonaryo | GTK Programming, GTK Tutorials | | No Comments

Setting Up Your GTK Workspace

Alright, so onto the meat of things! Before any of the magic happens, I need to set up my system so that I can actually build something.

Several months ago, I first tried doing this by reading the GTK Tutorial (also in my links), and it said I needed to download GTK source from the GTK website and install it from source. Needless to say, I had too much power in my hands than I knew what to do with and I trashed my Ubuntu box. Ok, so we’ll do this the easy way: Synaptic

Installing GTK Packages Via SynapticOpen up your Synaptic Package Manager and look for libgtk2.0-0, it should already be installed. Check to be sure. There should be several similar packages already installed, but the most important package is the libgtk2.0-0-dev. Hmm, I’ll install libgtk2.0-0-doc for good measure as well. Iwas going to install the libgtk2.0-0-dbg, too, but the package description says most people won’t need this, so scrap that.There are several more, but I think this is enough to get started.

After marking the packages (Synaptic will prompt you that it needs several more packages to install libgtk2.0-0-dev), click on the Apply button and we’re done! Man, this tutorial could be more interesting if it was just a little bit harder. On the other hand, at least I’m sure this won’t trash my desktop. Oh well.

[Edit] I forgot to say thanks to Kostkon and Compyx in the Ubuntu Forums for pointing me in this direction. Thanks!

Next >> Saying Hello World with GTK

February 28, 2008 Posted by punongbisyonaryo | GTK Tutorials | | No Comments

A Short Introduction to GTK

What is GTK And Why Would I Want To Learn It?

I’ll try to keep this really short. First, GTK stands for the Gimp Toolkit and is the main toolkit used by GNOME (GNU Network Object Model Environment), although it is an object-oriented cross-platform toolkit that can be used to create applications independent of GNOME. Why is it called the Gimp Toolkit you ask? Well, it was originally written for the Gimp (GNU Image Manipulation Program, basically the equivalent of Photoshop in Linux). But since GTK had support for several languages, GTK was chosen as the toolkit for GNOME.

It uses LGPL, which means you won’t have to pay a penny for licenses and royalties, EVEN IF you’re program is not for personal or educational use e.g. commercial, paid programs. Programming with a toolkit allows your program to integrate well with your GNOME desktop, which means your program will have the same look and feel as your desktop theme. The toolkit also features high-level and easy to use widgets which are built on the concept of hierarchies and containers, which I’ll probably find out to be more useful than I think (think class hierarchies and inheritance, change the parent, change the children, et cetera).

Well, that was longer than I had wanted. So without further ado, I’m just gonna dive in and get ready to set up my GTK workspace.

Next >> Setting Up Your GTK Workspace

February 28, 2008 Posted by punongbisyonaryo | GTK Programming | | No Comments

As Big As This

I created this blog as a sort of personal journal in which I would write all my discoveries as I try to learn how to program an application in GNOME using GTK, from scratch. While I’ve been coding in quite a few languages, I am not adept at GUI programming, mostly working on the internal subsystems of a software project.

Thus I am hoping that by putting this blog up I can more easily monitor my progress (via frequency of my posts) and track what I have learned so far. I’ll be making a syllabus page somewhere, outlining what I plan to learn and accomplish, and use that page to link to the individual posts as they are created. If there are some out there who might find this information useful, or who would like to contribute to this undertaking, you are all welcome. Please leave me a message in the comments.

With that, let’s start building some neat-o Linux Apps!

February 23, 2008 Posted by punongbisyonaryo | Ramblings | | No Comments