0

I have a class inheriting from Gtk::Box so I can create multiple instances of it at runtime and add them dynamically to a Gtk::Notebook.

However if there is a critical error, I want to pop up a message that won't disappear behind the main window, so I need to get the parent window from within my derived Gtk::Box class.

Going off the question here (which is for GTK, not for gtkmm), I have done this:

Gtk::Container *parent = this->get_toplevel();
if (parent->get_is_toplevel()) {
    Gtk::MessageDialog dlg(*parent, "blah");
    dlg.run();
}

However I get an error telling me there is "no known conversion from Gtk::Container to Gtk::Window".

Am I supposed to use a dynamic_cast<> to typecast the Gtk::Container into a Gtk::Window? If so, is it unnecessary to call get_is_toplevel()? (Because that's kind of the same as checking the result of the dynamic_cast<>).

Or is there a different way this should be done in gtkmm?

Community
  • 1
  • 1
Malvineous
  • 25,144
  • 16
  • 116
  • 151
  • `static_cast` seems to work, so there might not be a need for `dynamic_cast`. I guess `dynamic` is theoretically safer though, but `static` is probably fine as long as you know for sure the toplevel is a `Window`, which I do. :) – underscore_d Apr 12 '16 at 18:03

1 Answers1

3

Well, this works, but I've no idea whether it's correct or not:

Gtk::Window *parent = dynamic_cast<Gtk::Window *>(this->get_toplevel());
if (parent) {
    Gtk::MessageDialog dlg(*parent, "blah");
    dlg.run();
}
Malvineous
  • 25,144
  • 16
  • 116
  • 151
  • `static_cast` seems to work, so there might not be a need for `dynamic_cast`. I guess `dynamic` is theoretically safer though. – underscore_d Apr 12 '16 at 18:03