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