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