Re: TODO list item: menu selection

David Santiago <> writes:

> Hello folks. I saw on the GTK+ todo list that one of the items was making
> menus not select the first item when you click on them. It said to inquire
> here. 
> Someone posted a patch to the list to do this, but it was pointed out that
> it didn't handle the case when the user opened the menu with a keyboard
> shortcut, in which case you'd want the first item to be selected. When I
> checked (a few minutes ago), this patch had not been applied. 
> So, I went ahead and started working on this problem, since it has bugged
> me for a long time. I have included a possible patch for this. I've tested
> this out quite a bit, and it works pretty well as far as I can tell. It
> also handles the cases when the user uses the mouse and the keyboard
> differently (as was described earlier). 
> If this isn't up to snuff or whatever, just let me know. I'm interested in
> working on this problem, if no one else is. 
> Thanks, 
>    David Santiago :) 

Jonathan Blandford <> has been handling some of the
other TODO items about menus so this change should be coordinated with

To comment on your patch; when the comment says "this is a bit of a
hack", then I'd generally like to avoid adding more hacks in the same
place. And I think that storing a "select_item" flag on a menuitem is
definitely a hack.

The way I would look at it, what we want to do is select the first
child _only_ if the selection was done from a keyboard accelerator.

Ideally, this should apply:

  - If the user pops up a menu as "Alt-F"
  - If the user selects a submenu by shortcut accelerator

Windows adds one more situation to this, and selects the first item
when the user hits return when sitting on a the parent menu item of a
submenu. But since GTK+ automatically pops up the submenu immediately
when you cursor into the parent menu item, this probably doesn't
matter for us.

So, considering this, it is a very close approximation if the first
menu item is selected only when the popup occurs as a result of
gtk_real_menu_item_activate_item() and not otherwise.

Now the problem, right now, with making this do something different is
popping up the submenu is routed through the ->select signal, and
someone could, in theory override that in a subclass. My feeling is
that the menu code is hairy and convoluted enough that anybody that
does that is just asking for trouble anyways.

So, I think the right thing to do is to rename
gtk_menu_item_select_timeout_unlocked() to gtk_menu_item_popup_submenu(), 
call that from gtk_menu_item_select_timeout (), and _directly_
from gtk_real_menu_item_activate_item(), and then move the
code to select the first child from that function into
gtk_real_menu_item_activate_item(), where it can be 
unconditional - you don't need the existing check either.

I think that should simplify the code and get the desired behavior.


> @@ -658,13 +660,19 @@
>        /* This is a bit of a hack - we want to select the first item
>         * of menus hanging of a menu bar, but not for cascading submenus
> +       * and not when we are told otherwise (ie, the menu was selected
> +       * with the mouse) 
>         */
> -      if (GTK_IS_MENU_BAR (GTK_WIDGET (menu_item)->parent))
> +      if ((GTK_IS_MENU_BAR (GTK_WIDGET (menu_item)->parent))
> +	&& (menu_item->select_first_submenu_item))
>  	{
> -	  GtkMenuShell *submenu = GTK_MENU_SHELL (menu_item->submenu);
> +	  GtkMenuShell *submenu = GTK_MENU_SHELL (menu_item->submenu); 
>  	  if (submenu->children)
>  	    gtk_menu_shell_select_item (submenu, submenu->children->data);
>  	}
> +      /* Here we reset this flag to true, so we must be told again 
> +	 explicitly to select the first menu item. */ 
> +      menu_item->select_first_submenu_item = TRUE; 
>      }
>  }
> Index: gtk/gtkmenuitem.h
> ===================================================================
> RCS file: /cvs/gnome/gtk+/gtk/gtkmenuitem.h,v
> retrieving revision 1.10
> diff -u -r1.10 gtkmenuitem.h
> --- gtk/gtkmenuitem.h	2000/02/13 08:16:47	1.10
> +++ gtk/gtkmenuitem.h	2000/04/07 09:50:52
> @@ -64,6 +64,10 @@
>    guint submenu_direction : 1;
>    guint right_justify: 1;
>    guint timer;
> +  /* If the following flag is true and the menuitem is in a
> +     menubar, the first menu item in the submenu will be 
> +     selected when this menu item is selected. */ 
> +  guint select_first_submenu_item: 1; 
>  };

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