c++gtkmm4

How to fullscreen on the second monitor in GTKMM 4?


I have a window that extends Gtk::Window. I would like to go fullscreen on the second monitor using fullscreen_on_monitor(const Glib::RefPtr< Gdk::Monitor > &monitor). How do I get the second monitor?

I tried the following without luck:

Glib::RefPtr<Gio::ListModel> ptr = get_display()->get_monitors();
fullscreen_on_monitor((Gdk::Monitor*) ptr->get_object(1).get());

I get the following error: Cannot cast 'Glib::ObjectBase *' to 'Gdk::Monitor *' via virtual base 'Glib::ObjectBase'

I understand that it's because Gdk::Monitor is not a subclass of Glib::ObjectBase.

I also found that I can use Display::get_monitor_at_surface(), but if I understand well, I need to have the surface to be on the second monitor already.

Similar question for rust:

gtk4-rs: Get Monitors as gdk::Monitor not as a glib::Object


Solution

  • As per @Scheff'sCat's comment, using dynamic cast allowed to fullscreen on the second monitor.

    Glib::RefPtr<Gio::ListModel> ptr = get_display()->get_monitors();
    auto t = ptr->get_object(1).get();
    auto t2 = dynamic_cast<Gdk::Monitor*>(t);
    Glib::RefPtr<Gdk::Monitor> t3(t2);
    fullscreen_on_monitor(t3);