Andy Ef
Andy Ef

Reputation: 175

gtkmm 4: How to get X window ID from inside widget?

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

Answers (2)

Andy Ef
Andy Ef

Reputation: 175

I wanted to add two things to the accepted answer

  1. 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()

  2. 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

dumbass
dumbass

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

Related Questions