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