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