August 2009 Archives

Auto-connecting signal handlers in glademm

| Comments | No TrackBacks

libglade has the possibility to automatically connect all signal handlers specified in a Glade XML file. However, it is not directly possible in libglademm (C++).

Although you find some results when you search the Net for “libglademm autoconnect”, either no solution is given, or the posters mention that “wrapping functions with extern works for them”. No code. So here is a simple example working for me.

minimal.cc:

#include <gtkmm.h>
#include <libglademm.h>
#include <iostream>
#include <glade/glade-xml.h>

extern "C" {
    void on_button1_clicked() {
        std::cout << "Clicked" << std::endl;
    }
}

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

    Glib::RefPtr<Gnome::Glade::Xml> refXml = Gnome::Glade::Xml::create("minimal.glade");
    glade_xml_signal_autoconnect(refXml->gobj());

    Gtk::Window *mainWnd = 0;
    refXml->get_widget("window1", mainWnd);
    Gtk::Main::run(*mainWnd);

    return 0;
}

minimal.glade:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.5 on Thu Aug 20 15:37:26 2009 -->
<glade-interface>
  <widget class="GtkWindow" id="window1">
    <child>
      <widget class="GtkButton" id="button1">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="label" translatable="yes">click me</property>
        <property name="response_id">0</property>
        <signal name="clicked" handler="on_button1_clicked"/>
      </widget>
    </child>
  </widget>
</glade-interface>

Makefile:

CXXFLAGS = $(shell pkg-config gtkmm-2.4 libglademm-2.4 --cflags)
LDLIBS = $(shell pkg-config gtkmm-2.4 libglademm-2.4 --libs)
LDFLAGS = -export-dynamic

minimal: minimal.o

clean:
    rm minimal minimal.o

And then

make minimal

produces something like

g++ -D_REENTRANT -I/usr/include/gtkmm-2.4 -I/usr/lib/gtkmm-2.4/include -I/usr/include/glibmm-2.4 -I/usr/lib/glibmm-2.4/include -I/usr/include/giomm-2.4 -I/usr/lib/giomm-2.4/include -I/usr/include/gdkmm-2.4 -I/usr/lib/gdkmm-2.4/include -I/usr/include/pangomm-1.4 -I/usr/include/atkmm-1.6 -I/usr/include/gtk-2.0 -I/usr/include/sigc++-2.0 -I/usr/lib/sigc++-2.0/include -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/lib/gtk-2.0/include -I/usr/include/cairomm-1.0 -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/directfb -I/usr/include/libpng12 -I/usr/include/atk-1.0 -I/usr/include/libglademm-2.4 -I/usr/lib/libglademm-2.4/include -I/usr/include/libglade-2.0 -I/usr/include/libxml2     -c -o minimal.o minimal.cc
cc -export-dynamic  minimal.o  -lglademm-2.4 -lgtkmm-2.4 -lglade-2.0 -lgiomm-2.4 -lgdkmm-2.4 -latkmm-1.6 -lpangomm-1.4 -lcairomm-1.0 -lglibmm-2.4 -lsigc-2.0 -lgtk-x11-2.0 -lxml2 -lgdk-x11-2.0 -latk-1.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lgio-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglib-2.0   -o minimal

Running the minimal program and clicking the “click me” button should produce a message on stdout.

About this Archive

This page is an archive of entries from August 2009 listed from newest to oldest.

September 2009 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Pages

OpenID accepted here Learn more about OpenID