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