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