One essential thing, as I see it, to be able to size and position your widgets on the screen, is to get and set the position of a widget. When I use GtkWindow::get_position I always get position 0,0. According to https://developer.gnome.org/gtkmm/stable/classGtk_1_1Window.html#ad3a063730ccbf0e4fa26f89e220f55e4 that could happen given some circumstances. However, I have no idea if my environment have all those circumstances met, nor how I can bypass the problem to get the information (position) I need. And there is no information on that web page on how to get around the problem. This seems like a cruical problem to get around but I find it difficult to find anything by googling (please bear in mind that I just started with Gtk, but I have developed GUI applications using other frameworks and platforms).
How do I solve my problem?
I believe I found it finally; first a call to Gtk::Window::get_root_window followed by get_position on that window.
Yes there exist GUI toolkits where widgets are positioned manually. But I assume that you have already noticed that we do that generally not for GTK, we put widgets just into containers like boxes, grids and others, with some layout constraints like fill, expand and so. And I think for gtk4 there will be even one more layout tool for this kind of automatic layout. If you really intent manually positioning, I think there was a container called GtkFixed available – but I think no one has ever used that, so maybe it has vanished. And if you are still looking for coordinates, maybe try
But I have no idea if that helps.
Maybe you can tell us for what you need pixel coordinates. Maybe storing window position? But some window managers have their own idea of window positions, so that may not work. EBassi may tell you more, when you can convince him why you need it.
Hi Stefan and thanks for your response!
Hmm… on the other hand, if I can avoid having to calculate exactly where to put the widgets that is ok. When using, for instance, the Paned widgets you do need the height of the surrounding window to be able to tell your Paned instance where to draw the divider. For that I guess you need get_root_window to get the height first? But there I seem to be lost yet again; my maximzed root window has top X=0 and height=1024. When calling myPaned.set_position(512) the divider is not drawn in the middle, as I would expect it to be. Why not?
Yes, the GtkPaned can be a problem! I have used one side of the Paned for a listview and other side for a drawing area. I think I have never managed to get the size of the listview – but I have to admit that trying getting the size makes not much sense, as content can change… So I think just saving the actual position of the divider of the paned, and saving also the window size is all what makes sense. The user has to resize the initial window, and to divide the paned as useful. Saving the values is easy with gsettings.
So you have to live with an initial view (first time startup) that looks a bit weird then? Thanks for the info on gsettings! Will look into that!
Wrote it some days ago:
Word of warning:
- not all windowing systems have global screen coordinates; on Wayland, for instance, your windows will always be at (0, 0) and you won’t ever be able to set the absolute screen coordinates of any top level window
- root windows are an X11-ism, and they are gone in GTK 4 precisely for that reason
That’s an odd thing to do with GTK. Widgets are positioned by containers with a layout policy. The only containers in GTK that allows setting a position on its children are GtkFixed and GtkLayout—which are basically the same container. Sizes in GTK are driven by the content of a widget: for instance, the text inside a GtkLabel, or the margins and padding of a GtkButton. You don’t normally set the size of a widget in pixels at all.
Each Paned instance has two properties: “min-position” and “max-position”. Your program must get the values of those properties. Then, it would calculate a position (of the divider) that’s based on those two values. For example, the median value ((min + max) / 2) would put the divider in the middle of the Paned widget.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.