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