[gtk/dnd-gestures-2: 5/34] colorbutton: Port to GtkDropTarget



commit 4c8fc0f8ff9f7816449dc6a06ebfde0e752ac37e
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jan 1 14:40:49 2020 -0500

    colorbutton: Port to GtkDropTarget

 gtk/gtkcolorbutton.c | 74 ++++++++++++++++++++--------------------------------
 1 file changed, 29 insertions(+), 45 deletions(-)
---
diff --git a/gtk/gtkcolorbutton.c b/gtk/gtkcolorbutton.c
index d615e857a0..967e2b6ba7 100644
--- a/gtk/gtkcolorbutton.c
+++ b/gtk/gtkcolorbutton.c
@@ -46,6 +46,7 @@
 #include "gtksnapshot.h"
 #include "gtkstylecontext.h"
 #include "gtkdragsource.h"
+#include "gtkdragdest.h"
 
 
 /**
@@ -122,12 +123,6 @@ static void gtk_color_button_get_property  (GObject          *object,
 static void gtk_color_button_clicked       (GtkButton        *button,
                                             gpointer          user_data);
 
-/* target side drag signals */
-static void gtk_color_button_drag_data_received (GtkWidget        *widget,
-                                                 GdkDrop          *drop,
-                                                 GtkSelectionData *selection_data,
-                                                 GtkColorButton   *button);
-
 
 static guint color_button_signals[LAST_SIGNAL] = { 0 };
 
@@ -237,44 +232,37 @@ gtk_color_button_class_init (GtkColorButtonClass *klass)
 }
 
 static void
-gtk_color_button_drag_data_received (GtkWidget        *widget,
-                                     GdkDrop          *drop,
-                                     GtkSelectionData *selection_data,
-                                     GtkColorButton   *button)
+got_color (GObject      *source,
+           GAsyncResult *result,
+           gpointer      data)
 {
-  GtkColorButtonPrivate *priv = gtk_color_button_get_instance_private (button);
-  gint length;
-  guint16 *dropped;
+  GdkDrop *drop = GDK_DROP (source);
+  const GValue *value;
 
-  length = gtk_selection_data_get_length (selection_data);
-
-  if (length < 0)
-    return;
-
-  /* We accept drops with the wrong format, since the KDE color
-   * chooser incorrectly drops application/x-color with format 8.
-   */
-  if (length != 8)
+  value = gdk_drop_read_value_finish (drop, result, NULL);
+  if (value)
     {
-      g_warning ("%s: Received invalid color data", G_STRFUNC);
-      return;
+      GdkRGBA *color = g_value_get_boxed (value);
+      gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (data), color);
+      gdk_drop_finish (drop, GDK_ACTION_COPY);
     }
+  else
+    gdk_drop_finish (drop, 0);
+}
 
+static gboolean
+gtk_color_button_drag_drop (GtkDropTarget  *dest,
+                            GtkColorButton *button)
+{
+  GdkDrop *drop = gtk_drop_target_get_drop (dest);
 
-  dropped = (guint16 *) gtk_selection_data_get_data (selection_data);
-
-  priv->rgba.red = dropped[0] / 65535.;
-  priv->rgba.green = dropped[1] / 65535.;
-  priv->rgba.blue = dropped[2] / 65535.;
-  priv->rgba.alpha = dropped[3] / 65535.;
-
-  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
-
-  g_signal_emit (button, color_button_signals[COLOR_SET], 0);
+  if (gdk_drop_has_value (drop, GDK_TYPE_RGBA))
+    {
+      gdk_drop_read_value_async (drop, GDK_TYPE_RGBA, G_PRIORITY_DEFAULT, NULL, got_color, button);
+      return TRUE;
+    }
 
-  g_object_freeze_notify (G_OBJECT (button));
-  g_object_notify (G_OBJECT (button), "rgba");
-  g_object_thaw_notify (G_OBJECT (button));
+  return FALSE;
 }
 
 static void
@@ -312,6 +300,7 @@ gtk_color_button_init (GtkColorButton *button)
   GdkContentFormats *targets;
   GdkContentProvider *content;
   GtkDragSource *source;
+  GtkDropTarget *dest;
 
   priv->button = gtk_button_new ();
   g_signal_connect (priv->button, "clicked", G_CALLBACK (gtk_color_button_clicked), button);
@@ -336,14 +325,9 @@ gtk_color_button_init (GtkColorButton *button)
   priv->use_alpha = FALSE;
 
   targets = gdk_content_formats_new (drop_types, G_N_ELEMENTS (drop_types));
-  gtk_drag_dest_set (priv->button,
-                     GTK_DEST_DEFAULT_MOTION |
-                     GTK_DEST_DEFAULT_HIGHLIGHT |
-                     GTK_DEST_DEFAULT_DROP,
-                     targets,
-                     GDK_ACTION_COPY);
-  g_signal_connect (priv->button, "drag-data-received",
-                    G_CALLBACK (gtk_color_button_drag_data_received), button);
+  dest = gtk_drop_target_new (GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT, targets, 
GDK_ACTION_COPY);
+  g_signal_connect (dest, "drag-drop", G_CALLBACK (gtk_color_button_drag_drop), button);
+  gtk_drop_target_attach (dest, GTK_WIDGET (button));
   gdk_content_formats_unref (targets);
 
   content = gdk_content_provider_new_with_callback (GDK_TYPE_RGBA, get_rgba_value, button);


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