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