From af7f95207e2f45efd096f218167241653e9151a3 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Tue, 8 Jun 2010 21:45:15 +0200 Subject: [PATCH] menu: Fix wrong comparison causing navigation region to not get removed The navigation region that got created for right-facing submenus when moving the cursor up would have two negative values which caused the check to go wrong. I refactored the code to make that check into a separate function as it makes the check not only harder to get wrong, but also the code easier to read. http://bugzilla.gnome.org/show_bug.cgi?id=620863 --- gtk/gtkmenu.c | 89 ++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/gtk/gtkmenu.c b/gtk/gtkmenu.c index d1ae42225a..e626c26272 100644 --- a/gtk/gtkmenu.c +++ b/gtk/gtkmenu.c @@ -3215,6 +3215,16 @@ definitely_within_item (GtkWidget *widget, check_threshold (widget, 0, h - 1, x, y); } +static gboolean +gtk_menu_has_navigation_triangle (GtkMenu *menu) +{ + GtkMenuPrivate *priv; + + priv = gtk_menu_get_private (menu); + + return priv->navigation_height && priv->navigation_width; +} + static gboolean gtk_menu_motion_notify (GtkWidget *widget, GdkEventMotion *event) @@ -3222,7 +3232,6 @@ gtk_menu_motion_notify (GtkWidget *widget, GtkWidget *menu_item; GtkMenu *menu; GtkMenuShell *menu_shell; - GtkMenuPrivate *priv; gboolean need_enter; @@ -3257,8 +3266,7 @@ gtk_menu_motion_notify (GtkWidget *widget, if (definitely_within_item (menu_item, event->x, event->y)) menu_shell->activate_time = 0; - priv = gtk_menu_get_private (menu); - need_enter = (priv->navigation_width > 0 || priv->navigation_height > 0 || menu_shell->ignore_enter); + need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter); /* Check to see if we are within an active submenu's navigation region */ @@ -4008,46 +4016,47 @@ gtk_menu_navigating_submenu (GtkMenu *menu, gint event_x, gint event_y) { - GtkMenuPrivate *priv = gtk_menu_get_private (menu); + GtkMenuPrivate *priv; + int width, height; - if (priv->navigation_width && priv->navigation_height) + if (!gtk_menu_has_navigation_triangle (menu)) + return FALSE; + + priv = gtk_menu_get_private (menu); + width = priv->navigation_width; + height = priv->navigation_height; + + /* check if x/y are in the triangle spanned by the navigation parameters */ + + /* 1) Move the coordinates so the triangle starts at 0,0 */ + event_x -= priv->navigation_x; + event_y -= priv->navigation_y; + + /* 2) Ensure both legs move along the positive axis */ + if (width < 0) { - int width = priv->navigation_width; - int height = priv->navigation_height; - - /* check if x/y are in the triangle spanned by the navigation parameters */ - - /* 1) Move the coordinates so the triangle starts at 0,0 */ - event_x -= priv->navigation_x; - event_y -= priv->navigation_y; - - /* 2) Ensure both legs move along the positive axis */ - if (width < 0) - { - event_x = -event_x; - width = -width; - } - if (height < 0) - { - event_y = -event_y; - height = -height; - } - - /* 3) Check that the given coordinate is inside the triangle. The formula - * is a transformed form of this formula: x/w + y/h <= 1 - */ - if (event_x >= 0 && event_y >= 0 && - event_x * height + event_y * width <= width * height) - { - return TRUE; - } - else - { - gtk_menu_stop_navigating_submenu (menu); - return FALSE; - } + event_x = -event_x; + width = -width; + } + if (height < 0) + { + event_y = -event_y; + height = -height; + } + + /* 3) Check that the given coordinate is inside the triangle. The formula + * is a transformed form of this formula: x/w + y/h <= 1 + */ + if (event_x >= 0 && event_y >= 0 && + event_x * height + event_y * width <= width * height) + { + return TRUE; + } + else + { + gtk_menu_stop_navigating_submenu (menu); + return FALSE; } - return FALSE; } static void