[evince/wip/gpoo/gtk4-rebase-port: 51/65] ev-view: bring back the DnD support




commit 481da8b06a75e33d1818fb8a76a968e3c23a1b33
Author: Qiu Wenbo <qiuwenbo kylinos com cn>
Date:   Sun Aug 22 15:39:36 2021 +0800

    ev-view: bring back the DnD support
    
    Signed-off-by: Qiu Wenbo <qiuwenbo kylinos com cn>

 libview/ev-view.c | 101 ++++++++++++++++++++----------------------------------
 1 file changed, 37 insertions(+), 64 deletions(-)
---
diff --git a/libview/ev-view.c b/libview/ev-view.c
index 0278d8b2c..dd9eda6e9 100644
--- a/libview/ev-view.c
+++ b/libview/ev-view.c
@@ -5784,79 +5784,46 @@ ev_view_remove_all_form_fields (EvView *view)
        }
 }
 
-#if 0
 /*** Drag and Drop ***/
-static void
-ev_view_drag_data_get (GtkWidget        *widget,
-                      GdkDragContext   *context,
-                      GtkSelectionData *selection_data,
-                      guint             info,
-                      guint             time)
-{
-       EvView *view = EV_VIEW (widget);
-
-       switch (info) {
-               case TARGET_DND_TEXT:
-                       if (EV_IS_SELECTION (view->document) &&
-                           view->selection_info.selections) {
-                               gchar *text;
-
-                               text = get_selected_text (view);
-                               gtk_selection_data_set_text (selection_data,
-                                                            text,
-                                                            strlen (text));
-                               g_free (text);
-                       }
-                       break;
-               case TARGET_DND_IMAGE:
-                       if (view->image_dnd_info.image) {
-                               GdkPixbuf *pixbuf;
+static GdkContentProvider *
+on_drag_prepare (GtkDragSource *self,
+                gdouble         x,
+                gdouble         y,
+                EvView         *view)
+{
+       EvImage *image;
+       GdkPixbuf *pixbuf;
+       const char *tmp_uri;
+       GFile *file;
+
+       if (view->selection_info.in_select)
+               return NULL;
 
-                               ev_document_doc_mutex_lock ();
-                               pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document),
-                                                                      view->image_dnd_info.image);
-                               ev_document_doc_mutex_unlock ();
+       if (EV_IS_SELECTION (view->document) && view->selection_info.in_drag &&
+           location_in_selected_text (view, x + view->scroll_x, y + view->scroll_y)) {
+               gchar *text = get_selected_text (view);
 
-                               gtk_selection_data_set_pixbuf (selection_data, pixbuf);
-                               g_object_unref (pixbuf);
-                       }
-                       break;
-               case TARGET_DND_URI:
-                       if (view->image_dnd_info.image) {
-                               GdkPixbuf   *pixbuf;
-                               const gchar *tmp_uri;
-                               gchar       *uris[2];
+               return gdk_content_provider_new_for_bytes ("text/plain",
+                               g_bytes_new_take (text, strlen (text)));
+       }
 
-                               ev_document_doc_mutex_lock ();
-                               pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document),
-                                                                      view->image_dnd_info.image);
-                               ev_document_doc_mutex_unlock ();
+       if (!location_in_text (view, x + view->scroll_x, y + view->scroll_y) &&
+                                  (image = ev_view_get_image_at_location (view, x, y))) {
+               ev_document_doc_mutex_lock ();
+               pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (view->document), image);
+               ev_document_doc_mutex_unlock ();
 
-                               tmp_uri = ev_image_save_tmp (view->image_dnd_info.image, pixbuf);
-                               g_object_unref (pixbuf);
+               tmp_uri = ev_image_save_tmp (image, pixbuf);
+               file = g_file_new_for_uri (tmp_uri);
 
-                               uris[0] = (gchar *)tmp_uri;
-                                uris[1] = NULL;
-                               gtk_selection_data_set_uris (selection_data, uris);
-                       }
+               return gdk_content_provider_new_union ((GdkContentProvider *[2]) {
+                               gdk_content_provider_new_typed (G_TYPE_FILE, file),
+                               gdk_content_provider_new_typed (GDK_TYPE_PIXBUF, pixbuf),
+                               }, 2);
        }
-}
-
-static gboolean
-ev_view_drag_motion (GtkWidget      *widget,
-                    GdkDragContext *context,
-                    gint            x,
-                    gint            y,
-                    guint           time)
-{
-       if (gtk_drag_get_source_widget (context) == widget)
-               gdk_drag_status (context, 0, time);
-       else
-               gdk_drag_status (context, gdk_drag_context_get_suggested_action (context), time);
 
-       return TRUE;
+       return NULL;
 }
-#endif
 
 static gboolean
 selection_update_idle_cb (EvView *view)
@@ -8267,6 +8234,12 @@ ev_view_init (EvView *view)
        g_signal_connect (controller, "drag-end",
                          G_CALLBACK (on_middle_clicked_drag_end), view);
        gtk_widget_add_controller (widget, controller);
+
+       /* DnD for text selection and image */
+       controller = GTK_EVENT_CONTROLLER (gtk_drag_source_new ());
+       g_signal_connect (controller, "prepare",
+                          G_CALLBACK (on_drag_prepare), view);
+       gtk_widget_add_controller (widget, controller);
 }
 
 /*** Callbacks ***/


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