[gtk/dnd-gestures-2: 49/100] testdnd2: Port to GtkDropTarget



commit 815f15b2c3a01c1f09cbf1ad1f29d9d0e9cc1742
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jan 2 01:31:14 2020 -0500

    testdnd2: Port to GtkDropTarget

 tests/testdnd2.c | 75 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 39 insertions(+), 36 deletions(-)
---
diff --git a/tests/testdnd2.c b/tests/testdnd2.c
index a33afb172e..8253a95f6f 100644
--- a/tests/testdnd2.c
+++ b/tests/testdnd2.c
@@ -1,3 +1,4 @@
+#include <unistd.h>
 #include <gtk/gtk.h>
 
 static GdkPaintable *
@@ -37,20 +38,6 @@ enum {
   BOTTOM_RIGHT
 };
 
-static void
-update_dest_target_list (GtkWidget *image)
-{
-  GdkContentFormats *target_list;
-
-  target_list = gdk_content_formats_new (NULL, 0);
-  target_list = gtk_content_formats_add_image_targets (target_list, FALSE);
-  target_list = gtk_content_formats_add_text_targets (target_list);
-
-  gtk_drag_dest_set_target_list (image, target_list);
-
-  gdk_content_formats_unref (target_list);
-}
-
 void
 image_drag_data_get (GtkWidget        *widget,
                      GdkDrag          *drag,
@@ -84,31 +71,45 @@ image_drag_data_get (GtkWidget        *widget,
 }
 
 static void
-image_drag_data_received (GtkWidget        *widget,
-                          GdkDrop          *drop,
-                          GtkSelectionData *selection_data,
-                          gpointer          data)
+got_texture (GObject *source,
+             GAsyncResult *result,
+             gpointer data)
 {
-  GdkTexture *texture;
-  gchar *text;
-
-  if (gtk_selection_data_get_length (selection_data) == 0)
-    return;
+  GdkDrop *drop = GDK_DROP (source);
+  GtkWidget *image = data;
+  const GValue *value;
+  GError *error = NULL;
 
-  texture = gtk_selection_data_get_texture (selection_data);
-  if (texture)
+  value = gdk_drop_read_value_finish (drop, result, &error);
+  if (value)
     {
-      gtk_image_set_from_paintable (GTK_IMAGE (widget), GDK_PAINTABLE (texture));
-      g_object_unref (texture);
-      return;
+      GdkTexture *texture = g_value_get_object (value);
+      gtk_image_set_from_paintable (GTK_IMAGE (image), GDK_PAINTABLE (texture));
+      gdk_drop_finish (drop, GDK_ACTION_COPY);
     }
+  else
+    {
+      g_print ("Failed to get data: %s\n", error->message);
+      g_error_free (error);
+      gdk_drop_finish (drop, 0);
+    }
+}
+
+static gboolean
+image_drag_drop (GtkDropTarget    *dest,
+                 int               x,
+                 int               y,
+                 gpointer          data)
+{
+  GdkDrop *drop = gtk_drop_target_get_drop (dest);
 
-  text = (gchar *)gtk_selection_data_get_text (selection_data);
-  if (text)
+  if (gdk_drop_has_value (drop, GDK_TYPE_TEXTURE))
     {
-      gtk_image_set_from_icon_name (GTK_IMAGE (widget), text);
-      g_free (text);
+      gdk_drop_read_value_async (drop, GDK_TYPE_TEXTURE, G_PRIORITY_DEFAULT, NULL, got_texture, data);
+      return TRUE;
     }
+
+  return FALSE;
 }
 
 static void
@@ -215,6 +216,7 @@ make_image (const gchar *icon_name, int hotspot)
 {
   GtkWidget *image;
   GtkDragSource *source;
+  GtkDropTarget *dest;
   GdkContentFormats *formats;
   GdkContentProvider *content;
 
@@ -228,7 +230,6 @@ make_image (const gchar *icon_name, int hotspot)
   content = gdk_content_provider_new_with_formats (formats, get_data, image);
   source = gtk_drag_source_new (content, GDK_ACTION_COPY);
   g_object_unref (content);
-  gdk_content_formats_unref (formats);
   update_source_icon (source, icon_name, hotspot);
 
   g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), NULL);
@@ -236,9 +237,11 @@ make_image (const gchar *icon_name, int hotspot)
   g_signal_connect (source, "drag-failed", G_CALLBACK (drag_failed), NULL);
   gtk_drag_source_attach (source, image, GDK_BUTTON1_MASK);
 
-  gtk_drag_dest_set (image, GTK_DEST_DEFAULT_ALL, NULL, GDK_ACTION_COPY);
-  g_signal_connect (image, "drag-data-received", G_CALLBACK (image_drag_data_received), NULL);
-  update_dest_target_list (image);
+  dest = gtk_drop_target_new (GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT, formats, 
GDK_ACTION_COPY);
+  g_signal_connect (dest, "drag-drop", G_CALLBACK (image_drag_drop), image);
+  gtk_drop_target_attach (dest, image);
+
+  gdk_content_formats_unref (formats);
 
   return image;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]