Fix #624: Filter files by shell or regular expression pattern
[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                                 if (VFICON(vf)->click_fd->format_class == FORMAT_CLASS_COLLECTION)
1371                                         {
1372                                         collection_window_new(VFICON(vf)->click_fd->path);
1373                                         }
1374                                 else
1375                                         {
1376                                         vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1377                                         layout_image_full_screen_start(vf->layout);
1378                                         }
1379                                 }
1380                         break;
1381                 case MOUSE_BUTTON_RIGHT:
1382                         vf->popup = vf_pop_menu(vf);
1383                         gtk_menu_popup(GTK_MENU(vf->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1384                         break;
1385                 default:
1386                         break;
1387                 }
1388
1389         return FALSE;
1390 }
1391
1392 gboolean vficon_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1393 {
1394         ViewFile *vf = data;
1395         GtkTreeIter iter;
1396         FileData *fd = NULL;
1397         gboolean was_selected;
1398
1399         tip_schedule(vf);
1400
1401         if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1402                 {
1403                 fd = vficon_find_data_by_coord(vf, (gint)bevent->x, (gint)bevent->y, &iter);
1404                 }
1405
1406         if (VFICON(vf)->click_fd)
1407                 {
1408                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, NULL);
1409                 }
1410
1411         if (!fd || VFICON(vf)->click_fd != fd) return TRUE;
1412
1413         was_selected = !!(fd->selected & SELECTION_SELECTED);
1414
1415         switch (bevent->button)
1416                 {
1417                 case MOUSE_BUTTON_LEFT:
1418                         {
1419                         vficon_set_focus(vf, fd);
1420
1421                         if (bevent->state & GDK_CONTROL_MASK)
1422                                 {
1423                                 gboolean select;
1424
1425                                 select = !(fd->selected & SELECTION_SELECTED);
1426                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1427                                         {
1428                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, select);
1429                                         }
1430                                 else
1431                                         {
1432                                         vficon_select_util(vf, fd, select);
1433                                         }
1434                                 }
1435                         else
1436                                 {
1437                                 vf_select_none(vf);
1438
1439                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1440                                         {
1441                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, TRUE);
1442                                         }
1443                                 else
1444                                         {
1445                                         vficon_select_util(vf, fd, TRUE);
1446                                         was_selected = FALSE;
1447                                         }
1448                                 }
1449                         }
1450                         break;
1451                 case MOUSE_BUTTON_MIDDLE:
1452                         {
1453                         vficon_select_util(vf, fd, !(fd->selected & SELECTION_SELECTED));
1454                         }
1455                         break;
1456                 default:
1457                         break;
1458                 }
1459
1460         if (!was_selected && (fd->selected & SELECTION_SELECTED))
1461                 {
1462                 vficon_send_layout_select(vf, fd);
1463                 }
1464
1465         return TRUE;
1466 }
1467
1468 static gboolean vficon_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1469 {
1470         ViewFile *vf = data;
1471
1472         tip_unschedule(vf);
1473         return FALSE;
1474 }
1475
1476 /*
1477  *-------------------------------------------------------------------
1478  * population
1479  *-------------------------------------------------------------------
1480  */
1481
1482 static gboolean vficon_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1483 {
1484         GList *list;
1485
1486         gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
1487
1488         /* it seems that gtk_list_store_clear may call some callbacks
1489            that use the column. Set the pointer to NULL to be safe. */
1490         gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, NULL, -1);
1491         g_list_free(list);
1492
1493         return FALSE;
1494 }
1495
1496 static void vficon_clear_store(ViewFile *vf)
1497 {
1498         GtkTreeModel *store;
1499
1500         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1501         gtk_tree_model_foreach(store, vficon_destroy_node_cb, NULL);
1502
1503         gtk_list_store_clear(GTK_LIST_STORE(store));
1504 }
1505
1506 static GList *vficon_add_row(ViewFile *vf, GtkTreeIter *iter)
1507 {
1508         GtkListStore *store;
1509         GList *list = NULL;
1510         gint i;
1511
1512         for (i = 0; i < VFICON(vf)->columns; i++) list = g_list_prepend(list, NULL);
1513
1514         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview)));
1515         gtk_list_store_append(store, iter);
1516         gtk_list_store_set(store, iter, FILE_COLUMN_POINTER, list, -1);
1517
1518         return list;
1519 }
1520
1521 static void vficon_populate(ViewFile *vf, gboolean resize, gboolean keep_position)
1522 {
1523         GtkTreeModel *store;
1524         GtkTreePath *tpath;
1525         GList *work;
1526         FileData *visible_fd = NULL;
1527         gint r, c;
1528         gboolean valid;
1529         GtkTreeIter iter;
1530
1531         vficon_verify_selections(vf);
1532
1533         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1534
1535         if (keep_position && gtk_widget_get_realized(vf->listview) &&
1536             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, NULL, NULL, NULL))
1537                 {
1538                 GtkTreeIter iter;
1539                 GList *list;
1540
1541                 gtk_tree_model_get_iter(store, &iter, tpath);
1542                 gtk_tree_path_free(tpath);
1543
1544                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1545                 if (list) visible_fd = list->data;
1546                 }
1547
1548
1549         if (resize)
1550                 {
1551                 gint i;
1552                 gint thumb_width;
1553
1554                 vficon_clear_store(vf);
1555
1556                 thumb_width = vficon_get_icon_width(vf);
1557
1558                 for (i = 0; i < VFICON_MAX_COLUMNS; i++)
1559                         {
1560                         GtkTreeViewColumn *column;
1561                         GtkCellRenderer *cell;
1562                         GList *list;
1563
1564                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), i);
1565                         gtk_tree_view_column_set_visible(column, (i < VFICON(vf)->columns));
1566                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1567
1568                         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1569                         cell = (list) ? list->data : NULL;
1570                         g_list_free(list);
1571
1572                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1573                                 {
1574                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1575                                                              "fixed_height", options->thumbnails.max_height,
1576                                                              "show_text", VFICON(vf)->show_text || options->show_star_rating,
1577                                                              "show_marks", vf->marks_enabled,
1578                                                              "num_marks", FILEDATA_MARKS_SIZE,
1579                                                              NULL);
1580                                 }
1581                         }
1582                 if (gtk_widget_get_realized(vf->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(vf->listview));
1583                 }
1584
1585         r = -1;
1586         c = 0;
1587
1588         valid = gtk_tree_model_iter_children(store, &iter, NULL);
1589
1590         work = vf->list;
1591         while (work)
1592                 {
1593                 GList *list;
1594                 r++;
1595                 c = 0;
1596                 if (valid)
1597                         {
1598                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1599                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1600                         }
1601                 else
1602                         {
1603                         list = vficon_add_row(vf, &iter);
1604                         }
1605
1606                 while (list)
1607                         {
1608                         FileData *fd;
1609
1610                         if (work)
1611                                 {
1612                                 fd = work->data;
1613                                 work = work->next;
1614                                 c++;
1615                                 }
1616                         else
1617                                 {
1618                                 fd = NULL;
1619                                 }
1620
1621                         list->data = fd;
1622                         list = list->next;
1623                         }
1624                 if (valid) valid = gtk_tree_model_iter_next(store, &iter);
1625                 }
1626
1627         r++;
1628         while (valid)
1629                 {
1630                 GList *list;
1631
1632                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1633                 valid = gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1634                 g_list_free(list);
1635                 }
1636
1637         VFICON(vf)->rows = r;
1638
1639         if (visible_fd &&
1640             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, NULL, NULL, NULL))
1641                 {
1642                 GtkTreeIter iter;
1643                 GList *list;
1644
1645                 gtk_tree_model_get_iter(store, &iter, tpath);
1646                 gtk_tree_path_free(tpath);
1647
1648                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1649                 if (g_list_find(list, visible_fd) == NULL &&
1650                     vficon_find_iter(vf, visible_fd, &iter, NULL))
1651                         {
1652                         tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1653                         }
1654                 }
1655
1656
1657         vf_send_update(vf);
1658         vf_thumb_update(vf);
1659 }
1660
1661 static void vficon_populate_at_new_size(ViewFile *vf, gint w, gint h, gboolean force)
1662 {
1663         gint new_cols;
1664         gint thumb_width;
1665
1666         thumb_width = vficon_get_icon_width(vf);
1667
1668         new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1669         if (new_cols < 1) new_cols = 1;
1670
1671         if (!force && new_cols == VFICON(vf)->columns) return;
1672
1673         VFICON(vf)->columns = new_cols;
1674
1675         vficon_populate(vf, TRUE, TRUE);
1676
1677         DEBUG_1("col tab pop cols=%d rows=%d", VFICON(vf)->columns, VFICON(vf)->rows);
1678 }
1679
1680 static void vficon_sized_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
1681 {
1682         ViewFile *vf = data;
1683
1684         vficon_populate_at_new_size(vf, allocation->width, allocation->height, FALSE);
1685 }
1686
1687 /*
1688  *-----------------------------------------------------------------------------
1689  * misc
1690  *-----------------------------------------------------------------------------
1691  */
1692
1693 void vficon_sort_set(ViewFile *vf, SortType type, gboolean ascend)
1694 {
1695         if (vf->sort_method == type && vf->sort_ascend == ascend) return;
1696
1697         vf->sort_method = type;
1698         vf->sort_ascend = ascend;
1699
1700         if (!vf->list) return;
1701
1702         vf_refresh(vf);
1703 }
1704
1705 /*
1706  *-----------------------------------------------------------------------------
1707  * thumb updates
1708  *-----------------------------------------------------------------------------
1709  */
1710
1711 void vficon_thumb_progress_count(GList *list, gint *count, gint *done)
1712 {
1713         GList *work = list;
1714         while (work)
1715                 {
1716                 FileData *fd = work->data;
1717                 work = work->next;
1718
1719                 if (fd->thumb_pixbuf) (*done)++;
1720                 (*count)++;
1721                 }
1722 }
1723
1724 void vficon_read_metadata_progress_count(GList *list, gint *count, gint *done)
1725 {
1726         GList *work = list;
1727         while (work)
1728                 {
1729                 FileData *fd = work->data;
1730                 work = work->next;
1731
1732                 if (fd->metadata_in_idle_loaded) (*done)++;
1733                 (*count)++;
1734                 }
1735 }
1736
1737 void vficon_set_thumb_fd(ViewFile *vf, FileData *fd)
1738 {
1739         GtkTreeModel *store;
1740         GtkTreeIter iter;
1741         GList *list;
1742
1743         if (!g_list_find(vf->list, fd)) return;
1744         if (!vficon_find_iter(vf, fd, &iter, NULL)) return;
1745
1746         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1747
1748         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1749         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1750 }
1751
1752 /* Returns the next fd without a loaded pixbuf, so the thumb-loader can load the pixbuf for it. */
1753 FileData *vficon_thumb_next_fd(ViewFile *vf)
1754 {
1755         GtkTreePath *tpath;
1756
1757         /* First see if there are visible files that don't have a loaded thumb... */
1758         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, NULL, NULL, NULL))
1759                 {
1760                 GtkTreeModel *store;
1761                 GtkTreeIter iter;
1762                 gboolean valid = TRUE;
1763
1764                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1765                 gtk_tree_model_get_iter(store, &iter, tpath);
1766                 gtk_tree_path_free(tpath);
1767                 tpath = NULL;
1768
1769                 while (valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vf->listview), &iter, FALSE) == 0)
1770                         {
1771                         GList *list;
1772                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1773
1774                         // TODO(xsdg): for loop here.
1775                         for (; list; list = list->next)
1776                                 {
1777                                 FileData *fd = list->data;
1778                                 if (fd && !fd->thumb_pixbuf) return fd;
1779                                 }
1780
1781                         valid = gtk_tree_model_iter_next(store, &iter);
1782                         }
1783                 }
1784
1785         /* Then iterate through the entire list to load all of them. */
1786         GList *work;
1787         for (work = vf->list; work; work = work->next)
1788                 {
1789                 FileData *fd = work->data;
1790
1791                 // Note: This implementation differs from view_file_list.c because sidecar files are not
1792                 // distinct list elements here, as they are in the list view.
1793                 if (!fd->thumb_pixbuf) return fd;
1794                 }
1795
1796         return NULL;
1797 }
1798
1799 /*
1800  *-----------------------------------------------------------------------------
1801  * row stuff
1802  *-----------------------------------------------------------------------------
1803  */
1804
1805 gint vficon_index_by_fd(ViewFile *vf, FileData *in_fd)
1806 {
1807         gint p = 0;
1808         GList *work;
1809
1810         if (!in_fd) return -1;
1811
1812         work = vf->list;
1813         while (work)
1814                 {
1815                 FileData *fd = work->data;
1816                 if (fd == in_fd) return p;
1817                 work = work->next;
1818                 p++;
1819                 }
1820
1821         return -1;
1822 }
1823
1824 /*
1825  *-----------------------------------------------------------------------------
1826  *
1827  *-----------------------------------------------------------------------------
1828  */
1829
1830 static gboolean vficon_refresh_real(ViewFile *vf, gboolean keep_position)
1831 {
1832         gboolean ret = TRUE;
1833         GList *work, *new_work;
1834         FileData *focus_fd;
1835         FileData *first_selected = NULL;
1836         GList *new_filelist = NULL;
1837         GList *new_fd_list = NULL;
1838
1839         focus_fd = VFICON(vf)->focus_fd;
1840
1841         if (vf->dir_fd)
1842                 {
1843                 ret = filelist_read(vf->dir_fd, &new_filelist, NULL);
1844                 new_filelist = file_data_filter_marks_list(new_filelist, vf_marks_get_filter(vf));
1845                 new_filelist = g_list_first(new_filelist);
1846                 new_filelist = file_data_filter_file_filter_list(new_filelist, vf_file_filter_get_filter(vf));
1847                 }
1848
1849         vf->list = filelist_sort(vf->list, vf->sort_method, vf->sort_ascend); /* the list might not be sorted if there were renames */
1850         new_filelist = filelist_sort(new_filelist, vf->sort_method, vf->sort_ascend);
1851
1852         if (VFICON(vf)->selection)
1853                 {
1854                 first_selected = VFICON(vf)->selection->data;
1855                 file_data_ref(first_selected);
1856                 g_list_free(VFICON(vf)->selection);
1857                 VFICON(vf)->selection = NULL;
1858                 }
1859
1860         /* iterate old list and new list, looking for differences */
1861         work = vf->list;
1862         new_work = new_filelist;
1863         while (work || new_work)
1864                 {
1865                 FileData *fd = NULL;
1866                 FileData *new_fd = NULL;
1867                 gint match;
1868
1869                 if (work && new_work)
1870                         {
1871                         fd = work->data;
1872                         new_fd = new_work->data;
1873
1874                         if (fd == new_fd)
1875                                 {
1876                                 /* not changed, go to next */
1877                                 work = work->next;
1878                                 new_work = new_work->next;
1879                                 if (fd->selected & SELECTION_SELECTED)
1880                                         {
1881                                         VFICON(vf)->selection = g_list_prepend(VFICON(vf)->selection, fd);
1882                                         }
1883                                 continue;
1884                                 }
1885
1886                         match = filelist_sort_compare_filedata_full(fd, new_fd, vf->sort_method, vf->sort_ascend);
1887                         if (match == 0) g_warning("multiple fd for the same path");
1888                         }
1889                 else if (work)
1890                         {
1891                         /* old item was deleted */
1892                         fd = work->data;
1893                         match = -1;
1894                         }
1895                 else
1896                         {
1897                         /* new item was added */
1898                         new_fd = new_work->data;
1899                         match = 1;
1900                         }
1901
1902                 if (match < 0)
1903                         {
1904                         /* file no longer exists, delete from vf->list */
1905                         GList *to_delete = work;
1906                         work = work->next;
1907                         if (fd == VFICON(vf)->prev_selection) VFICON(vf)->prev_selection = NULL;
1908                         if (fd == VFICON(vf)->click_fd) VFICON(vf)->click_fd = NULL;
1909                         file_data_unref(fd);
1910                         vf->list = g_list_delete_link(vf->list, to_delete);
1911                         }
1912                 else
1913                         {
1914                         /* new file, add to vf->list */
1915                         file_data_ref(new_fd);
1916                         new_fd->selected = SELECTION_NONE;
1917                         if (work)
1918                                 {
1919                                 vf->list = g_list_insert_before(vf->list, work, new_fd);
1920                                 }
1921                         else
1922                                 {
1923                                 /* it is faster to append all new entries together later */
1924                                 new_fd_list = g_list_prepend(new_fd_list, new_fd);
1925                                 }
1926
1927                         new_work = new_work->next;
1928                         }
1929                 }
1930
1931         if (new_fd_list)
1932                 {
1933                 vf->list = g_list_concat(vf->list, g_list_reverse(new_fd_list));
1934                 }
1935
1936         VFICON(vf)->selection = g_list_reverse(VFICON(vf)->selection);
1937
1938         filelist_free(new_filelist);
1939
1940         vficon_populate(vf, TRUE, keep_position);
1941
1942         if (first_selected && !VFICON(vf)->selection)
1943                 {
1944                 /* all selected files disappeared */
1945                 vficon_select_closest(vf, first_selected);
1946                 }
1947         file_data_unref(first_selected);
1948
1949         /* attempt to keep focus on same icon when refreshing */
1950         if (focus_fd && g_list_find(vf->list, focus_fd))
1951                 {
1952                 vficon_set_focus(vf, focus_fd);
1953                 }
1954
1955         return ret;
1956 }
1957
1958 gboolean vficon_refresh(ViewFile *vf)
1959 {
1960         return vficon_refresh_real(vf, TRUE);
1961 }
1962
1963 /*
1964  *-----------------------------------------------------------------------------
1965  * draw, etc.
1966  *-----------------------------------------------------------------------------
1967  */
1968
1969 typedef struct _ColumnData ColumnData;
1970 struct _ColumnData
1971 {
1972         ViewFile *vf;
1973         gint number;
1974 };
1975
1976 static void vficon_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
1977                                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
1978 {
1979         GList *list;
1980         FileData *fd;
1981         ColumnData *cd = data;
1982         ViewFile *vf = cd->vf;
1983         gchar *star_rating;
1984
1985         if (!GQV_IS_CELL_RENDERER_ICON(cell)) return;
1986
1987         gtk_tree_model_get(tree_model, iter, FILE_COLUMN_POINTER, &list, -1);
1988
1989         fd = g_list_nth_data(list, cd->number);
1990
1991         if (fd)
1992                 {
1993                 GdkColor color_fg;
1994                 GdkColor color_bg;
1995                 GtkStyle *style;
1996                 gchar *name_sidecars = NULL;
1997                 gchar *link;
1998                 GtkStateType state = GTK_STATE_NORMAL;
1999
2000                 g_assert(fd->magick == FD_MAGICK);
2001
2002                 if (options->show_star_rating)
2003                         {
2004                         star_rating = metadata_read_rating_stars(fd);
2005                         }
2006                 else
2007                         {
2008                         star_rating = NULL;
2009                         }
2010
2011                 link = islink(fd->path) ? GQ_LINK_STR : "";
2012                 if (fd->sidecar_files)
2013                         {
2014                         gchar *sidecars = file_data_sc_list_to_string(fd);
2015                         if (options->show_star_rating && VFICON(vf)->show_text)
2016                                 {
2017                                 name_sidecars = g_strdup_printf("%s%s %s\n%s", link, fd->name, sidecars, star_rating);
2018                                 }
2019                         else if (options->show_star_rating)
2020                                 {
2021                                 name_sidecars = g_strdup_printf("%s", star_rating);
2022                                 }
2023                         else if (VFICON(vf)->show_text)
2024                                 {
2025                                 name_sidecars = g_strdup_printf("%s%s %s", link, fd->name, sidecars);
2026                                 }
2027                         g_free(sidecars);
2028                         }
2029                 else
2030                         {
2031                         gchar *disabled_grouping = fd->disable_grouping ? _(" [NO GROUPING]") : "";
2032                         if (options->show_star_rating && VFICON(vf)->show_text)
2033                                 {
2034                                 name_sidecars = g_strdup_printf("%s%s%s\n%s", link, fd->name, disabled_grouping, star_rating);
2035                                 }
2036                         else if (options->show_star_rating)
2037                                 {
2038                                 name_sidecars = g_strdup_printf("%s", star_rating);
2039                                 }
2040                         else if (VFICON(vf)->show_text)
2041                                 {
2042                                 name_sidecars = g_strdup_printf("%s%s%s", link, fd->name, disabled_grouping);
2043                                 }
2044                         }
2045                 g_free(star_rating);
2046
2047                 style = gtk_widget_get_style(vf->listview);
2048                 if (fd->selected & SELECTION_SELECTED)
2049                         {
2050                         state = GTK_STATE_SELECTED;
2051                         }
2052
2053                 memcpy(&color_fg, &style->text[state], sizeof(color_fg));
2054                 memcpy(&color_bg, &style->base[state], sizeof(color_bg));
2055
2056                 if (fd->selected & SELECTION_PRELIGHT)
2057                         {
2058                         shift_color(&color_bg, -1, 0);
2059                         }
2060
2061                 g_object_set(cell,      "pixbuf", fd->thumb_pixbuf,
2062                                         "text", name_sidecars,
2063                                         "marks", file_data_get_marks(fd),
2064                                         "show_marks", vf->marks_enabled,
2065                                         "cell-background-gdk", &color_bg,
2066                                         "cell-background-set", TRUE,
2067                                         "foreground-gdk", &color_fg,
2068                                         "foreground-set", TRUE,
2069                                         "has-focus", (VFICON(vf)->focus_fd == fd), NULL);
2070                 g_free(name_sidecars);
2071                 }
2072         else
2073                 {
2074                 g_object_set(cell,      "pixbuf", NULL,
2075                                         "text", NULL,
2076                                         "show_marks", FALSE,
2077                                         "cell-background-set", FALSE,
2078                                         "foreground-set", FALSE,
2079                                         "has-focus", FALSE, NULL);
2080                 }
2081 }
2082
2083 static void vficon_append_column(ViewFile *vf, gint n)
2084 {
2085         ColumnData *cd;
2086         GtkTreeViewColumn *column;
2087         GtkCellRenderer *renderer;
2088
2089         column = gtk_tree_view_column_new();
2090         gtk_tree_view_column_set_min_width(column, 0);
2091
2092         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2093         gtk_tree_view_column_set_alignment(column, 0.5);
2094
2095         renderer = gqv_cell_renderer_icon_new();
2096         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2097         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2098                                          "ypad", THUMB_BORDER_PADDING,
2099                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2100
2101         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2102         g_object_set_data(G_OBJECT(renderer), "column_number", GINT_TO_POINTER(n));
2103
2104         cd = g_new0(ColumnData, 1);
2105         cd->vf = vf;
2106         cd->number = n;
2107         gtk_tree_view_column_set_cell_data_func(column, renderer, vficon_cell_data_cb, cd, g_free);
2108
2109         gtk_tree_view_append_column(GTK_TREE_VIEW(vf->listview), column);
2110
2111         g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(vficon_mark_toggled_cb), vf);
2112 }
2113
2114 /*
2115  *-----------------------------------------------------------------------------
2116  * base
2117  *-----------------------------------------------------------------------------
2118  */
2119
2120 gboolean vficon_set_fd(ViewFile *vf, FileData *dir_fd)
2121 {
2122         gboolean ret;
2123
2124         if (!dir_fd) return FALSE;
2125         if (vf->dir_fd == dir_fd) return TRUE;
2126
2127         file_data_unref(vf->dir_fd);
2128         vf->dir_fd = file_data_ref(dir_fd);
2129
2130         g_list_free(VFICON(vf)->selection);
2131         VFICON(vf)->selection = NULL;
2132
2133         g_list_free(vf->list);
2134         vf->list = NULL;
2135
2136         /* NOTE: populate will clear the store for us */
2137         ret = vficon_refresh_real(vf, FALSE);
2138
2139         VFICON(vf)->focus_fd = NULL;
2140         vficon_move_focus(vf, 0, 0, FALSE);
2141
2142         return ret;
2143 }
2144
2145 void vficon_destroy_cb(GtkWidget *widget, gpointer data)
2146 {
2147         ViewFile *vf = data;
2148
2149         vf_refresh_idle_cancel(vf);
2150
2151         file_data_unregister_notify_func(vf_notify_cb, vf);
2152
2153         tip_unschedule(vf);
2154
2155         vf_thumb_cleanup(vf);
2156
2157         g_list_free(vf->list);
2158         g_list_free(VFICON(vf)->selection);
2159 }
2160
2161 ViewFile *vficon_new(ViewFile *vf, FileData *dir_fd)
2162 {
2163         GtkListStore *store;
2164         GtkTreeSelection *selection;
2165         gint i;
2166
2167         vf->info = g_new0(ViewFileInfoIcon, 1);
2168
2169         VFICON(vf)->show_text = options->show_icon_names;
2170
2171         store = gtk_list_store_new(1, G_TYPE_POINTER);
2172         vf->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2173         g_object_unref(store);
2174
2175         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vf->listview));
2176         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2177
2178         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vf->listview), FALSE);
2179         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vf->listview), FALSE);
2180
2181         for (i = 0; i < VFICON_MAX_COLUMNS; i++)
2182                 {
2183                 vficon_append_column(vf, i);
2184                 }
2185
2186         /* zero width column to hide tree view focus, we draw it ourselves */
2187         vficon_append_column(vf, i);
2188         /* end column to fill white space */
2189         vficon_append_column(vf, i);
2190
2191         g_signal_connect(G_OBJECT(vf->listview), "size_allocate",
2192                          G_CALLBACK(vficon_sized_cb), vf);
2193
2194         gtk_widget_set_events(vf->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2195                               GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2196
2197         g_signal_connect(G_OBJECT(vf->listview),"motion_notify_event",
2198                          G_CALLBACK(vficon_motion_cb), vf);
2199         g_signal_connect(G_OBJECT(vf->listview), "leave_notify_event",
2200                          G_CALLBACK(vficon_leave_cb), vf);
2201
2202         /* force VFICON(vf)->columns to be at least 1 (sane) - this will be corrected in the size_cb */
2203         vficon_populate_at_new_size(vf, 1, 1, FALSE);
2204
2205         file_data_register_notify_func(vf_notify_cb, vf, NOTIFY_PRIORITY_MEDIUM);
2206
2207         return vf;
2208 }
2209
2210 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */