Re: Option menus in GTK+




Matthew D Allen <s2mdalle@titan.vcu.edu> writes:

> About option menus - I can connect a signal to each menu item inside the
> optionmenu so that when it gets "activate" I can call a function of my
> choice.  the problem is that in this function, I also need to know what
> the state of the option menu was before it was changed.  Is there a way to
> do this?
> 
> What I"m trying to do is this:  I've got an entry widget, and an option
> menu next to it.  The entry widget can hold 4 different things, depending
> on what the option menu is set to.  So what I need to do is when a new
> option menu setting is chosen, I need to save whatever was in the entry
> widget, and then clear it out for input on the next item corresponding to
> the option menu item that as chosen.  The saving is easy, and the clearing
> is easy, but when I move from one option menu item to the next, and I grab
> the text out of the box, I have to know what option menu setting that
> corresponded to before the change.  Is there a way to do that?  Basically,
> find out whatever the setting on the option menu was BEFORE the new
> setting?
> 
> I know that's a convoluted question but it's as good as I can describe it.
> Any suggestions?

Well, sometimes connecting to "activate" on each menu item in the
option menu is the easy way to do it:

 void set_foo_option (GtkWidget *widget, gpointer value) 
  {
    foo_option = GPOINTER_TO_UINT (value);
  }

 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
                     GTK_SIGNAL_FUNC (set_foo_option),
                     GUINT_TO_POINTER (my_enum_value));

Alternatively, you could use the following function, I wrote
(with the comment) some months ago:

/* This really should be in GTK+
 */
static gint
option_menu_get_history (GtkOptionMenu *option_menu)
{
	GtkWidget *active_widget;
	
	g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), -1);
	
	active_widget = gtk_menu_get_active (GTK_MENU (option_menu->menu));

	if (active_widget)
		return g_list_index (GTK_MENU_SHELL (option_menu->menu)->children,
				     active_widget);
	else
		return -1;
}

And have now (as of five minutes ago) added it into the GTK+-1.3,
(named gtk_option_menu_get_history()) so it will be there
for GTK+-1.4.

Regards,
                                        Owen



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