Re: gtk_object_set_data



On Fri, 29 Sep 2000 19:35:56 Matt Eisemann wrote:
> Currently working on a project and using GTK+ and am aware of the 
> limitation in callbacks where gtk_sgnal_connect can only pass a single
> data 
> value.
> In my application I am using an array of records called TASK.  I need a
> way 
> to send more than just my GtkWidget to my callback when the user pushes a
> 
> pushbutton.
> 
> Below is the gtk_signal_connect I need to be used once a user pushes a 
> button, it will call the start tasks routine.  The display_tasks is a
> CList 
> widget and contains items
> in a list.  However, I need to perform actual operations on the items 
> selected and their values are stored in the TASK structure.
> 
> One possible solution that I could not get to work was using 
> gtk_object_set_data and gtk_object_get_data.   Can you use these
> functions 
> by passing an array of records like 'task_list' below.  I tried but when
> I 
> tried to print the value of a field in callback routine I would get
> garbage 
> in my print statement.  Please understand that pointers is not my strong 
> suit so maybe I did something wrong.  Below is some partial gtk stuff
> that 
> should show what i am trying to do.  Thanks ahead of time for your help.
> 
> typedef struct
> {
> .... ...
> .......
> } TASK
> 
> in main()
> {
>   TASK task_list[MAXIMUM_TASKS];
>   GtkWidget *display_tasks = NULL:
> 
> 
>   gtk_signal_connect(GTK_OBJECT(start_task),"clicked",
>                                start_tasks,GTK_CLIST(display_tasks));
> 
>   gtk_object_set_data(GTK_OBJECT(display_tasks),"task_list",&task_list);
> .....
> ....
> ............
> 
> }
> 
> void start_tasks(GtkWidget *widget, gpointer display_tasks)
> {
>   TASK *selected_tasks[MAXIMUM_TASKS];
> 
>   selected_tasks[MAXIMUM_TASKS] = 
> gtk_object_get_data(GTK_OBJECT(display_tasks),"task_list");
>   printf(" %s \n",selected_tasks[1]->task_name);
> 
> }
> 
> 
> 
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
> 

shouldn't it be:

selected_tasks = gtk_object_get_data(....);

You're setting a pointer to an array as a data. In the callback you store
this pointer in the last+1 (Array bounds passed) element of the array, not
in the pointer as you intend to do. Also, task_list only exists within the
scope of the function main After leaving that (ok that doesn't happen when
you call gtk_main() within that functions) task_list won't exist anymore.
In fact I think what you would have to do is:

int
main(void)
{
  TASK *task_list;

  task_list = g_new(TASK,MAXIMUM_TASKS);
  gtk_object_set_data(GTK_OBJECT(display_tasks),"task_list",task_list);
  ....
}

callback
{
  TASK *task_list;

  task_list = gtk_object_get_data(...);
}


Don't forget to free task_list when you don't need it anymore, or you'll
have a memory leak.

Jeroen





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