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