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