fixed dnd
[geeqie.git] / src / collect-table.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "collect-table.h"
16
17 #include "cellrenderericon.h"
18 #include "collect-dlg.h"
19 #include "collect-io.h"
20 #include "dnd.h"
21 #include "dupe.h"
22 #include "editors.h"
23 #include "filedata.h"
24 #include "img-view.h"
25 #include "layout.h"
26 #include "layout_image.h"
27 #include "menu.h"
28 #include "print.h"
29 #include "utilops.h"
30 #include "ui_fileops.h"
31 #include "ui_menu.h"
32 #include "ui_tree_edit.h"
33 #include "uri_utils.h"
34
35 #include "icons/marker.xpm"
36 #define MARKER_WIDTH 26
37 #define MARKER_HEIGHT 32
38
39 #include <gdk/gdkkeysyms.h> /* for keyboard values */
40
41 /* between these, the icon width is increased by thumb_max_width / 2 */
42 #define THUMB_MIN_ICON_WIDTH 128
43 #define THUMB_MAX_ICON_WIDTH 150
44
45 #define COLLECT_TABLE_MAX_COLUMNS 32
46 #define THUMB_BORDER_PADDING 2
47
48 #define COLLECT_TABLE_TIP_DELAY 500
49 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
50
51
52 enum {
53         CTABLE_COLUMN_POINTER = 0,
54         CTABLE_COLUMN_COUNT
55 };
56
57 typedef enum {
58         SELECTION_NONE          = 0,
59         SELECTION_SELECTED      = 1 << 0,
60         SELECTION_PRELIGHT      = 1 << 1,
61         SELECTION_FOCUS         = 1 << 2
62 } SelectionType;
63
64
65 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
66
67
68 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
69
70
71 /*
72  *-------------------------------------------------------------------
73  * more misc
74  *-------------------------------------------------------------------
75  */
76
77 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
78 {
79         gint n;
80
81         n = g_list_index(ct->cd->list, info);
82
83         if (n < 0) return FALSE;
84
85         *row = n / ct->columns;
86         *col = n - (*row * ct->columns);
87
88         return TRUE;
89 }
90
91 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
92 {
93         GtkTreeModel *store;
94         gint row, col;
95
96         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
97         if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
98         if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
99         if (column) *column = col;
100
101         return TRUE;
102 }
103
104 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
105 {
106         GtkTreeModel *store;
107         GtkTreeIter p;
108
109         if (row < 0 || col < 0) return NULL;
110
111         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
112         if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
113                 {
114                 GList *list;
115
116                 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
117                 if (!list) return NULL;
118
119                 if (iter) *iter = p;
120
121                 return g_list_nth_data(list, col);
122                 }
123
124         return NULL;
125 }
126
127 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
128 {
129         GtkTreePath *tpath;
130         GtkTreeViewColumn *column;
131         GtkTreeModel *store;
132         GtkTreeIter row;
133         GList *list;
134         gint n;
135
136         if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
137                                            &tpath, &column, NULL, NULL))
138                 return NULL;
139
140         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
141         gtk_tree_model_get_iter(store, &row, tpath);
142         gtk_tree_path_free(tpath);
143
144         gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
145         if (!list) return NULL;
146
147         n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
148         if (iter) *iter = row;
149         return g_list_nth_data(list, n);
150 }
151
152 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
153 {
154         if (bytes)
155                 {
156                 gint64 b = 0;
157                 GList *work;
158
159                 work = ct->cd->list;
160                 while (work)
161                         {
162                         CollectInfo *ci = work->data;
163                         work = work->next;
164                         b += ci->fd->size;
165                         }
166
167                 *bytes = b;
168                 }
169
170         return g_list_length(ct->cd->list);
171 }
172
173 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
174 {
175         if (bytes)
176                 {
177                 gint64 b = 0;
178                 GList *work;
179
180                 work = ct->selection;
181                 while (work)
182                         {
183                         CollectInfo *ci = work->data;
184                         work = work->next;
185                         b += ci->fd->size;
186                         }
187
188                 *bytes = b;
189                 }
190
191         return g_list_length(ct->selection);
192 }
193
194 static void collection_table_update_status(CollectTable *ct)
195 {
196         gchar *buf;
197         guint n;
198         gint64 n_bytes = 0;
199         guint s;
200         gint64 s_bytes = 0;
201
202         if (!ct->status_label) return;
203
204         n = collection_table_list_count(ct, &n_bytes);
205         s = collection_table_selection_count(ct, &s_bytes);
206
207         if (s > 0)
208                 {
209                 gchar *b = text_from_size_abrev(n_bytes);
210                 gchar *sb = text_from_size_abrev(s_bytes);
211                 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
212                 g_free(b);
213                 g_free(sb);
214                 }
215         else if (n > 0)
216                 {
217                 gchar *b = text_from_size_abrev(n_bytes);
218                 buf = g_strdup_printf(_("%s, %d images"), b, n);
219                 g_free(b);
220                 }
221         else
222                 {
223                 buf = g_strdup(_("Empty"));
224                 }
225
226         gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
227         g_free(buf);
228 }
229
230 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
231 {
232         gchar *text;
233
234         if (!ct->extra_label) return;
235
236         if (loading)
237                 text = _("Loading thumbs...");
238         else
239                 text = " ";
240
241         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
242         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
243 }
244
245 static void collection_table_toggle_filenames(CollectTable *ct)
246 {
247         GtkAllocation allocation;
248         ct->show_text = !ct->show_text;
249         options->show_icon_names = ct->show_text;
250
251         gtk_widget_get_allocation(ct->listview, &allocation);
252         collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
253 }
254
255 static gint collection_table_get_icon_width(CollectTable *ct)
256 {
257         gint width;
258
259         if (!ct->show_text) return options->thumbnails.max_width;
260
261         width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
262         if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
263         if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
264
265         return width;
266 }
267
268 /*
269  *-------------------------------------------------------------------
270  * cell updates
271  *-------------------------------------------------------------------
272  */
273
274 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
275 {
276         GtkTreeModel *store;
277         GList *list;
278
279         if (!info) return;
280
281         if (info->flag_mask == value) return;
282         info->flag_mask = value;
283
284         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
285         if (iter)
286                 {
287                 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
288                 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
289                 }
290         else
291                 {
292                 GtkTreeIter row;
293
294                 if (collection_table_find_iter(ct, info, &row, NULL))
295                         {
296                         gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
297                         if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
298                         }
299                 }
300 }
301
302 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
303 {
304         if (!info) return;
305
306         collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
307 }
308
309 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
310 {
311         if (!info) return;
312
313         collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
314 }
315 /*
316  *-------------------------------------------------------------------
317  * selections
318  *-------------------------------------------------------------------
319  */
320
321 static void collection_table_verify_selections(CollectTable *ct)
322 {
323         GList *work;
324
325         work = ct->selection;
326         while (work)
327                 {
328                 CollectInfo *info = work->data;
329                 work = work->next;
330                 if (!g_list_find(ct->cd->list, info))
331                         {
332                         ct->selection = g_list_remove(ct->selection, info);
333                         }
334                 }
335 }
336
337 void collection_table_select_all(CollectTable *ct)
338 {
339         GList *work;
340
341         g_list_free(ct->selection);
342         ct->selection = NULL;
343
344         work = ct->cd->list;
345         while (work)
346                 {
347                 ct->selection = g_list_append(ct->selection, work->data);
348                 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
349                 work = work->next;
350                 }
351
352         collection_table_update_status(ct);
353 }
354
355 void collection_table_unselect_all(CollectTable *ct)
356 {
357         GList *work;
358
359         work = ct->selection;
360         while (work)
361                 {
362                 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
363                 work = work->next;
364                 }
365
366         g_list_free(ct->selection);
367         ct->selection = NULL;
368
369         collection_table_update_status(ct);
370 }
371
372 /* Invert the current collection's selection */
373 static void collection_table_select_invert_all(CollectTable *ct)
374 {
375         GList *work;
376         GList *new_selection = NULL;
377
378         work = ct->cd->list;
379         while (work)
380                 {
381                 CollectInfo *info = work->data;
382
383                 if (INFO_SELECTED(info))
384                         {
385                         collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
386                         }
387                 else
388                         {
389                         new_selection = g_list_append(new_selection, info);
390                         collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
391
392                         }
393
394                 work = work->next;
395                 }
396
397         g_list_free(ct->selection);
398         ct->selection = new_selection;
399
400         collection_table_update_status(ct);
401 }
402
403 static void collection_table_select(CollectTable *ct, CollectInfo *info)
404 {
405         ct->prev_selection = info;
406
407         if (!info || INFO_SELECTED(info)) return;
408
409         ct->selection = g_list_append(ct->selection, info);
410         collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
411
412         collection_table_update_status(ct);
413 }
414
415 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
416 {
417         ct->prev_selection = info;
418
419         if (!info || !INFO_SELECTED(info) ) return;
420
421         ct->selection = g_list_remove(ct->selection, info);
422         collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
423
424         collection_table_update_status(ct);
425 }
426
427 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
428 {
429         if (select)
430                 {
431                 collection_table_select(ct, info);
432                 }
433         else
434                 {
435                 collection_table_unselect(ct, info);
436                 }
437 }
438
439 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
440 {
441         gint row1, col1;
442         gint row2, col2;
443         gint t;
444         gint i, j;
445
446         if (!collection_table_find_position(ct, start, &row1, &col1) ||
447             !collection_table_find_position(ct, end, &row2, &col2) ) return;
448
449         ct->prev_selection = end;
450
451         if (!options->collections.rectangular_selection)
452                 {
453                 GList *work;
454                 CollectInfo *info;
455
456                 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
457                         {
458                         info = start;
459                         start = end;
460                         end = info;
461                         }
462
463                 work = g_list_find(ct->cd->list, start);
464                 while (work)
465                         {
466                         info = work->data;
467                         collection_table_select_util(ct, info, select);
468
469                         if (work->data != end)
470                                 work = work->next;
471                         else
472                                 work = NULL;
473                         }
474                 return;
475                 }
476
477         if (row2 < row1)
478                 {
479                 t = row1;
480                 row1 = row2;
481                 row2 = t;
482                 }
483         if (col2 < col1)
484                 {
485                 t = col1;
486                 col1 = col2;
487                 col2 = t;
488                 }
489
490         DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
491
492         for (i = row1; i <= row2; i++)
493                 {
494                 for (j = col1; j <= col2; j++)
495                         {
496                         CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
497                         if (info) collection_table_select_util(ct, info, select);
498                         }
499                 }
500 }
501
502 GList *collection_table_selection_get_list(CollectTable *ct)
503 {
504         return collection_list_to_filelist(ct->selection);
505 }
506
507 static GList *collection_table_get_list(CollectTable *ct)
508 {
509         return collection_list_to_filelist(ct->cd->list);
510 }
511
512 /*
513  *-------------------------------------------------------------------
514  * tooltip type window
515  *-------------------------------------------------------------------
516  */
517
518 static void tip_show(CollectTable *ct)
519 {
520         GtkWidget *label;
521         gint x, y;
522
523         if (ct->tip_window) return;
524
525         gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
526
527         ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
528         if (!ct->tip_info) return;
529
530         ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
531         gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
532         gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
533
534         label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
535
536         g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
537         gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
538         gtk_widget_show(label);
539
540         gdk_window_get_pointer(NULL, &x, &y, NULL);
541
542 #if GTK_CHECK_VERSION(2,20,0)
543         if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
544 #else
545         if (!GTK_WIDGET_REALIZED(ct->tip_window)) gtk_widget_realize(ct->tip_window);
546 #endif
547         gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
548         gtk_widget_show(ct->tip_window);
549 }
550
551 static void tip_hide(CollectTable *ct)
552 {
553         if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
554         ct->tip_window = NULL;
555 }
556
557 static gboolean tip_schedule_cb(gpointer data)
558 {
559         CollectTable *ct = data;
560
561         if (!ct->tip_delay_id) return FALSE;
562
563         tip_show(ct);
564
565         ct->tip_delay_id = 0;
566         return FALSE;
567 }
568
569 static void tip_schedule(CollectTable *ct)
570 {
571         tip_hide(ct);
572
573         if (ct->tip_delay_id)
574                 {
575                 g_source_remove(ct->tip_delay_id);
576                 ct->tip_delay_id = 0;
577                 }
578
579         ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
580 }
581
582 static void tip_unschedule(CollectTable *ct)
583 {
584         tip_hide(ct);
585
586         if (ct->tip_delay_id)
587                 {
588                 g_source_remove(ct->tip_delay_id);
589                 ct->tip_delay_id = 0;
590                 }
591 }
592
593 static void tip_update(CollectTable *ct, CollectInfo *info)
594 {
595         tip_schedule(ct);
596
597         if (ct->tip_window)
598                 {
599                 gint x, y;
600
601                 gdk_window_get_pointer(NULL, &x, &y, NULL);
602                 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
603
604                 if (info != ct->tip_info)
605                         {
606                         GtkWidget *label;
607
608                         ct->tip_info = info;
609
610                         if (!ct->tip_info)
611                                 {
612                                 return;
613                                 }
614
615                         label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
616                         gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
617                         }
618                 }
619 }
620
621 /*
622  *-------------------------------------------------------------------
623  * popup menus
624  *-------------------------------------------------------------------
625  */
626
627 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
628 {
629         CollectTable *ct = data;
630
631         collection_dialog_save_as(NULL, ct->cd);
632 }
633
634 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
635 {
636         CollectTable *ct = data;
637
638         if (!ct->cd->path)
639                 {
640                 collection_table_popup_save_as_cb(widget, data);
641                 return;
642                 }
643
644         if (!collection_save(ct->cd, ct->cd->path))
645                 {
646                 log_printf("failed saving to collection path: %s\n", ct->cd->path);
647                 }
648 }
649
650 static GList *collection_table_popup_file_list(CollectTable *ct)
651 {
652         if (!ct->click_info) return NULL;
653
654         if (INFO_SELECTED(ct->click_info))
655                 {
656                 return collection_table_selection_get_list(ct);
657                 }
658
659         return g_list_append(NULL, file_data_ref(ct->click_info->fd));
660 }
661
662 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
663 {
664         CollectTable *ct;
665         const gchar *key = data;
666
667         ct = submenu_item_get_data(widget);
668
669         if (!ct) return;
670
671         file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
672 }
673
674 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
675 {
676         CollectTable *ct = data;
677
678         file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
679 }
680
681 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
682 {
683         CollectTable *ct = data;
684
685         file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
686 }
687
688 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
689 {
690         CollectTable *ct = data;
691
692         file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
693 }
694
695 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
696 {
697         CollectTable *ct = data;
698
699         file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
700 }
701
702 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
703 {
704         CollectTable *ct = data;
705
706         file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct));
707 }
708
709 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
710 {
711         CollectTable *ct;
712         SortType type;
713
714         ct = submenu_item_get_data(widget);
715
716         if (!ct) return;
717
718         type = (SortType)GPOINTER_TO_INT(data);
719
720         collection_set_sort_method(ct->cd, type);
721 }
722
723 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
724 {
725         CollectTable *ct;
726
727         ct = submenu_item_get_data(widget);
728
729         if (!ct) return;
730
731         collection_randomize(ct->cd);
732 }
733
734 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
735 {
736         CollectTable *ct = data;
737
738         if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
739                 {
740                 view_window_new_from_collection(ct->cd, ct->click_info);
741                 }
742 }
743
744 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
745 {
746         CollectTable *ct = data;
747
748         if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
749                 {
750                 layout_image_set_collection(NULL, ct->cd, ct->click_info);
751                 }
752 }
753
754 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
755 {
756         CollectTable *ct = data;
757
758         collection_table_select_all(ct);
759         ct->prev_selection= ct->click_info;
760 }
761
762 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
763 {
764         CollectTable *ct = data;
765
766         collection_table_unselect_all(ct);
767         ct->prev_selection= ct->click_info;
768 }
769
770 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
771 {
772         CollectTable *ct = data;
773
774         collection_table_select_invert_all(ct);
775         ct->prev_selection= ct->click_info;
776 }
777
778 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
779 {
780         CollectTable *ct = data;
781         GList *list;
782
783         if (!ct->click_info) return;
784
785         if (INFO_SELECTED(ct->click_info))
786                 {
787                 list = g_list_copy(ct->selection);
788                 }
789         else
790                 {
791                 list = g_list_append(NULL, ct->click_info);
792                 }
793
794         collection_remove_by_info_list(ct->cd, list);
795         g_list_free(list);
796 }
797
798 static void collection_table_popup_add_filelist_cb(GtkWidget *widget, gpointer data)
799 {
800         CollectTable *ct = data;
801         GList *list;
802
803         list = layout_list(NULL);
804
805         if (list)
806                 {
807                 collection_table_add_filelist(ct, list);
808                 filelist_free(list);
809                 }
810 }
811
812 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
813 {
814         CollectTable *ct = data;
815
816         collection_dialog_append(NULL, ct->cd);
817 }
818
819 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
820 {
821         CollectTable *ct = data;
822         DupeWindow *dw;
823
824         dw = dupe_window_new(DUPE_MATCH_NAME);
825         dupe_window_add_collection(dw, ct->cd);
826 }
827
828 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
829 {
830         CollectTable *ct = data;
831         FileData *fd;
832
833         fd = (ct->click_info) ? ct->click_info->fd : NULL;
834
835         print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), ct->listview);
836 }
837
838 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
839 {
840         CollectTable *ct = data;
841
842         collection_table_toggle_filenames(ct);
843 }
844
845 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
846 {
847         CollectTable *ct = data;
848
849         collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
850         ct->click_info = NULL;
851         ct->popup = NULL;
852
853         filelist_free(ct->drop_list);
854         ct->drop_list = NULL;
855         ct->drop_info = NULL;
856
857         filelist_free(ct->editmenu_fd_list);
858         ct->editmenu_fd_list = NULL;
859 }
860
861 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
862 {
863         GtkWidget *menu;
864         GtkWidget *item;
865         GtkWidget *submenu;
866
867         menu = popup_menu_short_lived();
868
869         g_signal_connect(G_OBJECT(menu), "destroy",
870                          G_CALLBACK(collection_table_popup_destroy_cb), ct);
871
872         menu_item_add_sensitive(menu, _("_View"), over_icon,
873                         G_CALLBACK(collection_table_popup_view_cb), ct);
874         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
875                         G_CALLBACK(collection_table_popup_view_new_cb), ct);
876         menu_item_add_divider(menu);
877         menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
878                         G_CALLBACK(collection_table_popup_remove_cb), ct);
879
880         menu_item_add_stock(menu, _("Append from file list"), GTK_STOCK_ADD,
881                         G_CALLBACK(collection_table_popup_add_filelist_cb), ct);
882         menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
883                         G_CALLBACK(collection_table_popup_add_collection_cb), ct);
884         menu_item_add_divider(menu);
885
886         item = menu_item_add(menu, _("_Selection"), NULL, NULL);
887         submenu = gtk_menu_new();
888         menu_item_add(submenu, _("Select all"),
889                         G_CALLBACK(collection_table_popup_selectall_cb), ct);
890         menu_item_add(submenu, _("Select none"),
891                         G_CALLBACK(collection_table_popup_unselectall_cb), ct);
892         menu_item_add(submenu, _("Invert selection"),
893                         G_CALLBACK(collection_table_popup_select_invert_cb), ct);
894         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
895         menu_item_add_divider(menu);
896
897         
898         ct->editmenu_fd_list = collection_table_selection_get_list(ct);
899         submenu_add_edit(menu, &item,
900                         G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
901         gtk_widget_set_sensitive(item, over_icon);
902
903         menu_item_add_divider(menu);
904         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
905                         G_CALLBACK(collection_table_popup_copy_cb), ct);
906         menu_item_add_sensitive(menu, _("_Move..."), over_icon,
907                         G_CALLBACK(collection_table_popup_move_cb), ct);
908         menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
909                         G_CALLBACK(collection_table_popup_rename_cb), ct);
910         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
911                         G_CALLBACK(collection_table_popup_delete_cb), ct);
912         menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
913                                 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
914         menu_item_add_divider(menu);
915
916         submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
917         menu_item_add_divider(submenu);
918         menu_item_add(submenu, _("Randomize"),
919                         G_CALLBACK(collection_table_popup_randomize_cb), ct);
920         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
921         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
922
923         menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
924                         G_CALLBACK(collection_table_popup_show_names_cb), ct);
925         menu_item_add_divider(menu);
926         menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
927                         G_CALLBACK(collection_table_popup_save_cb), ct);
928         menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
929                         G_CALLBACK(collection_table_popup_save_as_cb), ct);
930         menu_item_add_divider(menu);
931         menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
932                         G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
933         menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
934                         G_CALLBACK(collection_table_popup_print_cb), ct);
935
936         return menu;
937 }
938 /*
939  *-------------------------------------------------------------------
940  * keyboard callbacks
941  *-------------------------------------------------------------------
942  */
943
944 static void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
945 {
946         GtkTreeIter iter;
947         gint row, col;
948
949         if (g_list_find(ct->cd->list, ct->focus_info))
950                 {
951                 if (info == ct->focus_info)
952                         {
953                         /* ensure focus row col are correct */
954                         collection_table_find_position(ct, ct->focus_info,
955                                                        &ct->focus_row, &ct->focus_column);
956                         return;
957                         }
958                 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
959                 }
960
961         if (!collection_table_find_position(ct, info, &row, &col))
962                 {
963                 ct->focus_info = NULL;
964                 ct->focus_row = -1;
965                 ct->focus_column = -1;
966                 return;
967                 }
968
969         ct->focus_info = info;
970         ct->focus_row = row;
971         ct->focus_column = col;
972         collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
973
974         if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
975                 {
976                 GtkTreePath *tpath;
977                 GtkTreeViewColumn *column;
978                 GtkTreeModel *store;
979
980                 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
981
982                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
983                 tpath = gtk_tree_model_get_path(store, &iter);
984                 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
985                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
986                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
987                 gtk_tree_path_free(tpath);
988                 }
989 }
990
991 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
992 {
993         gint new_row;
994         gint new_col;
995
996         if (relative)
997                 {
998                 new_row = ct->focus_row;
999                 new_col = ct->focus_column;
1000
1001                 new_row += row;
1002                 if (new_row < 0) new_row = 0;
1003                 if (new_row >= ct->rows) new_row = ct->rows - 1;
1004
1005                 while (col != 0)
1006                         {
1007                         if (col < 0)
1008                                 {
1009                                 new_col--;
1010                                 col++;
1011                                 }
1012                         else
1013                                 {
1014                                 new_col++;
1015                                 col--;
1016                                 }
1017
1018                         if (new_col < 0)
1019                                 {
1020                                 if (new_row > 0)
1021                                         {
1022                                         new_row--;
1023                                         new_col = ct->columns - 1;
1024                                         }
1025                                 else
1026                                         {
1027                                         new_col = 0;
1028                                         }
1029                                 }
1030                         if (new_col >= ct->columns)
1031                                 {
1032                                 if (new_row < ct->rows - 1)
1033                                         {
1034                                         new_row++;
1035                                         new_col = 0;
1036                                         }
1037                                 else
1038                                         {
1039                                         new_col = ct->columns - 1;
1040                                         }
1041                                 }
1042                         }
1043                 }
1044         else
1045                 {
1046                 new_row = row;
1047                 new_col = col;
1048
1049                 if (new_row >= ct->rows)
1050                         {
1051                         if (ct->rows > 0)
1052                                 new_row = ct->rows - 1;
1053                         else
1054                                 new_row = 0;
1055                         new_col = ct->columns - 1;
1056                         }
1057                 if (new_col >= ct->columns) new_col = ct->columns - 1;
1058                 }
1059
1060         if (new_row == ct->rows - 1)
1061                 {
1062                 gint l;
1063
1064                 /* if we moved beyond the last image, go to the last image */
1065
1066                 l = g_list_length(ct->cd->list);
1067                 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1068                 if (new_col >= l) new_col = l - 1;
1069                 }
1070
1071         if (new_row == -1 || new_col == -1)
1072                 {
1073                 if (!ct->cd->list) return;
1074                 new_row = new_col = 0;
1075                 }
1076
1077         collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1078 }
1079
1080 static void collection_table_update_focus(CollectTable *ct)
1081 {
1082         gint new_row = 0;
1083         gint new_col = 0;
1084
1085         if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1086                 {
1087                 /* first find the old focus, if it exists and is valid */
1088                 }
1089         else
1090                 {
1091                 /* (try to) stay where we were */
1092                 new_row = ct->focus_row;
1093                 new_col = ct->focus_column;
1094                 }
1095
1096         collection_table_move_focus(ct, new_row, new_col, FALSE);
1097 }
1098
1099 /* used to figure the page up/down distances */
1100 static gint page_height(CollectTable *ct)
1101 {
1102         GtkAdjustment *adj;
1103         gint page_size;
1104         gint row_height;
1105         gint ret;
1106
1107         adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1108         page_size = (gint)gtk_adjustment_get_page_increment(adj);
1109
1110         row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1111         if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1112
1113         ret = page_size / row_height;
1114         if (ret < 1) ret = 1;
1115
1116         return ret;
1117 }
1118
1119 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1120 {
1121         CollectTable *ct = data;
1122         GtkTreeModel *store;
1123         GtkTreeIter iter;
1124         gint column;
1125         GtkTreePath *tpath;
1126         gint cw, ch;
1127
1128         if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1129         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1130         tpath = gtk_tree_model_get_path(store, &iter);
1131         tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1132         gtk_tree_path_free(tpath);
1133         *y += ch;
1134         popup_menu_position_clamp(menu, x, y, 0);
1135 }
1136
1137 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1138 {
1139         CollectTable *ct = data;
1140         gint focus_row = 0;
1141         gint focus_col = 0;
1142         CollectInfo *info;
1143         gboolean stop_signal = TRUE;
1144
1145         switch (event->keyval)
1146                 {
1147                 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1148                         focus_col = -1;
1149                         break;
1150                 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1151                         focus_col = 1;
1152                         break;
1153                 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1154                         focus_row = -1;
1155                         break;
1156                 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1157                         focus_row = 1;
1158                         break;
1159                 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1160                         focus_row = -page_height(ct);
1161                         break;
1162                 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1163                         focus_row = page_height(ct);
1164                         break;
1165                 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1166                         focus_row = -ct->focus_row;
1167                         focus_col = -ct->focus_column;
1168                         break;
1169                 case GDK_KEY_End: case GDK_KEY_KP_End:
1170                         focus_row = ct->rows - 1 - ct->focus_row;
1171                         focus_col = ct->columns - 1 - ct->focus_column;
1172                         break;
1173                 case GDK_KEY_space:
1174                         info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1175                         if (info)
1176                                 {
1177                                 ct->click_info = info;
1178                                 if (event->state & GDK_CONTROL_MASK)
1179                                         {
1180                                         collection_table_select_util(ct, info, !INFO_SELECTED(info));
1181                                         }
1182                                 else
1183                                         {
1184                                         collection_table_unselect_all(ct);
1185                                         collection_table_select(ct, info);
1186                                         }
1187                                 }
1188                         break;
1189                 case 'T': case 't':
1190                         if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1191                         break;
1192                 case GDK_KEY_Menu:
1193                 case GDK_KEY_F10:
1194                         info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1195                         ct->click_info = info;
1196
1197                         collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1198                         tip_unschedule(ct);
1199
1200                         ct->popup = collection_table_popup_menu(ct, (info != NULL));
1201                         gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1202                         break;
1203                 default:
1204                         stop_signal = FALSE;
1205                         break;
1206                 }
1207
1208         if (focus_row != 0 || focus_col != 0)
1209                 {
1210                 CollectInfo *new_info;
1211                 CollectInfo *old_info;
1212
1213                 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1214                 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1215                 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1216
1217                 if (new_info != old_info)
1218                         {
1219                         if (event->state & GDK_SHIFT_MASK)
1220                                 {
1221                                 if (!options->collections.rectangular_selection)
1222                                         {
1223                                         collection_table_select_region_util(ct, old_info, new_info, FALSE);
1224                                         }
1225                                 else
1226                                         {
1227                                         collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1228                                         }
1229                                 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1230                                 }
1231                         else if (event->state & GDK_CONTROL_MASK)
1232                                 {
1233                                 ct->click_info = new_info;
1234                                 }
1235                         else
1236                                 {
1237                                 ct->click_info = new_info;
1238                                 collection_table_unselect_all(ct);
1239                                 collection_table_select(ct, new_info);
1240                                 }
1241                         }
1242                 }
1243
1244         if (stop_signal)
1245                 {
1246 #if 0
1247                 g_signal_stop_emission_by_name(GTK_OBJECT(widget), "key_press_event");
1248 #endif
1249                 tip_unschedule(ct);
1250                 }
1251
1252         return stop_signal;
1253 }
1254
1255 /*
1256  *-------------------------------------------------------------------
1257  * insert marker
1258  *-------------------------------------------------------------------
1259  */
1260
1261 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1262                                                  gboolean use_coord, gint x, gint y)
1263 {
1264         CollectInfo *info = NULL;
1265         GtkTreeModel *store;
1266         GtkTreeIter iter;
1267         GtkTreePath *tpath;
1268         GtkTreeViewColumn *column;
1269
1270         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1271
1272         if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1273
1274         if (source)
1275                 {
1276                 gint col;
1277                 if (collection_table_find_iter(ct, source, &iter, &col))
1278                         {
1279                         tpath = gtk_tree_model_get_path(store, &iter);
1280                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1281                         gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1282                         gtk_tree_path_free(tpath);
1283
1284                         info = source;
1285                         *after = !!(x > cell->x + (cell->width / 2));
1286                         }
1287                 return info;
1288                 }
1289
1290         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1291                                           &tpath, &column, NULL, NULL))
1292                 {
1293                 GList *list;
1294                 gint n;
1295
1296                 gtk_tree_model_get_iter(store, &iter, tpath);
1297                 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1298
1299                 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1300                 info = g_list_nth_data(list, n);
1301
1302                 if (info)
1303                         {
1304                         gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1305                         *after = !!(x > cell->x + (cell->width / 2));
1306                         }
1307
1308                 gtk_tree_path_free(tpath);
1309                 }
1310
1311         if (info == NULL)
1312                 {
1313                 GList *work;
1314
1315                 work = g_list_last(ct->cd->list);
1316                 if (work)
1317                         {
1318                         gint col;
1319
1320                         info = work->data;
1321                         *after = TRUE;
1322
1323                         if (collection_table_find_iter(ct, info, &iter, &col))
1324                                 {
1325                                 tpath = gtk_tree_model_get_path(store, &iter);
1326                                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1327                                 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1328                                 gtk_tree_path_free(tpath);
1329                                 }
1330                         }
1331                 }
1332
1333         return info;
1334 }
1335
1336 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1337 {
1338         CollectInfo *info;
1339         GdkRectangle cell;
1340         gboolean after = FALSE;
1341
1342         info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1343
1344         if (info && after)
1345                 {
1346                 GList *work;
1347
1348                 work = g_list_find(ct->cd->list, info);
1349                 if (work && work->next)
1350                         {
1351                         info = work->next->data;
1352                         }
1353                 else
1354                         {
1355                         info = NULL;
1356                         }
1357                 }
1358
1359         return info;
1360 }
1361
1362 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1363 {
1364         gint row, col;
1365         gboolean after = FALSE;
1366         GdkRectangle cell;
1367
1368         if (!enable)
1369                 {
1370                 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1371                 ct->marker_window = NULL;
1372
1373                 return;
1374                 }
1375
1376         info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1377
1378         /* this setting does not take into account (after), but since it is not really used... */
1379         ct->marker_info = info;
1380
1381         row = -1;
1382         col = -1;
1383
1384         if (!ct->marker_window)
1385                 {
1386                 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1387                 GdkWindowAttr attributes;
1388                 gint attributes_mask;
1389                 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1390                 gint w = gdk_pixbuf_get_width(pb);
1391                 gint h = gdk_pixbuf_get_height(pb);
1392
1393                 attributes.window_type = GDK_WINDOW_CHILD;
1394                 attributes.wclass = GDK_INPUT_OUTPUT;
1395                 attributes.width = w;
1396                 attributes.height = h;
1397                 attributes.event_mask = gtk_widget_get_events(ct->listview);
1398                 attributes_mask = 0;
1399
1400                 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1401
1402 #if GTK_CHECK_VERSION(3,0,0)
1403                 cairo_region_t *mask;
1404                 cairo_pattern_t *pattern;
1405                 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1406                 cairo_t *cr = cairo_create(img);
1407                 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1408                 cairo_paint(cr);
1409                 pattern = cairo_pattern_create_for_surface(img);
1410                 mask = gdk_cairo_region_create_from_surface(img);
1411                 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1412                 gdk_window_set_background_pattern(ct->marker_window, pattern);
1413                 cairo_region_destroy(mask);
1414                 cairo_pattern_destroy(pattern);
1415                 cairo_destroy(cr);
1416                 cairo_surface_destroy(img);
1417 #else
1418                 GdkPixmap *pixmap;
1419                 GdkBitmap *mask;
1420                 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1421
1422                 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1423                 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1424
1425                 g_object_unref(pixmap);
1426                 if (mask) g_object_unref(mask);
1427 #endif
1428                 g_object_unref(pb);
1429                 }
1430
1431         if (info)
1432                 {
1433                 gint x, y;
1434                 gint w, h;
1435
1436                 w = gdk_window_get_width(ct->marker_window);
1437                 h = gdk_window_get_height(ct->marker_window);
1438
1439                 if (!after)
1440                         {
1441                         x = cell.x;
1442                         }
1443                 else
1444                         {
1445                         x = cell.x + cell.width;
1446                         }
1447                 x -= (w / 2);
1448                 y = cell.y + (cell.height / 2) - (h / 2);
1449
1450                 gdk_window_move(ct->marker_window, x, y);
1451 #if !GTK_CHECK_VERSION(3,0,0)
1452                 gdk_window_clear(ct->marker_window);
1453 #endif
1454                 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1455                 }
1456         else
1457                 {
1458                 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1459                 }
1460 }
1461
1462 /*
1463  *-------------------------------------------------------------------
1464  * mouse drag auto-scroll
1465  *-------------------------------------------------------------------
1466  */
1467
1468 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1469 {
1470         CollectInfo *info;
1471
1472         info = collection_table_find_data_by_coord(ct, x, y, NULL);
1473
1474         if (drop_event)
1475                 {
1476                 tip_unschedule(ct);
1477                 collection_table_insert_marker(ct, info, TRUE);
1478                 }
1479         else
1480                 {
1481                 tip_update(ct, info);
1482                 }
1483 }
1484
1485 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1486 {
1487         CollectTable *ct = data;
1488         GdkWindow *window;
1489         gint x, y;
1490         gint w, h;
1491
1492         if (!ct->drop_idle_id) return FALSE;
1493
1494         window = gtk_widget_get_window(ct->listview);
1495         gdk_window_get_pointer(window, &x, &y, NULL);
1496         w = gdk_window_get_width(window);
1497         h = gdk_window_get_height(window);
1498         if (x >= 0 && x < w && y >= 0 && y < h)
1499                 {
1500                 collection_table_motion_update(ct, x, y, TRUE);
1501                 }
1502
1503         ct->drop_idle_id = 0;
1504         return FALSE;
1505 }
1506
1507 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1508 {
1509         CollectTable *ct = data;
1510
1511         if (!ct->drop_idle_id)
1512                 {
1513                 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1514                 }
1515
1516         return TRUE;
1517 }
1518
1519 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1520 {
1521         if (!scroll)
1522                 {
1523                 if (ct->drop_idle_id)
1524                         {
1525                         g_source_remove(ct->drop_idle_id);
1526                         ct->drop_idle_id = 0;
1527                         }
1528                 widget_auto_scroll_stop(ct->listview);
1529                 collection_table_insert_marker(ct, NULL, FALSE);
1530                 }
1531         else
1532                 {
1533                 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1534                 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1535                                          collection_table_auto_scroll_notify_cb, ct);
1536                 }
1537 }
1538
1539 /*
1540  *-------------------------------------------------------------------
1541  * mouse callbacks
1542  *-------------------------------------------------------------------
1543  */
1544
1545 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1546 {
1547         CollectTable *ct = data;
1548
1549         collection_table_motion_update(ct, (gint)bevent->x, (gint)bevent->y, FALSE);
1550
1551         return FALSE;
1552 }
1553
1554 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1555 {
1556         CollectTable *ct = data;
1557         GtkTreeIter iter;
1558         CollectInfo *info;
1559
1560         tip_unschedule(ct);
1561
1562         info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1563
1564         ct->click_info = info;
1565         collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1566
1567         switch (bevent->button)
1568                 {
1569                 case MOUSE_BUTTON_LEFT:
1570                         if (bevent->type == GDK_2BUTTON_PRESS)
1571                                 {
1572                                 if (info)
1573                                         {
1574                                         layout_image_set_collection(NULL, ct->cd, info);
1575                                         }
1576                                 }
1577 #if GTK_CHECK_VERSION(2,20,0)
1578                         else if (!gtk_widget_has_focus(ct->listview))
1579 #else
1580                         else if (!GTK_WIDGET_HAS_FOCUS(ct->listview))
1581 #endif
1582                                 {
1583                                 gtk_widget_grab_focus(ct->listview);
1584                                 }
1585                         break;
1586                 case MOUSE_BUTTON_RIGHT:
1587                         ct->popup = collection_table_popup_menu(ct, (info != NULL));
1588                         gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1589                         break;
1590                 default:
1591                         break;
1592                 }
1593
1594         return TRUE;
1595 }
1596
1597 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1598 {
1599         CollectTable *ct = data;
1600         GtkTreeIter iter;
1601         CollectInfo *info = NULL;
1602
1603         tip_schedule(ct);
1604
1605         if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1606                 {
1607                 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1608                 }
1609
1610         if (ct->click_info)
1611                 {
1612                 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1613                 }
1614
1615         if (bevent->button == MOUSE_BUTTON_LEFT &&
1616             info && ct->click_info == info)
1617                 {
1618                 collection_table_set_focus(ct, info);
1619
1620                 if (bevent->state & GDK_CONTROL_MASK)
1621                         {
1622                         gboolean select = !INFO_SELECTED(info);
1623
1624                         if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1625                                 {
1626                                 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1627                                 }
1628                         else
1629                                 {
1630                                 collection_table_select_util(ct, info, select);
1631                                 }
1632                         }
1633                 else
1634                         {
1635                         collection_table_unselect_all(ct);
1636
1637                         if ((bevent->state & GDK_SHIFT_MASK) &&
1638                             ct->prev_selection)
1639                                 {
1640                                 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1641                                 }
1642                         else
1643                                 {
1644                                 collection_table_select_util(ct, info, TRUE);
1645                                 }
1646                         }
1647                 }
1648         else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1649                  info && ct->click_info == info)
1650                 {
1651                 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1652                 }
1653
1654         return TRUE;
1655 }
1656
1657 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1658 {
1659         CollectTable *ct = data;
1660
1661         tip_unschedule(ct);
1662         return FALSE;
1663 }
1664
1665 /*
1666  *-------------------------------------------------------------------
1667  * populate, add, insert, etc.
1668  *-------------------------------------------------------------------
1669  */
1670
1671 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1672 {
1673         GList *list;
1674
1675         gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1676         g_list_free(list);
1677
1678         return FALSE;
1679 }
1680
1681 static void collection_table_clear_store(CollectTable *ct)
1682 {
1683         GtkTreeModel *store;
1684
1685         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1686         gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1687
1688         gtk_list_store_clear(GTK_LIST_STORE(store));
1689 }
1690
1691 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1692 {
1693         GtkListStore *store;
1694         GList *list = NULL;
1695         gint i;
1696
1697         for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1698
1699         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1700         gtk_list_store_append(store, iter);
1701         gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1702
1703         return list;
1704 }
1705
1706 static void collection_table_populate(CollectTable *ct, gboolean resize)
1707 {
1708         gint row;
1709         GList *work;
1710
1711         collection_table_verify_selections(ct);
1712
1713         collection_table_clear_store(ct);
1714
1715         if (resize)
1716                 {
1717                 gint i;
1718                 gint thumb_width;
1719
1720                 thumb_width = collection_table_get_icon_width(ct);
1721
1722                 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1723                         {
1724                         GtkTreeViewColumn *column;
1725                         GtkCellRenderer *cell;
1726                         GList *list;
1727
1728                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1729                         gtk_tree_view_column_set_visible(column, (i < ct->columns));
1730                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1731
1732 #if GTK_CHECK_VERSION(2,18,0)
1733                         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1734 #else
1735                         list = gtk_tree_view_column_get_cell_renderers(column);
1736 #endif
1737                         cell = (list) ? list->data : NULL;
1738                         g_list_free(list);
1739
1740                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1741                                 {
1742                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1743                                                              "fixed_height", options->thumbnails.max_height,
1744                                                              "show_text", ct->show_text, NULL);
1745                                 }
1746                         }
1747 #if GTK_CHECK_VERSION(2,20,0)
1748                 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1749 #else
1750                 if (GTK_WIDGET_REALIZED(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1751 #endif
1752                 }
1753
1754         row = -1;
1755         work = ct->cd->list;
1756         while (work)
1757                 {
1758                 GList *list;
1759                 GtkTreeIter iter;
1760
1761                 row++;
1762
1763                 list = collection_table_add_row(ct, &iter);
1764                 while (work && list)
1765                         {
1766                         list->data = work->data;
1767                         list = list->next;
1768                         work = work->next;
1769                         }
1770                 }
1771
1772         ct->rows = row + 1;
1773
1774         collection_table_update_focus(ct);
1775         collection_table_update_status(ct);
1776 }
1777
1778 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1779 {
1780         gint new_cols;
1781         gint thumb_width;
1782
1783         thumb_width = collection_table_get_icon_width(ct);
1784
1785         new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1786         if (new_cols < 1) new_cols = 1;
1787
1788         if (!force && new_cols == ct->columns) return;
1789
1790         ct->columns = new_cols;
1791
1792         collection_table_populate(ct, TRUE);
1793
1794         DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1795 }
1796
1797 static void collection_table_sync(CollectTable *ct)
1798 {
1799         GtkTreeModel *store;
1800         GtkTreeIter iter;
1801         GList *work;
1802         gint r, c;
1803
1804         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1805
1806         r = -1;
1807         c = 0;
1808
1809         work = ct->cd->list;
1810         while (work)
1811                 {
1812                 GList *list;
1813                 r++;
1814                 c = 0;
1815                 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1816                         {
1817                         gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1818                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1819                         }
1820                 else
1821                         {
1822                         list = collection_table_add_row(ct, &iter);
1823                         }
1824
1825                 while (list)
1826                         {
1827                         CollectInfo *info;
1828                         if (work)
1829                                 {
1830                                 info = work->data;
1831                                 work = work->next;
1832                                 c++;
1833                                 }
1834                         else
1835                                 {
1836                                 info = NULL;
1837                                 }
1838                         if (list)
1839                                 {
1840                                 list->data = info;
1841                                 list = list->next;
1842                                 }
1843                         }
1844                 }
1845
1846         r++;
1847         while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1848                 {
1849                 GList *list;
1850
1851                 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1852                 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1853                 g_list_free(list);
1854                 }
1855
1856         ct->rows = r;
1857
1858         collection_table_update_focus(ct);
1859         collection_table_update_status(ct);
1860 }
1861
1862 static gboolean collection_table_sync_idle_cb(gpointer data)
1863 {
1864         CollectTable *ct = data;
1865
1866         if (!ct->sync_idle_id) return FALSE;
1867         g_source_remove(ct->sync_idle_id);
1868         ct->sync_idle_id = 0;
1869
1870         collection_table_sync(ct);
1871         return FALSE;
1872 }
1873
1874 static void collection_table_sync_idle(CollectTable *ct)
1875 {
1876         if (!ct->sync_idle_id)
1877                 {
1878                 /* high priority, the view needs to be resynced before a redraw
1879                  * may contain invalid pointers at this time
1880                  */
1881                 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1882                 }
1883 }
1884
1885 void collection_table_add_filelist(CollectTable *ct, GList *list)
1886 {
1887         GList *work;
1888
1889         if (!list) return;
1890
1891         work = list;
1892         while (work)
1893                 {
1894                 collection_add(ct->cd, (FileData *)work->data, FALSE);
1895                 work = work->next;
1896                 }
1897 }
1898
1899 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
1900 {
1901         GList *work;
1902
1903         if (!list) return;
1904
1905         work = list;
1906         while (work)
1907                 {
1908                 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
1909                 work = work->next;
1910                 }
1911
1912         collection_table_sync_idle(ct);
1913 }
1914
1915 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
1916 {
1917         GList *work;
1918         GList *insert_pos = NULL;
1919         GList *temp;
1920         CollectInfo *info;
1921
1922         if (!info_list) return;
1923
1924         info = collection_table_find_data(ct, row, col, NULL);
1925
1926         if (!info_list->next && info_list->data == info) return;
1927
1928         if (info) insert_pos = g_list_find(ct->cd->list, info);
1929
1930         /* FIXME: this may get slow for large lists */
1931         work = info_list;
1932         while (insert_pos && work)
1933                 {
1934                 if (insert_pos->data == work->data)
1935                         {
1936                         insert_pos = insert_pos->next;
1937                         work = info_list;
1938                         }
1939                 else
1940                         {
1941                         work = work->next;
1942                         }
1943                 }
1944
1945         work = info_list;
1946         while (work)
1947                 {
1948                 ct->cd->list = g_list_remove(ct->cd->list, work->data);
1949                 work = work->next;
1950                 }
1951
1952         /* place them back in */
1953         temp = g_list_copy(info_list);
1954
1955         if (insert_pos)
1956                 {
1957                 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
1958                 }
1959         else if (info)
1960                 {
1961                 ct->cd->list = g_list_concat(temp, ct->cd->list);
1962                 }
1963         else
1964                 {
1965                 ct->cd->list = g_list_concat(ct->cd->list, temp);
1966                 }
1967
1968         ct->cd->changed = TRUE;
1969
1970         collection_table_sync_idle(ct);
1971 }
1972
1973
1974 /*
1975  *-------------------------------------------------------------------
1976  * updating
1977  *-------------------------------------------------------------------
1978  */
1979
1980 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
1981 {
1982         GtkTreeIter iter;
1983         gint row, col;
1984         gdouble value;
1985
1986         if (!info)
1987                 {
1988                 collection_table_update_extras(ct, FALSE, 0.0);
1989                 return;
1990                 }
1991
1992         if (!collection_table_find_position(ct, info, &row, &col)) return;
1993
1994         if (ct->columns != 0 && ct->rows != 0)
1995                 {
1996                 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
1997                 }
1998         else
1999                 {
2000                 value = 0.0;
2001                 }
2002
2003         collection_table_update_extras(ct, TRUE, value);
2004
2005         if (collection_table_find_iter(ct, info, &iter, NULL))
2006                 {
2007                 GtkTreeModel *store;
2008                 GList *list;
2009
2010                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2011                 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2012                 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2013                 }
2014 }
2015
2016 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2017 {
2018         collection_table_sync_idle(ct);
2019 }
2020
2021 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2022 {
2023         collection_table_sync_idle(ct);
2024 }
2025
2026 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2027 {
2028         if (ci && INFO_SELECTED(ci))
2029                 {
2030                 ct->selection = g_list_remove(ct->selection, ci);
2031                 }
2032
2033         collection_table_sync_idle(ct);
2034 }
2035
2036 void collection_table_refresh(CollectTable *ct)
2037 {
2038         collection_table_populate(ct, FALSE);
2039 }
2040
2041 /*
2042  *-------------------------------------------------------------------
2043  * dnd
2044  *-------------------------------------------------------------------
2045  */
2046
2047 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2048 {
2049         GList *d;
2050         GList *f;
2051         GList *work;
2052
2053         if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2054                 return;
2055
2056         f = filelist_filter(f, FALSE);
2057         d = filelist_filter(d, TRUE);
2058
2059         f = filelist_sort_path(f);
2060         d = filelist_sort_path(d);
2061
2062         collection_table_insert_filelist(ct, f, ct->marker_info);
2063
2064         work = g_list_last(d);
2065         while (work)
2066                 {
2067                 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2068                 work = work->prev;
2069                 }
2070
2071         filelist_free(f);
2072         filelist_free(d);
2073 }
2074
2075 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2076 {
2077         GList *work = list;
2078         while (work)
2079                 {
2080                 FileData *fd = work->data;
2081                 work = work->next;
2082                 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2083                 }
2084         collection_table_insert_filelist(ct, list, ct->marker_info);
2085 }
2086
2087
2088 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2089 {
2090         CollectTable *ct = data;
2091
2092         confirm_dir_list_do(ct, ct->drop_list, FALSE);
2093 }
2094
2095 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2096 {
2097         CollectTable *ct = data;
2098
2099         confirm_dir_list_do(ct, ct->drop_list, TRUE);
2100 }
2101
2102 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2103 {
2104         CollectTable *ct = data;
2105
2106         collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2107 }
2108
2109 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2110 {
2111         GtkWidget *menu;
2112
2113         menu = popup_menu_short_lived();
2114         g_signal_connect(G_OBJECT(menu), "destroy",
2115                          G_CALLBACK(collection_table_popup_destroy_cb), ct);
2116
2117         menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2118         menu_item_add_divider(menu);
2119         menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2120                             G_CALLBACK(confirm_dir_list_add), ct);
2121         menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2122                             G_CALLBACK(confirm_dir_list_recurse), ct);
2123         menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2124                             G_CALLBACK(confirm_dir_list_skip), ct);
2125         menu_item_add_divider(menu);
2126         menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2127
2128         return menu;
2129 }
2130
2131 /*
2132  *-------------------------------------------------------------------
2133  * dnd
2134  *-------------------------------------------------------------------
2135  */
2136
2137 static GtkTargetEntry collection_drag_types[] = {
2138         { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2139         { "text/uri-list", 0, TARGET_URI_LIST },
2140         { "text/plain", 0, TARGET_TEXT_PLAIN }
2141 };
2142 static gint n_collection_drag_types = 3;
2143
2144 static GtkTargetEntry collection_drop_types[] = {
2145         { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2146         { "text/uri-list", 0, TARGET_URI_LIST }
2147 };
2148 static gint n_collection_drop_types = 2;
2149
2150
2151 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2152                                      GtkSelectionData *selection_data, guint info,
2153                                      guint time, gpointer data)
2154 {
2155         CollectTable *ct = data;
2156         gboolean selected;
2157         GList *list = NULL;
2158         gchar *uri_text = NULL;
2159         gint total;
2160
2161         if (!ct->click_info) return;
2162
2163         selected = INFO_SELECTED(ct->click_info);
2164
2165         switch (info)
2166                 {
2167                 case TARGET_APP_COLLECTION_MEMBER:
2168                         if (selected)
2169                                 {
2170                                 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2171                                 }
2172                         else
2173                                 {
2174                                 list = g_list_append(NULL, ct->click_info);
2175                                 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2176                                 g_list_free(list);
2177                                 }
2178                         gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2179                                                 8, (guchar *)uri_text, total);
2180                         g_free(uri_text);
2181                         break;
2182                 case TARGET_URI_LIST:
2183                 case TARGET_TEXT_PLAIN:
2184                 default:
2185                         if (selected)
2186                                 {
2187                                 list = collection_table_selection_get_list(ct);
2188                                 }
2189                         else
2190                                 {
2191                                 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2192                                 }
2193                         if (!list) return;
2194
2195                         uri_selection_data_set_uris_from_filelist(selection_data, list);
2196                         filelist_free(list);
2197                         break;
2198                 }
2199 }
2200
2201
2202 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2203                                           gint x, gint y,
2204                                           GtkSelectionData *selection_data, guint info,
2205                                           guint time, gpointer data)
2206 {
2207         CollectTable *ct = data;
2208         GList *list = NULL;
2209         GList *info_list = NULL;
2210         CollectionData *source;
2211         CollectInfo *drop_info;
2212         GList *work;
2213
2214         DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2215
2216         collection_table_scroll(ct, FALSE);
2217         collection_table_insert_marker(ct, NULL, FALSE);
2218
2219         drop_info = collection_table_insert_point(ct, x, y);
2220
2221         switch (info)
2222                 {
2223                 case TARGET_APP_COLLECTION_MEMBER:
2224                         source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2225                         if (source)
2226                                 {
2227                                 if (source == ct->cd)
2228                                         {
2229                                         gint row = -1;
2230                                         gint col = -1;
2231
2232                                         /* it is a move within a collection */
2233                                         filelist_free(list);
2234                                         list = NULL;
2235
2236                                         if (!drop_info)
2237                                                 {
2238                                                 collection_table_move_by_info_list(ct, info_list, -1, -1);
2239                                                 }
2240                                         else if (collection_table_find_position(ct, drop_info, &row, &col))
2241                                                 {
2242                                                 collection_table_move_by_info_list(ct, info_list, row, col);
2243                                                 }
2244                                         }
2245                                 else
2246                                         {
2247                                         /* it is a move/copy across collections */
2248                                         if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2249                                                 {
2250                                                 collection_remove_by_info_list(source, info_list);
2251                                                 }
2252                                         }
2253                                 g_list_free(info_list);
2254                                 }
2255                         break;
2256                 case TARGET_URI_LIST:
2257                         list = uri_filelist_from_gtk_selection_data(selection_data);
2258                         work = list;
2259                         while (work)
2260                                 {
2261                                 FileData *fd = work->data;
2262                                 if (isdir(fd->path))
2263                                         {
2264                                         GtkWidget *menu;
2265
2266                                         ct->drop_list = list;
2267                                         ct->drop_info = drop_info;
2268                                         menu = collection_table_drop_menu(ct);
2269                                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2270                                         return;
2271                                         }
2272                                 work = work->next;
2273                                 }
2274                         break;
2275                 default:
2276                         list = NULL;
2277                         break;
2278                 }
2279
2280         if (list)
2281                 {
2282                 collection_table_insert_filelist(ct, list, drop_info);
2283                 filelist_free(list);
2284                 }
2285 }
2286
2287 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2288 {
2289         CollectTable *ct = data;
2290
2291         if (ct->click_info && ct->click_info->pixbuf)
2292                 {
2293                 gint items;
2294
2295                 if (INFO_SELECTED(ct->click_info))
2296                         items = g_list_length(ct->selection);
2297                 else
2298                         items = 1;
2299                 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2300                 }
2301 }
2302
2303 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2304 {
2305         CollectTable *ct = data;
2306
2307         /* apparently a leave event is not generated on a drop */
2308         tip_unschedule(ct);
2309
2310         collection_table_scroll(ct, FALSE);
2311 }
2312
2313 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2314                                         gint x, gint y, guint time, gpointer data)
2315 {
2316         CollectTable *ct = data;
2317
2318         collection_table_motion_update(ct, x, y, TRUE);
2319         collection_table_scroll(ct, TRUE);
2320
2321         return FALSE;
2322 }
2323
2324 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2325 {
2326         CollectTable *ct = data;
2327
2328         collection_table_scroll(ct, FALSE);
2329 }
2330
2331 static void collection_table_dnd_init(CollectTable *ct)
2332 {
2333         gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2334                             collection_drag_types, n_collection_drag_types,
2335                             GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2336         g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2337                          G_CALLBACK(collection_table_dnd_get), ct);
2338         g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2339                          G_CALLBACK(collection_table_dnd_begin), ct);
2340         g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2341                          G_CALLBACK(collection_table_dnd_end), ct);
2342
2343         gtk_drag_dest_set(ct->listview,
2344                           GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2345                           collection_drop_types, n_collection_drop_types,
2346                           GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2347         g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2348                          G_CALLBACK(collection_table_dnd_motion), ct);
2349         g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2350                          G_CALLBACK(collection_table_dnd_leave), ct);
2351         g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2352                          G_CALLBACK(collection_table_dnd_receive), ct);
2353 }
2354
2355 /*
2356  *-----------------------------------------------------------------------------
2357  * draw, etc.
2358  *-----------------------------------------------------------------------------
2359  */
2360
2361 typedef struct _ColumnData ColumnData;
2362 struct _ColumnData
2363 {
2364         CollectTable *ct;
2365         gint number;
2366 };
2367
2368 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2369                                           GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2370 {
2371         ColumnData *cd = data;
2372         CollectTable *ct;
2373         GtkStyle *style;
2374         GList *list;
2375         CollectInfo *info;
2376         GdkColor color_fg;
2377         GdkColor color_bg;
2378
2379         ct = cd->ct;
2380
2381         gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2382         info = g_list_nth_data(list, cd->number);
2383
2384         style = gtk_widget_get_style(ct->listview);
2385         if (info && (info->flag_mask & SELECTION_SELECTED) )
2386                 {
2387                 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2388                 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2389                 }
2390         else
2391                 {
2392                 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2393                 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2394                 }
2395
2396         if (info && (info->flag_mask & SELECTION_PRELIGHT))
2397                 {
2398 #if 0
2399                 shift_color(&color_fg, -1, 0);
2400 #endif
2401                 shift_color(&color_bg, -1, 0);
2402                 }
2403
2404         if (GQV_IS_CELL_RENDERER_ICON(cell))
2405                 {
2406                 if (info)
2407                         {
2408                         g_object_set(cell,      "pixbuf", info->pixbuf,
2409                                                 "text", info->fd->name,
2410                                                 "cell-background-gdk", &color_bg,
2411                                                 "cell-background-set", TRUE,
2412                                                 "foreground-gdk", &color_fg,
2413                                                 "foreground-set", TRUE,
2414                                                 "has-focus", (ct->focus_info == info), NULL);
2415                         }
2416                 else
2417                         {
2418                         g_object_set(cell,      "pixbuf", NULL,
2419                                                 "text", NULL,
2420                                                 "cell-background-set", FALSE,
2421                                                 "foreground-set", FALSE,
2422                                                 "has-focus", FALSE,  NULL);
2423                         }
2424                 }
2425 }
2426
2427 static void collection_table_append_column(CollectTable *ct, gint n)
2428 {
2429         ColumnData *cd;
2430         GtkTreeViewColumn *column;
2431         GtkCellRenderer *renderer;
2432
2433         column = gtk_tree_view_column_new();
2434         gtk_tree_view_column_set_min_width(column, 0);
2435
2436         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2437         gtk_tree_view_column_set_alignment(column, 0.5);
2438
2439         renderer = gqv_cell_renderer_icon_new();
2440         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2441         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2442                                          "ypad", THUMB_BORDER_PADDING,
2443                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2444
2445         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2446
2447         cd = g_new0(ColumnData, 1);
2448         cd->ct = ct;
2449         cd->number = n;
2450         gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2451
2452         gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2453 }
2454
2455 /*
2456  *-------------------------------------------------------------------
2457  * init, destruction
2458  *-------------------------------------------------------------------
2459  */
2460
2461 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2462 {
2463         CollectTable *ct = data;
2464
2465         if (ct->popup)
2466                 {
2467                 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2468                                                      0, 0, 0, NULL, ct);
2469                 gtk_widget_destroy(ct->popup);
2470                 }
2471
2472         if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2473
2474         tip_unschedule(ct);
2475         collection_table_scroll(ct, FALSE);
2476
2477         g_free(ct);
2478 }
2479
2480 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2481 {
2482         CollectTable *ct = data;
2483
2484         collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2485 }
2486
2487 CollectTable *collection_table_new(CollectionData *cd)
2488 {
2489         CollectTable *ct;
2490         GtkListStore *store;
2491         GtkTreeSelection *selection;
2492         gint i;
2493
2494         ct = g_new0(CollectTable, 1);
2495         
2496         ct->cd = cd;
2497         ct->show_text = options->show_icon_names;
2498
2499         ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2500         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2501         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2502                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2503
2504         store = gtk_list_store_new(1, G_TYPE_POINTER);
2505         ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2506         g_object_unref(store);
2507
2508         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2509         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2510
2511         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2512         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2513
2514         for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2515                 {
2516                 collection_table_append_column(ct, i);
2517                 }
2518
2519         /* zero width column to hide tree view focus, we draw it ourselves */
2520         collection_table_append_column(ct, i);
2521         /* end column to fill white space */
2522         collection_table_append_column(ct, i);
2523
2524         g_signal_connect(G_OBJECT(ct->listview), "destroy",
2525                          G_CALLBACK(collection_table_destroy), ct);
2526         g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2527                          G_CALLBACK(collection_table_sized), ct);
2528         g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2529                          G_CALLBACK(collection_table_press_key_cb), ct);
2530
2531         gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2532         gtk_widget_show(ct->listview);
2533
2534         collection_table_dnd_init(ct);
2535
2536         gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2537                               GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2538         g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2539                          G_CALLBACK(collection_table_press_cb), ct);
2540         g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2541                          G_CALLBACK(collection_table_release_cb), ct);
2542         g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2543                          G_CALLBACK(collection_table_motion_cb), ct);
2544         g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2545                          G_CALLBACK(collection_table_leave_cb), ct);
2546
2547         return ct;
2548 }
2549
2550 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2551 {
2552         ct->status_label = status;
2553         ct->extra_label = extra;
2554         collection_table_update_status(ct);
2555         collection_table_update_extras(ct, FALSE, 0.0);
2556 }
2557
2558 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2559 {
2560         return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2561 }
2562
2563 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */