a9a5c8ba5d92787be7605593c0079c8fc997e559
[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         gtk_container_add(GTK_CONTAINER(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         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) gtk_widget_destroy(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                 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         GList *work, *work2;
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
853                 list = g_list_prepend(list, file_data_ref(fd));
854
855                 work2 = fd->sidecar_files;
856                 while (work2)
857                         {
858                         fd = static_cast<FileData *>(work2->data);
859                         list = g_list_prepend(list, file_data_ref(fd));
860                         work2 = work2->next;
861                         }
862
863                 work = work->next;
864                 }
865
866         list = g_list_reverse(list);
867
868         return list;
869 }
870
871 GList *vficon_selection_get_list_by_index(ViewFile *vf)
872 {
873         GList *list = nullptr;
874         GList *work;
875
876         work = VFICON(vf)->selection;
877         while (work)
878                 {
879                 list = g_list_prepend(list, GINT_TO_POINTER(g_list_index(vf->list, work->data)));
880                 work = work->next;
881                 }
882
883         return g_list_reverse(list);
884 }
885
886 void vficon_select_by_fd(ViewFile *vf, FileData *fd)
887 {
888         if (!fd) return;
889         if (!g_list_find(vf->list, fd)) return;
890
891         if (!(fd->selected & SELECTION_SELECTED))
892                 {
893                 vf_select_none(vf);
894                 vficon_select(vf, fd);
895                 }
896
897         vficon_set_focus(vf, fd);
898 }
899
900 void vficon_select_list(ViewFile *vf, GList *list)
901 {
902         GList *work;
903         FileData *fd;
904
905         if (!list) return;
906
907         work = list;
908         while (work)
909                 {
910                 fd = static_cast<FileData *>(work->data);
911                 if (g_list_find(vf->list, fd))
912                         {
913                         VFICON(vf)->selection = g_list_append(VFICON(vf)->selection, fd);
914                         vficon_selection_add(vf, fd, SELECTION_SELECTED, nullptr);
915                         }
916                 work = work->next;
917                 }
918 }
919
920 void vficon_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
921 {
922         GList *work;
923         gint n = mark - 1;
924
925         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
926
927         work = vf->list;
928         while (work)
929                 {
930                 auto fd = static_cast<FileData *>(work->data);
931                 gboolean mark_val, selected;
932
933                 g_assert(fd->magick == FD_MAGICK);
934
935                 mark_val = file_data_get_mark(fd, n);
936                 selected = fd->selected & SELECTION_SELECTED;
937
938                 switch (mode)
939                         {
940                         case MTS_MODE_SET: selected = mark_val;
941                                 break;
942                         case MTS_MODE_OR: selected = mark_val || selected;
943                                 break;
944                         case MTS_MODE_AND: selected = mark_val && selected;
945                                 break;
946                         case MTS_MODE_MINUS: selected = !mark_val && selected;
947                                 break;
948                         }
949
950                 vficon_select_util(vf, fd, selected);
951
952                 work = work->next;
953                 }
954 }
955
956 void vficon_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
957 {
958         GList *slist;
959         GList *work;
960         gint n = mark -1;
961
962         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
963
964         slist = vf_selection_get_list(vf);
965         work = slist;
966         while (work)
967                 {
968                 auto fd = static_cast<FileData *>(work->data);
969
970                 switch (mode)
971                         {
972                         case STM_MODE_SET: file_data_set_mark(fd, n, 1);
973                                 break;
974                         case STM_MODE_RESET: file_data_set_mark(fd, n, 0);
975                                 break;
976                         case STM_MODE_TOGGLE: file_data_set_mark(fd, n, !file_data_get_mark(fd, n));
977                                 break;
978                         }
979                 work = work->next;
980                 }
981         filelist_free(slist);
982 }
983
984 static void vficon_select_closest(ViewFile *vf, FileData *sel_fd)
985 {
986         GList *work;
987         FileData *fd = nullptr;
988
989         if (sel_fd->parent) sel_fd = sel_fd->parent;
990         work = vf->list;
991
992         while (work)
993                 {
994                 gint match;
995
996                 fd = static_cast<FileData *>(work->data);
997                 work = work->next;
998
999                 match = filelist_sort_compare_filedata_full(fd, sel_fd, vf->sort_method, vf->sort_ascend);
1000
1001                 if (match >= 0) break;
1002                 }
1003
1004         if (fd)
1005                 {
1006                 vficon_select(vf, fd);
1007                 vficon_send_layout_select(vf, fd);
1008                 }
1009 }
1010
1011
1012 /*
1013  *-------------------------------------------------------------------
1014  * focus
1015  *-------------------------------------------------------------------
1016  */
1017
1018 static void vficon_move_focus(ViewFile *vf, gint row, gint col, gboolean relative)
1019 {
1020         gint new_row;
1021         gint new_col;
1022
1023         if (relative)
1024                 {
1025                 new_row = VFICON(vf)->focus_row;
1026                 new_col = VFICON(vf)->focus_column;
1027
1028                 new_row += row;
1029                 if (new_row < 0) new_row = 0;
1030                 if (new_row >= VFICON(vf)->rows) new_row = VFICON(vf)->rows - 1;
1031
1032                 while (col != 0)
1033                         {
1034                         if (col < 0)
1035                                 {
1036                                 new_col--;
1037                                 col++;
1038                                 }
1039                         else
1040                                 {
1041                                 new_col++;
1042                                 col--;
1043                                 }
1044
1045                         if (new_col < 0)
1046                                 {
1047                                 if (new_row > 0)
1048                                         {
1049                                         new_row--;
1050                                         new_col = VFICON(vf)->columns - 1;
1051                                         }
1052                                 else
1053                                         {
1054                                         new_col = 0;
1055                                         }
1056                                 }
1057                         if (new_col >= VFICON(vf)->columns)
1058                                 {
1059                                 if (new_row < VFICON(vf)->rows - 1)
1060                                         {
1061                                         new_row++;
1062                                         new_col = 0;
1063                                         }
1064                                 else
1065                                         {
1066                                         new_col = VFICON(vf)->columns - 1;
1067                                         }
1068                                 }
1069                         }
1070                 }
1071         else
1072                 {
1073                 new_row = row;
1074                 new_col = col;
1075
1076                 if (new_row >= VFICON(vf)->rows)
1077                         {
1078                         if (VFICON(vf)->rows > 0)
1079                                 new_row = VFICON(vf)->rows - 1;
1080                         else
1081                                 new_row = 0;
1082                         new_col = VFICON(vf)->columns - 1;
1083                         }
1084                 if (new_col >= VFICON(vf)->columns) new_col = VFICON(vf)->columns - 1;
1085                 }
1086
1087         if (new_row == VFICON(vf)->rows - 1)
1088                 {
1089                 gint l;
1090
1091                 /* if we moved beyond the last image, go to the last image */
1092
1093                 l = g_list_length(vf->list);
1094                 if (VFICON(vf)->rows > 1) l -= (VFICON(vf)->rows - 1) * VFICON(vf)->columns;
1095                 if (new_col >= l) new_col = l - 1;
1096                 }
1097
1098         vficon_set_focus(vf, vficon_find_data(vf, new_row, new_col, nullptr));
1099 }
1100
1101 static void vficon_set_focus(ViewFile *vf, FileData *fd)
1102 {
1103         GtkTreeIter iter;
1104         gint row, col;
1105
1106         if (g_list_find(vf->list, VFICON(vf)->focus_fd))
1107                 {
1108                 if (fd == VFICON(vf)->focus_fd)
1109                         {
1110                         /* ensure focus row col are correct */
1111                         vficon_find_position(vf, VFICON(vf)->focus_fd, &VFICON(vf)->focus_row, &VFICON(vf)->focus_column);
1112
1113 /** @FIXME Refer to issue #467 on Github. The thumbnail position is not
1114  * preserved when the icon view is refreshed. Caused by an unknown call from
1115  * the idle loop. This patch hides the problem.
1116  */
1117                         if (vficon_find_iter(vf, VFICON(vf)->focus_fd, &iter, nullptr))
1118                                 {
1119                                 tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1120                                 }
1121
1122                         return;
1123                         }
1124                 vficon_selection_remove(vf, VFICON(vf)->focus_fd, SELECTION_FOCUS, nullptr);
1125                 }
1126
1127         if (!vficon_find_position(vf, fd, &row, &col))
1128                 {
1129                 VFICON(vf)->focus_fd = nullptr;
1130                 VFICON(vf)->focus_row = -1;
1131                 VFICON(vf)->focus_column = -1;
1132                 return;
1133                 }
1134
1135         VFICON(vf)->focus_fd = fd;
1136         VFICON(vf)->focus_row = row;
1137         VFICON(vf)->focus_column = col;
1138         vficon_selection_add(vf, VFICON(vf)->focus_fd, SELECTION_FOCUS, nullptr);
1139
1140         if (vficon_find_iter(vf, VFICON(vf)->focus_fd, &iter, nullptr))
1141                 {
1142                 GtkTreePath *tpath;
1143                 GtkTreeViewColumn *column;
1144                 GtkTreeModel *store;
1145
1146                 tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1147
1148                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1149                 tpath = gtk_tree_model_get_path(store, &iter);
1150                 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1151                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), VFICON_MAX_COLUMNS);
1152                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vf->listview), tpath, column, FALSE);
1153                 gtk_tree_path_free(tpath);
1154                 }
1155 }
1156
1157 /* used to figure the page up/down distances */
1158 static gint page_height(ViewFile *vf)
1159 {
1160         GtkAdjustment *adj;
1161         gint page_size;
1162         gint row_height;
1163         gint ret;
1164
1165         adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vf->listview));
1166         page_size = static_cast<gint>(gtk_adjustment_get_page_increment(adj));
1167
1168         row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1169         if (VFICON(vf)->show_text) row_height += options->thumbnails.max_height / 3;
1170
1171         ret = page_size / row_height;
1172         if (ret < 1) ret = 1;
1173
1174         return ret;
1175 }
1176
1177 /*
1178  *-------------------------------------------------------------------
1179  * keyboard
1180  *-------------------------------------------------------------------
1181  */
1182
1183 gboolean vficon_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1184 {
1185         auto vf = static_cast<ViewFile *>(data);
1186         gint focus_row = 0;
1187         gint focus_col = 0;
1188         FileData *fd;
1189         gboolean stop_signal;
1190
1191         stop_signal = TRUE;
1192         switch (event->keyval)
1193                 {
1194                 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1195                         focus_col = -1;
1196                         break;
1197                 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1198                         focus_col = 1;
1199                         break;
1200                 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1201                         focus_row = -1;
1202                         break;
1203                 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1204                         focus_row = 1;
1205                         break;
1206                 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1207                         focus_row = -page_height(vf);
1208                         break;
1209                 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1210                         focus_row = page_height(vf);
1211                         break;
1212                 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1213                         focus_row = -VFICON(vf)->focus_row;
1214                         focus_col = -VFICON(vf)->focus_column;
1215                         break;
1216                 case GDK_KEY_End: case GDK_KEY_KP_End:
1217                         focus_row = VFICON(vf)->rows - 1 - VFICON(vf)->focus_row;
1218                         focus_col = VFICON(vf)->columns - 1 - VFICON(vf)->focus_column;
1219                         break;
1220                 case GDK_KEY_space:
1221                         fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1222                         if (fd)
1223                                 {
1224                                 VFICON(vf)->click_fd = fd;
1225                                 if (event->state & GDK_CONTROL_MASK)
1226                                         {
1227                                         gint selected;
1228
1229                                         selected = fd->selected & SELECTION_SELECTED;
1230                                         if (selected)
1231                                                 {
1232                                                 vficon_unselect(vf, fd);
1233                                                 }
1234                                         else
1235                                                 {
1236                                                 vficon_select(vf, fd);
1237                                                 vficon_send_layout_select(vf, fd);
1238                                                 }
1239                                         }
1240                                 else
1241                                         {
1242                                         vf_select_none(vf);
1243                                         vficon_select(vf, fd);
1244                                         vficon_send_layout_select(vf, fd);
1245                                         }
1246                                 }
1247                         break;
1248                 case GDK_KEY_Menu:
1249                         fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1250                         VFICON(vf)->click_fd = fd;
1251
1252                         vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
1253                         tip_unschedule(vf);
1254
1255                         vf->popup = vf_pop_menu(vf);
1256                         gtk_menu_popup_at_widget(GTK_MENU(vf->popup), widget, GDK_GRAVITY_EAST, GDK_GRAVITY_CENTER, nullptr);
1257                         break;
1258                 default:
1259                         stop_signal = FALSE;
1260                         break;
1261                 }
1262
1263         if (focus_row != 0 || focus_col != 0)
1264                 {
1265                 FileData *new_fd;
1266                 FileData *old_fd;
1267
1268                 old_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1269                 vficon_move_focus(vf, focus_row, focus_col, TRUE);
1270                 new_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, nullptr);
1271
1272                 if (new_fd != old_fd)
1273                         {
1274                         if (event->state & GDK_SHIFT_MASK)
1275                                 {
1276                                 if (!options->collections.rectangular_selection)
1277                                         {
1278                                         vficon_select_region_util(vf, old_fd, new_fd, FALSE);
1279                                         }
1280                                 else
1281                                         {
1282                                         vficon_select_region_util(vf, VFICON(vf)->click_fd, old_fd, FALSE);
1283                                         }
1284                                 vficon_select_region_util(vf, VFICON(vf)->click_fd, new_fd, TRUE);
1285                                 vficon_send_layout_select(vf, new_fd);
1286                                 }
1287                         else if (event->state & GDK_CONTROL_MASK)
1288                                 {
1289                                 VFICON(vf)->click_fd = new_fd;
1290                                 }
1291                         else
1292                                 {
1293                                 VFICON(vf)->click_fd = new_fd;
1294                                 vf_select_none(vf);
1295                                 vficon_select(vf, new_fd);
1296                                 vficon_send_layout_select(vf, new_fd);
1297                                 }
1298                         }
1299                 }
1300
1301         if (stop_signal)
1302                 {
1303                 tip_unschedule(vf);
1304                 }
1305
1306         return stop_signal;
1307 }
1308
1309 /*
1310  *-------------------------------------------------------------------
1311  * mouse
1312  *-------------------------------------------------------------------
1313  */
1314
1315 static gboolean vficon_motion_cb(GtkWidget *, GdkEventMotion *event, gpointer data)
1316 {
1317         auto vf = static_cast<ViewFile *>(data);
1318         FileData *fd;
1319
1320         fd = vficon_find_data_by_coord(vf, static_cast<gint>(event->x), static_cast<gint>(event->y), nullptr);
1321         tip_update(vf, fd);
1322
1323         return FALSE;
1324 }
1325
1326 gboolean vficon_press_cb(GtkWidget *, GdkEventButton *bevent, gpointer data)
1327 {
1328         auto vf = static_cast<ViewFile *>(data);
1329         GtkTreeIter iter;
1330         FileData *fd;
1331
1332         tip_unschedule(vf);
1333
1334         fd = vficon_find_data_by_coord(vf, static_cast<gint>(bevent->x), static_cast<gint>(bevent->y), &iter);
1335
1336         VFICON(vf)->click_fd = fd;
1337         vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1338
1339         switch (bevent->button)
1340                 {
1341                 case MOUSE_BUTTON_LEFT:
1342                         if (!gtk_widget_has_focus(vf->listview))
1343                                 {
1344                                 gtk_widget_grab_focus(vf->listview);
1345                                 }
1346
1347                         if (bevent->type == GDK_2BUTTON_PRESS && vf->layout)
1348                                 {
1349                                 if (VFICON(vf)->click_fd->format_class == FORMAT_CLASS_COLLECTION)
1350                                         {
1351                                         collection_window_new(VFICON(vf)->click_fd->path);
1352                                         }
1353                                 else
1354                                         {
1355                                         vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1356                                         layout_image_full_screen_start(vf->layout);
1357                                         }
1358                                 }
1359                         break;
1360                 case MOUSE_BUTTON_RIGHT:
1361                         vf->popup = vf_pop_menu(vf);
1362                         gtk_menu_popup_at_pointer(GTK_MENU(vf->popup), nullptr);
1363                         break;
1364                 default:
1365                         break;
1366                 }
1367
1368         return FALSE;
1369 }
1370
1371 gboolean vficon_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1372 {
1373         auto vf = static_cast<ViewFile *>(data);
1374         GtkTreeIter iter;
1375         FileData *fd = nullptr;
1376         gboolean was_selected;
1377
1378         tip_schedule(vf);
1379
1380         if (defined_mouse_buttons(widget, bevent, vf->layout))
1381                 {
1382                 return TRUE;
1383                 }
1384
1385         if (static_cast<gint>(bevent->x) != 0 || static_cast<gint>(bevent->y) != 0)
1386                 {
1387                 fd = vficon_find_data_by_coord(vf, static_cast<gint>(bevent->x), static_cast<gint>(bevent->y), &iter);
1388                 }
1389
1390         if (VFICON(vf)->click_fd)
1391                 {
1392                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, nullptr);
1393                 }
1394
1395         if (!fd || VFICON(vf)->click_fd != fd) return TRUE;
1396
1397         was_selected = !!(fd->selected & SELECTION_SELECTED);
1398
1399         switch (bevent->button)
1400                 {
1401                 case MOUSE_BUTTON_LEFT:
1402                         {
1403                         vficon_set_focus(vf, fd);
1404
1405                         if (bevent->state & GDK_CONTROL_MASK)
1406                                 {
1407                                 gboolean select;
1408
1409                                 select = !(fd->selected & SELECTION_SELECTED);
1410                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1411                                         {
1412                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, select);
1413                                         }
1414                                 else
1415                                         {
1416                                         vficon_select_util(vf, fd, select);
1417                                         }
1418                                 }
1419                         else
1420                                 {
1421                                 vf_select_none(vf);
1422
1423                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1424                                         {
1425                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, TRUE);
1426                                         }
1427                                 else
1428                                         {
1429                                         vficon_select_util(vf, fd, TRUE);
1430                                         was_selected = FALSE;
1431                                         }
1432                                 }
1433                         }
1434                         break;
1435                 case MOUSE_BUTTON_MIDDLE:
1436                         {
1437                         vficon_select_util(vf, fd, !(fd->selected & SELECTION_SELECTED));
1438                         }
1439                         break;
1440                 default:
1441                         break;
1442                 }
1443
1444         if (!was_selected && (fd->selected & SELECTION_SELECTED))
1445                 {
1446                 vficon_send_layout_select(vf, fd);
1447                 }
1448
1449         return TRUE;
1450 }
1451
1452 static gboolean vficon_leave_cb(GtkWidget *, GdkEventCrossing *, gpointer data)
1453 {
1454         auto vf = static_cast<ViewFile *>(data);
1455
1456         tip_unschedule(vf);
1457         return FALSE;
1458 }
1459
1460 /*
1461  *-------------------------------------------------------------------
1462  * population
1463  *-------------------------------------------------------------------
1464  */
1465
1466 static gboolean vficon_destroy_node_cb(GtkTreeModel *store, GtkTreePath *, GtkTreeIter *iter, gpointer)
1467 {
1468         GList *list;
1469
1470         gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
1471
1472         /* it seems that gtk_list_store_clear may call some callbacks
1473            that use the column. Set the pointer to NULL to be safe. */
1474         gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, NULL, -1);
1475         g_list_free(list);
1476
1477         return FALSE;
1478 }
1479
1480 static void vficon_clear_store(ViewFile *vf)
1481 {
1482         GtkTreeModel *store;
1483
1484         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1485         gtk_tree_model_foreach(store, vficon_destroy_node_cb, nullptr);
1486
1487         gtk_list_store_clear(GTK_LIST_STORE(store));
1488 }
1489
1490 static GList *vficon_add_row(ViewFile *vf, GtkTreeIter *iter)
1491 {
1492         GtkListStore *store;
1493         GList *list = nullptr;
1494         gint i;
1495
1496         for (i = 0; i < VFICON(vf)->columns; i++) list = g_list_prepend(list, nullptr);
1497
1498         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview)));
1499         gtk_list_store_append(store, iter);
1500         gtk_list_store_set(store, iter, FILE_COLUMN_POINTER, list, -1);
1501
1502         return list;
1503 }
1504
1505 static void vficon_populate(ViewFile *vf, gboolean resize, gboolean keep_position)
1506 {
1507         GtkTreeModel *store;
1508         GtkTreePath *tpath;
1509         GList *work;
1510         FileData *visible_fd = nullptr;
1511         gint r, c;
1512         gboolean valid;
1513         GtkTreeIter iter;
1514
1515         vficon_verify_selections(vf);
1516
1517         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1518
1519         if (keep_position && gtk_widget_get_realized(vf->listview) &&
1520             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1521                 {
1522                 GtkTreeIter iter;
1523                 GList *list;
1524
1525                 gtk_tree_model_get_iter(store, &iter, tpath);
1526                 gtk_tree_path_free(tpath);
1527
1528                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1529                 if (list) visible_fd = static_cast<FileData *>(list->data);
1530                 }
1531
1532
1533         if (resize)
1534                 {
1535                 gint i;
1536                 gint thumb_width;
1537
1538                 vficon_clear_store(vf);
1539
1540                 thumb_width = vficon_get_icon_width(vf);
1541
1542                 for (i = 0; i < VFICON_MAX_COLUMNS; i++)
1543                         {
1544                         GtkTreeViewColumn *column;
1545                         GtkCellRenderer *cell;
1546                         GList *list;
1547
1548                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), i);
1549                         gtk_tree_view_column_set_visible(column, (i < VFICON(vf)->columns));
1550                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1551
1552                         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1553                         cell = static_cast<GtkCellRenderer *>((list) ? list->data : nullptr);
1554                         g_list_free(list);
1555
1556                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1557                                 {
1558                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1559                                                              "fixed_height", options->thumbnails.max_height,
1560                                                              "show_text", VFICON(vf)->show_text || options->show_star_rating,
1561                                                              "show_marks", vf->marks_enabled,
1562                                                              "num_marks", FILEDATA_MARKS_SIZE,
1563                                                              NULL);
1564                                 }
1565                         }
1566                 if (gtk_widget_get_realized(vf->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(vf->listview));
1567                 }
1568
1569         r = -1;
1570         c = 0;
1571
1572         valid = gtk_tree_model_iter_children(store, &iter, nullptr);
1573
1574         work = vf->list;
1575         while (work)
1576                 {
1577                 GList *list;
1578                 r++;
1579                 c = 0;
1580                 if (valid)
1581                         {
1582                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1583                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1584                         }
1585                 else
1586                         {
1587                         list = vficon_add_row(vf, &iter);
1588                         }
1589
1590                 while (list)
1591                         {
1592                         FileData *fd;
1593
1594                         if (work)
1595                                 {
1596                                 fd = static_cast<FileData *>(work->data);
1597                                 work = work->next;
1598                                 c++;
1599                                 }
1600                         else
1601                                 {
1602                                 fd = nullptr;
1603                                 }
1604
1605                         list->data = fd;
1606                         list = list->next;
1607                         }
1608                 if (valid) valid = gtk_tree_model_iter_next(store, &iter);
1609                 }
1610
1611         r++;
1612         while (valid)
1613                 {
1614                 GList *list;
1615
1616                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1617                 valid = gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1618                 g_list_free(list);
1619                 }
1620
1621         VFICON(vf)->rows = r;
1622
1623         if (visible_fd &&
1624             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1625                 {
1626                 GtkTreeIter iter;
1627                 GList *list;
1628
1629                 gtk_tree_model_get_iter(store, &iter, tpath);
1630                 gtk_tree_path_free(tpath);
1631
1632                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1633                 if (g_list_find(list, visible_fd) == nullptr &&
1634                     vficon_find_iter(vf, visible_fd, &iter, nullptr))
1635                         {
1636                         tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1637                         }
1638                 }
1639
1640
1641         vf_send_update(vf);
1642         vf_thumb_update(vf);
1643         vf_star_update(vf);
1644 }
1645
1646 static void vficon_populate_at_new_size(ViewFile *vf, gint w, gint, gboolean force)
1647 {
1648         gint new_cols;
1649         gint thumb_width;
1650
1651         thumb_width = vficon_get_icon_width(vf);
1652
1653         new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1654         if (new_cols < 1) new_cols = 1;
1655
1656         if (!force && new_cols == VFICON(vf)->columns) return;
1657
1658         VFICON(vf)->columns = new_cols;
1659
1660         vficon_populate(vf, TRUE, TRUE);
1661
1662         DEBUG_1("col tab pop cols=%d rows=%d", VFICON(vf)->columns, VFICON(vf)->rows);
1663 }
1664
1665 static void vficon_sized_cb(GtkWidget *, GtkAllocation *allocation, gpointer data)
1666 {
1667         auto vf = static_cast<ViewFile *>(data);
1668
1669         vficon_populate_at_new_size(vf, allocation->width, allocation->height, FALSE);
1670 }
1671
1672 /*
1673  *-----------------------------------------------------------------------------
1674  * misc
1675  *-----------------------------------------------------------------------------
1676  */
1677
1678 void vficon_sort_set(ViewFile *vf, SortType type, gboolean ascend)
1679 {
1680         if (vf->sort_method == type && vf->sort_ascend == ascend) return;
1681
1682         vf->sort_method = type;
1683         vf->sort_ascend = ascend;
1684
1685         if (!vf->list) return;
1686
1687         vf_refresh(vf);
1688 }
1689
1690 /*
1691  *-----------------------------------------------------------------------------
1692  * thumb updates
1693  *-----------------------------------------------------------------------------
1694  */
1695
1696 void vficon_thumb_progress_count(GList *list, gint *count, gint *done)
1697 {
1698         GList *work = list;
1699         while (work)
1700                 {
1701                 auto fd = static_cast<FileData *>(work->data);
1702                 work = work->next;
1703
1704                 if (fd->thumb_pixbuf) (*done)++;
1705                 (*count)++;
1706                 }
1707 }
1708
1709 void vficon_read_metadata_progress_count(GList *list, gint *count, gint *done)
1710 {
1711         GList *work = list;
1712         while (work)
1713                 {
1714                 auto fd = static_cast<FileData *>(work->data);
1715                 work = work->next;
1716
1717                 if (fd->metadata_in_idle_loaded) (*done)++;
1718                 (*count)++;
1719                 }
1720 }
1721
1722 void vficon_set_thumb_fd(ViewFile *vf, FileData *fd)
1723 {
1724         GtkTreeModel *store;
1725         GtkTreeIter iter;
1726         GList *list;
1727
1728         if (!g_list_find(vf->list, fd)) return;
1729         if (!vficon_find_iter(vf, fd, &iter, nullptr)) return;
1730
1731         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1732
1733         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1734         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1735 }
1736
1737 /* Returns the next fd without a loaded pixbuf, so the thumb-loader can load the pixbuf for it. */
1738 FileData *vficon_thumb_next_fd(ViewFile *vf)
1739 {
1740         GtkTreePath *tpath;
1741
1742         /* First see if there are visible files that don't have a loaded thumb... */
1743         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1744                 {
1745                 GtkTreeModel *store;
1746                 GtkTreeIter iter;
1747                 gboolean valid = TRUE;
1748
1749                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1750                 gtk_tree_model_get_iter(store, &iter, tpath);
1751                 gtk_tree_path_free(tpath);
1752                 tpath = nullptr;
1753
1754                 while (valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vf->listview), &iter, FALSE) == 0)
1755                         {
1756                         GList *list;
1757                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1758
1759                         /** @todo (xsdg): for loop here. */
1760                         for (; list; list = list->next)
1761                                 {
1762                                 auto fd = static_cast<FileData *>(list->data);
1763                                 if (fd && !fd->thumb_pixbuf) return fd;
1764                                 }
1765
1766                         valid = gtk_tree_model_iter_next(store, &iter);
1767                         }
1768                 }
1769
1770         /* Then iterate through the entire list to load all of them. */
1771         GList *work;
1772         for (work = vf->list; work; work = work->next)
1773                 {
1774                 auto fd = static_cast<FileData *>(work->data);
1775
1776                 // Note: This implementation differs from view-file-list.cc because sidecar files are not
1777                 // distinct list elements here, as they are in the list view.
1778                 if (!fd->thumb_pixbuf) return fd;
1779                 }
1780
1781         return nullptr;
1782 }
1783
1784 void vficon_set_star_fd(ViewFile *vf, FileData *fd)
1785 {
1786         GtkTreeModel *store;
1787         GtkTreeIter iter;
1788         GList *list;
1789
1790         if (!g_list_find(vf->list, fd)) return;
1791         if (!vficon_find_iter(vf, fd, &iter, nullptr)) return;
1792
1793         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1794
1795         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1796         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1797 }
1798
1799 FileData *vficon_star_next_fd(ViewFile *vf)
1800 {
1801         GtkTreePath *tpath;
1802
1803         /* first check the visible files */
1804
1805         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, nullptr, nullptr, nullptr))
1806                 {
1807                 GtkTreeModel *store;
1808                 GtkTreeIter iter;
1809                 gboolean valid = TRUE;
1810
1811                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1812                 gtk_tree_model_get_iter(store, &iter, tpath);
1813                 gtk_tree_path_free(tpath);
1814                 tpath = nullptr;
1815
1816                 while (valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vf->listview), &iter, FALSE) == 0)
1817                         {
1818                         GList *list;
1819                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1820
1821                         for (; list; list = list->next)
1822                                 {
1823                                 auto fd = static_cast<FileData *>(list->data);
1824                                 if (fd && fd->rating == STAR_RATING_NOT_READ)
1825                                         {
1826                                         vf->stars_filedata = fd;
1827
1828                                         if (vf->stars_id == 0)
1829                                                 {
1830                                                 vf->stars_id = g_idle_add_full(G_PRIORITY_LOW, vf_stars_cb, vf, nullptr);
1831                                                 }
1832
1833                                         return fd;
1834                                         }
1835                                 }
1836
1837                         valid = gtk_tree_model_iter_next(store, &iter);
1838                         }
1839                 }
1840
1841         /* Then iterate through the entire list to load all of them. */
1842
1843         GList *work;
1844         for (work = vf->list; work; work = work->next)
1845                 {
1846                 auto fd = static_cast<FileData *>(work->data);
1847
1848                 if (fd && fd->rating == STAR_RATING_NOT_READ)
1849                         {
1850                         vf->stars_filedata = fd;
1851
1852                         if (vf->stars_id == 0)
1853                                 {
1854                                 vf->stars_id = g_idle_add_full(G_PRIORITY_LOW, vf_stars_cb, vf, nullptr);
1855                                 }
1856
1857                         return fd;
1858                         }
1859                 }
1860
1861         return nullptr;
1862 }
1863
1864 /*
1865  *-----------------------------------------------------------------------------
1866  * row stuff
1867  *-----------------------------------------------------------------------------
1868  */
1869
1870 gint vficon_index_by_fd(ViewFile *vf, FileData *in_fd)
1871 {
1872         gint p = 0;
1873         GList *work;
1874
1875         if (!in_fd) return -1;
1876
1877         work = vf->list;
1878         while (work)
1879                 {
1880                 auto fd = static_cast<FileData *>(work->data);
1881                 if (fd == in_fd) return p;
1882                 work = work->next;
1883                 p++;
1884                 }
1885
1886         return -1;
1887 }
1888
1889 /*
1890  *-----------------------------------------------------------------------------
1891  *
1892  *-----------------------------------------------------------------------------
1893  */
1894
1895 static gboolean vficon_refresh_real(ViewFile *vf, gboolean keep_position)
1896 {
1897         gboolean ret = TRUE;
1898         GList *work, *new_work;
1899         FileData *first_selected = nullptr;
1900         GList *new_filelist = nullptr;
1901         GList *new_fd_list = nullptr;
1902         GList *old_selected = nullptr;
1903         GtkTreePath *end_path = nullptr;
1904         GtkTreePath *start_path = nullptr;
1905
1906         gtk_tree_view_get_visible_range(GTK_TREE_VIEW(vf->listview), &start_path, &end_path);
1907
1908         if (vf->dir_fd)
1909                 {
1910                 ret = filelist_read(vf->dir_fd, &new_filelist, nullptr);
1911                 new_filelist = file_data_filter_marks_list(new_filelist, vf_marks_get_filter(vf));
1912                 new_filelist = g_list_first(new_filelist);
1913                 new_filelist = file_data_filter_file_filter_list(new_filelist, vf_file_filter_get_filter(vf));
1914
1915                 new_filelist = g_list_first(new_filelist);
1916                 new_filelist = file_data_filter_class_list(new_filelist, vf_class_get_filter(vf));
1917
1918                 }
1919
1920         vf->list = filelist_sort(vf->list, vf->sort_method, vf->sort_ascend); /* the list might not be sorted if there were renames */
1921         new_filelist = filelist_sort(new_filelist, vf->sort_method, vf->sort_ascend);
1922
1923         if (VFICON(vf)->selection)
1924                 {
1925                 old_selected = g_list_copy(VFICON(vf)->selection);
1926                 first_selected = static_cast<FileData *>(VFICON(vf)->selection->data);
1927                 file_data_ref(first_selected);
1928                 g_list_free(VFICON(vf)->selection);
1929                 VFICON(vf)->selection = nullptr;
1930                 }
1931
1932         /* iterate old list and new list, looking for differences */
1933         work = vf->list;
1934         new_work = new_filelist;
1935         while (work || new_work)
1936                 {
1937                 FileData *fd = nullptr;
1938                 FileData *new_fd = nullptr;
1939                 gint match;
1940
1941                 if (work && new_work)
1942                         {
1943                         fd = static_cast<FileData *>(work->data);
1944                         new_fd = static_cast<FileData *>(new_work->data);
1945
1946                         if (fd == new_fd)
1947                                 {
1948                                 /* not changed, go to next */
1949                                 work = work->next;
1950                                 new_work = new_work->next;
1951                                 if (fd->selected & SELECTION_SELECTED)
1952                                         {
1953                                         VFICON(vf)->selection = g_list_prepend(VFICON(vf)->selection, fd);
1954                                         }
1955                                 continue;
1956                                 }
1957
1958                         match = filelist_sort_compare_filedata_full(fd, new_fd, vf->sort_method, vf->sort_ascend);
1959                         if (match == 0) g_warning("multiple fd for the same path");
1960                         }
1961                 else if (work)
1962                         {
1963                         /* old item was deleted */
1964                         fd = static_cast<FileData *>(work->data);
1965                         match = -1;
1966                         }
1967                 else
1968                         {
1969                         /* new item was added */
1970                         new_fd = static_cast<FileData *>(new_work->data);
1971                         match = 1;
1972                         }
1973
1974                 if (match < 0)
1975                         {
1976                         /* file no longer exists, delete from vf->list */
1977                         GList *to_delete = work;
1978                         work = work->next;
1979                         if (fd == VFICON(vf)->prev_selection) VFICON(vf)->prev_selection = nullptr;
1980                         if (fd == VFICON(vf)->click_fd) VFICON(vf)->click_fd = nullptr;
1981                         file_data_unref(fd);
1982                         vf->list = g_list_delete_link(vf->list, to_delete);
1983                         }
1984                 else
1985                         {
1986                         /* new file, add to vf->list */
1987                         file_data_ref(new_fd);
1988                         new_fd->selected = SELECTION_NONE;
1989                         if (work)
1990                                 {
1991                                 vf->list = g_list_insert_before(vf->list, work, new_fd);
1992                                 }
1993                         else
1994                                 {
1995                                 /* it is faster to append all new entries together later */
1996                                 new_fd_list = g_list_prepend(new_fd_list, new_fd);
1997                                 }
1998
1999                         new_work = new_work->next;
2000                         }
2001                 }
2002
2003         if (new_fd_list)
2004                 {
2005                 vf->list = g_list_concat(vf->list, g_list_reverse(new_fd_list));
2006                 }
2007
2008         VFICON(vf)->selection = g_list_reverse(VFICON(vf)->selection);
2009
2010         /* Preserve the original selection order */
2011         if (old_selected)
2012                 {
2013                 GList *reversed_old_selected;
2014
2015                 reversed_old_selected = g_list_reverse(old_selected);
2016                 while (reversed_old_selected)
2017                         {
2018                         GList *tmp;
2019                         tmp = g_list_find(VFICON(vf)->selection, reversed_old_selected->data);
2020                         if (tmp)
2021                                 {
2022                                 VFICON(vf)->selection = g_list_remove_link(VFICON(vf)->selection, tmp);
2023                                 VFICON(vf)->selection = g_list_concat(tmp, VFICON(vf)->selection);
2024                                 }
2025                         reversed_old_selected = reversed_old_selected->next;
2026                         }
2027                 g_list_free(old_selected);
2028                 }
2029
2030         filelist_free(new_filelist);
2031
2032         vficon_populate(vf, TRUE, keep_position);
2033
2034         if (first_selected && !VFICON(vf)->selection)
2035                 {
2036                 /* all selected files disappeared */
2037                 vficon_select_closest(vf, first_selected);
2038                 }
2039         file_data_unref(first_selected);
2040
2041         if (start_path)
2042                 {
2043                 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vf->listview), start_path, nullptr, FALSE, 0.0, 0.0);
2044                 }
2045
2046         gtk_tree_path_free(start_path);
2047         gtk_tree_path_free(end_path);
2048
2049         return ret;
2050 }
2051
2052 gboolean vficon_refresh(ViewFile *vf)
2053 {
2054         return vficon_refresh_real(vf, TRUE);
2055 }
2056
2057 /*
2058  *-----------------------------------------------------------------------------
2059  * draw, etc.
2060  *-----------------------------------------------------------------------------
2061  */
2062
2063 struct ColumnData
2064 {
2065         ViewFile *vf;
2066         gint number;
2067 };
2068
2069 static void vficon_cell_data_cb(GtkTreeViewColumn *, GtkCellRenderer *cell,
2070                                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2071 {
2072         GList *list;
2073         FileData *fd;
2074         auto cd = static_cast<ColumnData *>(data);
2075         ViewFile *vf = cd->vf;
2076         gchar *star_rating;
2077
2078         if (!GQV_IS_CELL_RENDERER_ICON(cell)) return;
2079
2080         gtk_tree_model_get(tree_model, iter, FILE_COLUMN_POINTER, &list, -1);
2081
2082         fd = static_cast<FileData *>(g_list_nth_data(list, cd->number));
2083
2084         if (fd)
2085                 {
2086                 GdkColor color_fg;
2087                 GdkColor color_bg;
2088                 GtkStyle *style;
2089                 gchar *name_sidecars = nullptr;
2090                 const gchar *link;
2091                 GtkStateType state = GTK_STATE_NORMAL;
2092
2093                 g_assert(fd->magick == FD_MAGICK);
2094
2095                 if (options->show_star_rating && fd->rating != STAR_RATING_NOT_READ)
2096                         {
2097                         star_rating = convert_rating_to_stars(fd->rating);
2098                         }
2099                 else
2100                         {
2101                         star_rating = nullptr;
2102                         }
2103
2104                 link = islink(fd->path) ? GQ_LINK_STR : "";
2105                 if (fd->sidecar_files)
2106                         {
2107                         gchar *sidecars = file_data_sc_list_to_string(fd);
2108                         if (options->show_star_rating && VFICON(vf)->show_text)
2109                                 {
2110                                 name_sidecars = g_strdup_printf("%s%s %s\n%s", link, fd->name, sidecars, star_rating);
2111                                 }
2112                         else if (options->show_star_rating)
2113                                 {
2114                                 name_sidecars = g_strdup_printf("%s", star_rating);
2115                                 }
2116                         else if (VFICON(vf)->show_text)
2117                                 {
2118                                 name_sidecars = g_strdup_printf("%s%s %s", link, fd->name, sidecars);
2119                                 }
2120                         g_free(sidecars);
2121                         }
2122                 else
2123                         {
2124                         const gchar *disabled_grouping = fd->disable_grouping ? _(" [NO GROUPING]") : "";
2125                         if (options->show_star_rating && VFICON(vf)->show_text)
2126                                 {
2127                                 name_sidecars = g_strdup_printf("%s%s%s\n%s", link, fd->name, disabled_grouping, star_rating);
2128                                 }
2129                         else if (options->show_star_rating)
2130                                 {
2131                                 name_sidecars = g_strdup_printf("%s", star_rating);
2132                                 }
2133                         else if (VFICON(vf)->show_text)
2134                                 {
2135                                 name_sidecars = g_strdup_printf("%s%s%s", link, fd->name, disabled_grouping);
2136                                 }
2137                         }
2138                 g_free(star_rating);
2139
2140                 style = gtk_widget_get_style(vf->listview);
2141                 if (fd->selected & SELECTION_SELECTED)
2142                         {
2143                         state = GTK_STATE_SELECTED;
2144                         }
2145
2146                 memcpy(&color_fg, &style->text[state], sizeof(color_fg));
2147                 memcpy(&color_bg, &style->base[state], sizeof(color_bg));
2148
2149                 if (fd->selected & SELECTION_PRELIGHT)
2150                         {
2151                         shift_color(&color_bg, -1, 0);
2152                         }
2153
2154                 g_object_set(cell,      "pixbuf", fd->thumb_pixbuf,
2155                                         "text", name_sidecars,
2156                                         "marks", file_data_get_marks(fd),
2157                                         "show_marks", vf->marks_enabled,
2158                                         "cell-background-gdk", &color_bg,
2159                                         "cell-background-set", TRUE,
2160                                         "foreground-gdk", &color_fg,
2161                                         "foreground-set", TRUE,
2162                                         "has-focus", (VFICON(vf)->focus_fd == fd), NULL);
2163                 g_free(name_sidecars);
2164                 }
2165         else
2166                 {
2167                 g_object_set(cell,      "pixbuf", NULL,
2168                                         "text", NULL,
2169                                         "show_marks", FALSE,
2170                                         "cell-background-set", FALSE,
2171                                         "foreground-set", FALSE,
2172                                         "has-focus", FALSE, NULL);
2173                 }
2174 }
2175
2176 static void vficon_append_column(ViewFile *vf, gint n)
2177 {
2178         GtkTreeViewColumn *column;
2179         GtkCellRenderer *renderer;
2180
2181         column = gtk_tree_view_column_new();
2182         gtk_tree_view_column_set_min_width(column, 0);
2183
2184         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2185         gtk_tree_view_column_set_alignment(column, 0.5);
2186
2187         renderer = gqv_cell_renderer_icon_new();
2188         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2189         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2190                                          "ypad", THUMB_BORDER_PADDING,
2191                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2192
2193         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2194         g_object_set_data(G_OBJECT(renderer), "column_number", GINT_TO_POINTER(n));
2195
2196         auto cd = g_new0(ColumnData, 1);
2197         cd->vf = vf;
2198         cd->number = n;
2199         gtk_tree_view_column_set_cell_data_func(column, renderer, vficon_cell_data_cb, cd, g_free);
2200
2201         gtk_tree_view_append_column(GTK_TREE_VIEW(vf->listview), column);
2202
2203         g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(vficon_mark_toggled_cb), vf);
2204 }
2205
2206 /*
2207  *-----------------------------------------------------------------------------
2208  * base
2209  *-----------------------------------------------------------------------------
2210  */
2211
2212 gboolean vficon_set_fd(ViewFile *vf, FileData *dir_fd)
2213 {
2214         gboolean ret;
2215
2216         if (!dir_fd) return FALSE;
2217         if (vf->dir_fd == dir_fd) return TRUE;
2218
2219         file_data_unref(vf->dir_fd);
2220         vf->dir_fd = file_data_ref(dir_fd);
2221
2222         g_list_free(VFICON(vf)->selection);
2223         VFICON(vf)->selection = nullptr;
2224
2225         g_list_free(vf->list);
2226         vf->list = nullptr;
2227
2228         /* NOTE: populate will clear the store for us */
2229         ret = vficon_refresh_real(vf, FALSE);
2230
2231         VFICON(vf)->focus_fd = nullptr;
2232         vficon_move_focus(vf, 0, 0, FALSE);
2233
2234         return ret;
2235 }
2236
2237 void vficon_destroy_cb(GtkWidget *, gpointer data)
2238 {
2239         auto vf = static_cast<ViewFile *>(data);
2240
2241         vf_refresh_idle_cancel(vf);
2242
2243         file_data_unregister_notify_func(vf_notify_cb, vf);
2244
2245         tip_unschedule(vf);
2246
2247         vf_thumb_cleanup(vf);
2248         vf_star_cleanup(vf);
2249
2250         g_list_free(vf->list);
2251         g_list_free(VFICON(vf)->selection);
2252 }
2253
2254 ViewFile *vficon_new(ViewFile *vf, FileData *)
2255 {
2256         GtkListStore *store;
2257         GtkTreeSelection *selection;
2258         gint i;
2259
2260         vf->info = g_new0(ViewFileInfoIcon, 1);
2261
2262         VFICON(vf)->show_text = options->show_icon_names;
2263
2264         store = gtk_list_store_new(1, G_TYPE_POINTER);
2265         vf->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2266         g_object_unref(store);
2267
2268         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vf->listview));
2269         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2270
2271         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vf->listview), FALSE);
2272         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vf->listview), FALSE);
2273
2274         for (i = 0; i < VFICON_MAX_COLUMNS; i++)
2275                 {
2276                 vficon_append_column(vf, i);
2277                 }
2278
2279         /* zero width column to hide tree view focus, we draw it ourselves */
2280         vficon_append_column(vf, i);
2281         /* end column to fill white space */
2282         vficon_append_column(vf, i);
2283
2284         g_signal_connect(G_OBJECT(vf->listview), "size_allocate",
2285                          G_CALLBACK(vficon_sized_cb), vf);
2286
2287         gtk_widget_set_events(vf->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2288                               static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK));
2289
2290         g_signal_connect(G_OBJECT(vf->listview),"motion_notify_event",
2291                          G_CALLBACK(vficon_motion_cb), vf);
2292         g_signal_connect(G_OBJECT(vf->listview), "leave_notify_event",
2293                          G_CALLBACK(vficon_leave_cb), vf);
2294
2295         /* force VFICON(vf)->columns to be at least 1 (sane) - this will be corrected in the size_cb */
2296         vficon_populate_at_new_size(vf, 1, 1, FALSE);
2297
2298         file_data_register_notify_func(vf_notify_cb, vf, NOTIFY_PRIORITY_MEDIUM);
2299
2300         return vf;
2301 }
2302
2303 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */