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