Re: [gtk-list] getting the optionmenu text in gtk--




On Sun, 27 Sep 1998, Marsel Osipov wrote:
> Hello. 
>     I have a Gtk_OptionMenu widget, and I was wondering how can I get
> the text of the selected item?
> 

You could extract the text from the label in the item, but it would be
suboptimal because you'd be doing a lot of casts that weren't guaranteed
to work (for example, if you later decided to add a pixmap to the menu
item, or the GtkLabel struct changed, your code would break). 

The best way is probably to do 

/* pointer_to_data is a pointer to your string */
gtk_object_set_data(GTK_OBJECT(menuitem), "my_data_key",
                    pointer_to_data);

then:

gchar* text_of_item = (gchar*)gtk_object_get_data(GTK_OBJECT(menuitem),
                                                  "my_data_key");

if (text_of_item == NULL) g_error("Bug! no data");
else {
 /* Use text */
}

If you aren't setting any other data you can use the
gtk_object_[get|set]_user_data shortcut functions, they let you set a
single item of data without having to type a key.

Havoc




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