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