Re: GtkCombo items?



Here, as a completation to my answer (which has some small errors) I attached a test program. It seem to work fine :)

--
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


_______________________________________________
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.


#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

static void
on_click(GtkWidget *button, GtkCombo *combo)
{
  GtkList *the_list = GTK_LIST(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 = (char*)malloc(len);
      memcpy(new_text, text, len); // do we have to do this way?
      if (strings == NULL) {
        strings = g_list_append(strings, new_text);
        si = strings;
      } else {
        si = g_list_append(si, new_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, GFunc(free), NULL);
}

int main(int argc, char **argv)
{
  gtk_init(&argc, &argv);

  GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(win), "fucking test");
  gtk_container_set_border_width(GTK_CONTAINER(win), 10);

  GtkWidget *box = gtk_hbox_new(FALSE, 2);
  gtk_widget_show(box);
  gtk_container_add(GTK_CONTAINER(win), box);

  GtkWidget *combo = gtk_combo_new();
  gtk_widget_show(combo);
  gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);

  GList *ls = NULL;
  ls = g_list_append(ls, (gpointer)"gigi");
  ls = g_list_append(ls, (gpointer)"vasile");
  ls = g_list_append(ls, (gpointer)"gheorghe");
  ls = g_list_append(ls, (gpointer)"ion");
  ls = g_list_append(ls, (gpointer)"all romanians");

  gtk_combo_set_popdown_strings(GTK_COMBO(combo), ls);

  GtkWidget *button = gtk_button_new_with_label("  Dump!  ");
  gtk_widget_show(button);
  gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
  gtk_signal_connect(GTK_OBJECT(button), "clicked",
                     GTK_SIGNAL_FUNC(on_click), combo);

  gtk_signal_connect(GTK_OBJECT(win), "destroy",
                     GTK_SIGNAL_FUNC(gtk_main_quit), NULL);

  gtk_widget_show(win);

  gtk_main();
  
  return 0;
}


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