clang-tidy: readability-isolate-declaration
[geeqie.git] / src / view-file / view-file-icon.cc
1 /*
2  * Copyright (C) 2006 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 "view-file-icon.h"
24
25 #include "cellrenderericon.h"
26 #include "collect.h"
27 #include "dnd.h"
28 #include "img-view.h"
29 #include "layout-image.h"
30 #include "metadata.h"
31 #include "misc.h"
32 #include "utilops.h"
33 #include "ui-fileops.h"
34 #include "ui-misc.h"
35 #include "ui-tree-edit.h"
36 #include "uri-utils.h"
37 #include "view-file.h"
38
39 /* between these, the icon width is increased by thumb_max_width / 2 */
40 enum {
41         THUMB_MIN_ICON_WIDTH = 128,
42         THUMB_MAX_ICON_WIDTH = 160
43 };
44 #define THUMB_MIN_ICON_WIDTH_WITH_MARKS TOGGLE_SPACING * FILEDATA_MARKS_SIZE
45
46 enum {
47         VFICON_MAX_COLUMNS = 32,
48         THUMB_BORDER_PADDING = 2
49 };
50
51 enum {
52         VFICON_TIP_DELAY = 500
53 };
54
55 enum {
56         FILE_COLUMN_POINTER = 0,
57         FILE_COLUMN_COUNT
58 };
59
60 static void vficon_toggle_filenames(ViewFile *vf);
61 static void vficon_selection_remove(ViewFile *vf, FileData *fd, SelectionType mask, GtkTreeIter *iter);
62 static void vficon_move_focus(ViewFile *vf, gint row, gint col, gboolean relative);
63 static void vficon_set_focus(ViewFile *vf, FileData *fd);
64 static void vficon_populate_at_new_size(ViewFile *vf, gint w, gint h, gboolean force);
65
66
67 /*
68  *-----------------------------------------------------------------------------
69  * pop-up menu
70  *-----------------------------------------------------------------------------
71  */
72
73 GList *vficon_selection_get_one(ViewFile *, FileData *fd)
74 {
75         return g_list_prepend(filelist_copy(fd->sidecar_files), file_data_ref(fd));
76 }
77
78 GList *vficon_pop_menu_file_list(ViewFile *vf)
79 {
80         if (!VFICON(vf)->click_fd) return nullptr;
81
82         if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
83                 {
84                 return vf_selection_get_list(vf);
85                 }
86
87         return vficon_selection_get_one(vf, VFICON(vf)->click_fd);
88 }
89
90 void vficon_pop_menu_view_cb(GtkWidget *, gpointer data)
91 {
92         auto vf = static_cast<ViewFile *>(data);
93
94         if (!VFICON(vf)->click_fd) return;
95
96         if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
97                 {
98                 GList *list;
99
100                 list = vf_selection_get_list(vf);
101                 view_window_new_from_list(list);
102                 filelist_free(list);
103                 }
104         else
105                 {
106                 view_window_new(VFICON(vf)->click_fd);
107                 }
108 }
109
110 void vficon_pop_menu_rename_cb(GtkWidget *, gpointer data)
111 {
112         auto vf = static_cast<ViewFile *>(data);
113
114         file_util_rename(nullptr, vf_pop_menu_file_list(vf), vf->listview);
115 }
116
117 void vficon_pop_menu_show_names_cb(GtkWidget *, gpointer data)
118 {
119         auto vf = static_cast<ViewFile *>(data);
120
121         vficon_toggle_filenames(vf);
122 }
123
124 static void vficon_toggle_star_rating(ViewFile *vf)
125 {
126         GtkAllocation allocation;
127
128         options->show_star_rating = !options->show_star_rating;
129
130         gtk_widget_get_allocation(vf->listview, &allocation);
131         vficon_populate_at_new_size(vf, allocation.width, allocation.height, TRUE);
132 }
133
134 void vficon_pop_menu_show_star_rating_cb(GtkWidget *, gpointer data)
135 {
136         auto vf = static_cast<ViewFile *>(data);
137
138         vficon_toggle_star_rating(vf);
139 }
140
141 void vficon_pop_menu_refresh_cb(GtkWidget *, gpointer data)
142 {
143         auto vf = static_cast<ViewFile *>(data);
144
145         vf_refresh(vf);
146 }
147
148 void vficon_popup_destroy_cb(GtkWidget *, gpointer data)
149 {
150         auto vf = static_cast<ViewFile *>(data);
151         vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
152         VFICON(vf)->click_fd = nullptr;
153         vf->popup = nullptr;
154 }
155
156 /*
157  *-------------------------------------------------------------------
158  * signals
159  *-------------------------------------------------------------------
160  */
161
162 static void vficon_send_layout_select(ViewFile *vf, FileData *fd)
163 {
164         FileData *read_ahead_fd = nullptr;
165         FileData *sel_fd;
166         FileData *cur_fd;
167
168         if (!vf->layout || !fd) return;
169
170         sel_fd = fd;
171
172         cur_fd = layout_image_get_fd(vf->layout);
173         if (sel_fd == cur_fd) return; /* no change */
174
175         if (options->image.enable_read_ahead)
176                 {
177                 gint row;
178
179                 row = g_list_index(vf->list, fd);
180                 if (row > vficon_index_by_fd(vf, cur_fd) &&
181                     static_cast<guint>(row + 1) < vf_count(vf, nullptr))
182                         {
183                         read_ahead_fd = vf_index_get_data(vf, row + 1);
184                         }
185                 else if (row > 0)
186                         {
187                         read_ahead_fd = vf_index_get_data(vf, row - 1);
188                         }
189                 }
190
191         layout_image_set_with_ahead(vf->layout, sel_fd, read_ahead_fd);
192 }
193
194 static void vficon_toggle_filenames(ViewFile *vf)
195 {
196         GtkAllocation allocation;
197         VFICON(vf)->show_text = !VFICON(vf)->show_text;
198         options->show_icon_names = VFICON(vf)->show_text;
199
200         gtk_widget_get_allocation(vf->listview, &allocation);
201         vficon_populate_at_new_size(vf, allocation.width, allocation.height, TRUE);
202 }
203
204 static gint vficon_get_icon_width(ViewFile *vf)
205 {
206         gint width;
207
208         if (!VFICON(vf)->show_text && !vf->marks_enabled ) return options->thumbnails.max_width;
209
210         width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
211         if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
212         if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
213         if (vf->marks_enabled && width < THUMB_MIN_ICON_WIDTH_WITH_MARKS) width = THUMB_MIN_ICON_WIDTH_WITH_MARKS;
214
215         return width;
216 }
217
218 /*
219  *-------------------------------------------------------------------
220  * misc utils
221  *-------------------------------------------------------------------
222  */
223
224 static gboolean vficon_find_position(ViewFile *vf, FileData *fd, gint *row, gint *col)
225 {
226         gint n;
227
228         n = g_list_index(vf->list, fd);
229
230         if (n < 0) return FALSE;
231
232         *row = n / VFICON(vf)->columns;
233         *col = n - (*row * VFICON(vf)->columns);
234
235         return TRUE;
236 }
237
238 static gboolean vficon_find_iter(ViewFile *vf, FileData *fd, GtkTreeIter *iter, gint *column)
239 {
240         GtkTreeModel *store;
241         gint row;
242         gint col;
243
244         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
245         if (!vficon_find_position(vf, fd, &row, &col)) return FALSE;
246         if (!gtk_tree_model_iter_nth_child(store, iter, nullptr, row)) return FALSE;
247         if (column) *column = col;
248
249         return TRUE;
250 }
251
252 static FileData *vficon_find_data(ViewFile *vf, gint row, gint col, GtkTreeIter *iter)
253 {
254         GtkTreeModel *store;
255         GtkTreeIter p;
256
257         if (row < 0 || col < 0) return nullptr;
258
259         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
260         if (gtk_tree_model_iter_nth_child(store, &p, nullptr, row))
261                 {
262                 GList *list;
263
264                 gtk_tree_model_get(store, &p, FILE_COLUMN_POINTER, &list, -1);
265                 if (!list) return nullptr;
266
267                 if (iter) *iter = p;
268
269                 return static_cast<FileData *>(g_list_nth_data(list, col));
270                 }
271
272         return nullptr;
273 }
274
275 static FileData *vficon_find_data_by_coord(ViewFile *vf, gint x, gint y, GtkTreeIter *iter)
276 {
277         GtkTreePath *tpath;
278         GtkTreeViewColumn *column;
279
280         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), x, y,
281                                           &tpath, &column, nullptr, nullptr))
282                 {
283                 GtkTreeModel *store;
284                 GtkTreeIter row;
285                 GList *list;
286                 gint n;
287
288                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
289                 gtk_tree_model_get_iter(store, &row, tpath);
290                 gtk_tree_path_free(tpath);
291
292                 gtk_tree_model_get(store, &row, FILE_COLUMN_POINTER, &list, -1);
293
294                 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
295                 if (list)
296                         {
297                         if (iter) *iter = row;
298                         return static_cast<FileData *>(g_list_nth_data(list, n));
299                         }
300                 }
301
302         return nullptr;
303 }
304
305 static void vficon_mark_toggled_cb(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
306 {
307         auto vf = static_cast<ViewFile *>(data);
308         GtkTreeModel *store;
309         GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
310         GtkTreeIter row;
311         gint column;
312         GList *list;
313         guint toggled_mark;
314         FileData *fd;
315
316         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
317         if (!path || !gtk_tree_model_get_iter(store, &row, path)) return;
318
319         gtk_tree_model_get(store, &row, FILE_COLUMN_POINTER, &list, -1);
320
321         column = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cell), "column_number"));
322         g_object_get(G_OBJECT(cell), "toggled_mark", &toggled_mark, NULL);
323
324         fd = static_cast<FileData *>(g_list_nth_data(list, column));
325         if (fd)
326                 {
327                 file_data_set_mark(fd, toggled_mark, !file_data_get_mark(fd, toggled_mark));
328                 }
329 }
330
331
332 /*
333  *-------------------------------------------------------------------
334  * tooltip type window
335  *-------------------------------------------------------------------
336  */
337
338 static void tip_show(ViewFile *vf)
339 {
340         GtkWidget *label;
341         gint x;
342         gint y;
343         GdkDisplay *display;
344         GdkSeat *seat;
345         GdkDevice *device;
346
347         if (VFICON(vf)->tip_window) return;
348
349         seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_tree_view_get_bin_window(GTK_TREE_VIEW(vf->listview))));
350         device = gdk_seat_get_pointer(seat);
351         gdk_window_get_device_position(gtk_tree_view_get_bin_window(GTK_TREE_VIEW(vf->listview)),
352                                                 device, &x, &y, nullptr);
353
354         VFICON(vf)->tip_fd = vficon_find_data_by_coord(vf, x, y, nullptr);
355         if (!VFICON(vf)->tip_fd) return;
356
357         VFICON(vf)->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
358         gtk_window_set_resizable(GTK_WINDOW(VFICON(vf)->tip_window), FALSE);
359         gtk_container_set_border_width(GTK_CONTAINER(VFICON(vf)->tip_window), 2);
360
361         label = gtk_label_new(VFICON(vf)->tip_fd->name);
362
363         g_object_set_data(G_OBJECT(VFICON(vf)->tip_window), "tip_label", label);
364         gq_gtk_container_add(GTK_WIDGET(VFICON(vf)->tip_window), label);
365         gtk_widget_show(label);
366
367         display = gdk_display_get_default();
368         seat = gdk_display_get_default_seat(display);
369         device = gdk_seat_get_pointer(seat);
370         gdk_device_get_position(device, nullptr, &x, &y);
371
372         if (!gtk_widget_get_realized(VFICON(vf)->tip_window)) gtk_widget_realize(VFICON(vf)->tip_window);
373         gq_gtk_window_move(GTK_WINDOW(VFICON(vf)->tip_window), x + 16, y + 16);
374         gtk_widget_show(VFICON(vf)->tip_window);
375 }
376
377 static void tip_hide(ViewFile *vf)
378 {
379         if (VFICON(vf)->tip_window) g_object_unref(VFICON(vf)->tip_window);
380         VFICON(vf)->tip_window = nullptr;
381 }
382
383 static gboolean tip_schedule_cb(gpointer data)
384 {
385         auto vf = static_cast<ViewFile *>(data);
386         GtkWidget *window;
387
388         if (!VFICON(vf)->tip_delay_id) return FALSE;
389
390         window = gtk_widget_get_toplevel(vf->listview);
391
392         if (gtk_widget_get_sensitive(window) &&
393             gtk_window_has_toplevel_focus(GTK_WINDOW(window)))
394                 {
395                 tip_show(vf);
396                 }
397
398         VFICON(vf)->tip_delay_id = 0;
399         return FALSE;
400 }
401
402 static void tip_schedule(ViewFile *vf)
403 {
404         tip_hide(vf);
405
406         if (VFICON(vf)->tip_delay_id)
407                 {
408                 g_source_remove(VFICON(vf)->tip_delay_id);
409                 VFICON(vf)->tip_delay_id = 0;
410                 }
411
412         if (!VFICON(vf)->show_text)
413                 {
414                 VFICON(vf)->tip_delay_id = g_timeout_add(VFICON_TIP_DELAY, tip_schedule_cb, vf);
415                 }
416 }
417
418 static void tip_unschedule(ViewFile *vf)
419 {
420         tip_hide(vf);
421
422         if (VFICON(vf)->tip_delay_id)
423                 {
424                 g_source_remove(VFICON(vf)->tip_delay_id);
425                 VFICON(vf)->tip_delay_id = 0;
426                 }
427 }
428
429 static void tip_update(ViewFile *vf, FileData *fd)
430 {
431         GdkDisplay *display = gdk_display_get_default();
432         GdkSeat *seat = gdk_display_get_default_seat(display);
433         GdkDevice *device = gdk_seat_get_pointer(seat);
434
435         if (VFICON(vf)->tip_window)
436                 {
437                 gint x;
438                 gint y;
439
440                 gdk_device_get_position(device, nullptr, &x, &y);
441
442                 gq_gtk_window_move(GTK_WINDOW(VFICON(vf)->tip_window), x + 16, y + 16);
443
444                 if (fd != VFICON(vf)->tip_fd)
445                         {
446                         GtkWidget *label;
447
448                         VFICON(vf)->tip_fd = fd;
449
450                         if (!VFICON(vf)->tip_fd)
451                                 {
452                                 tip_hide(vf);
453                                 tip_schedule(vf);
454                                 return;
455                                 }
456
457                         label = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(VFICON(vf)->tip_window), "tip_label"));
458                         gtk_label_set_text(GTK_LABEL(label), VFICON(vf)->tip_fd->name);
459                         }
460                 }
461         else
462                 {
463                 tip_schedule(vf);
464                 }
465 }
466
467 /*
468  *-------------------------------------------------------------------
469  * dnd
470  *-------------------------------------------------------------------
471  */
472
473 static void vficon_dnd_get(GtkWidget *, GdkDragContext *,
474                            GtkSelectionData *selection_data, guint,
475                            guint, gpointer data)
476 {
477         auto vf = static_cast<ViewFile *>(data);
478         GList *list = nullptr;
479
480         if (!VFICON(vf)->click_fd) return;
481
482         if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
483                 {
484                 list = vf_selection_get_list(vf);
485                 }
486         else
487                 {
488                 list = g_list_append(nullptr, file_data_ref(VFICON(vf)->click_fd));
489                 }
490
491         if (!list) return;
492         uri_selection_data_set_uris_from_filelist(selection_data, list);
493         filelist_free(list);
494 }
495
496 static void vficon_drag_data_received(GtkWidget *, GdkDragContext *,
497                                       int x, int y, GtkSelectionData *selection,
498                                       guint info, guint, gpointer data)
499 {
500         auto vf = static_cast<ViewFile *>(data);
501
502         if (info == TARGET_TEXT_PLAIN) {
503                 FileData *fd = vficon_find_data_by_coord(vf, x, y, nullptr);
504
505                 if (fd) {
506                         /* Add keywords to file */
507                         auto str = reinterpret_cast<gchar *>(gtk_selection_data_get_text(selection));
508                         GList *kw_list = string_to_keywords_list(str);
509
510                         metadata_append_list(fd, KEYWORD_KEY, kw_list);
511                         g_list_free_full(kw_list, g_free);
512                         g_free(str);
513                 }
514         }
515 }
516
517 static void vficon_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
518 {
519         auto vf = static_cast<ViewFile *>(data);
520
521         tip_unschedule(vf);
522
523         if (VFICON(vf)->click_fd && VFICON(vf)->click_fd->thumb_pixbuf)
524                 {
525                 gint items;
526
527                 if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
528                         items = g_list_length(VFICON(vf)->selection);
529                 else
530                         items = 1;
531
532                 dnd_set_drag_icon(widget, context, VFICON(vf)->click_fd->thumb_pixbuf, items);
533                 }
534 }
535
536 static void vficon_dnd_end(GtkWidget *, GdkDragContext *context, gpointer data)
537 {
538         auto vf = static_cast<ViewFile *>(data);
539
540         vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
541
542         if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
543                 {
544                 vf_refresh(vf);
545                 }
546
547         tip_unschedule(vf);
548 }
549
550 void vficon_dnd_init(ViewFile *vf)
551 {
552         gtk_drag_source_set(vf->listview, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
553                             dnd_file_drag_types, dnd_file_drag_types_count,
554                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
555         gtk_drag_dest_set(vf->listview, GTK_DEST_DEFAULT_ALL,
556                             dnd_file_drag_types, dnd_file_drag_types_count,
557                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
558
559         g_signal_connect(G_OBJECT(vf->listview), "drag_data_get",
560                          G_CALLBACK(vficon_dnd_get), vf);
561         g_signal_connect(G_OBJECT(vf->listview), "drag_begin",
562                          G_CALLBACK(vficon_dnd_begin), vf);
563         g_signal_connect(G_OBJECT(vf->listview), "drag_end",
564                          G_CALLBACK(vficon_dnd_end), vf);
565         g_signal_connect(G_OBJECT(vf->listview), "drag_data_received",
566                          G_CALLBACK(vficon_drag_data_received), vf);
567 }
568
569 /*
570  *-------------------------------------------------------------------
571  * cell updates
572  *-------------------------------------------------------------------
573  */
574
575 static void vficon_selection_set(ViewFile *vf, FileData *fd, SelectionType value, GtkTreeIter *iter)
576 {
577         GtkTreeModel *store;
578         GList *list;
579
580         if (!fd) return;
581
582         if (fd->selected == value) return;
583         fd->selected = value;
584
585         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
586         if (iter)
587                 {
588                 gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
589                 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, list, -1);
590                 }
591         else
592                 {
593                 GtkTreeIter row;
594
595                 if (vficon_find_iter(vf, fd, &row, nullptr))
596                         {
597                         gtk_tree_model_get(store, &row, FILE_COLUMN_POINTER, &list, -1);
598                         if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, FILE_COLUMN_POINTER, list, -1);
599                         }
600                 }
601 }
602
603 static void vficon_selection_add(ViewFile *vf, FileData *fd, SelectionType mask, GtkTreeIter *iter)
604 {
605         if (!fd) return;
606
607         vficon_selection_set(vf, fd, static_cast<SelectionType>(fd->selected | mask), iter);
608 }
609
610 static void vficon_selection_remove(ViewFile *vf, FileData *fd, SelectionType mask, GtkTreeIter *iter)
611 {
612         if (!fd) return;
613
614         vficon_selection_set(vf, fd, static_cast<SelectionType>(fd->selected & ~mask), iter);
615 }
616
617 void vficon_marks_set(ViewFile *vf, gint)
618 {
619         GtkAllocation allocation;
620         gtk_widget_get_allocation(vf->listview, &allocation);
621         vficon_populate_at_new_size(vf, allocation.width, allocation.height, TRUE);
622 }
623
624 void vficon_star_rating_set(ViewFile *vf, gint)
625 {
626         GtkAllocation allocation;
627         gtk_widget_get_allocation(vf->listview, &allocation);
628         vficon_populate_at_new_size(vf, allocation.width, allocation.height, TRUE);
629 }
630
631 /*
632  *-------------------------------------------------------------------
633  * selections
634  *-------------------------------------------------------------------
635  */
636
637 static void vficon_verify_selections(ViewFile *vf)
638 {
639         GList *work;
640
641         work = VFICON(vf)->selection;
642         while (work)
643                 {
644                 auto fd = static_cast<FileData *>(work->data);
645                 work = work->next;
646
647                 if (vficon_index_by_fd(vf, fd) >= 0) continue;
648
649                 VFICON(vf)->selection = g_list_remove(VFICON(vf)->selection, fd);
650                 }
651 }
652
653 void vficon_select_all(ViewFile *vf)
654 {
655         GList *work;
656
657         g_list_free(VFICON(vf)->selection);
658         VFICON(vf)->selection = nullptr;
659
660         work = vf->list;
661         while (work)
662                 {
663                 auto fd = static_cast<FileData *>(work->data);
664                 work = work->next;
665
666                 VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
667                 vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
668                 }
669
670         vf_send_update(vf);
671 }
672
673 void vficon_select_none(ViewFile *vf)
674 {
675         GList *work;
676
677         work = VFICON(vf)->selection;
678         while (work)
679                 {
680                 auto fd = static_cast<FileData *>(work->data);
681                 work = work->next;
682
683                 vficon_selection_remove(vf, fd, SELECTION_SELECTED, nullptr);
684                 }
685
686         g_list_free(VFICON(vf)->selection);
687         VFICON(vf)->selection = nullptr;
688
689         vf_send_update(vf);
690 }
691
692 void vficon_select_invert(ViewFile *vf)
693 {
694         GList *work;
695
696         work = vf->list;
697         while (work)
698                 {
699                 auto fd = static_cast<FileData *>(work->data);
700                 work = work->next;
701
702                 if (fd->selected & SELECTION_SELECTED)
703                         {
704                         VFICON(vf)->selection = g_list_remove(VFICON(vf)->selection, fd);
705                         vficon_selection_remove(vf, fd, SELECTION_SELECTED, nullptr);
706                         }
707                 else
708                         {
709                         VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
710                         vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
711                         }
712                 }
713
714         vf_send_update(vf);
715 }
716
717 static void vficon_select(ViewFile *vf, FileData *fd)
718 {
719         VFICON(vf)->prev_selection = fd;
720
721         if (!fd || fd->selected & SELECTION_SELECTED) return;
722
723         VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
724         vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
725
726         vf_send_update(vf);
727 }
728
729 static void vficon_unselect(ViewFile *vf, FileData *fd)
730 {
731         VFICON(vf)->prev_selection = fd;
732
733         if (!fd || !(fd->selected & SELECTION_SELECTED) ) return;
734
735         VFICON(vf)->selection = g_list_remove(VFICON(vf)->selection, fd);
736         vficon_selection_remove(vf, fd, SELECTION_SELECTED, nullptr);
737
738         vf_send_update(vf);
739 }
740
741 static void vficon_select_util(ViewFile *vf, FileData *fd, gboolean select)
742 {
743         if (select)
744                 {
745                 vficon_select(vf, fd);
746                 }
747         else
748                 {
749                 vficon_unselect(vf, fd);
750                 }
751 }
752
753 static void vficon_select_region_util(ViewFile *vf, FileData *start, FileData *end, gboolean select)
754 {
755         gint row1;
756         gint col1;
757         gint row2;
758         gint col2;
759         gint t;
760         gint i;
761         gint j;
762
763         if (!vficon_find_position(vf, start, &row1, &col1) ||
764             !vficon_find_position(vf, end, &row2, &col2) ) return;
765
766         VFICON(vf)->prev_selection = end;
767
768         if (!options->collections.rectangular_selection)
769                 {
770                 GList *work;
771
772                 if (g_list_index(vf->list, start) > g_list_index(vf->list, end))
773                         {
774                         FileData *tmp = start;
775                         start = end;
776                         end = tmp;
777                         }
778
779                 work = g_list_find(vf->list, start);
780                 while (work)
781                         {
782                         auto fd = static_cast<FileData *>(work->data);
783                         vficon_select_util(vf, fd, select);
784
785                         if (work->data != end)
786                                 work = work->next;
787                         else
788                                 work = nullptr;
789                         }
790                 return;
791                 }
792
793         // rectangular_selection==true.
794         if (row2 < row1)
795                 {
796                 t = row1;
797                 row1 = row2;
798                 row2 = t;
799                 }
800         if (col2 < col1)
801                 {
802                 t = col1;
803                 col1 = col2;
804                 col2 = t;
805                 }
806
807         DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
808
809         for (i = row1; i <= row2; i++)
810                 {
811                 for (j = col1; j <= col2; j++)
812                         {
813                         FileData *fd = vficon_find_data(vf, i, j, nullptr);
814                         if (fd) vficon_select_util(vf, fd, select);
815                         }
816                 }
817 }
818
819 #pragma GCC diagnostic push
820 #pragma GCC diagnostic ignored "-Wunused-function"
821 gboolean vficon_index_is_selected_unused(ViewFile *vf, gint row)
822 {
823         auto *fd = static_cast<FileData *>(g_list_nth_data(vf->list, row));
824
825         if (!fd) return FALSE;
826
827         return (fd->selected & SELECTION_SELECTED);
828 }
829 #pragma GCC diagnostic pop
830
831 guint vficon_selection_count(ViewFile *vf, gint64 *bytes)
832 {
833         if (bytes)
834                 {
835                 gint64 b = 0;
836                 GList *work;
837
838                 work = VFICON(vf)->selection;
839                 while (work)
840                         {
841                         auto fd = static_cast<FileData *>(work->data);
842                         g_assert(fd->magick == FD_MAGICK);
843                         b += fd->size;
844
845                         work = work->next;
846                         }
847
848                 *bytes = b;
849                 }
850
851         return g_list_length(VFICON(vf)->selection);
852 }
853
854 GList *vficon_selection_get_list(ViewFile *vf)
855 {
856         GList *list = nullptr;
857
858         for (GList *work = g_list_last(VFICON(vf)->selection); work; work = work->prev)
859                 {
860                 auto fd = static_cast<FileData *>(work->data);
861                 g_assert(fd->magick == FD_MAGICK);
862
863                 list = g_list_concat(filelist_copy(fd->sidecar_files), list);
864                 list = g_list_prepend(list, file_data_ref(fd));
865                 }
866
867         return list;
868 }
869
870 GList *vficon_selection_get_list_by_index(ViewFile *vf)
871 {
872         GList *list = nullptr;
873         GList *work;
874
875         work = VFICON(vf)->selection;
876         while (work)
877                 {
878                 list = g_list_prepend(list, GINT_TO_POINTER(g_list_index(vf->list, work->data)));
879                 work = work->next;
880                 }
881
882         return g_list_reverse(list);
883 }
884
885 void vficon_select_by_fd(ViewFile *vf, FileData *fd)
886 {
887         if (!fd) return;
888         if (!g_list_find(vf->list, fd)) return;
889
890         if (!(fd->selected & SELECTION_SELECTED))
891                 {
892                 vf_select_none(vf);
893                 vficon_select(vf, fd);
894                 }
895
896         vficon_set_focus(vf, fd);
897 }
898
899 void vficon_select_list(ViewFile *vf, GList *list)
900 {
901         GList *work;
902         FileData *fd;
903
904         if (!list) return;
905
906         work = list;
907         while (work)
908                 {
909                 fd = static_cast<FileData *>(work->data);
910                 if (g_list_find(vf->list, fd))
911                         {
912                         VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
913                         vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
914                         }
915                 work = work->next;
916                 }
917 }
918
919 void vficon_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
920 {
921         GList *work;
922         gint n = mark - 1;
923
924         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
925
926         work = vf->list;
927         while (work)
928                 {
929                 auto fd = static_cast<FileData *>(work->data);
930                 gboolean mark_val;
931                 gboolean selected;
932
933                 g_assert(fd->magick == FD_MAGICK);
934
935                 mark_val = file_data_get_mark(fd, n);
936                 selected = fd->selected & SELECTION_SELECTED;
937
938                 switch (mode)
939                         {
940                         case MTS_MODE_SET: selected = mark_val;
941                                 break;
942                         case MTS_MODE_OR: selected = mark_val || selected;
943                                 break;
944                         case MTS_MODE_AND: selected = mark_val && selected;
945                                 break;
946                         case MTS_MODE_MINUS: selected = !mark_val && selected;
947                                 break;
948                         }
949
950                 vficon_select_util(vf, fd, selected);
951
952                 work = work->next;
953                 }
954 }
955
956 void vficon_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
957 {
958         GList *slist;
959         GList *work;
960         gint n = mark -1;
961
962         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
963
964         slist = vf_selection_get_list(vf);
965         work = slist;
966         while (work)
967                 {
968                 auto fd = static_cast<FileData *>(work->data);
969
970                 switch (mode)
971                         {
972                         case STM_MODE_SET: file_data_set_mark(fd, n, 1);
973                                 break;
974                         case STM_MODE_RESET: file_data_set_mark(fd, n, 0);
975                                 break;
976                         case STM_MODE_TOGGLE: file_data_set_mark(fd, n, !file_data_get_mark(fd, n));
977                                 break;
978                         }
979                 work = work->next;
980                 }
981         filelist_free(slist);
982 }
983
984 static void vficon_select_closest(ViewFile *vf, FileData *sel_fd)
985 {
986         GList *work;
987         FileData *fd = nullptr;
988
989         if (sel_fd->parent) sel_fd = sel_fd->parent;
990         work = vf->list;
991
992         while (work)
993                 {
994                 gint match;
995
996                 fd = static_cast<FileData *>(work->data);
997                 work = work->next;
998
999                 match = filelist_sort_compare_filedata_full(fd, sel_fd, vf->sort_method, vf->sort_ascend);
1000
1001                 if (match >= 0) break;
1002                 }
1003
1004         if (fd)
1005                 {
1006                 vficon_select(vf, fd);
1007                 vficon_send_layout_select(vf, fd);
1008                 }
1009 }
1010
1011
1012 /*
1013  *-------------------------------------------------------------------
1014  * focus
1015  *-------------------------------------------------------------------
1016  */
1017
1018 static void vficon_move_focus(ViewFile *vf, gint row, gint col, gboolean relative)
1019 {
1020         gint new_row;
1021         gint new_col;
1022
1023         if (relative)
1024                 {
1025                 new_row = VFICON(vf)->focus_row;
1026                 new_col = VFICON(vf)->focus_column;
1027
1028                 new_row += row;
1029                 if (new_row < 0) new_row = 0;
1030                 if (new_row >= VFICON(vf)->rows) new_row = VFICON(vf)->rows - 1;
1031
1032                 while (col != 0)
1033                         {
1034                         if (col < 0)
1035                                 {
1036                                 new_col--;
1037                                 col++;
1038                                 }
1039                         else
1040                                 {
1041                                 new_col++;
1042                                 col--;
1043                                 }
1044
1045                         if (new_col < 0)
1046                                 {
1047                                 if (new_row > 0)
1048                                         {
1049                                         new_row--;
1050                                         new_col = VFICON(vf)->columns - 1;
1051                                         }
1052                                 else
1053                                         {
1054                                         new_col = 0;
1055                                         }
1056                                 }
1057                         if (new_col >= VFICON(vf)->columns)
1058                                 {
1059                                 if (new_row < VFICON(vf)->rows - 1)
1060                                         {
1061                                         new_row++;
1062                                         new_col = 0;
1063                                         }
1064                                 else
1065                                         {
1066                                         new_col = VFICON(vf)->columns - 1;
1067                                         }
1068                                 }
1069                         }
1070                 }
1071         else
1072                 {
1073                 new_row = row;
1074                 new_col = col;
1075
1076                 if (new_row >= VFICON(vf)->rows)
1077                         {
1078                         if (VFICON(vf)->rows > 0)
1079                                 new_row = VFICON(vf)->rows - 1;
1080                         else
1081                                 new_row = 0;
1082                         new_col = VFICON(vf)->columns - 1;
1083                         }
1084                 if (new_col >= VFICON(vf)->columns) new_col = VFICON(vf)->columns - 1;
1085                 }
1086
1087         if (new_row == VFICON(vf)->rows - 1)
1088                 {
1089                 gint l;
1090
1091                 /* if we moved beyond the last image, go to the last image */
1092
1093                 l = g_list_length(vf->list);
1094                 if (VFICON(vf)->rows > 1) l -= (VFICON(vf)->rows - 1) * VFICON(vf)->columns;
1095                 if (new_col >= l) new_col = l - 1;
1096                 }
1097
1098         vficon_set_focus(vf, vficon_find_data(vf, new_row, new_col, nullptr));
1099 }
1100
1101 static void vficon_set_focus(ViewFile *vf, FileData *fd)
1102 {
1103         GtkTreeIter iter;
1104         gint row;
1105         gint col;
1106
1107         if (g_list_find(vf->list, VFICON(vf)->focus_fd))
1108                 {
1109                 if (fd == VFICON(vf)->focus_fd)
1110                         {
1111                         /* ensure focus row col are correct */
1112                         vficon_find_position(vf, VFICON(vf)->focus_fd, &VFICON(vf)->focus_row, &VFICON(vf)->focus_column);
1113
1114 /** @FIXME Refer to issue #467 on Github. The thumbnail position is not
1115  * preserved when the icon view is refreshed. Caused by an unknown call from
1116  * the idle loop. This patch hides the problem.
1117  */
1118                         if (vficon_find_iter(vf, VFICON(vf)->focus_fd, &iter, nullptr))
1119                                 {
1120                                 tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1121                                 }
1122
1123                         return;
1124                         }
1125                 vficon_selection_remove(vf, VFICON(vf)->focus_fd, SELECTION_FOCUS, nullptr);
1126                 }
1127
1128         if (!vficon_find_position(vf, fd, &row, &col))
1129                 {
1130                 VFICON(vf)->focus_fd = nullptr;
1131                 VFICON(vf)->focus_row = -1;
1132                 VFICON(vf)->focus_column = -1;
1133                 return;
1134                 }
1135
1136         VFICON(vf)->focus_fd = fd;
1137         VFICON(vf)->focus_row = row;
1138         VFICON(vf)->focus_column = col;
1139         vficon_selection_add(vf, VFICON(vf)->focus_fd, SELECTION_FOCUS, nullptr);
1140
1141         if (vficon_find_iter(vf, VFICON(vf)->focus_fd, &iter, nullptr))
1142                 {
1143                 GtkTreePath *tpath;
1144                 GtkTreeViewColumn *column;
1145                 GtkTreeModel *store;
1146
1147                 tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1148
1149                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1150                 tpath = gtk_tree_model_get_path(store, &iter);
1151                 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1152                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), VFICON_MAX_COLUMNS);
1153                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vf->listview), tpath, column, FALSE);
1154                 gtk_tree_path_free(tpath);
1155                 }
1156 }
1157
1158 /* used to figure the page up/down distances */
1159 static gint page_height(ViewFile *vf)
1160 {
1161         GtkAdjustment *adj;
1162         gint page_size;
1163         gint row_height;
1164         gint ret;
1165
1166         adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vf->listview));
1167         page_size = static_cast<gint>(gtk_adjustment_get_page_increment(adj));
1168
1169         row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1170         if (VFICON(vf)->show_text) row_height += options->thumbnails.max_height / 3;
1171
1172         ret = page_size / row_height;
1173         if (ret < 1) ret = 1;
1174
1175         return ret;
1176 }
1177
1178 /*
1179  *-------------------------------------------------------------------
1180  * keyboard
1181  *-------------------------------------------------------------------
1182  */
1183
1184 gboolean vficon_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1185 {
1186         auto vf = static_cast<ViewFile *>(data);
1187         gint focus_row = 0;
1188         gint focus_col = 0;
1189         FileData *fd;
1190         gboolean stop_signal;
1191
1192         stop_signal = TRUE;
1193         switch (event->keyval)
1194                 {
1195                 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1196                         focus_col = -1;
1197                         break;
1198                 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1199                         focus_col = 1;
1200                         break;
1201                 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1202                         focus_row = -1;
1203                         break;
1204                 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1205                         focus_row = 1;
1206                         break;
1207                 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1208                         focus_row = -page_height(vf);
1209                         break;
1210                 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1211                         focus_row = page_height(vf);
1212                         break;
1213                 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1214                         focus_row = -VFICON(vf)->focus_row;
1215                         focus_col = -VFICON(vf)->focus_column;
1216                         break;
1217                 case GDK_KEY_End: case GDK_KEY_KP_End:
1218                         focus_row = VFICON(vf)->rows - 1 - VFICON(vf)->focus_row;
1219                         focus_col = VFICON(vf)->columns - 1 - VFICON(vf)->focus_column;
1220                         break;
1221                 case GDK_KEY_space:
1222                         fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1223                         if (fd)
1224                                 {
1225                                 VFICON(vf)->click_fd = fd;
1226                                 if (event->state & GDK_CONTROL_MASK)
1227                                         {
1228                                         gint selected;
1229
1230                                         selected = fd->selected & SELECTION_SELECTED;
1231                                         if (selected)
1232                                                 {
1233                                                 vficon_unselect(vf, fd);
1234                                                 }
1235                                         else
1236                                                 {
1237                                                 vficon_select(vf, fd);
1238                                                 vficon_send_layout_select(vf, fd);
1239                                                 }
1240                                         }
1241                                 else
1242                                         {
1243                                         vf_select_none(vf);
1244                                         vficon_select(vf, fd);
1245                                         vficon_send_layout_select(vf, fd);
1246                                         }
1247                                 }
1248                         break;
1249                 case GDK_KEY_Menu:
1250                         fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1251                         VFICON(vf)->click_fd = fd;
1252
1253                         vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
1254                         tip_unschedule(vf);
1255
1256                         vf->popup = vf_pop_menu(vf);
1257                         gtk_menu_popup_at_widget(GTK_MENU(vf->popup), widget, GDK_GRAVITY_EAST, GDK_GRAVITY_CENTER, nullptr);
1258                         break;
1259                 default:
1260                         stop_signal = FALSE;
1261                         break;
1262                 }
1263
1264         if (focus_row != 0 || focus_col != 0)
1265                 {
1266                 FileData *new_fd;
1267                 FileData *old_fd;
1268
1269                 old_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1270                 vficon_move_focus(vf, focus_row, focus_col, TRUE);
1271                 new_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1272
1273                 if (new_fd != old_fd)
1274                         {
1275                         if (event->state & GDK_SHIFT_MASK)
1276                                 {
1277                                 if (!options->collections.rectangular_selection)
1278                                         {
1279                                         vficon_select_region_util(vf, old_fd, new_fd, FALSE);
1280                                         }
1281                                 else
1282                                         {
1283                                         vficon_select_region_util(vf, VFICON(vf)->click_fd, old_fd, FALSE);
1284                                         }
1285                                 vficon_select_region_util(vf, VFICON(vf)->click_fd, new_fd, TRUE);
1286                                 vficon_send_layout_select(vf, new_fd);
1287                                 }
1288                         else if (event->state & GDK_CONTROL_MASK)
1289                                 {
1290                                 VFICON(vf)->click_fd = new_fd;
1291                                 }
1292                         else
1293                                 {
1294                                 VFICON(vf)->click_fd = new_fd;
1295                                 vf_select_none(vf);
1296                                 vficon_select(vf, new_fd);
1297                                 vficon_send_layout_select(vf, new_fd);
1298                                 }
1299                         }
1300                 }
1301
1302         if (stop_signal)
1303                 {
1304                 tip_unschedule(vf);
1305                 }
1306
1307         return stop_signal;
1308 }
1309
1310 /*
1311  *-------------------------------------------------------------------
1312  * mouse
1313  *-------------------------------------------------------------------
1314  */
1315
1316 static gboolean vficon_motion_cb(GtkWidget *, GdkEventMotion *event, gpointer data)
1317 {
1318         auto vf = static_cast<ViewFile *>(data);
1319         FileData *fd;
1320
1321         fd = vficon_find_data_by_coord(vf, static_cast<gint>(event->x), static_cast<gint>(event->y), nullptr);
1322         tip_update(vf, fd);
1323
1324         return FALSE;
1325 }
1326
1327 gboolean vficon_press_cb(GtkWidget *, GdkEventButton *bevent, gpointer data)
1328 {
1329         auto vf = static_cast<ViewFile *>(data);
1330         GtkTreeIter iter;
1331         FileData *fd;
1332
1333         tip_unschedule(vf);
1334
1335         fd = vficon_find_data_by_coord(vf, static_cast<gint>(bevent->x), static_cast<gint>(bevent->y), &iter);
1336
1337         if (fd)
1338                 {
1339                 VFICON(vf)->click_fd = fd;
1340                 vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1341
1342                 switch (bevent->button)
1343                         {
1344                         case MOUSE_BUTTON_LEFT:
1345                                 if (!gtk_widget_has_focus(vf->listview))
1346                                         {
1347                                         gtk_widget_grab_focus(vf->listview);
1348                                         }
1349
1350                                 if (bevent->type == GDK_2BUTTON_PRESS && vf->layout)
1351                                         {
1352                                         if (VFICON(vf)->click_fd->format_class == FORMAT_CLASS_COLLECTION)
1353                                                 {
1354                                                 collection_window_new(VFICON(vf)->click_fd->path);
1355                                                 }
1356                                         else
1357                                                 {
1358                                                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1359                                                 layout_image_full_screen_start(vf->layout);
1360                                                 }
1361                                         }
1362                                 break;
1363                         case MOUSE_BUTTON_RIGHT:
1364                                 vf->popup = vf_pop_menu(vf);
1365                                 gtk_menu_popup_at_pointer(GTK_MENU(vf->popup), nullptr);
1366                                 break;
1367                         default:
1368                                 break;
1369                         }
1370                 }
1371
1372         return FALSE;
1373 }
1374
1375 gboolean vficon_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1376 {
1377         auto vf = static_cast<ViewFile *>(data);
1378         GtkTreeIter iter;
1379         FileData *fd = nullptr;
1380         gboolean was_selected;
1381
1382         tip_schedule(vf);
1383
1384         if (defined_mouse_buttons(widget, bevent, vf->layout))
1385                 {
1386                 return TRUE;
1387                 }
1388
1389         if (static_cast<gint>(bevent->x) != 0 || static_cast<gint>(bevent->y) != 0)
1390                 {
1391                 fd = vficon_find_data_by_coord(vf, static_cast<gint>(bevent->x), static_cast<gint>(bevent->y), &iter);
1392                 }
1393
1394         if (VFICON(vf)->click_fd)
1395                 {
1396                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
1397                 }
1398
1399         if (!fd || VFICON(vf)->click_fd != fd) return TRUE;
1400
1401         was_selected = !!(fd->selected & SELECTION_SELECTED);
1402
1403         switch (bevent->button)
1404                 {
1405                 case MOUSE_BUTTON_LEFT:
1406                         {
1407                         vficon_set_focus(vf, fd);
1408
1409                         if (bevent->state & GDK_CONTROL_MASK)
1410                                 {
1411                                 gboolean select;
1412
1413                                 select = !(fd->selected & SELECTION_SELECTED);
1414                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1415                                         {
1416                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, select);
1417                                         }
1418                                 else
1419                                         {
1420                                         vficon_select_util(vf, fd, select);
1421                                         }
1422                                 }
1423                         else
1424                                 {
1425                                 vf_select_none(vf);
1426
1427                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1428                                         {
1429                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, TRUE);
1430                                         }
1431                                 else
1432                                         {
1433                                         vficon_select_util(vf, fd, TRUE);
1434                                         was_selected = FALSE;
1435                                         }
1436                                 }
1437                         }
1438                         break;
1439                 case MOUSE_BUTTON_MIDDLE:
1440                         {
1441                         vficon_select_util(vf, fd, !(fd->selected & SELECTION_SELECTED));
1442                         }
1443                         break;
1444                 default:
1445                         break;
1446                 }
1447
1448         if (!was_selected && (fd->selected & SELECTION_SELECTED))
1449                 {
1450                 vficon_send_layout_select(vf, fd);
1451                 }
1452
1453         return TRUE;
1454 }
1455
1456 static gboolean vficon_leave_cb(GtkWidget *, GdkEventCrossing *, gpointer data)
1457 {
1458         auto vf = static_cast<ViewFile *>(data);
1459
1460         tip_unschedule(vf);
1461         return FALSE;
1462 }
1463
1464 /*
1465  *-------------------------------------------------------------------
1466  * population
1467  *-------------------------------------------------------------------
1468  */
1469
1470 static gboolean vficon_destroy_node_cb(GtkTreeModel *store, GtkTreePath *, GtkTreeIter *iter, gpointer)
1471 {
1472         GList *list;
1473
1474         gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
1475
1476         /* it seems that gtk_list_store_clear may call some callbacks
1477            that use the column. Set the pointer to NULL to be safe. */
1478         gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, NULL, -1);
1479         g_list_free(list);
1480
1481         return FALSE;
1482 }
1483
1484 static void vficon_clear_store(ViewFile *vf)
1485 {
1486         GtkTreeModel *store;
1487
1488         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1489         gtk_tree_model_foreach(store, vficon_destroy_node_cb, nullptr);
1490
1491         gtk_list_store_clear(GTK_LIST_STORE(store));
1492 }
1493
1494 static GList *vficon_add_row(ViewFile *vf, GtkTreeIter *iter)
1495 {
1496         GtkListStore *store;
1497         GList *list = nullptr;
1498         gint i;
1499
1500         for (i = 0; i < VFICON(vf)->columns; i++) list = g_list_prepend(list, nullptr);
1501
1502         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview)));
1503         gtk_list_store_append(store, iter);
1504         gtk_list_store_set(store, iter, FILE_COLUMN_POINTER, list, -1);
1505
1506         return list;
1507 }
1508
1509 static void vficon_populate(ViewFile *vf, gboolean resize, gboolean keep_position)
1510 {
1511         GtkTreeModel *store;
1512         GtkTreePath *tpath;
1513         GList *work;
1514         FileData *visible_fd = nullptr;
1515         gint r;
1516         gint c;
1517         gboolean valid;
1518         GtkTreeIter iter;
1519
1520         vficon_verify_selections(vf);
1521
1522         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1523
1524         if (keep_position && gtk_widget_get_realized(vf->listview) &&
1525             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1526                 {
1527                 GtkTreeIter iter;
1528                 GList *list;
1529
1530                 gtk_tree_model_get_iter(store, &iter, tpath);
1531                 gtk_tree_path_free(tpath);
1532
1533                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1534                 if (list) visible_fd = static_cast<FileData *>(list->data);
1535                 }
1536
1537
1538         if (resize)
1539                 {
1540                 gint i;
1541                 gint thumb_width;
1542
1543                 vficon_clear_store(vf);
1544
1545                 thumb_width = vficon_get_icon_width(vf);
1546
1547                 for (i = 0; i < VFICON_MAX_COLUMNS; i++)
1548                         {
1549                         GtkTreeViewColumn *column;
1550                         GtkCellRenderer *cell;
1551                         GList *list;
1552
1553                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), i);
1554                         gtk_tree_view_column_set_visible(column, (i < VFICON(vf)->columns));
1555                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1556
1557                         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1558                         cell = static_cast<GtkCellRenderer *>((list) ? list->data : nullptr);
1559                         g_list_free(list);
1560
1561                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1562                                 {
1563                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1564                                                              "fixed_height", options->thumbnails.max_height,
1565                                                              "show_text", VFICON(vf)->show_text || options->show_star_rating,
1566                                                              "show_marks", vf->marks_enabled,
1567                                                              "num_marks", FILEDATA_MARKS_SIZE,
1568                                                              NULL);
1569                                 }
1570                         }
1571                 if (gtk_widget_get_realized(vf->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(vf->listview));
1572                 }
1573
1574         r = -1;
1575         c = 0;
1576
1577         valid = gtk_tree_model_iter_children(store, &iter, nullptr);
1578
1579         work = vf->list;
1580         while (work)
1581                 {
1582                 GList *list;
1583                 r++;
1584                 c = 0;
1585                 if (valid)
1586                         {
1587                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1588                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1589                         }
1590                 else
1591                         {
1592                         list = vficon_add_row(vf, &iter);
1593                         }
1594
1595                 while (list)
1596                         {
1597                         FileData *fd;
1598
1599                         if (work)
1600                                 {
1601                                 fd = static_cast<FileData *>(work->data);
1602                                 work = work->next;
1603                                 c++;
1604                                 }
1605                         else
1606                                 {
1607                                 fd = nullptr;
1608                                 }
1609
1610                         list->data = fd;
1611                         list = list->next;
1612                         }
1613                 if (valid) valid = gtk_tree_model_iter_next(store, &iter);
1614                 }
1615
1616         r++;
1617         while (valid)
1618                 {
1619                 GList *list;
1620
1621                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1622                 valid = gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1623                 g_list_free(list);
1624                 }
1625
1626         VFICON(vf)->rows = r;
1627
1628         if (visible_fd &&
1629             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1630                 {
1631                 GtkTreeIter iter;
1632                 GList *list;
1633
1634                 gtk_tree_model_get_iter(store, &iter, tpath);
1635                 gtk_tree_path_free(tpath);
1636
1637                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1638                 if (g_list_find(list, visible_fd) == nullptr &&
1639                     vficon_find_iter(vf, visible_fd, &iter, nullptr))
1640                         {
1641                         tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1642                         }
1643                 }
1644
1645
1646         vf_send_update(vf);
1647         vf_thumb_update(vf);
1648         vf_star_update(vf);
1649 }
1650
1651 static void vficon_populate_at_new_size(ViewFile *vf, gint w, gint, gboolean force)
1652 {
1653         gint new_cols;
1654         gint thumb_width;
1655
1656         thumb_width = vficon_get_icon_width(vf);
1657
1658         new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1659         if (new_cols < 1) new_cols = 1;
1660
1661         if (!force && new_cols == VFICON(vf)->columns) return;
1662
1663         VFICON(vf)->columns = new_cols;
1664
1665         vficon_populate(vf, TRUE, TRUE);
1666
1667         DEBUG_1("col tab pop cols=%d rows=%d", VFICON(vf)->columns, VFICON(vf)->rows);
1668 }
1669
1670 static void vficon_sized_cb(GtkWidget *, GtkAllocation *allocation, gpointer data)
1671 {
1672         auto vf = static_cast<ViewFile *>(data);
1673
1674         vficon_populate_at_new_size(vf, allocation->width, allocation->height, FALSE);
1675 }
1676
1677 /*
1678  *-----------------------------------------------------------------------------
1679  * misc
1680  *-----------------------------------------------------------------------------
1681  */
1682
1683 void vficon_sort_set(ViewFile *vf, SortType type, gboolean ascend, gboolean case_sensitive)
1684 {
1685         if (vf->sort_method == type && vf->sort_ascend == ascend && vf->sort_case == case_sensitive) return;
1686
1687         vf->sort_method = type;
1688         vf->sort_ascend = ascend;
1689         vf->sort_case = case_sensitive;
1690
1691         if (!vf->list) return;
1692
1693         vf_refresh(vf);
1694 }
1695
1696 /*
1697  *-----------------------------------------------------------------------------
1698  * thumb updates
1699  *-----------------------------------------------------------------------------
1700  */
1701
1702 void vficon_thumb_progress_count(GList *list, gint *count, gint *done)
1703 {
1704         GList *work = list;
1705         while (work)
1706                 {
1707                 auto fd = static_cast<FileData *>(work->data);
1708                 work = work->next;
1709
1710                 if (fd->thumb_pixbuf) (*done)++;
1711                 (*count)++;
1712                 }
1713 }
1714
1715 void vficon_read_metadata_progress_count(GList *list, gint *count, gint *done)
1716 {
1717         GList *work = list;
1718         while (work)
1719                 {
1720                 auto fd = static_cast<FileData *>(work->data);
1721                 work = work->next;
1722
1723                 if (fd->metadata_in_idle_loaded) (*done)++;
1724                 (*count)++;
1725                 }
1726 }
1727
1728 void vficon_set_thumb_fd(ViewFile *vf, FileData *fd)
1729 {
1730         GtkTreeModel *store;
1731         GtkTreeIter iter;
1732         GList *list;
1733
1734         if (!g_list_find(vf->list, fd)) return;
1735         if (!vficon_find_iter(vf, fd, &iter, nullptr)) return;
1736
1737         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1738
1739         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1740         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1741 }
1742
1743 /* Returns the next fd without a loaded pixbuf, so the thumb-loader can load the pixbuf for it. */
1744 FileData *vficon_thumb_next_fd(ViewFile *vf)
1745 {
1746         GtkTreePath *tpath;
1747
1748         /* First see if there are visible files that don't have a loaded thumb... */
1749         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1750                 {
1751                 GtkTreeModel *store;
1752                 GtkTreeIter iter;
1753                 gboolean valid = TRUE;
1754
1755                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1756                 gtk_tree_model_get_iter(store, &iter, tpath);
1757                 gtk_tree_path_free(tpath);
1758                 tpath = nullptr;
1759
1760                 while (valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vf->listview), &iter, FALSE) == 0)
1761                         {
1762                         GList *list;
1763                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1764
1765                         /** @todo (xsdg): for loop here. */
1766                         for (; list; list = list->next)
1767                                 {
1768                                 auto fd = static_cast<FileData *>(list->data);
1769                                 if (fd && !fd->thumb_pixbuf) return fd;
1770                                 }
1771
1772                         valid = gtk_tree_model_iter_next(store, &iter);
1773                         }
1774                 }
1775
1776         /* Then iterate through the entire list to load all of them. */
1777         GList *work;
1778         for (work = vf->list; work; work = work->next)
1779                 {
1780                 auto fd = static_cast<FileData *>(work->data);
1781
1782                 // Note: This implementation differs from view-file-list.cc because sidecar files are not
1783                 // distinct list elements here, as they are in the list view.
1784                 if (!fd->thumb_pixbuf) return fd;
1785                 }
1786
1787         return nullptr;
1788 }
1789
1790 void vficon_set_star_fd(ViewFile *vf, FileData *fd)
1791 {
1792         GtkTreeModel *store;
1793         GtkTreeIter iter;
1794         GList *list;
1795
1796         if (!g_list_find(vf->list, fd)) return;
1797         if (!vficon_find_iter(vf, fd, &iter, nullptr)) return;
1798
1799         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1800
1801         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1802         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1803 }
1804
1805 FileData *vficon_star_next_fd(ViewFile *vf)
1806 {
1807         GtkTreePath *tpath;
1808
1809         /* first check the visible files */
1810
1811         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1812                 {
1813                 GtkTreeModel *store;
1814                 GtkTreeIter iter;
1815                 gboolean valid = TRUE;
1816
1817                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1818                 gtk_tree_model_get_iter(store, &iter, tpath);
1819                 gtk_tree_path_free(tpath);
1820                 tpath = nullptr;
1821
1822                 while (valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vf->listview), &iter, FALSE) == 0)
1823                         {
1824                         GList *list;
1825                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1826
1827                         for (; list; list = list->next)
1828                                 {
1829                                 auto fd = static_cast<FileData *>(list->data);
1830                                 if (fd && fd->rating == STAR_RATING_NOT_READ)
1831                                         {
1832                                         vf->stars_filedata = fd;
1833
1834                                         if (vf->stars_id == 0)
1835                                                 {
1836                                                 vf->stars_id = g_idle_add_full(G_PRIORITY_LOW, vf_stars_cb, vf, nullptr);
1837                                                 }
1838
1839                                         return fd;
1840                                         }
1841                                 }
1842
1843                         valid = gtk_tree_model_iter_next(store, &iter);
1844                         }
1845                 }
1846
1847         /* Then iterate through the entire list to load all of them. */
1848
1849         GList *work;
1850         for (work = vf->list; work; work = work->next)
1851                 {
1852                 auto fd = static_cast<FileData *>(work->data);
1853
1854                 if (fd && fd->rating == STAR_RATING_NOT_READ)
1855                         {
1856                         vf->stars_filedata = fd;
1857
1858                         if (vf->stars_id == 0)
1859                                 {
1860                                 vf->stars_id = g_idle_add_full(G_PRIORITY_LOW, vf_stars_cb, vf, nullptr);
1861                                 }
1862
1863                         return fd;
1864                         }
1865                 }
1866
1867         return nullptr;
1868 }
1869
1870 /*
1871  *-----------------------------------------------------------------------------
1872  * row stuff
1873  *-----------------------------------------------------------------------------
1874  */
1875
1876 gint vficon_index_by_fd(ViewFile *vf, FileData *in_fd)
1877 {
1878         gint p = 0;
1879         GList *work;
1880
1881         if (!in_fd) return -1;
1882
1883         work = vf->list;
1884         while (work)
1885                 {
1886                 auto fd = static_cast<FileData *>(work->data);
1887                 if (fd == in_fd) return p;
1888                 work = work->next;
1889                 p++;
1890                 }
1891
1892         return -1;
1893 }
1894
1895 /*
1896  *-----------------------------------------------------------------------------
1897  *
1898  *-----------------------------------------------------------------------------
1899  */
1900
1901 static gboolean vficon_refresh_real(ViewFile *vf, gboolean keep_position)
1902 {
1903         gboolean ret = TRUE;
1904         GList *work;
1905         GList *new_work;
1906         FileData *first_selected = nullptr;
1907         GList *new_filelist = nullptr;
1908         GList *new_fd_list = nullptr;
1909         GList *old_selected = nullptr;
1910         GtkTreePath *end_path = nullptr;
1911         GtkTreePath *start_path = nullptr;
1912
1913         gtk_tree_view_get_visible_range(GTK_TREE_VIEW(vf->listview), &start_path, &end_path);
1914
1915         if (vf->dir_fd)
1916                 {
1917                 ret = filelist_read(vf->dir_fd, &new_filelist, nullptr);
1918                 new_filelist = file_data_filter_marks_list(new_filelist, vf_marks_get_filter(vf));
1919                 new_filelist = g_list_first(new_filelist);
1920                 new_filelist = file_data_filter_file_filter_list(new_filelist, vf_file_filter_get_filter(vf));
1921
1922                 new_filelist = g_list_first(new_filelist);
1923                 new_filelist = file_data_filter_class_list(new_filelist, vf_class_get_filter(vf));
1924
1925                 }
1926
1927         vf->list = filelist_sort(vf->list, vf->sort_method, vf->sort_ascend, vf->sort_case); /* the list might not be sorted if there were renames */
1928         new_filelist = filelist_sort(new_filelist, vf->sort_method, vf->sort_ascend, vf->sort_case);
1929
1930         if (VFICON(vf)->selection)
1931                 {
1932                 old_selected = g_list_copy(VFICON(vf)->selection);
1933                 first_selected = static_cast<FileData *>(VFICON(vf)->selection->data);
1934                 file_data_ref(first_selected);
1935                 g_list_free(VFICON(vf)->selection);
1936                 VFICON(vf)->selection = nullptr;
1937                 }
1938
1939         /* iterate old list and new list, looking for differences */
1940         work = vf->list;
1941         new_work = new_filelist;
1942         while (work || new_work)
1943                 {
1944                 FileData *fd = nullptr;
1945                 FileData *new_fd = nullptr;
1946                 gint match;
1947
1948                 if (work && new_work)
1949                         {
1950                         fd = static_cast<FileData *>(work->data);
1951                         new_fd = static_cast<FileData *>(new_work->data);
1952
1953                         if (fd == new_fd)
1954                                 {
1955                                 /* not changed, go to next */
1956                                 work = work->next;
1957                                 new_work = new_work->next;
1958                                 if (fd->selected & SELECTION_SELECTED)
1959                                         {
1960                                         VFICON(vf)->selection = g_list_prepend(VFICON(vf)->selection, fd);
1961                                         }
1962                                 continue;
1963                                 }
1964
1965                         match = filelist_sort_compare_filedata_full(fd, new_fd, vf->sort_method, vf->sort_ascend);
1966                         if (match == 0) g_warning("multiple fd for the same path");
1967                         }
1968                 else if (work)
1969                         {
1970                         /* old item was deleted */
1971                         fd = static_cast<FileData *>(work->data);
1972                         match = -1;
1973                         }
1974                 else
1975                         {
1976                         /* new item was added */
1977                         new_fd = static_cast<FileData *>(new_work->data);
1978                         match = 1;
1979                         }
1980
1981                 if (match < 0)
1982                         {
1983                         /* file no longer exists, delete from vf->list */
1984                         GList *to_delete = work;
1985                         work = work->next;
1986                         if (fd == VFICON(vf)->prev_selection) VFICON(vf)->prev_selection = nullptr;
1987                         if (fd == VFICON(vf)->click_fd) VFICON(vf)->click_fd = nullptr;
1988                         file_data_unref(fd);
1989                         vf->list = g_list_delete_link(vf->list, to_delete);
1990                         }
1991                 else
1992                         {
1993                         /* new file, add to vf->list */
1994                         file_data_ref(new_fd);
1995                         new_fd->selected = SELECTION_NONE;
1996                         if (work)
1997                                 {
1998                                 vf->list = g_list_insert_before(vf->list, work, new_fd);
1999                                 }
2000                         else
2001                                 {
2002                                 /* it is faster to append all new entries together later */
2003                                 new_fd_list = g_list_prepend(new_fd_list, new_fd);
2004                                 }
2005
2006                         new_work = new_work->next;
2007                         }
2008                 }
2009
2010         if (new_fd_list)
2011                 {
2012                 vf->list = g_list_concat(vf->list, g_list_reverse(new_fd_list));
2013                 }
2014
2015         VFICON(vf)->selection = g_list_reverse(VFICON(vf)->selection);
2016
2017         /* Preserve the original selection order */
2018         if (old_selected)
2019                 {
2020                 GList *reversed_old_selected;
2021
2022                 reversed_old_selected = g_list_reverse(old_selected);
2023                 while (reversed_old_selected)
2024                         {
2025                         GList *tmp;
2026                         tmp = g_list_find(VFICON(vf)->selection, reversed_old_selected->data);
2027                         if (tmp)
2028                                 {
2029                                 VFICON(vf)->selection = g_list_remove_link(VFICON(vf)->selection, tmp);
2030                                 VFICON(vf)->selection = g_list_concat(tmp, VFICON(vf)->selection);
2031                                 }
2032                         reversed_old_selected = reversed_old_selected->next;
2033                         }
2034                 g_list_free(old_selected);
2035                 }
2036
2037         filelist_free(new_filelist);
2038
2039         vficon_populate(vf, TRUE, keep_position);
2040
2041         if (first_selected && !VFICON(vf)->selection)
2042                 {
2043                 /* all selected files disappeared */
2044                 vficon_select_closest(vf, first_selected);
2045                 }
2046         file_data_unref(first_selected);
2047
2048         if (start_path)
2049                 {
2050                 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vf->listview), start_path, nullptr, FALSE, 0.0, 0.0);
2051                 }
2052
2053         gtk_tree_path_free(start_path);
2054         gtk_tree_path_free(end_path);
2055
2056         return ret;
2057 }
2058
2059 gboolean vficon_refresh(ViewFile *vf)
2060 {
2061         return vficon_refresh_real(vf, TRUE);
2062 }
2063
2064 /*
2065  *-----------------------------------------------------------------------------
2066  * draw, etc.
2067  *-----------------------------------------------------------------------------
2068  */
2069
2070 struct ColumnData
2071 {
2072         ViewFile *vf;
2073         gint number;
2074 };
2075
2076 static void vficon_cell_data_cb(GtkTreeViewColumn *, GtkCellRenderer *cell,
2077                                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2078 {
2079         auto cd = static_cast<ColumnData *>(data);
2080         FileData *fd;
2081         gchar *star_rating;
2082         GList *list;
2083         ViewFile *vf = cd->vf;
2084
2085         if (!GQV_IS_CELL_RENDERER_ICON(cell)) return;
2086
2087         gtk_tree_model_get(tree_model, iter, FILE_COLUMN_POINTER, &list, -1);
2088
2089         fd = static_cast<FileData *>(g_list_nth_data(list, cd->number));
2090
2091         if (fd)
2092                 {
2093                 const gchar *link;
2094                 gchar *name_sidecars = nullptr;
2095                 GdkRGBA color_bg;
2096                 GdkRGBA color_fg;
2097                 GdkRGBA color_bg_style;
2098                 GdkRGBA color_fg_style;
2099                 GtkStateType state = GTK_STATE_NORMAL;
2100                 GtkStyle *style;
2101
2102                 g_assert(fd->magick == FD_MAGICK);
2103
2104                 if (options->show_star_rating && fd->rating != STAR_RATING_NOT_READ)
2105                         {
2106                         star_rating = convert_rating_to_stars(fd->rating);
2107                         }
2108                 else
2109                         {
2110                         star_rating = nullptr;
2111                         }
2112
2113                 link = islink(fd->path) ? GQ_LINK_STR : "";
2114                 if (fd->sidecar_files)
2115                         {
2116                         gchar *sidecars = file_data_sc_list_to_string(fd);
2117                         if (options->show_star_rating && VFICON(vf)->show_text)
2118                                 {
2119                                 name_sidecars = g_strdup_printf("%s%s %s\n%s", link, fd->name, sidecars, star_rating);
2120                                 }
2121                         else if (options->show_star_rating)
2122                                 {
2123                                 name_sidecars = g_strdup_printf("%s", star_rating);
2124                                 }
2125                         else if (VFICON(vf)->show_text)
2126                                 {
2127                                 name_sidecars = g_strdup_printf("%s%s %s", link, fd->name, sidecars);
2128                                 }
2129                         g_free(sidecars);
2130                         }
2131                 else
2132                         {
2133                         const gchar *disabled_grouping = fd->disable_grouping ? _(" [NO GROUPING]") : "";
2134                         if (options->show_star_rating && VFICON(vf)->show_text)
2135                                 {
2136                                 name_sidecars = g_strdup_printf("%s%s%s\n%s", link, fd->name, disabled_grouping, star_rating);
2137                                 }
2138                         else if (options->show_star_rating)
2139                                 {
2140                                 name_sidecars = g_strdup_printf("%s", star_rating);
2141                                 }
2142                         else if (VFICON(vf)->show_text)
2143                                 {
2144                                 name_sidecars = g_strdup_printf("%s%s%s", link, fd->name, disabled_grouping);
2145                                 }
2146                         }
2147                 g_free(star_rating);
2148
2149                 style = gtk_widget_get_style(vf->listview);
2150                 if (fd->selected & SELECTION_SELECTED)
2151                         {
2152                         state = GTK_STATE_SELECTED;
2153                         }
2154
2155                 convert_gdkcolor_to_gdkrgba(&style->text[state], &color_fg_style);
2156                 convert_gdkcolor_to_gdkrgba(&style->base[state], &color_bg_style);
2157
2158                 memcpy(&color_fg, &color_fg_style, sizeof(color_fg));
2159                 memcpy(&color_bg, &color_bg_style, sizeof(color_bg));
2160
2161                 if (fd->selected & SELECTION_PRELIGHT)
2162                         {
2163                         shift_color(&color_bg, -1, 0);
2164                         }
2165
2166                 g_object_set(cell, "pixbuf", fd->thumb_pixbuf,
2167                                         "text", name_sidecars,
2168                                         "marks", file_data_get_marks(fd),
2169                                         "show_marks", vf->marks_enabled,
2170                                         "cell-background-rgba", &color_bg,
2171                                         "cell-background-set", TRUE,
2172                                         "foreground-rgba", &color_fg,
2173                                         "foreground-set", TRUE,
2174                                         "has-focus", (VFICON(vf)->focus_fd == fd), NULL);
2175                 g_free(name_sidecars);
2176                 }
2177         else
2178                 {
2179                 g_object_set(cell,      "pixbuf", NULL,
2180                                         "text", NULL,
2181                                         "show_marks", FALSE,
2182                                         "cell-background-set", FALSE,
2183                                         "foreground-set", FALSE,
2184                                         "has-focus", FALSE, NULL);
2185                 }
2186 }
2187
2188 static void vficon_append_column(ViewFile *vf, gint n)
2189 {
2190         GtkTreeViewColumn *column;
2191         GtkCellRenderer *renderer;
2192
2193         column = gtk_tree_view_column_new();
2194         gtk_tree_view_column_set_min_width(column, 0);
2195
2196         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2197         gtk_tree_view_column_set_alignment(column, 0.5);
2198
2199         renderer = gqv_cell_renderer_icon_new();
2200         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2201         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2202                                          "ypad", THUMB_BORDER_PADDING,
2203                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2204
2205         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2206         g_object_set_data(G_OBJECT(renderer), "column_number", GINT_TO_POINTER(n));
2207
2208         auto cd = g_new0(ColumnData, 1);
2209         cd->vf = vf;
2210         cd->number = n;
2211         gtk_tree_view_column_set_cell_data_func(column, renderer, vficon_cell_data_cb, cd, g_free);
2212
2213         gtk_tree_view_append_column(GTK_TREE_VIEW(vf->listview), column);
2214
2215         g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(vficon_mark_toggled_cb), vf);
2216 }
2217
2218 /*
2219  *-----------------------------------------------------------------------------
2220  * base
2221  *-----------------------------------------------------------------------------
2222  */
2223
2224 gboolean vficon_set_fd(ViewFile *vf, FileData *dir_fd)
2225 {
2226         gboolean ret;
2227
2228         if (!dir_fd) return FALSE;
2229         if (vf->dir_fd == dir_fd) return TRUE;
2230
2231         file_data_unref(vf->dir_fd);
2232         vf->dir_fd = file_data_ref(dir_fd);
2233
2234         g_list_free(VFICON(vf)->selection);
2235         VFICON(vf)->selection = nullptr;
2236
2237         g_list_free(vf->list);
2238         vf->list = nullptr;
2239
2240         /* NOTE: populate will clear the store for us */
2241         ret = vficon_refresh_real(vf, FALSE);
2242
2243         VFICON(vf)->focus_fd = nullptr;
2244         vficon_move_focus(vf, 0, 0, FALSE);
2245
2246         return ret;
2247 }
2248
2249 void vficon_destroy_cb(GtkWidget *, gpointer data)
2250 {
2251         auto vf = static_cast<ViewFile *>(data);
2252
2253         vf_refresh_idle_cancel(vf);
2254
2255         file_data_unregister_notify_func(vf_notify_cb, vf);
2256
2257         tip_unschedule(vf);
2258
2259         vf_thumb_cleanup(vf);
2260         vf_star_cleanup(vf);
2261
2262         g_list_free(vf->list);
2263         g_list_free(VFICON(vf)->selection);
2264 }
2265
2266 ViewFile *vficon_new(ViewFile *vf, FileData *)
2267 {
2268         GtkListStore *store;
2269         GtkTreeSelection *selection;
2270         gint i;
2271
2272         vf->info = g_new0(ViewFileInfoIcon, 1);
2273
2274         VFICON(vf)->show_text = options->show_icon_names;
2275
2276         store = gtk_list_store_new(1, G_TYPE_POINTER);
2277         vf->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2278         g_object_unref(store);
2279
2280         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vf->listview));
2281         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2282
2283         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vf->listview), FALSE);
2284         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vf->listview), FALSE);
2285
2286         for (i = 0; i < VFICON_MAX_COLUMNS; i++)
2287                 {
2288                 vficon_append_column(vf, i);
2289                 }
2290
2291         /* zero width column to hide tree view focus, we draw it ourselves */
2292         vficon_append_column(vf, i);
2293         /* end column to fill white space */
2294         vficon_append_column(vf, i);
2295
2296         g_signal_connect(G_OBJECT(vf->listview), "size_allocate",
2297                          G_CALLBACK(vficon_sized_cb), vf);
2298
2299         gtk_widget_set_events(vf->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2300                               static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK));
2301
2302         g_signal_connect(G_OBJECT(vf->listview),"motion_notify_event",
2303                          G_CALLBACK(vficon_motion_cb), vf);
2304         g_signal_connect(G_OBJECT(vf->listview), "leave_notify_event",
2305                          G_CALLBACK(vficon_leave_cb), vf);
2306
2307         /* force VFICON(vf)->columns to be at least 1 (sane) - this will be corrected in the size_cb */
2308         vficon_populate_at_new_size(vf, 1, 1, FALSE);
2309
2310         file_data_register_notify_func(vf_notify_cb, vf, NOTIFY_PRIORITY_MEDIUM);
2311
2312         return vf;
2313 }
2314
2315 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */