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