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