From 58d4df9fd57ce63007380493da8a3155f183d0b0 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 16 Oct 2021 05:14:54 +0300 Subject: [PATCH 50/51] gtk3x: Avoid gdk_event_get_click_count() calls See osdn #42328 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/cma_fe.c | 46 +++++++--------- client/gui-gtk-4.0/plrdlg.c | 107 ++++++++++++++++++++++++------------ 2 files changed, 94 insertions(+), 59 deletions(-) diff --git a/client/gui-gtk-4.0/cma_fe.c b/client/gui-gtk-4.0/cma_fe.c index 072939ac11..edf8094ec1 100644 --- a/client/gui-gtk-4.0/cma_fe.c +++ b/client/gui-gtk-4.0/cma_fe.c @@ -140,34 +140,26 @@ struct cma_dialog *get_cma_dialog(struct city *pcity) /**********************************************************************//** User has pressed button in cma dialog **************************************************************************/ -static gboolean button_press_callback(GtkTreeView *view, GdkEvent *ev, - gpointer data) +static gboolean button_press_callback(GtkGestureClick *gesture, int n_press, + double x, double y) { + GtkWidget *widget = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture)); + struct cma_dialog *pdialog + = (struct cma_dialog *) g_object_get_data(G_OBJECT(widget), "dialog"); GtkTreePath *path; GtkTreeViewColumn *column; - gdouble e_x, e_y; - gdk_event_get_position(ev, &e_x, &e_y); - if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(view), - e_x, e_y, &path, &column, NULL, NULL)) { - GdkEventType type; - - type = gdk_event_get_event_type(ev); - if (type == GDK_BUTTON_PRESS) { - guint click_count; - - gdk_event_get_click_count(ev, &click_count); - - if (click_count == 1) { - cma_activate_preset_callback(view, path, column, data); - } else if (click_count == 2) { - struct cma_dialog *pdialog = (struct cma_dialog *) data; - struct cm_parameter param; + if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(pdialog->preset_list), + x, y, &path, &column, NULL, NULL)) { + if (n_press == 1) { + cma_activate_preset_callback(GTK_TREE_VIEW(pdialog->preset_list), + path, column, pdialog); + } else if (n_press == 2) { + struct cm_parameter param; - cmafec_get_fe_parameter(pdialog->pcity, ¶m); - cma_put_city_under_agent(pdialog->pcity, ¶m); - refresh_city_dialog(pdialog->pcity); - } + cmafec_get_fe_parameter(pdialog->pcity, ¶m); + cma_put_city_under_agent(pdialog->pcity, ¶m); + refresh_city_dialog(pdialog->pcity); } } gtk_tree_path_free(path); @@ -233,6 +225,7 @@ struct cma_dialog *create_cma_dialog(struct city *pcity, bool tiny) GtkWidget *view; GtkTreeViewColumn *column; gint layout_width; + GtkEventController *controller; cmafec_get_fe_parameter(pcity, ¶m); pdialog = fc_malloc(sizeof(struct cma_dialog)); @@ -275,8 +268,11 @@ struct cma_dialog *create_cma_dialog(struct city *pcity, bool tiny) pdialog->preset_list = view; pdialog->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); - g_signal_connect(pdialog->preset_list, "button_press_event", - G_CALLBACK(button_press_callback), pdialog); + g_object_set_data(G_OBJECT(pdialog->preset_list), "dialog", pdialog); + controller = GTK_EVENT_CONTROLLER(gtk_gesture_click_new()); + g_signal_connect(controller, "pressed", + G_CALLBACK(button_press_callback), NULL); + gtk_widget_add_controller(pdialog->preset_list, controller); gtk_widget_set_tooltip_text(view, _("For information on\n" diff --git a/client/gui-gtk-4.0/plrdlg.c b/client/gui-gtk-4.0/plrdlg.c index 7eb8702285..4ec0967424 100644 --- a/client/gui-gtk-4.0/plrdlg.c +++ b/client/gui-gtk-4.0/plrdlg.c @@ -202,43 +202,70 @@ static void selection_callback(GtkTreeSelection *selection, gpointer data) } /**********************************************************************//** - Button pressed on player list + Left button pressed on player list **************************************************************************/ -static gboolean button_press_callback(GtkTreeView *view, GdkEvent *ev) +static gboolean left_button_press_callback(GtkGestureClick *gesture, int n_press, + double x, double y) { - if (gdk_event_get_event_type(ev) == GDK_BUTTON_PRESS) { - guint click_count; - - gdk_event_get_click_count(ev, &click_count); - - if (click_count == 2) { - GtkTreePath *path; - - gtk_tree_view_get_cursor(view, &path, NULL); - if (path) { - GtkTreeModel *model = gtk_tree_view_get_model(view); - GtkTreeIter it; - gint id; - struct player *plr; - guint button; - - gtk_tree_model_get_iter(model, &it, path); - gtk_tree_path_free(path); - - gtk_tree_model_get(model, &it, PLR_DLG_COL_ID, &id, -1); - plr = player_by_number(id); - - button = gdk_button_event_get_button(ev); - if (button == 1) { - if (can_intel_with_player(plr)) { - popup_intel_dialog(plr); - } - } else if (can_meet_with_player(plr)) { - dsend_packet_diplomacy_init_meeting_req(&client.conn, id); - } + GtkTreeView *view + = GTK_TREE_VIEW(gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture))); + + if (n_press == 2) { + GtkTreePath *path; + + gtk_tree_view_get_cursor(view, &path, NULL); + if (path) { + GtkTreeModel *model = gtk_tree_view_get_model(view); + GtkTreeIter it; + gint id; + struct player *plr; + + gtk_tree_model_get_iter(model, &it, path); + gtk_tree_path_free(path); + + gtk_tree_model_get(model, &it, PLR_DLG_COL_ID, &id, -1); + plr = player_by_number(id); + + if (can_intel_with_player(plr)) { + popup_intel_dialog(plr); } } } + + return FALSE; +} + +/**********************************************************************//** + Right button pressed on player list +**************************************************************************/ +static gboolean right_button_press_callback(GtkGestureClick *gesture, int n_press, + double x, double y) +{ + GtkTreeView *view + = GTK_TREE_VIEW(gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture))); + + if (n_press == 2) { + GtkTreePath *path; + + gtk_tree_view_get_cursor(view, &path, NULL); + if (path) { + GtkTreeModel *model = gtk_tree_view_get_model(view); + GtkTreeIter it; + gint id; + struct player *plr; + + gtk_tree_model_get_iter(model, &it, path); + gtk_tree_path_free(path); + + gtk_tree_model_get(model, &it, PLR_DLG_COL_ID, &id, -1); + plr = player_by_number(id); + + if (can_meet_with_player(plr)) { + dsend_packet_diplomacy_init_meeting_req(&client.conn, id); + } + } + } + return FALSE; } @@ -440,6 +467,9 @@ void create_players_dialog(void) enum ai_level level; #endif /* MENUS_GTK3 */ GtkWidget *vbox; + GtkEventController *left_controller; + GtkEventController *right_controller; + GtkGesture *gesture; gui_dialog_new(&players_dialog_shell, GTK_NOTEBOOK(top_notebook), NULL, TRUE); @@ -463,8 +493,17 @@ void create_players_dialog(void) players_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(players_list)); g_signal_connect(players_selection, "changed", G_CALLBACK(selection_callback), NULL); - g_signal_connect(players_list, "button_press_event", - G_CALLBACK(button_press_callback), NULL); + gesture = gtk_gesture_click_new(); + left_controller = GTK_EVENT_CONTROLLER(gesture); + g_signal_connect(left_controller, "pressed", + G_CALLBACK(left_button_press_callback), NULL); + gtk_widget_add_controller(players_list, left_controller); + gesture = gtk_gesture_click_new(); + gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), 3); + right_controller = GTK_EVENT_CONTROLLER(gesture); + g_signal_connect(right_controller, "pressed", + G_CALLBACK(right_button_press_callback), NULL); + gtk_widget_add_controller(players_list, right_controller); for (i = 0; i < num_player_dlg_columns; i++) { struct player_dlg_column *pcol; -- 2.33.0