views:

248

answers:

2

Which event from which widget should I catch when I need to run some code when ScrolledWindow is scrolled?

Ths widgets tree I am using is: (my widget : Gtk.Container) > Viewport > ScrolledWindow

I tried many combinations of ScrollEvent, ScrollChild, etc. event handlers connected to all of them, but the only one that runs anything is an event from Viewport that about SetScrollAdjutstments being changed to (x=0,y=0) when the application starts.

+1  A: 

You should attach to the GtkAdjustment living in the relevant scrollbar, and react to its "changed" event. Since Scrollbars are Ranges, you use the gtk_range_get_adjustment() call to do this.

unwind
It worked, but is a bit tricky in gtk#... - added code separately.
viraptor
A: 

unwind's answer was correct. Just posting my code in case someone needs a full solution:

// in the xxx : Gtk.Container class:

protected override void OnParentSet(Widget previous_parent) {
    Parent.ParentSet += HandleParentParentSet;
}

void HandleParentParentSet(object o, ParentSetArgs args) {
    ScrolledWindow swn = (o as Widget).Parent as ScrolledWindow;
    swn.Vadjustment.ValueChanged += HandleScrollChanged;
}

void HandleScrollChanged(object sender, EventArgs e) {
    // vertical value changed
}

If you need to change the parent of any of those widgets, or may need to change the types and change the hardcoded types and handle disconnecting from the previous parent.

viraptor