nautilus-canvas-dnd.c 59 KB
Newer Older
1
/* nautilus-canvas-dnd.c - Drag & drop handling for the canvas container widget.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  Copyright (C) 1999, 2000 Free Software Foundation
 *  Copyright (C) 2000 Eazel, Inc.
 *
 *  The Gnome Library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  The Gnome Library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with the Gnome Library; see the file COPYING.LIB.  If not,
 *  see <http://www.gnu.org/licenses/>.
 *
 *  Authors: Ettore Perazzoli <ettore@gnu.org>,
 *           Darin Adler <darin@bentspoon.com>,
 *           Andy Hertzfeld <andy@eazel.com>
 *           Pavel Cisler <pavel@eazel.com>
 *
 *
 *  XDS support: Benedikt Meurer <benny@xfce.org> (adapted by Amos Brocco <amos.brocco@unifr.ch>)
 *
 */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
29

30

31
#include <config.h>
32
#include <math.h>
33 34
#include <src/nautilus-window.h>

35
#include "nautilus-canvas-dnd.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
36

37
#include "nautilus-canvas-private.h"
38
#include "nautilus-global-preferences.h"
39
#include "nautilus-metadata.h"
40
#include "nautilus-selection-canvas-item.h"
Ramiro Estrugo's avatar
Ramiro Estrugo committed
41 42 43 44 45
#include <eel/eel-glib-extensions.h>
#include <eel/eel-graphic-effects.h>
#include <eel/eel-gtk-extensions.h>
#include <eel/eel-stock-dialogs.h>
#include <eel/eel-string.h>
46
#include <eel/eel-vfs-extensions.h>
47 48
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkx.h>
49
#include <gtk/gtk.h>
50
#include <glib/gi18n.h>
51

52 53
#include "nautilus-file-utilities.h"
#include "nautilus-file-changes-queue.h"
54 55
#include <stdio.h>
#include <string.h>
56

57
#define DEBUG_FLAG NAUTILUS_DEBUG_CANVAS_CONTAINER
58 59
#include "nautilus-debug.h"

60 61 62 63
static const GtkTargetEntry drag_types [] =
{
    { NAUTILUS_ICON_DND_GNOME_ICON_LIST_TYPE, 0, NAUTILUS_ICON_DND_GNOME_ICON_LIST },
    { NAUTILUS_ICON_DND_URI_LIST_TYPE, 0, NAUTILUS_ICON_DND_URI_LIST },
Ettore Perazzoli's avatar
Ettore Perazzoli committed
64 65
};

66 67 68 69 70 71 72 73 74 75 76
static const GtkTargetEntry drop_types [] =
{
    { NAUTILUS_ICON_DND_GNOME_ICON_LIST_TYPE, 0, NAUTILUS_ICON_DND_GNOME_ICON_LIST },
    /* prefer "_NETSCAPE_URL" over "text/uri-list" to satisfy web browsers. */
    { NAUTILUS_ICON_DND_NETSCAPE_URL_TYPE, 0, NAUTILUS_ICON_DND_NETSCAPE_URL },
    /* prefer XDS over "text/uri-list" */
    { NAUTILUS_ICON_DND_XDNDDIRECTSAVE_TYPE, 0, NAUTILUS_ICON_DND_XDNDDIRECTSAVE },     /* XDS Protocol Type */
    { NAUTILUS_ICON_DND_URI_LIST_TYPE, 0, NAUTILUS_ICON_DND_URI_LIST },
    { NAUTILUS_ICON_DND_RAW_TYPE, 0, NAUTILUS_ICON_DND_RAW },
    /* Must be last: */
    { NAUTILUS_ICON_DND_ROOTWINDOW_DROP_TYPE, 0, NAUTILUS_ICON_DND_ROOTWINDOW_DROP }
Ettore Perazzoli's avatar
Ettore Perazzoli committed
77
};
78 79
static void     stop_dnd_highlight (GtkWidget *widget);
static void     dnd_highlight_queue_redraw (GtkWidget *widget);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
80

81
static GtkTargetList *drop_types_list = NULL;
82
static GtkTargetList *drop_types_list_root = NULL;
83

84 85 86 87
static char *nautilus_canvas_container_find_drop_target (NautilusCanvasContainer *container,
                                                         GdkDragContext          *context,
                                                         int                      x,
                                                         int                      y,
88
                                                         gboolean                *icon_hit);
89

90
static EelCanvasItem *
91
create_selection_shadow (NautilusCanvasContainer *container,
92
                         GList                   *list)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
93
{
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    EelCanvasGroup *group;
    EelCanvas *canvas;
    int max_x, max_y;
    int min_x, min_y;
    GList *p;
    GtkAllocation allocation;

    if (list == NULL)
    {
        return NULL;
    }

    /* if we're only dragging a single item, don't worry about the shadow */
    if (list->next == NULL)
    {
        return NULL;
    }

    canvas = EEL_CANVAS (container);
    gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);

    /* Creating a big set of rectangles in the canvas can be expensive, so
     *  we try to be smart and only create the maximum number of rectangles
     *  that we will need, in the vertical/horizontal directions.  */

    max_x = allocation.width;
    min_x = -max_x;

    max_y = allocation.height;
    min_y = -max_y;

    /* Create a group, so that it's easier to move all the items around at
     *  once.  */
    group = EEL_CANVAS_GROUP
                (eel_canvas_item_new (EEL_CANVAS_GROUP (canvas->root),
                                      eel_canvas_group_get_type (),
                                      NULL));

    for (p = list; p != NULL; p = p->next)
    {
        NautilusDragSelectionItem *item;
        int x1, y1, x2, y2;

        item = p->data;

        if (!item->got_icon_position)
        {
            continue;
        }

        x1 = item->icon_x;
        y1 = item->icon_y;
        x2 = x1 + item->icon_width;
        y2 = y1 + item->icon_height;

        if (x2 >= min_x && x1 <= max_x && y2 >= min_y && y1 <= max_y)
        {
            eel_canvas_item_new
                (group,
                NAUTILUS_TYPE_SELECTION_CANVAS_ITEM,
                "x1", (double) x1,
                "y1", (double) y1,
                "x2", (double) x2,
                "y2", (double) y2,
                NULL);
        }
    }

    return EEL_CANVAS_ITEM (group);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
163 164
}

165 166 167
/* Set the affine instead of the x and y position.
 * Simple, and setting x and y was broken at one point.
 */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
168
static void
169
set_shadow_position (EelCanvasItem *shadow,
170 171
                     double         x,
                     double         y)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
172
{
173 174 175
    eel_canvas_item_set (shadow,
                         "x", x, "y", y,
                         NULL);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
176 177
}

178

179
/* Source-side handling of the drag. */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
180

181
/* iteration glue struct */
182 183 184 185 186
typedef struct
{
    gpointer iterator_context;
    NautilusDragEachSelectedItemDataGet iteratee;
    gpointer iteratee_data;
187
} CanvasGetDataBinderContext;
188

189 190
static void
canvas_rect_world_to_widget (EelCanvas *canvas,
191 192
                             EelDRect  *world_rect,
                             EelIRect  *widget_rect)
193
{
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    EelDRect window_rect;
    GtkAdjustment *hadj, *vadj;

    hadj = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (canvas));
    vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (canvas));

    eel_canvas_world_to_window (canvas,
                                world_rect->x0, world_rect->y0,
                                &window_rect.x0, &window_rect.y0);
    eel_canvas_world_to_window (canvas,
                                world_rect->x1, world_rect->y1,
                                &window_rect.x1, &window_rect.y1);
    widget_rect->x0 = (int) window_rect.x0 - gtk_adjustment_get_value (hadj);
    widget_rect->y0 = (int) window_rect.y0 - gtk_adjustment_get_value (vadj);
    widget_rect->x1 = (int) window_rect.x1 - gtk_adjustment_get_value (hadj);
    widget_rect->y1 = (int) window_rect.y1 - gtk_adjustment_get_value (vadj);
210 211 212 213
}

static void
canvas_widget_to_world (EelCanvas *canvas,
214 215 216 217
                        double     widget_x,
                        double     widget_y,
                        double    *world_x,
                        double    *world_y)
218
{
219 220 221 222
    eel_canvas_window_to_world (canvas,
                                widget_x + gtk_adjustment_get_value (gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (canvas))),
                                widget_y + gtk_adjustment_get_value (gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (canvas))),
                                world_x, world_y);
223 224
}

225
static gboolean
226 227
icon_get_data_binder (NautilusCanvasIcon *icon,
                      gpointer            data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
228
{
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    CanvasGetDataBinderContext *context;
    EelDRect world_rect;
    EelIRect widget_rect;
    char *uri;
    NautilusCanvasContainer *container;
    NautilusFile *file;

    context = (CanvasGetDataBinderContext *) data;

    g_assert (NAUTILUS_IS_CANVAS_CONTAINER (context->iterator_context));

    container = NAUTILUS_CANVAS_CONTAINER (context->iterator_context);

    world_rect = nautilus_canvas_item_get_icon_rectangle (icon->item);

    canvas_rect_world_to_widget (EEL_CANVAS (container), &world_rect, &widget_rect);

    uri = nautilus_canvas_container_get_icon_uri (container, icon);
    file = nautilus_file_get_by_uri (uri);
248 249
    g_free (uri);
    uri = nautilus_canvas_container_get_icon_activation_uri (container, icon);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

    if (uri == NULL)
    {
        g_warning ("no URI for one of the iterated icons");
        nautilus_file_unref (file);
        return TRUE;
    }

    widget_rect = eel_irect_offset_by (widget_rect,
                                       -container->details->dnd_info->drag_info.start_x,
                                       -container->details->dnd_info->drag_info.start_y);

    widget_rect = eel_irect_scale_by (widget_rect,
                                      1 / EEL_CANVAS (container)->pixels_per_unit);

    /* pass the uri, mouse-relative x/y and icon width/height */
    context->iteratee (uri,
                       (int) widget_rect.x0,
                       (int) widget_rect.y0,
                       widget_rect.x1 - widget_rect.x0,
                       widget_rect.y1 - widget_rect.y0,
                       context->iteratee_data);

    g_free (uri);
    nautilus_file_unref (file);

    return TRUE;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
277 278
}

279
/* Iterate over each selected icon in a NautilusCanvasContainer,
280 281
 * calling each_function on each.
 */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
282
static void
283
nautilus_canvas_container_each_selected_icon (NautilusCanvasContainer *container,
284 285
                                              gboolean (*each_function)(NautilusCanvasIcon *, gpointer),
                                              gpointer data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
286
{
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    GList *p;
    NautilusCanvasIcon *icon;

    for (p = container->details->icons; p != NULL; p = p->next)
    {
        icon = p->data;
        if (!icon->is_selected)
        {
            continue;
        }
        if (!each_function (icon, data))
        {
            return;
        }
    }
302 303
}

304
/* Adaptor function used with nautilus_canvas_container_each_selected_icon
305
 * to help iterate over all selected items, passing uris, x, y, w and h
306 307 308
 * values to the iteratee
 */
static void
309 310 311
each_icon_get_data_binder (NautilusDragEachSelectedItemDataGet iteratee,
                           gpointer                            iterator_context,
                           gpointer                            data)
312
{
313 314
    CanvasGetDataBinderContext context;
    NautilusCanvasContainer *container;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
315

316 317
    g_assert (NAUTILUS_IS_CANVAS_CONTAINER (iterator_context));
    container = NAUTILUS_CANVAS_CONTAINER (iterator_context);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
318

319 320 321 322
    context.iterator_context = iterator_context;
    context.iteratee = iteratee;
    context.iteratee_data = data;
    nautilus_canvas_container_each_selected_icon (container, icon_get_data_binder, &context);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
323 324
}

325
/* Called when the data for drag&drop is needed */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
326
static void
327 328 329 330 331 332
drag_data_get_callback (GtkWidget        *widget,
                        GdkDragContext   *context,
                        GtkSelectionData *selection_data,
                        guint             info,
                        guint32           time,
                        gpointer          data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
333
{
334 335 336 337 338 339 340 341 342 343 344 345
    NautilusDragInfo *drag_info;

    g_assert (widget != NULL);
    g_assert (NAUTILUS_IS_CANVAS_CONTAINER (widget));
    g_return_if_fail (context != NULL);

    /* Call common function from nautilus-drag that set's up
     * the selection data in the right format. Pass it means to
     * iterate all the selected icons.
     */
    drag_info = &(NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info->drag_info);
    nautilus_drag_drag_data_get_from_cache (drag_info->selection_cache, context, selection_data, info, time);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
346 347
}

348

Ettore Perazzoli's avatar
Ettore Perazzoli committed
349 350 351
/* Target-side handling of the drag.  */

static void
352
nautilus_canvas_container_position_shadow (NautilusCanvasContainer *container,
353 354
                                           int                      x,
                                           int                      y)
355
{
356 357
    EelCanvasItem *shadow;
    double world_x, world_y;
358

359 360 361 362 363
    shadow = container->details->dnd_info->shadow;
    if (shadow == NULL)
    {
        return;
    }
364

365 366
    canvas_widget_to_world (EEL_CANVAS (container), x, y,
                            &world_x, &world_y);
367

368 369
    set_shadow_position (shadow, world_x, world_y);
    eel_canvas_item_show (shadow);
370 371
}

372 373 374
static void
stop_cache_selection_list (NautilusDragInfo *drag_info)
{
375 376 377 378 379
    if (drag_info->file_list_info_handler)
    {
        nautilus_file_list_cancel_call_when_ready (drag_info->file_list_info_handler);
        drag_info->file_list_info_handler = NULL;
    }
380 381 382 383 384
}

static void
cache_selection_list (NautilusDragInfo *drag_info)
{
385
    GList *files;
386

387 388 389 390 391
    files = nautilus_drag_file_list_from_selection_list (drag_info->selection_list);
    nautilus_file_list_call_when_ready (files,
                                        NAUTILUS_FILE_ATTRIBUTE_INFO,
                                        drag_info->file_list_info_handler,
                                        NULL, NULL);
392

393
    g_list_free_full (files, g_object_unref);
394 395
}

396
static void
397 398 399 400
nautilus_canvas_container_dropped_canvas_feedback (GtkWidget        *widget,
                                                   GtkSelectionData *data,
                                                   int               x,
                                                   int               y)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
401
{
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    NautilusCanvasContainer *container;
    NautilusCanvasDndInfo *dnd_info;

    container = NAUTILUS_CANVAS_CONTAINER (widget);
    dnd_info = container->details->dnd_info;

    /* Delete old selection list. */
    stop_cache_selection_list (&dnd_info->drag_info);
    nautilus_drag_destroy_selection_list (dnd_info->drag_info.selection_list);
    dnd_info->drag_info.selection_list = NULL;

    /* Delete old shadow if any. */
    if (dnd_info->shadow != NULL)
    {
        /* FIXME bugzilla.gnome.org 42484:
         * Is a destroy really sufficient here? Who does the unref? */
        eel_canvas_item_destroy (dnd_info->shadow);
    }

    /* Build the selection list and the shadow. */
    dnd_info->drag_info.selection_list = nautilus_drag_build_selection_list (data);
    cache_selection_list (&dnd_info->drag_info);
    dnd_info->shadow = create_selection_shadow (container, dnd_info->drag_info.selection_list);
    nautilus_canvas_container_position_shadow (container, x, y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
426 427
}

428 429 430
static char *
get_direct_save_filename (GdkDragContext *context)
{
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    guchar *prop_text;
    gint prop_len;

    if (!gdk_property_get (gdk_drag_context_get_source_window (context), gdk_atom_intern (NAUTILUS_ICON_DND_XDNDDIRECTSAVE_TYPE, FALSE),
                           gdk_atom_intern ("text/plain", FALSE), 0, 1024, FALSE, NULL, NULL,
                           &prop_len, &prop_text))
    {
        return NULL;
    }

    /* Zero-terminate the string */
    prop_text = g_realloc (prop_text, prop_len + 1);
    prop_text[prop_len] = '\0';

    /* Verify that the file name provided by the source is valid */
    if (*prop_text == '\0' ||
        strchr ((const gchar *) prop_text, G_DIR_SEPARATOR) != NULL)
    {
        DEBUG ("Invalid filename provided by XDS drag site");
        g_free (prop_text);
        return NULL;
    }

    return (gchar *) prop_text;
455 456 457
}

static void
458 459 460 461 462
set_direct_save_uri (GtkWidget        *widget,
                     GdkDragContext   *context,
                     NautilusDragInfo *drag_info,
                     int               x,
                     int               y)
463
{
464 465 466 467 468 469 470 471 472 473 474
    GFile *base, *child;
    char *filename, *drop_target;
    gchar *uri;

    drag_info->got_drop_data_type = TRUE;
    drag_info->data_type = NAUTILUS_ICON_DND_XDNDDIRECTSAVE;

    uri = NULL;

    filename = get_direct_save_filename (context);
    drop_target = nautilus_canvas_container_find_drop_target (NAUTILUS_CANVAS_CONTAINER (widget),
475
                                                              context, x, y, NULL);
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

    if (drop_target && eel_uri_is_trash (drop_target))
    {
        g_free (drop_target);
        drop_target = NULL;         /* Cannot save to trash ...*/
    }

    if (filename != NULL && drop_target != NULL)
    {
        /* Resolve relative path */
        base = g_file_new_for_uri (drop_target);
        child = g_file_get_child (base, filename);
        uri = g_file_get_uri (child);
        g_object_unref (base);
        g_object_unref (child);

        /* Change the uri property */
        gdk_property_change (gdk_drag_context_get_source_window (context),
                             gdk_atom_intern (NAUTILUS_ICON_DND_XDNDDIRECTSAVE_TYPE, FALSE),
                             gdk_atom_intern ("text/plain", FALSE), 8,
                             GDK_PROP_MODE_REPLACE, (const guchar *) uri,
                             strlen (uri));

        drag_info->direct_save_uri = uri;
    }

    g_free (filename);
    g_free (drop_target);
504 505
}

506
/* FIXME bugzilla.gnome.org 47445: Needs to become a shared function */
507
static void
508 509 510 511 512
get_data_on_first_target_we_support (GtkWidget      *widget,
                                     GdkDragContext *context,
                                     guint32         time,
                                     int             x,
                                     int             y)
513
{
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    GtkTargetList *list;
    GdkAtom target;

    if (drop_types_list == NULL)
    {
        drop_types_list = gtk_target_list_new (drop_types,
                                               G_N_ELEMENTS (drop_types) - 1);
        gtk_target_list_add_text_targets (drop_types_list, NAUTILUS_ICON_DND_TEXT);
    }
    if (drop_types_list_root == NULL)
    {
        drop_types_list_root = gtk_target_list_new (drop_types,
                                                    G_N_ELEMENTS (drop_types));
        gtk_target_list_add_text_targets (drop_types_list_root, NAUTILUS_ICON_DND_TEXT);
    }

530
    list = drop_types_list;
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

    target = gtk_drag_dest_find_target (widget, context, list);
    if (target != GDK_NONE)
    {
        guint info;
        NautilusDragInfo *drag_info;
        gboolean found;

        drag_info = &(NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info->drag_info);

        found = gtk_target_list_find (list, target, &info);
        g_assert (found);

        /* Don't get_data for destructive ops */
        if ((info == NAUTILUS_ICON_DND_ROOTWINDOW_DROP ||
             info == NAUTILUS_ICON_DND_XDNDDIRECTSAVE) &&
547
            !drag_info->drop_occurred)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
        {
            /* We can't call get_data here, because that would
             *  make the source execute the rootwin action or the direct save */
            drag_info->got_drop_data_type = TRUE;
            drag_info->data_type = info;
        }
        else
        {
            if (info == NAUTILUS_ICON_DND_XDNDDIRECTSAVE)
            {
                set_direct_save_uri (widget, context, drag_info, x, y);
            }
            gtk_drag_get_data (GTK_WIDGET (widget), context,
                               target, time);
        }
    }
564 565
}

566
static void
567
nautilus_canvas_container_ensure_drag_data (NautilusCanvasContainer *container,
568 569
                                            GdkDragContext          *context,
                                            guint32                  time)
570
{
571
    NautilusCanvasDndInfo *dnd_info;
572

573
    dnd_info = container->details->dnd_info;
574

575 576 577 578
    if (!dnd_info->drag_info.got_drop_data_type)
    {
        get_data_on_first_target_we_support (GTK_WIDGET (container), context, time, 0, 0);
    }
579 580
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
581
static void
582 583 584
drag_end_callback (GtkWidget      *widget,
                   GdkDragContext *context,
                   gpointer        data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
585
{
586 587 588
    NautilusCanvasContainer *container;
    NautilusCanvasDndInfo *dnd_info;
    NautilusWindow *window;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
589

590 591 592
    container = NAUTILUS_CANVAS_CONTAINER (widget);
    window = NAUTILUS_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (container)));
    dnd_info = container->details->dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
593

594 595
    stop_cache_selection_list (&dnd_info->drag_info);
    nautilus_drag_destroy_selection_list (dnd_info->drag_info.selection_list);
596
    nautilus_drag_destroy_selection_list (dnd_info->drag_info.selection_cache);
597 598
    nautilus_drag_destroy_selection_list (container->details->dnd_source_info->selection_cache);
    dnd_info->drag_info.selection_list = NULL;
599
    dnd_info->drag_info.selection_cache = NULL;
600
    container->details->dnd_source_info->selection_cache = NULL;
601

602
    nautilus_window_end_dnd (window, context);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
603 604
}

605 606
static NautilusCanvasIcon *
nautilus_canvas_container_item_at (NautilusCanvasContainer *container,
607 608
                                   int                      x,
                                   int                      y)
609
{
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    GList *p;
    int size;
    EelDRect point;
    EelIRect canvas_point;

    /* build the hit-test rectangle. Base the size on the scale factor to ensure that it is
     * non-empty even at the smallest scale factor
     */

    size = MAX (1, 1 + (1 / EEL_CANVAS (container)->pixels_per_unit));
    point.x0 = x;
    point.y0 = y;
    point.x1 = x + size;
    point.y1 = y + size;

    for (p = container->details->icons; p != NULL; p = p->next)
    {
        NautilusCanvasIcon *icon;
        icon = p->data;

        eel_canvas_w2c (EEL_CANVAS (container),
                        point.x0,
                        point.y0,
                        &canvas_point.x0,
                        &canvas_point.y0);
        eel_canvas_w2c (EEL_CANVAS (container),
                        point.x1,
                        point.y1,
                        &canvas_point.x1,
                        &canvas_point.y1);
        if (nautilus_canvas_item_hit_test_rectangle (icon->item, canvas_point))
        {
            return icon;
        }
    }

    return NULL;
647 648 649
}

static char *
650
get_container_uri (NautilusCanvasContainer *container)
651
{
652
    char *uri;
653

654 655 656 657
    /* get the URI associated with the container */
    uri = NULL;
    g_signal_emit_by_name (container, "get-container-uri", &uri);
    return uri;
658 659 660
}

static gboolean
661
nautilus_canvas_container_selection_items_local (NautilusCanvasContainer *container,
662
                                                 GList                   *items)
663
{
664 665 666 667 668 669 670 671 672
    char *container_uri_string;
    gboolean result;

    /* must have at least one item */
    g_assert (items);

    /* get the URI associated with the container */
    container_uri_string = get_container_uri (container);

673 674
    result = nautilus_drag_items_local (container_uri_string, items);

675 676 677
    g_free (container_uri_string);

    return result;
678 679
}

680 681
/* handle dropped url */
static void
682 683 684 685 686
receive_dropped_netscape_url (NautilusCanvasContainer *container,
                              const char              *encoded_url,
                              GdkDragContext          *context,
                              int                      x,
                              int                      y)
687
{
688
    char *drop_target;
689

690 691 692 693
    if (encoded_url == NULL)
    {
        return;
    }
694

695
    drop_target = nautilus_canvas_container_find_drop_target (container, context, x, y, NULL);
696

697 698 699
    g_signal_emit_by_name (container, "handle-netscape-url",
                           encoded_url,
                           drop_target,
700
                           gdk_drag_context_get_selected_action (context));
701

702
    g_free (drop_target);
703 704
}

705 706
/* handle dropped uri list */
static void
707 708 709 710 711 712 713
receive_dropped_uri_list (NautilusCanvasContainer *container,
                          const char              *uri_list,
                          GdkDragContext          *context,
                          int                      x,
                          int                      y)
{
    char *drop_target;
714

715 716 717 718
    if (uri_list == NULL)
    {
        return;
    }
719

720
    drop_target = nautilus_canvas_container_find_drop_target (container, context, x, y, NULL);
721

722 723 724
    g_signal_emit_by_name (container, "handle-uri-list",
                           uri_list,
                           drop_target,
725
                           gdk_drag_context_get_selected_action (context));
726

727
    g_free (drop_target);
728 729
}

730 731
/* handle dropped text */
static void
732 733 734 735 736 737 738 739 740 741 742 743 744
receive_dropped_text (NautilusCanvasContainer *container,
                      const char              *text,
                      GdkDragContext          *context,
                      int                      x,
                      int                      y)
{
    char *drop_target;

    if (text == NULL)
    {
        return;
    }

745
    drop_target = nautilus_canvas_container_find_drop_target (container, context, x, y, NULL);
746 747 748 749

    g_signal_emit_by_name (container, "handle-text",
                           text,
                           drop_target,
750
                           gdk_drag_context_get_selected_action (context));
751 752

    g_free (drop_target);
753 754
}

James Dietrich's avatar
James Dietrich committed
755 756
/* handle dropped raw data */
static void
757 758 759 760 761 762 763
receive_dropped_raw (NautilusCanvasContainer *container,
                     const char              *raw_data,
                     int                      length,
                     const char              *direct_save_uri,
                     GdkDragContext          *context,
                     int                      x,
                     int                      y)
James Dietrich's avatar
James Dietrich committed
764
{
765
    char *drop_target;
James Dietrich's avatar
James Dietrich committed
766

767 768 769 770
    if (raw_data == NULL)
    {
        return;
    }
James Dietrich's avatar
James Dietrich committed
771

772
    drop_target = nautilus_canvas_container_find_drop_target (container, context, x, y, NULL);
James Dietrich's avatar
James Dietrich committed
773

774 775 776 777 778
    g_signal_emit_by_name (container, "handle-raw",
                           raw_data,
                           length,
                           drop_target,
                           direct_save_uri,
779
                           gdk_drag_context_get_selected_action (context));
James Dietrich's avatar
James Dietrich committed
780

781
    g_free (drop_target);
James Dietrich's avatar
James Dietrich committed
782 783
}

Pavel Cisler's avatar
Pavel Cisler committed
784 785 786
static int
auto_scroll_timeout_callback (gpointer data)
{
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
    NautilusCanvasContainer *container;
    GtkWidget *widget;
    float x_scroll_delta, y_scroll_delta;
    GdkRectangle exposed_area;
    GtkAllocation allocation;

    g_assert (NAUTILUS_IS_CANVAS_CONTAINER (data));
    widget = GTK_WIDGET (data);
    container = NAUTILUS_CANVAS_CONTAINER (widget);

    if (container->details->dnd_info->drag_info.waiting_to_autoscroll
        && container->details->dnd_info->drag_info.start_auto_scroll_in > g_get_monotonic_time ())
    {
        /* not yet */
        return TRUE;
    }

    container->details->dnd_info->drag_info.waiting_to_autoscroll = FALSE;

    nautilus_drag_autoscroll_calculate_delta (widget, &x_scroll_delta, &y_scroll_delta);
    if (x_scroll_delta == 0 && y_scroll_delta == 0)
    {
        /* no work */
        return TRUE;
    }

    /* Clear the old dnd highlight frame */
    dnd_highlight_queue_redraw (widget);

    if (!nautilus_canvas_container_scroll (container, (int) x_scroll_delta, (int) y_scroll_delta))
    {
        /* the scroll value got pinned to a min or max adjustment value,
         * we ended up not scrolling
         */
        return TRUE;
    }

    /* Make sure the dnd highlight frame is redrawn */
    dnd_highlight_queue_redraw (widget);

    /* update cached drag start offsets */
    container->details->dnd_info->drag_info.start_x -= x_scroll_delta;
    container->details->dnd_info->drag_info.start_y -= y_scroll_delta;

    /* Due to a glitch in GtkLayout, whe need to do an explicit draw of the exposed
     * area.
     * Calculate the size of the area we need to draw
     */
    gtk_widget_get_allocation (widget, &allocation);
    exposed_area.x = allocation.x;
    exposed_area.y = allocation.y;
    exposed_area.width = allocation.width;
    exposed_area.height = allocation.height;

    if (x_scroll_delta > 0)
    {
        exposed_area.x = exposed_area.width - x_scroll_delta;
    }
    else if (x_scroll_delta < 0)
    {
        exposed_area.width = -x_scroll_delta;
    }

    if (y_scroll_delta > 0)
    {
        exposed_area.y = exposed_area.height - y_scroll_delta;
    }
    else if (y_scroll_delta < 0)
    {
        exposed_area.height = -y_scroll_delta;
    }

    /* offset it to 0, 0 */
    exposed_area.x -= allocation.x;
    exposed_area.y -= allocation.y;

    gtk_widget_queue_draw_area (widget,
                                exposed_area.x,
                                exposed_area.y,
                                exposed_area.width,
                                exposed_area.height);

    return TRUE;
Pavel Cisler's avatar
Pavel Cisler committed
870 871 872
}

static void
873
set_up_auto_scroll_if_needed (NautilusCanvasContainer *container)
Pavel Cisler's avatar
Pavel Cisler committed
874
{
875 876 877 878
    nautilus_drag_autoscroll_start (&container->details->dnd_info->drag_info,
                                    GTK_WIDGET (container),
                                    auto_scroll_timeout_callback,
                                    container);
Pavel Cisler's avatar
Pavel Cisler committed
879 880 881
}

static void
882
stop_auto_scroll (NautilusCanvasContainer *container)
Pavel Cisler's avatar
Pavel Cisler committed
883
{
884
    nautilus_drag_autoscroll_stop (&container->details->dnd_info->drag_info);
Pavel Cisler's avatar
Pavel Cisler committed
885 886
}

887
static void
888
handle_nonlocal_move (NautilusCanvasContainer *container,
889 890 891
                      GdkDragAction            action,
                      const char              *target_uri,
                      gboolean                 icon_hit)
892
{
893
    GList *source_uris, *p;
894
    gboolean free_target_uri;
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914

    if (container->details->dnd_info->drag_info.selection_list == NULL)
    {
        return;
    }

    source_uris = NULL;
    for (p = container->details->dnd_info->drag_info.selection_list; p != NULL; p = p->next)
    {
        /* do a shallow copy of all the uri strings of the copied files */
        source_uris = g_list_prepend (source_uris, ((NautilusDragSelectionItem *) p->data)->uri);
    }
    source_uris = g_list_reverse (source_uris);

    free_target_uri = FALSE;

    /* start the copy */
    g_signal_emit_by_name (container, "move-copy-items",
                           source_uris,
                           target_uri,
915
                           action);
916 917 918 919 920 921 922

    if (free_target_uri)
    {
        g_free ((char *) target_uri);
    }

    g_list_free (source_uris);
923 924
}

925
static char *
926
nautilus_canvas_container_find_drop_target (NautilusCanvasContainer *container,
927 928 929
                                            GdkDragContext          *context,
                                            int                      x,
                                            int                      y,
930
                                            gboolean                *icon_hit)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
931
{
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
    NautilusCanvasIcon *drop_target_icon;
    double world_x, world_y;
    NautilusFile *file;
    char *icon_uri;
    char *container_uri;

    if (icon_hit)
    {
        *icon_hit = FALSE;
    }

    if (!container->details->dnd_info->drag_info.got_drop_data_type)
    {
        return NULL;
    }

    canvas_widget_to_world (EEL_CANVAS (container), x, y, &world_x, &world_y);

    /* FIXME bugzilla.gnome.org 42485:
     * These "can_accept_items" tests need to be done by
     * the canvas view, not here. This file is not supposed to know
     * that the target is a file.
     */

    /* Find the item we hit with our drop, if any */
    drop_target_icon = nautilus_canvas_container_item_at (container, world_x, world_y);
    if (drop_target_icon != NULL)
    {
        icon_uri = nautilus_canvas_container_get_icon_uri (container, drop_target_icon);
        if (icon_uri != NULL)
        {
            file = nautilus_file_get_by_uri (icon_uri);

            if (!nautilus_drag_can_accept_info (file,
                                                container->details->dnd_info->drag_info.data_type,
                                                container->details->dnd_info->drag_info.selection_list))
            {
                /* the item we dropped our selection on cannot accept the items,
                 * do the same thing as if we just dropped the items on the canvas
                 */
                drop_target_icon = NULL;
            }

            g_free (icon_uri);
            nautilus_file_unref (file);
        }
    }

    if (drop_target_icon == NULL)
    {
        if (icon_hit)
        {
            *icon_hit = FALSE;
        }

        container_uri = get_container_uri (container);

        if (container_uri != NULL)
        {
991 992 993 994 995 996 997
            gboolean can;
            file = nautilus_file_get_by_uri (container_uri);
            can = nautilus_drag_can_accept_info (file,
                                                 container->details->dnd_info->drag_info.data_type,
                                                 container->details->dnd_info->drag_info.selection_list);
            g_object_unref (file);
            if (!can)
998 999
            {
                g_free (container_uri);
1000
                container_uri = NULL;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
            }
        }

        return container_uri;
    }

    if (icon_hit)
    {
        *icon_hit = TRUE;
    }
    return nautilus_canvas_container_get_icon_drop_target_uri (container, drop_target_icon);
1012 1013 1014
}

static void
1015
nautilus_canvas_container_receive_dropped_icons (NautilusCanvasContainer *container,
1016 1017 1018
                                                 GdkDragContext          *context,
                                                 int                      x,
                                                 int                      y)
1019
{
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    char *drop_target;
    gboolean local_move_only;
    double world_x, world_y;
    gboolean icon_hit;
    GdkDragAction action, real_action;

    drop_target = NULL;

    if (container->details->dnd_info->drag_info.selection_list == NULL)
    {
        return;
    }

    real_action = gdk_drag_context_get_selected_action (context);

    if (real_action == GDK_ACTION_ASK)
    {
1037 1038
        action = GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_LINK;
        real_action = nautilus_drag_drop_action_ask (GTK_WIDGET (container), action);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    }

    if (real_action > 0)
    {
        eel_canvas_window_to_world (EEL_CANVAS (container),
                                    x + gtk_adjustment_get_value (gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (container))),
                                    y + gtk_adjustment_get_value (gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (container))),
                                    &world_x, &world_y);

        drop_target = nautilus_canvas_container_find_drop_target (container,
1049
                                                                  context, x, y, &icon_hit);
1050 1051 1052 1053 1054 1055 1056 1057

        local_move_only = FALSE;
        if (!icon_hit && real_action == GDK_ACTION_MOVE)
        {
            local_move_only = nautilus_canvas_container_selection_items_local
                                  (container, container->details->dnd_info->drag_info.selection_list);
        }

1058 1059
        /* If the move is local, there is nothing to do. */
        if (!local_move_only)
1060
        {
1061
            handle_nonlocal_move (container, real_action, drop_target, icon_hit);
1062 1063 1064 1065 1066 1067 1068
        }
    }

    g_free (drop_target);
    stop_cache_selection_list (&container->details->dnd_info->drag_info);
    nautilus_drag_destroy_selection_list (container->details->dnd_info->drag_info.selection_list);
    container->details->dnd_info->drag_info.selection_list = NULL;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1069 1070
}

1071 1072 1073 1074
NautilusDragInfo *
nautilus_canvas_dnd_get_drag_source_data (NautilusCanvasContainer *container,
                                          GdkDragContext          *context)
{
1075
    return container->details->dnd_source_info;
1076 1077
}

1078
static void
1079
nautilus_canvas_container_get_drop_action (NautilusCanvasContainer *container,
1080 1081 1082 1083
                                           GdkDragContext          *context,
                                           int                      x,
                                           int                      y,
                                           int                     *action)
1084
{
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    char *drop_target;
    gboolean icon_hit;
    double world_x, world_y;

    icon_hit = FALSE;
    if (!container->details->dnd_info->drag_info.got_drop_data_type)
    {
        /* drag_data_received_callback didn't get called yet */
        return;
    }

    /* find out if we're over an canvas */
    canvas_widget_to_world (EEL_CANVAS (container), x, y, &world_x, &world_y);
    *action = 0;

    drop_target = nautilus_canvas_container_find_drop_target (container,
1101
                                                              context, x, y, &icon_hit);
1102 1103 1104 1105 1106 1107 1108 1109 1110
    if (drop_target == NULL)
    {
        return;
    }

    /* case out on the type of object being dragged */
    switch (container->details->dnd_info->drag_info.data_type)
    {
        case NAUTILUS_ICON_DND_GNOME_ICON_LIST:
1111 1112
        {
            if (container->details->dnd_info->drag_info.selection_list != NULL)
1113
            {
1114 1115 1116 1117
                nautilus_drag_default_drop_action_for_icons (context, drop_target,
                                                             container->details->dnd_info->drag_info.selection_list,
                                                             0,
                                                             action);
1118
            }
1119 1120
        }
        break;
1121 1122

        case NAUTILUS_ICON_DND_URI_LIST:
1123 1124 1125 1126
        {
            *action = nautilus_drag_default_drop_action_for_uri_list (context, drop_target);
        }
        break;
1127 1128

        case NAUTILUS_ICON_DND_NETSCAPE_URL:
1129 1130 1131 1132
        {
            *action = nautilus_drag_default_drop_action_for_netscape_url (context);
        }
        break;
1133 1134

        case NAUTILUS_ICON_DND_ROOTWINDOW_DROP:
1135 1136 1137 1138
        {
            *action = gdk_drag_context_get_suggested_action (context);
        }
        break;
1139 1140 1141 1142

        case NAUTILUS_ICON_DND_TEXT:
        case NAUTILUS_ICON_DND_XDNDDIRECTSAVE:
        case NAUTILUS_ICON_DND_RAW:
1143 1144 1145 1146
        {
            *action = GDK_ACTION_COPY;
        }
        break;
1147 1148 1149
    }

    g_free (drop_target);
1150 1151
}

1152
static void
1153
set_drop_target (NautilusCanvasContainer *container,
1154
                 NautilusCanvasIcon      *icon)
1155
{
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    NautilusCanvasIcon *old_icon;

    /* Check if current drop target changed, update icon drop
     * higlight if needed.
     */
    old_icon = container->details->drop_target;
    if (icon == old_icon)
    {
        return;
    }

    /* Remember the new drop target for the next round. */
    container->details->drop_target = icon;
    nautilus_canvas_container_update_icon (container, old_icon);
    nautilus_canvas_container_update_icon (container, icon);
1171 1172 1173
}

static void
1174
nautilus_canvas_dnd_update_drop_target (NautilusCanvasContainer *container,
1175 1176 1177
                                        GdkDragContext          *context,
                                        int                      x,
                                        int                      y)
1178
{
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
    NautilusCanvasIcon *icon;
    NautilusFile *file;
    double world_x, world_y;
    char *uri;

    g_assert (NAUTILUS_IS_CANVAS_CONTAINER (container));

    canvas_widget_to_world (EEL_CANVAS (container), x, y, &world_x, &world_y);

    /* Find the item we hit with our drop, if any. */
    icon = nautilus_canvas_container_item_at (container, world_x, world_y);

    /* FIXME bugzilla.gnome.org 42485:
     * These "can_accept_items" tests need to be done by
     * the canvas view, not here. This file is not supposed to know
     * that the target is a file.
     */

    /* Find if target canvas accepts our drop. */
    if (icon != NULL)
    {
        uri = nautilus_canvas_container_get_icon_uri (container, icon);
        file = nautilus_file_get_by_uri (uri);
        g_free (uri);

        if (!nautilus_drag_can_accept_info (file,
                                            container->details->dnd_info->drag_info.data_type,
                                            container->details->dnd_info->drag_info.selection_list))
        {
            icon = NULL;
        }

        nautilus_file_unref (file);
    }

    set_drop_target (container, icon);
1215 1216
}

1217 1218 1219
static void
remove_hover_timer (NautilusCanvasDndInfo *dnd_info)
{
1220 1221 1222 1223 1224
    if (dnd_info->hover_id != 0)
    {
        g_source_remove (dnd_info->hover_id);
        dnd_info->hover_id = 0;
    }
1225 1226
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
1227
static void
1228
nautilus_canvas_container_free_drag_data (NautilusCanvasContainer *container)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1229
{
1230
    NautilusCanvasDndInfo *dnd_info;
1231

1232
    dnd_info = container->details->dnd_info;
1233

1234
    dnd_info->drag_info.got_drop_data_type = FALSE;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1235

1236 1237 1238 1239 1240
    if (dnd_info->shadow != NULL)
    {
        eel_canvas_item_destroy (dnd_info->shadow);
        dnd_info->shadow = NULL;
    }
1241

1242 1243 1244 1245 1246
    if (dnd_info->drag_info.selection_data != NULL)
    {
        gtk_selection_data_free (dnd_info->drag_info.selection_data);
        dnd_info->drag_info.selection_data = NULL;
    }
1247

1248 1249 1250 1251 1252
    if (dnd_info->drag_info.direct_save_uri != NULL)
    {
        g_free (dnd_info->drag_info.direct_save_uri);
        dnd_info->drag_info.direct_save_uri = NULL;
    }
1253

1254 1255 1256 1257
    g_free (dnd_info->target_uri);
    dnd_info->target_uri = NULL;

    remove_hover_timer (dnd_info);
1258 1259 1260
}

static void
1261 1262 1263 1264
drag_leave_callback (GtkWidget      *widget,
                     GdkDragContext *context,
                     guint32         time,
                     gpointer        data)
1265
{
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
    NautilusCanvasDndInfo *dnd_info;

    dnd_info = NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info;

    if (dnd_info->shadow != NULL)
    {
        eel_canvas_item_hide (dnd_info->shadow);
    }

    stop_dnd_highlight (widget);

    set_drop_target (NAUTILUS_CANVAS_CONTAINER (widget), NULL);
    stop_auto_scroll (NAUTILUS_CANVAS_CONTAINER (widget));
    nautilus_canvas_container_free_drag_data (NAUTILUS_CANVAS_CONTAINER (widget));
1280
}
1281

1282 1283
static void
drag_begin_callback (GtkWidget      *widget,
1284 1285
                     GdkDragContext *context,
                     gpointer        data)
1286
{
1287 1288 1289 1290 1291 1292 1293
    NautilusCanvasContainer *container;
    NautilusDragInfo *drag_info;
    NautilusWindow *window;
    cairo_surface_t *surface;
    double x1, y1, x2, y2, winx, winy;
    int x_offset, y_offset;
    int start_x, start_y;
1294
    GList *dragged_files;
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

    container = NAUTILUS_CANVAS_CONTAINER (widget);
    window = NAUTILUS_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (container)));

    start_x = container->details->dnd_info->drag_info.start_x +
              gtk_adjustment_get_value (gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (container)));
    start_y = container->details->dnd_info->drag_info.start_y +
              gtk_adjustment_get_value (gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (container)));

    /* create a pixmap and mask to drag with */
    surface = nautilus_canvas_item_get_drag_surface (container->details->drag_icon->item);

    /* compute the image's offset */
    eel_canvas_item_get_bounds (EEL_CANVAS_ITEM (container->details->drag_icon->item),
                                &x1, &y1, &x2, &y2);
    eel_canvas_world_to_window (EEL_CANVAS (container),
                                x1, y1, &winx, &winy);
    x_offset = start_x - winx;
    y_offset = start_y - winy;

    cairo_surface_set_device_offset (surface, -x_offset, -y_offset);
    gtk_drag_set_icon_surface (context, surface);
    cairo_surface_destroy (surface);

    /* cache the data at the beginning since the view may change */
    drag_info = &(container->details->dnd_info->drag_info);
    drag_info->selection_cache = nautilus_drag_create_selection_cache (widget,
                                                                       each_icon_get_data_binder);

    container->details->dnd_source_info->selection_cache = nautilus_drag_create_selection_cache (widget,
                                                                                                 each_icon_get_data_binder);

1327 1328 1329 1330 1331 1332
    dragged_files = nautilus_drag_file_list_from_selection_list (drag_info->selection_cache);
    if (nautilus_file_list_are_all_folders (dragged_files))
    {
        nautilus_window_start_dnd (window, context);
    }
    g_list_free_full (dragged_files, g_object_unref);
1333 1334
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
1335
void
1336
nautilus_canvas_dnd_begin_drag (NautilusCanvasContainer *container,
1337 1338 1339 1340 1341
                                GdkDragAction            actions,
                                int                      button,
                                GdkEventMotion          *event,
                                int                      start_x,
                                int                      start_y)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1342
{
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    NautilusCanvasDndInfo *dnd_info;
    NautilusDragInfo *dnd_source_info;

    g_return_if_fail (NAUTILUS_IS_CANVAS_CONTAINER (container));
    g_return_if_fail (event != NULL);

    dnd_info = container->details->dnd_info;
    container->details->dnd_source_info = g_new0 (NautilusDragInfo, 1);
    dnd_source_info = container->details->dnd_source_info;
    g_return_if_fail (dnd_info != NULL);

    /* Notice that the event is in bin_window coordinates, because of
     *  the way the canvas handles events.
     */
    dnd_info->drag_info.start_x = start_x -
                                  gtk_adjustment_get_value (gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (container)));
    dnd_info->drag_info.start_y = start_y -
                                  gtk_adjustment_get_value (gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (container)));

    dnd_source_info->source_actions = actions;
    /* start the drag */
    gtk_drag_begin_with_coordinates (GTK_WIDGET (container),
                                     dnd_info->drag_info.target_list,
                                     actions,
                                     button,
                                     (GdkEvent *) event,
                                     dnd_info->drag_info.start_x,
                                     dnd_info->drag_info.start_y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1371 1372
}

1373
static gboolean
1374 1375 1376
drag_highlight_draw (GtkWidget *widget,
                     cairo_t   *cr,
                     gpointer   user_data)
1377
{
1378 1379 1380
    gint width, height;
    GdkWindow *window;
    GtkStyleContext *style;
1381

1382 1383 1384
    window = gtk_widget_get_window (widget);
    width = gdk_window_get_width (window);
    height = gdk_window_get_height (window);
1385

1386
    style = gtk_widget_get_style_context (widget);
1387

1388 1389 1390
    gtk_style_context_save (style);
    gtk_style_context_add_class (style, GTK_STYLE_CLASS_DND);
    gtk_style_context_set_state (style, GTK_STATE_FLAG_FOCUSED);
1391

1392 1393 1394
    gtk_render_frame (style,
                      cr,
                      0, 0, width, height);
1395

1396 1397 1398
    gtk_style_context_restore (style);

    return FALSE;
1399 1400 1401 1402 1403 1404
}

/* Queue a redraw of the dnd highlight rect */
static void
dnd_highlight_queue_redraw (GtkWidget *widget)
{
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
    NautilusCanvasDndInfo *dnd_info;
    int width, height;
    GtkAllocation allocation;

    dnd_info = NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info;

    if (!dnd_info->highlighted)
    {
        return;
    }

    gtk_widget_get_allocation (widget, &allocation);
    width = allocation.width;
    height = allocation.height;

    /* we don't know how wide the shadow is exactly,
     * so we expose a 10-pixel wide border
     */
    gtk_widget_queue_draw_area (widget,
                                0, 0,
                                width, 10);
    gtk_widget_queue_draw_area (widget,
                                0, 0,
                                10, height);
    gtk_widget_queue_draw_area (widget,
                                0, height - 10,
                                width, 10);
    gtk_widget_queue_draw_area (widget,
                                width - 10, 0,
                                10, height);
1435 1436 1437 1438 1439
}

static void
start_dnd_highlight (GtkWidget *widget)
{
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
    NautilusCanvasDndInfo *dnd_info;

    dnd_info = NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info;

    if (!dnd_info->highlighted)
    {
        dnd_info->highlighted = TRUE;
        g_signal_connect_after (widget, "draw",
                                G_CALLBACK (drag_highlight_draw),
                                NULL);
        dnd_highlight_queue_redraw (widget);
    }
1452 1453 1454 1455 1456
}

static void
stop_dnd_highlight (GtkWidget *widget)
{
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
    NautilusCanvasDndInfo *dnd_info;

    dnd_info = NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info;

    if (dnd_info->highlighted)
    {
        g_signal_handlers_disconnect_by_func (widget,
                                              drag_highlight_draw,
                                              NULL);
        dnd_highlight_queue_redraw (widget);
        dnd_info->highlighted = FALSE;
    }
1469 1470
}

1471 1472 1473
static gboolean
hover_timer (gpointer user_data)
{
1474 1475
    NautilusCanvasContainer *container = user_data;
    NautilusCanvasDndInfo *dnd_info;
1476

1477
    dnd_info = container->details->dnd_info;
1478

1479
    dnd_info->hover_id = 0;
1480

1481
    g_signal_emit_by_name (container, "handle-hover", dnd_info->target_uri);
1482

1483
    return FALSE;
1484 1485 1486 1487
}

static void
check_hover_timer (NautilusCanvasContainer *container,
1488
                   const char              *uri)
1489
{
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
    NautilusCanvasDndInfo *dnd_info;
    GtkSettings *settings;
    guint timeout;

    dnd_info = container->details->dnd_info;

    if (g_strcmp0 (uri, dnd_info->target_uri) == 0)
    {
        return;
    }

    remove_hover_timer (dnd_info);

    settings = gtk_widget_get_settings (GTK_WIDGET (container));
    g_object_get (settings, "gtk-timeout-expand", &timeout, NULL);

    g_free (dnd_info->target_uri);
    dnd_info->target_uri = NULL;

    if (uri != NULL)
    {
        dnd_info->target_uri = g_strdup (uri);
        dnd_info->hover_id =
            gdk_threads_add_timeout (timeout,
                                     hover_timer,
                                     container);
    }
1517 1518
}

1519
static gboolean
1520 1521 1522 1523 1524
drag_motion_callback (GtkWidget      *widget,
                      GdkDragContext *context,
                      int             x,
                      int             y,
                      guint32         time)
1525
{
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
    int action;

    nautilus_canvas_container_ensure_drag_data (NAUTILUS_CANVAS_CONTAINER (widget), context, time);
    nautilus_canvas_container_position_shadow (NAUTILUS_CANVAS_CONTAINER (widget), x, y);
    nautilus_canvas_dnd_update_drop_target (NAUTILUS_CANVAS_CONTAINER (widget), context, x, y);
    set_up_auto_scroll_if_needed (NAUTILUS_CANVAS_CONTAINER (widget));
    /* Find out what the drop actions are based on our drag selection and
     * the drop target.
     */
    action = 0;
    nautilus_canvas_container_get_drop_action (NAUTILUS_CANVAS_CONTAINER (widget), context, x, y,
                                               &action);
    if (action != 0)
    {
        char *uri;
        uri = nautilus_canvas_container_find_drop_target (NAUTILUS_CANVAS_CONTAINER (widget),
1542
                                                          context, x, y, NULL);
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
        check_hover_timer (NAUTILUS_CANVAS_CONTAINER (widget), uri);
        g_free (uri);
        start_dnd_highlight (widget);
    }
    else
    {
        remove_hover_timer (NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info);
    }

    gdk_drag_status (context, action, time);

    return TRUE;
1555 1556
}

1557
static gboolean
1558 1559 1560 1561 1562 1563
drag_drop_callback (GtkWidget      *widget,
                    GdkDragContext *context,
                    int             x,
                    int             y,
                    guint32         time,
                    gpointer        data)
1564
{
1565
    NautilusCanvasDndInfo *dnd_info;
1566

1567
    dnd_info = NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info;
1568

1569
    /* tell the drag_data_received callback that
1570
     *  the drop occurred and that it can actually
1571 1572 1573
     *  process the actions.
     *  make sure it is going to be called at least once.
     */
1574
    dnd_info->drag_info.drop_occurred = TRUE;
1575

1576 1577 1578
    get_data_on_first_target_we_support (widget, context, time, x, y);

    return TRUE;
1579 1580
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
1581
void
1582
nautilus_canvas_dnd_end_drag (NautilusCanvasContainer *container)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1583
{
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
    NautilusCanvasDndInfo *dnd_info;

    g_return_if_fail (NAUTILUS_IS_CANVAS_CONTAINER (container));

    dnd_info = container->details->dnd_info;
    g_return_if_fail (dnd_info != NULL);
    stop_auto_scroll (container);
    /* Do nothing.
     * Can that possibly be right?
     */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1594
}
1595

1596
/** this callback is called in 2 cases.
1597 1598 1599 1600 1601 1602
 *   It is called upon drag_motion events to get the actual data
 *   In that case, it just makes sure it gets the data.
 *   It is called upon drop_drop events to execute the actual
 *   actions on the received action. In that case, it actually first makes sure
 *   that we have got the data then processes it.
 */
1603 1604

static void
1605 1606 1607 1608 1609 1610 1611 1612
drag_data_received_callback (GtkWidget        *widget,
                             GdkDragContext   *context,
                             int               x,
                             int               y,
                             GtkSelectionData *data,
                             guint             info,
                             guint32           time,
                             gpointer          user_data)
1613
{
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
    NautilusDragInfo *drag_info;
    guchar *tmp;
    const guchar *tmp_raw;
    int length;
    gboolean success;

    drag_info = &(NAUTILUS_CANVAS_CONTAINER (widget)->details->dnd_info->drag_info);

    drag_info->got_drop_data_type = TRUE;
    drag_info->data_type = info;

    switch (info)
    {
        case NAUTILUS_ICON_DND_GNOME_ICON_LIST:
1628 1629 1630 1631
        {
            nautilus_canvas_container_dropped_canvas_feedback (widget, data, x, y);
        }
        break;
1632 1633 1634 1635 1636

        case NAUTILUS_ICON_DND_URI_LIST:
        case NAUTILUS_ICON_DND_TEXT:
        case NAUTILUS_ICON_DND_XDNDDIRECTSAVE:
        case NAUTILUS_ICON_DND_RAW:
1637 1638 1639
        {
            /* Save the data so we can do the actual work on drop. */
            if (drag_info->selection_data != NULL)
1640
            {
1641
                gtk_selection_data_free (drag_info->selection_data);
1642
            }
1643 1644 1645
            drag_info->selection_data = gtk_selection_data_copy (data);
        }
        break;
1646 1647 1648

        /* Netscape keeps sending us the data, even though we accept the first drag */
        case NAUTILUS_ICON_DND_NETSCAPE_URL:
1649 1650
        {
            if (drag_info->selection_data != NULL)
1651
            {
1652 1653
                gtk_selection_data_free (drag_info->selection_data);
                drag_info->selection_data = gtk_selection_data_copy (data);
1654
            }
1655 1656
        }
        break;
1657 1658

        case NAUTILUS_ICON_DND_ROOTWINDOW_DROP:
1659 1660 1661 1662
        {
            /* Do nothing, this won't even happen, since we don't want to call get_data twice */
        }
        break;
1663 1664 1665 1666 1667
    }

    /* this is the second use case of this callback.
     * we have to do the actual work for the drop.
     */
1668
    if (drag_info->drop_occurred)
1669 1670 1671 1672 1673
    {
        success = FALSE;
        switch (info)
        {
            case NAUTILUS_ICON_DND_GNOME_ICON_LIST:
1674 1675 1676 1677 1678 1679
            {
                nautilus_canvas_container_receive_dropped_icons
                    (NAUTILUS_CANVAS_CONTAINER (widget),
                    context, x, y);
            }
            break;