Subject: bug#59351: 29.0.50; [PATCH] Fix mouse click position to menu bar entry

From: Manuel Giraud <[email-masked]>
Date: Fri, 18 Nov 2022 16:20:16 +0100

Eli Zaretskii <[email-masked]> writes:

[...]

> This cannot be a compile-time condition, because Emacs compiled with X
> support can still have TTY frames.  So you need to test for
> FRAME_WINDOW_P, and only call x_y_to_hpos_vpos if FRAME_WINDOW_P
> returns non-zero; otherwise call pixel_to_glyph_coords even in a build
> where HAVE_WINDOW_SYSTEM is defined.

Ok.  Sorry about that.  Here is a new one.
>From 13cd5a971b6ff7e7b809b9cc4073039c695b9dcd Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Fri, 18 Nov 2022 09:24:55 +0100
Subject: [PATCH] Fix click position to menu bar entry with no-toolkit

* src/keyboard.c (make_lispy_event): Use x_y_to_hpos_vpos to
compute correct menu bar position should the menu face change.
* src/xdisp.c (x_y_to_hpos_vpos): Not static anymore.
* src/dispextern.h: Export x_y_to_hpos_vpos.
---
 src/dispextern.h |  3 ++-
 src/keyboard.c   | 16 ++++++++++++++--
 src/xdisp.c      |  2 +-
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/dispextern.h b/src/dispextern.h
index 2f5f4335fe..2afbdeabaa 100644
--- a/src/dispextern.h
+++ b/src/dispextern.h
@@ -3495,7 +3495,8 @@ #define TTY_CAP_STRIKE_THROUGH	0x20
 extern void tty_draw_row_with_mouse_face (struct window *, struct glyph_row *,
 					  int, int, enum draw_glyphs_face);
 extern void display_tty_menu_item (const char *, int, int, int, int, bool);
-
+extern struct glyph *x_y_to_hpos_vpos (struct window *, int, int, int *, int *,
+				       int *, int *, int *);
 /* Flags passed to try_window.  */
 #define TRY_WINDOW_CHECK_MARGINS	(1 << 0)
 #define TRY_WINDOW_IGNORE_FONTS_CHANGE	(1 << 1)
diff --git a/src/keyboard.c b/src/keyboard.c
index 069cf0627b..c16f8808c4 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -5974,8 +5974,20 @@ make_lispy_event (struct input_event *event)
 	       in a menu (non-toolkit version).  */
 	    if (!toolkit_menubar_in_use (f))
 	      {
-		pixel_to_glyph_coords (f, XFIXNUM (event->x), XFIXNUM (event->y),
-				       &column, &row, NULL, 1);
+		if (FRAME_WINDOW_P (f))
+		  {
+		    struct window *menu_w = XWINDOW (f->menu_bar_window);
+		    int x, y, dummy;
+
+		    x = FRAME_TO_WINDOW_PIXEL_X (menu_w, XFIXNUM (event->x));
+		    y = FRAME_TO_WINDOW_PIXEL_Y (menu_w, XFIXNUM (event->y));
+
+		    x_y_to_hpos_vpos (XWINDOW (f->menu_bar_window), x, y, &column, &row,
+				      NULL, NULL, &dummy);
+		  }
+		else
+		  pixel_to_glyph_coords (f, XFIXNUM (event->x), XFIXNUM (event->y),
+					 &column, &row, NULL, 1);
 
 		/* In the non-toolkit version, clicks on the menu bar
 		   are ordinary button events in the event buffer.
diff --git a/src/xdisp.c b/src/xdisp.c
index f6a279636a..0c073c0fb5 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -2330,7 +2330,7 @@ pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
    text, or we can't tell because W's current matrix is not up to
    date.  */
 
-static struct glyph *
+struct glyph *
 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
 		  int *dx, int *dy, int *area)
 {
-- 
2.38.1

-- 
Manuel Giraud