~snaggen/rhythmbox/bpm

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
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
323
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
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
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
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
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
/* rbtreednd.c
 * Copyright (C) 2001  Red Hat, Inc.
 *
 * This 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.
 *
 * This 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 this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301  USA.
 */

#include "config.h"

#include <string.h>
#include <gtk/gtktreeselection.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkwidget.h>
#include <gtk/gtkmain.h>
#include <gtk/gtktreednd.h>
#include "rb-tree-dnd.h"

#include "rb-debug.h"

#define RB_TREE_DND_STRING "RbTreeDndString"
/* must be the same value as in gtk_tree_view.c */
#define SCROLL_EDGE_SIZE 15

typedef struct
{
  guint pressed_button;
  gint x;
  gint y;
  guint button_press_event_handler;
  guint motion_notify_handler;
  guint button_release_handler;
  guint drag_data_get_handler;
  guint drag_data_delete_handler;
  guint drag_motion_handler;
  guint drag_leave_handler;
  guint drag_drop_handler;
  guint drag_data_received_handler;
  GSList *event_list;
  gboolean pending_event;

  GtkTargetList *dest_target_list;
  GdkDragAction dest_actions;
  RbTreeDestFlag dest_flags;

  GtkTargetList *source_target_list;
  GdkDragAction source_actions;
  GdkModifierType start_button_mask;

  /* Scroll timeout (e.g. during dnd) */
  guint scroll_timeout;

  /* Select on drag timeout */
  GtkTreePath * previous_dest_path;
  guint select_on_drag_timeout;
} RbTreeDndData;

RbTreeDndData *init_rb_tree_dnd_data (GtkWidget *widget);
GList * get_context_data (GdkDragContext *context);
static gboolean filter_drop_position (GtkWidget *widget, GdkDragContext *context, GtkTreePath *path, GtkTreeViewDropPosition *pos);
static gint scroll_row_timeout (gpointer data);
static gboolean select_on_drag_timeout (gpointer data);
static void remove_scroll_timeout (GtkTreeView *tree_view);
static void remove_select_on_drag_timeout (GtkTreeView *tree_view);

GType
rb_tree_drag_source_get_type (void)
{
  static GType our_type = 0;

  if (!our_type)
    {
      static const GTypeInfo our_info =
      {
        sizeof (RbTreeDragSourceIface), /* class_size */
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	NULL,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	0,
	0,              /* n_preallocs */
	NULL
      };

      our_type = g_type_register_static (G_TYPE_INTERFACE, "RbTreeDragSource", &our_info, 0);
    }

  return our_type;
}


/**
 * rb_tree_drag_source_row_draggable:
 * @drag_source: a #EggTreeMultiDragSource
 * @path: row on which user is initiating a drag
 *
 * Asks the #EggTreeMultiDragSource whether a particular row can be used as
 * the source of a DND operation. If the source doesn't implement
 * this interface, the row is assumed draggable.
 *
 * Return value: %TRUE if the row can be dragged
 **/
gboolean
rb_tree_drag_source_row_draggable (RbTreeDragSource *drag_source,
				   GList                  *path_list)
{
  RbTreeDragSourceIface *iface = RB_TREE_DRAG_SOURCE_GET_IFACE (drag_source);

  g_return_val_if_fail (RB_IS_TREE_DRAG_SOURCE (drag_source), FALSE);
  g_return_val_if_fail (iface->rb_row_draggable != NULL, FALSE);
  g_return_val_if_fail (path_list != NULL, FALSE);

  if (iface->rb_row_draggable)
    return (* iface->rb_row_draggable) (drag_source, path_list);
  else
    return TRUE;
}


/**
 * rb_tree_drag_source_drag_data_delete:
 * @drag_source: a #EggTreeMultiDragSource
 * @path: row that was being dragged
 *
 * Asks the #EggTreeMultiDragSource to delete the row at @path, because
 * it was moved somewhere else via drag-and-drop. Returns %FALSE
 * if the deletion fails because @path no longer exists, or for
 * some model-specific reason. Should robustly handle a @path no
 * longer found in the model!
 *
 * Return value: %TRUE if the row was successfully deleted
 **/
gboolean
rb_tree_drag_source_drag_data_delete (RbTreeDragSource *drag_source,
					     GList                  *path_list)
{
  RbTreeDragSourceIface *iface = RB_TREE_DRAG_SOURCE_GET_IFACE (drag_source);

  g_return_val_if_fail (RB_IS_TREE_DRAG_SOURCE (drag_source), FALSE);
  g_return_val_if_fail (iface->rb_drag_data_delete != NULL, FALSE);
  g_return_val_if_fail (path_list != NULL, FALSE);

  return (* iface->rb_drag_data_delete) (drag_source, path_list);
}

/**
 * rb_tree_drag_source_drag_data_get:
 * @drag_source: a #EggTreeMultiDragSource
 * @path: row that was dragged
 * @selection_data: a #EggSelectionData to fill with data from the dragged row
 *
 * Asks the #EggTreeMultiDragSource to fill in @selection_data with a
 * representation of the row at @path. @selection_data->target gives
 * the required type of the data.  Should robustly handle a @path no
 * longer found in the model!
 *
 * Return value: %TRUE if data of the required type was provided
 **/
gboolean
rb_tree_drag_source_drag_data_get    (RbTreeDragSource *drag_source,
					     GList                  *path_list,
					     GtkSelectionData  *selection_data)
{
  RbTreeDragSourceIface *iface = RB_TREE_DRAG_SOURCE_GET_IFACE (drag_source);

  g_return_val_if_fail (RB_IS_TREE_DRAG_SOURCE (drag_source), FALSE);
  g_return_val_if_fail (iface->rb_drag_data_get != NULL, FALSE);
  g_return_val_if_fail (path_list != NULL, FALSE);
  g_return_val_if_fail (selection_data != NULL, FALSE);

  return (* iface->rb_drag_data_get) (drag_source, path_list, selection_data);
}



GType
rb_tree_drag_dest_get_type (void)
{
  static GType our_type = 0;

  if (!our_type)
    {
      static const GTypeInfo our_info =
      {
        sizeof (RbTreeDragDestIface), /* class_size */
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	NULL,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	0,
	0,              /* n_preallocs */
	NULL
      };

      our_type = g_type_register_static (G_TYPE_INTERFACE, "RbTreeDragDest", &our_info, 0);
    }

  return our_type;
}



gboolean
rb_tree_drag_dest_drag_data_received (RbTreeDragDest   *drag_dest,
				      GtkTreePath       *dest,
				      GtkTreeViewDropPosition pos, 
				      GtkSelectionData  *selection_data)
{
  RbTreeDragDestIface *iface = RB_TREE_DRAG_DEST_GET_IFACE (drag_dest);

  g_return_val_if_fail (RB_IS_TREE_DRAG_DEST (drag_dest), FALSE);
  g_return_val_if_fail (iface->rb_drag_data_received != NULL, FALSE);
  g_return_val_if_fail (selection_data != NULL, FALSE);

  return (* iface->rb_drag_data_received) (drag_dest, dest, pos, selection_data);
}



gboolean
rb_tree_drag_dest_row_drop_possible (RbTreeDragDest   *drag_dest,
				     GtkTreePath       *dest_path,
				     GtkTreeViewDropPosition pos,
				     GtkSelectionData  *selection_data)
{
  RbTreeDragDestIface *iface = RB_TREE_DRAG_DEST_GET_IFACE (drag_dest);

  g_return_val_if_fail (RB_IS_TREE_DRAG_DEST (drag_dest), FALSE);
  g_return_val_if_fail (iface->rb_row_drop_possible != NULL, FALSE);
  g_return_val_if_fail (selection_data != NULL, FALSE);

  return (* iface->rb_row_drop_possible) (drag_dest, dest_path, pos, selection_data);
}


gboolean
rb_tree_drag_dest_row_drop_position (RbTreeDragDest   *drag_dest,
				     GtkTreePath       *dest_path,
				     GList *targets,
				     GtkTreeViewDropPosition *pos)
{
  RbTreeDragDestIface *iface = RB_TREE_DRAG_DEST_GET_IFACE (drag_dest);

  g_return_val_if_fail (RB_IS_TREE_DRAG_DEST (drag_dest), FALSE);
  g_return_val_if_fail (iface->rb_row_drop_position != NULL, FALSE);
  g_return_val_if_fail (targets != NULL, FALSE);
  g_return_val_if_fail (pos != NULL, FALSE);

  return (* iface->rb_row_drop_position) (drag_dest, dest_path, targets, pos);
}

static void
rb_tree_dnd_data_free (gpointer data)
{
  RbTreeDndData *priv_data = data;

  if (priv_data->source_target_list != NULL) {
	  gtk_target_list_unref (priv_data->source_target_list);
  }
  if (priv_data->dest_target_list != NULL) {
	  gtk_target_list_unref (priv_data->dest_target_list);
  }

  g_free (priv_data);
}

RbTreeDndData *
init_rb_tree_dnd_data (GtkWidget *widget)
{
	RbTreeDndData *priv_data;

	priv_data = g_object_get_data (G_OBJECT (widget), RB_TREE_DND_STRING);
	if (priv_data == NULL)
	{
		priv_data = g_new0 (RbTreeDndData, 1);
		priv_data->pending_event = FALSE;
		g_object_set_data_full (G_OBJECT (widget), RB_TREE_DND_STRING, priv_data, rb_tree_dnd_data_free);
		priv_data->drag_motion_handler = 0;
		priv_data->drag_leave_handler = 0;
		priv_data->button_press_event_handler = 0;
		priv_data->scroll_timeout = 0;
		priv_data->previous_dest_path = NULL;
		priv_data->select_on_drag_timeout = 0;
	}

	return priv_data;
}

static void
stop_drag_check (GtkWidget *widget)
{
	RbTreeDndData *priv_data;
	GSList *l;

	priv_data = g_object_get_data (G_OBJECT (widget), RB_TREE_DND_STRING);

	for (l = priv_data->event_list; l != NULL; l = l->next)
		gdk_event_free (l->data);

	g_slist_free (priv_data->event_list);
	priv_data->event_list = NULL;
	priv_data->pending_event = FALSE;
	g_signal_handler_disconnect (widget, priv_data->motion_notify_handler);
	g_signal_handler_disconnect (widget, priv_data->button_release_handler);
}


static gboolean
rb_tree_dnd_button_release_event_cb (GtkWidget      *widget,
				   GdkEventButton *event,
				   gpointer        data)
{
	RbTreeDndData *priv_data;
	GSList *l;

	priv_data = g_object_get_data (G_OBJECT (widget), RB_TREE_DND_STRING);

	for (l = priv_data->event_list; l != NULL; l = l->next)
		gtk_propagate_event (widget, l->data);

	stop_drag_check (widget);

	return FALSE;
}


static void
selection_foreach (GtkTreeModel *model,
		   GtkTreePath  *path,
		   GtkTreeIter  *iter,
		   gpointer      data)
{
	GList **list_ptr;

	list_ptr = (GList **) data;
	*list_ptr = g_list_prepend (*list_ptr, gtk_tree_row_reference_new (model, path));
}


static void
path_list_free (GList *path_list)
{
	g_list_foreach (path_list, (GFunc) gtk_tree_row_reference_free, NULL);
	g_list_free (path_list);
}

static void
set_context_data (GdkDragContext *context,
		  GList          *path_list)
{
	g_object_set_data_full (G_OBJECT (context),
				"rb-tree-view-multi-source-row",
				path_list,
				(GDestroyNotify) path_list_free);

	rb_debug ("Setting path_list: index=%i", gtk_tree_path_get_indices(path_list->data)[0]);
}

GList *
get_context_data (GdkDragContext *context)
{
	return g_object_get_data (G_OBJECT (context), "rb-tree-view-multi-source-row");
}

gboolean
filter_drop_position (GtkWidget *widget, GdkDragContext *context, GtkTreePath *path, GtkTreeViewDropPosition *pos)
{
	GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
	GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
	RbTreeDndData *priv_data = g_object_get_data (G_OBJECT (widget), RB_TREE_DND_STRING);
	gboolean ret;

	if (!(priv_data->dest_flags & RB_TREE_DEST_CAN_DROP_INTO)) {
		if (*pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
			*pos = GTK_TREE_VIEW_DROP_BEFORE;
		else if (*pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
			*pos = GTK_TREE_VIEW_DROP_AFTER;
	} else if (!(priv_data->dest_flags & RB_TREE_DEST_CAN_DROP_BETWEEN)) {
		if (*pos == GTK_TREE_VIEW_DROP_BEFORE)
			*pos = GTK_TREE_VIEW_DROP_INTO_OR_BEFORE;
		else if (*pos == GTK_TREE_VIEW_DROP_AFTER)
			*pos = GTK_TREE_VIEW_DROP_INTO_OR_AFTER;
	}

	ret = rb_tree_drag_dest_row_drop_position (RB_TREE_DRAG_DEST (model),
						   path,
						   context->targets,
						   pos);
  
	rb_debug ("filtered drop position: %s", ret ? "TRUE" : "FALSE");	
	return ret;
}


/* Scroll function taken/adapted from gtktreeview.c */
static gint
scroll_row_timeout (gpointer data)
{
	GtkTreeView *tree_view = data;
	GdkRectangle visible_rect;
	gint y, x;
	gint offset;
	gfloat value;
	GtkAdjustment* vadj;
	RbTreeDndData *priv_data;

	GDK_THREADS_ENTER ();

	priv_data = g_object_get_data (G_OBJECT (tree_view), RB_TREE_DND_STRING);
	g_return_val_if_fail(priv_data != NULL, TRUE);

	gdk_window_get_pointer (gtk_tree_view_get_bin_window (tree_view), &x, &y, NULL);
	gtk_tree_view_widget_to_tree_coords (tree_view, x, y, &x, &y);
	gtk_tree_view_get_visible_rect (tree_view, &visible_rect);

	/* see if we are near the edge. */
	if (x < visible_rect.x && x > visible_rect.x + visible_rect.width)
	{
		GDK_THREADS_LEAVE ();
		priv_data->scroll_timeout = 0;
		return FALSE;
	}

	offset = y - (visible_rect.y + 2 * SCROLL_EDGE_SIZE);
	if (offset > 0)
	{
		offset = y - (visible_rect.y + visible_rect.height - 2 * SCROLL_EDGE_SIZE);
		if (offset < 0) 
		{
			GDK_THREADS_LEAVE ();
			priv_data->scroll_timeout = 0;
			return FALSE;
		}
	}

	vadj = gtk_tree_view_get_vadjustment (tree_view);
	value = CLAMP (vadj->value + offset, vadj->lower, vadj->upper - vadj->page_size);
	gtk_adjustment_set_value (vadj, value);

	/* don't remove it if we're on the edge and not scrolling */
	if (ABS (vadj->value - value) > 0.0001)
		remove_select_on_drag_timeout(tree_view);

	GDK_THREADS_LEAVE ();

	return TRUE;
}


static gboolean
select_on_drag_timeout (gpointer data)
{
	GtkTreeView *tree_view = data;
	GtkTreeSelection *selection;
	RbTreeDndData *priv_data;

	GDK_THREADS_ENTER ();

	priv_data = g_object_get_data (G_OBJECT (tree_view), RB_TREE_DND_STRING);
	g_return_val_if_fail(priv_data != NULL, FALSE);
	g_return_val_if_fail(priv_data->previous_dest_path != NULL, FALSE);

	selection = gtk_tree_view_get_selection(tree_view);
	if (!gtk_tree_selection_path_is_selected(selection,priv_data->previous_dest_path)) {
		rb_debug("Changing selection because of drag timeout");
		gtk_tree_view_set_cursor(tree_view,priv_data->previous_dest_path,NULL,FALSE);
	}

	priv_data->select_on_drag_timeout = 0;
	gtk_tree_path_free(priv_data->previous_dest_path);
	priv_data->previous_dest_path = NULL;

	GDK_THREADS_LEAVE ();
	return FALSE;
}


static void
remove_scroll_timeout (GtkTreeView *tree_view)
 {
	 RbTreeDndData *priv_data;

	 priv_data = g_object_get_data (G_OBJECT (tree_view), RB_TREE_DND_STRING);
	 g_return_if_fail(priv_data != NULL);

	 if (priv_data->scroll_timeout != 0)
	 {
		 g_source_remove (priv_data->scroll_timeout);
		 priv_data->scroll_timeout = 0;
	 }
}


static void
remove_select_on_drag_timeout (GtkTreeView *tree_view)
 {
	RbTreeDndData *priv_data;

	priv_data = g_object_get_data (G_OBJECT (tree_view), RB_TREE_DND_STRING);
	g_return_if_fail(priv_data != NULL);

	if (priv_data->select_on_drag_timeout != 0) {
		rb_debug("Removing the select on drag timeout");
		g_source_remove (priv_data->select_on_drag_timeout);
		priv_data->select_on_drag_timeout = 0;
	}
	if (priv_data->previous_dest_path != NULL) {
		gtk_tree_path_free(priv_data->previous_dest_path);
		priv_data->previous_dest_path = NULL;
	}
}


static void
rb_tree_dnd_drag_data_delete_cb (GtkWidget *widget,
			  GdkDragContext *drag_context,
			  gpointer user_data)
{
	GList *path_list;
	GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(widget));

	path_list = get_context_data (drag_context);
	rb_tree_drag_source_drag_data_delete (RB_TREE_DRAG_SOURCE (model),
					      path_list);

	g_signal_stop_emission_by_name (widget, "drag_data_delete");
}



static void
rb_tree_dnd_drag_data_get_cb (GtkWidget        *widget,
			    GdkDragContext   *context,
			    GtkSelectionData *selection_data,
			    guint             info,
			    guint             time)
{
	GtkTreeView *tree_view;
	GtkTreeModel *model;
	GList *path_list;

	tree_view = GTK_TREE_VIEW (widget);
	model = gtk_tree_view_get_model (tree_view);

	if (model == NULL)
		return;

	path_list = get_context_data (context);

	if (path_list == NULL)
		return;

	/* We can implement the GTK_TREE_MODEL_ROW target generically for
	* any model; for DragSource models there are some other targets
	* we also support.
	*/
	if (RB_IS_TREE_DRAG_SOURCE (model))
	{
		rb_tree_drag_source_drag_data_get (RB_TREE_DRAG_SOURCE (model),
						   path_list,
						   selection_data);
    	}
}


static gboolean
rb_tree_dnd_motion_notify_event_cb (GtkWidget      *widget,
				     GdkEventMotion *event,
                 		     gpointer        data)
{
	RbTreeDndData *priv_data;

	priv_data = g_object_get_data (G_OBJECT (widget), RB_TREE_DND_STRING);

	if (gtk_drag_check_threshold (widget,
				      priv_data->x,
				      priv_data->y,
				      event->x,
				      event->y))
	{
		GList *path_list = NULL;
		GtkTreeSelection *selection;
		GtkTreeModel *model;
		GdkDragContext *context;

		selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
		stop_drag_check (widget);
		gtk_tree_selection_selected_foreach (selection, selection_foreach, &path_list);
		path_list = g_list_reverse (path_list);
		model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));

		if (rb_tree_drag_source_row_draggable (RB_TREE_DRAG_SOURCE (model), path_list))
		{
			rb_debug ("drag begin");
			context = gtk_drag_begin (widget,
						  priv_data->source_target_list,
						  priv_data->source_actions,
						  priv_data->pressed_button,
						  (GdkEvent*)event);
	  		set_context_data (context, path_list);
	  		gtk_drag_set_icon_default (context);

		} else {
			path_list_free (path_list);
		}
	}

	return TRUE;
}

static gboolean
rb_tree_dnd_drag_motion_cb (GtkWidget        *widget,
                           GdkDragContext   *context,
                           gint              x,
                           gint              y,
                           guint             time)
{
	GtkTreeView *tree_view;
	GtkTreePath *path = NULL;
	GtkTreeModel *model;
	GtkTreeViewDropPosition pos;
	RbTreeDndData *priv_data;
	GdkDragAction action;

	rb_debug ("drag and drop motion: (%i,%i)", x, y);

  	tree_view = GTK_TREE_VIEW (widget);
	model = gtk_tree_view_get_model (tree_view);

	priv_data = g_object_get_data (G_OBJECT (widget), RB_TREE_DND_STRING);

	gtk_tree_view_get_dest_row_at_pos (tree_view, x, y, &path, &pos);

	if ((priv_data->previous_dest_path == NULL)
	    || (path == NULL)
	    || gtk_tree_path_compare(path,priv_data->previous_dest_path)) {
				remove_select_on_drag_timeout(tree_view);
	}

	if (path == NULL)
	{
	  	gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget),
						 NULL,
						 GTK_TREE_VIEW_DROP_BEFORE);

		if (!(priv_data->dest_flags & RB_TREE_DEST_EMPTY_VIEW_DROP)) {
			/* Can't drop here. */
			gdk_drag_status (context, 0, time);

			return TRUE;
		} else if (!filter_drop_position (widget, context, path, &pos)) {
			gdk_drag_status (context, 0, time);
			return TRUE;
		}
	}
	else
	{
		if (!filter_drop_position (widget, context, path, &pos)) {
			gdk_drag_status (context, 0, time);
			return TRUE;
		}

	  if (priv_data->scroll_timeout == 0)
	  {
		  priv_data->scroll_timeout = g_timeout_add (150, scroll_row_timeout, tree_view);
	  }
	}

	if (GTK_WIDGET (tree_view) == gtk_drag_get_source_widget (context) &&
	    context->actions & GDK_ACTION_MOVE)
		action = GDK_ACTION_MOVE;
	else
		action = context->suggested_action;

	if (path) {
		gtk_tree_view_set_drag_dest_row (tree_view, path, pos);
		if (priv_data->dest_flags & RB_TREE_DEST_SELECT_ON_DRAG_TIMEOUT) {
			if (priv_data->previous_dest_path != NULL) {
				gtk_tree_path_free (priv_data->previous_dest_path);
			}
			priv_data->previous_dest_path = path;
			if (priv_data->select_on_drag_timeout == 0) {
				rb_debug("Setting up a new select on drag timeout");
				priv_data->select_on_drag_timeout = g_timeout_add (2000, select_on_drag_timeout, tree_view);
			}
		} else {
			gtk_tree_path_free (path);
		}
	}

	gdk_drag_status (context, action, time);

	return TRUE;
}


static gboolean
rb_tree_dnd_drag_leave_cb (GtkWidget        *widget,
                           GdkDragContext   *context,
                           gint              x,
                           gint              y,
                           guint             time)
{
	remove_select_on_drag_timeout(GTK_TREE_VIEW (widget));
	return TRUE;
}

static gboolean
rb_tree_dnd_drag_drop_cb (GtkWidget        *widget,
                         GdkDragContext   *context,
                         gint              x,
                         gint              y,
                         guint             time)
{
	GtkTreeView *tree_view;
	GtkTreePath *path;
	GtkTreeModel *model;
	GtkTreeViewDropPosition pos;
	RbTreeDndData *priv_data;

	tree_view = GTK_TREE_VIEW (widget);
	model = gtk_tree_view_get_model (tree_view);
	priv_data = g_object_get_data (G_OBJECT (widget), RB_TREE_DND_STRING);
	gtk_tree_view_get_dest_row_at_pos (tree_view, x, y, &path, &pos);

	remove_scroll_timeout (tree_view);

	/* Unset this thing */
	gtk_tree_view_set_drag_dest_row (tree_view,
               				 NULL,
					 GTK_TREE_VIEW_DROP_BEFORE);

	if (path || priv_data->dest_flags & RB_TREE_DEST_EMPTY_VIEW_DROP) {

		GdkAtom target;
		RbTreeDragDestIface *iface = RB_TREE_DRAG_DEST_GET_IFACE (model);
		if (iface->rb_get_drag_target) {
			RbTreeDragDest *dest = RB_TREE_DRAG_DEST (model);
			target = (* iface->rb_get_drag_target) (dest, widget, 
								context, path, 
								priv_data->dest_target_list);
		} else {
			target = gtk_drag_dest_find_target (widget, context, 
							    priv_data->dest_target_list);
		}

		if (path)
			gtk_tree_path_free (path);

		if (target != GDK_NONE) {
			gtk_drag_get_data (widget, context, target, time);
			return TRUE;
		}
	}

	return FALSE;
}


static void
rb_tree_dnd_drag_data_received_cb (GtkWidget        *widget,
                                  GdkDragContext   *context,
                                  gint              x,
                                  gint              y,
                                  GtkSelectionData *selection_data,
                                  guint             info,
                                  guint             time)
{
	GtkTreeView *tree_view;
	GtkTreeModel *model;
	GtkTreePath *dest_row;
	GtkTreeViewDropPosition pos;
	gboolean filtered = TRUE;
	gboolean accepted = FALSE;

	tree_view = GTK_TREE_VIEW (widget);
	model = gtk_tree_view_get_model (tree_view);

	gtk_tree_view_get_dest_row_at_pos (tree_view, x, y, &dest_row, &pos);

	if (dest_row)
		if (!filter_drop_position (widget, context, dest_row, &pos))
			filtered = FALSE;

	if (filtered && selection_data->length >= 0)
	{
		if (rb_tree_drag_dest_drag_data_received (RB_TREE_DRAG_DEST (model),
                					  dest_row,
							  pos,
                					  selection_data))
        		accepted = TRUE;
	}

	gtk_drag_finish (context,
        		 accepted,
			 (context->action == GDK_ACTION_MOVE),
			 time);

	if (dest_row)
  		gtk_tree_path_free (dest_row);

	g_signal_stop_emission_by_name (widget, "drag_data_received");

}


static gboolean
rb_tree_dnd_button_press_event_cb (GtkWidget      *widget,
					GdkEventButton *event,
					gpointer        data)
{
	GtkTreeView *tree_view;
	GtkTreePath *path = NULL;
	GtkTreeViewColumn *column = NULL;
	gint cell_x, cell_y;
	GtkTreeSelection *selection;
	RbTreeDndData *priv_data;

	if (event->button == 3)
		return FALSE;

	tree_view = GTK_TREE_VIEW (widget);
	if (event->window != gtk_tree_view_get_bin_window (tree_view))
		return FALSE;

	priv_data = g_object_get_data (G_OBJECT (tree_view), RB_TREE_DND_STRING);
	if (priv_data == NULL)
	{
		priv_data = g_new0 (RbTreeDndData, 1);
		priv_data->pending_event = FALSE;
		g_object_set_data (G_OBJECT (tree_view), RB_TREE_DND_STRING, priv_data);
	}

	if (g_slist_find (priv_data->event_list, event))
		return FALSE;

	if (priv_data->pending_event)
	{
		/* save the event to be propagated in order */
		priv_data->event_list = g_slist_append (priv_data->event_list, gdk_event_copy ((GdkEvent*)event));
		return TRUE;
	}

	if (event->type == GDK_2BUTTON_PRESS)
		return FALSE;

	gtk_tree_view_get_path_at_pos (tree_view,
				       event->x, event->y,
				       &path, &column,
				       &cell_x, &cell_y);

	selection = gtk_tree_view_get_selection (tree_view);

	if (path)
	{
		gboolean call_parent = (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ||
					!gtk_tree_selection_path_is_selected (selection, path) ||
					event->button != 1);

		if (call_parent)
			(GTK_WIDGET_GET_CLASS (tree_view))->button_press_event (widget, event);

		if (gtk_tree_selection_path_is_selected (selection, path))
		{
			priv_data->pressed_button = event->button;
			priv_data->x = event->x;
			priv_data->y = event->y;

			priv_data->pending_event = TRUE;

			if (!call_parent)
				priv_data->event_list = g_slist_append (priv_data->event_list,
									gdk_event_copy ((GdkEvent*)event));

			priv_data->motion_notify_handler = g_signal_connect (G_OBJECT (tree_view),
									     "motion_notify_event",
									     G_CALLBACK (rb_tree_dnd_motion_notify_event_cb),
									     NULL);
			priv_data->button_release_handler = g_signal_connect (G_OBJECT (tree_view),
									      "button_release_event",
									      G_CALLBACK (rb_tree_dnd_button_release_event_cb),
									      NULL);

		}

		gtk_tree_path_free (path);
		/* We called the default handler so we don't let the default handler run */
		return TRUE;
	}

	return FALSE;
}


void
rb_tree_dnd_add_drag_source_support (GtkTreeView *tree_view,
				 GdkModifierType start_button_mask,
				 const GtkTargetEntry *targets,
				 gint n_targets,
				 GdkDragAction actions)
{
	RbTreeDndData *priv_data = NULL;
 	g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));

	priv_data = init_rb_tree_dnd_data (GTK_WIDGET(tree_view));

	if (!priv_data->button_press_event_handler) {

		priv_data->source_target_list = gtk_target_list_new (targets, n_targets);
		priv_data->source_actions = actions;
		priv_data->start_button_mask = start_button_mask;

		priv_data->button_press_event_handler = g_signal_connect (G_OBJECT (tree_view),
							"button_press_event",
							G_CALLBACK (rb_tree_dnd_button_press_event_cb),
							NULL);
		priv_data->drag_data_get_handler = g_signal_connect (G_OBJECT (tree_view),
								     "drag_data_get",
								     G_CALLBACK (rb_tree_dnd_drag_data_get_cb),
								     NULL);
		priv_data->drag_data_delete_handler = g_signal_connect (G_OBJECT (tree_view),
									"drag_data_delete",
									G_CALLBACK (rb_tree_dnd_drag_data_delete_cb),
									NULL);
	}
}





void
rb_tree_dnd_add_drag_dest_support (GtkTreeView *tree_view,
			       RbTreeDestFlag flags,
			       const GtkTargetEntry *targets,
			       gint n_targets,
			       GdkDragAction actions)
{
	RbTreeDndData *priv_data = NULL;
	g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));

	priv_data = init_rb_tree_dnd_data (GTK_WIDGET(tree_view));

	if (!priv_data->drag_motion_handler) {

		priv_data->dest_target_list = gtk_target_list_new (targets, n_targets);
		priv_data->dest_actions = actions;
		priv_data->dest_flags = flags;

		gtk_drag_dest_set (GTK_WIDGET (tree_view),
				   0,
				   NULL,
				   0,
				   actions);

		priv_data->drag_motion_handler = g_signal_connect (G_OBJECT (tree_view),
								  "drag_motion",
								  G_CALLBACK (rb_tree_dnd_drag_motion_cb),
								  NULL);
		priv_data->drag_leave_handler = g_signal_connect (G_OBJECT (tree_view),
								  "drag_leave",
								  G_CALLBACK (rb_tree_dnd_drag_leave_cb),
								  NULL);
		priv_data->drag_drop_handler = g_signal_connect (G_OBJECT (tree_view),
								 "drag_drop",
								 G_CALLBACK (rb_tree_dnd_drag_drop_cb),
								 NULL);
	  	priv_data->drag_data_received_handler = g_signal_connect (G_OBJECT (tree_view),
									  "drag_data_received",
									  G_CALLBACK (rb_tree_dnd_drag_data_received_cb), 										  NULL);
	}

}