Re: [gtk-list] Getting text of a menu item from a GtkOptionMenu




On Thu, 8 Jul 1999, Damien Miller wrote:
> 
> I have a GtkOptionMenu from which I would like to get the text of the 
> currently selected option.
> 
> How do I do this?
> 

Is there some other toolkit where this is the standard thing to do? People
keep asking to do it, but I don't think it's that intuitive, plus it's a
bad idea. :-)

This is probably a poor way to structure your program; it will break when
you internationalize, when you add pixmaps to your menu items, or when you
change the name of a menu item. So, I would recommend doing it another
way.

The simplest way, which I often use, is to gtk_object_set_data() some
unique identifier on each menu item; it might be the data structure in
your program the option menu is associated with, or an enumeration value
(use GPOINTER_TO_INT() and GINT_TO_POINTER() to stuff an enum value into
the gpointer you store with set_data()). Then one normally connects to the
"activate" signal of each menu item of the option menu, and the callback
checks the data stored on the menu item to determine which item it is.

If you want to get the name, you have to do some sick hack like:

void
activate_callback(GtkWidget* menuitem, gpointer data)
{
  GList* children;
  gchar* text;

  children = gtk_container_children(menuitem);

  g_assert(children != NULL);
  g_assert(GTK_IS_LABEL(children->data));

  gtk_label_get(GTK_LABEL(children->data), &text);

  g_list_free(children);
}

Havoc




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