gnome-icon-container-dnd.c 19.7 KB
Newer Older
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1 2 3 4
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-icon-container-dnd.c - Drag & drop handling for the icon container
   widget.

Elliot Lee's avatar
Elliot Lee committed
5
   Copyright (C) 1999, 2000 Free Software Foundation
6 7
   Copyright (C) 2000 Eazel, Inc.
   
Ettore Perazzoli's avatar
Ettore Perazzoli committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   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,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Ettore Perazzoli <ettore@gnu.org>
*/

26 27 28 29 30
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gnome-icon-container-dnd.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
31

32
#include "gnome-icon-container-private.h"
33 34 35
#include "nautilus-background.h"
#include "nautilus-gtk-extensions.h"

36 37 38 39 40
#include <libgnomeui/gnome-canvas-rect-ellipse.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtksignal.h>

41

42

Ettore Perazzoli's avatar
Ettore Perazzoli committed
43
struct _DndSelectionItem {
44 45
	char *uri;
	int x, y;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
46 47 48 49 50 51
};
typedef struct _DndSelectionItem DndSelectionItem;

static GtkTargetEntry drag_types [] = {
	{ GNOME_ICON_CONTAINER_DND_GNOME_ICON_LIST_TYPE, 0, GNOME_ICON_CONTAINER_DND_GNOME_ICON_LIST },
	{ GNOME_ICON_CONTAINER_DND_URI_LIST_TYPE, 0, GNOME_ICON_CONTAINER_DND_URI_LIST },
52
	{ GNOME_ICON_CONTAINER_DND_URL_TYPE, 0, GNOME_ICON_CONTAINER_DND_URL }
Ettore Perazzoli's avatar
Ettore Perazzoli committed
53 54 55 56 57 58
};
static const int num_drag_types = sizeof (drag_types) / sizeof (drag_types[0]);

static GtkTargetEntry drop_types [] = {
	{ GNOME_ICON_CONTAINER_DND_GNOME_ICON_LIST_TYPE, 0, GNOME_ICON_CONTAINER_DND_GNOME_ICON_LIST },
	{ GNOME_ICON_CONTAINER_DND_URI_LIST_TYPE, 0, GNOME_ICON_CONTAINER_DND_URI_LIST },
59 60
	{ GNOME_ICON_CONTAINER_DND_URL_TYPE, 0, GNOME_ICON_CONTAINER_DND_URL },
	{ GNOME_ICON_CONTAINER_DND_COLOR_TYPE, 0, GNOME_ICON_CONTAINER_DND_COLOR }
Ettore Perazzoli's avatar
Ettore Perazzoli committed
61 62 63
};
static const int num_drop_types = sizeof (drop_types) / sizeof (drop_types[0]);

64 65 66
/* number of pixels of margin for shadow box  */
#define SHADOW_MARGIN 8

Ettore Perazzoli's avatar
Ettore Perazzoli committed
67 68 69 70 71 72 73
static GnomeCanvasItem *
create_selection_shadow (GnomeIconContainer *container,
			 GList *list)
{
	GnomeCanvasGroup *group;
	GnomeCanvas *canvas;
	GdkBitmap *stipple;
74 75 76 77 78
	int max_x, max_y;
	int min_x, min_y;
	int icon_width, icon_height;
	int cell_width;
	int x_offset;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
79 80 81
	GList *p;

	if (list == NULL)
82
	    return NULL;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
83

84
	stipple = container->details->dnd_info->stipple;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
85 86 87 88 89
	g_return_val_if_fail (stipple != NULL, NULL);

	icon_width = GNOME_ICON_CONTAINER_ICON_WIDTH (container);
	icon_height = GNOME_ICON_CONTAINER_ICON_HEIGHT (container);
	cell_width = GNOME_ICON_CONTAINER_CELL_WIDTH (container);
90 91
	x_offset = cell_width - icon_width - SHADOW_MARGIN;
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	canvas = GNOME_CANVAS (container);

	/* 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 = GTK_WIDGET (container)->allocation.width;
	min_x = -max_x;

	max_y = GTK_WIDGET (container)->allocation.height;
	min_y = -max_y;

	/* Create a group, so that it's easier to move all the items around at
           once.  */
	group = GNOME_CANVAS_GROUP
		(gnome_canvas_item_new (GNOME_CANVAS_GROUP (canvas->root),
					gnome_canvas_group_get_type (),
					NULL));
	
	for (p = list; p != NULL; p = p->next) {
		DndSelectionItem *item;
113 114
		int x1, y1;
		int x2, y2;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
115 116 117

		item = p->data;

118
		x1 = item->x + x_offset;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
119
		y1 = item->y;
120
		x2 = item->x + icon_width + x_offset;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
121
		y2 = item->y + icon_height;
122
        				
Ettore Perazzoli's avatar
Ettore Perazzoli committed
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		if (x2 >= min_x && x1 <= max_x && y2 >= min_y && y1 <= max_y) {
			GnomeCanvasItem *rect;

			rect = gnome_canvas_item_new
				(group,
				 gnome_canvas_rect_get_type (),
				 "x1", (double) x1, "y1", (double) y1,
				 "x2", (double) x2, "y2", (double) y2,
				 "outline_color", "black",
				 "outline_stipple", stipple,
				 "width_pixels", 1,
				 NULL);
		}
	}

	return GNOME_CANVAS_ITEM (group);
}

/* This is a workaround for a gnome-canvas bug: with the current (1.0.18)
   gnome-libs, setting the x/y values for an existing group fails at updating
   the bounds of the group.  So, instead of setting the x/y values to the
   current position at initialization time, we set them to (0,0) and then use a
   simple affine transform.  */
static void
set_shadow_position (GnomeCanvasItem *shadow,
		     gdouble x, gdouble y)
{
	double affine[6];

	affine[0] = 1.0;
	affine[1] = 0.0;
	affine[2] = 0.0;
	affine[3] = 1.0;
	affine[4] = x;
	affine[5] = y;

	gnome_canvas_item_affine_absolute (shadow, affine);
}


/* Functions to deal with DndSelectionItems.  */

static DndSelectionItem *
dnd_selection_item_new (void)
{
	DndSelectionItem *new;

	new = g_new (DndSelectionItem, 1);
	new->uri = NULL;
	new->x = 0;
	new->y = 0;
	
	return new;
}

static void
dnd_selection_item_destroy (DndSelectionItem *item)
{
	g_free (item->uri);
	g_free (item);
}

static void
destroy_selection_list (GList *list)
{
	GList *p;

	if (list == NULL)
		return;

	for (p = list; p != NULL; p = p->next)
194
		dnd_selection_item_destroy (p->data);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
195 196

	g_list_free (list);
197 198
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
199 200 201 202 203 204 205
/* Source-side handling of the drag.  */

/* Encode a "special/x-gnome-icon-list" selection.  */
static void
set_gnome_icon_list_selection (GnomeIconContainer *container,
			       GtkSelectionData *selection_data)
{
206
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
207 208 209 210
	GList *p;
	GString *data;
	gdouble x_offset, y_offset;

211
	details = container->details;
212 213
	x_offset = details->dnd_info->start_x;
	y_offset = details->dnd_info->start_y;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
214 215

	data = g_string_new (NULL);
216
	for (p = details->icons; p != NULL; p = p->next) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
217
		GnomeIconContainerIcon *icon;
218 219 220
		char *uri;
		char *s;
		int x, y;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
221 222

		icon = p->data;
223
		if (!icon->is_selected)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
224 225
			continue;

226 227
		x = icon->x - x_offset;
		y = icon->y - y_offset;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
228

229 230 231 232
		x += GNOME_ICON_CONTAINER_ICON_X_OFFSET (container)
		      - GNOME_ICON_CONTAINER_ICON_WIDTH (container) / 2;
		y += GNOME_ICON_CONTAINER_ICON_Y_OFFSET (container)
		      - GNOME_ICON_CONTAINER_ICON_HEIGHT (container) / 2;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
233

234 235 236
		uri = nautilus_icons_controller_get_icon_uri (details->controller, icon->data);
		s = g_strdup_printf ("%s\r%d:%d\r\n", uri, x, y);
		g_free (uri);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
237 238 239 240 241 242 243

		g_string_append (data, s);
		g_free (s);
	}

	gtk_selection_data_set (selection_data,
				selection_data->target,
244
				8, data->str, data->len);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
245 246 247 248 249 250 251 252 253

	g_string_free (data, TRUE);
}

/* Encode a "text/uri-list" selection.  */
static void
set_uri_list_selection (GnomeIconContainer *container,
			GtkSelectionData *selection_data)
{
254
	GnomeIconContainerDetails *details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
255
	GList *p;
256
	char *uri;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
257 258
	GString *data;

259
	details = container->details;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
260 261 262

	data = g_string_new (NULL);

263
	for (p = details->icons; p != NULL; p = p->next) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
264 265 266
		GnomeIconContainerIcon *icon;

		icon = p->data;
267
		if (!icon->is_selected)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
268 269
			continue;

270 271 272 273
		uri = nautilus_icons_controller_get_icon_uri (details->controller, icon->data);
		g_string_append (data, uri);
		g_free (uri);

274
		g_string_append (data, "\r\n");
Ettore Perazzoli's avatar
Ettore Perazzoli committed
275 276 277 278
	}

	gtk_selection_data_set (selection_data,
				selection_data->target,
279
				8, data->str, data->len);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

	g_string_free (data, TRUE);
}

static void
drag_data_get_cb (GtkWidget *widget,
		  GdkDragContext *context,
		  GtkSelectionData *selection_data,
		  guint info,
		  guint32 time,
		  gpointer data)
{
	GnomeIconContainer *container;

	g_return_if_fail (widget != NULL);
	g_return_if_fail (GNOME_IS_ICON_CONTAINER (widget));
	g_return_if_fail (context != NULL);

	container = GNOME_ICON_CONTAINER (widget);

	switch (info) {
	case GNOME_ICON_CONTAINER_DND_GNOME_ICON_LIST:
		set_gnome_icon_list_selection (container, selection_data);
		break;
	case GNOME_ICON_CONTAINER_DND_URI_LIST:
		set_uri_list_selection (container, selection_data);
		break;
	default:
		g_assert_not_reached ();
	}
}


/* Target-side handling of the drag.  */

static void
get_gnome_icon_list_selection (GnomeIconContainer *container,
			       GtkSelectionData *data)
{
	GnomeIconContainerDndInfo *dnd_info;
	const guchar *p, *oldp;
	gint size;

323
	dnd_info = container->details->dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

	oldp = data->data;
	size = data->length;

	while (1) {
		DndSelectionItem *item;
		guint len;

		/* The list is in the form:

		   name\rx:y:width:height\r\n

		   The geometry information after the first \r is optional.  */

		/* 1: Decode name. */

		p = memchr (oldp, '\r', size);
		if (p == NULL)
			break;

		item = dnd_selection_item_new ();

		len = p - oldp;

		item->uri = g_malloc (len + 1);
		memcpy (item->uri, oldp, len);
		item->uri[len] = 0;

		p++;
		if (*p == '\n' || *p == '\0') {
			dnd_info->selection_list
				= g_list_prepend (dnd_info->selection_list,
						  item);
			if (p == 0) {
				g_warning ("Invalid special/x-gnome-icon-list data received: "
					   "missing newline character.");
				break;
			} else {
				oldp = p + 1;
				continue;
			}
		}

		size -= p - oldp;
		oldp = p;

		/* 2: Decode geometry information.  */

372
		if (sscanf (p, "%d:%d%*s", &item->x, &item->y) != 2)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
			g_warning ("Invalid special/x-gnome-icon-list data received: "
				   "invalid geometry specification.");

		dnd_info->selection_list
			= g_list_prepend (dnd_info->selection_list, item);

		p = memchr (p, '\r', size);
		if (p == NULL || p[1] != '\n') {
			g_warning ("Invalid special/x-gnome-icon-list data received: "
				   "missing newline character.");
			if (p == NULL)
				break;
		} else {
			p += 2;
		}

		size -= p - oldp;
		oldp = p;
	}
}

static void
395 396 397 398 399 400
gnome_icon_container_position_shadow (GnomeIconContainer *container,
				      int x, int y)
{
	GnomeCanvasItem *shadow;
	double world_x, world_y;

401
	shadow = container->details->dnd_info->shadow;
402 403 404 405 406 407 408 409 410 411 412 413 414
	if (shadow == NULL)
		return;

	gnome_canvas_window_to_world (GNOME_CANVAS (container),
				      x, y, &world_x, &world_y);
	gnome_canvas_item_show (shadow);
	set_shadow_position (shadow, world_x, world_y);
}

static void
gnome_icon_container_dropped_icon_feedback (GtkWidget *widget,
					    GtkSelectionData *data,
					    int x, int y)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
415 416 417 418 419 420 421
{
	GnomeIconContainer *container;
	GnomeIconContainerDndInfo *dnd_info;
	GnomeCanvasItem *shadow;
	double world_x, world_y;

	container = GNOME_ICON_CONTAINER (widget);
422
	dnd_info = container->details->dnd_info;
423

424
	/* delete old selection list if any */
425 426 427 428 429 430

	if (dnd_info->selection_list != NULL) {
		destroy_selection_list (dnd_info->selection_list);
		dnd_info->selection_list = NULL;
	}

431
	/* build the selection list from the drag data */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
432

433
	get_gnome_icon_list_selection (container, data);		
Ettore Perazzoli's avatar
Ettore Perazzoli committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

	shadow = create_selection_shadow (container, dnd_info->selection_list);

	gnome_canvas_item_set (shadow, "x", (gdouble) 0, "y", (gdouble) 0,
			       NULL);

	gnome_canvas_window_to_world (GNOME_CANVAS (widget),
				      x, y, &world_x, &world_y);
	set_shadow_position (shadow, world_x, world_y);

	gnome_canvas_item_show (shadow);

	if (dnd_info->shadow != NULL)
		gtk_object_destroy (GTK_OBJECT (dnd_info->shadow));
	dnd_info->shadow = shadow;
}

451 452 453 454 455 456 457 458 459
static void
drag_data_received_cb (GtkWidget *widget,
		       GdkDragContext *context,
		       gint x,
		       gint y,
		       GtkSelectionData *data,
		       guint info,
		       guint32 time,
		       gpointer user_data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
460
{
461
    	GnomeIconContainerDndInfo *dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
462

463
	dnd_info = GNOME_ICON_CONTAINER (widget)->details->dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
464

465 466
	dnd_info->got_data_type = TRUE;
	dnd_info->data_type = info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
467

468 469 470 471 472 473 474 475 476 477
	switch (info) {
	case GNOME_ICON_CONTAINER_DND_GNOME_ICON_LIST:
		gnome_icon_container_dropped_icon_feedback (widget, data, x, y);
		break;
	case GNOME_ICON_CONTAINER_DND_COLOR:
		/* Save the data so we can do the actual work on drop. */
		dnd_info->selection_data = nautilus_gtk_selection_data_copy_deep (data);
		break;
	default:
		g_warning ("drag_data_received_cb received data but could not handle it");
Ettore Perazzoli's avatar
Ettore Perazzoli committed
478
	}
479 480 481 482 483 484 485 486 487
}

static void
gnome_icon_container_ensure_drag_data (GnomeIconContainer *container,
				       GdkDragContext *context,
				       guint32 time)
{
	GnomeIconContainerDndInfo *dnd_info;

488
	dnd_info = container->details->dnd_info;
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

	if (!dnd_info->got_data_type)
		gtk_drag_get_data (GTK_WIDGET (container), context,
				   GPOINTER_TO_INT (context->targets->data),
				   time);
}

static gboolean
drag_motion_cb (GtkWidget *widget,
		GdkDragContext *context,
		int x, int y,
		guint32 time)
{
	gnome_icon_container_ensure_drag_data (GNOME_ICON_CONTAINER (widget), context, time);
	gnome_icon_container_position_shadow (GNOME_ICON_CONTAINER (widget), x, y);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
504 505

	gdk_drag_status (context, context->suggested_action, time);
506

Ettore Perazzoli's avatar
Ettore Perazzoli committed
507 508 509 510 511 512 513 514 515 516 517 518
	return TRUE;
}

static void
drag_end_cb (GtkWidget *widget,
	     GdkDragContext *context,
	     gpointer data)
{
	GnomeIconContainer *container;
	GnomeIconContainerDndInfo *dnd_info;

	container = GNOME_ICON_CONTAINER (widget);
519
	dnd_info = container->details->dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
520 521 522 523 524

	destroy_selection_list (dnd_info->selection_list);
	dnd_info->selection_list = NULL;
}

525 526 527
/* Utility routine to extract the directory from an item_uri
   (which may have geometry info attached).
*/
528

529 530
static void
gnome_icon_container_receive_dropped_icons (GnomeIconContainer *container,
531 532
					    GdkDragContext *context,
					    int x, int y)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
533 534 535
{
	GnomeIconContainerDndInfo *dnd_info;

536
	dnd_info = container->details->dnd_info;
537 538 539
	if (dnd_info->selection_list == NULL)
		return;

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	/* Move files in same window.

	   FIXME: This won't work between windows for two reasons.

	   First, we have to figure out whether we need to actually
	   move or copy some files. For directory views this is based
	   on the URIs, but that should be done by the controller, not
	   here in the view.
	   
	   Second, the start_x and start_y values are only good for
	   this window. To make dragging between multiple windows work,
	   we need to communicate the offset between the mouse and the
	   corner of the selection along with the other selection info
	   so we can position the icons correctly in the window they
	   are dropped in. The geometry that's currently included along
	   with the selection is not sufficient.
	*/
	if (context->action == GDK_ACTION_MOVE) {
		double world_x, world_y;
559
		
560 561 562 563 564 565 566
		gnome_canvas_window_to_world (GNOME_CANVAS (container),
					      x, y, &world_x, &world_y);
		
		gnome_icon_container_xlate_selected (container,
						     world_x - dnd_info->start_x,
						     world_y - dnd_info->start_y,
						     TRUE);
567 568 569 570
	}
	
	destroy_selection_list (dnd_info->selection_list);
	dnd_info->selection_list = NULL;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
571 572 573
}

static void
574
gnome_icon_container_free_drag_data (GnomeIconContainer *container)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
575 576
{
	GnomeIconContainerDndInfo *dnd_info;
577
	
578
	dnd_info = container->details->dnd_info;
579 580 581
	
	dnd_info->got_data_type = FALSE;
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
582 583 584 585 586
	if (dnd_info->shadow != NULL) {
		gtk_object_destroy (GTK_OBJECT (dnd_info->shadow));
		dnd_info->shadow = NULL;
	}

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	if (dnd_info->selection_data != NULL) {
		nautilus_gtk_selection_data_free_deep (dnd_info->selection_data);
		dnd_info->selection_data = NULL;
	}
}

static gboolean
drag_drop_cb (GtkWidget *widget,
	      GdkDragContext *context,
	      gint x,
	      gint y,
	      guint32 time,
	      gpointer data)
{
	GnomeIconContainerDndInfo *dnd_info;

603
	dnd_info = GNOME_ICON_CONTAINER (widget)->details->dnd_info;
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

	gnome_icon_container_ensure_drag_data (GNOME_ICON_CONTAINER (widget), context, time);

	g_assert (dnd_info->got_data_type);
	switch (dnd_info->data_type) {
	case GNOME_ICON_CONTAINER_DND_GNOME_ICON_LIST:
		gnome_icon_container_receive_dropped_icons
			(GNOME_ICON_CONTAINER (widget),
			 context, x, y);
		break;
	case GNOME_ICON_CONTAINER_DND_COLOR:
		nautilus_background_receive_dropped_color
			(nautilus_get_widget_background (widget),
			 widget, x, y, dnd_info->selection_data);
		gtk_drag_finish (context, TRUE, FALSE, time);
		break;
	default:
		g_warning ("drag_drop_cb received data but could not handle it");
	}

	gnome_icon_container_free_drag_data (GNOME_ICON_CONTAINER (widget));

	return FALSE;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
627 628
}

629 630 631 632 633 634 635
static void
drag_leave_cb (GtkWidget *widget,
	       GdkDragContext *context,
	       guint32 time,
	       gpointer data)
{
}
636

Ettore Perazzoli's avatar
Ettore Perazzoli committed
637 638 639 640 641 642 643 644 645
void
gnome_icon_container_dnd_init (GnomeIconContainer *container,
			       GdkBitmap *stipple)
{
	GnomeIconContainerDndInfo *dnd_info;

	g_return_if_fail (container != NULL);
	g_return_if_fail (GNOME_IS_ICON_CONTAINER (container));

646
	dnd_info = g_new0 (GnomeIconContainerDndInfo, 1);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

	dnd_info->target_list = gtk_target_list_new (drag_types,
						     num_drag_types);

	dnd_info->stipple = gdk_bitmap_ref (stipple);


	/* Set up the widget as a drag destination.  */
	/* (But not a source, as drags starting from this widget will be
           implemented by dealing with events manually.)  */

	gtk_drag_dest_set  (GTK_WIDGET (container),
			    0,
			    drop_types, num_drop_types,
			    GDK_ACTION_COPY | GDK_ACTION_MOVE);

663 664
	/* Messages for outgoing drag. */

Ettore Perazzoli's avatar
Ettore Perazzoli committed
665 666 667 668
	gtk_signal_connect (GTK_OBJECT (container), "drag_data_get",
			    GTK_SIGNAL_FUNC (drag_data_get_cb), NULL);
	gtk_signal_connect (GTK_OBJECT (container), "drag_end",
			    GTK_SIGNAL_FUNC (drag_end_cb), NULL);
669 670 671

	/* Messages for incoming drag. */

Ettore Perazzoli's avatar
Ettore Perazzoli committed
672 673
	gtk_signal_connect (GTK_OBJECT (container), "drag_data_received",
			    GTK_SIGNAL_FUNC (drag_data_received_cb), NULL);
674 675
	gtk_signal_connect (GTK_OBJECT (container), "drag_motion",
			    GTK_SIGNAL_FUNC (drag_motion_cb), NULL);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
676 677 678 679 680
	gtk_signal_connect (GTK_OBJECT (container), "drag_drop",
			    GTK_SIGNAL_FUNC (drag_drop_cb), NULL);
	gtk_signal_connect (GTK_OBJECT (container), "drag_leave",
			    GTK_SIGNAL_FUNC (drag_leave_cb), NULL);

681
	container->details->dnd_info = dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
682 683 684 685 686 687 688 689 690 691
}

void
gnome_icon_container_dnd_fini (GnomeIconContainer *container)
{
	GnomeIconContainerDndInfo *dnd_info;

	g_return_if_fail (container != NULL);
	g_return_if_fail (GNOME_IS_ICON_CONTAINER (container));

692
	dnd_info = container->details->dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	g_return_if_fail (dnd_info != NULL);

	gtk_target_list_unref (dnd_info->target_list);
	destroy_selection_list (dnd_info->selection_list);

	if (dnd_info->shadow != NULL)
		gtk_object_destroy (GTK_OBJECT (dnd_info->shadow));

	gdk_bitmap_unref (dnd_info->stipple);

	g_free (dnd_info);
}

void
gnome_icon_container_dnd_begin_drag (GnomeIconContainer *container,
				     GdkDragAction actions,
				     gint button,
				     GdkEventMotion *event)
{
712
	GtkArg pixbuf_args[1];
Ettore Perazzoli's avatar
Ettore Perazzoli committed
713
	GnomeIconContainerDndInfo *dnd_info;
714 715 716
	GdkDragContext *context;
	GnomeCanvasItem *pixbuf_item;
	GdkPixbuf* temp_pixbuf;
717 718
	GdkPixmap* pixmap_for_dragged_file;
	GdkBitmap* mask_for_dragged_file;
719
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
720 721 722 723
	g_return_if_fail (container != NULL);
	g_return_if_fail (GNOME_IS_ICON_CONTAINER (container));
	g_return_if_fail (event != NULL);

724
	dnd_info = container->details->dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
725
	g_return_if_fail (dnd_info != NULL);
726
	
Ettore Perazzoli's avatar
Ettore Perazzoli committed
727
	/* Notice that the event is already in world coordinates, because of
728
           the way the canvas handles events! */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
729 730
	dnd_info->start_x = event->x;
	dnd_info->start_y = event->y;
731
	
732
	context = gtk_drag_begin (GTK_WIDGET (container),
733 734 735 736 737
				  dnd_info->target_list,
				  actions,
				  button,
				  (GdkEvent *) event);
	
738
        /* create a pixmap and mask to drag with */
739
        pixbuf_item = GNOME_CANVAS_ITEM (container->details->drag_icon->image_item);
740
        pixbuf_args[0].name = "GnomeCanvasPixbuf::pixbuf";
741 742 743
        gtk_object_getv (GTK_OBJECT (pixbuf_item), 1, pixbuf_args);
        temp_pixbuf = (GdkPixbuf *) GTK_VALUE_OBJECT (pixbuf_args[0]);
        gdk_pixbuf_render_pixmap_and_mask (temp_pixbuf, &pixmap_for_dragged_file, &mask_for_dragged_file, 128); 
744 745
       
        /* set the pixmap and mask for dragging */
746 747 748
        gtk_drag_set_icon_pixmap (context, gtk_widget_get_colormap (GTK_WIDGET (container)),
				  pixmap_for_dragged_file, mask_for_dragged_file,
				  event->x - pixbuf_item->x1, event->y - pixbuf_item->y1);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
749 750 751 752 753 754 755 756 757 758
}

void
gnome_icon_container_dnd_end_drag (GnomeIconContainer *container)
{
	GnomeIconContainerDndInfo *dnd_info;

	g_return_if_fail (container != NULL);
	g_return_if_fail (GNOME_IS_ICON_CONTAINER (container));

759
	dnd_info = container->details->dnd_info;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
760 761
	g_return_if_fail (dnd_info != NULL);
}