How to get widget absolute position?

I have a GtkWindow with a GtkWidget inside it. How do I get the GtkWidget absolute position on the screen (I might want relative to the monitor )?

Windows have decorations, and if I position window on 200, 200 the content will be positioned on 200 + left decoration and 200 + top decoration.

I guess gtk does this to position combo-boxes menus (for example). I want to do a similar thing on a custom component not written using gtk, but on a gtk window.

I have looked on gtk menu positioning, but it takes many things into account. I was wondering if there is a simpler way to get widget positioning.

You don’t. At most, you can get the relative position to the top-left corner of the GtkWindow’s content area.

This is the result of two things:

  • on X11, the location of the decoration depends on whether the window manager is a reparenting one or not, and whether the window manager exposes that information to its clients
  • on Wayland, applications do not have access to the global coordinate system, only the compositor has that information, so every top-level window exist in its own isolated coordinate space that has its origin in the top-left corner of the window

Additionally, both on X11 and Wayland, window management policies may change the position of top level windows, unless they are marked as “pop up” (override-redirect, on X11); and you need to know the geometry of the monitors to see if your window will be fully on screen or not.

GtkMenu has a ton of API to deal with this because it’s not easy, and it cannot be done portably from outside the toolkit.

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