Reputation: 175
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
?
Upvotes: 2
Views: 965
Reputation: 175
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's signal_realize()
is emitted, which is done after the widget is assigned to a surface. This can be achieved by overriding the default handler on_realize()
Instead of casting and calling ((Gtk::Native)this)->get_surface()
it is also possible to call like get_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.
Upvotes: 3
Reputation: 27200
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.
Upvotes: 4