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