In gtkmm 4, how can one get the X Window ID of type XID as defined in X11/X.h, from inside a class, that inherits from Gtk::Widget?
gtkmm 4: How to get X window ID from inside widget?
787 Views Asked by Andy Ef At
2
There are 2 best solutions below
0
On
I wanted to add two things to the accepted answer
It is of course important to check if
get_surface()returned a valid nonzero object indeed. Otherwise get the ID after the Widget'ssignal_realize()is emitted, which is done after the widget is assigned to a surface. This can be achieved by overriding the default handleron_realize()Instead of casting and calling
((Gtk::Native)this)->get_surface()it is also possible to call likeget_native()->get_surface().
In conclusion do
void myWidget::on_realize() {
// Call default handler
Gtk::Widget::on_realize();
XID x_window = GDK_SURFACE_XID(get_native()->get_surface()->gobj());
}
to get the X window ID as early as possible.
Not all of them have one.
Those widgets that do will implement the
GtkNativeinterface, which provides thegtk_native_get_surfacefunction, allowing you to obtain aGdkSurface. In gtkmm, this will correspond to casting to toGtk::Nativeand callingget_surface.To obtain a
Windowhandle from that, you can use theGDK_SURFACE_XIDmacro. For that, I don’t think a C++ wrapper exists; you will have to callGdk::Surface::gobjto obtain aGdkSurface *and use the C API.