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