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