get_toplevel
Description:
This function returns the topmost widget in the container hierarchy this is a part of.
If this has no parent widgets, it will be returned as the topmost widget. No reference will be added to the returned widget; it should not be unreferenced.
Note the difference in behavior vs. get_ancestor; `gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW)` would return null if this wasn’t inside a toplevel window, and if the window was inside a Window-derived widget which was in turn inside the toplevel Window. While the second case may seem unlikely, it actually happens when a Plug is embedded inside a Socket within the same application.
To reliably find the toplevel Window, use get_toplevel and
call GTK_IS_WINDOW
on the result. For instance, to get the title of a widget's toplevel window, one might use:
static const char *
get_widget_toplevel_title (GtkWidget *widget)
{
GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
if (GTK_IS_WINDOW (toplevel))
{
return gtk_window_get_title (GTK_WINDOW (toplevel));
}
return NULL;
}
Parameters:
this |
a Widget |
Returns:
the topmost ancestor of this, or this itself if there’s no ancestor. |