2

I make my interface with glade. Works great. I write a tiny little main function which calls gtkbuilder and renders everything in the glade file.

Gtk::Main kit(num, opts);
// Load the GtkBuilder file and instantiate its widgets:
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("dsg.glade", "mainwindow");

Works even better. Then I get my widget

Gtk::Button *sf = 0;
builder->get_widget("button", sf);

Now what? Every example I've found to connect a signal handler is not built by gtkbuilder. If you instantiate your own class to represent/handle/render a button it's easy to connect a signal handler to it, but I'm using gtkbuilder and I don't see how to write a function that I can then attach to my widget, since I wasn't the one creating the button object, gtkbuilder was.

Do I make a subclass of gtkbutton write my function then point to that? But my class isn't being instantiated by gtkbuilder.

I just don't get it. Help?

skaffman
  • 398,947
  • 96
  • 818
  • 769
stu
  • 8,461
  • 18
  • 74
  • 112

2 Answers2

4

You're not missing something obvious. It seems that gtkmm doesn't provide a C++ version of the gtk_builder_connect_signals() function, which is how you do it in C. I've done a little Google searching, but I can't figure out why they would leave it out.

You can access the C function directly like this:

Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("dsg.glade", "mainwindow");
gtk_builder_connect_signals(builder->gobj(), callback_data);
ptomato
  • 56,175
  • 13
  • 112
  • 165
  • Thanks kindly. Nice to know I'm not going nuts. – stu Nov 04 '10 at 20:33
  • It seems what you have to do is connect it to an standalone function, as in a function that's not part of a class. I can see why attaching it to a class would be a problem, in fact this is the exact problem I'm having, you don't have source code for the object that would get the callback. – stu Nov 04 '10 at 20:34
  • And I imagine making it bind to a C++ function would be a PIA. I imagine I could call gtk_builder_connect_signals() if I made all my signal handlers extern "C" {} – stu Nov 04 '10 at 20:35
  • @stu, that's true. So annoying that different C++ compilers don't mangle their names in the same way... – ptomato Nov 05 '10 at 00:06
3

you should use your own button class like this:

#ifndef _MYBUTTON_H_
#define _MYBUTTON_H_

#include <gtkmm.h>
class MyButton : public Gtk::Button { 
  public:
    inline MyButton(BaseObjectType* cobject) : Gtk::Button(cobject) {};
  protected:
    // overwrite virtual void on_pressed() function of Gtk::Button
    void on_pressed() { /* do something */ };
};
#endif

Now somewhere in your main code (I put it in my Core class who is of public of Gtk::Window):

MyButton *sf;
refBuilder_->get_widget_derived("button", sf);

I found this in the official gtkmm manual some month ago and using it successfully in my GUI applications.

I hope this helps.

Henning
  • 31
  • 1