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