From b8be67800a4dbb419fb8f0a90d18e174541590fd Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 31 Dec 2022 10:20:21 +0200 Subject: [PATCH 37/37] gtk4: Make citydlg production bar an drag&drop target See osdn #46401 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/citydlg.c | 44 ++++++++++-------------------------- client/gui-gtk-4.0/wldlg.c | 30 ++++++++++-------------- client/gui-gtk-4.0/wldlg.h | 8 +++++-- 3 files changed, 30 insertions(+), 52 deletions(-) diff --git a/client/gui-gtk-4.0/citydlg.c b/client/gui-gtk-4.0/citydlg.c index 77494aab62..acacc4154a 100644 --- a/client/gui-gtk-4.0/citydlg.c +++ b/client/gui-gtk-4.0/citydlg.c @@ -1151,44 +1151,29 @@ static void create_and_append_map_page(struct city_dialog *pdialog) } } -#ifdef GTK3_DRAG_DROP /***********************************************************************//** Something dragged to worklist dialog. ***************************************************************************/ -static void target_drag_data_received(GtkWidget *w, - GdkDragContext *context, - gint x, gint y, - GtkSelectionData *data, - guint info, guint time, - gpointer user_data) +static gboolean target_drag_data_received(GtkDropTarget *target, + const GValue *value, + double x, double y, gpointer data) { - struct city_dialog *pdialog = (struct city_dialog *) user_data; - GtkTreeModel *model; - GtkTreePath *path; + struct city_dialog *pdialog = (struct city_dialog *) data; + cid id; + struct universal univ; if (NULL != client.conn.playing && city_owner(pdialog->pcity) != client.conn.playing) { - gtk_drag_finish(context, FALSE, FALSE, time); + return FALSE; } - if (gtk_tree_get_row_drag_data(data, &model, &path)) { - GtkTreeIter it; - - if (gtk_tree_model_get_iter(model, &it, path)) { - cid id; - struct universal univ; + id = g_value_get_int(value); + univ = cid_production(id); - gtk_tree_model_get(model, &it, 0, &id, -1); - univ = cid_production(id); - city_change_production(pdialog->pcity, &univ); - gtk_drag_finish(context, TRUE, FALSE, time); - } - gtk_tree_path_free(path); - } + city_change_production(pdialog->pcity, &univ); - gtk_drag_finish(context, FALSE, FALSE, time); + return TRUE; } -#endif /* GTK3_DRAG_DROP */ /***********************************************************************//** Create production page header - what tab this actually is, @@ -1216,12 +1201,7 @@ static int create_production_header(struct city_dialog *pdialog, gtk_grid_attach(GTK_GRID(hgrid), bar, grid_col++, 0, 1, 1); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(bar), _("%d/%d %d turns")); - add_worklist_dnd_target(bar); - -#ifdef GTK3_DRAG_DROP - g_signal_connect(bar, "drag_data_received", - G_CALLBACK(target_drag_data_received), pdialog); -#endif /* GTK3_DRAG_DROP */ + add_worklist_dnd_target(bar, target_drag_data_received, pdialog); pdialog->production.buy_command = icon_label_button_new("system-run", _("_Buy")); diff --git a/client/gui-gtk-4.0/wldlg.c b/client/gui-gtk-4.0/wldlg.c index 771233927b..40752a497f 100644 --- a/client/gui-gtk-4.0/wldlg.c +++ b/client/gui-gtk-4.0/wldlg.c @@ -325,28 +325,22 @@ static GHashTable *hash; static void commit_worklist(struct worklist_data *ptr); - -enum { - TARGET_GTK_TREE_MODEL_ROW -}; - -#ifdef GTK3_DRAG_DROP -static GtkTargetEntry wl_dnd_targets[] = { - { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP, TARGET_GTK_TREE_MODEL_ROW }, -}; -#endif /* GTK3_DRAG_DROP */ - - /************************************************************************//** Add drag&drop target ****************************************************************************/ -void add_worklist_dnd_target(GtkWidget *w) +void add_worklist_dnd_target(GtkWidget *w, + gboolean (drag_drop_cb) + (GtkDropTarget *target, const GValue *value, + double x, double y, gpointer data), + gpointer data) { -#ifdef GTK3_DRAG_DROP - gtk_drag_dest_set(w, GTK_DEST_DEFAULT_ALL, - wl_dnd_targets, G_N_ELEMENTS(wl_dnd_targets), - GDK_ACTION_COPY); -#endif /* GTK3_DRAG_DROP */ + GtkDropTarget *dnd_tgt; + + dnd_tgt = gtk_drop_target_new(G_TYPE_INT, GDK_ACTION_COPY); + + g_signal_connect(dnd_tgt, "drop", G_CALLBACK(drag_drop_cb), data); + + gtk_widget_add_controller(w, GTK_EVENT_CONTROLLER(dnd_tgt)); } /************************************************************************//** diff --git a/client/gui-gtk-4.0/wldlg.h b/client/gui-gtk-4.0/wldlg.h index 764a499b68..7e6cd239bf 100644 --- a/client/gui-gtk-4.0/wldlg.h +++ b/client/gui-gtk-4.0/wldlg.h @@ -33,8 +33,12 @@ GtkWidget *create_worklist(void); void reset_city_worklist(GtkWidget *editor, struct city *pcity); void refresh_worklist(GtkWidget *editor); -void add_worklist_dnd_target(GtkWidget *w); +void add_worklist_dnd_target(GtkWidget *w, + gboolean (drag_drop_cb) + (GtkDropTarget *target, const GValue *value, + double x, double y, gpointer data), + gpointer data); void blank_max_unit_size(void); -#endif /* FC__WLDLG_H */ +#endif /* FC__WLDLG_H */ -- 2.39.0