Wednesday, February 28, 2007

A detailed exploration of gears.c

As soon as I saw gears.c, I decided to make it my first experiment of the library. Here is a snapshot of the program.

So, let's focus on its source.
int
main (int argc, char *argv[])
{
GdkGLConfig *glconfig;
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *drawing_area;
GtkWidget *button;

gtk_init (&argc, &argv);
gtk_gl_init (&argc, &argv);
glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB    |
                                      GDK_GL_MODE_DEPTH  |
                                      GDK_GL_MODE_DOUBLE);
if (glconfig == NULL) {
  g_print ("*** Cannot find the double-buffered visual.\n");
  g_print ("*** Trying single-buffered visual.\n");

  glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB   |
                                        GDK_GL_MODE_DEPTH);
  if (glconfig == NULL) {
    g_print ("*** No appropriate OpenGL-capable visual found.\n");
    exit (1);
  }
}

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "gears");
gtk_container_set_reallocate_redraws (GTK_CONTAINER (window), TRUE);

g_signal_connect (G_OBJECT (window), "delete_event",
                  G_CALLBACK (gtk_main_quit), NULL);

vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);

drawing_area = gtk_drawing_area_new ();
gtk_widget_set_size_request (drawing_area, 300, 300);

gtk_widget_set_gl_capability (drawing_area,
                              glconfig,
                              NULL,
                              TRUE,
                              GDK_GL_RGBA_TYPE);

gtk_widget_add_events (drawing_area,
                       GDK_VISIBILITY_NOTIFY_MASK);

g_signal_connect_after (G_OBJECT (drawing_area), "realize",
                        G_CALLBACK (init), NULL);
g_signal_connect (G_OBJECT (drawing_area), "configure_event",
                  G_CALLBACK (reshape), NULL);
g_signal_connect (G_OBJECT (drawing_area), "expose_event",
                  G_CALLBACK (draw), NULL);
g_signal_connect (G_OBJECT (drawing_area), "map_event",
                  G_CALLBACK (map), NULL);
g_signal_connect (G_OBJECT (drawing_area), "unmap_event",
                  G_CALLBACK (unmap), NULL);
g_signal_connect (G_OBJECT (drawing_area), "visibility_notify_event",
                  G_CALLBACK (visible), NULL);

g_signal_connect_swapped (G_OBJECT (window), "key_press_event",
                          G_CALLBACK (key), drawing_area);

gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);

gtk_widget_show (drawing_area);
button = gtk_button_new_with_label ("Quit");

g_signal_connect (G_OBJECT (button), "clicked",
                  G_CALLBACK (gtk_main_quit), NULL);

gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
gtk_widget_show (button);
gtk_widget_show (window);

gtk_main ();
return 0;
}
Here we will explain the whole thing step by step. There are two init functions in the beginning, which extracts those options they care so that the rest are what the application really cares about.

Friday, February 09, 2007

The relationship of several libraries

As is mentioned in the web sites of GTK+ and Gtkmm, there are several packages which are highly related to the topics here. Their relationship is of utmost importance to the understanding of the mechanisms of GTK+ and Gtkmm.

GTK+ is originally GTK, GIMP ToolKit. There are three supportive libraries, GLib, Pango and ATK. GLib provides lots of related functions that GTK+ widgets rely on, such signal system(here the term signal is different from the one Linux OS uses to send to processes). Pango handles layout and rendering texts. And ATK contains interfaces for accessibility.

However, the term GDK, GIMP Drawing Kit, can be found from time to time in the related materials. GDK is simply a wrapper for Xlib and if familiar with Xlib, it is not that essential here.

Gtkmm, a C++ wrapper of GTK+, provides a different developing model. It relies on libsigc++, glibmm. Here libsigc++ is a C++ flavor of realization of signal system.

GNOME project prospers on the GTK+. And based on Gtkmm, there is also a gnomemm library. Glade is a tool for creating GUI of GTK+ and libglade provides an alternative with XML as the GUI descriptor and dynamic creation of GUI. Not surprisingly, there will be a libglademm.

In order to compile several programs listed later, we need several packages:

  • GTK: gtk2, gtk2-devel
  • gtkglext: gtkglext, gtkglext-devel
  • gtkmm: gtkmm24, gtkmm24-devel
  • libglade: libglade, libglade-devel

A blueprint

Here are several things that I have decided to read. The progress will be exhibited here.

GTK+, a light GUI library, we have a pieve of tutorial for GTK+-2.0 and related FAQ. A C++ wrapper is provided, with the name Gtkmm. They kindly give us an API documentation and its FAQ. There is an OpenGL interface for GTK+, called GtkGLExt. There are few materials on it yet. I have to find more later on.