Search code examples
gtkui-automation

Finding children of a GtkWidget


I need to be able to explore a GTK GUI's structure programmatically. I have the GtkWidget and I want to find any children of that widget. Now I know that GtkContainer's have a function to find children and that GtkContainer is derived from GtkWidget.

Is there anyway I can check if a widget is a GtkContainer and then perform the cast? If not, is there any other way I can discover the GtkWidget's that are children of the one I have?


Solution

  • Yes, there are macros for every GObject type to check whether an object is of that type:

    if(GTK_IS_CONTAINER(widget)) {
        GList *children = gtk_container_get_children(GTK_CONTAINER(widget));
        ...
    }
    

    If the widget is a GtkBin it has only one child. In that case, the following is simpler than dealing with a GList:

    if(GTK_IS_BIN(widget)) {
        GtkWidget *child = gtk_bin_get_child(GTK_BIN(widget));
        ...
    }