Consider the following macros: #define WindowObject gtk_widget_new(GTK_TYPE_WINDOW #define VBoxObject gtk_widget_new(GTK_TYPE_VBOX #define HBoxObject gtk_widget_new(GTK_TYPE_HBOX #define LabelObject gtk_widget_new(GTK_TYPE_LABEL #define ButtonObject gtk_widget_new(GTK_TYPE_BUTTON ... // add one for each widget type #define End NULL)
... allows to write code like this: GtkWidget *win, *label, *ok, *cancel; win = WindowObject, "child", VBoxObject, "child", label = LabelObject, "label", _("Hello world!"), "wrap", FALSE. End, "child", HBoxObject, "child", ok = ButtonObject, "label", _("_Ok"), "use-underline", TRUE, End, "child", cancel = ButtonObject, "label", _("_Cancel"), "use-underline", TRUE, End, End, End End; The same code, using "traditional" gtk coding would be: GtkWidget *win, *label, *ok, *cancel; GtkWidget *top, *buttons; ok = gtk_button_new(); gtk_button_set_label(GTK_BUTTON(ok), _("_Ok")); gtk_button_set_use_underline((GTK_BUTTON(ok), TRUE); cancel = gtk_button_new(); gtk_button_set_label(GTK_BUTTON(cancel), _("_Cancel")); gtk_button_set_use_underline(GTK_BUTTON(cancel), TRUE); buttons = gtk_hox_new(); gtk_container_add(GTK_CONTAINER(buttons), ok); gtk_container_add(GTK_CONTAINER(buttons), cancel); label = gtk_label_new(_("Hello world!"); gtk_label_set_wrap(GTK_LABEL(label), FALSE); top = gtk_vbox_new(); gtk_container_add(GTK_CONTAINER(top), label); gtk_container_add(GTK_CONTAINER(top), buttons); win = gtk_window_new(); gtk_container_add(GTK_CONTAINER(win), top); What do people think about this style of writing GTK programs? Obviously there are pros and cons. I start with the latter - no type checking. Since the whole creation of the object tree is one nested function call with variable number of arguments, the compiler cannot check the types of arguments. I actually had a crash once when I forgot to place a comma between two string constants, an attribute followed by its value. I am wondering if there any extensions in current C compilers that would enable type checking on vargars calls (similar to how printf format strings and arguments are type-checked nowadays)? - no syntax checking of attributes. Since the code uses strings like "label" there is no check (at compile-time) possible when one accidentally misspells an attribute. A solution would be to use defines (e.g. GTKA_LABEL instead of "label"). - formatting of the calling tree has to be done by manually. In case the structure changes, e.g. inserting another VBox somewhere, other lines might have to be indented accordingly. And the pros: + reads much better (in my opinion) + source code looks more compact + it is possible to use attributes that are not present in all versions of GTK. E.g. "xalign" for GtkEntry was introduced in 2.4, thus by checking the version of GTK at runtime, one can use or not use this attribute: if (gtk_minor_version >= 4) { gtk_widget_set(entry, "xalign", 0.5); // center text } I believe that this programming style would make GTK more attractive for developers as it looks very simple and straight forward, without the need for various casts (GTK_WHATEVER(widget)) as in the "traditional" version. However, the current implementation does not support it really. For example if one also wants to specify packing attributes (e.g. make the label expandable but not the button box) then this is not currently possible. It could be done by adding new attributes to the Box widget API though (actually I did this by writing my own GtkGroup class). Well, just some thoughts. I have coded a major application using this technique and simply wanted to share my experience with you. Best, Steffen PS: if this sounds familiar to you, I first saw this idea proposed in the MUI programming toolkit in the early 90's. SunOS's OpenWindows also had a similar approach). __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list