Re: Pass more widgets as gpointer.



As I've said in he past, I believe the best thing is to sub-class GtkWindow
and then make all the pointers you want to pass around members of your new
class. Conceptually this looks like:

    class MyWindow : GtkWindow {
            GtkWidget *w_label;
            GtkWidget *w_button;
            GtkWidget *w_tooltip;
    }

    :
    MyWindow *w_window = my_window_new();
    g_signal_connect(w_button, "clicked", G_CALLBACK(cb_button), w_window);
    :
    int cb_button(GtkWidget *widget,
                  GtkButtonEvent *event,
                  gpointer userdata)
       {
            MyWindow *self = (MyWindow*)self;

           gtk_tool_tip...(self->w_tooltip, ...);
           gtk_label_set_text(self->w_label, "foo");
       }


I hope you get the idea. The problem with this approach though is that it is
a bit of an effort quite to subclass Gtk in C, (in contrast to gtkmm, perl,
python, javascript in which it has been nicely mapped to the native objects,
not to talk about vala where a GObject is native). That's why I'm using gob2
whenever I'm writing C. It makes this technique as natural as in any other
language that I mentioned.

Regards,
Dov

2009/4/22 Vlad Volodin <vest 84 gmail com>

Hi, Jens

Yes, your way is good (thank you for a small code, maybe I will use it
in my projects). But, are you sure your way is safe?
You don't know the size of array (at the first),
where do you free the array of pointers itself (try g_malloc and
g_free, because it is GLIB :) ) (at the second).
And I think, your way isn't type-safe (at the third), because you
won't be sure that your element's type is needed.
Well, of course I have many questions and now answers :) I think, in
small projects your way is good (but you can also easily forget the
order of the elements). Maybe HashTables, or STL sets will be more
applicable.

What do you think about all of this?

Good luck in GTK programming :),
Vlad Volodin

2009/4/22 Jens Hansen <jensh604 gmail com>:
Hi All.

Thanks for all ideas, to this problem. I figured out how to pass more
widgets in an array, which is IMHO the best way to do it. Just for
reference, the following snippet.

"Packing" the widgets pointers in the array
 GtkWidget * *data;
 data = malloc(sizeof(GtkWidget *)*3);
 data[0]=spinbutton;
 data[1]=combobox;
 data[2]=check_hidden
And "unpacking" them
GtkWidget * *array=(GtkWidget **)data;
Then you just need to type cast the elements of the array:
for instance
 gtk_combo_box_get_active((GtkComboBox *)array[1])
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]