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