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