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