Re: Getting value from an option menu



Thanks for the info. Unfortunately, I have not been able to 
get this working. Maybe you can quickly see what I'm doing wrong?

 option_menu=glade_xml_get_widget(xml, "optionmenu1");
 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(option_menu));
 menu_item_active = gtk_menu_get_active(GTK_MENU(menu));
 menu_item = (GtkWidget *) gtk_object_get_data (GTK_OBJECT
(main_window), "Red");
 assert(menu_item != NULL);  /* ASSERTION FAILS */


I'm not sure if I'm passing the right parameters to 
gtk_object_get_data. I've also tried using all the widgets above for
the first parameter (option_menu, menu, menu_item_activate) in
addition to main_window, which is a pointer to the GtkWidget for
the window the widget is on. It always returns a NULL value
and my assertion fails.

I'm assuming that the second (key) parameter is the value of the 
menu item that I have displayed?

When you create the red menu_item, you should have this:

gtk_object_set_data (GTK_OBJECT (main_window), "Red", menu_item);

The "Red" string must be exactly the same in set/get instructions.
If you write "red" it will compile fine but you will get a nice 
core dump when you run it. :-) I post an almost complete example below.

Carlos

When you create the option menu, you have something like this:
--------------------------
option_menu = gtk_option_menu_new ();
gtk_widget_set_usize (GTK_WIDGET (option_menu), 80, 25);
gtk_box_pack_end (GTK_BOX (hbox1), option_menu, FALSE, FALSE, 3);
gtk_object_set_data (GTK_OBJECT (dialog), "option_menu_fruit", option_menu);
menu = gtk_menu_new ();

menu_item = gtk_radio_menu_item_new_with_label (NULL, "Orange");
gtk_menu_append (GTK_MENU (menu), menu_item);
gtk_object_set_data (GTK_OBJECT (dialog), "menu_item_orange", menu_item);
gtk_widget_show (menu_item);

etc...
--------------------------
To retrieve the information:
---------------------------
GtkWidget *option_menu, *menu, *menu_item, *menu_item_active;

option_menu = (GtkWidget *) gtk_object_get_data (GTK_OBJECT (dialog), "option_menu_fruit");
menu = gtk_option_menu_get_menu (GTK_OPTION_MENU (option_menu));
menu_item_active = gtk_menu_get_active (GTK_MENU (menu));

menu_item = (GtkWidget *) gtk_object_get_data (GTK_OBJECT (dialog), "menu_item_orange");
if (menu_item == menu_item_active) do_orange ();
menu_item = (GtkWidget *) gtk_object_get_data (GTK_OBJECT (dialog), "menu_item_banana");
if (menu_item == menu_item_active) do_banana ();
etc...
------------------------------------

I strongly suggest you to be very clear with the names you use.
Don't be affraid to use names a little too long, as I did here
with "menu_item_orange", etc... you are not writting a kernel 
driver after all... :-)

Try to use the same name convention throughout 
the whole application, I know it's hard, but it
pays off...

Carlos




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