From 8899bd48e90e70b52423a9278faea18ad876195b Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 8 May 2022 04:37:26 +0300 Subject: [PATCH 54/54] gtk4: Implement map canvas motion events See osdn #44542 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/editgui.c | 9 +++---- client/gui-gtk-4.0/editgui.h | 3 ++- client/gui-gtk-4.0/gui_main.c | 12 ++++----- client/gui-gtk-4.0/mapctrl.c | 47 +++++++++++------------------------ client/gui-gtk-4.0/mapctrl.h | 6 +++-- 5 files changed, 30 insertions(+), 47 deletions(-) diff --git a/client/gui-gtk-4.0/editgui.c b/client/gui-gtk-4.0/editgui.c index 43222c33b0..f46742d0a2 100644 --- a/client/gui-gtk-4.0/editgui.c +++ b/client/gui-gtk-4.0/editgui.c @@ -953,14 +953,13 @@ gboolean handle_edit_mouse_button_release(GtkGestureClick *gesture, /************************************************************************//** Pass on the gdk mouse event to the editor's handler. ****************************************************************************/ -gboolean handle_edit_mouse_move(GdkEvent *ev) +gboolean handle_edit_mouse_move(GtkEventControllerMotion *controller, + gdouble x, gdouble y) { - gdouble e_x, e_y; GdkModifierType state; - gdk_event_get_position(ev, &e_x, &e_y); - state = gdk_event_get_modifier_state(ev); - editor_mouse_move(e_x, e_y, convert_modifiers(state)); + state = gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(controller)); + editor_mouse_move(x, y, convert_modifiers(state)); return TRUE; } diff --git a/client/gui-gtk-4.0/editgui.h b/client/gui-gtk-4.0/editgui.h index cb2f6462ca..19acbb87cf 100644 --- a/client/gui-gtk-4.0/editgui.h +++ b/client/gui-gtk-4.0/editgui.h @@ -42,7 +42,8 @@ gboolean handle_edit_mouse_button_press(GtkGestureClick *gesture, 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_mouse_move(GtkEventControllerMotion *controller, + gdouble x, gdouble y); gboolean handle_edit_key_press(GdkEvent *ev); struct editinfobox { diff --git a/client/gui-gtk-4.0/gui_main.c b/client/gui-gtk-4.0/gui_main.c index a9ac443ad9..610a1f8db0 100644 --- a/client/gui-gtk-4.0/gui_main.c +++ b/client/gui-gtk-4.0/gui_main.c @@ -1522,16 +1522,16 @@ static void setup_widgets(void) g_signal_connect(mc_controller, "scroll", G_CALLBACK(mouse_scroll_mapcanvas), NULL); gtk_widget_add_controller(map_canvas, mc_controller); + mc_controller = gtk_event_controller_motion_new(); + g_signal_connect(mc_controller, "motion", + G_CALLBACK(move_mapcanvas), NULL); + g_signal_connect(mc_controller, "leave", + G_CALLBACK(leave_mapcanvas), NULL); + gtk_widget_add_controller(map_canvas, mc_controller); g_signal_connect(map_canvas, "resize", G_CALLBACK(map_canvas_resize), NULL); - g_signal_connect(map_canvas, "motion_notify_event", - G_CALLBACK(move_mapcanvas), NULL); - - g_signal_connect(toplevel, "enter_notify_event", - G_CALLBACK(leave_mapcanvas), NULL); - g_signal_connect(toplevel, "key_press_event", G_CALLBACK(toplevel_key_press_handler), NULL); diff --git a/client/gui-gtk-4.0/mapctrl.c b/client/gui-gtk-4.0/mapctrl.c index eac91b1dd8..27fd9b994c 100644 --- a/client/gui-gtk-4.0/mapctrl.c +++ b/client/gui-gtk-4.0/mapctrl.c @@ -404,10 +404,10 @@ void update_rect_at_mouse_pos(void) Triggered by the mouse moving on the mapcanvas, this function will update the mouse cursor and goto lines. **************************************************************************/ -gboolean move_mapcanvas(GtkWidget *w, GdkEvent *ev, gpointer data) +gboolean move_mapcanvas(GtkEventControllerMotion *controller, + gdouble x, gdouble y, gpointer data) { GdkModifierType state; - gdouble e_x, e_y; if (GUI_GTK_OPTION(mouse_over_map_focus) && !gtk_widget_has_focus(map_canvas)) { @@ -415,22 +415,21 @@ gboolean move_mapcanvas(GtkWidget *w, GdkEvent *ev, gpointer data) } if (editor_is_active()) { - return handle_edit_mouse_move(ev); + return handle_edit_mouse_move(controller, x, y); } - gdk_event_get_position(ev, &e_x, &e_y); - cur_x = e_x; - cur_y = e_y; - update_line(e_x, e_y); - state = gdk_event_get_modifier_state(ev); + cur_x = x; + cur_y = y; + update_line(x, y); + state = gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(controller)); if (rbutton_down && (state & GDK_BUTTON3_MASK)) { - update_selection_rectangle(e_x, e_y); + update_selection_rectangle(x, y); } if (keyboardless_goto_button_down && hover_state == HOVER_NONE) { - maybe_activate_keyboardless_goto(e_x, e_y); + maybe_activate_keyboardless_goto(x, y); } - control_mouse_cursor(canvas_pos_to_tile(e_x, e_y)); + control_mouse_cursor(canvas_pos_to_tile(x, y)); return TRUE; } @@ -438,30 +437,12 @@ gboolean move_mapcanvas(GtkWidget *w, GdkEvent *ev, gpointer data) /**********************************************************************//** This function will reset the mouse cursor if it leaves the map. **************************************************************************/ -gboolean leave_mapcanvas(GtkWidget *widget, GdkEvent *ev) +gboolean leave_mapcanvas(GtkEventControllerMotion *controller, + gpointer data) { - gdouble e_x, e_y; - - if (gtk_notebook_get_current_page(GTK_NOTEBOOK(top_notebook)) - != gtk_notebook_page_num(GTK_NOTEBOOK(top_notebook), map_widget)) { - /* Map is not currently topmost tab. Do not use tile specific cursors. */ - update_mouse_cursor(CURSOR_DEFAULT); - return TRUE; - } - - /* Bizarrely, this function can be called even when we don't "leave" - * the map canvas, for instance, it gets called any time the mouse is - * clicked. */ - gdk_event_get_position(ev, &e_x, &e_y); - if (!map_is_empty() - && e_x >= 0 && e_y >= 0 - && e_x < mapview.width && e_y < mapview.height) { - control_mouse_cursor(canvas_pos_to_tile(e_x, e_y)); - } else { - update_mouse_cursor(CURSOR_DEFAULT); - } - + update_mouse_cursor(CURSOR_DEFAULT); update_unit_info_label(get_units_in_focus()); + return TRUE; } diff --git a/client/gui-gtk-4.0/mapctrl.h b/client/gui-gtk-4.0/mapctrl.h index f65654eecd..208dd1e368 100644 --- a/client/gui-gtk-4.0/mapctrl.h +++ b/client/gui-gtk-4.0/mapctrl.h @@ -32,8 +32,10 @@ gboolean right_butt_down_mapcanvas(GtkGestureClick *gesture, int n_press, gboolean middle_butt_down_mapcanvas(GtkGestureClick *gesture, int n_press, double x, double y); gboolean butt_down_overviewcanvas(GtkWidget *w, GdkEvent *ev, gpointer data); -gboolean move_mapcanvas(GtkWidget *w, GdkEvent *ev, gpointer data); -gboolean leave_mapcanvas(GtkWidget *widget, GdkEvent *ev); +gboolean move_mapcanvas(GtkEventControllerMotion *controller, + gdouble x, gdouble y, gpointer data); +gboolean leave_mapcanvas(GtkEventControllerMotion *controller, + gpointer data); gboolean move_overviewcanvas(GtkWidget *w, GdkEvent *ev, gpointer data); void center_on_unit(void); -- 2.35.1