1

I'm trying to create a GUI using gtk3.0 in c, using gnome-builder 3.22.

I have created a gui using the built-in designer in gome-builder, which is just glade, and for each widget, I have assigned an ID. I have only defined the IDs, since the built-in version of glade inside gnome-builder is not complete, and doesn't have the tab for signals/handlers, only the standalone version of Glade has that.

I want to know, if there's a way to programmatically add signal handler/callback functions to the IDs specified in the xml, without having to do it in the xml itself, or use the standalone Glade software.

It's a fairly small gui, so a few extra lines of code doesn't bother me.

I also tried adding the handlers/callbacks in the xml, and use gtkbuilder's autoconnect function to connect these handlers/signals automatically, but that didn't work either.

The important files in the project has been uploaded to gist:

  • main.c
  • ba-compression-window.ui
  • ba-compression-window.c

gist address: https://gist.github.com/Jebiel/08fdf3b8fbbd34e09bd48d05d79d9cbf

short url: http://gg.gg/eh4hn

Jebiel
  • 52
  • 6

1 Answers1

0

Yes you can do that. You can use g_signal_connect or g_signal_connect_object to connect a handler to an object. The former will require you disconnect the handler yourself manually when you no longer need the handler to be run (ie, when the object is finalized), the later one will do that for you when the object is destroyed (ie, finalized).

You can connect a signal handler in any method after the object is ready for use. Below, I override the constructed method which will be run after the object (Here, BaCompressionWindow) is constructed.

A minimal example will look like this (the back_button should be defined in .ui file):

static void
back_button_clicked_cb (GtkButton           *button,
                        BaCompressionWindow *self)
{
  g_assert (GTK_IS_BUTTON (button));
  g_assert (BA_IS_COMPRESSION_WINDOW (self));

  g_warning ("Back button clicked");
}

static void
ba_compression_window_constructed (GObject *object)
{
  BaCompressionWindow *self = (BaCompressionWindow *)self;

  G_OBJECT_CLASS (ba_compression_window_parent_class)->constructed (object);

  g_signal_connect (self->back_button, "clicked",
                   G_CALLBACK (back_button_clicked_cb),
                   self);
}

static void
ba_compression_window_class_init (BaCompressionWindowClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = ba_compression_window_constructed;
}

A simpler way to do the same would be adding the signal handler in GtkBuilder .ui file like this:

<object class="GtkButton" id="back_button">
  <property name="visible">1</property>
  <property name="label">Back</property>
  <signal name="clicked" handler="back_button_clicked_cb"
          object="BaCompressionWindow"/>
</object>

and bind the callback in the c file (refer the above code for the rest)

static void
ba_compression_window_class_init (BaCompressionWindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  /* Other code */

  gtk_widget_class_bind_template_callback (widget_class, back_button_clicked_cb);
}

Btw, please add relevant code here instead of linking to the source file

Mohammed Sadiq
  • 741
  • 3
  • 3