Re: Custom renderer for GtkCombo



Diego Zuccato wrote:

Since the user shouldn't edit the entry contents, it's better to use a
GtkOptionMenu (w/ Gtk2).
Then you could connect a callback to every item, or simply
gtk_option_menu_get_history() afterwards to read the number of the
currently selected item.


I'd actaully got half way to a solution before reading your message. I
realised on closer inspection that gtk_list_set_item_string() should
read gtk_combo_set_item_string(). So I built my own GtkList and called
g_object_set_data() on each list item. When I processed the users input,
I was cycling through the entire list trying to match the string in the
GtkCombo's GtkEntry, then calling g_object_set_data() on the correct
list item.

Thanks to your advice I switched to a GtkOptionMenu, and it all became
so much simpler:

static GtkWidget *
create_device_option_menu(void)
{
    int fd, nmidi;
    char *name, *device;
    struct synth_info info;
    GtkWidget *option_menu, *menu, *item;
    ...

    option_menu = gtk_option_menu_new();

    /* determine number of MIDI devices */
    ...

    menu = gtk_menu_new();

    for (info.device = 0; info.device < nmidi; info.device++) {
        ioctl(fd, SEQUENCER_INFO, &info);

        asprintf(&name, "%d: %s", info.device, info.name);
        asprintf(&device, "/dev/rmidi%d", info.device);

        item = gtk_menu_item_new_with_label(name);
        g_object_set_data_full(G_OBJECT(item), "device", device, free);
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);

        free(name);
    }

    gtk_option_menu_set_menu(GTK_OPTION_MENU(option_menu), menu);

    /* cleanup MIDI device stuff */
    ...

    return option_menu;
}

Then when I process the users input:

static void
note_on_cb(GtkWidget *widget, gpointer data)
{
    DataWidgets *widgets = data;
    GtkWidget *menu, *item;
    char *device;

    menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(widgets->device));
    item = gtk_menu_get_active(GTK_MENU(menu));
    device = (char *)g_object_get_data(G_OBJECT(item), "device");

    ...
}

Chris
--
chris wareham iosystems co uk (work)
chris wareham btopenworld com (home)




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