Re: Pass 2 or more widgets in callback



I have a doubt. I have a program, that listens on port 3000. Then, i
defined an idle function, wich accepts incoming connections and binds
the incoming connection to a IO callback (gdk_input_add).
But i have to remove the callback when the connection is finished, so I
did a
g_malloc(sizeof int), and passed the tag return by gdk_input_add to the
callback this way:


*tag = gdk_input_add(read_socket, GDK_INPUT_READ,
GTK_SIGNAL_FUNC(ler_socket), tag);

Then, inside the ler_socket function, i read the socket, and, if the
connect was ended, i did a gdk_input_remove(*tag) and a g_free(tag), so
that i dint have any leaks.
It worked ok (at least it seemed). But i was printing the data to the
console. Now, i want to set the text of a gtk_label with the data. I
cant set the tag as a _set_data, becouse there will be many diferent
tags (each connection) to only 1 widget.
So, i think the best would be to pass an array with tag and label. But i
just can't get it to work.
Does anyone have a tip, a code sample of how to do this?

1) If you want to preserve only the information regarding the LAST 
connection, then you might consider:

free g_lib memory from previous connection with:
gtk_object_remove_data
set the new information with:
gtk_object_set_data
then just get it with gtk_object_get_data
this way you have the current connection information updated
and you don't have mem leaks (check this part...)

2) If you want to preserve the information regarding ALL
connections done so far, then you might consider two cases:

2a) If you know in advance that there is a limit for the
number of connections you might consider an array, and
then pass the address of the array again with the
set_data/get_data mechanism. This is not very
elegant but might be faster than the second approach.
You can use realloc but complexity will increase.

2b) Use dynamic slists or d_dlists, provided by glib
or by yourself. This is way more elegant and flexible
than arrays but if the number of connections is large,
it might be slow... Then again you can pass the head or tail
of your list with get_data/set_data.

These are just suggestions, I don't know enough
of your case to be more specific.

Carlos




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