Szilard@BalaBit

Posts Tagged ‘GTK+’

How to use gtkmm custom widget in GTK+

Tuesday, February 26, 2013 @ 09:02 AM Author: Pfeiffer Szilárd

The aim of this post is to demonstrate the fact that it is possible to combine the easy
tom widget derivation in gtkmm with the good old C language-based GTK+. The Glade interface designer binds C and C++ together, since the custom widget created in gtkmm can be used in Glade and can be loaded in a GTK+-based application. Only the following steps have to be performed to reach that goal:

  1. implement the desired custom widget in gtkmm
  2. create a library from the widget for Glade
  3. design the user interface containing the custom widget with Glade
  4. declare the C interface for the application written in GTK+
  5. build the user interface from XML with GtkBuilder

Derive Custom Widget

If you are familiar with the C++ language and also with gtkmm, it will be easy to create the desired custom widget by following the instructions in the custom widget chapter of the gtkmm tutorial. After you have finished that, compile a library from your custom widget that can be used in the Glade user interface designer, and therefore in the GTK+ based application.


Design the User Interface

To use the custom widgets in Glade’y add some extra pure ‘C functions above the custom widget classes, a catalog file that describes the widgets, and the library containing the custom widgets. The details are in my “How to Add Custom gtkmm Widget to Glade” blog post. As a result of the aforementioned development, your custom widget will be handled in Glade just like the stock GTK+ widgets.


Implement the Application

The implementation of the application differs only slightly from the standard way. You only have to register the custom widget, as it is not part of the distributed GTK+ widget set. Then, the user interface can be built from .glade files by GtkBuilder.

Register Custom Widget

Here is the simple C interface for your C++ custom widget library. The only function you have is the same one you have set in the Glade catalog file between the init-function tags as an entry point of the custom widget. The same steps are required when the Glade uses the gtkmm-based custom widget, and also when the actually developed GTK+ application does that, because the situation is the same, as Glade is also written in C.

custom_widget_glade.h

#ifndef CUSTOM_WIDGET_GLADE_H_INCLUDED
#define CUSTOM_WIDGET_GLADE_H_INCLUDED
 
void custom_widgets_glade_init ();
 
#endif

Build the User Interface

The following minimal example demonstrates how you can load a .glade file containing a custom widget. There are only two extra lines compared to a simple GTK+ builder example.

custom_widget_example.c

#include <gtk/gtk.h>
 
#include "custom_widgets_glade.h" // <1>
 
int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkBuilder *builder;
  GError *error = NULL;
 
  gtk_init (&argc, &argv);
  custom_widgets_glade_init (); // <2>
 
  builder = gtk_builder_new ();
  if (!gtk_builder_add_from_file (builder, "custom_widget.glade", &error))
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }
 
  window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
  g_signal_connect (G_OBJECT (window), "delete-event",
                   G_CALLBACK (gtk_main_quit), NULL);
  gtk_widget_show_all (window);
 
  g_object_unref (G_OBJECT (builder));
 
  gtk_main ();
 
  return 0;
}
  1. Include of the C interface of your gtkmm custom widget library.
  2. Initialization of your custom widget after the GTK+ has been initialized and before the user interface itself is loaded from the .glade file that was created earlier.

The custom widget example code can be compiled and run with the following commands. Compilation uses custom_widgets_glade.h and libcustomwidgetsglade.so, which can be created as it is described in “How to Add Custom gtkmm Widget to Glade”. For the compilation to work properly, the library and header file must be placed where the example code is placed, since gcc is searching for them in the current directory (-I -L.).

Compilation and run

gcc custom_widget_example.c -o custom_widget_example \
-I. -L. -lcustomwidgetsglade -fPIC `pkg-config --cflags --libs gtk+-2.0`
LD_LIBRARY_PATH=. ./custom_widget_example

When you run the compiled C source you have to specify where the custom widget library can be found. Youcan do that by setting the LD_LIBRARY_PATH environment variable to the current directory (.) where libcustomwidgetsglade.so can be loaded from.