Hopefully make it not crash
[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_fd) return NULL;
84
85         if (VFICON(vf)->click_fd->selected & SELECTION_SELECTED)
86                 {
87                 return vf_selection_get_list(vf);
88                 }
89
90         return vficon_selection_get_one(vf, VFICON(vf)->click_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         if (!g_list_find(vf->list, fd)) return;
887
888         if (!(fd->selected & SELECTION_SELECTED))
889                 {
890                 vf_select_none(vf);
891                 vficon_select(vf, fd);
892                 }
893
894         vficon_set_focus(vf, fd);
895 }
896
897 void vficon_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
898 {
899         GList *work;
900         gint n = mark - 1;
901
902         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
903
904         work = vf->list;
905         while (work)
906                 {
907                 FileData *fd = work->data;
908                 gboolean mark_val, selected;
909
910                 g_assert(fd->magick == FD_MAGICK);
911
912                 mark_val = file_data_get_mark(fd, n);
913                 selected = fd->selected & SELECTION_SELECTED;
914
915                 switch (mode)
916                         {
917                         case MTS_MODE_SET: selected = mark_val;
918                                 break;
919                         case MTS_MODE_OR: selected = mark_val || selected;
920                                 break;
921                         case MTS_MODE_AND: selected = mark_val && selected;
922                                 break;
923                         case MTS_MODE_MINUS: selected = !mark_val && selected;
924                                 break;
925                         }
926
927                 vficon_select_util(vf, fd, selected);
928
929                 work = work->next;
930                 }
931 }
932
933 void vficon_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
934 {
935         GList *slist;
936         GList *work;
937         gint n = mark -1;
938
939         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
940
941         slist = vf_selection_get_list(vf);
942         work = slist;
943         while (work)
944                 {
945                 FileData *fd = work->data;
946
947                 switch (mode)
948                         {
949                         case STM_MODE_SET: file_data_set_mark(fd, n, 1);
950                                 break;
951                         case STM_MODE_RESET: file_data_set_mark(fd, n, 0);
952                                 break;
953                         case STM_MODE_TOGGLE: file_data_set_mark(fd, n, !file_data_get_mark(fd, n));
954                                 break;
955                         }
956                 work = work->next;
957                 }
958         filelist_free(slist);
959 }
960
961 static void vficon_select_closest(ViewFile *vf, FileData *sel_fd)
962 {
963         GList *work;
964         FileData *fd = NULL;
965
966         if (sel_fd->parent) sel_fd = sel_fd->parent;
967         work = vf->list;
968
969         while (work)
970                 {
971                 gint match;
972
973                 fd = work->data;
974                 work = work->next;
975
976                 match = filelist_sort_compare_filedata_full(fd, sel_fd, vf->sort_method, vf->sort_ascend);
977
978                 if (match >= 0) break;
979                 }
980
981         if (fd)
982                 {
983                 vficon_select(vf, fd);
984                 vficon_send_layout_select(vf, fd);
985                 }
986 }
987
988
989 /*
990  *-------------------------------------------------------------------
991  * focus
992  *-------------------------------------------------------------------
993  */
994
995 static void vficon_move_focus(ViewFile *vf, gint row, gint col, gboolean relative)
996 {
997         gint new_row;
998         gint new_col;
999
1000         if (relative)
1001                 {
1002                 new_row = VFICON(vf)->focus_row;
1003                 new_col = VFICON(vf)->focus_column;
1004
1005                 new_row += row;
1006                 if (new_row < 0) new_row = 0;
1007                 if (new_row >= VFICON(vf)->rows) new_row = VFICON(vf)->rows - 1;
1008
1009                 while (col != 0)
1010                         {
1011                         if (col < 0)
1012                                 {
1013                                 new_col--;
1014                                 col++;
1015                                 }
1016                         else
1017                                 {
1018                                 new_col++;
1019                                 col--;
1020                                 }
1021
1022                         if (new_col < 0)
1023                                 {
1024                                 if (new_row > 0)
1025                                         {
1026                                         new_row--;
1027                                         new_col = VFICON(vf)->columns - 1;
1028                                         }
1029                                 else
1030                                         {
1031                                         new_col = 0;
1032                                         }
1033                                 }
1034                         if (new_col >= VFICON(vf)->columns)
1035                                 {
1036                                 if (new_row < VFICON(vf)->rows - 1)
1037                                         {
1038                                         new_row++;
1039                                         new_col = 0;
1040                                         }
1041                                 else
1042                                         {
1043                                         new_col = VFICON(vf)->columns - 1;
1044                                         }
1045                                 }
1046                         }
1047                 }
1048         else
1049                 {
1050                 new_row = row;
1051                 new_col = col;
1052
1053                 if (new_row >= VFICON(vf)->rows)
1054                         {
1055                         if (VFICON(vf)->rows > 0)
1056                                 new_row = VFICON(vf)->rows - 1;
1057                         else
1058                                 new_row = 0;
1059                         new_col = VFICON(vf)->columns - 1;
1060                         }
1061                 if (new_col >= VFICON(vf)->columns) new_col = VFICON(vf)->columns - 1;
1062                 }
1063
1064         if (new_row == VFICON(vf)->rows - 1)
1065                 {
1066                 gint l;
1067
1068                 /* if we moved beyond the last image, go to the last image */
1069
1070                 l = g_list_length(vf->list);
1071                 if (VFICON(vf)->rows > 1) l -= (VFICON(vf)->rows - 1) * VFICON(vf)->columns;
1072                 if (new_col >= l) new_col = l - 1;
1073                 }
1074
1075         vficon_set_focus(vf, vficon_find_data(vf, new_row, new_col, NULL));
1076 }
1077
1078 static void vficon_set_focus(ViewFile *vf, FileData *fd)
1079 {
1080         GtkTreeIter iter;
1081         gint row, col;
1082
1083         if (g_list_find(vf->list, VFICON(vf)->focus_fd))
1084                 {
1085                 if (fd == VFICON(vf)->focus_fd)
1086                         {
1087                         /* ensure focus row col are correct */
1088                         vficon_find_position(vf, VFICON(vf)->focus_fd, &VFICON(vf)->focus_row, &VFICON(vf)->focus_column);
1089 #if GTK_CHECK_VERSION(3,0,0)
1090 /* FIXME: Refer to issue #467 on Github. The thumbnail position is not
1091  * preserved when the icon view is refreshed. Caused by an unknown call from
1092  * the idle loop. This patch hides the problem.
1093  */
1094                         if (vficon_find_iter(vf, VFICON(vf)->focus_fd, &iter, NULL))
1095                                 {
1096                                 tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1097                                 }
1098 #endif
1099                         return;
1100                         }
1101                 vficon_selection_remove(vf, VFICON(vf)->focus_fd, SELECTION_FOCUS, NULL);
1102                 }
1103
1104         if (!vficon_find_position(vf, fd, &row, &col))
1105                 {
1106                 VFICON(vf)->focus_fd = NULL;
1107                 VFICON(vf)->focus_row = -1;
1108                 VFICON(vf)->focus_column = -1;
1109                 return;
1110                 }
1111
1112         VFICON(vf)->focus_fd = fd;
1113         VFICON(vf)->focus_row = row;
1114         VFICON(vf)->focus_column = col;
1115         vficon_selection_add(vf, VFICON(vf)->focus_fd, SELECTION_FOCUS, NULL);
1116
1117         if (vficon_find_iter(vf, VFICON(vf)->focus_fd, &iter, NULL))
1118                 {
1119                 GtkTreePath *tpath;
1120                 GtkTreeViewColumn *column;
1121                 GtkTreeModel *store;
1122
1123                 tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1124
1125                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1126                 tpath = gtk_tree_model_get_path(store, &iter);
1127                 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1128                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), VFICON_MAX_COLUMNS);
1129                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vf->listview), tpath, column, FALSE);
1130                 gtk_tree_path_free(tpath);
1131                 }
1132 }
1133
1134 /* used to figure the page up/down distances */
1135 static gint page_height(ViewFile *vf)
1136 {
1137         GtkAdjustment *adj;
1138         gint page_size;
1139         gint row_height;
1140         gint ret;
1141
1142         adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(vf->listview));
1143         page_size = (gint)gtk_adjustment_get_page_increment(adj);
1144
1145         row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1146         if (VFICON(vf)->show_text) row_height += options->thumbnails.max_height / 3;
1147
1148         ret = page_size / row_height;
1149         if (ret < 1) ret = 1;
1150
1151         return ret;
1152 }
1153
1154 /*
1155  *-------------------------------------------------------------------
1156  * keyboard
1157  *-------------------------------------------------------------------
1158  */
1159
1160 static void vfi_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1161 {
1162         ViewFile *vf = data;
1163         GtkTreeModel *store;
1164         GtkTreeIter iter;
1165         gint column;
1166         GtkTreePath *tpath;
1167         gint cw, ch;
1168
1169         if (!vficon_find_iter(vf, VFICON(vf)->click_fd, &iter, &column)) return;
1170         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1171         tpath = gtk_tree_model_get_path(store, &iter);
1172         tree_view_get_cell_clamped(GTK_TREE_VIEW(vf->listview), tpath, column, FALSE, x, y, &cw, &ch);
1173         gtk_tree_path_free(tpath);
1174         *y += ch;
1175         popup_menu_position_clamp(menu, x, y, 0);
1176 }
1177
1178 gboolean vficon_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1179 {
1180         ViewFile *vf = data;
1181         gint focus_row = 0;
1182         gint focus_col = 0;
1183         FileData *fd;
1184         gboolean stop_signal;
1185
1186         stop_signal = TRUE;
1187         switch (event->keyval)
1188                 {
1189                 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1190                         focus_col = -1;
1191                         break;
1192                 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1193                         focus_col = 1;
1194                         break;
1195                 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1196                         focus_row = -1;
1197                         break;
1198                 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1199                         focus_row = 1;
1200                         break;
1201                 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1202                         focus_row = -page_height(vf);
1203                         break;
1204                 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1205                         focus_row = page_height(vf);
1206                         break;
1207                 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1208                         focus_row = -VFICON(vf)->focus_row;
1209                         focus_col = -VFICON(vf)->focus_column;
1210                         break;
1211                 case GDK_KEY_End: case GDK_KEY_KP_End:
1212                         focus_row = VFICON(vf)->rows - 1 - VFICON(vf)->focus_row;
1213                         focus_col = VFICON(vf)->columns - 1 - VFICON(vf)->focus_column;
1214                         break;
1215                 case GDK_KEY_space:
1216                         fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, NULL);
1217                         if (fd)
1218                                 {
1219                                 VFICON(vf)->click_fd = fd;
1220                                 if (event->state & GDK_CONTROL_MASK)
1221                                         {
1222                                         gint selected;
1223
1224                                         selected = fd->selected & SELECTION_SELECTED;
1225                                         if (selected)
1226                                                 {
1227                                                 vficon_unselect(vf, fd);
1228                                                 }
1229                                         else
1230                                                 {
1231                                                 vficon_select(vf, fd);
1232                                                 vficon_send_layout_select(vf, fd);
1233                                                 }
1234                                         }
1235                                 else
1236                                         {
1237                                         vf_select_none(vf);
1238                                         vficon_select(vf, fd);
1239                                         vficon_send_layout_select(vf, fd);
1240                                         }
1241                                 }
1242                         break;
1243                 case GDK_KEY_Menu:
1244                         fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, NULL);
1245                         VFICON(vf)->click_fd = fd;
1246
1247                         vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, NULL);
1248                         tip_unschedule(vf);
1249
1250                         vf->popup = vf_pop_menu(vf);
1251                         gtk_menu_popup(GTK_MENU(vf->popup), NULL, NULL, vfi_menu_position_cb, vf, 0, GDK_CURRENT_TIME);
1252                         break;
1253                 default:
1254                         stop_signal = FALSE;
1255                         break;
1256                 }
1257
1258         if (focus_row != 0 || focus_col != 0)
1259                 {
1260                 FileData *new_fd;
1261                 FileData *old_fd;
1262
1263                 old_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, NULL);
1264                 vficon_move_focus(vf, focus_row, focus_col, TRUE);
1265                 new_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, NULL);
1266
1267                 if (new_fd != old_fd)
1268                         {
1269                         if (event->state & GDK_SHIFT_MASK)
1270                                 {
1271                                 if (!options->collections.rectangular_selection)
1272                                         {
1273                                         vficon_select_region_util(vf, old_fd, new_fd, FALSE);
1274                                         }
1275                                 else
1276                                         {
1277                                         vficon_select_region_util(vf, VFICON(vf)->click_fd, old_fd, FALSE);
1278                                         }
1279                                 vficon_select_region_util(vf, VFICON(vf)->click_fd, new_fd, TRUE);
1280                                 vficon_send_layout_select(vf, new_fd);
1281                                 }
1282                         else if (event->state & GDK_CONTROL_MASK)
1283                                 {
1284                                 VFICON(vf)->click_fd = new_fd;
1285                                 }
1286                         else
1287                                 {
1288                                 VFICON(vf)->click_fd = new_fd;
1289                                 vf_select_none(vf);
1290                                 vficon_select(vf, new_fd);
1291                                 vficon_send_layout_select(vf, new_fd);
1292                                 }
1293                         }
1294                 }
1295
1296         if (stop_signal)
1297                 {
1298                 tip_unschedule(vf);
1299                 }
1300
1301         return stop_signal;
1302 }
1303
1304 /*
1305  *-------------------------------------------------------------------
1306  * mouse
1307  *-------------------------------------------------------------------
1308  */
1309
1310 static gboolean vficon_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1311 {
1312         ViewFile *vf = data;
1313         FileData *fd;
1314
1315         fd = vficon_find_data_by_coord(vf, (gint)event->x, (gint)event->y, NULL);
1316         tip_update(vf, fd);
1317
1318         return FALSE;
1319 }
1320
1321 gboolean vficon_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1322 {
1323         ViewFile *vf = data;
1324         GtkTreeIter iter;
1325         FileData *fd;
1326
1327         tip_unschedule(vf);
1328
1329         fd = vficon_find_data_by_coord(vf, (gint)bevent->x, (gint)bevent->y, &iter);
1330
1331         VFICON(vf)->click_fd = fd;
1332         vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1333
1334         switch (bevent->button)
1335                 {
1336                 case MOUSE_BUTTON_LEFT:
1337                         if (!gtk_widget_has_focus(vf->listview))
1338                                 {
1339                                 gtk_widget_grab_focus(vf->listview);
1340                                 }
1341
1342                         if (bevent->type == GDK_2BUTTON_PRESS && vf->layout)
1343                                 {
1344                                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1345                                 layout_image_full_screen_start(vf->layout);
1346                                 }
1347                         break;
1348                 case MOUSE_BUTTON_RIGHT:
1349                         vf->popup = vf_pop_menu(vf);
1350                         gtk_menu_popup(GTK_MENU(vf->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1351                         break;
1352                 default:
1353                         break;
1354                 }
1355
1356         return FALSE;
1357 }
1358
1359 gboolean vficon_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1360 {
1361         ViewFile *vf = data;
1362         GtkTreeIter iter;
1363         FileData *fd = NULL;
1364         gboolean was_selected;
1365
1366         tip_schedule(vf);
1367
1368         if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1369                 {
1370                 fd = vficon_find_data_by_coord(vf, (gint)bevent->x, (gint)bevent->y, &iter);
1371                 }
1372
1373         if (VFICON(vf)->click_fd)
1374                 {
1375                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, NULL);
1376                 }
1377
1378         if (!fd || VFICON(vf)->click_fd != fd) return TRUE;
1379
1380         was_selected = !!(fd->selected & SELECTION_SELECTED);
1381
1382         switch (bevent->button)
1383                 {
1384                 case MOUSE_BUTTON_LEFT:
1385                         {
1386                         vficon_set_focus(vf, fd);
1387
1388                         if (bevent->state & GDK_CONTROL_MASK)
1389                                 {
1390                                 gboolean select;
1391
1392                                 select = !(fd->selected & SELECTION_SELECTED);
1393                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1394                                         {
1395                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, select);
1396                                         }
1397                                 else
1398                                         {
1399                                         vficon_select_util(vf, fd, select);
1400                                         }
1401                                 }
1402                         else
1403                                 {
1404                                 vf_select_none(vf);
1405
1406                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1407                                         {
1408                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, TRUE);
1409                                         }
1410                                 else
1411                                         {
1412                                         vficon_select_util(vf, fd, TRUE);
1413                                         was_selected = FALSE;
1414                                         }
1415                                 }
1416                         }
1417                         break;
1418                 case MOUSE_BUTTON_MIDDLE:
1419                         {
1420                         vficon_select_util(vf, fd, !(fd->selected & SELECTION_SELECTED));
1421                         }
1422                         break;
1423                 default:
1424                         break;
1425                 }
1426
1427         if (!was_selected && (fd->selected & SELECTION_SELECTED))
1428                 {
1429                 vficon_send_layout_select(vf, fd);
1430                 }
1431
1432         return TRUE;
1433 }
1434
1435 static gboolean vficon_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1436 {
1437         ViewFile *vf = data;
1438
1439         tip_unschedule(vf);
1440         return FALSE;
1441 }
1442
1443 /*
1444  *-------------------------------------------------------------------
1445  * population
1446  *-------------------------------------------------------------------
1447  */
1448
1449 static gboolean vficon_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1450 {
1451         GList *list;
1452
1453         gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
1454
1455         /* it seems that gtk_list_store_clear may call some callbacks
1456            that use the column. Set the pointer to NULL to be safe. */
1457         gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, NULL, -1);
1458         g_list_free(list);
1459
1460         return FALSE;
1461 }
1462
1463 static void vficon_clear_store(ViewFile *vf)
1464 {
1465         GtkTreeModel *store;
1466
1467         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1468         gtk_tree_model_foreach(store, vficon_destroy_node_cb, NULL);
1469
1470         gtk_list_store_clear(GTK_LIST_STORE(store));
1471 }
1472
1473 static GList *vficon_add_row(ViewFile *vf, GtkTreeIter *iter)
1474 {
1475         GtkListStore *store;
1476         GList *list = NULL;
1477         gint i;
1478
1479         for (i = 0; i < VFICON(vf)->columns; i++) list = g_list_prepend(list, NULL);
1480
1481         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview)));
1482         gtk_list_store_append(store, iter);
1483         gtk_list_store_set(store, iter, FILE_COLUMN_POINTER, list, -1);
1484
1485         return list;
1486 }
1487
1488 static void vficon_populate(ViewFile *vf, gboolean resize, gboolean keep_position)
1489 {
1490         GtkTreeModel *store;
1491         GtkTreePath *tpath;
1492         GList *work;
1493         FileData *visible_fd = NULL;
1494         gint r, c;
1495         gboolean valid;
1496         GtkTreeIter iter;
1497
1498         vficon_verify_selections(vf);
1499
1500         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1501
1502         if (keep_position && gtk_widget_get_realized(vf->listview) &&
1503             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, NULL, NULL, NULL))
1504                 {
1505                 GtkTreeIter iter;
1506                 GList *list;
1507
1508                 gtk_tree_model_get_iter(store, &iter, tpath);
1509                 gtk_tree_path_free(tpath);
1510
1511                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1512                 if (list) visible_fd = list->data;
1513                 }
1514
1515
1516         if (resize)
1517                 {
1518                 gint i;
1519                 gint thumb_width;
1520
1521                 vficon_clear_store(vf);
1522
1523                 thumb_width = vficon_get_icon_width(vf);
1524
1525                 for (i = 0; i < VFICON_MAX_COLUMNS; i++)
1526                         {
1527                         GtkTreeViewColumn *column;
1528                         GtkCellRenderer *cell;
1529                         GList *list;
1530
1531                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), i);
1532                         gtk_tree_view_column_set_visible(column, (i < VFICON(vf)->columns));
1533                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1534
1535                         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1536                         cell = (list) ? list->data : NULL;
1537                         g_list_free(list);
1538
1539                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1540                                 {
1541                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1542                                                              "fixed_height", options->thumbnails.max_height,
1543                                                              "show_text", VFICON(vf)->show_text,
1544                                                              "show_marks", vf->marks_enabled,
1545                                                              "num_marks", FILEDATA_MARKS_SIZE,
1546                                                              NULL);
1547                                 }
1548                         }
1549                 if (gtk_widget_get_realized(vf->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(vf->listview));
1550                 }
1551
1552         r = -1;
1553         c = 0;
1554
1555         valid = gtk_tree_model_iter_children(store, &iter, NULL);
1556
1557         work = vf->list;
1558         while (work)
1559                 {
1560                 GList *list;
1561                 r++;
1562                 c = 0;
1563                 if (valid)
1564                         {
1565                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1566                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1567                         }
1568                 else
1569                         {
1570                         list = vficon_add_row(vf, &iter);
1571                         }
1572
1573                 while (list)
1574                         {
1575                         FileData *fd;
1576
1577                         if (work)
1578                                 {
1579                                 fd = work->data;
1580                                 work = work->next;
1581                                 c++;
1582                                 }
1583                         else
1584                                 {
1585                                 fd = NULL;
1586                                 }
1587
1588                         list->data = fd;
1589                         list = list->next;
1590                         }
1591                 if (valid) valid = gtk_tree_model_iter_next(store, &iter);
1592                 }
1593
1594         r++;
1595         while (valid)
1596                 {
1597                 GList *list;
1598
1599                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1600                 valid = gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1601                 g_list_free(list);
1602                 }
1603
1604         VFICON(vf)->rows = r;
1605
1606         if (visible_fd &&
1607             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, NULL, NULL, NULL))
1608                 {
1609                 GtkTreeIter iter;
1610                 GList *list;
1611
1612                 gtk_tree_model_get_iter(store, &iter, tpath);
1613                 gtk_tree_path_free(tpath);
1614
1615                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1616                 if (g_list_find(list, visible_fd) == NULL &&
1617                     vficon_find_iter(vf, visible_fd, &iter, NULL))
1618                         {
1619                         tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1620                         }
1621                 }
1622
1623
1624         vf_send_update(vf);
1625         vf_thumb_update(vf);
1626 }
1627
1628 static void vficon_populate_at_new_size(ViewFile *vf, gint w, gint h, gboolean force)
1629 {
1630         gint new_cols;
1631         gint thumb_width;
1632
1633         thumb_width = vficon_get_icon_width(vf);
1634
1635         new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1636         if (new_cols < 1) new_cols = 1;
1637
1638         if (!force && new_cols == VFICON(vf)->columns) return;
1639
1640         VFICON(vf)->columns = new_cols;
1641
1642         vficon_populate(vf, TRUE, TRUE);
1643
1644         DEBUG_1("col tab pop cols=%d rows=%d", VFICON(vf)->columns, VFICON(vf)->rows);
1645 }
1646
1647 static void vficon_sized_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
1648 {
1649         ViewFile *vf = data;
1650
1651         vficon_populate_at_new_size(vf, allocation->width, allocation->height, FALSE);
1652 }
1653
1654 /*
1655  *-----------------------------------------------------------------------------
1656  * misc
1657  *-----------------------------------------------------------------------------
1658  */
1659
1660 void vficon_sort_set(ViewFile *vf, SortType type, gboolean ascend)
1661 {
1662         if (vf->sort_method == type && vf->sort_ascend == ascend) return;
1663
1664         vf->sort_method = type;
1665         vf->sort_ascend = ascend;
1666
1667         if (!vf->list) return;
1668
1669         vf_refresh(vf);
1670 }
1671
1672 /*
1673  *-----------------------------------------------------------------------------
1674  * thumb updates
1675  *-----------------------------------------------------------------------------
1676  */
1677
1678 void vficon_thumb_progress_count(GList *list, gint *count, gint *done)
1679 {
1680         GList *work = list;
1681         while (work)
1682                 {
1683                 FileData *fd = work->data;
1684                 work = work->next;
1685
1686                 if (fd->thumb_pixbuf) (*done)++;
1687                 (*count)++;
1688                 }
1689 }
1690
1691 void vficon_set_thumb_fd(ViewFile *vf, FileData *fd)
1692 {
1693         GtkTreeModel *store;
1694         GtkTreeIter iter;
1695         GList *list;
1696
1697         if (!g_list_find(vf->list, fd)) return;
1698         if (!vficon_find_iter(vf, 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         FileData *first_selected = NULL;
1863         GList *new_filelist = NULL;
1864         GList *new_fd_list = NULL;
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         /* iterate old list and new list, looking for differences */
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                         /* old item was deleted */
1917                         fd = work->data;
1918                         match = -1;
1919                         }
1920                 else
1921                         {
1922                         /* new item was added */
1923                         new_fd = new_work->data;
1924                         match = 1;
1925                         }
1926
1927                 if (match < 0)
1928                         {
1929                         /* file no longer exists, delete from vf->list */
1930                         GList *to_delete = work;
1931                         work = work->next;
1932                         if (fd == VFICON(vf)->prev_selection) VFICON(vf)->prev_selection = NULL;
1933                         if (fd == VFICON(vf)->click_fd) VFICON(vf)->click_fd = NULL;
1934                         file_data_unref(fd);
1935                         vf->list = g_list_delete_link(vf->list, to_delete);
1936                         }
1937                 else
1938                         {
1939                         /* new file, add to vf->list */
1940                         file_data_ref(new_fd);
1941                         new_fd->selected = SELECTION_NONE;
1942                         if (work)
1943                                 {
1944                                 vf->list = g_list_insert_before(vf->list, work, new_fd);
1945                                 }
1946                         else
1947                                 {
1948                                 /* it is faster to append all new entries together later */
1949                                 new_fd_list = g_list_prepend(new_fd_list, new_fd);
1950                                 }
1951
1952                         new_work = new_work->next;
1953                         }
1954                 }
1955
1956         if (new_fd_list)
1957                 {
1958                 vf->list = g_list_concat(vf->list, g_list_reverse(new_fd_list));
1959                 }
1960
1961         VFICON(vf)->selection = g_list_reverse(VFICON(vf)->selection);
1962
1963         filelist_free(new_filelist);
1964
1965         vficon_populate(vf, TRUE, keep_position);
1966
1967         if (first_selected && !VFICON(vf)->selection)
1968                 {
1969                 /* all selected files disappeared */
1970                 vficon_select_closest(vf, first_selected);
1971                 }
1972         file_data_unref(first_selected);
1973
1974         /* attempt to keep focus on same icon when refreshing */
1975         if (focus_fd && g_list_find(vf->list, focus_fd))
1976                 {
1977                 vficon_set_focus(vf, focus_fd);
1978                 }
1979
1980         return ret;
1981 }
1982
1983 gboolean vficon_refresh(ViewFile *vf)
1984 {
1985         return vficon_refresh_real(vf, TRUE);
1986 }
1987
1988 /*
1989  *-----------------------------------------------------------------------------
1990  * draw, etc.
1991  *-----------------------------------------------------------------------------
1992  */
1993
1994 typedef struct _ColumnData ColumnData;
1995 struct _ColumnData
1996 {
1997         ViewFile *vf;
1998         gint number;
1999 };
2000
2001 static void vficon_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2002                                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2003 {
2004         GList *list;
2005         FileData *fd;
2006         ColumnData *cd = data;
2007         ViewFile *vf = cd->vf;
2008
2009         if (!GQV_IS_CELL_RENDERER_ICON(cell)) return;
2010
2011         gtk_tree_model_get(tree_model, iter, FILE_COLUMN_POINTER, &list, -1);
2012
2013         fd = g_list_nth_data(list, cd->number);
2014
2015         if (fd)
2016                 {
2017                 GdkColor color_fg;
2018                 GdkColor color_bg;
2019                 GtkStyle *style;
2020                 gchar *name_sidecars;
2021                 gchar *link;
2022                 GtkStateType state = GTK_STATE_NORMAL;
2023
2024                 g_assert(fd->magick == FD_MAGICK);
2025
2026                 link = islink(fd->path) ? GQ_LINK_STR : "";
2027                 if (fd->sidecar_files)
2028                         {
2029                         gchar *sidecars = file_data_sc_list_to_string(fd);
2030                         name_sidecars = g_strdup_printf("%s%s %s", link, fd->name, sidecars);
2031                         g_free(sidecars);
2032                         }
2033                 else
2034                         {
2035                         gchar *disabled_grouping = fd->disable_grouping ? _(" [NO GROUPING]") : "";
2036                         name_sidecars = g_strdup_printf("%s%s%s", link, fd->name, disabled_grouping);
2037                         }
2038
2039                 style = gtk_widget_get_style(vf->listview);
2040                 if (fd->selected & SELECTION_SELECTED)
2041                         {
2042                         state = GTK_STATE_SELECTED;
2043                         }
2044
2045                 memcpy(&color_fg, &style->text[state], sizeof(color_fg));
2046                 memcpy(&color_bg, &style->base[state], sizeof(color_bg));
2047
2048                 if (fd->selected & SELECTION_PRELIGHT)
2049                         {
2050                         shift_color(&color_bg, -1, 0);
2051                         }
2052
2053                 g_object_set(cell,      "pixbuf", fd->thumb_pixbuf,
2054                                         "text", name_sidecars,
2055                                         "marks", file_data_get_marks(fd),
2056                                         "show_marks", vf->marks_enabled,
2057                                         "cell-background-gdk", &color_bg,
2058                                         "cell-background-set", TRUE,
2059                                         "foreground-gdk", &color_fg,
2060                                         "foreground-set", TRUE,
2061                                         "has-focus", (VFICON(vf)->focus_fd == fd), NULL);
2062                 g_free(name_sidecars);
2063                 }
2064         else
2065                 {
2066                 g_object_set(cell,      "pixbuf", NULL,
2067                                         "text", NULL,
2068                                         "show_marks", FALSE,
2069                                         "cell-background-set", FALSE,
2070                                         "foreground-set", FALSE,
2071                                         "has-focus", FALSE, NULL);
2072                 }
2073 }
2074
2075 static void vficon_append_column(ViewFile *vf, gint n)
2076 {
2077         ColumnData *cd;
2078         GtkTreeViewColumn *column;
2079         GtkCellRenderer *renderer;
2080
2081         column = gtk_tree_view_column_new();
2082         gtk_tree_view_column_set_min_width(column, 0);
2083
2084         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2085         gtk_tree_view_column_set_alignment(column, 0.5);
2086
2087         renderer = gqv_cell_renderer_icon_new();
2088         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2089         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2090                                          "ypad", THUMB_BORDER_PADDING,
2091                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2092
2093         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2094         g_object_set_data(G_OBJECT(renderer), "column_number", GINT_TO_POINTER(n));
2095
2096         cd = g_new0(ColumnData, 1);
2097         cd->vf = vf;
2098         cd->number = n;
2099         gtk_tree_view_column_set_cell_data_func(column, renderer, vficon_cell_data_cb, cd, g_free);
2100
2101         gtk_tree_view_append_column(GTK_TREE_VIEW(vf->listview), column);
2102
2103         g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(vficon_mark_toggled_cb), vf);
2104 }
2105
2106 /*
2107  *-----------------------------------------------------------------------------
2108  * base
2109  *-----------------------------------------------------------------------------
2110  */
2111
2112 gboolean vficon_set_fd(ViewFile *vf, FileData *dir_fd)
2113 {
2114         gboolean ret;
2115
2116         if (!dir_fd) return FALSE;
2117         if (vf->dir_fd == dir_fd) return TRUE;
2118
2119         file_data_unref(vf->dir_fd);
2120         vf->dir_fd = file_data_ref(dir_fd);
2121
2122         g_list_free(VFICON(vf)->selection);
2123         VFICON(vf)->selection = NULL;
2124
2125         g_list_free(vf->list);
2126         vf->list = NULL;
2127
2128         /* NOTE: populate will clear the store for us */
2129         ret = vficon_refresh_real(vf, FALSE);
2130
2131         VFICON(vf)->focus_fd = NULL;
2132         vficon_move_focus(vf, 0, 0, FALSE);
2133
2134         return ret;
2135 }
2136
2137 void vficon_destroy_cb(GtkWidget *widget, gpointer data)
2138 {
2139         ViewFile *vf = data;
2140
2141         vf_refresh_idle_cancel(vf);
2142
2143         file_data_unregister_notify_func(vf_notify_cb, vf);
2144
2145         tip_unschedule(vf);
2146
2147         vf_thumb_cleanup(vf);
2148
2149         g_list_free(vf->list);
2150         g_list_free(VFICON(vf)->selection);
2151 }
2152
2153 ViewFile *vficon_new(ViewFile *vf, FileData *dir_fd)
2154 {
2155         GtkListStore *store;
2156         GtkTreeSelection *selection;
2157         gint i;
2158
2159         vf->info = g_new0(ViewFileInfoIcon, 1);
2160
2161         VFICON(vf)->show_text = options->show_icon_names;
2162
2163         store = gtk_list_store_new(1, G_TYPE_POINTER);
2164         vf->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2165         g_object_unref(store);
2166
2167         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vf->listview));
2168         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2169
2170         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vf->listview), FALSE);
2171         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vf->listview), FALSE);
2172
2173         for (i = 0; i < VFICON_MAX_COLUMNS; i++)
2174                 {
2175                 vficon_append_column(vf, i);
2176                 }
2177
2178         /* zero width column to hide tree view focus, we draw it ourselves */
2179         vficon_append_column(vf, i);
2180         /* end column to fill white space */
2181         vficon_append_column(vf, i);
2182
2183         g_signal_connect(G_OBJECT(vf->listview), "size_allocate",
2184                          G_CALLBACK(vficon_sized_cb), vf);
2185
2186         gtk_widget_set_events(vf->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2187                               GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2188
2189         g_signal_connect(G_OBJECT(vf->listview),"motion_notify_event",
2190                          G_CALLBACK(vficon_motion_cb), vf);
2191         g_signal_connect(G_OBJECT(vf->listview), "leave_notify_event",
2192                          G_CALLBACK(vficon_leave_cb), vf);
2193
2194         /* force VFICON(vf)->columns to be at least 1 (sane) - this will be corrected in the size_cb */
2195         vficon_populate_at_new_size(vf, 1, 1, FALSE);
2196
2197         file_data_register_notify_func(vf_notify_cb, vf, NOTIFY_PRIORITY_MEDIUM);
2198
2199         return vf;
2200 }
2201
2202 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */