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