From 107c05485fa03125fbf8848de8edb3944e1a0998 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 13 Apr 2022 03:18:07 +0300 Subject: [PATCH 44/44] gtk4: Implement mouse button up gestures for mapcanvas. See osdn #44305 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/editgui.c | 40 ++++++----------------------------- client/gui-gtk-4.0/editgui.h | 4 +++- client/gui-gtk-4.0/gui_main.c | 7 +++--- client/gui-gtk-4.0/mapctrl.c | 39 ++++++++++++++++++++++------------ client/gui-gtk-4.0/mapctrl.h | 5 ++++- 5 files changed, 43 insertions(+), 52 deletions(-) diff --git a/client/gui-gtk-4.0/editgui.c b/client/gui-gtk-4.0/editgui.c index 93dfd8b437..43222c33b0 100644 --- a/client/gui-gtk-4.0/editgui.c +++ b/client/gui-gtk-4.0/editgui.c @@ -917,28 +917,6 @@ static int convert_modifiers(int gdk_event_state) return modifiers; } -/************************************************************************//** - Convert gdk mouse button values to editor mouse button values. -****************************************************************************/ -static int convert_mouse_button(int gdk_mouse_button) -{ - switch (gdk_mouse_button) { - case 1: - return MOUSE_BUTTON_LEFT; - break; - case 2: - return MOUSE_BUTTON_MIDDLE; - break; - case 3: - return MOUSE_BUTTON_RIGHT; - break; - default: - break; - } - - return MOUSE_BUTTON_OTHER; -} - /************************************************************************//** Pass on the gdk mouse event to the editor's handler. ****************************************************************************/ @@ -959,21 +937,15 @@ gboolean handle_edit_mouse_button_press(GtkGestureClick *gesture, /************************************************************************//** Pass on the gdk mouse event to the editor's handler. ****************************************************************************/ -gboolean handle_edit_mouse_button_release(GdkEvent *ev) +gboolean handle_edit_mouse_button_release(GtkGestureClick *gesture, + int editor_mouse_button, + double x, double y) { - gdouble e_x, e_y; - guint button; GdkModifierType state; - if (gdk_event_get_event_type(ev) != GDK_BUTTON_RELEASE) { - return TRUE; - } - - gdk_event_get_position(ev, &e_x, &e_y); - button = gdk_button_event_get_button(ev); - state = gdk_event_get_modifier_state(ev); - editor_mouse_button_release(e_x, e_y, - convert_mouse_button(button), + state = gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(gesture)); + editor_mouse_button_release(x, y, + editor_mouse_button, convert_modifiers(state)); return TRUE; } diff --git a/client/gui-gtk-4.0/editgui.h b/client/gui-gtk-4.0/editgui.h index 4987f0f1c6..cb2f6462ca 100644 --- a/client/gui-gtk-4.0/editgui.h +++ b/client/gui-gtk-4.0/editgui.h @@ -39,7 +39,9 @@ struct editbar { gboolean handle_edit_mouse_button_press(GtkGestureClick *gesture, int editor_mouse_button, double x, double y); -gboolean handle_edit_mouse_button_release(GdkEvent *ev); +gboolean handle_edit_mouse_button_release(GtkGestureClick *gesture, + int editor_mouse_button, + double x, double y); gboolean handle_edit_mouse_move(GdkEvent *ev); gboolean handle_edit_key_press(GdkEvent *ev); diff --git a/client/gui-gtk-4.0/gui_main.c b/client/gui-gtk-4.0/gui_main.c index 5f5c111771..fb0f7b33e0 100644 --- a/client/gui-gtk-4.0/gui_main.c +++ b/client/gui-gtk-4.0/gui_main.c @@ -1510,12 +1510,16 @@ static void setup_widgets(void) mc_controller = GTK_EVENT_CONTROLLER(gtk_gesture_click_new()); g_signal_connect(mc_controller, "pressed", G_CALLBACK(left_butt_down_mapcanvas), NULL); + g_signal_connect(mc_controller, "released", + G_CALLBACK(left_butt_up_mapcanvas), NULL); gtk_widget_add_controller(map_canvas, mc_controller); mc_gesture = gtk_gesture_click_new(); gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(mc_gesture), 3); mc_controller = GTK_EVENT_CONTROLLER(mc_gesture); g_signal_connect(mc_controller, "pressed", G_CALLBACK(right_butt_down_mapcanvas), NULL); + g_signal_connect(mc_controller, "released", + G_CALLBACK(right_butt_up_mapcanvas), NULL); gtk_widget_add_controller(map_canvas, mc_controller); mc_gesture = gtk_gesture_click_new(); gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(mc_gesture), 2); @@ -1533,9 +1537,6 @@ static void setup_widgets(void) g_signal_connect(toplevel, "enter_notify_event", G_CALLBACK(leave_mapcanvas), NULL); - g_signal_connect(map_canvas, "button_release_event", - G_CALLBACK(butt_release_mapcanvas), NULL); - g_signal_connect(map_canvas, "scroll_event", G_CALLBACK(mouse_scroll_mapcanvas), NULL); diff --git a/client/gui-gtk-4.0/mapctrl.c b/client/gui-gtk-4.0/mapctrl.c index a4917666d9..eac91b1dd8 100644 --- a/client/gui-gtk-4.0/mapctrl.c +++ b/client/gui-gtk-4.0/mapctrl.c @@ -147,28 +147,41 @@ void set_turn_done_button_state(bool state) } /**********************************************************************//** - Handle 'Mouse button released'. Because of the quickselect feature, + Handle 'Left mouse button released'. Because of the quickselect feature, the release of both left and right mousebutton can launch the goto. **************************************************************************/ -gboolean butt_release_mapcanvas(GtkWidget *w, GdkEvent *ev, gpointer data) +gboolean left_butt_up_mapcanvas(GtkGestureClick *gesture, int n_press, + double x, double y) { - guint button; - gdouble e_x, e_y; - if (editor_is_active()) { - return handle_edit_mouse_button_release(ev); + return handle_edit_mouse_button_release(gesture, MOUSE_BUTTON_LEFT, + x, y); } - button = gdk_button_event_get_button(ev); - gdk_event_get_position(ev, &e_x, &e_y); - if (button == 1 || button == 3) { - release_goto_button(e_x, e_y); + release_goto_button(x, y); + + return TRUE; +} + +/**********************************************************************//** + Handle 'Right mouse button released'. Because of the quickselect feature, + the release of both left and right mousebutton can launch the goto. +**************************************************************************/ +gboolean right_butt_up_mapcanvas(GtkGestureClick *gesture, int n_press, + double x, double y) +{ + if (editor_is_active()) { + return handle_edit_mouse_button_release(gesture, MOUSE_BUTTON_RIGHT, + x, y); } - if (button == 3 && (rbutton_down || hover_state != HOVER_NONE)) { + + release_goto_button(x, y); + + if (rbutton_down || hover_state != HOVER_NONE) { GdkModifierType state; - state = gdk_event_get_modifier_state(ev); - release_right_button(e_x, e_y, + state = gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(gesture)); + release_right_button(x, y, (state & GDK_SHIFT_MASK) != 0); } diff --git a/client/gui-gtk-4.0/mapctrl.h b/client/gui-gtk-4.0/mapctrl.h index 92f547c6e6..f65654eecd 100644 --- a/client/gui-gtk-4.0/mapctrl.h +++ b/client/gui-gtk-4.0/mapctrl.h @@ -21,7 +21,10 @@ /* client */ #include "mapctrl_g.h" -gboolean butt_release_mapcanvas(GtkWidget *w, GdkEvent *ev, gpointer data); +gboolean left_butt_up_mapcanvas(GtkGestureClick *gesture, int n_press, + double x, double y); +gboolean right_butt_up_mapcanvas(GtkGestureClick *gesture, int n_press, + double x, double y); gboolean left_butt_down_mapcanvas(GtkGestureClick *gesture, int n_press, double x, double y); gboolean right_butt_down_mapcanvas(GtkGestureClick *gesture, int n_press, -- 2.35.1