Use std::swap instead of temporary values
[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 #include <utility>
26
27 #include <glib-object.h>
28
29 #include "cellrenderericon.h"
30 #include "collect.h"
31 #include "compat.h"
32 #include "debug.h"
33 #include "dnd.h"
34 #include "filedata.h"
35 #include "img-view.h"
36 #include "intl.h"
37 #include "layout-image.h"
38 #include "main-defines.h"
39 #include "metadata.h"
40 #include "misc.h"
41 #include "options.h"
42 #include "ui-fileops.h"
43 #include "ui-misc.h"
44 #include "ui-tree-edit.h"
45 #include "uri-utils.h"
46 #include "utilops.h"
47 #include "view-file.h"
48
49 namespace
50 {
51
52 /* between these, the icon width is increased by thumb_max_width / 2 */
53 constexpr gint THUMB_MIN_ICON_WIDTH = 128;
54 constexpr gint THUMB_MAX_ICON_WIDTH = 160;
55
56 constexpr gint THUMB_MIN_ICON_WIDTH_WITH_MARKS = TOGGLE_SPACING * FILEDATA_MARKS_SIZE;
57
58 constexpr gint VFICON_MAX_COLUMNS = 32;
59
60 constexpr gint THUMB_BORDER_PADDING = 2;
61
62 constexpr gint VFICON_TIP_DELAY = 500;
63
64 enum {
65         FILE_COLUMN_POINTER = VIEW_FILE_COLUMN_POINTER,
66         FILE_COLUMN_COUNT
67 };
68
69 struct ColumnData
70 {
71         ViewFile *vf;
72         gint number;
73 };
74
75 } // namespace
76
77 static void vficon_toggle_filenames(ViewFile *vf);
78 static void vficon_selection_remove(ViewFile *vf, FileData *fd, SelectionType mask, GtkTreeIter *iter);
79 static void vficon_move_focus(ViewFile *vf, gint row, gint col, gboolean relative);
80 static void vficon_set_focus(ViewFile *vf, FileData *fd);
81 static void vficon_populate_at_new_size(ViewFile *vf, gint w, gint h, gboolean force);
82
83
84 /*
85  *-----------------------------------------------------------------------------
86  * pop-up menu
87  *-----------------------------------------------------------------------------
88  */
89
90 GList *vficon_selection_get_one(ViewFile *, FileData *fd)
91 {
92         return g_list_prepend(filelist_copy(fd->sidecar_files), file_data_ref(fd));
93 }
94
95 GList *vficon_pop_menu_file_list(ViewFile *vf)
96 {
97         if (!VFICON(vf)->click_fd) return nullptr;
98
99         if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
100                 {
101                 return vf_selection_get_list(vf);
102                 }
103
104         return vficon_selection_get_one(vf, VFICON(vf)->click_fd);
105 }
106
107 void vficon_pop_menu_view_cb(GtkWidget *, gpointer data)
108 {
109         auto vf = static_cast<ViewFile *>(data);
110
111         if (!VFICON(vf)->click_fd) return;
112
113         if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
114                 {
115                 GList *list;
116
117                 list = vf_selection_get_list(vf);
118                 view_window_new_from_list(list);
119                 filelist_free(list);
120                 }
121         else
122                 {
123                 view_window_new(VFICON(vf)->click_fd);
124                 }
125 }
126
127 void vficon_pop_menu_rename_cb(GtkWidget *, gpointer data)
128 {
129         auto vf = static_cast<ViewFile *>(data);
130
131         file_util_rename(nullptr, vf_pop_menu_file_list(vf), vf->listview);
132 }
133
134 void vficon_pop_menu_show_names_cb(GtkWidget *, gpointer data)
135 {
136         auto vf = static_cast<ViewFile *>(data);
137
138         vficon_toggle_filenames(vf);
139 }
140
141 static void vficon_toggle_star_rating(ViewFile *vf)
142 {
143         GtkAllocation allocation;
144
145         options->show_star_rating = !options->show_star_rating;
146
147         gtk_widget_get_allocation(vf->listview, &allocation);
148         vficon_populate_at_new_size(vf, allocation.width, allocation.height, TRUE);
149 }
150
151 void vficon_pop_menu_show_star_rating_cb(GtkWidget *, gpointer data)
152 {
153         auto vf = static_cast<ViewFile *>(data);
154
155         vficon_toggle_star_rating(vf);
156 }
157
158 void vficon_pop_menu_refresh_cb(GtkWidget *, gpointer data)
159 {
160         auto vf = static_cast<ViewFile *>(data);
161
162         vf_refresh(vf);
163 }
164
165 void vficon_popup_destroy_cb(GtkWidget *, gpointer data)
166 {
167         auto vf = static_cast<ViewFile *>(data);
168         vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
169         VFICON(vf)->click_fd = nullptr;
170         vf->popup = nullptr;
171 }
172
173 /*
174  *-------------------------------------------------------------------
175  * signals
176  *-------------------------------------------------------------------
177  */
178
179 static void vficon_send_layout_select(ViewFile *vf, FileData *fd)
180 {
181         FileData *read_ahead_fd = nullptr;
182         FileData *sel_fd;
183         FileData *cur_fd;
184
185         if (!vf->layout || !fd) return;
186
187         sel_fd = fd;
188
189         cur_fd = layout_image_get_fd(vf->layout);
190         if (sel_fd == cur_fd) return; /* no change */
191
192         if (options->image.enable_read_ahead)
193                 {
194                 gint row;
195
196                 row = g_list_index(vf->list, fd);
197                 if (row > vficon_index_by_fd(vf, cur_fd) &&
198                     static_cast<guint>(row + 1) < vf_count(vf, nullptr))
199                         {
200                         read_ahead_fd = vf_index_get_data(vf, row + 1);
201                         }
202                 else if (row > 0)
203                         {
204                         read_ahead_fd = vf_index_get_data(vf, row - 1);
205                         }
206                 }
207
208         layout_image_set_with_ahead(vf->layout, sel_fd, read_ahead_fd);
209 }
210
211 static void vficon_toggle_filenames(ViewFile *vf)
212 {
213         GtkAllocation allocation;
214         VFICON(vf)->show_text = !VFICON(vf)->show_text;
215         options->show_icon_names = VFICON(vf)->show_text;
216
217         gtk_widget_get_allocation(vf->listview, &allocation);
218         vficon_populate_at_new_size(vf, allocation.width, allocation.height, TRUE);
219 }
220
221 static gint vficon_get_icon_width(ViewFile *vf)
222 {
223         gint width;
224
225         if (!VFICON(vf)->show_text && !vf->marks_enabled ) return options->thumbnails.max_width;
226
227         width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
228         if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
229         if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
230         if (vf->marks_enabled && width < THUMB_MIN_ICON_WIDTH_WITH_MARKS) width = THUMB_MIN_ICON_WIDTH_WITH_MARKS;
231
232         return width;
233 }
234
235 /*
236  *-------------------------------------------------------------------
237  * misc utils
238  *-------------------------------------------------------------------
239  */
240
241 static gboolean vficon_find_position(ViewFile *vf, FileData *fd, gint *row, gint *col)
242 {
243         gint n;
244
245         n = g_list_index(vf->list, fd);
246
247         if (n < 0) return FALSE;
248
249         *row = n / VFICON(vf)->columns;
250         *col = n - (*row * VFICON(vf)->columns);
251
252         return TRUE;
253 }
254
255 static gboolean vficon_find_iter(ViewFile *vf, FileData *fd, GtkTreeIter *iter, gint *column)
256 {
257         GtkTreeModel *store;
258         gint row;
259         gint col;
260
261         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
262         if (!vficon_find_position(vf, fd, &row, &col)) return FALSE;
263         if (!gtk_tree_model_iter_nth_child(store, iter, nullptr, row)) return FALSE;
264         if (column) *column = col;
265
266         return TRUE;
267 }
268
269 static FileData *vficon_find_data(ViewFile *vf, gint row, gint col, GtkTreeIter *iter)
270 {
271         GtkTreeModel *store;
272         GtkTreeIter p;
273
274         if (row < 0 || col < 0) return nullptr;
275
276         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
277         if (gtk_tree_model_iter_nth_child(store, &p, nullptr, row))
278                 {
279                 GList *list;
280
281                 gtk_tree_model_get(store, &p, FILE_COLUMN_POINTER, &list, -1);
282                 if (!list) return nullptr;
283
284                 if (iter) *iter = p;
285
286                 return static_cast<FileData *>(g_list_nth_data(list, col));
287                 }
288
289         return nullptr;
290 }
291
292 static FileData *vficon_find_data_by_coord(ViewFile *vf, gint x, gint y, GtkTreeIter *iter)
293 {
294         GtkTreePath *tpath;
295         GtkTreeViewColumn *column;
296
297         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), x, y,
298                                           &tpath, &column, nullptr, nullptr))
299                 {
300                 GtkTreeModel *store;
301                 GtkTreeIter row;
302                 GList *list;
303                 gint n;
304
305                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
306                 gtk_tree_model_get_iter(store, &row, tpath);
307                 gtk_tree_path_free(tpath);
308
309                 gtk_tree_model_get(store, &row, FILE_COLUMN_POINTER, &list, -1);
310
311                 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
312                 if (list)
313                         {
314                         if (iter) *iter = row;
315                         return static_cast<FileData *>(g_list_nth_data(list, n));
316                         }
317                 }
318
319         return nullptr;
320 }
321
322 static void vficon_mark_toggled_cb(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
323 {
324         auto vf = static_cast<ViewFile *>(data);
325         GtkTreeModel *store;
326         GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
327         GtkTreeIter row;
328         gint column;
329         GList *list;
330         guint toggled_mark;
331         FileData *fd;
332
333         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
334         if (!path || !gtk_tree_model_get_iter(store, &row, path)) return;
335
336         gtk_tree_model_get(store, &row, FILE_COLUMN_POINTER, &list, -1);
337
338         column = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cell), "column_number"));
339         g_object_get(G_OBJECT(cell), "toggled_mark", &toggled_mark, NULL);
340
341         fd = static_cast<FileData *>(g_list_nth_data(list, column));
342         if (fd)
343                 {
344                 file_data_set_mark(fd, toggled_mark, !file_data_get_mark(fd, toggled_mark));
345                 }
346 }
347
348
349 /*
350  *-------------------------------------------------------------------
351  * tooltip type window
352  *-------------------------------------------------------------------
353  */
354
355 static void tip_show(ViewFile *vf)
356 {
357         GtkWidget *label;
358         gint x;
359         gint y;
360         GdkDisplay *display;
361         GdkSeat *seat;
362         GdkDevice *device;
363
364         if (VFICON(vf)->tip_window) return;
365
366         seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_tree_view_get_bin_window(GTK_TREE_VIEW(vf->listview))));
367         device = gdk_seat_get_pointer(seat);
368         gdk_window_get_device_position(gtk_tree_view_get_bin_window(GTK_TREE_VIEW(vf->listview)),
369                                                 device, &x, &y, nullptr);
370
371         VFICON(vf)->tip_fd = vficon_find_data_by_coord(vf, x, y, nullptr);
372         if (!VFICON(vf)->tip_fd) return;
373
374         VFICON(vf)->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
375         gtk_window_set_resizable(GTK_WINDOW(VFICON(vf)->tip_window), FALSE);
376         gtk_container_set_border_width(GTK_CONTAINER(VFICON(vf)->tip_window), 2);
377
378         label = gtk_label_new(VFICON(vf)->tip_fd->name);
379
380         g_object_set_data(G_OBJECT(VFICON(vf)->tip_window), "tip_label", label);
381         gq_gtk_container_add(GTK_WIDGET(VFICON(vf)->tip_window), label);
382         gtk_widget_show(label);
383
384         display = gdk_display_get_default();
385         seat = gdk_display_get_default_seat(display);
386         device = gdk_seat_get_pointer(seat);
387         gdk_device_get_position(device, nullptr, &x, &y);
388
389         if (!gtk_widget_get_realized(VFICON(vf)->tip_window)) gtk_widget_realize(VFICON(vf)->tip_window);
390         gq_gtk_window_move(GTK_WINDOW(VFICON(vf)->tip_window), x + 16, y + 16);
391         gtk_widget_show(VFICON(vf)->tip_window);
392 }
393
394 static void tip_hide(ViewFile *vf)
395 {
396         if (VFICON(vf)->tip_window) gq_gtk_widget_destroy(VFICON(vf)->tip_window);
397         VFICON(vf)->tip_window = nullptr;
398 }
399
400 static gboolean tip_schedule_cb(gpointer data)
401 {
402         auto vf = static_cast<ViewFile *>(data);
403         GtkWidget *window;
404
405         if (!VFICON(vf)->tip_delay_id) return FALSE;
406
407         window = gtk_widget_get_toplevel(vf->listview);
408
409         if (gtk_widget_get_sensitive(window) &&
410             gtk_window_has_toplevel_focus(GTK_WINDOW(window)))
411                 {
412                 tip_show(vf);
413                 }
414
415         VFICON(vf)->tip_delay_id = 0;
416         return FALSE;
417 }
418
419 static void tip_schedule(ViewFile *vf)
420 {
421         tip_hide(vf);
422
423         if (VFICON(vf)->tip_delay_id)
424                 {
425                 g_source_remove(VFICON(vf)->tip_delay_id);
426                 VFICON(vf)->tip_delay_id = 0;
427                 }
428
429         if (!VFICON(vf)->show_text)
430                 {
431                 VFICON(vf)->tip_delay_id = g_timeout_add(VFICON_TIP_DELAY, tip_schedule_cb, vf);
432                 }
433 }
434
435 static void tip_unschedule(ViewFile *vf)
436 {
437         tip_hide(vf);
438
439         if (VFICON(vf)->tip_delay_id)
440                 {
441                 g_source_remove(VFICON(vf)->tip_delay_id);
442                 VFICON(vf)->tip_delay_id = 0;
443                 }
444 }
445
446 static void tip_update(ViewFile *vf, FileData *fd)
447 {
448         GdkDisplay *display = gdk_display_get_default();
449         GdkSeat *seat = gdk_display_get_default_seat(display);
450         GdkDevice *device = gdk_seat_get_pointer(seat);
451
452         if (VFICON(vf)->tip_window)
453                 {
454                 gint x;
455                 gint y;
456
457                 gdk_device_get_position(device, nullptr, &x, &y);
458
459                 gq_gtk_window_move(GTK_WINDOW(VFICON(vf)->tip_window), x + 16, y + 16);
460
461                 if (fd != VFICON(vf)->tip_fd)
462                         {
463                         GtkWidget *label;
464
465                         VFICON(vf)->tip_fd = fd;
466
467                         if (!VFICON(vf)->tip_fd)
468                                 {
469                                 tip_hide(vf);
470                                 tip_schedule(vf);
471                                 return;
472                                 }
473
474                         label = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(VFICON(vf)->tip_window), "tip_label"));
475                         gtk_label_set_text(GTK_LABEL(label), VFICON(vf)->tip_fd->name);
476                         }
477                 }
478         else
479                 {
480                 tip_schedule(vf);
481                 }
482 }
483
484 /*
485  *-------------------------------------------------------------------
486  * dnd
487  *-------------------------------------------------------------------
488  */
489
490 static void vficon_dnd_get(GtkWidget *, GdkDragContext *,
491                            GtkSelectionData *selection_data, guint,
492                            guint, gpointer data)
493 {
494         auto vf = static_cast<ViewFile *>(data);
495         GList *list = nullptr;
496
497         if (!VFICON(vf)->click_fd) return;
498
499         if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
500                 {
501                 list = vf_selection_get_list(vf);
502                 }
503         else
504                 {
505                 list = g_list_append(nullptr, file_data_ref(VFICON(vf)->click_fd));
506                 }
507
508         if (!list) return;
509         uri_selection_data_set_uris_from_filelist(selection_data, list);
510         filelist_free(list);
511 }
512
513 static void vficon_drag_data_received(GtkWidget *, GdkDragContext *,
514                                       int x, int y, GtkSelectionData *selection,
515                                       guint info, guint, gpointer data)
516 {
517         auto vf = static_cast<ViewFile *>(data);
518
519         if (info == TARGET_TEXT_PLAIN) {
520                 FileData *fd = vficon_find_data_by_coord(vf, x, y, nullptr);
521
522                 if (fd) {
523                         /* Add keywords to file */
524                         auto str = reinterpret_cast<gchar *>(gtk_selection_data_get_text(selection));
525                         GList *kw_list = string_to_keywords_list(str);
526
527                         metadata_append_list(fd, KEYWORD_KEY, kw_list);
528                         g_list_free_full(kw_list, g_free);
529                         g_free(str);
530                 }
531         }
532 }
533
534 static void vficon_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
535 {
536         auto vf = static_cast<ViewFile *>(data);
537
538         tip_unschedule(vf);
539
540         if (VFICON(vf)->click_fd && VFICON(vf)->click_fd->thumb_pixbuf)
541                 {
542                 gint items;
543
544                 if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
545                         items = g_list_length(VFICON(vf)->selection);
546                 else
547                         items = 1;
548
549                 dnd_set_drag_icon(widget, context, VFICON(vf)->click_fd->thumb_pixbuf, items);
550                 }
551 }
552
553 static void vficon_dnd_end(GtkWidget *, GdkDragContext *context, gpointer data)
554 {
555         auto vf = static_cast<ViewFile *>(data);
556
557         vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
558
559         if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
560                 {
561                 vf_refresh(vf);
562                 }
563
564         tip_unschedule(vf);
565 }
566
567 void vficon_dnd_init(ViewFile *vf)
568 {
569         gtk_drag_source_set(vf->listview, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
570                             dnd_file_drag_types, dnd_file_drag_types_count,
571                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
572         gtk_drag_dest_set(vf->listview, GTK_DEST_DEFAULT_ALL,
573                             dnd_file_drag_types, dnd_file_drag_types_count,
574                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
575
576         g_signal_connect(G_OBJECT(vf->listview), "drag_data_get",
577                          G_CALLBACK(vficon_dnd_get), vf);
578         g_signal_connect(G_OBJECT(vf->listview), "drag_begin",
579                          G_CALLBACK(vficon_dnd_begin), vf);
580         g_signal_connect(G_OBJECT(vf->listview), "drag_end",
581                          G_CALLBACK(vficon_dnd_end), vf);
582         g_signal_connect(G_OBJECT(vf->listview), "drag_data_received",
583                          G_CALLBACK(vficon_drag_data_received), vf);
584 }
585
586 /*
587  *-------------------------------------------------------------------
588  * cell updates
589  *-------------------------------------------------------------------
590  */
591
592 static void vficon_selection_set(ViewFile *vf, FileData *fd, SelectionType value, GtkTreeIter *iter)
593 {
594         GtkTreeModel *store;
595         GList *list;
596
597         if (!fd) return;
598
599         if (fd->selected == value) return;
600         fd->selected = value;
601
602         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
603         if (iter)
604                 {
605                 gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
606                 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, list, -1);
607                 }
608         else
609                 {
610                 GtkTreeIter row;
611
612                 if (vficon_find_iter(vf, fd, &row, nullptr))
613                         {
614                         gtk_tree_model_get(store, &row, FILE_COLUMN_POINTER, &list, -1);
615                         if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, FILE_COLUMN_POINTER, list, -1);
616                         }
617                 }
618 }
619
620 static void vficon_selection_add(ViewFile *vf, FileData *fd, SelectionType mask, GtkTreeIter *iter)
621 {
622         if (!fd) return;
623
624         vficon_selection_set(vf, fd, static_cast<SelectionType>(fd->selected | mask), iter);
625 }
626
627 static void vficon_selection_remove(ViewFile *vf, FileData *fd, SelectionType mask, GtkTreeIter *iter)
628 {
629         if (!fd) return;
630
631         vficon_selection_set(vf, fd, static_cast<SelectionType>(fd->selected & ~mask), iter);
632 }
633
634 void vficon_marks_set(ViewFile *vf, gint)
635 {
636         GtkAllocation allocation;
637         gtk_widget_get_allocation(vf->listview, &allocation);
638         vficon_populate_at_new_size(vf, allocation.width, allocation.height, TRUE);
639 }
640
641 void vficon_star_rating_set(ViewFile *vf, gint)
642 {
643         GtkAllocation allocation;
644         gtk_widget_get_allocation(vf->listview, &allocation);
645         vficon_populate_at_new_size(vf, allocation.width, allocation.height, TRUE);
646 }
647
648 /*
649  *-------------------------------------------------------------------
650  * selections
651  *-------------------------------------------------------------------
652  */
653
654 static void vficon_verify_selections(ViewFile *vf)
655 {
656         GList *work;
657
658         work = VFICON(vf)->selection;
659         while (work)
660                 {
661                 auto fd = static_cast<FileData *>(work->data);
662                 work = work->next;
663
664                 if (vficon_index_by_fd(vf, fd) >= 0) continue;
665
666                 VFICON(vf)->selection = g_list_remove(VFICON(vf)->selection, fd);
667                 }
668 }
669
670 void vficon_select_all(ViewFile *vf)
671 {
672         GList *work;
673
674         g_list_free(VFICON(vf)->selection);
675         VFICON(vf)->selection = nullptr;
676
677         work = vf->list;
678         while (work)
679                 {
680                 auto fd = static_cast<FileData *>(work->data);
681                 work = work->next;
682
683                 VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
684                 vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
685                 }
686
687         vf_send_update(vf);
688 }
689
690 void vficon_select_none(ViewFile *vf)
691 {
692         GList *work;
693
694         work = VFICON(vf)->selection;
695         while (work)
696                 {
697                 auto fd = static_cast<FileData *>(work->data);
698                 work = work->next;
699
700                 vficon_selection_remove(vf, fd, SELECTION_SELECTED, nullptr);
701                 }
702
703         g_list_free(VFICON(vf)->selection);
704         VFICON(vf)->selection = nullptr;
705
706         vf_send_update(vf);
707 }
708
709 void vficon_select_invert(ViewFile *vf)
710 {
711         GList *work;
712
713         work = vf->list;
714         while (work)
715                 {
716                 auto fd = static_cast<FileData *>(work->data);
717                 work = work->next;
718
719                 if (fd->selected & SELECTION_SELECTED)
720                         {
721                         VFICON(vf)->selection = g_list_remove(VFICON(vf)->selection, fd);
722                         vficon_selection_remove(vf, fd, SELECTION_SELECTED, nullptr);
723                         }
724                 else
725                         {
726                         VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
727                         vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
728                         }
729                 }
730
731         vf_send_update(vf);
732 }
733
734 static void vficon_select(ViewFile *vf, FileData *fd)
735 {
736         VFICON(vf)->prev_selection = fd;
737
738         if (!fd || fd->selected & SELECTION_SELECTED) return;
739
740         VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
741         vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
742
743         vf_send_update(vf);
744 }
745
746 static void vficon_unselect(ViewFile *vf, FileData *fd)
747 {
748         VFICON(vf)->prev_selection = fd;
749
750         if (!fd || !(fd->selected & SELECTION_SELECTED) ) return;
751
752         VFICON(vf)->selection = g_list_remove(VFICON(vf)->selection, fd);
753         vficon_selection_remove(vf, fd, SELECTION_SELECTED, nullptr);
754
755         vf_send_update(vf);
756 }
757
758 static void vficon_select_util(ViewFile *vf, FileData *fd, gboolean select)
759 {
760         if (select)
761                 {
762                 vficon_select(vf, fd);
763                 }
764         else
765                 {
766                 vficon_unselect(vf, fd);
767                 }
768 }
769
770 static void vficon_select_region_util(ViewFile *vf, FileData *start, FileData *end, gboolean select)
771 {
772         gint row1;
773         gint col1;
774         gint row2;
775         gint col2;
776         gint i;
777         gint j;
778
779         if (!vficon_find_position(vf, start, &row1, &col1) ||
780             !vficon_find_position(vf, end, &row2, &col2) ) return;
781
782         VFICON(vf)->prev_selection = end;
783
784         if (!options->collections.rectangular_selection)
785                 {
786                 GList *work;
787
788                 if (g_list_index(vf->list, start) > g_list_index(vf->list, end))
789                         {
790                         std::swap(start, end);
791                         }
792
793                 work = g_list_find(vf->list, start);
794                 while (work)
795                         {
796                         auto fd = static_cast<FileData *>(work->data);
797                         vficon_select_util(vf, fd, select);
798
799                         if (work->data != end)
800                                 work = work->next;
801                         else
802                                 work = nullptr;
803                         }
804                 return;
805                 }
806
807         // rectangular_selection==true.
808         if (row2 < row1)
809                 {
810                 std::swap(row1, row2);
811                 }
812         if (col2 < col1)
813                 {
814                 std::swap(col1, col2);
815                 }
816
817         DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
818
819         for (i = row1; i <= row2; i++)
820                 {
821                 for (j = col1; j <= col2; j++)
822                         {
823                         FileData *fd = vficon_find_data(vf, i, j, nullptr);
824                         if (fd) vficon_select_util(vf, fd, select);
825                         }
826                 }
827 }
828
829 #pragma GCC diagnostic push
830 #pragma GCC diagnostic ignored "-Wunused-function"
831 gboolean vficon_index_is_selected_unused(ViewFile *vf, gint row)
832 {
833         auto *fd = static_cast<FileData *>(g_list_nth_data(vf->list, row));
834
835         if (!fd) return FALSE;
836
837         return (fd->selected & SELECTION_SELECTED);
838 }
839 #pragma GCC diagnostic pop
840
841 guint vficon_selection_count(ViewFile *vf, gint64 *bytes)
842 {
843         if (bytes)
844                 {
845                 gint64 b = 0;
846                 GList *work;
847
848                 work = VFICON(vf)->selection;
849                 while (work)
850                         {
851                         auto fd = static_cast<FileData *>(work->data);
852                         g_assert(fd->magick == FD_MAGICK);
853                         b += fd->size;
854
855                         work = work->next;
856                         }
857
858                 *bytes = b;
859                 }
860
861         return g_list_length(VFICON(vf)->selection);
862 }
863
864 GList *vficon_selection_get_list(ViewFile *vf)
865 {
866         GList *list = nullptr;
867
868         for (GList *work = g_list_last(VFICON(vf)->selection); work; work = work->prev)
869                 {
870                 auto fd = static_cast<FileData *>(work->data);
871                 g_assert(fd->magick == FD_MAGICK);
872
873                 list = g_list_concat(filelist_copy(fd->sidecar_files), list);
874                 list = g_list_prepend(list, file_data_ref(fd));
875                 }
876
877         return list;
878 }
879
880 GList *vficon_selection_get_list_by_index(ViewFile *vf)
881 {
882         GList *list = nullptr;
883         GList *work;
884
885         work = VFICON(vf)->selection;
886         while (work)
887                 {
888                 list = g_list_prepend(list, GINT_TO_POINTER(g_list_index(vf->list, work->data)));
889                 work = work->next;
890                 }
891
892         return g_list_reverse(list);
893 }
894
895 void vficon_select_by_fd(ViewFile *vf, FileData *fd)
896 {
897         if (!fd) return;
898         if (!g_list_find(vf->list, fd)) return;
899
900         if (!(fd->selected & SELECTION_SELECTED))
901                 {
902                 vf_select_none(vf);
903                 vficon_select(vf, fd);
904                 }
905
906         vficon_set_focus(vf, fd);
907 }
908
909 void vficon_select_list(ViewFile *vf, GList *list)
910 {
911         GList *work;
912         FileData *fd;
913
914         if (!list) return;
915
916         work = list;
917         while (work)
918                 {
919                 fd = static_cast<FileData *>(work->data);
920                 if (g_list_find(vf->list, fd))
921                         {
922                         VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
923                         vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
924                         }
925                 work = work->next;
926                 }
927 }
928
929 void vficon_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
930 {
931         GList *work;
932         gint n = mark - 1;
933
934         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
935
936         work = vf->list;
937         while (work)
938                 {
939                 auto fd = static_cast<FileData *>(work->data);
940                 gboolean mark_val;
941                 gboolean selected;
942
943                 g_assert(fd->magick == FD_MAGICK);
944
945                 mark_val = file_data_get_mark(fd, n);
946                 selected = fd->selected & SELECTION_SELECTED;
947
948                 switch (mode)
949                         {
950                         case MTS_MODE_SET: selected = mark_val;
951                                 break;
952                         case MTS_MODE_OR: selected = mark_val || selected;
953                                 break;
954                         case MTS_MODE_AND: selected = mark_val && selected;
955                                 break;
956                         case MTS_MODE_MINUS: selected = !mark_val && selected;
957                                 break;
958                         }
959
960                 vficon_select_util(vf, fd, selected);
961
962                 work = work->next;
963                 }
964 }
965
966 void vficon_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
967 {
968         GList *slist;
969         GList *work;
970         gint n = mark -1;
971
972         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
973
974         slist = vf_selection_get_list(vf);
975         work = slist;
976         while (work)
977                 {
978                 auto fd = static_cast<FileData *>(work->data);
979
980                 switch (mode)
981                         {
982                         case STM_MODE_SET: file_data_set_mark(fd, n, 1);
983                                 break;
984                         case STM_MODE_RESET: file_data_set_mark(fd, n, 0);
985                                 break;
986                         case STM_MODE_TOGGLE: file_data_set_mark(fd, n, !file_data_get_mark(fd, n));
987                                 break;
988                         }
989                 work = work->next;
990                 }
991         filelist_free(slist);
992 }
993
994 static void vficon_select_closest(ViewFile *vf, FileData *sel_fd)
995 {
996         GList *work;
997         FileData *fd = nullptr;
998
999         if (sel_fd->parent) sel_fd = sel_fd->parent;
1000         work = vf->list;
1001
1002         while (work)
1003                 {
1004                 gint match;
1005
1006                 fd = static_cast<FileData *>(work->data);
1007                 work = work->next;
1008
1009                 match = filelist_sort_compare_filedata_full(fd, sel_fd, vf->sort_method, vf->sort_ascend);
1010
1011                 if (match >= 0) break;
1012                 }
1013
1014         if (fd)
1015                 {
1016                 vficon_select(vf, fd);
1017                 vficon_send_layout_select(vf, fd);
1018                 }
1019 }
1020
1021
1022 /*
1023  *-------------------------------------------------------------------
1024  * focus
1025  *-------------------------------------------------------------------
1026  */
1027
1028 static void vficon_move_focus(ViewFile *vf, gint row, gint col, gboolean relative)
1029 {
1030         gint new_row;
1031         gint new_col;
1032
1033         if (relative)
1034                 {
1035                 new_row = VFICON(vf)->focus_row;
1036                 new_col = VFICON(vf)->focus_column;
1037
1038                 new_row += row;
1039                 if (new_row < 0) new_row = 0;
1040                 if (new_row >= VFICON(vf)->rows) new_row = VFICON(vf)->rows - 1;
1041
1042                 while (col != 0)
1043                         {
1044                         if (col < 0)
1045                                 {
1046                                 new_col--;
1047                                 col++;
1048                                 }
1049                         else
1050                                 {
1051                                 new_col++;
1052                                 col--;
1053                                 }
1054
1055                         if (new_col < 0)
1056                                 {
1057                                 if (new_row > 0)
1058                                         {
1059                                         new_row--;
1060                                         new_col = VFICON(vf)->columns - 1;
1061                                         }
1062                                 else
1063                                         {
1064                                         new_col = 0;
1065                                         }
1066                                 }
1067                         if (new_col >= VFICON(vf)->columns)
1068                                 {
1069                                 if (new_row < VFICON(vf)->rows - 1)
1070                                         {
1071                                         new_row++;
1072                                         new_col = 0;
1073                                         }
1074                                 else
1075                                         {
1076                                         new_col = VFICON(vf)->columns - 1;
1077                                         }
1078                                 }
1079                         }
1080                 }
1081         else
1082                 {
1083                 new_row = row;
1084                 new_col = col;
1085
1086                 if (new_row >= VFICON(vf)->rows)
1087                         {
1088                         if (VFICON(vf)->rows > 0)
1089                                 new_row = VFICON(vf)->rows - 1;
1090                         else
1091                                 new_row = 0;
1092                         new_col = VFICON(vf)->columns - 1;
1093                         }
1094                 if (new_col >= VFICON(vf)->columns) new_col = VFICON(vf)->columns - 1;
1095                 }
1096
1097         if (new_row == VFICON(vf)->rows - 1)
1098                 {
1099                 gint l;
1100
1101                 /* if we moved beyond the last image, go to the last image */
1102
1103                 l = g_list_length(vf->list);
1104                 if (VFICON(vf)->rows > 1) l -= (VFICON(vf)->rows - 1) * VFICON(vf)->columns;
1105                 if (new_col >= l) new_col = l - 1;
1106                 }
1107
1108         vficon_set_focus(vf, vficon_find_data(vf, new_row, new_col, nullptr));
1109 }
1110
1111 static void vficon_set_focus(ViewFile *vf, FileData *fd)
1112 {
1113         GtkTreeIter iter;
1114         gint row;
1115         gint col;
1116
1117         if (g_list_find(vf->list, VFICON(vf)->focus_fd))
1118                 {
1119                 if (fd == VFICON(vf)->focus_fd)
1120                         {
1121                         /* ensure focus row col are correct */
1122                         vficon_find_position(vf, VFICON(vf)->focus_fd, &VFICON(vf)->focus_row, &VFICON(vf)->focus_column);
1123
1124 /** @FIXME Refer to issue #467 on Github. The thumbnail position is not
1125  * preserved when the icon view is refreshed. Caused by an unknown call from
1126  * the idle loop. This patch hides the problem.
1127  */
1128                         if (vficon_find_iter(vf, VFICON(vf)->focus_fd, &iter, nullptr))
1129                                 {
1130                                 tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1131                                 }
1132
1133                         return;
1134                         }
1135                 vficon_selection_remove(vf, VFICON(vf)->focus_fd, SELECTION_FOCUS, nullptr);
1136                 }
1137
1138         if (!vficon_find_position(vf, fd, &row, &col))
1139                 {
1140                 VFICON(vf)->focus_fd = nullptr;
1141                 VFICON(vf)->focus_row = -1;
1142                 VFICON(vf)->focus_column = -1;
1143                 return;
1144                 }
1145
1146         VFICON(vf)->focus_fd = fd;
1147         VFICON(vf)->focus_row = row;
1148         VFICON(vf)->focus_column = col;
1149         vficon_selection_add(vf, VFICON(vf)->focus_fd, SELECTION_FOCUS, nullptr);
1150
1151         if (vficon_find_iter(vf, VFICON(vf)->focus_fd, &iter, nullptr))
1152                 {
1153                 GtkTreePath *tpath;
1154                 GtkTreeViewColumn *column;
1155                 GtkTreeModel *store;
1156
1157                 tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1158
1159                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1160                 tpath = gtk_tree_model_get_path(store, &iter);
1161                 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1162                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), VFICON_MAX_COLUMNS);
1163                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vf->listview), tpath, column, FALSE);
1164                 gtk_tree_path_free(tpath);
1165                 }
1166 }
1167
1168 /* used to figure the page up/down distances */
1169 static gint page_height(ViewFile *vf)
1170 {
1171         GtkAdjustment *adj;
1172         gint page_size;
1173         gint row_height;
1174         gint ret;
1175
1176         adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vf->listview));
1177         page_size = static_cast<gint>(gtk_adjustment_get_page_increment(adj));
1178
1179         row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1180         if (VFICON(vf)->show_text) row_height += options->thumbnails.max_height / 3;
1181
1182         ret = page_size / row_height;
1183         if (ret < 1) ret = 1;
1184
1185         return ret;
1186 }
1187
1188 /*
1189  *-------------------------------------------------------------------
1190  * keyboard
1191  *-------------------------------------------------------------------
1192  */
1193
1194 gboolean vficon_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1195 {
1196         auto vf = static_cast<ViewFile *>(data);
1197         gint focus_row = 0;
1198         gint focus_col = 0;
1199         FileData *fd;
1200         gboolean stop_signal;
1201
1202         stop_signal = TRUE;
1203         switch (event->keyval)
1204                 {
1205                 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1206                         focus_col = -1;
1207                         break;
1208                 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1209                         focus_col = 1;
1210                         break;
1211                 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1212                         focus_row = -1;
1213                         break;
1214                 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1215                         focus_row = 1;
1216                         break;
1217                 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1218                         focus_row = -page_height(vf);
1219                         break;
1220                 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1221                         focus_row = page_height(vf);
1222                         break;
1223                 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1224                         focus_row = -VFICON(vf)->focus_row;
1225                         focus_col = -VFICON(vf)->focus_column;
1226                         break;
1227                 case GDK_KEY_End: case GDK_KEY_KP_End:
1228                         focus_row = VFICON(vf)->rows - 1 - VFICON(vf)->focus_row;
1229                         focus_col = VFICON(vf)->columns - 1 - VFICON(vf)->focus_column;
1230                         break;
1231                 case GDK_KEY_space:
1232                         fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1233                         if (fd)
1234                                 {
1235                                 VFICON(vf)->click_fd = fd;
1236                                 if (event->state & GDK_CONTROL_MASK)
1237                                         {
1238                                         gint selected;
1239
1240                                         selected = fd->selected & SELECTION_SELECTED;
1241                                         if (selected)
1242                                                 {
1243                                                 vficon_unselect(vf, fd);
1244                                                 }
1245                                         else
1246                                                 {
1247                                                 vficon_select(vf, fd);
1248                                                 vficon_send_layout_select(vf, fd);
1249                                                 }
1250                                         }
1251                                 else
1252                                         {
1253                                         vf_select_none(vf);
1254                                         vficon_select(vf, fd);
1255                                         vficon_send_layout_select(vf, fd);
1256                                         }
1257                                 }
1258                         break;
1259                 case GDK_KEY_Menu:
1260                         fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1261                         VFICON(vf)->click_fd = fd;
1262
1263                         vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
1264                         tip_unschedule(vf);
1265
1266                         vf->popup = vf_pop_menu(vf);
1267                         gtk_menu_popup_at_widget(GTK_MENU(vf->popup), widget, GDK_GRAVITY_EAST, GDK_GRAVITY_CENTER, nullptr);
1268                         break;
1269                 default:
1270                         stop_signal = FALSE;
1271                         break;
1272                 }
1273
1274         if (focus_row != 0 || focus_col != 0)
1275                 {
1276                 FileData *new_fd;
1277                 FileData *old_fd;
1278
1279                 old_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1280                 vficon_move_focus(vf, focus_row, focus_col, TRUE);
1281                 new_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1282
1283                 if (new_fd != old_fd)
1284                         {
1285                         if (event->state & GDK_SHIFT_MASK)
1286                                 {
1287                                 if (!options->collections.rectangular_selection)
1288                                         {
1289                                         vficon_select_region_util(vf, old_fd, new_fd, FALSE);
1290                                         }
1291                                 else
1292                                         {
1293                                         vficon_select_region_util(vf, VFICON(vf)->click_fd, old_fd, FALSE);
1294                                         }
1295                                 vficon_select_region_util(vf, VFICON(vf)->click_fd, new_fd, TRUE);
1296                                 vficon_send_layout_select(vf, new_fd);
1297                                 }
1298                         else if (event->state & GDK_CONTROL_MASK)
1299                                 {
1300                                 VFICON(vf)->click_fd = new_fd;
1301                                 }
1302                         else
1303                                 {
1304                                 VFICON(vf)->click_fd = new_fd;
1305                                 vf_select_none(vf);
1306                                 vficon_select(vf, new_fd);
1307                                 vficon_send_layout_select(vf, new_fd);
1308                                 }
1309                         }
1310                 }
1311
1312         if (stop_signal)
1313                 {
1314                 tip_unschedule(vf);
1315                 }
1316
1317         return stop_signal;
1318 }
1319
1320 /*
1321  *-------------------------------------------------------------------
1322  * mouse
1323  *-------------------------------------------------------------------
1324  */
1325
1326 static gboolean vficon_motion_cb(GtkWidget *, GdkEventMotion *event, gpointer data)
1327 {
1328         auto vf = static_cast<ViewFile *>(data);
1329         FileData *fd;
1330
1331         fd = vficon_find_data_by_coord(vf, static_cast<gint>(event->x), static_cast<gint>(event->y), nullptr);
1332         tip_update(vf, fd);
1333
1334         return FALSE;
1335 }
1336
1337 gboolean vficon_press_cb(GtkWidget *, GdkEventButton *bevent, gpointer data)
1338 {
1339         auto vf = static_cast<ViewFile *>(data);
1340         GtkTreeIter iter;
1341         FileData *fd;
1342
1343         tip_unschedule(vf);
1344
1345         fd = vficon_find_data_by_coord(vf, static_cast<gint>(bevent->x), static_cast<gint>(bevent->y), &iter);
1346
1347         if (fd)
1348                 {
1349                 VFICON(vf)->click_fd = fd;
1350                 vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1351
1352                 switch (bevent->button)
1353                         {
1354                         case MOUSE_BUTTON_LEFT:
1355                                 if (!gtk_widget_has_focus(vf->listview))
1356                                         {
1357                                         gtk_widget_grab_focus(vf->listview);
1358                                         }
1359
1360                                 if (bevent->type == GDK_2BUTTON_PRESS && vf->layout)
1361                                         {
1362                                         if (VFICON(vf)->click_fd->format_class == FORMAT_CLASS_COLLECTION)
1363                                                 {
1364                                                 collection_window_new(VFICON(vf)->click_fd->path);
1365                                                 }
1366                                         else
1367                                                 {
1368                                                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1369                                                 layout_image_full_screen_start(vf->layout);
1370                                                 }
1371                                         }
1372                                 break;
1373                         case MOUSE_BUTTON_RIGHT:
1374                                 vf->popup = vf_pop_menu(vf);
1375                                 gtk_menu_popup_at_pointer(GTK_MENU(vf->popup), nullptr);
1376                                 break;
1377                         default:
1378                                 break;
1379                         }
1380                 }
1381
1382         return FALSE;
1383 }
1384
1385 gboolean vficon_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1386 {
1387         auto vf = static_cast<ViewFile *>(data);
1388         GtkTreeIter iter;
1389         FileData *fd = nullptr;
1390         gboolean was_selected;
1391
1392         tip_schedule(vf);
1393
1394         if (defined_mouse_buttons(widget, bevent, vf->layout))
1395                 {
1396                 return TRUE;
1397                 }
1398
1399         if (static_cast<gint>(bevent->x) != 0 || static_cast<gint>(bevent->y) != 0)
1400                 {
1401                 fd = vficon_find_data_by_coord(vf, static_cast<gint>(bevent->x), static_cast<gint>(bevent->y), &iter);
1402                 }
1403
1404         if (VFICON(vf)->click_fd)
1405                 {
1406                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
1407                 }
1408
1409         if (!fd || VFICON(vf)->click_fd != fd) return TRUE;
1410
1411         was_selected = !!(fd->selected & SELECTION_SELECTED);
1412
1413         switch (bevent->button)
1414                 {
1415                 case MOUSE_BUTTON_LEFT:
1416                         {
1417                         vficon_set_focus(vf, fd);
1418
1419                         if (bevent->state & GDK_CONTROL_MASK)
1420                                 {
1421                                 gboolean select;
1422
1423                                 select = !(fd->selected & SELECTION_SELECTED);
1424                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1425                                         {
1426                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, select);
1427                                         }
1428                                 else
1429                                         {
1430                                         vficon_select_util(vf, fd, select);
1431                                         }
1432                                 }
1433                         else
1434                                 {
1435                                 vf_select_none(vf);
1436
1437                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1438                                         {
1439                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, TRUE);
1440                                         }
1441                                 else
1442                                         {
1443                                         vficon_select_util(vf, fd, TRUE);
1444                                         was_selected = FALSE;
1445                                         }
1446                                 }
1447                         }
1448                         break;
1449                 case MOUSE_BUTTON_MIDDLE:
1450                         {
1451                         vficon_select_util(vf, fd, !(fd->selected & SELECTION_SELECTED));
1452                         }
1453                         break;
1454                 default:
1455                         break;
1456                 }
1457
1458         if (!was_selected && (fd->selected & SELECTION_SELECTED))
1459                 {
1460                 vficon_send_layout_select(vf, fd);
1461                 }
1462
1463         return TRUE;
1464 }
1465
1466 static gboolean vficon_leave_cb(GtkWidget *, GdkEventCrossing *, gpointer data)
1467 {
1468         auto vf = static_cast<ViewFile *>(data);
1469
1470         tip_unschedule(vf);
1471         return FALSE;
1472 }
1473
1474 /*
1475  *-------------------------------------------------------------------
1476  * population
1477  *-------------------------------------------------------------------
1478  */
1479
1480 static gboolean vficon_destroy_node_cb(GtkTreeModel *store, GtkTreePath *, GtkTreeIter *iter, gpointer)
1481 {
1482         GList *list;
1483
1484         gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
1485
1486         /* it seems that gtk_list_store_clear may call some callbacks
1487            that use the column. Set the pointer to NULL to be safe. */
1488         gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, NULL, -1);
1489         g_list_free(list);
1490
1491         return FALSE;
1492 }
1493
1494 static void vficon_clear_store(ViewFile *vf)
1495 {
1496         GtkTreeModel *store;
1497
1498         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1499         gtk_tree_model_foreach(store, vficon_destroy_node_cb, nullptr);
1500
1501         gtk_list_store_clear(GTK_LIST_STORE(store));
1502 }
1503
1504 static GList *vficon_add_row(ViewFile *vf, GtkTreeIter *iter)
1505 {
1506         GtkListStore *store;
1507         GList *list = nullptr;
1508         gint i;
1509
1510         for (i = 0; i < VFICON(vf)->columns; i++) list = g_list_prepend(list, nullptr);
1511
1512         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview)));
1513         gtk_list_store_append(store, iter);
1514         gtk_list_store_set(store, iter, FILE_COLUMN_POINTER, list, -1);
1515
1516         return list;
1517 }
1518
1519 static void vficon_populate(ViewFile *vf, gboolean resize, gboolean keep_position)
1520 {
1521         GtkTreeModel *store;
1522         GtkTreePath *tpath;
1523         GList *work;
1524         FileData *visible_fd = nullptr;
1525         gint r;
1526         gboolean valid;
1527         GtkTreeIter iter;
1528
1529         vficon_verify_selections(vf);
1530
1531         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1532
1533         if (keep_position && gtk_widget_get_realized(vf->listview) &&
1534             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1535                 {
1536                 GtkTreeIter iter;
1537                 GList *list;
1538
1539                 gtk_tree_model_get_iter(store, &iter, tpath);
1540                 gtk_tree_path_free(tpath);
1541
1542                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1543                 if (list) visible_fd = static_cast<FileData *>(list->data);
1544                 }
1545
1546
1547         if (resize)
1548                 {
1549                 gint i;
1550                 gint thumb_width;
1551
1552                 vficon_clear_store(vf);
1553
1554                 thumb_width = vficon_get_icon_width(vf);
1555
1556                 for (i = 0; i < VFICON_MAX_COLUMNS; i++)
1557                         {
1558                         GtkTreeViewColumn *column;
1559                         GtkCellRenderer *cell;
1560                         GList *list;
1561
1562                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), i);
1563                         gtk_tree_view_column_set_visible(column, (i < VFICON(vf)->columns));
1564                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1565
1566                         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1567                         cell = static_cast<GtkCellRenderer *>((list) ? list->data : nullptr);
1568                         g_list_free(list);
1569
1570                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1571                                 {
1572                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1573                                                              "fixed_height", options->thumbnails.max_height,
1574                                                              "show_text", VFICON(vf)->show_text || options->show_star_rating,
1575                                                              "show_marks", vf->marks_enabled,
1576                                                              "num_marks", FILEDATA_MARKS_SIZE,
1577                                                              NULL);
1578                                 }
1579                         }
1580                 if (gtk_widget_get_realized(vf->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(vf->listview));
1581                 }
1582
1583         r = -1;
1584
1585         valid = gtk_tree_model_iter_children(store, &iter, nullptr);
1586
1587         work = vf->list;
1588         while (work)
1589                 {
1590                 GList *list;
1591                 r++;
1592                 if (valid)
1593                         {
1594                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1595                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1596                         }
1597                 else
1598                         {
1599                         list = vficon_add_row(vf, &iter);
1600                         }
1601
1602                 while (list)
1603                         {
1604                         FileData *fd;
1605
1606                         if (work)
1607                                 {
1608                                 fd = static_cast<FileData *>(work->data);
1609                                 work = work->next;
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 static void vficon_cell_data_cb(GtkTreeViewColumn *, GtkCellRenderer *cell,
2077                                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2078 {
2079         auto cd = static_cast<ColumnData *>(data);
2080         FileData *fd;
2081         gchar *star_rating;
2082         GList *list;
2083         ViewFile *vf = cd->vf;
2084
2085         if (!GQV_IS_CELL_RENDERER_ICON(cell)) return;
2086
2087         gtk_tree_model_get(tree_model, iter, FILE_COLUMN_POINTER, &list, -1);
2088
2089         fd = static_cast<FileData *>(g_list_nth_data(list, cd->number));
2090
2091         if (fd)
2092                 {
2093                 const gchar *link;
2094                 gchar *name_sidecars = nullptr;
2095                 GdkRGBA color_bg;
2096                 GdkRGBA color_fg;
2097                 GtkStateType state = GTK_STATE_NORMAL;
2098                 GtkStyle *style;
2099
2100                 g_assert(fd->magick == FD_MAGICK);
2101
2102                 if (options->show_star_rating && fd->rating != STAR_RATING_NOT_READ)
2103                         {
2104                         star_rating = convert_rating_to_stars(fd->rating);
2105                         }
2106                 else
2107                         {
2108                         star_rating = nullptr;
2109                         }
2110
2111                 link = islink(fd->path) ? GQ_LINK_STR : "";
2112                 if (fd->sidecar_files)
2113                         {
2114                         gchar *sidecars = file_data_sc_list_to_string(fd);
2115                         if (options->show_star_rating && VFICON(vf)->show_text)
2116                                 {
2117                                 name_sidecars = g_strdup_printf("%s%s %s\n%s", link, fd->name, sidecars, star_rating);
2118                                 }
2119                         else if (options->show_star_rating)
2120                                 {
2121                                 name_sidecars = g_strdup_printf("%s", star_rating);
2122                                 }
2123                         else if (VFICON(vf)->show_text)
2124                                 {
2125                                 name_sidecars = g_strdup_printf("%s%s %s", link, fd->name, sidecars);
2126                                 }
2127                         g_free(sidecars);
2128                         }
2129                 else
2130                         {
2131                         const gchar *disabled_grouping = fd->disable_grouping ? _(" [NO GROUPING]") : "";
2132                         if (options->show_star_rating && VFICON(vf)->show_text)
2133                                 {
2134                                 name_sidecars = g_strdup_printf("%s%s%s\n%s", link, fd->name, disabled_grouping, star_rating);
2135                                 }
2136                         else if (options->show_star_rating)
2137                                 {
2138                                 name_sidecars = g_strdup_printf("%s", star_rating);
2139                                 }
2140                         else if (VFICON(vf)->show_text)
2141                                 {
2142                                 name_sidecars = g_strdup_printf("%s%s%s", link, fd->name, disabled_grouping);
2143                                 }
2144                         }
2145                 g_free(star_rating);
2146
2147                 style = gtk_widget_get_style(vf->listview);
2148                 if (fd->selected & SELECTION_SELECTED)
2149                         {
2150                         state = GTK_STATE_SELECTED;
2151                         }
2152
2153                 convert_gdkcolor_to_gdkrgba(&style->text[state], &color_fg);
2154                 convert_gdkcolor_to_gdkrgba(&style->base[state], &color_bg);
2155
2156                 if (fd->selected & SELECTION_PRELIGHT)
2157                         {
2158                         shift_color(&color_bg, -1, 0);
2159                         }
2160
2161                 g_object_set(cell, "pixbuf", fd->thumb_pixbuf,
2162                                         "text", name_sidecars,
2163                                         "marks", file_data_get_marks(fd),
2164                                         "show_marks", vf->marks_enabled,
2165                                         "cell-background-rgba", &color_bg,
2166                                         "cell-background-set", TRUE,
2167                                         "foreground-rgba", &color_fg,
2168                                         "foreground-set", TRUE,
2169                                         "has-focus", (VFICON(vf)->focus_fd == fd), NULL);
2170                 g_free(name_sidecars);
2171                 }
2172         else
2173                 {
2174                 g_object_set(cell,      "pixbuf", NULL,
2175                                         "text", NULL,
2176                                         "show_marks", FALSE,
2177                                         "cell-background-set", FALSE,
2178                                         "foreground-set", FALSE,
2179                                         "has-focus", FALSE, NULL);
2180                 }
2181 }
2182
2183 static void vficon_append_column(ViewFile *vf, gint n)
2184 {
2185         GtkTreeViewColumn *column;
2186         GtkCellRenderer *renderer;
2187
2188         column = gtk_tree_view_column_new();
2189         gtk_tree_view_column_set_min_width(column, 0);
2190
2191         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2192         gtk_tree_view_column_set_alignment(column, 0.5);
2193
2194         renderer = gqv_cell_renderer_icon_new();
2195         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2196         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2197                                          "ypad", THUMB_BORDER_PADDING,
2198                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2199
2200         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2201         g_object_set_data(G_OBJECT(renderer), "column_number", GINT_TO_POINTER(n));
2202
2203         auto cd = g_new0(ColumnData, 1);
2204         cd->vf = vf;
2205         cd->number = n;
2206         gtk_tree_view_column_set_cell_data_func(column, renderer, vficon_cell_data_cb, cd, g_free);
2207
2208         gtk_tree_view_append_column(GTK_TREE_VIEW(vf->listview), column);
2209
2210         g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(vficon_mark_toggled_cb), vf);
2211 }
2212
2213 /*
2214  *-----------------------------------------------------------------------------
2215  * base
2216  *-----------------------------------------------------------------------------
2217  */
2218
2219 gboolean vficon_set_fd(ViewFile *vf, FileData *dir_fd)
2220 {
2221         gboolean ret;
2222
2223         if (!dir_fd) return FALSE;
2224         if (vf->dir_fd == dir_fd) return TRUE;
2225
2226         file_data_unref(vf->dir_fd);
2227         vf->dir_fd = file_data_ref(dir_fd);
2228
2229         g_list_free(VFICON(vf)->selection);
2230         VFICON(vf)->selection = nullptr;
2231
2232         g_list_free(vf->list);
2233         vf->list = nullptr;
2234
2235         /* NOTE: populate will clear the store for us */
2236         ret = vficon_refresh_real(vf, FALSE);
2237
2238         VFICON(vf)->focus_fd = nullptr;
2239         vficon_move_focus(vf, 0, 0, FALSE);
2240
2241         return ret;
2242 }
2243
2244 void vficon_destroy_cb(GtkWidget *, gpointer data)
2245 {
2246         auto vf = static_cast<ViewFile *>(data);
2247
2248         vf_refresh_idle_cancel(vf);
2249
2250         file_data_unregister_notify_func(vf_notify_cb, vf);
2251
2252         tip_unschedule(vf);
2253
2254         vf_thumb_cleanup(vf);
2255         vf_star_cleanup(vf);
2256
2257         g_list_free(vf->list);
2258         g_list_free(VFICON(vf)->selection);
2259 }
2260
2261 ViewFile *vficon_new(ViewFile *vf, FileData *)
2262 {
2263         GtkListStore *store;
2264         GtkTreeSelection *selection;
2265         gint i;
2266
2267         vf->info = g_new0(ViewFileInfoIcon, 1);
2268
2269         VFICON(vf)->show_text = options->show_icon_names;
2270
2271         store = gtk_list_store_new(1, G_TYPE_POINTER);
2272         vf->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2273         g_object_unref(store);
2274
2275         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vf->listview));
2276         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2277
2278         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vf->listview), FALSE);
2279         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vf->listview), FALSE);
2280
2281         for (i = 0; i < VFICON_MAX_COLUMNS; i++)
2282                 {
2283                 vficon_append_column(vf, i);
2284                 }
2285
2286         /* zero width column to hide tree view focus, we draw it ourselves */
2287         vficon_append_column(vf, i);
2288         /* end column to fill white space */
2289         vficon_append_column(vf, i);
2290
2291         g_signal_connect(G_OBJECT(vf->listview), "size_allocate",
2292                          G_CALLBACK(vficon_sized_cb), vf);
2293
2294         gtk_widget_set_events(vf->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2295                               static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK));
2296
2297         g_signal_connect(G_OBJECT(vf->listview),"motion_notify_event",
2298                          G_CALLBACK(vficon_motion_cb), vf);
2299         g_signal_connect(G_OBJECT(vf->listview), "leave_notify_event",
2300                          G_CALLBACK(vficon_leave_cb), vf);
2301
2302         /* force VFICON(vf)->columns to be at least 1 (sane) - this will be corrected in the size_cb */
2303         vficon_populate_at_new_size(vf, 1, 1, FALSE);
2304
2305         file_data_register_notify_func(vf_notify_cb, vf, NOTIFY_PRIORITY_MEDIUM);
2306
2307         return vf;
2308 }
2309
2310 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */