1

I am writing a Vala application.And I want some function to be executed on window resize. I tried to rewrite C example with window resizing callback, it seems to be working (compiling, I mean), but when I run my program it segfaults.

Here is my code:

Gtk.Widget toplevel = this.get_toplevel();
Gtk.Window window = (Gtk.Window)toplevel;

....

Signal.connect(window, "size-allocate", (GLib.Callback)this.callback, null);

and the function callback() is:

private bool callback(Gtk.Widget* window, Gtk.Allocation? a,  char * data )
{       
    resizeAllImages(window->get_allocated_width());
    return false;
}

I tried to rewrite this (http://osdir.com/ml/gtk-list/2010-01/msg00092.html) tutorial into Vala, but it looks like I am doing something wrong. Can you help me with it?

serge1peshcoff
  • 3,242
  • 8
  • 36
  • 63

1 Answers1

3

You don't need to use connect directly. There is built in handling for signals. The signals have the same name with the dashes changed to underscores (e.g., Gtk.Widget.size_allocate).

To connect, simply add it to the signal handler like this:

widget.size_allocate.connect(this.callback);

Your signal handler should look like:

private bool callback(Gtk.Widget sender, Gtk.Allocation? a) { ...

The reason your method is failing is that you are passing null to the handler:

Signal.connect(window, "size-allocate", (GLib.Callback)this.callback, this);

Vala makes a distinction between delegates with a context (i.e., closures) and delegates without context (i.e., function pointers, a.k.a. “static delegates”). If you look at Gtk.Callback you'll notice that has_target = false. This makes it a static delegate, so you need to pass the data ponter with something as needed by your callback.

apmasell
  • 6,645
  • 17
  • 28