Re: How can I get index when combox changed



On Tue, 2001-08-28 at 17:01, zxf3436 wrote:
> How can I get index of text in entry when combox changed
> 
> example:
> 
>  one combox item has three string:"aaa","bbb","ccc",I can get text in entry ("bbb") when combox changed,but I can't get number 2,any idea?

I've attached a program that does what you're asking. It's not pretty,
but then, I don't think there *is* a pretty way to do this...as far as I
can see you need to access the list inside the combo to get this.

Anyway, the program shows two things:

        1. How you look up the index of the entry string (which I think
           is what you were asking for), and
        2. how you can keep track of list-selections in the combo, since
           that has been discussed resently on the list(s).

The select_callback function does the stuff from 2., while the
text_changed does the stuff from 1.

HTH
	/mailund

-- 
I used up all my sick days, so I'm calling in dead.
#include <string.h>
#include <gtk/gtk.h>

static void
select_callback (GtkList *list, GtkWidget *item)
{
    gint index = gtk_list_child_position (list, item);
    g_print ("selected %d\n", index);
}

static void
text_changed (GtkEntry *entry, GtkList *list)
{
    int count;
    GList *popdown;
    const char *text = gtk_entry_get_text (entry);

    g_print ("entry text is \"%s\"\n", text);

    for (count = 0, popdown = list->children; 
	 popdown;
	 count++, popdown = popdown->next)
	{
	    /* WARNING: this only works if you *know* that the item
	     * is a label.  You really *should* test for it! */
	    GtkLabel *label = GTK_LABEL (GTK_BIN (popdown->data)->child);
	    char *label_string;
	    gtk_label_get (label, &label_string);
	    if (strcmp (text, label_string) == 0)
		{
		    g_print ("text \"%s\" found at index %d\n",
			     text, count);
		    return;
		}
	}
    g_print ("text \"%s\" not found in list\n", text);
}

int
main (int argc, char *argv[])
{
    GtkWidget *win;
    GtkWidget *combo;
    GList *popdown_list;

    gtk_init (&argc, &argv);

    win = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    // create popdown items
    popdown_list = NULL;
    popdown_list = g_list_prepend (popdown_list, "three");
    popdown_list = g_list_prepend (popdown_list, "two");
    popdown_list = g_list_prepend (popdown_list, "one");

    combo = gtk_combo_new ();
    gtk_combo_set_popdown_strings (GTK_COMBO (combo),
				   popdown_list);
    gtk_container_add (GTK_CONTAINER (win), combo);

    gtk_signal_connect (GTK_OBJECT (GTK_COMBO(combo)->list),
			"select-child",
			GTK_SIGNAL_FUNC (select_callback),
			NULL);
    gtk_signal_connect (GTK_OBJECT (GTK_COMBO(combo)->entry),
			"changed",
			GTK_SIGNAL_FUNC (text_changed),
			GTK_COMBO(combo)->list);

    gtk_widget_show_all (win);

    gtk_main ();

    return 0;
}


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