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?
2 Answers
Not all of them have one.
Those widgets that do will implement the GtkNative interface, which provides the gtk_native_get_surface function, allowing you to obtain a GdkSurface. In gtkmm, this will correspond to casting to to Gtk::Native and calling get_surface.
To obtain a Window handle from that, you can use the GDK_SURFACE_XID macro. For that, I don’t think a C++ wrapper exists; you will have to call Gdk::Surface::gobj to obtain a GdkSurface * and use the C API.
- 26,597
- 4
- 30
- 62
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.
- 155
- 1
- 8