Re: Button in Gtk



Ajax John wrote:

I want to know what is the difference does it make when a Button is
decleared as
GtkWidget *button;
GtkButton *button;

I'm confused which one to used in my program.
In simple words when should I declare my button as Gtkwidget * and as
GtkButton *.

There's no difference; it's merely a matter of convention and/or convenience. Most functions in gtk that you'll use after creating a widget (of any type, not just GtkButton) will take a GtkWidget pointer. Also, most (all?) of the widget _new() functions will return GtkWidget pointers. So you can either do things like this:

GtkWidget *box = /* box created somehow */
GtkButton *button = GTK_WIDGET(gtk_button_new());
gtk_widget_show(GTK_WIDGET(button));
gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(button), FALSE, FALSE, 0);

or you can do things like this:

GtkWidget *box = /* ... */
GtkWidget *button = gtk_button_new();
gtk_widget_show(button);
gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);

As you can see, declaring all your widgets as GtkWidget* rather than their actual type saves you a bit of typing. This way seems to be standard practice.

Of course, there might be situations where you use the widget a lot both as a GtkWidget and as it's actual type, so you might want to just keep around another variable pointing to the same object:

GtkWidget *box = /* ... */
GtkWidget *widget = gtk_button_new();
GtkButton *button = GTK_BUTTON(widget);
gtk_button_set_foo(button, foo);
gtk_button_set_bar(button, bar);
gtk_button_set_baz(button, baz);
gtk_widget_show(widget);
gtk_box_pack_start(GTK_BOX(box), widget, FALSE, FALSE, 0);

(Though I rarely actually do this myself.) It's really up to you; it makes no difference to the actual functioning of the program. Just be sure to use the class-cast macros when needed and you'll be fine.

        -brian



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