Archive for the ‘C/C++’ Category

Why Gtk+ May Just be the Best GUI Toolkit Ever

Posted on June 2nd, 2009 in C/C++, GUI, Gtk+, Toolkits | No Comments »

When it comes to programming a lot of people look to C/C++ for answers. They’re pretty straightforward languages that are general-purpose(they aren’t really good at anything in particular, but can be used to do just about anything). The only strike against these languages is that unlike Java, they don’t bring a GUI API built in. Instead, C/C++ coders must rely on other libraries and even system APIs to get the job done.

Introduction
Nowadays almost every application has to have a GUI. Users rely on graphical interfaces, and a programmers job isn’t only to code up an app, but to also make it best for the consumer. Now this leads one to wonder, “What should I use to make the GUI part of my program?”.

Well, the answer isn’t always straightforward, but there is one toolkit that looks a little better than the rest. It’s Gtk+. GTK+, or The GIMP Toolkit, is a cross-platform GUI toolkit and, is one of the most popular toolkits for the X Window System, along with Qt.

So what makes Gtk+ so speacial? Why could it possibly deserve the title of “Greatest GUI Toolkit Ever”? That is the point of this article. Here we will discuss a little bit about it, and see why it is a very good choice for developers.

1. Simple Syntax
One of the best features of Gtk+ is its simplicity of syntax. Almost everything is predefined, you just gotta point everything in the right direction.

For example, this little snippet below creates a pretty basic window (300px wide, 150px high, aligned to the center of screen, and says “A Window” in the titlebar):

#include 

int main( int argc, char *argv[])
{
 GtkWidget *window; // creates the window object

 gtk_init(&argc, &argv); // this handles commandline arguments, don't worry to much about it

  /* Here we setup the window */
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 300, 150);
  gtk_window_set_title(GTK_WINDOW(window), "A Window");

  gtk_widget_show(window); // this makes it appear

  gtk_main(); // this puts the program in a loop waiting for events

  return 0;
}

Amazing. That was all done in about twenty lines of code, and you could even reduce that more by getting rid of whitespace. Just for fun, I tried making something similar in Win32; my result was more than 70 lines of code, and that was just for the basics.

In case you’re wondering why you would choose a toolkit just because of syntax ease (or if you should even care), you must consider that the easier it is to write a program, the easier it would be to take care of it, as well as find bugs. As you will see later, Gtk+ has many features that help to make maintenance easier.

2. Great Structure
Not does only does Gtk+ have sytax that’s easy to follow, but is also designed in a way that makes it easier to work with. For example, you define the objects and widgets (a button or window, or dialogue), you pass it parameters and set it up, and you then either pack it into the window, or a box etc. Then all that’s left is to make it show up. Of course a real application would have callbacks so that widgets would respond to events, but still that’s pretty good.

It’s structure like this that helps programs for becoming “code soup” and lets developers worry less about “how they’re gonna setup the application”, and more about “when they are gonna code that badboy up”.

3. Lightweight and Fast
One of the best things about Gtk+ is its weight and execution times. This toolkit is especially good if resources are a concern to you. Applications written with Gtk+ are far less likely to crash due to not enough memory, freeze up, etc.

Here’s a good example: I’m working on creating a open source project right now. So far all I’ve coded is the GUI, and while I still have a long way to go before it has full functionality, the bare-bones app is only 30.4KB and executes in well under a second. Now it’s been a while since I’ve done a lot in Win32, but from what I remember even very simple applications were in the “200KB” range. That’s more than 6 times as big. In the long run, that could really add up.

4. Portable
Last but not least is the fact that Gtk+ is portable. It’s pretty much write once, work anywhere (Of course the users have to have the right stuff to run you’re program, but this generally wouldn’t be an issue).

This is a great thing for programmers. A lot of programmers are turning to Java because of its “multi-platformism”; but this gives C/C++ coders a real alternative to learning a whole new language, or having to be greatly limited by another language. The portability of Gtk+ is a truly great advantage and is also a good explanation for success of toolkits like “WxWidgets” and “QT”

Conclusion
In conclusion I hope you considered many of the things I’ve mentioned here, and are now looking into getting to know/use Gtk+. My goal here was to show you that Gtk+ is an amazing resource for C/C++ developers, and I hope I did so in as best as I could. If you find any errors, or have any suggestions, etc. please comment (I love it when people comment :D).

I Think Java Coderz Are Just Lazy

Posted on March 30th, 2009 in C/C++, Java, Opinions | 2 Comments »

In the world of programming, if an application needs a GUI (what doesn’t these days) the first thing that comes to mind is “Java“; aren’t I right???? Seriously! It’s easy, cross compatible, and has great reviews. Java looks like an all around great deal!

Well I don’t want to rain on all you Java coderz parades, so I’m gonna pour and flood.

When it comes to application working, it doesn’t get any better that C++. Now I’m pretty bias, but let me explain. C++ can beat Java in the three things I mentioned above.

It’s Easy - Sure Java is easy. Things are based in a OOP kinda way. However, one must remember Java is just C++ deep down inside, really it is. The only difference between the two is that Java brings it’s API built in, while C++ relies on imported/exported libraries and interfaces. However, just include the file windows.h and you have the entire Win32 API at your finger tips. C++ is better than Java, you just got to find a good library or two.

It’s cross compatible - Well while we all would love “write once work on all toasters”, that’s beyond the point. C++ will work on any platform just like Java. Sure you’ll need separate libraries for different things, but how much does it matter. While you may have to recode the GUI, most of the internal code will remain untouched. If you use Java just because you don’t want to rewrite an interface, then you are just lazy (and my parents taught me that was bad. *Tisk Tisk*) Really how hard is it to rewrite it, unless we’re talking a “extremely” large application.

Java has great reviews - So does C++, as simple as that

Example
Now to help illustrate all of this, let me share a story if you will. I have a friend called Blake. He made a pretty simple instant messaging program in Java (with the server software in C++). Well, let me tell you the trouble he’s had. In my opinion it’s GUI sucked under Linux, it didn’t work in Mac OS X (so much for platform independent), and a lot of the time it froze.

Now I don’t want to criticize my good ol’ buddy, but if he had written it in C++, I’d bet a $100,000,032.67 that he would not have a single problem.

I hope that all of you will consider my opinions and look more to C++. I’m not saying Java is a terrible language, I’m saying C++ is better

Until next time,
~ mike