gtkgtkmm

Gtk::ScrolledWindow disable scroll to focused child


I have a very big Gtk::EventBox in a Gtk::ScrolledWindow.
The moment I do grab_focus() at my Gtk::EventBox,
the Gtk::ScrolledWindow scrolls to the top of the Gtk::EventBox.

How can I disable this behaviour ?


Solution

  • Gtk::EventBox does not inherit Gtk::Scrollable
    and therefor gets wrapped with a Gtk::Viewport
    when it gets added to a Gtk::ScrolledWindow.

    To disable scroll to focused child you need to change the focus_hadjustment/focus_vadjustment

    //Disable scroll to focused child
    auto viewport = dynamic_cast<Gtk::Viewport*>(m_scrolled.get_child());
    if (viewport) {
      auto dummy_adj = Gtk::Adjustment::create(0,0,0);
      viewport->set_focus_hadjustment(dummy_adj);
      viewport->set_focus_vadjustment(dummy_adj);
    }