TODO list item: menu selection



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 :) 



Index: gtk/gtkmenuitem.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkmenuitem.c,v
retrieving revision 1.37
diff -u -r1.37 gtkmenuitem.c
--- gtk/gtkmenuitem.c	1999/11/22 21:52:47	1.37
+++ gtk/gtkmenuitem.c	2000/04/07 09:50:52
@@ -176,6 +176,8 @@
   menu_item->right_justify = FALSE;
 
   menu_item->timer = 0;
+  
+  menu_item->select_first_submenu_item = TRUE; 
 }
 
 GtkWidget*
@@ -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; 
 };
 
 struct _GtkMenuItemClass
Index: gtk/gtkmenushell.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkmenushell.c,v
retrieving revision 1.30
diff -u -r1.30 gtkmenushell.c
--- gtk/gtkmenushell.c	2000/03/14 19:57:24	1.30
+++ gtk/gtkmenushell.c	2000/04/07 09:50:52
@@ -431,6 +431,8 @@
 	{
 	  if ((menu_item->parent == widget) &&
 	      (menu_item != menu_shell->active_menu_item))
+	    /* First we set it to not select the first item. */ 
+	    GTK_MENU_ITEM (menu_item)->select_first_submenu_item = FALSE; 
 	    gtk_menu_shell_select_item (menu_shell, menu_item);
 	}
     }
@@ -581,6 +583,8 @@
 	  if ((event->detail != GDK_NOTIFY_INFERIOR) &&
 	      (GTK_WIDGET_STATE (menu_item) != GTK_STATE_PRELIGHT))
 	    {
+	      /* First we set it to not select the first item. */ 
+	      GTK_MENU_ITEM (menu_item)->select_first_submenu_item = FALSE; 
 	      gtk_menu_shell_select_item (menu_shell, menu_item);
 	    }
 	}


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