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