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