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