nautilus-tree-view-drag-dest.c 22.6 KB
Newer Older
1 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/*
 * Nautilus
 *
 * Copyright (C) 2002 Sun Microsystems, Inc.
 *
 * Nautilus is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * Nautilus 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 * 
 * Author: Dave Camp <dave@ximian.com>
 */

/* nautilus-tree-view-drag-dest.c: Handles drag and drop for treeviews which 
 *                                 contain a hierarchy of files
 */

#include <config.h>
#include "nautilus-tree-view-drag-dest.h"

#include <eel/eel-gtk-macros.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <libgnome/gnome-macros.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include "nautilus-file-dnd.h"
#include "nautilus-icon-dnd.h"
#include "nautilus-link.h"
#include "nautilus-marshal.h"

#define AUTO_SCROLL_MARGIN 20

45 46
#define HOVER_EXPAND_TIMEOUT 1000

47 48 49 50 51 52 53 54 55 56 57 58
struct _NautilusTreeViewDragDestDetails {
	GtkTreeView *tree_view;

	gboolean drop_occurred;

	gboolean have_drag_data;
	guint drag_type;
	GtkSelectionData *drag_data;
	GList *drag_list;

	guint highlight_id;
	guint scroll_id;
Alexander Larsson's avatar
Alexander Larsson committed
59
	guint expand_id;
60 61 62 63 64 65
};

enum {
	GET_ROOT_URI,
	GET_FILE_FOR_PATH,
	MOVE_COPY_ITEMS,
66
	HANDLE_NETSCAPE_URL,
67
	HANDLE_URI_LIST,
68
	HANDLE_TEXT,
69 70 71 72 73 74 75 76 77 78 79 80
	LAST_SIGNAL
};

static void nautilus_tree_view_drag_dest_instance_init (NautilusTreeViewDragDest      *dest);
static void nautilus_tree_view_drag_dest_class_init    (NautilusTreeViewDragDestClass *class);

static guint signals[LAST_SIGNAL];

GNOME_CLASS_BOILERPLATE (NautilusTreeViewDragDest,
			 nautilus_tree_view_drag_dest,
			 GObject, G_TYPE_OBJECT);

81
static const GtkTargetEntry drag_types [] = {
82
	{ NAUTILUS_ICON_DND_GNOME_ICON_LIST_TYPE, 0, NAUTILUS_ICON_DND_GNOME_ICON_LIST },
83 84
	/* prefer "_NETSCAPE_URL" over "text/uri-list" to satisfy web browsers. */
	{ NAUTILUS_ICON_DND_NETSCAPE_URL_TYPE, 0, NAUTILUS_ICON_DND_NETSCAPE_URL },
85
	{ NAUTILUS_ICON_DND_URI_LIST_TYPE, 0, NAUTILUS_ICON_DND_URI_LIST },
86
	{ NAUTILUS_ICON_DND_KEYWORD_TYPE, 0, NAUTILUS_ICON_DND_KEYWORD }
87 88 89 90 91 92 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
};


static void
gtk_tree_view_vertical_autoscroll (GtkTreeView *tree_view)
{
	GdkRectangle visible_rect;
	GtkAdjustment *vadjustment;
	GdkWindow *window;
	int y;
	int offset;
	float value;
	
	window = gtk_tree_view_get_bin_window (tree_view);
	vadjustment = gtk_tree_view_get_vadjustment (tree_view);
	
	gdk_window_get_pointer (window, NULL, &y, NULL);
	
	y += vadjustment->value;

	gtk_tree_view_get_visible_rect (tree_view, &visible_rect);
	
	offset = y - (visible_rect.y + 2 * AUTO_SCROLL_MARGIN);
	if (offset > 0) {
		offset = y - (visible_rect.y + visible_rect.height - 2 * AUTO_SCROLL_MARGIN);
		if (offset < 0) {
			return;
		}
	}

	value = CLAMP (vadjustment->value + offset, 0.0,
		       vadjustment->upper - vadjustment->page_size);
	gtk_adjustment_set_value (vadjustment, value);
}

static int
scroll_timeout (gpointer data)
{
	GtkTreeView *tree_view = GTK_TREE_VIEW (data);
	
	gtk_tree_view_vertical_autoscroll (tree_view);

	return TRUE;
}

static void
remove_scroll_timeout (NautilusTreeViewDragDest *dest)
{
	if (dest->details->scroll_id) {
136
		g_source_remove (dest->details->scroll_id);
137 138 139 140
		dest->details->scroll_id = 0;
	}
}

Alexander Larsson's avatar
Alexander Larsson committed
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
static int
expand_timeout (gpointer data)
{
	GtkTreeView *tree_view;
	GtkTreePath *drop_path;
	
	tree_view = GTK_TREE_VIEW (data);
	
	gtk_tree_view_get_drag_dest_row (tree_view, &drop_path, NULL);
	
	if (drop_path) {
		gtk_tree_view_expand_row (tree_view, drop_path, FALSE);
		gtk_tree_path_free (drop_path);
	}

	return FALSE;
}

static void
remove_expand_timeout (NautilusTreeViewDragDest *dest)
{
	if (dest->details->expand_id) {
		g_source_remove (dest->details->expand_id);
		dest->details->expand_id = 0;
	}
}

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static gboolean
highlight_expose (GtkWidget *widget,
		  GdkEventExpose *event,
		  gpointer data)
{
	GdkWindow *bin_window;
	int width;
	int height;

	if (GTK_WIDGET_DRAWABLE (widget)) {
		bin_window = 
			gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
		
		gdk_drawable_get_size (bin_window, &width, &height);
		
		gtk_paint_focus (widget->style,
				 bin_window,
				 GTK_WIDGET_STATE (widget),
				 NULL,
				 widget,
				 "treeview-drop-indicator",
				 0, 0, width, height);
	}
	
	return FALSE;
}

static void
set_widget_highlight (NautilusTreeViewDragDest *dest, gboolean highlight)
{
	if (!highlight && dest->details->highlight_id) {
		g_signal_handler_disconnect (dest->details->tree_view,
					     dest->details->highlight_id);
		dest->details->highlight_id = 0;
	}
	
	if (highlight && !dest->details->highlight_id) {
		dest->details->highlight_id = 
			g_signal_connect_object (dest->details->tree_view,
						 "expose_event",
						 G_CALLBACK (highlight_expose),
209
						 dest, 0);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 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 277 278 279 280 281 282 283 284 285 286 287 288 289
	}
	gtk_widget_queue_draw (GTK_WIDGET (dest->details->tree_view));
}

static void
set_drag_dest_row (NautilusTreeViewDragDest *dest,
		   GtkTreePath *path)
{
	if (path) {
		set_widget_highlight (dest, FALSE);
		gtk_tree_view_set_drag_dest_row
			(dest->details->tree_view,
			 path,
			 GTK_TREE_VIEW_DROP_INTO_OR_BEFORE);
	} else {
		set_widget_highlight (dest, TRUE);
		gtk_tree_view_set_drag_dest_row (dest->details->tree_view, 
						 NULL, 
						 0);
	}
}

static void
clear_drag_dest_row (NautilusTreeViewDragDest *dest)
{
	gtk_tree_view_set_drag_dest_row (dest->details->tree_view, NULL, 0);
	set_widget_highlight (dest, FALSE);
}

static void
get_drag_data (NautilusTreeViewDragDest *dest,
	       GdkDragContext *context, 
	       guint32 time)
{
	GdkAtom target;
	
	target = gtk_drag_dest_find_target (GTK_WIDGET (dest->details->tree_view), 
					    context, 
					    NULL);

	gtk_drag_get_data (GTK_WIDGET (dest->details->tree_view),
			   context, target, time);
}

static void
free_drag_data (NautilusTreeViewDragDest *dest)
{
	dest->details->have_drag_data = FALSE;

	if (dest->details->drag_data) {
		gtk_selection_data_free (dest->details->drag_data);
		dest->details->drag_data = NULL;
	}

	if (dest->details->drag_list) {
		nautilus_drag_destroy_selection_list (dest->details->drag_list);
		dest->details->drag_list = NULL;
	}
}

static char *
get_root_uri (NautilusTreeViewDragDest *dest)
{
	char *uri;
	
	g_signal_emit (dest, signals[GET_ROOT_URI], 0, &uri);
	
	return uri;
}

static NautilusFile *
file_for_path (NautilusTreeViewDragDest *dest, GtkTreePath *path)
{
	NautilusFile *file;
	char *uri;
	
	if (path) {
		g_signal_emit (dest, signals[GET_FILE_FOR_PATH], 0, path, &file);
	} else {
		uri = get_root_uri (dest);
290 291 292 293 294

		file = NULL;
		if (uri != NULL) {
			file = nautilus_file_get (uri);
		}
295 296 297 298 299 300 301 302 303 304 305 306 307 308
		
		g_free (uri);
	}
	
	return file;
}

static GtkTreePath *
get_drop_path (NautilusTreeViewDragDest *dest,
	       GtkTreePath *path)
{
	NautilusFile *file;
	GtkTreePath *ret;
	
309
	if (!path || !dest->details->have_drag_data) {
310 311 312
		return NULL;
	}

313 314
	ret = gtk_tree_path_copy (path);
	file = file_for_path (dest, ret);
315

316 317
	/* Go up the tree until we find a file that can accept a drop */
	while (file == NULL /* dummy row */ ||
318 319 320
	       !nautilus_drag_can_accept_info (file,
		       			       dest->details->drag_type,
					       dest->details->drag_list)) {
321
		if (gtk_tree_path_get_depth (ret) == 1) {
322
			gtk_tree_path_free (ret);
323
			ret = NULL;
324
			break;
325 326
		} else {
			gtk_tree_path_up (ret);
327

328 329
			nautilus_file_unref (file);
			file = file_for_path (dest, ret);
330 331 332 333 334 335 336 337
		}
	}
	nautilus_file_unref (file);
	
	return ret;
}

static char *
338 339
get_drop_target_uri_for_path (NautilusTreeViewDragDest *dest,
			      GtkTreePath *path)
340 341 342 343 344
{
	NautilusFile *file;
	char *target;

	file = file_for_path (dest, path);
345 346 347 348
	if (file == NULL) {
		return NULL;
	}
	
349 350 351 352 353 354 355 356 357 358 359 360
	target = nautilus_file_get_drop_target_uri (file);
	nautilus_file_unref (file);
	
	return target;
}

static guint
get_drop_action (NautilusTreeViewDragDest *dest, 
		 GdkDragContext *context,
		 GtkTreePath *path)
{
	char *drop_target;
361
	int action;
362
	
363 364 365
	if (!dest->details->have_drag_data ||
	    (dest->details->drag_type == NAUTILUS_ICON_DND_GNOME_ICON_LIST &&
	     dest->details->drag_list == NULL)) {
366 367 368 369 370
		return 0;
	}

	switch (dest->details->drag_type) {
	case NAUTILUS_ICON_DND_GNOME_ICON_LIST :
371
		drop_target = get_drop_target_uri_for_path (dest, path);
372 373 374 375 376 377 378 379 380 381 382 383 384 385
		
		if (!drop_target) {
			return 0;
		}

		nautilus_drag_default_drop_action_for_icons
			(context,
			 drop_target,
			 dest->details->drag_list,
			 &action);

		g_free (drop_target);
		
		return action;
386
		
387
	case NAUTILUS_ICON_DND_NETSCAPE_URL:
388 389 390 391 392 393
		drop_target = get_drop_target_uri_for_path (dest, path);

		if (drop_target == NULL) {
			return 0;
		}

394
		action = nautilus_drag_default_drop_action_for_netscape_url (context);
395 396 397 398

		g_free (drop_target);

		return action;
399
		
400
	case NAUTILUS_ICON_DND_URI_LIST :
401 402 403 404 405 406 407 408
		drop_target = get_drop_target_uri_for_path (dest, path);

		if (drop_target == NULL) {
			return 0;
		}

		g_free (drop_target);

409
		return context->suggested_action;
410 411

	case NAUTILUS_ICON_DND_TEXT:
412
		return GDK_ACTION_COPY;
413

414 415 416
	case NAUTILUS_ICON_DND_KEYWORD:

		if (!path) {
417 418 419 420
			return 0;
		}

		return GDK_ACTION_COPY;
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	}

	return 0;
}

static gboolean
drag_motion_callback (GtkWidget *widget,
		      GdkDragContext *context,
		      int x,
		      int y,
		      guint32 time,
		      gpointer data)
{
	NautilusTreeViewDragDest *dest;
	GtkTreePath *path;
Alexander Larsson's avatar
Alexander Larsson committed
436 437 438
	GtkTreePath *drop_path, *old_drop_path;
	GtkTreeModel *model;
	GtkTreeIter drop_iter;
439
	GtkTreeViewDropPosition pos;
440
	GdkWindow *bin_window;
441 442 443 444 445 446 447 448 449 450 451 452 453
	guint action;

	dest = NAUTILUS_TREE_VIEW_DRAG_DEST (data);

	gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
					   x, y, &path, &pos);
	

	if (!dest->details->have_drag_data) {
		get_drag_data (dest, context, time);
	}
	drop_path = get_drop_path (dest, path);
	
454 455 456 457 458 459 460 461 462 463 464
	action = 0;
	bin_window = gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget));
	if (bin_window != NULL) {
		int bin_x, bin_y;
		gdk_window_get_position (bin_window, &bin_x, &bin_y);
		if (bin_y <= y) {
			/* ignore drags on the header */
			action = get_drop_action (dest, context, drop_path);
		}
	}

Alexander Larsson's avatar
Alexander Larsson committed
465 466 467
	gtk_tree_view_get_drag_dest_row (GTK_TREE_VIEW (widget), &old_drop_path,
					 NULL);
	
468 469
	if (action) {
		set_drag_dest_row (dest, drop_path);
Alexander Larsson's avatar
Alexander Larsson committed
470 471 472 473 474 475 476 477
		model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
		if (drop_path == NULL || (old_drop_path != NULL &&
		    gtk_tree_path_compare (old_drop_path, drop_path) != 0)) {
			remove_expand_timeout (dest);
		}
		if (dest->details->expand_id == 0 && drop_path != NULL) {
			gtk_tree_model_get_iter (model, &drop_iter, drop_path);
			if (gtk_tree_model_iter_has_child (model, &drop_iter)) {
478 479 480
				dest->details->expand_id = g_timeout_add (HOVER_EXPAND_TIMEOUT,
									  expand_timeout,
									  dest->details->tree_view);
Alexander Larsson's avatar
Alexander Larsson committed
481 482
			}
		}
483 484
	} else {
		clear_drag_dest_row (dest);
Alexander Larsson's avatar
Alexander Larsson committed
485
		remove_expand_timeout (dest);
486 487 488 489 490 491 492 493 494 495
	}
	
	if (path) {
		gtk_tree_path_free (path);
	}
	
	if (drop_path) {
		gtk_tree_path_free (drop_path);
	}
	
Alexander Larsson's avatar
Alexander Larsson committed
496 497 498 499
	if (old_drop_path) {
		gtk_tree_path_free (old_drop_path);
	}
	
500 501
	if (dest->details->scroll_id == 0) {
		dest->details->scroll_id = 
502 503 504
			g_timeout_add (150, 
				       scroll_timeout, 
				       dest->details->tree_view);
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	}

	gdk_drag_status (context, action, time);

	return TRUE;
}

static void
drag_leave_callback (GtkWidget *widget,
		     GdkDragContext *context,
		     guint32 time,
		     gpointer data)
{
	NautilusTreeViewDragDest *dest;

	dest = NAUTILUS_TREE_VIEW_DRAG_DEST (data);

	clear_drag_dest_row (dest);

	free_drag_data (dest);

	remove_scroll_timeout (dest);
Alexander Larsson's avatar
Alexander Larsson committed
527
	remove_expand_timeout (dest);
528 529
}

530 531
static char *
get_drop_target_uri_at_pos (NautilusTreeViewDragDest *dest, int x, int y)
532 533 534 535 536 537 538 539 540 541 542
{
	char *drop_target;
	GtkTreePath *path;
	GtkTreePath *drop_path;
	GtkTreeViewDropPosition pos;

	gtk_tree_view_get_dest_row_at_pos (dest->details->tree_view, x, y, 
					   &path, &pos);

	drop_path = get_drop_path (dest, path);

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	drop_target = get_drop_target_uri_for_path (dest, drop_path);

	if (path != NULL) {
		gtk_tree_path_free (path);
	}

	if (drop_path != NULL) {
		gtk_tree_path_free (drop_path);
	}

	return drop_target;
}

static void
receive_uris (NautilusTreeViewDragDest *dest,
	      GdkDragContext *context,
	      GList *source_uris,
	      int x, int y)
{
	char *drop_target;
	GdkDragAction action;

	drop_target = get_drop_target_uri_at_pos (dest, x, y);
	g_assert (drop_target != NULL);
567 568 569 570 571 572 573 574

	if (context->action == GDK_ACTION_ASK) {
		if (nautilus_drag_selection_includes_special_link (dest->details->drag_list)) {
			/* We only want to move the trash */
			action = GDK_ACTION_MOVE;
		} else {
			action = GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_LINK;
		}
575 576
		context->action = nautilus_drag_drop_action_ask
			(GTK_WIDGET (dest->details->tree_view), action);
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
	}

	/* We only want to copy external uris */
	if (dest->details->drag_type == NAUTILUS_ICON_DND_URI_LIST) {
		action = GDK_ACTION_COPY;
	}

	if (context->action > 0) {
		g_signal_emit (dest, signals[MOVE_COPY_ITEMS], 0,
			       source_uris, 
			       drop_target,
			       context->action,
			       x, y);
	}

	g_free (drop_target);
}

static void
receive_dropped_icons (NautilusTreeViewDragDest *dest,
		       GdkDragContext *context,
		       int x, int y)
{
	GList *source_uris;
	GList *l;

	/* FIXME: ignore local only moves */

	if (!dest->details->drag_list) {
		return;
	}
	
	source_uris = NULL;
	for (l = dest->details->drag_list; l != NULL; l = l->next) {
		source_uris = g_list_prepend (source_uris,
					      ((NautilusDragSelectionItem *)l->data)->uri);
	}

	source_uris = g_list_reverse (source_uris);

	receive_uris (dest, context, source_uris, x, y);
	
	g_list_free (source_uris);
}

static void
receive_dropped_uri_list (NautilusTreeViewDragDest *dest,
			  GdkDragContext *context,
			  int x, int y)
{
627 628
	char *drop_target;

629 630 631 632
	if (!dest->details->drag_data) {
		return;
	}

633 634 635
	drop_target = get_drop_target_uri_at_pos (dest, x, y);
	g_assert (drop_target != NULL);

636 637
	g_signal_emit (dest, signals[HANDLE_URI_LIST], 0,
		       (char*)dest->details->drag_data->data,
638
		       drop_target,
639 640
		       context->action,
		       x, y);
641 642

	g_free (drop_target);
643 644
}

645 646 647 648 649
static void
receive_dropped_text (NautilusTreeViewDragDest *dest,
		      GdkDragContext *context,
		      int x, int y)
{
650
	char *drop_target;
651 652 653 654 655 656
	char *text;

	if (!dest->details->drag_data) {
		return;
	}

657 658 659
	drop_target = get_drop_target_uri_at_pos (dest, x, y);
	g_assert (drop_target != NULL);

660 661
	text = gtk_selection_data_get_text (dest->details->drag_data);
	g_signal_emit (dest, signals[HANDLE_TEXT], 0,
662
		       (char *) text, drop_target,
663 664
		       context->action,
		       x, y);
665

666
	g_free (text);
667
	g_free (drop_target);
668 669 670
}


671
static void
672 673 674
receive_dropped_netscape_url (NautilusTreeViewDragDest *dest,
			      GdkDragContext *context,
			      int x, int y)
675
{
676 677
	char *drop_target;

678 679 680 681
	if (!dest->details->drag_data) {
		return;
	}

682 683 684
	drop_target = get_drop_target_uri_at_pos (dest, x, y);
	g_assert (drop_target != NULL);

685
	g_signal_emit (dest, signals[HANDLE_NETSCAPE_URL], 0,
686
		       (char*)dest->details->drag_data->data,
687
		       drop_target,
688 689
		       context->action,
		       x, y);
690 691

	g_free (drop_target);
692 693
}

694 695 696 697 698 699 700 701
static void
receive_dropped_keyword (NautilusTreeViewDragDest *dest,
			 GdkDragContext *context,
			 int x, int y)
{
	char *drop_target_uri;
	NautilusFile *drop_target_file;

702 703 704
	if (!dest->details->drag_data) {
		return;
	}
705

706 707
	drop_target_uri = get_drop_target_uri_at_pos (dest, x, y);
	g_assert (drop_target_uri != NULL);
708 709 710 711 712 713 714 715 716 717 718 719

	drop_target_file = nautilus_file_get (drop_target_uri);

	if (drop_target_file != NULL) {
		nautilus_drag_file_receive_dropped_keyword (drop_target_file,
							    (char *) dest->details->drag_data->data);
		nautilus_file_unref (drop_target_file);
	}

	g_free (drop_target_uri);
}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
static gboolean
drag_data_received_callback (GtkWidget *widget,
			     GdkDragContext *context,
			     int x,
			     int y,
			     GtkSelectionData *selection_data,
			     guint info,
			     guint32 time,
			     gpointer data)
{
	NautilusTreeViewDragDest *dest;
	gboolean success;
	
	dest = NAUTILUS_TREE_VIEW_DRAG_DEST (data);

	if (!dest->details->have_drag_data) {
		dest->details->have_drag_data = TRUE;
		dest->details->drag_type = info;
		dest->details->drag_data = 
			gtk_selection_data_copy (selection_data);
		if (info == NAUTILUS_ICON_DND_GNOME_ICON_LIST) {
			dest->details->drag_list = 
				nautilus_drag_build_selection_list (selection_data);
		}
	}

	if (dest->details->drop_occurred) {
		success = FALSE;
		switch (info) {
		case NAUTILUS_ICON_DND_GNOME_ICON_LIST :
			receive_dropped_icons (dest, context, x, y);
			success = TRUE;
			break;
753 754
		case NAUTILUS_ICON_DND_NETSCAPE_URL :
			receive_dropped_netscape_url (dest, context, x, y);
755 756 757
			success = TRUE;
			break;
		case NAUTILUS_ICON_DND_URI_LIST :
758 759 760
			receive_dropped_uri_list (dest, context, x, y);
			success = TRUE;
			break;
761 762 763 764
		case NAUTILUS_ICON_DND_TEXT:
			receive_dropped_text (dest, context, x, y);
			success = TRUE;
			break;
765 766 767 768
		case NAUTILUS_ICON_DND_KEYWORD:
			receive_dropped_keyword (dest, context, x, y);
			success = TRUE;
			break;
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
		}

		dest->details->drop_occurred = FALSE;
		free_drag_data (dest);
		gtk_drag_finish (context, success, FALSE, time);
	}

	/* appease GtkTreeView by preventing its drag_data_receive
	 * from being called */
	g_signal_stop_emission_by_name (dest->details->tree_view,
					"drag_data_received");

	return TRUE;
}

static gboolean
drag_drop_callback (GtkWidget *widget,
		    GdkDragContext *context,
		    int x, 
		    int y,
		    guint32 time,
		    gpointer data)
{
	NautilusTreeViewDragDest *dest;

	dest = NAUTILUS_TREE_VIEW_DRAG_DEST (data);

	dest->details->drop_occurred = TRUE;

	get_drag_data (dest, context, time);
	remove_scroll_timeout (dest);
Alexander Larsson's avatar
Alexander Larsson committed
800
	remove_expand_timeout (dest);
801 802 803 804 805
	clear_drag_dest_row (dest);
	
	return TRUE;
}

806 807 808 809 810 811 812 813 814
static void
tree_view_weak_notify (gpointer user_data,
		       GObject *object)
{
	NautilusTreeViewDragDest *dest;

	dest = NAUTILUS_TREE_VIEW_DRAG_DEST (user_data);
	
	remove_scroll_timeout (dest);
Alexander Larsson's avatar
Alexander Larsson committed
815
	remove_expand_timeout (dest);
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

	dest->details->tree_view = NULL;
}

static void
nautilus_tree_view_drag_dest_dispose (GObject *object)
{
	NautilusTreeViewDragDest *dest;
	
	dest = NAUTILUS_TREE_VIEW_DRAG_DEST (object);

	if (dest->details->tree_view) {
		g_object_weak_unref (G_OBJECT (dest->details->tree_view),
				     tree_view_weak_notify,
				     dest);
	}
	
	remove_scroll_timeout (dest);
Alexander Larsson's avatar
Alexander Larsson committed
834
	remove_expand_timeout (dest);
835 836 837 838

	EEL_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
}

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
static void
nautilus_tree_view_drag_dest_finalize (GObject *object)
{
	NautilusTreeViewDragDest *dest;
	
	dest = NAUTILUS_TREE_VIEW_DRAG_DEST (object);

	free_drag_data (dest);

	g_free (dest->details);

	EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}

static void
nautilus_tree_view_drag_dest_instance_init (NautilusTreeViewDragDest *dest)
{
	dest->details = g_new0 (NautilusTreeViewDragDestDetails, 1);
}

static void
nautilus_tree_view_drag_dest_class_init (NautilusTreeViewDragDestClass *class)
{
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS (class);
	
866
	gobject_class->dispose = nautilus_tree_view_drag_dest_dispose;
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
	gobject_class->finalize = nautilus_tree_view_drag_dest_finalize;

	signals[GET_ROOT_URI] = 
		g_signal_new ("get_root_uri",
			      G_TYPE_FROM_CLASS (class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (NautilusTreeViewDragDestClass,
					       get_root_uri),
			      NULL, NULL,
			      nautilus_marshal_STRING__VOID,
			      G_TYPE_STRING, 0);
	signals[GET_FILE_FOR_PATH] = 
		g_signal_new ("get_file_for_path",
			      G_TYPE_FROM_CLASS (class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (NautilusTreeViewDragDestClass,
					       get_file_for_path),
			      NULL, NULL,
			      nautilus_marshal_OBJECT__BOXED,
			      NAUTILUS_TYPE_FILE, 1,
			      GTK_TYPE_TREE_PATH);
	signals[MOVE_COPY_ITEMS] =
		g_signal_new ("move_copy_items",
			      G_TYPE_FROM_CLASS (class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (NautilusTreeViewDragDestClass,
					       move_copy_items),
			      NULL, NULL,
			      
896
			      nautilus_marshal_VOID__POINTER_STRING_ENUM_INT_INT,
897 898 899
			      G_TYPE_NONE, 5,
			      G_TYPE_POINTER,
			      G_TYPE_STRING,
900
			      GDK_TYPE_DRAG_ACTION,
901 902
			      G_TYPE_INT,
			      G_TYPE_INT);
903 904
	signals[HANDLE_NETSCAPE_URL] =
		g_signal_new ("handle_netscape_url",
905 906 907
			      G_TYPE_FROM_CLASS (class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (NautilusTreeViewDragDestClass, 
908
					       handle_netscape_url),
909
			      NULL, NULL,
910 911 912
			      nautilus_marshal_VOID__STRING_STRING_ENUM_INT_INT,
			      G_TYPE_NONE, 5,
			      G_TYPE_STRING,
913
			      G_TYPE_STRING,
914
			      GDK_TYPE_DRAG_ACTION,
915 916 917 918 919 920 921 922 923
			      G_TYPE_INT,
			      G_TYPE_INT);
	signals[HANDLE_URI_LIST] =
		g_signal_new ("handle_uri_list",
			      G_TYPE_FROM_CLASS (class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (NautilusTreeViewDragDestClass, 
					       handle_uri_list),
			      NULL, NULL,
924 925 926
			      nautilus_marshal_VOID__STRING_STRING_ENUM_INT_INT,
			      G_TYPE_NONE, 5,
			      G_TYPE_STRING,
927
			      G_TYPE_STRING,
928
			      GDK_TYPE_DRAG_ACTION,
929
			      G_TYPE_INT,
930 931 932 933 934 935 936 937
			      G_TYPE_INT);
	signals[HANDLE_TEXT] =
		g_signal_new ("handle_text",
			      G_TYPE_FROM_CLASS (class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (NautilusTreeViewDragDestClass, 
					       handle_text),
			      NULL, NULL,
938 939 940
			      nautilus_marshal_VOID__STRING_STRING_ENUM_INT_INT,
			      G_TYPE_NONE, 5,
			      G_TYPE_STRING,
941 942
			      G_TYPE_STRING,
			      GDK_TYPE_DRAG_ACTION,
943 944
			      G_TYPE_INT,
			      G_TYPE_INT);
945 946
}

947 948


949 950 951 952
NautilusTreeViewDragDest *
nautilus_tree_view_drag_dest_new (GtkTreeView *tree_view)
{
	NautilusTreeViewDragDest *dest;
953
	GtkTargetList *targets;
954 955 956 957
	
	dest = g_object_new (NAUTILUS_TYPE_TREE_VIEW_DRAG_DEST, NULL);

	dest->details->tree_view = tree_view;
958 959
	g_object_weak_ref (G_OBJECT (dest->details->tree_view),
			   tree_view_weak_notify, dest);
960 961 962
	
	gtk_drag_dest_set (GTK_WIDGET (tree_view),
			   0, drag_types, G_N_ELEMENTS (drag_types),
963 964 965 966 967
			   GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_LINK | GDK_ACTION_ASK);

	targets = gtk_drag_dest_get_target_list (GTK_WIDGET (tree_view));
	gtk_target_list_add_text_targets (targets, NAUTILUS_ICON_DND_TEXT);

968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
	g_signal_connect_object (tree_view,
				 "drag_motion",
				 G_CALLBACK (drag_motion_callback),
				 dest, 0);
	g_signal_connect_object (tree_view,
				 "drag_leave",
				 G_CALLBACK (drag_leave_callback),
				 dest, 0);
	g_signal_connect_object (tree_view,
				 "drag_drop",
				 G_CALLBACK (drag_drop_callback),
				 dest, 0);
	g_signal_connect_object (tree_view, 
				 "drag_data_received",
				 G_CALLBACK (drag_data_received_callback),
				 dest, 0);
	
	return dest;
}