Re: GtkCombo items?



The GtkCombo structure contains a member named "list", which is a GtkList widget pointer (in fact, it is pointer to GtkWidget, but you can get the list using GtkList *ls = GTK_LIST(combo->list)).

This is basically a GtkListItem container. So we study the GtkList structure: it contains a member named "children", of type GList* (this is glib's double linked list). This member is a list of GtkListItem*. The structure of this one could appear a little complicated. Inheritance looks like: GtkListItem is derived from GtkItem, which in turn is derived from GtkBin, which contains a member named "child", of type GtkWidget*. This approach makes GtkList very flexible, in terms that it may contain any kind of widgets (although they are usually GtkLabel-s). Hope my explanations are not very hard to follow, code below should clear the mess...

In conclusion, if your list contains only GtkLabel items (I mean, string values), you can get the string list by using the following code (or something similar):

GtkCombo *the_combo;
// ...
GtkList *the_list = the_combo->list;
GList *list_items = the_list->children;
GList *p = list_items;
GList *strings = NULL;
GList *si;

while (p != NULL) {
 GtkListItem *item = GTK_LIST_ITEM(p->data);
 GtkLabel *label = GTK_LABEL(GTK_BIN(item)->child);
 char *text = NULL;

 gtk_label_get(label, &text);

 if (text != NULL) {
   int len = strlen(text) + 1;
   char *new_text = malloc(len);
   memcpy(new_text, text, len); // do we have to do this way?
   if (strings == NULL) {
     strings = g_list_append(strings, text);
     si = strings;
   } else {
     si = g_list_append(si, text);
   }
 }
p = g_list_next(p);
}

// hopefully, here you have all the strings in the "strings" list.

si = strings;
while (si != NULL) {
 const char *text = (const char*)(si->data);
 fprintf(stderr, "%s\n", text);
 si = g_list_next(si);
}

// free the allocated memory

g_list_foreach(strings, free, NULL);

Disclaimer: I did not test the above code. I did not read any documents, except the header files, in order to give you this reply. So I think I'm right, but I cannot guarantee it. Sorry if anything goes wrong. Also, please excuse me the C++-like style (variables should be declared at the beginning of block, for ANSI C :)

Cheers,
Mishoo

Janus N. Tøndering wrote:

Dear Sirs,

I've looked at the header files and the references found at
developer.gnome.org but I cannot figure out how to get the list of
strings in a standard GtkCombo. Could someone point me in the right
direction?

Thank you.

--
Janus N. Tøndering
"Trying is the first step to failure" -- Homer Simpson

Or to success... :)   I rather think positive..




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


--

 ... and on the seventh day, He exited from append mode.







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