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