Re: Optionmenuitem and labels



Alexander Nagel a écrit:
Hi,
i build a option menu like in the gtk-docs and it works fine. Now i want to retrieve the label of the selected menuitem.
my code
ConfDatei.NewEntry ("Language", gtk_label_get_text (GTK_LABEL(gtk_bin_get_child(GTK_BIN(gtk_menu_get_active (GTK_MENU(General.LanguageSubMenu)))))));

Theoretically this should work and it should give me the label.
Compiling runs without any warning.
But i get this during execution:

(trollhunter:7444): Gtk-CRITICAL **: file ../../gtk/gtklabel.c: line 1142 (gtk_label_get_text): assertion `GTK_IS_LABEL (label)' failed
Aborted (core dumped)

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

___________________________________________________________________

Hi,

Sorry for the delay, I haven't read the list often recently.

It seems that nobody answered. If you haven't found yet, here is how I do the thing in a callback, connected to each menu item (it's with GTK 1.2.x,I don't know if it works with 2.x):

/**************************************************/
void omenu_callb(GtkWidget *wdg, gpointer data) {
        //      Set the current value from the option menu choice
        gchar *label;
        option_type *opt;
        opt = (option_type *) data;
        if (!GTK_CHECK_MENU_ITEM(wdg)->active) {
                return;
        }
        if (!GTK_IS_LABEL(GTK_BIN(opt->widget)->child))
        // First call, label not available (!), opt->value is already initialized
                return;
        gtk_label_get(GTK_LABEL(GTK_BIN(opt->widget)->child ),&label);
        if (!strcmp(label,_(NO_VALUE)))
                opt->value[0] = '\0';  // Empty string
        else
        // opt->value is already allocated with the max label length
                strcpy(opt->value, label);
}
/**************************************************/

Here is how I create the option menu (first item shown, I loop for the others after - it should be pretty standard):

/**************************************************/
        (...)
        GSList *group = NULL;
        GtkWidget *menu, *menuitem;

        opt->widget = gtk_option_menu_new();
        menu = gtk_menu_new();
        gtk_option_menu_set_menu (GTK_OPTION_MENU(opt->widget),menu);
        gtk_widget_show(opt->widget);
        menuitem = gtk_radio_menu_item_new_with_label (group,NO_VALUE);
        group =
            gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(menuitem));
        gtk_menu_append(GTK_MENU(menu),menuitem);
        gtk_widget_show(menuitem);
        gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
                GTK_SIGNAL_FUNC(omenu_callb), (gpointer) opt);
        (...)
/**************************************************/

Now here is how I set the option menu item from a label - it could give you some hint on how you can "dig" inside the option menu:

/**************************************************/
void set_option_menu_from_label (GtkWidget *omenu, gchar *label) {
        GtkWidget *menu;
        GList *node;
        gchar *txt;
        if (GTK_IS_LABEL(GTK_BIN(omenu)->child)) {
                gtk_label_get(GTK_LABEL(GTK_BIN(omenu)->child), &txt);
                if (!strcmp(txt,label))  // Nothing to do, the label is already set
                        return;
        }
        menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(omenu));
        node = GTK_MENU_SHELL(menu)->children;
        while (node) {
                if (GTK_IS_LABEL(GTK_BIN(node->data)->child)) {
                        gtk_label_get(GTK_LABEL(GTK_BIN(node->data)->child), &txt);
                        if (!strcmp(txt,label))
                                break;
                } // If the child is not a label (typically it would be NULL),
                 // we skip it because the menu item is the current one
                node = node->next;
        }
        if (node)       {       // Label found
                gtk_menu_item_activate(GTK_MENU_ITEM(node->data));
                gtk_option_menu_set_history(GTK_OPTION_MENU(omenu),
                        g_list_index(GTK_MENU_SHELL(menu)->children, node->data));
        }
        else {          // Label not found, revert to default
                gtk_menu_item_activate(GTK_MENU_ITEM(GTK_MENU_SHELL(menu)->children->data));
                gtk_option_menu_set_history(GTK_OPTION_MENU(omenu),0);
        }
}
/**************************************************/

I wouldn't be able to explain all the details right away, I've done it by trial and error some time ago. Hoping this could help, BTW.

Best regards,

Patrice St-Gelais




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