Problem with ScrolledWindow

Hello! I am using GTK-3 with Vala. In my app when button is clicked then new child is added to Box which is inside ScrolledWindow. I want ScrolledWindow to be scrolled to the bottom when such a thing happens. I tried to use ResizeMode.IMMEDIATE and it worked well (else it just scrolled to the bottom minus added element size) but it is deprecated. What is correct way of doing that?

You can get the scrolled window vadjustment and set its value. Look into gtk_scrolled_window_get_vadjustment and gtk_adjustment_set_value. According to the docs you should set the adjustment value to upper - page-size:

static void
scroll_to_bottom (GtkWidget *scrolled_window)
{
  GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrolled_window));
  double upper = gtk_adjustment_get_upper (adjustment);
  double page_size = gtk_adjustment_get_page_size (adjustment);
  gtk_adjustment_set_value (adjustment, upper - page_size);
}
1 Like

Also, to the “scrolled to bottom minus element size”, you can do.

create a callback to “changed” signal of the GtkAdjustment. This signal is emmited when some attribute different from ‘value’ is changed. Eg. It is emmited when ‘page_size’ changes.

So, all we need to do is to get the ‘upper’ attribute from the GtkAdjustment and set the ‘value’ attribute to this value (or to ‘upper’ - ‘page_size’, if you prefer).

Doing this, the scroll will be on the bottom of the Scrolled Window.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.