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.