Monday, November 2, 2009

Tut 2: Simple Gtkmm Window

Ok guys, I'm jumping straight to the chase, no program design tutorial, no "basics" of anything, were going to display a simple "hi" box.

GTK+ is a GUI toolkit. That means you build Graphical User Interfaces with it, and that's what were going to do. If you don't understand a certain aspect of this code/tutorial, please let me know so I can improve the content.

#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>

int main (int argc,char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Button button;

button.set_label("This is a GTK+ GUI");

window.add(button);
window.set_border_width(10);
window.set_default_size(300,150);
window.set_position(Gtk::WIN_POS_CENTER);

window.show_all();

kit.run(window);

return 0;
}


I'm assuming that you can set up the enviroment as per the "Hello World" tutorial. Then put that code into your main.cpp file. If you try running now, MonoDevelop will probably tell you this:


Why does this not work? That has to do with how C++ works. When we compile code, we transform the code into something called an Object file. These files end in .o These .o files are then "linked" with libraries that we've used, to create an "executable" which we can then run on the computer.

What's going wrong here, is that we have used GTKmm in our code, (see the #includes), but we havent told MonoDevelop that we want it to link to GTKmm for our exectuable. So that's all we've got to do.

On the left, there should be "pane" showing this:

If that's not there on your left, make sure that the "solution" tab is selected (bottom left).

Now right click on the "Packages" item (as per 2 pictures up), select "Edit Packages" from the right-click menu, and a popup box should appear:


If the "gtkmm" item isnt in your list, that's ok! That means you don't have the gtkmm-dev packages installed. Lets go to Synaptic (or your favorite package manager), and search for GTKmm. The following are my results, if yours are a bit different that's ok. If gtkmm-dev isnt in that list either, get in contact with somebody that knows your setup or email me!



Just install that package (& its dependencies), go back to MonoDevelop, try to add the package and this time it should be there. Now click Debug in MonoDevelop (or hit F5), and it should pop up a little box with a button in the middle..! Kinda like this one:


Now the colours might be different, it might have a different name on top, but it should run!
Any problems, please comment! Congrats on your Gtkmm GUI app!
Till next post, -Harry

1 comment: