Re: setting default choice for popup menu?



Peter Lees wrote:

hello,

i have created a popup menu with a list of choices, but i
want to be able to set the default item (ie the item that is shown
when the widget is drawn) to somethign other than the first item
in the list.

how can i achieve this?

assuming you're using an OptionMenu as the popup menu parent, the set_history method does this.

e.g.

   $optionmenu->set_history ( 3 );

annoyingly enough, that's an index, not a string or a value.
i typically use this kind of construct:


   @items = (
       # name, value
       [ 'first', 'who' ],
       [ 'second', 'what' ],
       [ 'third', 'i dunno' ],
   );

   $guy = 'what';    # initial value

   $optionmenu = new Gtk::OptionMenu;
   $menu = new Gtk::Menu;
   $which = undef;
   $n  = 0;
   foreach $i ( @items ) {
       $menuitem = new Gtk::MenuItem $i[0];
       $menuitem->signal_connect ( activate => sub {
                       my ($widget, $item) = @_;
                       $guy = $item->[1];
                       return 1;
                       }, $i );
       $menuitem->show;
       $menu->append ( $menuitem );
       # does this one match the initial value?
       $which = $n if $guy eq $i[1];
       $n++;
   }
   $menu->show;
   $optionmenu->set_menu ( $menu );
   # set the initial value
   $optionmenu->set_history ( $which )
       if defined $which;
   $optionmenu->show;





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