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