Fix 1302: Cut image to clipboard
[geeqie.git] / src / view-file / view-file.cc
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Laurent Monin
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "view-file.h"
22
23 #include <gdk/gdk.h>
24 #include <glib-object.h>
25
26 #include "archives.h"
27 #include "compat.h"
28 #include "debug.h"
29 #include "dupe.h"
30 #include "filedata.h"
31 #include "history-list.h"
32 #include "intl.h"
33 #include "layout.h"
34 #include "main-defines.h"
35 #include "main.h"
36 #include "menu.h"
37 #include "misc.h"
38 #include "options.h"
39 #include "thumb.h"
40 #include "ui-fileops.h"
41 #include "ui-menu.h"
42 #include "ui-misc.h"
43 #include "ui-utildlg.h"
44 #include "utilops.h"
45 #include "view-file/view-file-icon.h"
46 #include "view-file/view-file-list.h"
47 #include "window.h"
48
49 /*
50  *-----------------------------------------------------------------------------
51  * signals
52  *-----------------------------------------------------------------------------
53  */
54
55 void vf_send_update(ViewFile *vf)
56 {
57         if (vf->func_status) vf->func_status(vf, vf->data_status);
58 }
59
60 /*
61  *-----------------------------------------------------------------------------
62  * misc
63  *-----------------------------------------------------------------------------
64  */
65
66 void vf_sort_set(ViewFile *vf, SortType type, gboolean ascend, gboolean case_sensitive)
67 {
68         switch (vf->type)
69         {
70         case FILEVIEW_LIST: vflist_sort_set(vf, type, ascend, case_sensitive); break;
71         case FILEVIEW_ICON: vficon_sort_set(vf, type, ascend, case_sensitive); break;
72         }
73 }
74
75 /*
76  *-----------------------------------------------------------------------------
77  * row stuff
78  *-----------------------------------------------------------------------------
79  */
80
81 FileData *vf_index_get_data(ViewFile *vf, gint row)
82 {
83         return static_cast<FileData *>(g_list_nth_data(vf->list, row));
84 }
85
86 gint vf_index_by_fd(ViewFile *vf, FileData *fd)
87 {
88         gint ret;
89
90         switch (vf->type)
91         {
92         case FILEVIEW_LIST: ret = vflist_index_by_fd(vf, fd); break;
93         case FILEVIEW_ICON: ret = vficon_index_by_fd(vf, fd); break;
94         default: ret = 0;
95         }
96
97         return ret;
98 }
99
100 guint vf_count(ViewFile *vf, gint64 *bytes)
101 {
102         if (bytes)
103                 {
104                 gint64 b = 0;
105                 GList *work;
106
107                 work = vf->list;
108                 while (work)
109                         {
110                         auto fd = static_cast<FileData *>(work->data);
111                         work = work->next;
112
113                         b += fd->size;
114                         }
115
116                 *bytes = b;
117                 }
118
119         return g_list_length(vf->list);
120 }
121
122 GList *vf_get_list(ViewFile *vf)
123 {
124         return filelist_copy(vf->list);
125 }
126
127 /*
128  *-------------------------------------------------------------------
129  * keyboard
130  *-------------------------------------------------------------------
131  */
132
133 static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
134 {
135         auto vf = static_cast<ViewFile *>(data);
136         gboolean ret;
137
138         switch (vf->type)
139         {
140         case FILEVIEW_LIST: ret = vflist_press_key_cb(widget, event, data); break;
141         case FILEVIEW_ICON: ret = vficon_press_key_cb(widget, event, data); break;
142         default: ret = FALSE;
143         }
144
145         return ret;
146 }
147
148 /*
149  *-------------------------------------------------------------------
150  * mouse
151  *-------------------------------------------------------------------
152  */
153
154 static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
155 {
156         auto vf = static_cast<ViewFile *>(data);
157         gboolean ret;
158
159         switch (vf->type)
160         {
161         case FILEVIEW_LIST: ret = vflist_press_cb(widget, bevent, data); break;
162         case FILEVIEW_ICON: ret = vficon_press_cb(widget, bevent, data); break;
163         default: ret = FALSE;
164         }
165
166         return ret;
167 }
168
169 static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
170 {
171         auto vf = static_cast<ViewFile *>(data);
172         gboolean ret;
173
174         switch (vf->type)
175         {
176         case FILEVIEW_LIST: ret = vflist_release_cb(widget, bevent, data); break;
177         case FILEVIEW_ICON: ret = vficon_release_cb(widget, bevent, data); break;
178         default: ret = FALSE;
179         }
180
181         return ret;
182 }
183
184
185 /*
186  *-----------------------------------------------------------------------------
187  * selections
188  *-----------------------------------------------------------------------------
189  */
190
191 guint vf_selection_count(ViewFile *vf, gint64 *bytes)
192 {
193         guint ret;
194
195         switch (vf->type)
196         {
197         case FILEVIEW_LIST: ret = vflist_selection_count(vf, bytes); break;
198         case FILEVIEW_ICON: ret = vficon_selection_count(vf, bytes); break;
199         default: ret = 0;
200         }
201
202         return ret;
203 }
204
205 GList *vf_selection_get_list(ViewFile *vf)
206 {
207         GList *ret;
208
209         switch (vf->type)
210         {
211         case FILEVIEW_LIST: ret = vflist_selection_get_list(vf); break;
212         case FILEVIEW_ICON: ret = vficon_selection_get_list(vf); break;
213         default: ret = nullptr;
214         }
215
216         return ret;
217 }
218
219 GList *vf_selection_get_list_by_index(ViewFile *vf)
220 {
221         GList *ret;
222
223         switch (vf->type)
224         {
225         case FILEVIEW_LIST: ret = vflist_selection_get_list_by_index(vf); break;
226         case FILEVIEW_ICON: ret = vficon_selection_get_list_by_index(vf); break;
227         default: ret = nullptr;
228         }
229
230         return ret;
231 }
232
233 void vf_select_all(ViewFile *vf)
234 {
235         switch (vf->type)
236         {
237         case FILEVIEW_LIST: vflist_select_all(vf); break;
238         case FILEVIEW_ICON: vficon_select_all(vf); break;
239         }
240 }
241
242 void vf_select_none(ViewFile *vf)
243 {
244         switch (vf->type)
245         {
246         case FILEVIEW_LIST: vflist_select_none(vf); break;
247         case FILEVIEW_ICON: vficon_select_none(vf); break;
248         }
249 }
250
251 void vf_select_invert(ViewFile *vf)
252 {
253         switch (vf->type)
254         {
255         case FILEVIEW_LIST: vflist_select_invert(vf); break;
256         case FILEVIEW_ICON: vficon_select_invert(vf); break;
257         }
258 }
259
260 void vf_select_by_fd(ViewFile *vf, FileData *fd)
261 {
262         switch (vf->type)
263         {
264         case FILEVIEW_LIST: vflist_select_by_fd(vf, fd); break;
265         case FILEVIEW_ICON: vficon_select_by_fd(vf, fd); break;
266         }
267 }
268
269 void vf_select_list(ViewFile *vf, GList *list)
270 {
271         switch (vf->type)
272         {
273         case FILEVIEW_LIST: vflist_select_list(vf, list); break;
274         case FILEVIEW_ICON: vficon_select_list(vf, list); break;
275         }
276 }
277
278 void vf_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
279 {
280         switch (vf->type)
281         {
282         case FILEVIEW_LIST: vflist_mark_to_selection(vf, mark, mode); break;
283         case FILEVIEW_ICON: vficon_mark_to_selection(vf, mark, mode); break;
284         }
285 }
286
287 void vf_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
288 {
289         switch (vf->type)
290         {
291         case FILEVIEW_LIST: vflist_selection_to_mark(vf, mark, mode); break;
292         case FILEVIEW_ICON: vficon_selection_to_mark(vf, mark, mode); break;
293         }
294 }
295
296 /*
297  *-----------------------------------------------------------------------------
298  * dnd
299  *-----------------------------------------------------------------------------
300  */
301
302
303 static void vf_dnd_init(ViewFile *vf)
304 {
305         switch (vf->type)
306         {
307         case FILEVIEW_LIST: vflist_dnd_init(vf); break;
308         case FILEVIEW_ICON: vficon_dnd_init(vf); break;
309         }
310 }
311
312 /*
313  *-----------------------------------------------------------------------------
314  * pop-up menu
315  *-----------------------------------------------------------------------------
316  */
317
318 GList *vf_pop_menu_file_list(ViewFile *vf)
319 {
320         GList *ret;
321
322         switch (vf->type)
323         {
324         case FILEVIEW_LIST: ret = vflist_pop_menu_file_list(vf); break;
325         case FILEVIEW_ICON: ret = vficon_pop_menu_file_list(vf); break;
326         default: ret = nullptr;
327         }
328
329         return ret;
330 }
331
332 GList *vf_selection_get_one(ViewFile *vf, FileData *fd)
333 {
334         GList *ret;
335
336         switch (vf->type)
337         {
338         case FILEVIEW_LIST: ret = vflist_selection_get_one(vf, fd); break;
339         case FILEVIEW_ICON: ret = vficon_selection_get_one(vf, fd); break;
340         default: ret = nullptr;
341         }
342
343         return ret;
344 }
345
346 static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
347 {
348         ViewFile *vf;
349         auto key = static_cast<const gchar *>(data);
350
351         vf = static_cast<ViewFile *>(submenu_item_get_data(widget));
352
353         if (!vf) return;
354
355         file_util_start_editor_from_filelist(key, vf_pop_menu_file_list(vf), vf->dir_fd->path, vf->listview);
356 }
357
358 static void vf_pop_menu_view_cb(GtkWidget *widget, gpointer data)
359 {
360         auto vf = static_cast<ViewFile *>(data);
361
362         switch (vf->type)
363         {
364         case FILEVIEW_LIST: vflist_pop_menu_view_cb(widget, data); break;
365         case FILEVIEW_ICON: vficon_pop_menu_view_cb(widget, data); break;
366         }
367 }
368
369 static void vf_pop_menu_open_archive_cb(GtkWidget *, gpointer data)
370 {
371         auto vf = static_cast<ViewFile *>(data);
372         LayoutWindow *lw_new;
373         FileData *fd = nullptr;
374         gchar *dest_dir;
375
376         switch (vf->type)
377         {
378         case FILEVIEW_LIST:
379                 fd = (VFLIST(vf)->click_fd);
380                 break;
381         case FILEVIEW_ICON:
382                 fd = (VFICON(vf)->click_fd);
383                 break;
384         }
385
386         dest_dir = open_archive(fd);
387         if (dest_dir)
388                 {
389                 lw_new = layout_new_from_default();
390                 layout_set_path(lw_new, dest_dir);
391                 g_free(dest_dir);
392                 }
393         else
394                 {
395                 warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GQ_ICON_DIALOG_WARNING, nullptr);
396                 }
397 }
398
399 static void vf_pop_menu_copy_cb(GtkWidget *, gpointer data)
400 {
401         auto vf = static_cast<ViewFile *>(data);
402
403         file_util_copy(nullptr, vf_pop_menu_file_list(vf), nullptr, vf->listview);
404 }
405
406 static void vf_pop_menu_move_cb(GtkWidget *, gpointer data)
407 {
408         auto vf = static_cast<ViewFile *>(data);
409
410         file_util_move(nullptr, vf_pop_menu_file_list(vf), nullptr, vf->listview);
411 }
412
413 static void vf_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
414 {
415         auto vf = static_cast<ViewFile *>(data);
416
417         switch (vf->type)
418         {
419         case FILEVIEW_LIST: vflist_pop_menu_rename_cb(widget, data); break;
420         case FILEVIEW_ICON: vficon_pop_menu_rename_cb(widget, data); break;
421         }
422 }
423
424 static void vf_pop_menu_delete_cb(GtkWidget *, gpointer data)
425 {
426         auto vf = static_cast<ViewFile *>(data);
427
428         options->file_ops.safe_delete_enable = FALSE;
429         file_util_delete(nullptr, vf_pop_menu_file_list(vf), vf->listview);
430 }
431
432 static void vf_pop_menu_move_to_trash_cb(GtkWidget *, gpointer data)
433 {
434         auto vf = static_cast<ViewFile *>(data);
435
436         options->file_ops.safe_delete_enable = TRUE;
437         file_util_delete(nullptr, vf_pop_menu_file_list(vf), vf->listview);
438 }
439
440 static void vf_pop_menu_copy_path_cb(GtkWidget *, gpointer data)
441 {
442         auto vf = static_cast<ViewFile *>(data);
443
444         file_util_path_list_to_clipboard(vf_pop_menu_file_list(vf), TRUE, TRUE);
445 }
446
447 static void vf_pop_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
448 {
449         auto vf = static_cast<ViewFile *>(data);
450
451         file_util_path_list_to_clipboard(vf_pop_menu_file_list(vf), FALSE, TRUE);
452 }
453
454 static void vf_pop_menu_cut_path_cb(GtkWidget *, gpointer data)
455 {
456         auto vf = static_cast<ViewFile *>(data);
457
458         file_util_path_list_to_clipboard(vf_pop_menu_file_list(vf), FALSE, FALSE);
459 }
460
461 static void vf_pop_menu_enable_grouping_cb(GtkWidget *, gpointer data)
462 {
463         auto vf = static_cast<ViewFile *>(data);
464
465         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), FALSE);
466 }
467
468 static void vf_pop_menu_duplicates_cb(GtkWidget *, gpointer data)
469 {
470         auto vf = static_cast<ViewFile *>(data);
471         DupeWindow *dw;
472
473         dw = dupe_window_new();
474         dupe_window_add_files(dw, vf_pop_menu_file_list(vf), FALSE);
475 }
476
477 static void vf_pop_menu_disable_grouping_cb(GtkWidget *, gpointer data)
478 {
479         auto vf = static_cast<ViewFile *>(data);
480
481         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), TRUE);
482 }
483
484 static void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
485 {
486         ViewFile *vf;
487         SortType type;
488
489         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
490
491         vf = static_cast<ViewFile *>(submenu_item_get_data(widget));
492         if (!vf) return;
493
494         type = static_cast<SortType>GPOINTER_TO_INT(data);
495
496         if (type == SORT_EXIFTIME || type == SORT_EXIFTIMEDIGITIZED || type == SORT_RATING)
497                 {
498                 vf_read_metadata_in_idle(vf);
499                 }
500
501         if (vf->layout)
502                 {
503                 layout_sort_set_files(vf->layout, type, vf->sort_ascend, vf->sort_case);
504                 }
505         else
506                 {
507                 vf_sort_set(vf, type, vf->sort_ascend, vf->sort_case);
508                 }
509 }
510
511 static void vf_pop_menu_sort_ascend_cb(GtkWidget *, gpointer data)
512 {
513         auto vf = static_cast<ViewFile *>(data);
514
515         if (vf->layout)
516                 {
517                 layout_sort_set_files(vf->layout, vf->sort_method, !vf->sort_ascend, vf->sort_case);
518                 }
519         else
520                 {
521                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend, vf->sort_case);
522                 }
523 }
524
525 static void vf_pop_menu_sort_case_cb(GtkWidget *, gpointer data)
526 {
527         auto vf = static_cast<ViewFile *>(data);
528
529         if (vf->layout)
530                 {
531                 layout_sort_set_files(vf->layout, vf->sort_method, vf->sort_ascend, !vf->sort_case);
532                 }
533         else
534                 {
535                 vf_sort_set(vf, vf->sort_method, vf->sort_ascend, !vf->sort_case);
536                 }
537 }
538
539 static void vf_pop_menu_sel_mark_cb(GtkWidget *, gpointer data)
540 {
541         auto vf = static_cast<ViewFile *>(data);
542         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
543 }
544
545 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *, gpointer data)
546 {
547         auto vf = static_cast<ViewFile *>(data);
548         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
549 }
550
551 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *, gpointer data)
552 {
553         auto vf = static_cast<ViewFile *>(data);
554         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
555 }
556
557 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *, gpointer data)
558 {
559         auto vf = static_cast<ViewFile *>(data);
560         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
561 }
562
563 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *, gpointer data)
564 {
565         auto vf = static_cast<ViewFile *>(data);
566         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
567 }
568
569 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *, gpointer data)
570 {
571         auto vf = static_cast<ViewFile *>(data);
572         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
573 }
574
575 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *, gpointer data)
576 {
577         auto vf = static_cast<ViewFile *>(data);
578         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
579 }
580
581 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
582 {
583         auto vf = static_cast<ViewFile *>(data);
584         auto new_type = static_cast<FileViewType>(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data")));
585         if (!vf->layout) return;
586
587         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
588 }
589
590 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
591 {
592         auto vf = static_cast<ViewFile *>(data);
593
594         switch (vf->type)
595         {
596         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
597         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
598         }
599 }
600
601 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
602 {
603         auto vf = static_cast<ViewFile *>(data);
604
605         switch (vf->type)
606         {
607         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
608         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
609         }
610
611         filelist_free(vf->editmenu_fd_list);
612         vf->editmenu_fd_list = nullptr;
613 }
614
615 /**
616  * @brief Add file selection list to a collection
617  * @param[in] widget
618  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
619  *
620  *
621  */
622 static void vf_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
623 {
624         ViewFile *vf;
625         GList *selection_list;
626
627         vf = static_cast<ViewFile *>(submenu_item_get_data(widget));
628         selection_list = vf_selection_get_list(vf);
629         pop_menu_collections(selection_list, data);
630
631         filelist_free(selection_list);
632 }
633
634 GtkWidget *vf_pop_menu(ViewFile *vf)
635 {
636         GtkWidget *menu;
637         GtkWidget *item;
638         GtkWidget *submenu;
639         gboolean active = FALSE;
640         gboolean class_archive = FALSE;
641         GtkAccelGroup *accel_group;
642
643         switch (vf->type)
644         {
645         case FILEVIEW_LIST:
646                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
647                 active = (VFLIST(vf)->click_fd != nullptr);
648                 class_archive = (VFLIST(vf)->click_fd != nullptr && VFLIST(vf)->click_fd->format_class == FORMAT_CLASS_ARCHIVE);
649                 break;
650         case FILEVIEW_ICON:
651                 active = (VFICON(vf)->click_fd != nullptr);
652                 class_archive = (VFICON(vf)->click_fd != nullptr && VFICON(vf)->click_fd->format_class == FORMAT_CLASS_ARCHIVE);
653                 break;
654         }
655
656         menu = popup_menu_short_lived();
657
658         accel_group = gtk_accel_group_new();
659         gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
660
661         g_object_set_data(G_OBJECT(menu), "window_keys", nullptr);
662         g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
663
664         g_signal_connect(G_OBJECT(menu), "destroy",
665                          G_CALLBACK(vf_popup_destroy_cb), vf);
666
667         if (vf->clicked_mark > 0)
668                 {
669                 gint mark = vf->clicked_mark;
670                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
671                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
672                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
673                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
674                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
675                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
676                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
677
678                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
679
680                 vf->active_mark = mark;
681                 vf->clicked_mark = 0;
682
683                 menu_item_add_sensitive(menu, str_set_mark, active,
684                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
685
686                 menu_item_add_sensitive(menu, str_res_mark, active,
687                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
688
689                 menu_item_add_sensitive(menu, str_toggle_mark, active,
690                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
691
692                 menu_item_add_divider(menu);
693
694                 menu_item_add_sensitive(menu, str_sel_mark, active,
695                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
696                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
697                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
698                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
699                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
700                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
701                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
702
703                 menu_item_add_divider(menu);
704
705                 g_free(str_set_mark);
706                 g_free(str_res_mark);
707                 g_free(str_toggle_mark);
708                 g_free(str_sel_mark);
709                 g_free(str_sel_mark_and);
710                 g_free(str_sel_mark_or);
711                 g_free(str_sel_mark_minus);
712                 }
713
714         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
715         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
716         gtk_widget_set_sensitive(item, active);
717
718         menu_item_add_icon_sensitive(menu, _("View in _new window"), GQ_ICON_NEW, active,
719                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
720
721         menu_item_add_icon_sensitive(menu, _("Open archive"), GQ_ICON_OPEN, active & class_archive, G_CALLBACK(vf_pop_menu_open_archive_cb), vf);
722
723         menu_item_add_divider(menu);
724         menu_item_add_icon_sensitive(menu, _("_Copy..."), GQ_ICON_COPY, active,
725                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
726         menu_item_add_sensitive(menu, _("_Move..."), active,
727                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
728         menu_item_add_sensitive(menu, _("_Rename..."), active,
729                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
730         menu_item_add_sensitive(menu, _("_Copy to clipboard"), active,
731                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
732         menu_item_add_sensitive(menu, _("_Copy to clipboard (unquoted)"), active,
733                                 G_CALLBACK(vf_pop_menu_copy_path_unquoted_cb), vf);
734         menu_item_add_sensitive(menu, _("_Cut to clipboard"), active,
735                                 G_CALLBACK(vf_pop_menu_cut_path_cb), vf);
736         menu_item_add_divider(menu);
737         menu_item_add_icon_sensitive(menu,
738                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
739                                         _("Move to Trash"), GQ_ICON_DELETE, active,
740                                 G_CALLBACK(vf_pop_menu_move_to_trash_cb), vf);
741         menu_item_add_icon_sensitive(menu,
742                                 options->file_ops.confirm_delete ? _("_Delete...") :
743                                         _("_Delete"), GQ_ICON_DELETE_SHRED, active,
744                                 G_CALLBACK(vf_pop_menu_delete_cb), vf);
745         menu_item_add_divider(menu);
746
747         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
748                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
749         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
750                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
751
752         menu_item_add_divider(menu);
753         menu_item_add_icon_sensitive(menu, _("_Find duplicates..."), GQ_ICON_FIND, active,
754                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
755         menu_item_add_divider(menu);
756
757         submenu = submenu_add_collections(menu, &item,
758                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
759         gtk_widget_set_sensitive(item, active);
760         menu_item_add_divider(menu);
761
762         submenu = submenu_add_sort(nullptr, G_CALLBACK(vf_pop_menu_sort_cb), vf,
763                                    FALSE, FALSE, TRUE, vf->sort_method);
764         menu_item_add_divider(submenu);
765         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
766                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
767         menu_item_add_check(submenu, _("Case"), vf->sort_ascend,
768                             G_CALLBACK(vf_pop_menu_sort_case_cb), vf);
769
770         item = menu_item_add(menu, _("_Sort"), nullptr, nullptr);
771         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
772
773         item = menu_item_add_radio(menu, _("Images as List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
774                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
775
776         item = menu_item_add_radio(menu, _("Images as Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
777                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
778
779         switch (vf->type)
780         {
781         case FILEVIEW_LIST:
782                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
783                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
784                 break;
785         case FILEVIEW_ICON:
786                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
787                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
788                 break;
789         }
790
791         switch (vf->type)
792         {
793         case FILEVIEW_LIST:
794                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
795                                     G_CALLBACK(vflist_pop_menu_show_star_rating_cb), vf);
796                 break;
797         case FILEVIEW_ICON:
798                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
799                                     G_CALLBACK(vficon_pop_menu_show_star_rating_cb), vf);
800                 break;
801         }
802
803         menu_item_add_icon(menu, _("Re_fresh"), GQ_ICON_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
804
805         return menu;
806 }
807
808 gboolean vf_refresh(ViewFile *vf)
809 {
810         gboolean ret;
811
812         switch (vf->type)
813         {
814         case FILEVIEW_LIST: ret = vflist_refresh(vf); break;
815         case FILEVIEW_ICON: ret = vficon_refresh(vf); break;
816         default: ret = FALSE;
817         }
818
819         return ret;
820 }
821
822 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
823 {
824         gboolean ret;
825
826         switch (vf->type)
827         {
828         case FILEVIEW_LIST: ret = vflist_set_fd(vf, dir_fd); break;
829         case FILEVIEW_ICON: ret = vficon_set_fd(vf, dir_fd); break;
830         default: ret = FALSE;
831         }
832
833         return ret;
834 }
835
836 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
837 {
838         auto vf = static_cast<ViewFile *>(data);
839
840         switch (vf->type)
841         {
842         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
843         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
844         }
845
846         if (vf->popup)
847                 {
848                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
849                                                      0, 0, nullptr, nullptr, vf);
850                 gq_gtk_widget_destroy(vf->popup);
851                 }
852
853         if (vf->read_metadata_in_idle_id)
854                 {
855                 g_idle_remove_by_data(vf);
856                 }
857         file_data_unref(vf->dir_fd);
858         g_free(vf->info);
859         g_free(vf);
860 }
861
862 static void vf_marks_filter_toggle_cb(GtkWidget *, gpointer data)
863 {
864         auto vf = static_cast<ViewFile *>(data);
865         vf_refresh_idle(vf);
866 }
867
868 struct MarksTextEntry {
869         GenericDialog *gd;
870         gint mark_no;
871         GtkWidget *edit_widget;
872         gchar *text_entry;
873         GtkWidget *parent;
874 };
875
876 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
877 {
878         auto mte = static_cast<MarksTextEntry *>(data);
879
880         g_free(mte->text_entry);
881         generic_dialog_close(gd);
882 }
883
884 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
885 {
886         auto mte = static_cast<MarksTextEntry *>(data);
887
888         g_free(options->marks_tooltips[mte->mark_no]);
889         options->marks_tooltips[mte->mark_no] = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
890
891         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
892
893         g_free(mte->text_entry);
894         generic_dialog_close(gd);
895 }
896
897 void vf_marks_filter_on_icon_press(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer userdata)
898 {
899         auto mte = static_cast<MarksTextEntry *>(userdata);
900
901         g_free(mte->text_entry);
902         mte->text_entry = g_strdup("");
903         gq_gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
904 }
905
906 static void vf_marks_tooltip_help_cb(GenericDialog *, gpointer)
907 {
908         help_window_show("GuideImageMarks.html");
909 }
910
911 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
912                                                                                 GdkEventButton *event,
913                                                                                 gpointer user_data)
914 {
915         GtkWidget *table;
916         gint i = GPOINTER_TO_INT(user_data);
917
918         if (event->button != MOUSE_BUTTON_RIGHT)
919                 return FALSE;
920
921         auto mte = g_new0(MarksTextEntry, 1);
922         mte->mark_no = i;
923         mte->text_entry = g_strdup(options->marks_tooltips[i]);
924         mte->parent = widget;
925
926         mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
927                                      widget, FALSE,
928                                      vf_marks_tooltip_cancel_cb, mte);
929         generic_dialog_add_message(mte->gd, GQ_ICON_DIALOG_QUESTION, _("Set mark text"),
930                                    _("This will set or clear the mark text."), FALSE);
931         generic_dialog_add_button(mte->gd, GQ_ICON_OK, "OK",
932                                   vf_marks_tooltip_ok_cb, TRUE);
933         generic_dialog_add_button(mte->gd, GQ_ICON_HELP, _("Help"),
934                                   vf_marks_tooltip_help_cb, FALSE);
935
936         table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
937         pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), GTK_ALIGN_END);
938         mte->edit_widget = gtk_entry_new();
939         gtk_widget_set_size_request(mte->edit_widget, 300, -1);
940         if (mte->text_entry)
941                 {
942                 gq_gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
943                 }
944         gq_gtk_grid_attach_default(GTK_GRID(table), mte->edit_widget, 1, 2, 0, 1);
945         generic_dialog_attach_default(mte->gd, mte->edit_widget);
946
947         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(mte->edit_widget),
948                                       GTK_ENTRY_ICON_SECONDARY, GQ_ICON_CLEAR);
949         gtk_entry_set_icon_tooltip_text(GTK_ENTRY(mte->edit_widget),
950                                         GTK_ENTRY_ICON_SECONDARY, _("Clear"));
951         g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
952                          G_CALLBACK(vf_marks_filter_on_icon_press), mte);
953
954         gtk_widget_show(mte->edit_widget);
955         gtk_widget_grab_focus(mte->edit_widget);
956         gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
957
958         return TRUE;
959 }
960
961 static void vf_file_filter_save_cb(GtkWidget *, gpointer data)
962 {
963         auto vf = static_cast<ViewFile *>(data);
964         gchar *entry_text;
965         gchar *remove_text = nullptr;
966         gchar *index_text = nullptr;
967         gboolean text_found = FALSE;
968         gint i;
969
970         entry_text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)))));
971
972         if (entry_text[0] == '\0' && vf->file_filter.last_selected >= 0)
973                 {
974                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.last_selected);
975                 remove_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
976                 history_list_item_remove("file_filter", remove_text);
977                 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), vf->file_filter.last_selected);
978                 g_free(remove_text);
979
980                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), -1);
981                 vf->file_filter.last_selected = - 1;
982                 gq_gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo))), "");
983                 vf->file_filter.count--;
984                 }
985         else
986                 {
987                 if (entry_text[0] != '\0')
988                         {
989                         for (i = 0; i < vf->file_filter.count; i++)
990                                 {
991                                 if (index_text)
992                                         {
993                                         g_free(index_text);
994                                         }
995                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), i);
996                                 index_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
997
998                                 if (g_strcmp0(index_text, entry_text) == 0)
999                                         {
1000                                         text_found = TRUE;
1001                                         break;
1002                                         }
1003                                 }
1004
1005                         g_free(index_text);
1006                         if (!text_found)
1007                                 {
1008                                 history_list_add_to_key("file_filter", entry_text, 10);
1009                                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), entry_text);
1010                                 vf->file_filter.count++;
1011                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.count - 1);
1012                                 }
1013                         }
1014                 }
1015         vf_refresh(vf);
1016
1017         g_free(entry_text);
1018 }
1019
1020 static void vf_file_filter_cb(GtkWidget *, gpointer data)
1021 {
1022         auto vf = static_cast<ViewFile *>(data);
1023
1024         vf_refresh(vf);
1025 }
1026
1027 static gboolean vf_file_filter_press_cb(GtkWidget *widget, GdkEventButton *, gpointer data)
1028 {
1029         auto vf = static_cast<ViewFile *>(data);
1030         vf->file_filter.last_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(vf->file_filter.combo));
1031
1032         gtk_widget_grab_focus(widget);
1033
1034         return TRUE;
1035 }
1036
1037 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
1038 {
1039         GtkWidget *frame = gtk_frame_new(nullptr);
1040         GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1041
1042         gint i;
1043
1044         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1045                 {
1046                 GtkWidget *check = gtk_check_button_new();
1047                 gq_gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
1048                 g_signal_connect(G_OBJECT(check), "toggled",
1049                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
1050                 g_signal_connect(G_OBJECT(check), "button_press_event",
1051                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
1052                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
1053
1054                 gtk_widget_show(check);
1055                 vf->filter_check[i] = check;
1056                 }
1057         gq_gtk_container_add(GTK_WIDGET(frame), hbox);
1058         gtk_widget_show(hbox);
1059         return frame;
1060 }
1061
1062 void vf_file_filter_set(ViewFile *vf, gboolean enable)
1063 {
1064         if (enable)
1065                 {
1066                 gtk_widget_show(vf->file_filter.combo);
1067                 gtk_widget_show(vf->file_filter.frame);
1068                 }
1069         else
1070                 {
1071                 gtk_widget_hide(vf->file_filter.combo);
1072                 gtk_widget_hide(vf->file_filter.frame);
1073                 }
1074
1075         vf_refresh(vf);
1076 }
1077
1078 static gboolean vf_file_filter_class_cb(GtkWidget *widget, gpointer data)
1079 {
1080         auto vf = static_cast<ViewFile *>(data);
1081         gint i;
1082
1083         gboolean state = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1084
1085         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1086                 {
1087                 if (g_strcmp0(format_class_list[i], gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1088                         {
1089                         options->class_filter[i] = state;
1090                         }
1091                 }
1092         vf_refresh(vf);
1093
1094         return TRUE;
1095 }
1096
1097 static gboolean vf_file_filter_class_set_all_cb(GtkWidget *widget, gpointer data)
1098 {
1099         auto vf = static_cast<ViewFile *>(data);
1100         GtkWidget *parent;
1101         GList *children;
1102         GtkWidget *child;
1103         gint i;
1104         gboolean state;
1105
1106         if (g_strcmp0(_("Select all"), gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1107                 {
1108                 state = TRUE;
1109                 }
1110         else
1111                 {
1112                 state = FALSE;
1113                 }
1114
1115         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1116                 {
1117                 options->class_filter[i] = state;
1118                 }
1119
1120         i = 0;
1121         parent = gtk_widget_get_parent(widget);
1122         children = gtk_container_get_children(GTK_CONTAINER(parent));
1123         while (children)
1124                 {
1125                 child = static_cast<GtkWidget *>(children->data);
1126                 if (i < FILE_FORMAT_CLASSES)
1127                         {
1128                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(child), state);
1129                         }
1130                 i++;
1131                 children = children->next;
1132                 }
1133         g_list_free(children);
1134         vf_refresh(vf);
1135
1136         return TRUE;
1137 }
1138
1139 static GtkWidget *class_filter_menu (ViewFile *vf)
1140 {
1141         GtkWidget *menu;
1142         GtkWidget *menu_item;
1143         int i;
1144
1145         menu = gtk_menu_new();
1146
1147         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1148             {
1149                 menu_item = gtk_check_menu_item_new_with_label(format_class_list[i]);
1150                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), options->class_filter[i]);
1151                 g_signal_connect(G_OBJECT(menu_item), "toggled", G_CALLBACK(vf_file_filter_class_cb), vf);
1152                 gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1153                 gtk_widget_show(menu_item);
1154                 }
1155
1156         menu_item = gtk_menu_item_new_with_label(_("Select all"));
1157         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1158         gtk_widget_show(menu_item);
1159         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1160
1161         menu_item = gtk_menu_item_new_with_label(_("Select none"));
1162         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1163         gtk_widget_show(menu_item);
1164         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1165
1166         return menu;
1167 }
1168
1169 static void case_sensitive_cb(GtkWidget *widget, gpointer data)
1170 {
1171         auto vf = static_cast<ViewFile *>(data);
1172
1173         vf->file_filter.case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1174         vf_refresh(vf);
1175 }
1176
1177 static void file_filter_clear_cb(GtkEntry *, GtkEntryIconPosition pos, GdkEvent *, gpointer userdata)
1178 {
1179         if (pos == GTK_ENTRY_ICON_SECONDARY)
1180                 {
1181                 gq_gtk_entry_set_text(GTK_ENTRY(userdata), "");
1182                 gtk_widget_grab_focus(GTK_WIDGET(userdata));
1183                 }
1184 }
1185
1186 static GtkWidget *vf_file_filter_init(ViewFile *vf)
1187 {
1188         GtkWidget *frame = gtk_frame_new(nullptr);
1189         GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1190         GList *work;
1191         gint n = 0;
1192         GtkWidget *combo_entry;
1193         GtkWidget *menubar;
1194         GtkWidget *menuitem;
1195         GtkWidget *case_sensitive;
1196         GtkWidget *box;
1197         GtkWidget *icon;
1198         GtkWidget *label;
1199
1200         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
1201         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
1202         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
1203         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
1204         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), _("Use regular expressions"));
1205
1206         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(combo_entry), GTK_ENTRY_ICON_SECONDARY, GQ_ICON_CLEAR);
1207         gtk_entry_set_icon_tooltip_text (GTK_ENTRY(combo_entry), GTK_ENTRY_ICON_SECONDARY, _("Clear"));
1208         g_signal_connect(GTK_ENTRY(combo_entry), "icon-press", G_CALLBACK(file_filter_clear_cb), combo_entry);
1209
1210         work = history_list_get_by_key("file_filter");
1211         while (work)
1212                 {
1213                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), static_cast<gchar *>(work->data));
1214                 work = work->next;
1215                 n++;
1216                 vf->file_filter.count = n;
1217                 }
1218         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
1219
1220         g_signal_connect(G_OBJECT(combo_entry), "activate",
1221                 G_CALLBACK(vf_file_filter_save_cb), vf);
1222
1223         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
1224                 G_CALLBACK(vf_file_filter_cb), vf);
1225
1226         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
1227                          G_CALLBACK(vf_file_filter_press_cb), vf);
1228
1229         gq_gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
1230         gtk_widget_show(vf->file_filter.combo);
1231         gq_gtk_container_add(GTK_WIDGET(frame), hbox);
1232         gtk_widget_show(hbox);
1233
1234         case_sensitive = gtk_check_button_new_with_label(_("Case"));
1235         gq_gtk_box_pack_start(GTK_BOX(hbox), case_sensitive, FALSE, FALSE, 0);
1236         gtk_widget_set_tooltip_text(GTK_WIDGET(case_sensitive), _("Case sensitive"));
1237         g_signal_connect(G_OBJECT(case_sensitive), "clicked", G_CALLBACK(case_sensitive_cb), vf);
1238         gtk_widget_show(case_sensitive);
1239
1240         menubar = gtk_menu_bar_new();
1241         gq_gtk_box_pack_start(GTK_BOX(hbox), menubar, FALSE, TRUE, 0);
1242         gtk_widget_show(menubar);
1243
1244         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1245         icon = gtk_image_new_from_icon_name(GQ_ICON_PAN_DOWN, GTK_ICON_SIZE_MENU);
1246         label = gtk_label_new(_("Class"));
1247
1248         gq_gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
1249         gq_gtk_box_pack_end(GTK_BOX(box), icon, FALSE, FALSE, 0);
1250
1251         menuitem = gtk_menu_item_new();
1252
1253         gtk_widget_set_tooltip_text(GTK_WIDGET(menuitem), _("Select Class filter"));
1254         gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), class_filter_menu(vf));
1255         gtk_menu_shell_append(GTK_MENU_SHELL(menubar), menuitem);
1256         gq_gtk_container_add(GTK_WIDGET(menuitem), box);
1257         gq_gtk_widget_show_all(menuitem);
1258
1259         return frame;
1260 }
1261
1262 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
1263 {
1264         gint n = mark - 1;
1265         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1266                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1267 }
1268
1269 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1270 {
1271         ViewFile *vf;
1272
1273         vf = g_new0(ViewFile, 1);
1274
1275         vf->type = type;
1276         vf->sort_method = SORT_NAME;
1277         vf->sort_ascend = TRUE;
1278         vf->read_metadata_in_idle_id = 0;
1279
1280         vf->scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
1281         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1282         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1283                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1284
1285         vf->filter = vf_marks_filter_init(vf);
1286         vf->file_filter.frame = vf_file_filter_init(vf);
1287
1288         vf->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1289         gq_gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1290         gq_gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1291         gq_gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1292         gtk_widget_show(vf->scrolled);
1293
1294         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1295                          G_CALLBACK(vf_destroy_cb), vf);
1296
1297         switch (type)
1298         {
1299         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1300         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1301         }
1302
1303         vf_dnd_init(vf);
1304
1305         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1306                          G_CALLBACK(vf_press_key_cb), vf);
1307         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1308                          G_CALLBACK(vf_press_cb), vf);
1309         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1310                          G_CALLBACK(vf_release_cb), vf);
1311
1312         gq_gtk_container_add(GTK_WIDGET(vf->scrolled), vf->listview);
1313         gtk_widget_show(vf->listview);
1314
1315         if (dir_fd) vf_set_fd(vf, dir_fd);
1316
1317         return vf;
1318 }
1319
1320 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1321 {
1322         vf->func_status = func;
1323         vf->data_status = data;
1324 }
1325
1326 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1327 {
1328         vf->func_thumb_status = func;
1329         vf->data_thumb_status = data;
1330 }
1331
1332 void vf_thumb_set(ViewFile *vf, gboolean enable)
1333 {
1334         switch (vf->type)
1335         {
1336         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1337         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1338         }
1339 }
1340
1341
1342 static gboolean vf_thumb_next(ViewFile *vf);
1343
1344 static gdouble vf_thumb_progress(ViewFile *vf)
1345 {
1346         gint count = 0;
1347         gint done = 0;
1348
1349         switch (vf->type)
1350         {
1351         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1352         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1353         }
1354
1355         DEBUG_1("thumb progress: %d of %d", done, count);
1356         return static_cast<gdouble>(done) / count;
1357 }
1358
1359 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1360 {
1361         gint count = 0;
1362         gint done = 0;
1363
1364         switch (vf->type)
1365                 {
1366                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1367                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1368                 }
1369
1370         return static_cast<gdouble>(done) / count;
1371 }
1372
1373 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1374 {
1375         switch (vf->type)
1376         {
1377         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1378         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1379         }
1380 }
1381
1382 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1383 {
1384         if (vf->func_thumb_status)
1385                 {
1386                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1387                 }
1388 }
1389
1390 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1391 {
1392         if (!fd) return;
1393
1394         vf_set_thumb_fd(vf, fd);
1395         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1396 }
1397
1398 void vf_thumb_cleanup(ViewFile *vf)
1399 {
1400         vf_thumb_status(vf, 0.0, nullptr);
1401
1402         vf->thumbs_running = FALSE;
1403
1404         thumb_loader_free(vf->thumbs_loader);
1405         vf->thumbs_loader = nullptr;
1406
1407         vf->thumbs_filedata = nullptr;
1408 }
1409
1410 void vf_thumb_stop(ViewFile *vf)
1411 {
1412         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1413 }
1414
1415 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1416 {
1417         auto vf = static_cast<ViewFile *>(data);
1418
1419         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1420                 {
1421                 vf_thumb_do(vf, vf->thumbs_filedata);
1422                 }
1423
1424         while (vf_thumb_next(vf));
1425 }
1426
1427 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1428 {
1429         vf_thumb_common_cb(tl, data);
1430 }
1431
1432 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1433 {
1434         vf_thumb_common_cb(tl, data);
1435 }
1436
1437 static gboolean vf_thumb_next(ViewFile *vf)
1438 {
1439         FileData *fd = nullptr;
1440
1441         if (!gtk_widget_get_realized(vf->listview))
1442                 {
1443                 vf_thumb_status(vf, 0.0, nullptr);
1444                 return FALSE;
1445                 }
1446
1447         switch (vf->type)
1448         {
1449         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1450         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1451         }
1452
1453         if (!fd)
1454                 {
1455                 /* done */
1456                 vf_thumb_cleanup(vf);
1457                 return FALSE;
1458                 }
1459
1460         vf->thumbs_filedata = fd;
1461
1462         thumb_loader_free(vf->thumbs_loader);
1463
1464         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1465         thumb_loader_set_callbacks(vf->thumbs_loader,
1466                                    vf_thumb_done_cb,
1467                                    vf_thumb_error_cb,
1468                                    nullptr,
1469                                    vf);
1470
1471         if (!thumb_loader_start(vf->thumbs_loader, fd))
1472                 {
1473                 /* set icon to unknown, continue */
1474                 DEBUG_1("thumb loader start failed %s", fd->path);
1475                 vf_thumb_do(vf, fd);
1476
1477                 return TRUE;
1478                 }
1479
1480         return FALSE;
1481 }
1482
1483 static void vf_thumb_reset_all(ViewFile *vf)
1484 {
1485         GList *work;
1486
1487         for (work = vf->list; work; work = work->next)
1488                 {
1489                 auto fd = static_cast<FileData *>(work->data);
1490                 if (fd->thumb_pixbuf)
1491                         {
1492                         g_object_unref(fd->thumb_pixbuf);
1493                         fd->thumb_pixbuf = nullptr;
1494                         }
1495                 }
1496 }
1497
1498 void vf_thumb_update(ViewFile *vf)
1499 {
1500         vf_thumb_stop(vf);
1501
1502         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1503
1504         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1505         vf->thumbs_running = TRUE;
1506
1507         if (thumb_format_changed)
1508                 {
1509                 vf_thumb_reset_all(vf);
1510                 thumb_format_changed = FALSE;
1511                 }
1512
1513         while (vf_thumb_next(vf));
1514 }
1515
1516 void vf_star_cleanup(ViewFile *vf)
1517 {
1518         if (vf->stars_id != 0)
1519                 {
1520                 g_source_remove(vf->stars_id);
1521                 }
1522
1523         vf->stars_id = 0;
1524         vf->stars_filedata = nullptr;
1525 }
1526
1527 void vf_star_stop(ViewFile *vf)
1528 {
1529          vf_star_cleanup(vf);
1530 }
1531
1532 static void vf_set_star_fd(ViewFile *vf, FileData *fd)
1533 {
1534         switch (vf->type)
1535                 {
1536                 case FILEVIEW_LIST: vflist_set_star_fd(vf, fd); break;
1537                 case FILEVIEW_ICON: vficon_set_star_fd(vf, fd); break;
1538                 default: break;
1539                 }
1540 }
1541
1542 static void vf_star_do(ViewFile *vf, FileData *fd)
1543 {
1544         if (!fd) return;
1545
1546         vf_set_star_fd(vf, fd);
1547 }
1548
1549 static gboolean vf_star_next(ViewFile *vf)
1550 {
1551         FileData *fd = nullptr;
1552
1553         switch (vf->type)
1554                 {
1555                 case FILEVIEW_LIST: fd = vflist_star_next_fd(vf); break;
1556                 case FILEVIEW_ICON: fd = vficon_star_next_fd(vf); break;
1557                 default: break;
1558                 }
1559
1560         if (!fd)
1561                 {
1562                 /* done */
1563                 vf_star_cleanup(vf);
1564                 return FALSE;
1565                 }
1566
1567         return TRUE;
1568 }
1569
1570 gboolean vf_stars_cb(gpointer data)
1571 {
1572         auto vf = static_cast<ViewFile *>(data);
1573         FileData *fd = vf->stars_filedata;
1574
1575         if (fd)
1576                 {
1577                 read_rating_data(fd);
1578
1579                 vf_star_do(vf, fd);
1580
1581                 if (vf_star_next(vf))
1582                         {
1583                         return G_SOURCE_CONTINUE;
1584                         }
1585
1586                 vf->stars_filedata = nullptr;
1587                 vf->stars_id = 0;
1588                 return G_SOURCE_REMOVE;
1589                 }
1590
1591         return G_SOURCE_REMOVE;
1592 }
1593
1594 void vf_star_update(ViewFile *vf)
1595 {
1596         vf_star_stop(vf);
1597
1598         if (!options->show_star_rating)
1599                 {
1600                 return;
1601                 }
1602
1603         vf_star_next(vf);
1604 }
1605
1606 void vf_marks_set(ViewFile *vf, gboolean enable)
1607 {
1608         if (vf->marks_enabled == enable) return;
1609
1610         vf->marks_enabled = enable;
1611
1612         switch (vf->type)
1613         {
1614         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1615         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1616         }
1617         if (enable)
1618                 gtk_widget_show(vf->filter);
1619         else
1620                 gtk_widget_hide(vf->filter);
1621
1622         vf_refresh_idle(vf);
1623 }
1624 #pragma GCC diagnostic push
1625 #pragma GCC diagnostic ignored "-Wunused-function"
1626 void vf_star_rating_set_unused(ViewFile *vf, gboolean enable)
1627 {
1628         if (options->show_star_rating == enable) return;
1629         options->show_star_rating = enable;
1630
1631         switch (vf->type)
1632                 {
1633                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1634                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1635                 }
1636         vf_refresh_idle(vf);
1637 }
1638 #pragma GCC diagnostic pop
1639
1640 guint vf_marks_get_filter(ViewFile *vf)
1641 {
1642         guint ret = 0;
1643         gint i;
1644         if (!vf->marks_enabled) return 0;
1645
1646         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1647                 {
1648                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1649                         {
1650                         ret |= 1 << i;
1651                         }
1652                 }
1653         return ret;
1654 }
1655
1656 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1657 {
1658         GRegex *ret = nullptr;
1659         GError *error = nullptr;
1660         gchar *file_filter_text = nullptr;
1661
1662         if (!gtk_widget_get_visible(vf->file_filter.combo))
1663                 {
1664                 return g_regex_new("", static_cast<GRegexCompileFlags>(0), static_cast<GRegexMatchFlags>(0), nullptr);
1665                 }
1666
1667         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1668
1669         if (file_filter_text[0] != '\0')
1670                 {
1671                 ret = g_regex_new(file_filter_text, vf->file_filter.case_sensitive ? static_cast<GRegexCompileFlags>(0) : G_REGEX_CASELESS, static_cast<GRegexMatchFlags>(0), &error);
1672                 if (error)
1673                         {
1674                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1675                         g_error_free(error);
1676                         error = nullptr;
1677                         ret = g_regex_new("", static_cast<GRegexCompileFlags>(0), static_cast<GRegexMatchFlags>(0), nullptr);
1678                         }
1679                 g_free(file_filter_text);
1680                 }
1681         else
1682                 {
1683                 ret = g_regex_new("", static_cast<GRegexCompileFlags>(0), static_cast<GRegexMatchFlags>(0), nullptr);
1684                 }
1685
1686         return ret;
1687 }
1688
1689 guint vf_class_get_filter(ViewFile *vf)
1690 {
1691         guint ret = 0;
1692         gint i;
1693
1694         if (!gtk_widget_get_visible(vf->file_filter.combo))
1695                 {
1696                 return G_MAXUINT;
1697                 }
1698
1699         for ( i = 0; i < FILE_FORMAT_CLASSES; i++)
1700                 {
1701                 if (options->class_filter[i])
1702                         {
1703                         ret |= 1 << i;
1704                         }
1705                 }
1706
1707         return ret;
1708 }
1709
1710 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1711 {
1712         vf->layout = layout;
1713 }
1714
1715
1716 /*
1717  *-----------------------------------------------------------------------------
1718  * maintenance (for rename, move, remove)
1719  *-----------------------------------------------------------------------------
1720  */
1721
1722 static gboolean vf_refresh_idle_cb(gpointer data)
1723 {
1724         auto vf = static_cast<ViewFile *>(data);
1725
1726         vf_refresh(vf);
1727         vf->refresh_idle_id = 0;
1728         return G_SOURCE_REMOVE;
1729 }
1730
1731 void vf_refresh_idle_cancel(ViewFile *vf)
1732 {
1733         if (vf->refresh_idle_id)
1734                 {
1735                 g_source_remove(vf->refresh_idle_id);
1736                 vf->refresh_idle_id = 0;
1737                 }
1738 }
1739
1740
1741 void vf_refresh_idle(ViewFile *vf)
1742 {
1743         if (!vf->refresh_idle_id)
1744                 {
1745                 vf->time_refresh_set = time(nullptr);
1746                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1747                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, nullptr);
1748                 }
1749         else if (time(nullptr) - vf->time_refresh_set > 1)
1750                 {
1751                 /* more than 1 sec since last update - increase priority */
1752                 vf_refresh_idle_cancel(vf);
1753                 vf->time_refresh_set = time(nullptr);
1754                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, nullptr);
1755                 }
1756 }
1757
1758 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1759 {
1760         auto vf = static_cast<ViewFile *>(data);
1761         gboolean refresh;
1762
1763         auto interested = static_cast<NotifyType>(NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING);
1764         if (vf->marks_enabled) interested = static_cast<NotifyType>(interested | NOTIFY_MARKS | NOTIFY_METADATA);
1765         /** @FIXME NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1766
1767         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1768
1769         refresh = (fd == vf->dir_fd);
1770
1771         if (!refresh)
1772                 {
1773                 gchar *base = remove_level_from_path(fd->path);
1774                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1775                 g_free(base);
1776                 }
1777
1778         if ((type & NOTIFY_CHANGE) && fd->change)
1779                 {
1780                 if (!refresh && fd->change->dest)
1781                         {
1782                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1783                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1784                         g_free(dest_base);
1785                         }
1786
1787                 if (!refresh && fd->change->source)
1788                         {
1789                         gchar *source_base = remove_level_from_path(fd->change->source);
1790                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1791                         g_free(source_base);
1792                         }
1793                 }
1794
1795         if (refresh)
1796                 {
1797                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1798                 vf_refresh_idle(vf);
1799                 }
1800 }
1801
1802 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1803 {
1804         FileData *fd;
1805         auto vf = static_cast<ViewFile *>(data);
1806         GList *work;
1807
1808         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1809
1810         work = vf->list;
1811
1812         while (work)
1813                 {
1814                 fd = static_cast<FileData *>(work->data);
1815
1816                 if (fd && !fd->metadata_in_idle_loaded)
1817                         {
1818                         if (!fd->exifdate)
1819                                 {
1820                                 read_exif_time_data(fd);
1821                                 }
1822                         if (!fd->exifdate_digitized)
1823                                 {
1824                                 read_exif_time_digitized_data(fd);
1825                                 }
1826                         if (fd->rating == STAR_RATING_NOT_READ)
1827                                 {
1828                                 read_rating_data(fd);
1829                                 }
1830                         fd->metadata_in_idle_loaded = TRUE;
1831                         return G_SOURCE_CONTINUE;
1832                         }
1833                 work = work->next;
1834                 }
1835
1836         vf_thumb_status(vf, 0.0, nullptr);
1837         vf->read_metadata_in_idle_id = 0;
1838         vf_refresh(vf);
1839         return G_SOURCE_REMOVE;
1840 }
1841
1842 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1843 {
1844         auto vf = static_cast<ViewFile *>(data);
1845
1846         vf_thumb_status(vf, 0.0, _("Loading meta..."));
1847         vf->read_metadata_in_idle_id = 0;
1848 }
1849
1850 void vf_read_metadata_in_idle(ViewFile *vf)
1851 {
1852         if (!vf) return;
1853
1854         if (vf->read_metadata_in_idle_id)
1855                 {
1856                 g_idle_remove_by_data(vf);
1857                 }
1858         vf->read_metadata_in_idle_id = 0;
1859
1860         if (vf->list)
1861                 {
1862                 vf->read_metadata_in_idle_id = g_idle_add_full(G_PRIORITY_LOW, vf_read_metadata_in_idle_cb, vf, vf_read_metadata_in_idle_finished_cb);
1863                 }
1864 }
1865
1866 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */