How do I identifiy a widgets top level window or other ancestor? [GTK 2.x]

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

# How do I identifiy a widgets top level window or other ancestor? [GTK 2.x]

There are a couple of ways to find the top level parent of a widget. The easiest way is to call the `gtk_widget_get_toplevel()` function that returns a 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);

You can also follow the a widgets ancestry by using the function `gtk_widget_get_parent()` that returns a pointer to a widgets parent widget.

<<< 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... [GTK 2.x] | Up | How do I get the Window ID of a GtkWindow? [GTK 2.x]