Sunday, February 13, 2011

GTK Chapter 3 Example 1



This does what it looks like - makes a window with four buttons arranged vertically.

I don't have time to go into this in detail, but a big question is the behavior of the boxes. When you click them they vanish and the other buttons automatically adjust to fill in the missing space. It's not the movement that bugs me, it's how the callback to catch the button press signal works. I'm recycling my variable "button" for each new button, so how does that callback know which button is which? My best guess is that there is something going on under the hood that keeps track of things, but I don't really like that answer. If I was doing this without the book's help I'd have made an array of pointers to GtkWidgets and incremented the array with the loop. Is that not necessary?

2 comments:

  1. 'button' is of type 'pointer to GtkWidget'. Its value is the address of the button object created by 'gtk_button_new_with_label(...)'. 'button' is passed by value into the functions that follow... which means that its contents are merely copied.

    You can think of 'button' as a temporary storage variable that only exists in the scope of 'main()'. It is used to temporarily store the address of the current button so that it can be passed on to the functions that care about it.

    Your suspicion that there's "something under the hood that keeps track of things" is most likely correct. I don't know anything about GTK, but I presume 'gtk_box_pack_start_defaults(...)' maintains some sort of table of pointers to the widgets it contains (each COPIED from the arguments supplied to it).

    In fact, GTK itself probably maintains a similar table that it uses to keep track of the mappings between signals and widgets.

    Your proposed "array of pointers to GtkWidgets" would be useful if you wanted to manipulate each of your widgets directly from main(). As it stands now, you lose your pointer to each button after each re-assignment of 'button' and you probably can't get them back without using a GTK API call to dig them out from "under the hood".

    ReplyDelete
  2. I found some documentation on this:

    The GtkVBox object has a distant parent, GtkObject, that has functionality for keeping track of its children.

    GtkVBox lineage:

    http://library.gnome.org/devel/gtk/stable/GtkVBox.html#GtkVBox.object-hierarchy

    GtkObjet mention of child tracking:

    http://library.gnome.org/devel/gtk/stable/GtkObject.html#GtkObject.description

    ReplyDelete