How to use Gtk::Layout and Gtk::ScrolledWindow


I started a previous thread on how to achieve a node based interface for visual programming here.

My goal is to have a scrollable and zoomable area where the nodes (that are actually a derived Gtk::Grid widget) are displayed.

I was advised to use the Gtk::Layout class which have the following documentation :

Infinite scrollable area containing child widgets and/or custom drawing.

This is exactly what I want so I am also using the Gtk::ScrolledWindow class where I can have scrollbars since the layout supports scrolling natively.

Now I am struggling to figure out how it works. I read on StackOverflow here the following (by @underscore-d) :

Gtk::Layout can be placed into a Gtk::ScrolledWindow , and when the scrollable area is set to something larger than the visible allocation, scrollbars will show up.

However, this example doesn’t work, the scrollbars are not here :

#include <gtkmm.h>

int main(int argc, char *argv[])
  auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  Gtk::Window window;
  Gtk::ScrolledWindow scrolled_window(Gtk::Adjustment::create(0, 0, 5000), 
                                      Gtk::Adjustment::create(0, 0, 5000));

  // The layout scrolling area is 5 times lower than the scrolled window one
  Gtk::Layout layout(Gtk::Adjustment::create(0, 0, 1000), 
                     Gtk::Adjustment::create(0, 0, 1000));

  Gtk::Button button("Hello World!");
  layout.put(button, 100, 100);



  return app->run(window);

Can someone tell me what is wrong here? :wink:

You must share the GtkAdjustment instance between the scrolled window and the layout, which will then be used by the scroll bars. Adjustments are the “model” for the scrollable area and its changes; widgets are the “views”.

If you want to apply some sort of scaling factor, you will need to ensure that the layout and scrolled window adjustments are “bound” together, for instance by using g_object_bind_property_full() with a custom transformation function, to ensure that any change in the scrolled window adjustments will cause a change in the layout’s adjustments.


Hi @ebassi,

Thank you for your answer! :wink:

However when I try to share the Gtk::Adjustment instances :

Glib::RefPtr<Gtk::Adjustment> hAdj = Gtk::Adjustment::create(0, 0, 1000);
Glib::RefPtr<Gtk::Adjustment> vAdj = Gtk::Adjustment::create(0, 0, 1000);

Gtk::ScrolledWindow scrolled_window(hAdj, vAdj);
Gtk::Layout layout(hAdj, vAdj);

The scrollable are is not larger than the location of the button at (100, 100).

Does it have something to do with the page_size member variable?

Can someone please help me to understand how it works? I am really stuck now :wink:

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