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