[gtk/dnd-gestures-2: 121/242] gtk-demo: Convert clipboard demo to GtkDropTarget



commit 16dc52565a3c55458ec6cd1ce6c81726b23e163e
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jan 1 13:13:39 2020 -0500

    gtk-demo: Convert clipboard demo to GtkDropTarget

 demos/gtk-demo/clipboard.c | 74 +++++++++++++++++++++++++++++++---------------
 1 file changed, 50 insertions(+), 24 deletions(-)
---
diff --git a/demos/gtk-demo/clipboard.c b/demos/gtk-demo/clipboard.c
index 5cec527655..f60dcca83d 100644
--- a/demos/gtk-demo/clipboard.c
+++ b/demos/gtk-demo/clipboard.c
@@ -143,19 +143,45 @@ get_texture (GValue   *value,
 }
 
 static void
-drag_data_received (GtkWidget        *widget,
-                    GdkDrop          *drop,
-                    GtkSelectionData *selection_data,
-                    gpointer          data)
+got_texture (GObject *source,
+             GAsyncResult *result,
+             gpointer data)
 {
-  if (gtk_selection_data_get_length (selection_data) > 0)
+  GdkDrop *drop = GDK_DROP (source);
+  GtkWidget *image = data;
+  const GValue *value;
+  GError *error = NULL;
+
+  value = gdk_drop_read_value_finish (drop, result, &error);
+  if (value)
+    {
+      GdkTexture *texture = g_value_get_object (value);
+      gtk_image_set_from_paintable (GTK_IMAGE (image), GDK_PAINTABLE (texture));
+    }
+  else
     {
-      GdkTexture *texture;
+      g_print ("Failed to get data: %s\n", error->message);
+      g_error_free (error);
+    }
+}
 
-      texture = gtk_selection_data_get_texture (selection_data);
-      gtk_image_set_from_paintable (GTK_IMAGE (data), GDK_PAINTABLE (texture));
-      g_object_unref (texture);
+static gboolean
+drag_drop (GtkDropTarget *dest,
+           int            x,
+           int            y,
+           GtkWidget     *widget)
+{
+  GdkDrop *drop;
+
+  drop = gtk_drop_target_get_drop (dest);
+
+  if (gdk_drop_has_value (drop, GDK_TYPE_TEXTURE))
+    {
+      gdk_drop_read_value_async (drop, GDK_TYPE_TEXTURE, G_PRIORITY_DEFAULT, NULL, got_texture, widget);
+      return TRUE;
     }
+
+  return FALSE;
 }
 
 static void
@@ -166,12 +192,8 @@ copy_image (GSimpleAction *action,
   GdkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (data));
   GdkPaintable *paintable = get_image_paintable (GTK_IMAGE (data));
 
-  g_print ("copy image\n");
   if (GDK_IS_TEXTURE (paintable))
-    {
-g_print ("set clipboard\n");
-      gdk_clipboard_set_texture (clipboard, GDK_TEXTURE (paintable));
-    }
+    gdk_clipboard_set_texture (clipboard, GDK_TEXTURE (paintable));
 
   if (paintable)
     g_object_unref (paintable);
@@ -244,6 +266,8 @@ do_clipboard (GtkWidget *do_widget)
       GActionGroup *actions;
       GdkContentProvider *content = NULL;
       GtkDragSource *source;
+      GtkDropTarget *dest;
+      GdkContentFormats *formats;
 
       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
       gtk_window_set_display (GTK_WINDOW (window),
@@ -313,11 +337,12 @@ do_clipboard (GtkWidget *do_widget)
       g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
 
       /* accept drops on image */
-      gtk_drag_dest_set (image, GTK_DEST_DEFAULT_ALL,
-                         NULL, GDK_ACTION_COPY);
-      gtk_drag_dest_add_image_targets (image);
-      g_signal_connect (image, "drag-data-received",
-                        G_CALLBACK (drag_data_received), image);
+      formats = gdk_content_formats_new (NULL, 0);
+      formats = gtk_content_formats_add_image_targets (formats, FALSE);
+      dest = gtk_drop_target_new (GTK_DEST_DEFAULT_MOTION|GTK_DEST_DEFAULT_HIGHLIGHT, formats, 
GDK_ACTION_COPY);
+      gdk_content_formats_unref (formats);
+      g_signal_connect (dest, "drag-drop", G_CALLBACK (drag_drop), image);
+      gtk_drop_target_attach (dest, image);
 
       /* context menu on image */
       gesture = gtk_gesture_click_new ();
@@ -345,11 +370,12 @@ do_clipboard (GtkWidget *do_widget)
       gtk_drag_source_attach (source, image, GDK_BUTTON1_MASK);
 
       /* accept drops on image */
-      gtk_drag_dest_set (image, GTK_DEST_DEFAULT_ALL,
-                         NULL, GDK_ACTION_COPY);
-      gtk_drag_dest_add_image_targets (image);
-      g_signal_connect (image, "drag-data-received",
-                        G_CALLBACK (drag_data_received), image);
+      formats = gdk_content_formats_new (NULL, 0);
+      formats = gtk_content_formats_add_image_targets (formats, FALSE);
+      dest = gtk_drop_target_new (GTK_DEST_DEFAULT_MOTION|GTK_DEST_DEFAULT_HIGHLIGHT, formats, 
GDK_ACTION_COPY);
+      gdk_content_formats_unref (formats);
+      g_signal_connect (dest, "drag-drop", G_CALLBACK (drag_drop), image);
+      gtk_drop_target_attach (dest, image);
 
       /* context menu on image */
       gesture = gtk_gesture_click_new ();


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