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