[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: widget array
- From: adlaib <adlaib lucent com>
- To: gtk-app-devel-list redhat com
- Subject: Re: widget array
- Date: Wed, 20 Oct 1999 10:16:31 -0500
> fine but the compiler warns about incompatible pointer type for the gtk_label_set().
instead of:
> gtk_label_set(*((GtkWidget **)widgetPtr), "changed 1st label");
u need to do:
gtk_label_set(GTK_LABEL(*((GtkWidget **)widgetPtr)), "changed 1st label");
to make gtk think it's a label widget ptr (which it actually is - cause u have
it pointing
to a label)
however, there are more pressing matters to attend to - u have
> *((GtkWidget **)widgetPtr +1)
this is a core dump waiting to happen. on 32 bit machines pointers are 4 bytes
so
it should be + 4; it is much easier (and safer/portable) to do +
sizeof(widgetPtr) - which
should give u the pointer size in bytes.
there were comments about using an array[], if you are doing this from main
(i assumed u were not) or if the array were global then the array[] would work.
alternatively, u could define a struct with an array element and malloc the
struct and pass a ptr to the struct around if u dont like messing with the
pointers
there were also comments about using structures only. it's your code and your
choice. i
will say that some designs lend themselves to arrays rather than structures,
ie:
a window with N labels that you want to do the same kind of processing with -
seems easier
to me to stuff them in an array and do your processing in a loop rather than
using a struct,
- if N is large and u r using a struct, your fingers are gonna bleed from all
that typing...
ksteen@earthlink.net wrote:
>
> adlaib wrote:
>
> > oh yeah.
> >
> > just cast the array
> > ie:
> > ---------------------------------------------------------------------------------------
> > int i;
> > gpointer widgetPtr;
> > gpointer tempWidgetPtr;
> >
> > widgetPtr = (gpointer)malloc(sizeof(widgetPtr)*NUM_WIDGETS);
> >
> > //use tempWidgetPtr to traverse the array,
> > tempWidgetPtr = widgetPtr;
> >
> > // i guess u can write a macro for the typecast
> > for (i=0; i<NUM_WIDGETS; i++) {
> > *((GtkWidget **)tempWidgetPtr) = gtk_CREATE_SOME_KIND_OF_WIDGET();
> > DO_SOME_WIDGET_PROCESSING(*((GtkWidget **)tempWidgetPtr));
> > tempWidgetPtr=+ sizeof(tempWidgetPtr);
> > }
> >
> > //u don't need a cast here cause widgetPtr is already a gpointer.
> > gtk_signal_connect(GTK_OBJECT(THE_WIDGET_THAT_GENERATES_THE_SINGAL),
> > "THE_SIGNAL",
> > GTK_SIGNAL_FUNC(THE_SIGNAL_HANDLER), widgetPtr);
> > ---------------------------------------------------------------------------------------
> >
> > THE_SIGNAL_HANDLER() - should destroy all NUM_WIDGETS (depending on what u r
> > doing) and
> > free widgetPtr - if nobody else needs it or else youre gonna have some nasty
> > memory leaks.
> >
> > what i do is define a stucture of several ptrs, malloc the stuct - and pass the
> > structPtr to the signal handler - that way i can do many arrays of widgets at
> > the
> > same time.
> >
> > widgetArray[NUM_WIDGETS] willl NOT work... (unless u declare it static - yuck)
> >
> > hope that helps...
> >
> >
>
> Thanks. That works. When I tryed It in the simple program below The program runs
> Can it be written so that it will compile cleanly?
>
> #include<stdlib.h>
> #include <gtk/gtk.h>
>
> void button_click(GtkWidget *widget, gpointer data);
> #define NUM_WIDGETS 2
>
> int main (int argc, char *argv[])
> {
> GtkWidget *window;
> GtkWidget *vbox;
> GtkWidget *button;
> gpointer widgetPtr;
>
> gtk_init (&argc, &argv);
>
> widgetPtr = (gpointer)malloc(sizeof(widgetPtr)*NUM_WIDGETS);
> window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
> gtk_widget_show (window);
> gtk_signal_connect(GTK_OBJECT(window), "delete_event",
> GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
>
> vbox = gtk_vbox_new(FALSE, 0);
> gtk_widget_show(vbox);
> gtk_container_add(GTK_CONTAINER(window), vbox);
>
> *((GtkWidget **)widgetPtr) = gtk_label_new("Initial label");
> gtk_widget_show(*((GtkWidget **)widgetPtr));
> gtk_box_pack_start(GTK_BOX(vbox), *((GtkWidget **)widgetPtr), TRUE, TRUE, 0);
>
> *((GtkWidget **)widgetPtr +1) = gtk_label_new("second label");
> gtk_widget_show(*((GtkWidget **)widgetPtr +1));
> gtk_box_pack_start(GTK_BOX(vbox), *((GtkWidget **)widgetPtr +1), TRUE, TRUE, 0);
>
> button = gtk_button_new_with_label("Change Label");
> gtk_widget_show(button);
> gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
> gtk_signal_connect(GTK_OBJECT(button), "clicked",
> GTK_SIGNAL_FUNC(button_click), widgetPtr);
>
> gtk_main ();
> return 0;
> }
>
> void button_click(GtkWidget *widget, gpointer widgetPtr){
>
> gtk_label_set(*((GtkWidget **)widgetPtr), "changed 1st label");
> // Compiler warning about incompatible pointer types for arg 1
> gtk_label_set(*((GtkWidget **)widgetPtr +1), "New second label");
>
> }
>
> --
> To unsubscribe: mail gtk-app-devel-list-request@redhat.com with
> "unsubscribe" as the Subject.
>
> Mailing list concerns should be mailed to <listmaster@redhat.com>
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]