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