testdnd: Convert to GtkDropTarget

This commit is contained in:
Matthias Clasen 2020-01-02 01:46:26 -05:00
parent 3f33a0ed27
commit 2fd9431f23

View File

@ -298,9 +298,8 @@ static const char *target_table[] = {
static guint n_targets = sizeof(target_table) / sizeof(target_table[0]); static guint n_targets = sizeof(target_table) / sizeof(target_table[0]);
void void
target_drag_leave (GtkWidget *widget, target_drag_leave (GtkDropTarget *dest,
GdkDrop *drop, GtkWidget *widget)
guint time)
{ {
g_print("leave\n"); g_print("leave\n");
have_drag = FALSE; have_drag = FALSE;
@ -308,11 +307,12 @@ target_drag_leave (GtkWidget *widget,
} }
gboolean gboolean
target_drag_motion (GtkWidget *widget, target_drag_motion (GtkDropTarget *dest,
GdkDrop *drop, int x,
gint x, int y,
gint y) GtkWidget *widget)
{ {
GdkDrop *drop = gtk_drop_target_get_drop (dest);
char *s; char *s;
if (!have_drag) if (!have_drag)
@ -329,12 +329,28 @@ target_drag_motion (GtkWidget *widget,
return TRUE; return TRUE;
} }
gboolean static void
target_drag_drop (GtkWidget *widget, got_text_in_target (GObject *object,
GdkDrop *drop, GAsyncResult *result,
gint x, gpointer data)
gint y)
{ {
char *str;
str = gdk_drop_read_text_finish (GDK_DROP (object), result, NULL);
if (str)
{
g_print ("Received \"%s\" in target\n", str);
g_free (str);
}
}
gboolean
target_drag_drop (GtkDropTarget *dest,
int x,
int y,
GtkWidget *widget)
{
GdkDrop *drop = gtk_drop_target_get_drop (dest);
GdkContentFormats *formats; GdkContentFormats *formats;
const char *format; const char *format;
@ -347,7 +363,7 @@ target_drag_drop (GtkWidget *widget,
format = gdk_content_formats_match_mime_type (formats, formats); format = gdk_content_formats_match_mime_type (formats, formats);
if (format) if (format)
{ {
gtk_drag_get_data (widget, drop, format); gdk_drop_read_text_async (drop, NULL, got_text_in_target, widget);
return TRUE; return TRUE;
} }
@ -373,39 +389,32 @@ action_make_unique (GdkDragAction action)
return 0; return 0;
} }
void static void
target_drag_data_received (GtkWidget *widget, got_text (GObject *object,
GdkDrop *drop, GAsyncResult *result,
GtkSelectionData *selection_data) gpointer data)
{ {
if (gtk_selection_data_get_length (selection_data) >= 0 && char *str;
gtk_selection_data_get_format (selection_data) == 8)
{
GdkDragAction action = gdk_drop_get_actions (drop);
g_print ("Received \"%s\" in trashcan\n", (gchar *) gtk_selection_data_get_data (selection_data));
action = action_make_unique (action);
gdk_drop_finish (drop, action);
return;
}
gdk_drop_finish (drop, 0); str = gdk_drop_read_text_finish (GDK_DROP (object), result, NULL);
if (str)
{
g_print ("Received \"%s\" in label\n", str);
g_free (str);
}
} }
void void
label_drag_data_received (GtkWidget *widget, label_drag_drop (GtkDropTarget *dest,
GdkDrop *drop, int x,
GtkSelectionData *selection_data) int y,
GtkWidget *widget)
{ {
if (gtk_selection_data_get_length (selection_data) >= 0 && GdkDrop *drop = gtk_drop_target_get_drop (dest);
gtk_selection_data_get_format (selection_data) == 8)
{
GdkDragAction action = action_make_unique (gdk_drop_get_actions (drop));
g_print ("Received \"%s\" in label\n", (gchar *) gtk_selection_data_get_data (selection_data));
gdk_drop_finish (drop, action);
return;
}
gdk_drop_finish (drop, 0); gdk_drop_read_text_async (drop, NULL, got_text, widget);
GdkDragAction action = action_make_unique (gdk_drop_get_actions (drop));
gdk_drop_finish (drop, action);
} }
/* The following is a rather elaborate example demonstrating/testing /* The following is a rather elaborate example demonstrating/testing
@ -431,10 +440,9 @@ popdown_cb (gpointer data)
} }
gboolean gboolean
popup_motion (GtkWidget *widget, popup_motion (GtkDropTarget *dest,
GdkDrop *drop, int x,
gint x, int y)
gint y)
{ {
if (!in_popup) if (!in_popup)
{ {
@ -451,8 +459,7 @@ popup_motion (GtkWidget *widget,
} }
void void
popup_leave (GtkWidget *widget, popup_leave (GtkDropTarget *dest)
GdkDrop *drop)
{ {
if (in_popup) if (in_popup)
{ {
@ -486,20 +493,18 @@ popup_cb (gpointer data)
for (j=0; j<3; j++) for (j=0; j<3; j++)
{ {
char buffer[128]; char buffer[128];
GtkDropTarget *dest;
g_snprintf(buffer, sizeof(buffer), "%d,%d", i, j); g_snprintf(buffer, sizeof(buffer), "%d,%d", i, j);
button = gtk_button_new_with_label (buffer); button = gtk_button_new_with_label (buffer);
gtk_widget_set_hexpand (button, TRUE); gtk_widget_set_hexpand (button, TRUE);
gtk_widget_set_vexpand (button, TRUE); gtk_widget_set_vexpand (button, TRUE);
gtk_grid_attach (GTK_GRID (grid), button, i, j, 1, 1); gtk_grid_attach (GTK_GRID (grid), button, i, j, 1, 1);
gtk_drag_dest_set (button, dest = gtk_drop_target_new (GTK_DEST_DEFAULT_ALL, targets, GDK_ACTION_COPY | GDK_ACTION_MOVE);
GTK_DEST_DEFAULT_ALL, g_signal_connect (dest, "drag-motion", G_CALLBACK (popup_motion), NULL);
targets, g_signal_connect (dest, "drag-leave", G_CALLBACK (popup_leave), NULL);
GDK_ACTION_COPY | GDK_ACTION_MOVE); gtk_drop_target_attach (dest, button);
g_signal_connect (button, "drag-motion",
G_CALLBACK (popup_motion), NULL);
g_signal_connect (button, "drag-leave",
G_CALLBACK (popup_leave), NULL);
} }
gtk_container_add (GTK_CONTAINER (popup_window), grid); gtk_container_add (GTK_CONTAINER (popup_window), grid);
gdk_content_formats_unref (targets); gdk_content_formats_unref (targets);
@ -518,10 +523,10 @@ popup_cb (gpointer data)
} }
gboolean gboolean
popsite_motion (GtkWidget *widget, popsite_motion (GtkDropTarget *dest,
GdkDrop *drop, int x,
gint x, int y,
gint y) GtkWidget *widget)
{ {
if (!popup_timer) if (!popup_timer)
popup_timer = g_timeout_add (500, popup_cb, NULL); popup_timer = g_timeout_add (500, popup_cb, NULL);
@ -530,8 +535,7 @@ popsite_motion (GtkWidget *widget,
} }
void void
popsite_leave (GtkWidget *widget, popsite_leave (GtkDropTarget *dest)
GdkDrop *drop)
{ {
if (popup_timer) if (popup_timer)
{ {
@ -568,6 +572,7 @@ main (int argc, char **argv)
GValue value = G_VALUE_INIT; GValue value = G_VALUE_INIT;
GtkDragSource *source; GtkDragSource *source;
GdkContentFormats *targets; GdkContentFormats *targets;
GtkDropTarget *dest;
test_init (); test_init ();
@ -590,13 +595,9 @@ main (int argc, char **argv)
label = gtk_label_new ("Drop Here\n"); label = gtk_label_new ("Drop Here\n");
targets = gdk_content_formats_new (target_table, n_targets - 1); /* no rootwin */ targets = gdk_content_formats_new (target_table, n_targets - 1); /* no rootwin */
gtk_drag_dest_set (label, dest = gtk_drop_target_new (GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT, targets, GDK_ACTION_COPY | GDK_ACTION_MOVE);
GTK_DEST_DEFAULT_ALL, g_signal_connect (dest, "drag-drop", G_CALLBACK (label_drag_drop), NULL);
targets, gtk_drop_target_attach (dest, label);
GDK_ACTION_COPY | GDK_ACTION_MOVE);
g_signal_connect (label, "drag_data_received",
G_CALLBACK( label_drag_data_received), NULL);
gtk_widget_set_hexpand (label, TRUE); gtk_widget_set_hexpand (label, TRUE);
gtk_widget_set_vexpand (label, TRUE); gtk_widget_set_vexpand (label, TRUE);
@ -604,38 +605,30 @@ main (int argc, char **argv)
label = gtk_label_new ("Popup\n"); label = gtk_label_new ("Popup\n");
gtk_drag_dest_set (label, dest = gtk_drop_target_new (GTK_DEST_DEFAULT_ALL, targets, GDK_ACTION_COPY | GDK_ACTION_MOVE);
GTK_DEST_DEFAULT_ALL, g_signal_connect (dest, "drag-motion", G_CALLBACK (popsite_motion), NULL);
targets, g_signal_connect (dest, "drag-leave", G_CALLBACK (popsite_leave), NULL);
GDK_ACTION_COPY | GDK_ACTION_MOVE); gtk_drop_target_attach (dest, label);
gtk_widget_set_hexpand (label, TRUE); gtk_widget_set_hexpand (label, TRUE);
gtk_widget_set_vexpand (label, TRUE); gtk_widget_set_vexpand (label, TRUE);
gtk_grid_attach (GTK_GRID (grid), label, 1, 1, 1, 1); gtk_grid_attach (GTK_GRID (grid), label, 1, 1, 1, 1);
g_signal_connect (label, "drag-motion",
G_CALLBACK (popsite_motion), NULL);
g_signal_connect (label, "drag-leave",
G_CALLBACK (popsite_leave), NULL);
gdk_content_formats_unref (targets); gdk_content_formats_unref (targets);
pixmap = gtk_image_new_from_pixbuf (trashcan_closed); pixmap = gtk_image_new_from_pixbuf (trashcan_closed);
gtk_drag_dest_set (pixmap, 0, NULL, 0); targets = gdk_content_formats_new (NULL, 0);
dest = gtk_drop_target_new (0, targets, 0);
g_signal_connect (dest, "drag-leave", G_CALLBACK (target_drag_leave), pixmap);
g_signal_connect (dest, "drag-motion", G_CALLBACK (target_drag_motion), pixmap);
g_signal_connect (dest, "drag-drop", G_CALLBACK (target_drag_drop), pixmap);
gtk_drop_target_attach (dest, pixmap);
gdk_content_formats_unref (targets);
gtk_widget_set_hexpand (pixmap, TRUE); gtk_widget_set_hexpand (pixmap, TRUE);
gtk_widget_set_vexpand (pixmap, TRUE); gtk_widget_set_vexpand (pixmap, TRUE);
gtk_grid_attach (GTK_GRID (grid), pixmap, 1, 0, 1, 1); gtk_grid_attach (GTK_GRID (grid), pixmap, 1, 0, 1, 1);
g_signal_connect (pixmap, "drag-leave",
G_CALLBACK (target_drag_leave), NULL);
g_signal_connect (pixmap, "drag-motion",
G_CALLBACK (target_drag_motion), NULL);
g_signal_connect (pixmap, "drag-drop",
G_CALLBACK (target_drag_drop), NULL);
g_signal_connect (pixmap, "drag-data-received",
G_CALLBACK (target_drag_data_received), NULL);
/* Drag site */ /* Drag site */