[gnome-todo/wip/gbsneto/dnd-fixups: 4/6] task-row: Use real, unscaled widget in DnD



commit 89ba2be6d84bd0a1b427d3db0914f350979ca887
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Oct 31 16:28:48 2017 -0200

    task-row: Use real, unscaled widget in DnD

 data/ui/task-row.ui |  1 +
 src/gtd-task-row.c  | 86 +++++++++++++++++++++++++----------------------------
 2 files changed, 42 insertions(+), 45 deletions(-)
---
diff --git a/data/ui/task-row.ui b/data/ui/task-row.ui
index 12e9325..d959045 100644
--- a/data/ui/task-row.ui
+++ b/data/ui/task-row.ui
@@ -44,6 +44,7 @@
                     <property name="visible">True</property>
                     <signal name="button-press-event" handler="button_press_event" object="GtdTaskRow" 
swapped="no" />
                     <signal name="drag-begin" handler="drag_begin_cb" object="GtdTaskRow" swapped="no" />
+                    <signal name="drag-end" handler="drag_end_cb" object="GtdTaskRow" swapped="no" />
                     <signal name="drag-failed" handler="drag_failed_cb" object="GtdTaskRow" swapped="no" />
                     <signal name="enter-notify-event" handler="mouse_over_dnd_event_cb" object="GtdTaskRow" 
swapped="no" />
                     <signal name="leave-notify-event" handler="mouse_out_dnd_event_cb" object="GtdTaskRow" 
swapped="no" />
diff --git a/src/gtd-task-row.c b/src/gtd-task-row.c
index 61277a0..05212ac 100644
--- a/src/gtd-task-row.c
+++ b/src/gtd-task-row.c
@@ -89,6 +89,18 @@ static guint signals[NUM_SIGNALS] = { 0, };
  * Auxiliary methods
  */
 
+static GtkWidget*
+create_transient_row (GtdTaskRow *self)
+{
+  GtdTaskRow *new_row;
+
+  new_row = GTD_TASK_ROW (gtd_task_row_new (self->task));
+  gtk_revealer_set_transition_duration (new_row->revealer, 0);
+  gtk_revealer_set_reveal_child (new_row->revealer, TRUE);
+
+  return GTK_WIDGET (new_row);
+}
+
 static void
 set_cursor (GtkWidget   *widget,
             const gchar *cursor_name)
@@ -111,34 +123,6 @@ set_cursor (GtkWidget   *widget,
   g_clear_object (&cursor);
 }
 
-static cairo_surface_t*
-get_dnd_icon (GtdTaskRow *self)
-{
-  cairo_surface_t *surface;
-  GtkWidget *widget;
-  cairo_t *cr;
-  gint real_x;
-  gint real_y;
-
-  widget = GTK_WIDGET (self);
-  gtk_widget_translate_coordinates (self->dnd_event_box,
-                                    widget,
-                                    self->clicked_x,
-                                    self->clicked_y,
-                                    &real_x,
-                                    &real_y);
-
-  /* Make it transparent */
-  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
-                                        gtk_widget_get_allocated_width (widget),
-                                        gtk_widget_get_allocated_height (widget));
-
-  cr = cairo_create (surface);
-  gtk_widget_draw (widget, cr);
-
-  return surface;
-}
-
 
 /*
  * Callbacks
@@ -215,42 +199,53 @@ button_press_event (GtkWidget      *widget,
 }
 
 static void
-drag_begin_cb (GtkWidget      *widget,
+drag_begin_cb (GtkWidget      *dnd_event_box,
                GdkDragContext *context,
                GtdTaskRow     *self)
 {
-  cairo_surface_t *surface;
-
+  GtkWidget *widget, *new_row;
   gint real_x;
   gint real_y;
 
   widget = GTK_WIDGET (self);
-  gtk_widget_translate_coordinates (self->dnd_event_box,
-                                    GTK_WIDGET (self),
+
+  gtk_widget_translate_coordinates (dnd_event_box,
+                                    widget,
                                     self->clicked_x,
                                     self->clicked_y,
                                     &real_x,
                                     &real_y);
 
-  surface = get_dnd_icon (self);
-
   set_cursor (widget, "grabbing");
 
-  gtk_drag_set_icon_surface (context, surface);
-  gdk_drag_context_set_hotspot (context, self->clicked_x, self->clicked_y);
+  /*
+   * gtk_drag_set_icon_widget() inserts the row in a different GtkWindow, so
+   * we have to create a new, transient row.
+   */
+  new_row = create_transient_row (self);
 
-  g_message ("Hotspot → %d, %d", real_x, real_y);
+  gtk_widget_set_size_request (new_row,
+                               gtk_widget_get_allocated_width (widget),
+                               gtk_widget_get_allocated_height (widget));
 
-  gtk_widget_hide (GTK_WIDGET (self));
+  gtk_drag_set_icon_widget (context, new_row, real_x, real_y);
+
+  gtk_widget_hide (widget);
+}
 
-  g_clear_pointer (&surface, cairo_surface_destroy);
+static void
+drag_end_cb (GtkWidget      *dnd_event_box,
+             GdkDragContext *context,
+             GtdTaskRow     *self)
+{
+  gtk_widget_show (GTK_WIDGET (self));
 }
 
 static gboolean
-drag_failed_cb (GtkWidget      *widget,
-                GdkDragContext *context,
-                GtkDragResult  result,
-                GtdTaskRow     *self)
+drag_failed_cb (GtkWidget      *widget,
+                GdkDragContext *context,
+                GtkDragResult   result,
+                GtdTaskRow     *self)
 {
   gtk_widget_show (GTK_WIDGET (self));
 
@@ -631,6 +626,7 @@ gtd_task_row_class_init (GtdTaskRowClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, button_press_event);
   gtk_widget_class_bind_template_callback (widget_class, complete_check_toggled_cb);
   gtk_widget_class_bind_template_callback (widget_class, drag_begin_cb);
+  gtk_widget_class_bind_template_callback (widget_class, drag_end_cb);
   gtk_widget_class_bind_template_callback (widget_class, drag_failed_cb);
   gtk_widget_class_bind_template_callback (widget_class, mouse_out_event_cb);
   gtk_widget_class_bind_template_callback (widget_class, mouse_out_dnd_event_cb);
@@ -655,7 +651,7 @@ gtd_task_row_init (GtdTaskRow *self)
                        GDK_BUTTON1_MASK,
                        NULL,
                        0,
-                       GDK_ACTION_COPY);
+                       GDK_ACTION_MOVE);
 }
 
 GtkWidget*


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