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