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