From 5845f42e3a1c71cbb2fa7e26681511092887915c Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 25 May 2022 23:19:28 +0300 Subject: [PATCH 26/26] gtk4: Implement toplevel key press events See osdn #44673 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/editgui.c | 16 +++++----------- client/gui-gtk-4.0/editgui.h | 2 +- client/gui-gtk-4.0/gui_main.c | 36 +++++++++++++++++------------------ 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/client/gui-gtk-4.0/editgui.c b/client/gui-gtk-4.0/editgui.c index f46742d0a2..67aec168fc 100644 --- a/client/gui-gtk-4.0/editgui.c +++ b/client/gui-gtk-4.0/editgui.c @@ -1622,7 +1622,7 @@ static void editinfobox_refresh(struct editinfobox *ei) /************************************************************************//** Handle ctrl+ combinations. ****************************************************************************/ -static gboolean handle_edit_key_press_with_ctrl(GdkEvent *ev) +static gboolean handle_edit_key_press_with_ctrl(guint keyval) { return FALSE; /* Don't gobble. */ } @@ -1630,13 +1630,11 @@ static gboolean handle_edit_key_press_with_ctrl(GdkEvent *ev) /************************************************************************//** Handle shift+ combinations. ****************************************************************************/ -static gboolean handle_edit_key_press_with_shift(GdkEvent *ev) +static gboolean handle_edit_key_press_with_shift(guint keyval) { enum editor_tool_type ett; - guint keyval; ett = editor_get_tool(); - keyval = gdk_key_event_get_keyval(ev); switch (keyval) { case GDK_KEY_D: editor_tool_toggle_mode(ett, ETM_ERASE); @@ -1680,24 +1678,20 @@ static gboolean handle_edit_key_press_with_shift(GdkEvent *ev) /************************************************************************//** Handle any kind of key press event. ****************************************************************************/ -gboolean handle_edit_key_press(GdkEvent *ev) +gboolean handle_edit_key_press(guint keyval, GdkModifierType state) { enum editor_tool_type ett, new_ett = NUM_EDITOR_TOOL_TYPES; - GdkModifierType state; - guint keyval; - state = gdk_event_get_modifier_state(ev); if (state & GDK_SHIFT_MASK) { - return handle_edit_key_press_with_shift(ev); + return handle_edit_key_press_with_shift(keyval); } if (state & GDK_CONTROL_MASK) { - return handle_edit_key_press_with_ctrl(ev); + return handle_edit_key_press_with_ctrl(keyval); } ett = editor_get_tool(); - keyval = gdk_key_event_get_keyval(ev); switch (keyval) { case GDK_KEY_t: new_ett = ETT_TERRAIN; diff --git a/client/gui-gtk-4.0/editgui.h b/client/gui-gtk-4.0/editgui.h index 19acbb87cf..4bdeb11764 100644 --- a/client/gui-gtk-4.0/editgui.h +++ b/client/gui-gtk-4.0/editgui.h @@ -44,7 +44,7 @@ gboolean handle_edit_mouse_button_release(GtkGestureClick *gesture, double x, double y); gboolean handle_edit_mouse_move(GtkEventControllerMotion *controller, gdouble x, gdouble y); -gboolean handle_edit_key_press(GdkEvent *ev); +gboolean handle_edit_key_press(guint keyval, GdkModifierType state); struct editinfobox { GtkWidget *widget; diff --git a/client/gui-gtk-4.0/gui_main.c b/client/gui-gtk-4.0/gui_main.c index 34620d1c35..ee71a2a906 100644 --- a/client/gui-gtk-4.0/gui_main.c +++ b/client/gui-gtk-4.0/gui_main.c @@ -195,7 +195,11 @@ static void set_wait_for_writable_socket(struct connection *pc, static void print_usage(void); static void activate_gui(GtkApplication *app, gpointer data); static void parse_options(int argc, char **argv); -static gboolean toplevel_key_press_handler(GtkWidget *w, GdkEvent *ev, gpointer data); +static gboolean toplevel_key_press_handler(GtkEventControllerKey *controller, + guint keyval, + guint keycode, + GdkModifierType state, + gpointer data); static gboolean mouse_scroll_mapcanvas(GtkEventControllerScroll *controller, gdouble dx, gdouble dy, gpointer data); @@ -377,14 +381,8 @@ gboolean map_canvas_focus(void) /**********************************************************************//** Handle keypress events when map canvas is in focus **************************************************************************/ -static gboolean key_press_map_canvas(GtkWidget *w, GdkEvent *ev, - gpointer data) +static gboolean key_press_map_canvas(guint keyval, GdkModifierType state) { - GdkModifierType state; - guint keyval; - - state = gdk_event_get_modifier_state(ev); - keyval = gdk_key_event_get_keyval(ev); if ((state & GDK_SHIFT_MASK)) { switch (keyval) { @@ -410,12 +408,12 @@ static gboolean key_press_map_canvas(GtkWidget *w, GdkEvent *ev, case GDK_KEY_Page_Up: g_signal_emit_by_name(main_message_area, "move_cursor", - GTK_MOVEMENT_PAGES, -1, FALSE); + GTK_MOVEMENT_PAGES, -1, FALSE); return TRUE; case GDK_KEY_Page_Down: g_signal_emit_by_name(main_message_area, "move_cursor", - GTK_MOVEMENT_PAGES, 1, FALSE); + GTK_MOVEMENT_PAGES, 1, FALSE); return TRUE; default: @@ -581,17 +579,16 @@ static gboolean key_press_map_canvas(GtkWidget *w, GdkEvent *ev, /**********************************************************************//** Handle a keyboard key press made in the client's toplevel window. **************************************************************************/ -static gboolean toplevel_key_press_handler(GtkWidget *w, GdkEvent *ev, +static gboolean toplevel_key_press_handler(GtkEventControllerKey *controller, + guint keyval, + guint keycode, + GdkModifierType state, gpointer data) { - guint keyval; - GdkModifierType state; - if (inputline_has_focus()) { return FALSE; } - keyval = gdk_key_event_get_keyval(ev); switch (keyval) { case GDK_KEY_apostrophe: @@ -630,12 +627,11 @@ static gboolean toplevel_key_press_handler(GtkWidget *w, GdkEvent *ev, } if (editor_is_active()) { - if (handle_edit_key_press(ev)) { + if (handle_edit_key_press(keyval, state)) { return TRUE; } } - state = gdk_event_get_modifier_state(ev); if (state & GDK_SHIFT_MASK) { switch (keyval) { @@ -651,7 +647,7 @@ static gboolean toplevel_key_press_handler(GtkWidget *w, GdkEvent *ev, if (0 == gtk_notebook_get_current_page(GTK_NOTEBOOK(top_notebook))) { /* 0 means the map view is focused. */ - return key_press_map_canvas(w, ev, data); + return key_press_map_canvas(keyval, state); } #if 0 @@ -1541,8 +1537,10 @@ static void setup_widgets(void) g_signal_connect(map_canvas, "resize", G_CALLBACK(map_canvas_resize), NULL); - g_signal_connect(toplevel, "key_press_event", + mc_controller = gtk_event_controller_key_new(); + g_signal_connect(mc_controller, "key-pressed", G_CALLBACK(toplevel_key_press_handler), NULL); + gtk_widget_add_controller(toplevel, mc_controller); /* *** The message window -- this is a detachable widget *** */ -- 2.35.1