1

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?

BobMorane
  • 3,870
  • 3
  • 20
  • 42
Andy Ef
  • 155
  • 1
  • 8

2 Answers2

3

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.

user3840170
  • 26,597
  • 4
  • 30
  • 62
2

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.

Andy Ef
  • 155
  • 1
  • 8