e3ab038ef1e762f2bdf7420cd1d13174addd4427
[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                                         // At least two items selected; go for it.
1265                                         g_warning("Had requisite number of selection items; going for it!");
1266                                         fileclusterlist_create_cluster(vf->cluster_list, VFICON(vf)->selection);
1267                                         }
1268                                 else
1269                                         {
1270                                         if (VFICON(vf)->selection)
1271                                                 {
1272                                                 g_warning("Only one item selected; need at least two.");
1273                                                 }
1274                                         else
1275                                                 {
1276                                                 g_warning("No items selected; need at least two.");
1277                                                 }
1278                                         }
1279                                 }
1280                         break;
1281                 default:
1282                         stop_signal = FALSE;
1283                         break;
1284                 }
1285
1286         if (focus_row != 0 || focus_col != 0)
1287                 {
1288                 FileData *new_fd;
1289                 FileData *old_fd;
1290
1291                 old_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, NULL);
1292                 vficon_move_focus(vf, focus_row, focus_col, TRUE);
1293                 new_fd = vficon_find_data(vf, VFICON(vf)->focus_row, VFICON(vf)->focus_column, NULL);
1294
1295                 if (new_fd != old_fd)
1296                         {
1297                         if (event->state & GDK_SHIFT_MASK)
1298                                 {
1299                                 if (!options->collections.rectangular_selection)
1300                                         {
1301                                         vficon_select_region_util(vf, old_fd, new_fd, FALSE);
1302                                         }
1303                                 else
1304                                         {
1305                                         vficon_select_region_util(vf, VFICON(vf)->click_fd, old_fd, FALSE);
1306                                         }
1307                                 vficon_select_region_util(vf, VFICON(vf)->click_fd, new_fd, TRUE);
1308                                 vficon_send_layout_select(vf, new_fd);
1309                                 }
1310                         else if (event->state & GDK_CONTROL_MASK)
1311                                 {
1312                                 VFICON(vf)->click_fd = new_fd;
1313                                 }
1314                         else
1315                                 {
1316                                 VFICON(vf)->click_fd = new_fd;
1317                                 vf_select_none(vf);
1318                                 vficon_select(vf, new_fd);
1319                                 vficon_send_layout_select(vf, new_fd);
1320                                 }
1321                         }
1322                 }
1323
1324         if (stop_signal)
1325                 {
1326                 tip_unschedule(vf);
1327                 }
1328
1329         return stop_signal;
1330 }
1331
1332 /*
1333  *-------------------------------------------------------------------
1334  * mouse
1335  *-------------------------------------------------------------------
1336  */
1337
1338 static gboolean vficon_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1339 {
1340         ViewFile *vf = data;
1341         FileData *fd;
1342
1343         fd = vficon_find_data_by_coord(vf, (gint)event->x, (gint)event->y, NULL);
1344         tip_update(vf, fd);
1345
1346         return FALSE;
1347 }
1348
1349 gboolean vficon_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1350 {
1351         ViewFile *vf = data;
1352         GtkTreeIter iter;
1353         FileData *fd;
1354
1355         tip_unschedule(vf);
1356
1357         fd = vficon_find_data_by_coord(vf, (gint)bevent->x, (gint)bevent->y, &iter);
1358
1359         VFICON(vf)->click_fd = fd;
1360         vficon_selection_add(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1361
1362         switch (bevent->button)
1363                 {
1364                 case MOUSE_BUTTON_LEFT:
1365                         if (!gtk_widget_has_focus(vf->listview))
1366                                 {
1367                                 gtk_widget_grab_focus(vf->listview);
1368                                 }
1369
1370                         if (bevent->type == GDK_2BUTTON_PRESS && vf->layout)
1371                                 {
1372                                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, &iter);
1373                                 layout_image_full_screen_start(vf->layout);
1374                                 }
1375                         break;
1376                 case MOUSE_BUTTON_RIGHT:
1377                         vf->popup = vf_pop_menu(vf);
1378                         gtk_menu_popup(GTK_MENU(vf->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1379                         break;
1380                 default:
1381                         break;
1382                 }
1383
1384         return FALSE;
1385 }
1386
1387 gboolean vficon_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1388 {
1389         ViewFile *vf = data;
1390         GtkTreeIter iter;
1391         FileData *fd = NULL;
1392         gboolean was_selected;
1393
1394         tip_schedule(vf);
1395
1396         if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1397                 {
1398                 fd = vficon_find_data_by_coord(vf, (gint)bevent->x, (gint)bevent->y, &iter);
1399                 }
1400
1401         if (VFICON(vf)->click_fd)
1402                 {
1403                 vficon_selection_remove(vf, VFICON(vf)->click_fd, SELECTION_PRELIGHT, NULL);
1404                 }
1405
1406         if (!fd || VFICON(vf)->click_fd != fd) return TRUE;
1407
1408         was_selected = !!(fd->selected & SELECTION_SELECTED);
1409
1410         switch (bevent->button)
1411                 {
1412                 case MOUSE_BUTTON_LEFT:
1413                         {
1414                         vficon_set_focus(vf, fd);
1415
1416                         if (bevent->state & GDK_CONTROL_MASK)
1417                                 {
1418                                 gboolean select;
1419
1420                                 select = !(fd->selected & SELECTION_SELECTED);
1421                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1422                                         {
1423                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, select);
1424                                         }
1425                                 else
1426                                         {
1427                                         vficon_select_util(vf, fd, select);
1428                                         }
1429                                 }
1430                         else
1431                                 {
1432                                 vf_select_none(vf);
1433
1434                                 if ((bevent->state & GDK_SHIFT_MASK) && VFICON(vf)->prev_selection)
1435                                         {
1436                                         vficon_select_region_util(vf, VFICON(vf)->prev_selection, fd, TRUE);
1437                                         }
1438                                 else
1439                                         {
1440                                         vficon_select_util(vf, fd, TRUE);
1441                                         was_selected = FALSE;
1442                                         }
1443                                 }
1444                         }
1445                         break;
1446                 case MOUSE_BUTTON_MIDDLE:
1447                         {
1448                         vficon_select_util(vf, fd, !(fd->selected & SELECTION_SELECTED));
1449                         }
1450                         break;
1451                 default:
1452                         break;
1453                 }
1454
1455         if (!was_selected && (fd->selected & SELECTION_SELECTED))
1456                 {
1457                 vficon_send_layout_select(vf, fd);
1458                 }
1459
1460         return TRUE;
1461 }
1462
1463 static gboolean vficon_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1464 {
1465         ViewFile *vf = data;
1466
1467         tip_unschedule(vf);
1468         return FALSE;
1469 }
1470
1471 /*
1472  *-------------------------------------------------------------------
1473  * population
1474  *-------------------------------------------------------------------
1475  */
1476
1477 static gboolean vficon_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1478 {
1479         GList *list;
1480
1481         gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
1482
1483         /* it seems that gtk_list_store_clear may call some callbacks
1484            that use the column. Set the pointer to NULL to be safe. */
1485         gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, NULL, -1);
1486         g_list_free(list);
1487
1488         return FALSE;
1489 }
1490
1491 static void vficon_clear_store(ViewFile *vf)
1492 {
1493         GtkTreeModel *store;
1494
1495         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1496         gtk_tree_model_foreach(store, vficon_destroy_node_cb, NULL);
1497
1498         gtk_list_store_clear(GTK_LIST_STORE(store));
1499 }
1500
1501 static GList *vficon_add_row(ViewFile *vf, GtkTreeIter *iter)
1502 {
1503         GtkListStore *store;
1504         GList *list = NULL;
1505         gint i;
1506
1507         for (i = 0; i < VFICON(vf)->columns; i++) list = g_list_prepend(list, NULL);
1508
1509         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview)));
1510         gtk_list_store_append(store, iter);
1511         gtk_list_store_set(store, iter, FILE_COLUMN_POINTER, list, -1);
1512
1513         return list;
1514 }
1515
1516 static void vficon_populate(ViewFile *vf, gboolean resize, gboolean keep_position)
1517 {
1518         GtkTreeModel *store;
1519         GtkTreePath *tpath;
1520         GList *work;
1521         FileData *visible_fd = NULL;
1522         gint r, c;
1523         gboolean valid;
1524         GtkTreeIter iter;
1525
1526         vficon_verify_selections(vf);
1527
1528         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1529
1530         if (keep_position && gtk_widget_get_realized(vf->listview) &&
1531             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, NULL, NULL, NULL))
1532                 {
1533                 GtkTreeIter iter;
1534                 GList *list;
1535
1536                 gtk_tree_model_get_iter(store, &iter, tpath);
1537                 gtk_tree_path_free(tpath);
1538
1539                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1540                 if (list) visible_fd = list->data;
1541                 }
1542
1543
1544         if (resize)
1545                 {
1546                 gint i;
1547                 gint thumb_width;
1548
1549                 vficon_clear_store(vf);
1550
1551                 thumb_width = vficon_get_icon_width(vf);
1552
1553                 for (i = 0; i < VFICON_MAX_COLUMNS; i++)
1554                         {
1555                         GtkTreeViewColumn *column;
1556                         GtkCellRenderer *cell;
1557                         GList *list;
1558
1559                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(vf->listview), i);
1560                         gtk_tree_view_column_set_visible(column, (i < VFICON(vf)->columns));
1561                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1562
1563                         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1564                         cell = (list) ? list->data : NULL;
1565                         g_list_free(list);
1566
1567                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1568                                 {
1569                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1570                                                              "fixed_height", options->thumbnails.max_height,
1571                                                              "show_text", VFICON(vf)->show_text,
1572                                                              "show_marks", vf->marks_enabled,
1573                                                              "num_marks", FILEDATA_MARKS_SIZE,
1574                                                              NULL);
1575                                 }
1576                         }
1577                 if (gtk_widget_get_realized(vf->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(vf->listview));
1578                 }
1579
1580         r = -1;
1581         c = 0;
1582
1583         valid = gtk_tree_model_iter_children(store, &iter, NULL);
1584
1585         work = vf->list;
1586         while (work)
1587                 {
1588                 GList *list;
1589                 r++;
1590                 c = 0;
1591                 if (valid)
1592                         {
1593                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1594                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1595                         }
1596                 else
1597                         {
1598                         list = vficon_add_row(vf, &iter);
1599                         }
1600
1601                 while (list)
1602                         {
1603                         FileData *fd;
1604
1605                         if (work)
1606                                 {
1607                                 fd = work->data;
1608                                 work = work->next;
1609                                 c++;
1610                                 }
1611                         else
1612                                 {
1613                                 fd = NULL;
1614                                 }
1615
1616                         list->data = fd;
1617                         list = list->next;
1618                         }
1619                 if (valid) valid = gtk_tree_model_iter_next(store, &iter);
1620                 }
1621
1622         r++;
1623         while (valid)
1624                 {
1625                 GList *list;
1626
1627                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1628                 valid = gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1629                 g_list_free(list);
1630                 }
1631
1632         VFICON(vf)->rows = r;
1633
1634         if (visible_fd &&
1635             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, NULL, NULL, NULL))
1636                 {
1637                 GtkTreeIter iter;
1638                 GList *list;
1639
1640                 gtk_tree_model_get_iter(store, &iter, tpath);
1641                 gtk_tree_path_free(tpath);
1642
1643                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1644                 if (g_list_find(list, visible_fd) == NULL &&
1645                     vficon_find_iter(vf, visible_fd, &iter, NULL))
1646                         {
1647                         tree_view_row_make_visible(GTK_TREE_VIEW(vf->listview), &iter, FALSE);
1648                         }
1649                 }
1650
1651
1652         vf_send_update(vf);
1653         vf_thumb_update(vf);
1654 }
1655
1656 static void vficon_populate_at_new_size(ViewFile *vf, gint w, gint h, gboolean force)
1657 {
1658         gint new_cols;
1659         gint thumb_width;
1660
1661         thumb_width = vficon_get_icon_width(vf);
1662
1663         new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1664         if (new_cols < 1) new_cols = 1;
1665
1666         if (!force && new_cols == VFICON(vf)->columns) return;
1667
1668         VFICON(vf)->columns = new_cols;
1669
1670         vficon_populate(vf, TRUE, TRUE);
1671
1672         DEBUG_1("col tab pop cols=%d rows=%d", VFICON(vf)->columns, VFICON(vf)->rows);
1673 }
1674
1675 static void vficon_sized_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
1676 {
1677         ViewFile *vf = data;
1678
1679         vficon_populate_at_new_size(vf, allocation->width, allocation->height, FALSE);
1680 }
1681
1682 /*
1683  *-----------------------------------------------------------------------------
1684  * misc
1685  *-----------------------------------------------------------------------------
1686  */
1687
1688 void vficon_sort_set(ViewFile *vf, SortType type, gboolean ascend)
1689 {
1690         if (vf->sort_method == type && vf->sort_ascend == ascend) return;
1691
1692         vf->sort_method = type;
1693         vf->sort_ascend = ascend;
1694
1695         if (!vf->list) return;
1696
1697         vf_refresh(vf);
1698 }
1699
1700 /*
1701  *-----------------------------------------------------------------------------
1702  * thumb updates
1703  *-----------------------------------------------------------------------------
1704  */
1705
1706 void vficon_thumb_progress_count(GList *list, gint *count, gint *done)
1707 {
1708         GList *work = list;
1709         while (work)
1710                 {
1711                 FileData *fd = work->data;
1712                 work = work->next;
1713
1714                 if (fd->thumb_pixbuf) (*done)++;
1715                 (*count)++;
1716                 }
1717 }
1718
1719 void vficon_set_thumb_fd(ViewFile *vf, FileData *fd)
1720 {
1721         GtkTreeModel *store;
1722         GtkTreeIter iter;
1723         GList *list;
1724
1725         if (!g_list_find(vf->list, fd)) return;
1726         if (!vficon_find_iter(vf, fd, &iter, NULL)) return;
1727
1728         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1729
1730         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1731         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1732 }
1733
1734 /* Returns the next fd without a loaded pixbuf, so the thumb-loader can load the pixbuf for it. */
1735 FileData *vficon_thumb_next_fd(ViewFile *vf)
1736 {
1737         GtkTreePath *tpath;
1738
1739         /* First see if there are visible files that don't have a loaded thumb... */
1740         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vf->listview), 0, 0, &tpath, NULL, NULL, NULL))
1741                 {
1742                 GtkTreeModel *store;
1743                 GtkTreeIter iter;
1744                 gboolean valid = TRUE;
1745
1746                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
1747                 gtk_tree_model_get_iter(store, &iter, tpath);
1748                 gtk_tree_path_free(tpath);
1749                 tpath = NULL;
1750
1751                 while (valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vf->listview), &iter, FALSE) == 0)
1752                         {
1753                         GList *list;
1754                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1755
1756                         // TODO(xsdg): for loop here.
1757                         for (; list; list = list->next)
1758                                 {
1759                                 FileData *fd = list->data;
1760                                 if (fd && !fd->thumb_pixbuf) return fd;
1761                                 }
1762
1763                         valid = gtk_tree_model_iter_next(store, &iter);
1764                         }
1765                 }
1766
1767         /* Then iterate through the entire list to load all of them. */
1768         GList *work;
1769         for (work = vf->list; work; work = work->next)
1770                 {
1771                 FileData *fd = work->data;
1772
1773                 // Note: This implementation differs from view_file_list.c because sidecar files are not
1774                 // distinct list elements here, as they are in the list view.
1775                 if (!fd->thumb_pixbuf) return fd;
1776                 }
1777
1778         return NULL;
1779 }
1780
1781 /*
1782  *-----------------------------------------------------------------------------
1783  * row stuff
1784  *-----------------------------------------------------------------------------
1785  */
1786
1787 gint vficon_index_by_fd(ViewFile *vf, FileData *in_fd)
1788 {
1789         gint p = 0;
1790         GList *work;
1791
1792         if (!in_fd) return -1;
1793
1794         work = vf->list;
1795         while (work)
1796                 {
1797                 FileData *fd = work->data;
1798                 if (fd == in_fd) return p;
1799                 work = work->next;
1800                 p++;
1801                 }
1802
1803         return -1;
1804 }
1805
1806 /*
1807  *-----------------------------------------------------------------------------
1808  *
1809  *-----------------------------------------------------------------------------
1810  */
1811
1812 static gboolean vficon_refresh_real(ViewFile *vf, gboolean keep_position)
1813 {
1814         gboolean ret = TRUE;
1815         GList *work, *new_work;
1816         FileData *focus_fd;
1817         FileData *first_selected = NULL;
1818         GList *new_filelist = NULL;
1819         GList *new_fd_list = NULL;
1820
1821         focus_fd = VFICON(vf)->focus_fd;
1822
1823         if (vf->dir_fd)
1824                 {
1825                 ret = filelist_read(vf->dir_fd, &new_filelist, NULL);
1826                 new_filelist = file_data_filter_marks_list(new_filelist, vf_marks_get_filter(vf));
1827                 new_filelist = filecluster_remove_children_from_list(vf->cluster_list, new_filelist);
1828                 }
1829
1830         /* the list might not be sorted if there were renames */
1831         vf->list = filelist_sort(vf->list, vf->sort_method, vf->sort_ascend);
1832         new_filelist = filelist_sort(new_filelist, vf->sort_method, vf->sort_ascend);
1833
1834         if (VFICON(vf)->selection)
1835                 {
1836                 first_selected = VFICON(vf)->selection->data;
1837                 file_data_ref(first_selected);
1838                 g_list_free(VFICON(vf)->selection);
1839                 VFICON(vf)->selection = NULL;
1840                 }
1841
1842         /* iterate old list and new list, looking for differences */
1843         work = vf->list;
1844         new_work = new_filelist;
1845         while (work || new_work)
1846                 {
1847                 FileData *fd = NULL;
1848                 FileData *new_fd = NULL;
1849                 gint match;
1850
1851                 if (work && new_work)
1852                         {
1853                         fd = work->data;
1854                         new_fd = new_work->data;
1855
1856                         if (fd == new_fd)
1857                                 {
1858                                 /* not changed, go to next */
1859                                 work = work->next;
1860                                 new_work = new_work->next;
1861                                 if (fd->selected & SELECTION_SELECTED)
1862                                         {
1863                                         VFICON(vf)->selection = g_list_prepend(VFICON(vf)->selection, fd);
1864                                         }
1865                                 continue;
1866                                 }
1867
1868                         match = filelist_sort_compare_filedata_full(fd, new_fd, vf->sort_method, vf->sort_ascend);
1869                         if (match == 0) g_warning("multiple fd for the same path");
1870                         }
1871                 else if (work)
1872                         {
1873                         /* old item was deleted */
1874                         fd = work->data;
1875                         match = -1;
1876                         }
1877                 else
1878                         {
1879                         /* new item was added */
1880                         new_fd = new_work->data;
1881                         match = 1;
1882                         }
1883
1884                 if (match < 0)
1885                         {
1886                         /* file no longer exists, delete from vf->list */
1887                         GList *to_delete = work;
1888                         work = work->next;
1889                         if (fd == VFICON(vf)->prev_selection) VFICON(vf)->prev_selection = NULL;
1890                         if (fd == VFICON(vf)->click_fd) VFICON(vf)->click_fd = NULL;
1891                         file_data_unref(fd);
1892                         vf->list = g_list_delete_link(vf->list, to_delete);
1893                         }
1894                 else
1895                         {
1896                         /* new file, add to vf->list */
1897                         file_data_ref(new_fd);
1898                         new_fd->selected = SELECTION_NONE;
1899                         if (work)
1900                                 {
1901                                 vf->list = g_list_insert_before(vf->list, work, new_fd);
1902                                 }
1903                         else
1904                                 {
1905                                 /* it is faster to append all new entries together later */
1906                                 new_fd_list = g_list_prepend(new_fd_list, new_fd);
1907                                 }
1908
1909                         new_work = new_work->next;
1910                         }
1911                 }
1912
1913         if (new_fd_list)
1914                 {
1915                 vf->list = g_list_concat(vf->list, g_list_reverse(new_fd_list));
1916                 }
1917
1918         VFICON(vf)->selection = g_list_reverse(VFICON(vf)->selection);
1919
1920         filelist_free(new_filelist);
1921
1922         vficon_populate(vf, TRUE, keep_position);
1923
1924         if (first_selected && !VFICON(vf)->selection)
1925                 {
1926                 /* all selected files disappeared */
1927                 vficon_select_closest(vf, first_selected);
1928                 }
1929         file_data_unref(first_selected);
1930
1931         /* attempt to keep focus on same icon when refreshing */
1932         if (focus_fd && g_list_find(vf->list, focus_fd))
1933                 {
1934                 vficon_set_focus(vf, focus_fd);
1935                 }
1936
1937         return ret;
1938 }
1939
1940 gboolean vficon_refresh(ViewFile *vf)
1941 {
1942         return vficon_refresh_real(vf, TRUE);
1943 }
1944
1945 /*
1946  *-----------------------------------------------------------------------------
1947  * draw, etc.
1948  *-----------------------------------------------------------------------------
1949  */
1950
1951 typedef struct _ColumnData ColumnData;
1952 struct _ColumnData
1953 {
1954         ViewFile *vf;
1955         gint number;
1956 };
1957
1958 static void vficon_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
1959                                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
1960 {
1961         GList *list;
1962         FileData *fd;
1963         ColumnData *cd = data;
1964         ViewFile *vf = cd->vf;
1965
1966         if (!GQV_IS_CELL_RENDERER_ICON(cell)) return;
1967
1968         gtk_tree_model_get(tree_model, iter, FILE_COLUMN_POINTER, &list, -1);
1969
1970         fd = g_list_nth_data(list, cd->number);
1971
1972         if (fd)
1973                 {
1974                 GdkColor color_fg;
1975                 GdkColor color_bg;
1976                 GtkStyle *style;
1977                 gchar *name_sidecars;
1978                 gchar *link;
1979                 GtkStateType state = GTK_STATE_NORMAL;
1980
1981                 g_assert(fd->magick == FD_MAGICK);
1982
1983                 link = islink(fd->path) ? GQ_LINK_STR : "";
1984                 if (fd->sidecar_files)
1985                         {
1986                         gchar *sidecars = file_data_sc_list_to_string(fd);
1987                         name_sidecars = g_strdup_printf("%s%s %s", link, fd->name, sidecars);
1988                         g_free(sidecars);
1989                         }
1990                 else
1991                         {
1992                         gchar *disabled_grouping = fd->disable_grouping ? _(" [NO GROUPING]") : "";
1993                         name_sidecars = g_strdup_printf("%s%s%s", link, fd->name, disabled_grouping);
1994                         }
1995
1996                 style = gtk_widget_get_style(vf->listview);
1997                 if (fd->selected & SELECTION_SELECTED)
1998                         {
1999                         state = GTK_STATE_SELECTED;
2000                         }
2001
2002                 memcpy(&color_fg, &style->text[state], sizeof(color_fg));
2003                 memcpy(&color_bg, &style->base[state], sizeof(color_bg));
2004
2005                 if (fd->selected & SELECTION_PRELIGHT)
2006                         {
2007                         shift_color(&color_bg, -1, 0);
2008                         }
2009
2010                 g_object_set(cell,      "pixbuf", fd->thumb_pixbuf,
2011                                         "text", name_sidecars,
2012                                         "marks", file_data_get_marks(fd),
2013                                         "show_marks", vf->marks_enabled,
2014                                         "cell-background-gdk", &color_bg,
2015                                         "cell-background-set", TRUE,
2016                                         "foreground-gdk", &color_fg,
2017                                         "foreground-set", TRUE,
2018                                         "has-focus", (VFICON(vf)->focus_fd == fd), NULL);
2019                 g_free(name_sidecars);
2020                 }
2021         else
2022                 {
2023                 g_object_set(cell,      "pixbuf", NULL,
2024                                         "text", NULL,
2025                                         "show_marks", FALSE,
2026                                         "cell-background-set", FALSE,
2027                                         "foreground-set", FALSE,
2028                                         "has-focus", FALSE, NULL);
2029                 }
2030 }
2031
2032 static void vficon_append_column(ViewFile *vf, gint n)
2033 {
2034         ColumnData *cd;
2035         GtkTreeViewColumn *column;
2036         GtkCellRenderer *renderer;
2037
2038         column = gtk_tree_view_column_new();
2039         gtk_tree_view_column_set_min_width(column, 0);
2040
2041         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2042         gtk_tree_view_column_set_alignment(column, 0.5);
2043
2044         renderer = gqv_cell_renderer_icon_new();
2045         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2046         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2047                                          "ypad", THUMB_BORDER_PADDING,
2048                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2049
2050         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2051         g_object_set_data(G_OBJECT(renderer), "column_number", GINT_TO_POINTER(n));
2052
2053         cd = g_new0(ColumnData, 1);
2054         cd->vf = vf;
2055         cd->number = n;
2056         gtk_tree_view_column_set_cell_data_func(column, renderer, vficon_cell_data_cb, cd, g_free);
2057
2058         gtk_tree_view_append_column(GTK_TREE_VIEW(vf->listview), column);
2059
2060         g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(vficon_mark_toggled_cb), vf);
2061 }
2062
2063 /*
2064  *-----------------------------------------------------------------------------
2065  * base
2066  *-----------------------------------------------------------------------------
2067  */
2068
2069 gboolean vficon_set_fd(ViewFile *vf, FileData *dir_fd)
2070 {
2071         gboolean ret;
2072
2073         if (!dir_fd) return FALSE;
2074         if (vf->dir_fd == dir_fd) return TRUE;
2075
2076         file_data_unref(vf->dir_fd);
2077         vf->dir_fd = file_data_ref(dir_fd);
2078
2079         g_list_free(VFICON(vf)->selection);
2080         VFICON(vf)->selection = NULL;
2081
2082         g_list_free(vf->list);
2083         vf->list = NULL;
2084
2085         /* NOTE: populate will clear the store for us */
2086         ret = vficon_refresh_real(vf, FALSE);
2087
2088         VFICON(vf)->focus_fd = NULL;
2089         vficon_move_focus(vf, 0, 0, FALSE);
2090
2091         return ret;
2092 }
2093
2094 void vficon_destroy_cb(GtkWidget *widget, gpointer data)
2095 {
2096         ViewFile *vf = data;
2097
2098         vf_refresh_idle_cancel(vf);
2099
2100         file_data_unregister_notify_func(vf_notify_cb, vf);
2101
2102         tip_unschedule(vf);
2103
2104         vf_thumb_cleanup(vf);
2105
2106         g_list_free(vf->list);
2107         g_list_free(VFICON(vf)->selection);
2108 }
2109
2110 ViewFile *vficon_new(ViewFile *vf, FileData *dir_fd)
2111 {
2112         GtkListStore *store;
2113         GtkTreeSelection *selection;
2114         gint i;
2115
2116         vf->info = g_new0(ViewFileInfoIcon, 1);
2117
2118         VFICON(vf)->show_text = options->show_icon_names;
2119
2120         store = gtk_list_store_new(1, G_TYPE_POINTER);
2121         vf->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2122         g_object_unref(store);
2123
2124         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vf->listview));
2125         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2126
2127         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vf->listview), FALSE);
2128         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vf->listview), FALSE);
2129
2130         for (i = 0; i < VFICON_MAX_COLUMNS; i++)
2131                 {
2132                 vficon_append_column(vf, i);
2133                 }
2134
2135         /* zero width column to hide tree view focus, we draw it ourselves */
2136         vficon_append_column(vf, i);
2137         /* end column to fill white space */
2138         vficon_append_column(vf, i);
2139
2140         g_signal_connect(G_OBJECT(vf->listview), "size_allocate",
2141                          G_CALLBACK(vficon_sized_cb), vf);
2142
2143         gtk_widget_set_events(vf->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2144                               GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2145
2146         g_signal_connect(G_OBJECT(vf->listview),"motion_notify_event",
2147                          G_CALLBACK(vficon_motion_cb), vf);
2148         g_signal_connect(G_OBJECT(vf->listview), "leave_notify_event",
2149                          G_CALLBACK(vficon_leave_cb), vf);
2150
2151         /* force VFICON(vf)->columns to be at least 1 (sane) - this will be corrected in the size_cb */
2152         vficon_populate_at_new_size(vf, 1, 1, FALSE);
2153
2154         file_data_register_notify_func(vf_notify_cb, vf, NOTIFY_PRIORITY_MEDIUM);
2155
2156         return vf;
2157 }
2158
2159 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */