>From cc5a4e5702c4eeb427d91776204b844ceda61a32 Mon Sep 17 00:00:00 2001 From: Manuel Giraud Date: Sun, 9 Oct 2022 13:27:31 +0200 Subject: [PATCH] start bars refactoring --- src/frame.h | 7 ++ src/xdisp.c | 280 ++++++++++++++++++++++++---------------------------- 2 files changed, 137 insertions(+), 150 deletions(-) diff --git a/src/frame.h b/src/frame.h index 458b6257e4..4c78f0a555 100644 --- a/src/frame.h +++ b/src/frame.h @@ -76,6 +76,13 @@ #define EMACS_FRAME_H #endif #endif /* HAVE_WINDOW_SYSTEM */ +enum bar_type + { + MENU_BAR, + TAB_BAR, + TOOL_BAR + }; + /* The structure representing a frame. */ struct frame diff --git a/src/xdisp.c b/src/xdisp.c index 9534e27843..a822b0fe1c 100644 --- a/src/xdisp.c +++ b/src/xdisp.c @@ -13524,6 +13524,110 @@ gui_consider_frame_title (Lisp_Object frame) #endif /* not HAVE_WINDOW_SYSTEM */ + +/*********************************************************************** + Generics for Bars + ***********************************************************************/ + +/* Get information about the bar item which is displayed in GLYPH on + frame F. Return in *PROP_IDX the index where bar item properties + start in the bar items. For TAB_BAR, return in CLOSE_P an + indication whether the click was on the close-tab icon of the tab. + Value is false if GLYPH doesn't display a bar item. */ + +static bool +bar_item_info (ENUM_BF(bar_type) bar, struct frame *f, struct glyph *glyph, + int *prop_idx, bool *close_p) +{ + Lisp_Object prop, string; + ptrdiff_t charpos; + + if (bar == TOOL_BAR) + string = f->current_tool_bar_string; + else if (bar == TAB_BAR) + string = f->current_tab_bar_string; + else + return false; + + /* This function can be called asynchronously, which means we must + exclude any possibility that Fget_text_property signals an + error. */ + charpos = min (SCHARS (string), glyph->charpos); + charpos = max (0, charpos); + + /* Get the text property `menu-item' at pos. The value of that + property is the start index of this item's properties in + F->tool_bar_items. */ + prop = Fget_text_property (make_fixnum (charpos), Qmenu_item, string); + if (! FIXNUMP (prop)) + return false; + *prop_idx = XFIXNUM (prop); + + if (bar == TAB_BAR) + *close_p = !NILP (Fget_text_property (make_fixnum (charpos), + Qclose_tab, + string)); + + return true; +} + +/* Get information about the bar item at position X/Y on frame F. + Return in *GLYPH a pointer to the glyph of the bar item in the + current matrix of the bar window of F, or NULL if not on a bar + item. Return in *PROP_IDX the index of the bar item in bar items. + Value is + + -1 if X/Y is not on a bar item + 0 if X/Y is on the same item that was highlighted before. + 1 otherwise. */ + +static int +get_bar_item (ENUM_BF(bar_type) bar, struct frame *f, int x, int y, + struct glyph **glyph, int *hpos, int *vpos, int *prop_idx, + bool *close_p) +{ + Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f); + Lisp_Object window; + struct window *w; + int area; + + if (bar == TOOL_BAR) + window = f->tool_bar_window; + else if (bar == MENU_BAR) + window = f->menu_bar_window; + else if (bar == TAB_BAR) + window = f->tab_bar_window; + else + return -1; + + w = XWINDOW (window); + + /* Find the glyph under X/Y. */ + *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area); + if (*glyph == NULL) + return -1; + + /* Get the start of this tool-bar item's properties in + f->tool_bar_items. */ + if (!bar_item_info (bar, f, *glyph, prop_idx, close_p)) + return -1; + + /* Is mouse on the highlighted item? */ + if (bar == TAB_BAR) + return *prop_idx == f->last_tab_bar_item ? 0 : 1; + else if (EQ (window, hlinfo->mouse_face_window) + && *vpos >= hlinfo->mouse_face_beg_row + && *vpos <= hlinfo->mouse_face_end_row + && (*vpos > hlinfo->mouse_face_beg_row + || *hpos >= hlinfo->mouse_face_beg_col) + && (*vpos < hlinfo->mouse_face_end_row + || *hpos < hlinfo->mouse_face_end_col + || hlinfo->mouse_face_past_end)) + return 0; + + return 1; +} + /*********************************************************************** Menu Bars @@ -14471,73 +14575,6 @@ redisplay_tab_bar (struct frame *f) return false; } -/* Get information about the tab-bar item which is displayed in GLYPH - on frame F. Return in *PROP_IDX the index where tab-bar item - properties start in F->tab_bar_items. Return in CLOSE_P an - indication whether the click was on the close-tab icon of the tab. - Value is false if GLYPH doesn't display a tab-bar item. */ - -static bool -tab_bar_item_info (struct frame *f, struct glyph *glyph, - int *prop_idx, bool *close_p) -{ - Lisp_Object prop; - ptrdiff_t charpos; - - /* This function can be called asynchronously, which means we must - exclude any possibility that Fget_text_property signals an - error. */ - charpos = min (SCHARS (f->current_tab_bar_string), glyph->charpos); - charpos = max (0, charpos); - - /* Get the text property `menu-item' at pos. The value of that - property is the start index of this item's properties in - F->tab_bar_items. */ - prop = Fget_text_property (make_fixnum (charpos), - Qmenu_item, f->current_tab_bar_string); - if (! FIXNUMP (prop)) - return false; - *prop_idx = XFIXNUM (prop); - - *close_p = !NILP (Fget_text_property (make_fixnum (charpos), - Qclose_tab, - f->current_tab_bar_string)); - - return true; -} - - -/* Get information about the tab-bar item at position X/Y on frame F. - Return in *GLYPH a pointer to the glyph of the tab-bar item in - the current matrix of the tab-bar window of F, or NULL if not - on a tab-bar item. Return in *PROP_IDX the index of the tab-bar - item in F->tab_bar_items. Value is - - -1 if X/Y is not on a tab-bar item - 0 if X/Y is on the same item that was highlighted before. - 1 otherwise. */ - -static int -get_tab_bar_item (struct frame *f, int x, int y, struct glyph **glyph, - int *hpos, int *vpos, int *prop_idx, bool *close_p) -{ - struct window *w = XWINDOW (f->tab_bar_window); - int area; - - /* Find the glyph under X/Y. */ - *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area); - if (*glyph == NULL) - return -1; - - /* Get the start of this tab-bar item's properties in - f->tab_bar_items. */ - if (!tab_bar_item_info (f, *glyph, prop_idx, close_p)) - return -1; - - return *prop_idx == f->last_tab_bar_item ? 0 : 1; -} - - /* EXPORT: Handle mouse button event on the tab-bar of frame F, at frame-relative coordinates X/Y. DOWN_P is true for a button press, @@ -14557,7 +14594,7 @@ handle_tab_bar_click (struct frame *f, int x, int y, bool down_p, int ts; frame_to_window_pixel_xy (w, &x, &y); - ts = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p); + ts = get_bar_item (TAB_BAR, f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p); if (ts == -1) return Fcons (Qtab_bar, Qnil); @@ -14624,7 +14661,7 @@ note_tab_bar_highlight (struct frame *f, int x, int y) return; } - rc = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p); + rc = get_bar_item (TAB_BAR, f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p); if (rc < 0) { /* Not on tab-bar item. */ @@ -15400,77 +15437,6 @@ redisplay_tool_bar (struct frame *f) return false; } -/* Get information about the tool-bar item which is displayed in GLYPH - on frame F. Return in *PROP_IDX the index where tool-bar item - properties start in F->tool_bar_items. Value is false if - GLYPH doesn't display a tool-bar item. */ - -static bool -tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx) -{ - Lisp_Object prop; - ptrdiff_t charpos; - - /* This function can be called asynchronously, which means we must - exclude any possibility that Fget_text_property signals an - error. */ - charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos); - charpos = max (0, charpos); - - /* Get the text property `menu-item' at pos. The value of that - property is the start index of this item's properties in - F->tool_bar_items. */ - prop = Fget_text_property (make_fixnum (charpos), - Qmenu_item, f->current_tool_bar_string); - if (! FIXNUMP (prop)) - return false; - *prop_idx = XFIXNUM (prop); - return true; -} - - -/* Get information about the tool-bar item at position X/Y on frame F. - Return in *GLYPH a pointer to the glyph of the tool-bar item in - the current matrix of the tool-bar window of F, or NULL if not - on a tool-bar item. Return in *PROP_IDX the index of the tool-bar - item in F->tool_bar_items. Value is - - -1 if X/Y is not on a tool-bar item - 0 if X/Y is on the same item that was highlighted before. - 1 otherwise. */ - -static int -get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph, - int *hpos, int *vpos, int *prop_idx) -{ - Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f); - struct window *w = XWINDOW (f->tool_bar_window); - int area; - - /* Find the glyph under X/Y. */ - *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area); - if (*glyph == NULL) - return -1; - - /* Get the start of this tool-bar item's properties in - f->tool_bar_items. */ - if (!tool_bar_item_info (f, *glyph, prop_idx)) - return -1; - - /* Is mouse on the highlighted item? */ - if (EQ (f->tool_bar_window, hlinfo->mouse_face_window) - && *vpos >= hlinfo->mouse_face_beg_row - && *vpos <= hlinfo->mouse_face_end_row - && (*vpos > hlinfo->mouse_face_beg_row - || *hpos >= hlinfo->mouse_face_beg_col) - && (*vpos < hlinfo->mouse_face_end_row - || *hpos < hlinfo->mouse_face_end_col - || hlinfo->mouse_face_past_end)) - return 0; - - return 1; -} - /* EXPORT: Handle mouse button event on the tool-bar of frame F, at @@ -15485,6 +15451,7 @@ handle_tool_bar_click_with_device (struct frame *f, int x, int y, bool down_p, Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f); struct window *w = XWINDOW (f->tool_bar_window); int hpos, vpos, prop_idx; + bool close_p; struct glyph *glyph; Lisp_Object enabled_p; int ts; @@ -15497,7 +15464,7 @@ handle_tool_bar_click_with_device (struct frame *f, int x, int y, bool down_p, highlight, since tool-bar items are not highlighted in that case. */ frame_to_window_pixel_xy (w, &x, &y); - ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx); + ts = get_bar_item (TOOL_BAR, f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p); if (ts == -1 || (ts != 0 && !NILP (Vmouse_highlight))) return; @@ -15555,11 +15522,10 @@ handle_tool_bar_click (struct frame *f, int x, int y, bool down_p, note_mouse_highlight. */ static void -note_tool_bar_highlight (struct frame *f, int x, int y) +note_bar_highlight (ENUM_BF(bar_type) bar, struct frame *f, int x, int y) { - Lisp_Object window = f->tool_bar_window; - struct window *w = XWINDOW (window); - Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); + Lisp_Object window; + struct window *w; Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f); int hpos, vpos; struct glyph *glyph; @@ -15567,10 +15533,22 @@ note_tool_bar_highlight (struct frame *f, int x, int y) int i; Lisp_Object enabled_p; int prop_idx; + bool close_p; enum draw_glyphs_face draw = DRAW_IMAGE_RAISED; - bool mouse_down_p; int rc; + /* Select bar window. */ + if (bar == TOOL_BAR) + window = f->tool_bar_window; + else if (bar == MENU_BAR) + window = f->menu_bar_window; + else if (bar == TAB_BAR) + window = f->tab_bar_window; + else + return; + + w = XWINDOW (window); + /* Function note_mouse_highlight is called with negative X/Y values when mouse moves outside of the frame. */ if (x <= 0 || y <= 0) @@ -15579,20 +15557,22 @@ note_tool_bar_highlight (struct frame *f, int x, int y) return; } - rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx); + rc = get_bar_item (bar, f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p); if (rc < 0) { - /* Not on tool-bar item. */ + /* Not on bar item. */ clear_mouse_face (hlinfo); return; } else if (rc == 0) - /* On same tool-bar item as before. */ + /* On same bar item as before. */ goto set_help_echo; clear_mouse_face (hlinfo); + bool mouse_down_p = false; /* Mouse is down, but on different tool-bar item? */ + Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f); mouse_down_p = (gui_mouse_grabbed (dpyinfo) && f == dpyinfo->last_mouse_frame); @@ -35049,7 +35029,7 @@ note_mouse_highlight (struct frame *f, int x, int y) buffer. */ if (EQ (window, f->tool_bar_window)) { - note_tool_bar_highlight (f, x, y); + note_bar_highlight (TOOL_BAR, f, x, y); return; } #endif -- 2.37.3