Re: Option menus



Jered Bolton wrote:
> 
> How do you connect callbacks to the individual items (or widgets) inside
> on option menu, after it has been created? (In otherwords I'm using
> glade and it provides no means of connecting to the "activate" signal.)
> I've been trawling through a few archived lists and get the general
> impression that I can get at the gtk_menu_items because they are the
> children of Gtk_Menu, but how?

The Glade FAQ helps a little:

4.7 How do I get the value of a GtkOptionMenu?

Call gtk_menu_get_active() with the GtkOptionMenu's menu to get the
currently
selected menu item. You can use g_list_index() to find its index in the
menu:

void
on_button1_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
  GtkWidget *option_menu, *menu, *active_item;
  gint active_index;

  option_menu = lookup_widget (GTK_WIDGET (button), "optionmenu1");
  menu = GTK_OPTION_MENU (option_menu)->menu;
  active_item = gtk_menu_get_active (GTK_MENU (menu));
  active_index = g_list_index (GTK_MENU_SHELL (menu)->children,
active_item);

  g_print ("Active index: %i\n", active_index);
}


4.8 How do I get a GtkOptionMenu to call a function when it changes?

Glade doesn't support this at present, but you can set it up manually.

When you create the window, get the option menu and connect to the
"deactivate"
signal emitted by its menu:

  window1 = create_window1 ();
  option_menu = lookup_widget (window1, "optionmenu1");
  gtk_signal_connect (GTK_OBJECT (GTK_OPTION_MENU (option_menu)->menu),
                      "deactivate", GTK_SIGNAL_FUNC
(on_option_selected),
                      NULL);


Then add a handler to callbacks.c. You can get the index of the selected
item
just like the previous answer:

static void
on_option_selected (GtkMenuShell *menu_shell,
                    gpointer data)
{
  GtkWidget *active_item;
  gint item_index;
  
  active_item = gtk_menu_get_active (GTK_MENU (menu_shell));
  item_index = g_list_index (menu_shell->children, active_item);

  g_print ("In on_option_selected active: %i\n", item_index);
}


If you really want to connect an "activate" signal to each item you
could use lookup_widget() to get the option menu, then walk through
the menu items connecting the signals.

Damon




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