GTK+ FAQ
<<< Previous | Development with GTK+: general questions | Next >>>

# How do I identifiy a widgets top level window or other ancestor?

There are a couple of ways to find the top level parent of a widget. The easier way is to call the `gtk_widget_top_level()` function that returns pointer to a GtkWidget that is the top level window.

A more complicated way to do this (but less limited, as it allows the user to get the closest ancestor of a known type) is to use `gtk_widget_get_ancestor()` as in:

          GtkWidget       *widget;
          widget = gtk_widget_get_ancestor(w, GTK_TYPE_WINDOW);

Since virtually all the GTK_TYPEs can be used as the second parameter of this function, you can get any parent widget of a particular widget. Suppose you have an hbox which contains a vbox, which in turn contains some other atomic widget (entry, label, etc. To find the master hbox using the `entry` widget simply use:

          GtkWidget       *hbox;
          hbox = gtk_widget_get_ancestor(w, GTK_TYPE_HBOX);

<<< Previous | Home | Next >>>
Why don't the contents of a button move when the button is pressed? Here's a patch to make it work that way... | Up | How do I get the Window ID of a GtkWindow?