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