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