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