7d371643a70cf4b1945444693d395f2f7ae81130
[geeqie.git] / src / layout-util.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "layout-util.h"
23
24 #include <dirent.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27
28 #include <cstdio>
29 #include <cstdlib>
30 #include <cstring>
31
32 #include <gio/gio.h>
33 #include <glib-object.h>
34
35 #include <config.h>
36
37 #include "advanced-exif.h"
38 #include "archives.h"
39 #include "bar-keywords.h"
40 #include "bar-sort.h"
41 #include "bar.h"
42 #include "cache-maint.h"
43 #include "collect-io.h"
44 #include "collect.h"
45 #include "color-man.h"
46 #include "compat.h"
47 #include "debug.h"
48 #include "desktop-file.h"
49 #include "dupe.h"
50 #include "editors.h"
51 #include "filedata.h"
52 #include "fullscreen.h"
53 #include "histogram.h"
54 #include "history-list.h"
55 #include "image-overlay.h"
56 #include "image.h"
57 #include "img-view.h"
58 #include "intl.h"
59 #include "keymap-template.h"
60 #include "layout-image.h"
61 #include "layout.h"
62 #include "logwindow.h"
63 #include "main-defines.h"
64 #include "main.h"
65 #include "metadata.h"
66 #include "misc.h"
67 #include "options.h"
68 #include "pan-view.h"
69 #include "pixbuf-renderer.h"
70 #include "pixbuf-util.h"
71 #include "preferences.h"
72 #include "print.h"
73 #include "rcfile.h"
74 #include "search-and-run.h"
75 #include "search.h"
76 #include "slideshow.h"
77 #include "ui-fileops.h"
78 #include "ui-menu.h"
79 #include "ui-misc.h"
80 #include "ui-utildlg.h"
81 #include "utilops.h"
82 #include "view-dir.h"
83 #include "view-file.h"
84 #include "window.h"
85
86 static gboolean layout_bar_enabled(LayoutWindow *lw);
87 static gboolean layout_bar_sort_enabled(LayoutWindow *lw);
88 static void layout_bars_hide_toggle(LayoutWindow *lw);
89 static void layout_util_sync_views(LayoutWindow *lw);
90 static void layout_search_and_run_window_new(LayoutWindow *lw);
91
92 /*
93  *-----------------------------------------------------------------------------
94  * keyboard handler
95  *-----------------------------------------------------------------------------
96  */
97
98 static guint tree_key_overrides[] = {
99         GDK_KEY_Page_Up,        GDK_KEY_KP_Page_Up,
100         GDK_KEY_Page_Down,      GDK_KEY_KP_Page_Down,
101         GDK_KEY_Home,   GDK_KEY_KP_Home,
102         GDK_KEY_End,    GDK_KEY_KP_End
103 };
104
105 static gboolean layout_key_match(guint keyval)
106 {
107         guint i;
108
109         for (i = 0; i < sizeof(tree_key_overrides) / sizeof(guint); i++)
110                 {
111                 if (keyval == tree_key_overrides[i]) return TRUE;
112                 }
113
114         return FALSE;
115 }
116
117 void keyboard_scroll_calc(gint &x, gint &y, const GdkEventKey *event)
118 {
119         static gint delta = 0;
120         static guint32 time_old = 0;
121         static guint keyval_old = 0;
122
123         if (event->state & GDK_SHIFT_MASK)
124                 {
125                 x *= 3;
126                 y *= 3;
127                 }
128
129         if (event->state & GDK_CONTROL_MASK)
130                 {
131                 if (x < 0) x = G_MININT / 2;
132                 if (x > 0) x = G_MAXINT / 2;
133                 if (y < 0) y = G_MININT / 2;
134                 if (y > 0) y = G_MAXINT / 2;
135
136                 return;
137                 }
138
139         if (options->progressive_key_scrolling)
140                 {
141                 guint32 time_diff;
142
143                 time_diff = event->time - time_old;
144
145                 /* key pressed within 125ms ? (1/8 second) */
146                 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
147
148                 time_old = event->time;
149                 keyval_old = event->keyval;
150
151                 delta += 2;
152                 }
153         else
154                 {
155                 delta = 8;
156                 }
157
158         x *= delta * options->keyboard_scroll_step;
159         y *= delta * options->keyboard_scroll_step;
160 }
161
162 gboolean layout_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
163 {
164         auto lw = static_cast<LayoutWindow *>(data);
165         GtkWidget *focused;
166         gboolean stop_signal = FALSE;
167         gint x = 0;
168         gint y = 0;
169
170         if (lw->path_entry && gtk_widget_has_focus(lw->path_entry))
171                 {
172                 if (event->keyval == GDK_KEY_Escape && lw->dir_fd)
173                         {
174                         gq_gtk_entry_set_text(GTK_ENTRY(lw->path_entry), lw->dir_fd->path);
175                         }
176
177                 /* the gtkaccelgroup of the window is stealing presses before they get to the entry (and more),
178                  * so when the some widgets have focus, give them priority (HACK)
179                  */
180                 if (gtk_widget_event(lw->path_entry, reinterpret_cast<GdkEvent *>(event)))
181                         {
182                         return TRUE;
183                         }
184                 }
185
186         if (lw->vf->file_filter.combo && gtk_widget_has_focus(gtk_bin_get_child(GTK_BIN(lw->vf->file_filter.combo))))
187                 {
188                 if (gtk_widget_event(gtk_bin_get_child(GTK_BIN(lw->vf->file_filter.combo)), reinterpret_cast<GdkEvent *>(event)))
189                         {
190                         return TRUE;
191                         }
192                 }
193
194         if (lw->vd && lw->options.dir_view_type == DIRVIEW_TREE && gtk_widget_has_focus(lw->vd->view) &&
195             !layout_key_match(event->keyval) &&
196             gtk_widget_event(lw->vd->view, reinterpret_cast<GdkEvent *>(event)))
197                 {
198                 return TRUE;
199                 }
200         if (lw->bar &&
201             bar_event(lw->bar, reinterpret_cast<GdkEvent *>(event)))
202                 {
203                 return TRUE;
204                 }
205
206         focused = gtk_container_get_focus_child(GTK_CONTAINER(lw->image->widget));
207         if (lw->image &&
208             ((focused && gtk_widget_has_focus(focused)) || (lw->tools && widget == lw->window) || lw->full_screen) )
209                 {
210                 stop_signal = TRUE;
211                 switch (event->keyval)
212                         {
213                         case GDK_KEY_Left: case GDK_KEY_KP_Left:
214                                 x -= 1;
215                                 break;
216                         case GDK_KEY_Right: case GDK_KEY_KP_Right:
217                                 x += 1;
218                                 break;
219                         case GDK_KEY_Up: case GDK_KEY_KP_Up:
220                                 y -= 1;
221                                 break;
222                         case GDK_KEY_Down: case GDK_KEY_KP_Down:
223                                 y += 1;
224                                 break;
225                         default:
226                                 stop_signal = FALSE;
227                                 break;
228                         }
229
230                 if (!stop_signal &&
231                     !(event->state & GDK_CONTROL_MASK))
232                         {
233                         stop_signal = TRUE;
234                         switch (event->keyval)
235                                 {
236                                 case GDK_KEY_Menu:
237                                         layout_image_menu_popup(lw);
238                                         break;
239                                 default:
240                                         stop_signal = FALSE;
241                                         break;
242                                 }
243                         }
244                 }
245
246         if (x != 0 || y!= 0)
247                 {
248                 keyboard_scroll_calc(x, y, event);
249                 layout_image_scroll(lw, x, y, (event->state & GDK_SHIFT_MASK));
250                 }
251
252         return stop_signal;
253 }
254
255 void layout_keyboard_init(LayoutWindow *lw, GtkWidget *window)
256 {
257         g_signal_connect(G_OBJECT(window), "key_press_event",
258                          G_CALLBACK(layout_key_press_cb), lw);
259 }
260
261 /*
262  *-----------------------------------------------------------------------------
263  * menu callbacks
264  *-----------------------------------------------------------------------------
265  */
266
267
268 static GtkWidget *layout_window(LayoutWindow *lw)
269 {
270         return lw->full_screen ? lw->full_screen->window : lw->window;
271 }
272
273 static void layout_exit_fullscreen(LayoutWindow *lw)
274 {
275         if (!lw->full_screen) return;
276         layout_image_full_screen_stop(lw);
277 }
278
279 static void clear_marks_cancel_cb(GenericDialog *gd, gpointer)
280 {
281         generic_dialog_close(gd);
282 }
283
284 static void clear_marks_help_cb(GenericDialog *, gpointer)
285 {
286         help_window_show("GuideMainWindowMenus.html");
287 }
288
289 void layout_menu_clear_marks_ok_cb(GenericDialog *gd, gpointer)
290 {
291         marks_clear_all();
292         generic_dialog_close(gd);
293 }
294
295 static void layout_menu_clear_marks_cb(GtkAction *, gpointer)
296 {
297         GenericDialog *gd;
298
299         gd = generic_dialog_new(_("Clear Marks"),
300                                 "marks_clear", nullptr, FALSE, clear_marks_cancel_cb, nullptr);
301         generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION, _("Clear all marks?"), _("This will clear all marks for all images,\nincluding those linked to keywords"), TRUE);
302         generic_dialog_add_button(gd, GQ_ICON_OK, "OK", layout_menu_clear_marks_ok_cb, TRUE);
303         generic_dialog_add_button(gd, GQ_ICON_HELP, _("Help"),
304                                 clear_marks_help_cb, FALSE);
305
306         gtk_widget_show(gd->dialog);
307 }
308
309 static void layout_menu_new_cb(GtkAction *, gpointer data)
310 {
311         auto lw = static_cast<LayoutWindow *>(data);
312
313         layout_exit_fullscreen(lw);
314         collection_window_new(nullptr);
315 }
316
317 static void layout_menu_open_cb(GtkAction *widget, gpointer data)
318 {
319         auto lw = static_cast<LayoutWindow *>(data);
320         gchar *path;
321         gint n;
322         GList *collection_list = nullptr;
323
324         layout_exit_fullscreen(lw);
325
326         n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "recent_index"));
327         collect_manager_list(nullptr, nullptr, &collection_list);
328
329         path = static_cast<gchar *>(g_list_nth_data(collection_list, n));
330
331         if (path)
332                 {
333                 /* make a copy of it */
334                 path = g_strdup(path);
335                 collection_window_new(path);
336                 g_free(path);
337                 }
338
339         string_list_free(collection_list);
340 }
341
342 static void layout_menu_search_cb(GtkAction *, gpointer data)
343 {
344         auto lw = static_cast<LayoutWindow *>(data);
345
346         layout_exit_fullscreen(lw);
347         search_new(lw->dir_fd, layout_image_get_fd(lw));
348 }
349
350 static void layout_menu_dupes_cb(GtkAction *, gpointer data)
351 {
352         auto lw = static_cast<LayoutWindow *>(data);
353
354         layout_exit_fullscreen(lw);
355         dupe_window_new();
356 }
357
358 static void layout_menu_pan_cb(GtkAction *, gpointer data)
359 {
360         auto lw = static_cast<LayoutWindow *>(data);
361
362         layout_exit_fullscreen(lw);
363         pan_window_new(lw->dir_fd);
364 }
365
366 static void layout_menu_print_cb(GtkAction *, gpointer data)
367 {
368         auto lw = static_cast<LayoutWindow *>(data);
369
370         print_window_new(layout_image_get_fd(lw), layout_selection_list(lw), layout_list(lw), layout_window(lw));
371 }
372
373 static void layout_menu_dir_cb(GtkAction *, gpointer data)
374 {
375         auto lw = static_cast<LayoutWindow *>(data);
376
377         if (lw->vd) vd_new_folder(lw->vd, lw->dir_fd);
378 }
379
380 static void layout_menu_copy_cb(GtkAction *, gpointer data)
381 {
382         auto lw = static_cast<LayoutWindow *>(data);
383
384         file_util_copy(nullptr, layout_selection_list(lw), nullptr, layout_window(lw));
385 }
386
387 static void layout_menu_copy_path_cb(GtkAction *, gpointer data)
388 {
389         auto lw = static_cast<LayoutWindow *>(data);
390
391         file_util_path_list_to_clipboard(layout_selection_list(lw), TRUE, TRUE);
392 }
393
394 static void layout_menu_copy_path_unquoted_cb(GtkAction *, gpointer data)
395 {
396         auto lw = static_cast<LayoutWindow *>(data);
397
398         file_util_path_list_to_clipboard(layout_selection_list(lw), FALSE, TRUE);
399 }
400
401 static void layout_menu_cut_path_cb(GtkAction *, gpointer data)
402 {
403         auto lw = static_cast<LayoutWindow *>(data);
404
405         file_util_path_list_to_clipboard(layout_selection_list(lw), FALSE, FALSE);
406 }
407
408 static void layout_menu_move_cb(GtkAction *, gpointer data)
409 {
410         auto lw = static_cast<LayoutWindow *>(data);
411
412         file_util_move(nullptr, layout_selection_list(lw), nullptr, layout_window(lw));
413 }
414
415 static void layout_menu_rename_cb(GtkAction *, gpointer data)
416 {
417         auto lw = static_cast<LayoutWindow *>(data);
418
419         file_util_rename(nullptr, layout_selection_list(lw), layout_window(lw));
420 }
421
422 static void layout_menu_delete_cb(GtkAction *, gpointer data)
423 {
424         auto lw = static_cast<LayoutWindow *>(data);
425
426         options->file_ops.safe_delete_enable = FALSE;
427         file_util_delete(nullptr, layout_selection_list(lw), layout_window(lw));
428 }
429
430 static void layout_menu_move_to_trash_cb(GtkAction *, gpointer data)
431 {
432         auto lw = static_cast<LayoutWindow *>(data);
433
434         options->file_ops.safe_delete_enable = TRUE;
435         file_util_delete(nullptr, layout_selection_list(lw), layout_window(lw));
436 }
437
438 static void layout_menu_move_to_trash_key_cb(GtkAction *, gpointer data)
439 {
440         auto lw = static_cast<LayoutWindow *>(data);
441
442         if (options->file_ops.enable_delete_key)
443                 {
444                 options->file_ops.safe_delete_enable = TRUE;
445                 file_util_delete(nullptr, layout_selection_list(lw), layout_window(lw));
446                 }
447 }
448
449 static void layout_menu_disable_grouping_cb(GtkAction *, gpointer data)
450 {
451         auto lw = static_cast<LayoutWindow *>(data);
452
453         file_data_disable_grouping_list(layout_selection_list(lw), TRUE);
454 }
455
456 static void layout_menu_enable_grouping_cb(GtkAction *, gpointer data)
457 {
458         auto lw = static_cast<LayoutWindow *>(data);
459
460         file_data_disable_grouping_list(layout_selection_list(lw), FALSE);
461 }
462
463 void layout_menu_close_cb(GtkAction *, gpointer data)
464 {
465         auto lw = static_cast<LayoutWindow *>(data);
466
467         layout_exit_fullscreen(lw);
468         layout_close(lw);
469 }
470
471 static void layout_menu_exit_cb(GtkAction *, gpointer)
472 {
473         exit_program();
474 }
475
476 static void layout_menu_alter_90_cb(GtkAction *, gpointer data)
477 {
478         auto lw = static_cast<LayoutWindow *>(data);
479
480         layout_image_alter_orientation(lw, ALTER_ROTATE_90);
481 }
482
483 static void layout_menu_rating_0_cb(GtkAction *, gpointer data)
484 {
485         auto lw = static_cast<LayoutWindow *>(data);
486
487         layout_image_rating(lw, "0");
488 }
489
490 static void layout_menu_rating_1_cb(GtkAction *, gpointer data)
491 {
492         auto lw = static_cast<LayoutWindow *>(data);
493
494         layout_image_rating(lw, "1");
495 }
496
497 static void layout_menu_rating_2_cb(GtkAction *, gpointer data)
498 {
499         auto lw = static_cast<LayoutWindow *>(data);
500
501         layout_image_rating(lw, "2");
502 }
503
504 static void layout_menu_rating_3_cb(GtkAction *, gpointer data)
505 {
506         auto lw = static_cast<LayoutWindow *>(data);
507
508         layout_image_rating(lw, "3");
509 }
510
511 static void layout_menu_rating_4_cb(GtkAction *, gpointer data)
512 {
513         auto lw = static_cast<LayoutWindow *>(data);
514
515         layout_image_rating(lw, "4");
516 }
517
518 static void layout_menu_rating_5_cb(GtkAction *, gpointer data)
519 {
520         auto lw = static_cast<LayoutWindow *>(data);
521
522         layout_image_rating(lw, "5");
523 }
524
525 static void layout_menu_rating_m1_cb(GtkAction *, gpointer data)
526 {
527         auto lw = static_cast<LayoutWindow *>(data);
528
529         layout_image_rating(lw, "-1");
530 }
531
532 static void layout_menu_alter_90cc_cb(GtkAction *, gpointer data)
533 {
534         auto lw = static_cast<LayoutWindow *>(data);
535
536         layout_image_alter_orientation(lw, ALTER_ROTATE_90_CC);
537 }
538
539 static void layout_menu_alter_180_cb(GtkAction *, gpointer data)
540 {
541         auto lw = static_cast<LayoutWindow *>(data);
542
543         layout_image_alter_orientation(lw, ALTER_ROTATE_180);
544 }
545
546 static void layout_menu_alter_mirror_cb(GtkAction *, gpointer data)
547 {
548         auto lw = static_cast<LayoutWindow *>(data);
549
550         layout_image_alter_orientation(lw, ALTER_MIRROR);
551 }
552
553 static void layout_menu_alter_flip_cb(GtkAction *, gpointer data)
554 {
555         auto lw = static_cast<LayoutWindow *>(data);
556
557         layout_image_alter_orientation(lw, ALTER_FLIP);
558 }
559
560 static void layout_menu_alter_desaturate_cb(GtkToggleAction *action, gpointer data)
561 {
562         auto lw = static_cast<LayoutWindow *>(data);
563
564         layout_image_set_desaturate(lw, gtk_toggle_action_get_active(action));
565 }
566
567 static void layout_menu_alter_ignore_alpha_cb(GtkToggleAction *action, gpointer data)
568 {
569    auto lw = static_cast<LayoutWindow *>(data);
570
571         if (lw->options.ignore_alpha == gtk_toggle_action_get_active(action)) return;
572
573    layout_image_set_ignore_alpha(lw, gtk_toggle_action_get_active(action));
574 }
575
576 static void layout_menu_alter_none_cb(GtkAction *, gpointer data)
577 {
578         auto lw = static_cast<LayoutWindow *>(data);
579
580         layout_image_alter_orientation(lw, ALTER_NONE);
581 }
582
583 static void layout_menu_exif_rotate_cb(GtkToggleAction *action, gpointer data)
584 {
585         auto lw = static_cast<LayoutWindow *>(data);
586
587         options->image.exif_rotate_enable = gtk_toggle_action_get_active(action);
588         layout_image_reset_orientation(lw);
589 }
590
591 static void layout_menu_select_rectangle_cb(GtkToggleAction *action, gpointer)
592 {
593         options->draw_rectangle = gtk_toggle_action_get_active(action);
594 }
595
596 static void layout_menu_split_pane_sync_cb(GtkToggleAction *action, gpointer data)
597 {
598         auto lw = static_cast<LayoutWindow *>(data);
599
600         lw->options.split_pane_sync = gtk_toggle_action_get_active(action);
601 }
602
603 static void layout_menu_select_overunderexposed_cb(GtkToggleAction *action, gpointer data)
604 {
605         auto lw = static_cast<LayoutWindow *>(data);
606
607         layout_image_set_overunderexposed(lw, gtk_toggle_action_get_active(action));
608 }
609
610 static void layout_menu_write_rotate(GtkToggleAction *, gpointer data, gboolean keep_date)
611 {
612         auto lw = static_cast<LayoutWindow *>(data);
613         GtkTreeModel *store;
614         GList *work;
615         GtkTreeSelection *selection;
616         GtkTreePath *tpath;
617         FileData *fd_n;
618         GtkTreeIter iter;
619         gchar *rotation;
620         gchar *command;
621         gint run_result;
622         GenericDialog *gd;
623         GString *message;
624         int cmdstatus;
625
626         if (!layout_valid(&lw)) return;
627
628         if (!lw || !lw->vf) return;
629
630         if (lw->vf->type == FILEVIEW_ICON)
631                 {
632                 if (!VFICON(lw->vf)->selection) return;
633                 work = VFICON(lw->vf)->selection;
634                 }
635         else
636                 {
637                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
638                 work = gtk_tree_selection_get_selected_rows(selection, &store);
639                 }
640
641         while (work)
642                 {
643                 if (lw->vf->type == FILEVIEW_ICON)
644                         {
645                         fd_n = static_cast<FileData *>(work->data);
646                         work = work->next;
647                         }
648                 else
649                         {
650                         tpath = static_cast<GtkTreePath *>(work->data);
651                         gtk_tree_model_get_iter(store, &iter, tpath);
652                         gtk_tree_model_get(store, &iter, VIEW_FILE_COLUMN_POINTER, &fd_n, -1);
653                         work = work->next;
654                         }
655
656                 rotation = g_strdup_printf("%d", fd_n->user_orientation);
657                 command = g_strconcat(gq_bindir, "/geeqie-rotate -r ", rotation,
658                                                                 keep_date ? " -t \"" : " \"", fd_n->path, "\"", NULL);
659                 cmdstatus = runcmd(command);
660                 run_result = WEXITSTATUS(cmdstatus);
661                 if (!run_result)
662                         {
663                         fd_n->user_orientation = 0;
664                         }
665                 else
666                         {
667                         message = g_string_new(_("Operation failed:\n"));
668
669                         if (run_result == 1)
670                                 message = g_string_append(message, _("No file extension\n"));
671                         else if (run_result == 3)
672                                 message = g_string_append(message, _("Cannot create tmp file\n"));
673                         else if (run_result == 4)
674                                 message = g_string_append(message, _("Operation not supported for filetype\n"));
675                         else if (run_result == 5)
676                                 message = g_string_append(message, _("File is not writable\n"));
677                         else if (run_result == 6)
678                                 message = g_string_append(message, _("Exiftran error\n"));
679                         else if (run_result == 7)
680                                 message = g_string_append(message, _("Mogrify error\n"));
681
682                         message = g_string_append(message, fd_n->name);
683
684                         gd = generic_dialog_new(_("Image orientation"), "image_orientation", nullptr, TRUE, nullptr, nullptr);
685                         generic_dialog_add_message(gd, GQ_ICON_DIALOG_ERROR, _("Image orientation"), message->str, TRUE);
686                         generic_dialog_add_button(gd, GQ_ICON_OK, "OK", nullptr, TRUE);
687
688                         gtk_widget_show(gd->dialog);
689
690                         g_string_free(message, TRUE);
691                         }
692
693                 g_free(rotation);
694                 g_free(command);
695                 }
696 }
697
698 static void layout_menu_write_rotate_keep_date_cb(GtkToggleAction *action, gpointer data)
699 {
700         layout_menu_write_rotate(action, data, TRUE);
701 }
702
703 static void layout_menu_write_rotate_cb(GtkToggleAction *action, gpointer data)
704 {
705         layout_menu_write_rotate(action, data, FALSE);
706 }
707
708 static void layout_menu_config_cb(GtkAction *, gpointer data)
709 {
710         auto lw = static_cast<LayoutWindow *>(data);
711
712         layout_exit_fullscreen(lw);
713         show_config_window(lw);
714 }
715
716 static void layout_menu_editors_cb(GtkAction *, gpointer data)
717 {
718         auto lw = static_cast<LayoutWindow *>(data);
719
720         layout_exit_fullscreen(lw);
721         show_editor_list_window();
722 }
723
724 static void layout_menu_layout_config_cb(GtkAction *, gpointer data)
725 {
726         auto lw = static_cast<LayoutWindow *>(data);
727
728         layout_exit_fullscreen(lw);
729         layout_show_config_window(lw);
730 }
731
732 static void layout_menu_remove_thumb_cb(GtkAction *, gpointer data)
733 {
734         auto lw = static_cast<LayoutWindow *>(data);
735
736         layout_exit_fullscreen(lw);
737         cache_manager_show();
738 }
739
740 static void layout_menu_wallpaper_cb(GtkAction *, gpointer data)
741 {
742         auto lw = static_cast<LayoutWindow *>(data);
743
744         layout_image_to_root(lw);
745 }
746
747 /* single window zoom */
748 static void layout_menu_zoom_in_cb(GtkAction *, gpointer data)
749 {
750         auto lw = static_cast<LayoutWindow *>(data);
751
752         layout_image_zoom_adjust(lw, get_zoom_increment(), FALSE);
753 }
754
755 static void layout_menu_zoom_out_cb(GtkAction *, gpointer data)
756 {
757         auto lw = static_cast<LayoutWindow *>(data);
758
759         layout_image_zoom_adjust(lw, -get_zoom_increment(), FALSE);
760 }
761
762 static void layout_menu_zoom_1_1_cb(GtkAction *, gpointer data)
763 {
764         auto lw = static_cast<LayoutWindow *>(data);
765
766         layout_image_zoom_set(lw, 1.0, FALSE);
767 }
768
769 static void layout_menu_zoom_fit_cb(GtkAction *, gpointer data)
770 {
771         auto lw = static_cast<LayoutWindow *>(data);
772
773         layout_image_zoom_set(lw, 0.0, FALSE);
774 }
775
776 static void layout_menu_zoom_fit_hor_cb(GtkAction *, gpointer data)
777 {
778         auto lw = static_cast<LayoutWindow *>(data);
779
780         layout_image_zoom_set_fill_geometry(lw, FALSE, FALSE);
781 }
782
783 static void layout_menu_zoom_fit_vert_cb(GtkAction *, gpointer data)
784 {
785         auto lw = static_cast<LayoutWindow *>(data);
786
787         layout_image_zoom_set_fill_geometry(lw, TRUE, FALSE);
788 }
789
790 static void layout_menu_zoom_2_1_cb(GtkAction *, gpointer data)
791 {
792         auto lw = static_cast<LayoutWindow *>(data);
793
794         layout_image_zoom_set(lw, 2.0, FALSE);
795 }
796
797 static void layout_menu_zoom_3_1_cb(GtkAction *, gpointer data)
798 {
799         auto lw = static_cast<LayoutWindow *>(data);
800
801         layout_image_zoom_set(lw, 3.0, FALSE);
802 }
803 static void layout_menu_zoom_4_1_cb(GtkAction *, gpointer data)
804 {
805         auto lw = static_cast<LayoutWindow *>(data);
806
807         layout_image_zoom_set(lw, 4.0, FALSE);
808 }
809
810 static void layout_menu_zoom_1_2_cb(GtkAction *, gpointer data)
811 {
812         auto lw = static_cast<LayoutWindow *>(data);
813
814         layout_image_zoom_set(lw, -2.0, FALSE);
815 }
816
817 static void layout_menu_zoom_1_3_cb(GtkAction *, gpointer data)
818 {
819         auto lw = static_cast<LayoutWindow *>(data);
820
821         layout_image_zoom_set(lw, -3.0, FALSE);
822 }
823
824 static void layout_menu_zoom_1_4_cb(GtkAction *, gpointer data)
825 {
826         auto lw = static_cast<LayoutWindow *>(data);
827
828         layout_image_zoom_set(lw, -4.0, FALSE);
829 }
830
831 /* connected zoom */
832 static void layout_menu_connect_zoom_in_cb(GtkAction *, gpointer data)
833 {
834         auto lw = static_cast<LayoutWindow *>(data);
835
836         layout_image_zoom_adjust(lw, get_zoom_increment(), TRUE);
837 }
838
839 static void layout_menu_connect_zoom_out_cb(GtkAction *, gpointer data)
840 {
841         auto lw = static_cast<LayoutWindow *>(data);
842
843         layout_image_zoom_adjust(lw, -get_zoom_increment(), TRUE);
844 }
845
846 static void layout_menu_connect_zoom_1_1_cb(GtkAction *, gpointer data)
847 {
848         auto lw = static_cast<LayoutWindow *>(data);
849
850         layout_image_zoom_set(lw, 1.0, TRUE);
851 }
852
853 static void layout_menu_connect_zoom_fit_cb(GtkAction *, gpointer data)
854 {
855         auto lw = static_cast<LayoutWindow *>(data);
856
857         layout_image_zoom_set(lw, 0.0, TRUE);
858 }
859
860 static void layout_menu_connect_zoom_fit_hor_cb(GtkAction *, gpointer data)
861 {
862         auto lw = static_cast<LayoutWindow *>(data);
863
864         layout_image_zoom_set_fill_geometry(lw, FALSE, TRUE);
865 }
866
867 static void layout_menu_connect_zoom_fit_vert_cb(GtkAction *, gpointer data)
868 {
869         auto lw = static_cast<LayoutWindow *>(data);
870
871         layout_image_zoom_set_fill_geometry(lw, TRUE, TRUE);
872 }
873
874 static void layout_menu_connect_zoom_2_1_cb(GtkAction *, gpointer data)
875 {
876         auto lw = static_cast<LayoutWindow *>(data);
877
878         layout_image_zoom_set(lw, 2.0, TRUE);
879 }
880
881 static void layout_menu_connect_zoom_3_1_cb(GtkAction *, gpointer data)
882 {
883         auto lw = static_cast<LayoutWindow *>(data);
884
885         layout_image_zoom_set(lw, 3.0, TRUE);
886 }
887 static void layout_menu_connect_zoom_4_1_cb(GtkAction *, gpointer data)
888 {
889         auto lw = static_cast<LayoutWindow *>(data);
890
891         layout_image_zoom_set(lw, 4.0, TRUE);
892 }
893
894 static void layout_menu_connect_zoom_1_2_cb(GtkAction *, gpointer data)
895 {
896         auto lw = static_cast<LayoutWindow *>(data);
897
898         layout_image_zoom_set(lw, -2.0, TRUE);
899 }
900
901 static void layout_menu_connect_zoom_1_3_cb(GtkAction *, gpointer data)
902 {
903         auto lw = static_cast<LayoutWindow *>(data);
904
905         layout_image_zoom_set(lw, -3.0, TRUE);
906 }
907
908 static void layout_menu_connect_zoom_1_4_cb(GtkAction *, gpointer data)
909 {
910         auto lw = static_cast<LayoutWindow *>(data);
911
912         layout_image_zoom_set(lw, -4.0, TRUE);
913 }
914
915 static void layout_menu_zoom_to_rectangle_cb(GtkAction *, gpointer data)
916 {
917         auto lw = static_cast<LayoutWindow *>(data);
918         PixbufRenderer *pr;
919         gdouble zoom_height;
920         gdouble zoom_width;
921         gint center_x;
922         gint center_y;
923         gint height;
924         gint vis_height;
925         gint vis_width;
926         gint width;
927         gint x1;
928         gint x2;
929         gint x;
930         gint y1;
931         gint y2;
932         gint y;
933
934         image_get_rectangle(&x1, &y1, &x2, &y2);
935
936         pr = reinterpret_cast<PixbufRenderer *>(lw->image->pr);
937
938         vis_width = pr->vis_width;
939         vis_height = pr->vis_height;
940         zoom_width = (gdouble(vis_width) / (x2 - x1));
941         zoom_height = (gdouble(vis_height) / (y2 - y1));
942
943         pr_coords_map_orientation_reverse(pr->orientation, x1, y1, pr->image_width, pr->image_height, x2 - x1, y2 - y1, &x, &y, &width, &height);
944
945         center_x = (width / 2) + x;
946         center_y = (height / 2) + y;
947
948         layout_image_zoom_set(lw, zoom_width > zoom_height ? zoom_height : zoom_width, FALSE);
949         image_scroll_to_point(lw->image, center_x, center_y, 0.5, 0.5);
950 }
951
952 static void layout_menu_split_cb(GtkRadioAction *action, GtkRadioAction *, gpointer data)
953 {
954         auto lw = static_cast<LayoutWindow *>(data);
955         ImageSplitMode mode;
956
957         layout_exit_fullscreen(lw);
958         mode = static_cast<ImageSplitMode>(gtk_radio_action_get_current_value(action));
959         layout_split_change(lw, mode);
960 }
961
962
963 static void layout_menu_thumb_cb(GtkToggleAction *action, gpointer data)
964 {
965         auto lw = static_cast<LayoutWindow *>(data);
966
967         layout_thumb_set(lw, gtk_toggle_action_get_active(action));
968 }
969
970
971 static void layout_menu_list_cb(GtkRadioAction *action, GtkRadioAction *, gpointer data)
972 {
973         auto lw = static_cast<LayoutWindow *>(data);
974
975         layout_exit_fullscreen(lw);
976         layout_views_set(lw, lw->options.dir_view_type, static_cast<FileViewType>(gtk_radio_action_get_current_value(action)));
977 }
978
979 static void layout_menu_view_dir_as_cb(GtkToggleAction *action,  gpointer data)
980 {
981         auto lw = static_cast<LayoutWindow *>(data);
982
983         layout_exit_fullscreen(lw);
984
985         if (gtk_toggle_action_get_active(action))
986                 {
987                 layout_views_set(lw, DIRVIEW_TREE, lw->options.file_view_type);
988                 }
989         else
990                 {
991                 layout_views_set(lw, DIRVIEW_LIST, lw->options.file_view_type);
992                 }
993 }
994
995 static void layout_menu_view_in_new_window_cb(GtkAction *, gpointer data)
996 {
997         auto lw = static_cast<LayoutWindow *>(data);
998
999         layout_exit_fullscreen(lw);
1000         view_window_new(layout_image_get_fd(lw));
1001 }
1002
1003 struct OpenWithData
1004 {
1005         GAppInfo *application;
1006         GList *g_file_list;
1007         GtkWidget *app_chooser_dialog;
1008 };
1009
1010 void open_with_response_cb(GtkDialog *, gint response_id, gpointer data)
1011 {
1012         GError *error = nullptr;
1013         auto open_with_data = static_cast<OpenWithData *>(data);
1014
1015         if (response_id == GTK_RESPONSE_OK)
1016                 {
1017                 g_app_info_launch(open_with_data->application, open_with_data->g_file_list, nullptr, &error);
1018
1019                 if (error)
1020                         {
1021                         log_printf("Error launching app: %s\n", error->message);
1022                         g_error_free(error);
1023                         }
1024                 }
1025
1026         g_object_unref(open_with_data->application);
1027         g_object_unref(g_list_first(open_with_data->g_file_list)->data);
1028         g_list_free(open_with_data->g_file_list);
1029         gtk_widget_destroy(GTK_WIDGET(open_with_data->app_chooser_dialog));
1030         g_free(open_with_data);
1031 }
1032
1033 static void open_with_application_selected_cb(GtkAppChooserWidget *, GAppInfo *application, gpointer data)
1034 {
1035         auto open_with_data = static_cast<OpenWithData *>(data);
1036
1037         g_object_unref(open_with_data->application);
1038
1039         open_with_data->application = g_app_info_dup(application);
1040 }
1041
1042 static void open_with_application_activated_cb(GtkAppChooserWidget *, GAppInfo *application, gpointer data)
1043 {
1044         GError *error = nullptr;
1045         auto open_with_data = static_cast<OpenWithData *>(data);
1046
1047         g_app_info_launch(application, open_with_data->g_file_list, nullptr, &error);
1048
1049         if (error)
1050                 {
1051                 log_printf("Error launching app.: %s\n", error->message);
1052                 g_error_free(error);
1053                 }
1054
1055         g_object_unref(open_with_data->application);
1056         g_object_unref(g_list_first(open_with_data->g_file_list)->data);
1057         g_list_free(open_with_data->g_file_list);
1058         gtk_widget_destroy(GTK_WIDGET(open_with_data->app_chooser_dialog));
1059         g_free(open_with_data);
1060 }
1061
1062 static void layout_menu_open_with_cb(GtkAction *, gpointer data)
1063 {
1064         auto lw = static_cast<LayoutWindow *>(data);
1065         FileData *fd;
1066         GtkWidget *widget;
1067         OpenWithData *open_with_data;
1068
1069         if (layout_selection_list(lw))
1070                 {
1071                 open_with_data = g_new(OpenWithData, 1);
1072
1073                 fd = static_cast<FileData *>(g_list_first(layout_selection_list(lw))->data);
1074
1075                 open_with_data->g_file_list = g_list_append(nullptr, g_file_new_for_path(fd->path));
1076
1077                 open_with_data->app_chooser_dialog = gtk_app_chooser_dialog_new(nullptr, GTK_DIALOG_DESTROY_WITH_PARENT, G_FILE(g_list_first(open_with_data->g_file_list)->data));
1078
1079                 widget = gtk_app_chooser_dialog_get_widget(GTK_APP_CHOOSER_DIALOG(open_with_data->app_chooser_dialog));
1080
1081                 open_with_data->application = gtk_app_chooser_get_app_info(GTK_APP_CHOOSER(open_with_data->app_chooser_dialog));
1082
1083                 g_signal_connect(G_OBJECT(widget), "application-selected", G_CALLBACK(open_with_application_selected_cb), open_with_data);
1084                 g_signal_connect(G_OBJECT(widget), "application-activated", G_CALLBACK(open_with_application_activated_cb), open_with_data);
1085                 g_signal_connect(G_OBJECT(open_with_data->app_chooser_dialog), "response", G_CALLBACK(open_with_response_cb), open_with_data);
1086                 g_signal_connect(G_OBJECT(open_with_data->app_chooser_dialog), "close", G_CALLBACK(open_with_response_cb), open_with_data);
1087
1088                 gtk_widget_show(open_with_data->app_chooser_dialog);
1089                 }
1090 }
1091
1092 static void layout_menu_open_archive_cb(GtkAction *, gpointer data)
1093 {
1094         auto lw = static_cast<LayoutWindow *>(data);
1095         LayoutWindow *lw_new;
1096         gchar *dest_dir;
1097         FileData *fd;
1098
1099         layout_exit_fullscreen(lw);
1100         fd = layout_image_get_fd(lw);
1101
1102         if (fd->format_class == FORMAT_CLASS_ARCHIVE)
1103                 {
1104                 dest_dir = open_archive(layout_image_get_fd(lw));
1105                 if (dest_dir)
1106                         {
1107                         lw_new = layout_new_from_default();
1108                         layout_set_path(lw_new, dest_dir);
1109                         g_free(dest_dir);
1110                         }
1111                 else
1112                         {
1113                         warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GQ_ICON_DIALOG_WARNING, nullptr);
1114                         }
1115                 }
1116 }
1117
1118 static void layout_menu_fullscreen_cb(GtkAction *, gpointer data)
1119 {
1120         auto lw = static_cast<LayoutWindow *>(data);
1121
1122         layout_image_full_screen_toggle(lw);
1123 }
1124
1125 static void layout_menu_escape_cb(GtkAction *, gpointer data)
1126 {
1127         auto lw = static_cast<LayoutWindow *>(data);
1128
1129         layout_exit_fullscreen(lw);
1130 }
1131
1132 static void layout_menu_overlay_toggle_cb(GtkAction *, gpointer data)
1133 {
1134         auto lw = static_cast<LayoutWindow *>(data);
1135
1136         image_osd_toggle(lw->image);
1137         layout_util_sync_views(lw);
1138 }
1139
1140
1141 static void layout_menu_overlay_cb(GtkToggleAction *action, gpointer data)
1142 {
1143         auto lw = static_cast<LayoutWindow *>(data);
1144
1145         if (gtk_toggle_action_get_active(action))
1146                 {
1147                 OsdShowFlags flags = image_osd_get(lw->image);
1148
1149                 if ((flags | OSD_SHOW_INFO | OSD_SHOW_STATUS) != flags)
1150                         image_osd_set(lw->image, static_cast<OsdShowFlags>(flags | OSD_SHOW_INFO | OSD_SHOW_STATUS));
1151                 }
1152         else
1153                 {
1154                 GtkToggleAction *histogram_action = GTK_TOGGLE_ACTION(gtk_action_group_get_action(lw->action_group, "ImageHistogram"));
1155
1156                 image_osd_set(lw->image, OSD_SHOW_NOTHING);
1157                 gtk_toggle_action_set_active(histogram_action, FALSE); /* this calls layout_menu_histogram_cb */
1158                 }
1159 }
1160
1161 static void layout_menu_histogram_cb(GtkToggleAction *action, gpointer data)
1162 {
1163         auto lw = static_cast<LayoutWindow *>(data);
1164
1165         if (gtk_toggle_action_get_active(action))
1166                 {
1167                 image_osd_set(lw->image, static_cast<OsdShowFlags>(OSD_SHOW_INFO | OSD_SHOW_STATUS | OSD_SHOW_HISTOGRAM));
1168                 layout_util_sync_views(lw); /* show the overlay state, default channel and mode in the menu */
1169                 }
1170         else
1171                 {
1172                 OsdShowFlags flags = image_osd_get(lw->image);
1173                 if (flags & OSD_SHOW_HISTOGRAM)
1174                         image_osd_set(lw->image, static_cast<OsdShowFlags>(flags & ~OSD_SHOW_HISTOGRAM));
1175                 }
1176 }
1177
1178 static void layout_menu_animate_cb(GtkToggleAction *action, gpointer data)
1179 {
1180         auto lw = static_cast<LayoutWindow *>(data);
1181
1182         if (lw->options.animate == gtk_toggle_action_get_active(action)) return;
1183         layout_image_animate_toggle(lw);
1184 }
1185
1186 static void layout_menu_rectangular_selection_cb(GtkToggleAction *action, gpointer)
1187 {
1188         options->collections.rectangular_selection = gtk_toggle_action_get_active(action);
1189 }
1190
1191 static void layout_menu_histogram_toggle_channel_cb(GtkAction *, gpointer data)
1192 {
1193         auto lw = static_cast<LayoutWindow *>(data);
1194
1195         image_osd_histogram_toggle_channel(lw->image);
1196         layout_util_sync_views(lw);
1197 }
1198
1199 static void layout_menu_histogram_toggle_mode_cb(GtkAction *, gpointer data)
1200 {
1201         auto lw = static_cast<LayoutWindow *>(data);
1202
1203         image_osd_histogram_toggle_mode(lw->image);
1204         layout_util_sync_views(lw);
1205 }
1206
1207 static void layout_menu_histogram_channel_cb(GtkRadioAction *action, GtkRadioAction *, gpointer data)
1208 {
1209         auto lw = static_cast<LayoutWindow *>(data);
1210         gint channel = gtk_radio_action_get_current_value(action);
1211         GtkToggleAction *histogram_action = GTK_TOGGLE_ACTION(gtk_action_group_get_action(lw->action_group, "ImageHistogram"));
1212
1213         if (channel < 0 || channel >= HCHAN_COUNT) return;
1214
1215         gtk_toggle_action_set_active(histogram_action, TRUE); /* this calls layout_menu_histogram_cb */
1216         image_osd_histogram_set_channel(lw->image, channel);
1217 }
1218
1219 static void layout_menu_histogram_mode_cb(GtkRadioAction *action, GtkRadioAction *, gpointer data)
1220 {
1221         auto lw = static_cast<LayoutWindow *>(data);
1222         gint mode = gtk_radio_action_get_current_value(action);
1223         GtkToggleAction *histogram_action = GTK_TOGGLE_ACTION(gtk_action_group_get_action(lw->action_group, "ImageHistogram"));
1224
1225         if (mode < 0 || mode > 1) return;
1226
1227         gtk_toggle_action_set_active(histogram_action, TRUE); /* this calls layout_menu_histogram_cb */
1228         image_osd_histogram_set_mode(lw->image, mode);
1229 }
1230
1231 static void layout_menu_refresh_cb(GtkAction *, gpointer data)
1232 {
1233         auto lw = static_cast<LayoutWindow *>(data);
1234
1235         layout_refresh(lw);
1236 }
1237
1238 static void layout_menu_bar_exif_cb(GtkAction *, gpointer data)
1239 {
1240         auto lw = static_cast<LayoutWindow *>(data);
1241
1242         layout_exit_fullscreen(lw);
1243         layout_exif_window_new(lw);
1244 }
1245
1246 static void layout_menu_search_and_run_cb(GtkAction *, gpointer data)
1247 {
1248         auto lw = static_cast<LayoutWindow *>(data);
1249
1250         layout_exit_fullscreen(lw);
1251         layout_search_and_run_window_new(lw);
1252 }
1253
1254
1255 static void layout_menu_float_cb(GtkToggleAction *action, gpointer data)
1256 {
1257         auto lw = static_cast<LayoutWindow *>(data);
1258
1259         if (lw->options.tools_float == gtk_toggle_action_get_active(action)) return;
1260
1261         layout_exit_fullscreen(lw);
1262         layout_tools_float_toggle(lw);
1263 }
1264
1265 static void layout_menu_hide_cb(GtkAction *, gpointer data)
1266 {
1267         auto lw = static_cast<LayoutWindow *>(data);
1268
1269         layout_exit_fullscreen(lw);
1270         layout_tools_hide_toggle(lw);
1271 }
1272
1273 static void layout_menu_selectable_toolbars_cb(GtkToggleAction *action, gpointer data)
1274 {
1275         auto lw = static_cast<LayoutWindow *>(data);
1276
1277         if (lw->options.selectable_toolbars_hidden == gtk_toggle_action_get_active(action)) return;
1278
1279         layout_exit_fullscreen(lw);
1280         layout_selectable_toolbars_toggle(lw);
1281 }
1282
1283 static void layout_menu_info_pixel_cb(GtkToggleAction *action, gpointer data)
1284 {
1285         auto lw = static_cast<LayoutWindow *>(data);
1286
1287         if (lw->options.show_info_pixel == gtk_toggle_action_get_active(action)) return;
1288
1289         layout_exit_fullscreen(lw);
1290         layout_info_pixel_set(lw, !lw->options.show_info_pixel);
1291 }
1292
1293 /* NOTE: these callbacks are called also from layout_util_sync_views */
1294 static void layout_menu_bar_cb(GtkToggleAction *action, gpointer data)
1295 {
1296         auto lw = static_cast<LayoutWindow *>(data);
1297
1298         if (layout_bar_enabled(lw) == gtk_toggle_action_get_active(action)) return;
1299
1300         layout_exit_fullscreen(lw);
1301         layout_bar_toggle(lw);
1302 }
1303
1304 static void layout_menu_bar_sort_cb(GtkToggleAction *action, gpointer data)
1305 {
1306         auto lw = static_cast<LayoutWindow *>(data);
1307
1308         if (layout_bar_sort_enabled(lw) == gtk_toggle_action_get_active(action)) return;
1309
1310         layout_exit_fullscreen(lw);
1311         layout_bar_sort_toggle(lw);
1312 }
1313
1314 static void layout_menu_hide_bars_cb(GtkToggleAction *action, gpointer data)
1315 {
1316         auto lw = static_cast<LayoutWindow *>(data);
1317
1318         if (lw->options.bars_state.hidden == gtk_toggle_action_get_active(action))
1319                 {
1320                 return;
1321                 }
1322         layout_bars_hide_toggle(lw);
1323 }
1324
1325 static void layout_menu_slideshow_cb(GtkToggleAction *action, gpointer data)
1326 {
1327         auto lw = static_cast<LayoutWindow *>(data);
1328
1329         if (layout_image_slideshow_active(lw) == gtk_toggle_action_get_active(action)) return;
1330         layout_image_slideshow_toggle(lw);
1331 }
1332
1333 static void layout_menu_slideshow_pause_cb(GtkAction *, gpointer data)
1334 {
1335         auto lw = static_cast<LayoutWindow *>(data);
1336
1337         layout_image_slideshow_pause_toggle(lw);
1338 }
1339
1340 static void layout_menu_slideshow_slower_cb(GtkAction *, gpointer)
1341 {
1342         options->slideshow.delay = options->slideshow.delay + 5;
1343         if (options->slideshow.delay > SLIDESHOW_MAX_SECONDS)
1344                 options->slideshow.delay = SLIDESHOW_MAX_SECONDS;
1345 }
1346
1347 static void layout_menu_slideshow_faster_cb(GtkAction *, gpointer)
1348 {
1349         options->slideshow.delay = options->slideshow.delay - 5;
1350         if (options->slideshow.delay < SLIDESHOW_MIN_SECONDS * 10)
1351                 options->slideshow.delay = SLIDESHOW_MIN_SECONDS * 10;
1352 }
1353
1354
1355 static void layout_menu_stereo_mode_next_cb(GtkAction *, gpointer data)
1356 {
1357         auto lw = static_cast<LayoutWindow *>(data);
1358         gint mode = layout_image_stereo_pixbuf_get(lw);
1359
1360         /* 0->1, 1->2, 2->3, 3->1 - disable auto, then cycle */
1361         mode = mode % 3 + 1;
1362
1363         GtkAction *radio = gtk_action_group_get_action(lw->action_group, "StereoAuto");
1364         gtk_radio_action_set_current_value(GTK_RADIO_ACTION(radio), mode);
1365
1366         /*
1367         this is called via fallback in layout_menu_stereo_mode_cb
1368         layout_image_stereo_pixbuf_set(lw, mode);
1369         */
1370
1371 }
1372
1373 static void layout_menu_stereo_mode_cb(GtkRadioAction *action, GtkRadioAction *, gpointer data)
1374 {
1375         auto lw = static_cast<LayoutWindow *>(data);
1376         gint mode = gtk_radio_action_get_current_value(action);
1377         layout_image_stereo_pixbuf_set(lw, mode);
1378 }
1379
1380 static void layout_menu_help_cb(GtkAction *, gpointer data)
1381 {
1382         auto lw = static_cast<LayoutWindow *>(data);
1383
1384         layout_exit_fullscreen(lw);
1385         help_window_show("index.html");
1386 }
1387
1388 static void layout_menu_help_search_cb(GtkAction *, gpointer data)
1389 {
1390         auto lw = static_cast<LayoutWindow *>(data);
1391
1392         layout_exit_fullscreen(lw);
1393         help_search_window_show();
1394 }
1395
1396 static void layout_menu_help_keys_cb(GtkAction *, gpointer data)
1397 {
1398         auto lw = static_cast<LayoutWindow *>(data);
1399
1400         layout_exit_fullscreen(lw);
1401         help_window_show("GuideReferenceKeyboardShortcuts.html");
1402 }
1403
1404 static void layout_menu_notes_cb(GtkAction *, gpointer data)
1405 {
1406         auto lw = static_cast<LayoutWindow *>(data);
1407
1408         layout_exit_fullscreen(lw);
1409         help_window_show("release_notes");
1410 }
1411
1412 static void layout_menu_changelog_cb(GtkAction *, gpointer data)
1413 {
1414         auto lw = static_cast<LayoutWindow *>(data);
1415
1416         layout_exit_fullscreen(lw);
1417         help_window_show("changelog");
1418 }
1419
1420 static constexpr const char *keyboard_map_hardcoded[][2] = {
1421         {"Scroll","Left"},
1422         {"FastScroll", "&lt;Shift&gt;Left"},
1423         {"Left Border", "&lt;Primary&gt;Left"},
1424         {"Left Border", "&lt;Primary&gt;&lt;Shift&gt;Left"},
1425         {"Scroll", "Right"},
1426         {"FastScroll", "&lt;Shift&gt;Right"},
1427         {"Right Border", "&lt;Primary&gt;Right"},
1428         {"Right Border", "&lt;Primary&gt;&lt;Shift&gt;Right"},
1429         {"Scroll", "Up"},
1430         {"FastScroll", "&lt;Shift&gt;Up"},
1431         {"Upper Border", "&lt;Primary&gt;Up"},
1432         {"Upper Border", "&lt;Primary&gt;&lt;Shift&gt;Up"},
1433         {"Scroll", "Down"},
1434         {"FastScroll", "&lt;Shift&gt;Down"},
1435         {"Lower Border", "&lt;Primary&gt;Down"},
1436         {"Lower Border", "&lt;Primary&gt;&lt;Shift&gt;Down"},
1437         {"Next/Drag", "M1"},
1438         {"FastDrag", "&lt;Shift&gt;M1"},
1439         {"DnD Start", "M2"},
1440         {"Menu", "M3"},
1441         {"PrevImage", "MW4"},
1442         {"NextImage", "MW5"},
1443         {"ScrollUp", "&lt;Shift&gt;MW4"},
1444         {"ScrollDown", "&lt;Shift&gt;MW5"},
1445         {"ZoomIn", "&lt;Primary&gt;MW4"},
1446         {"ZoomOut", "&lt;Primary&gt;MW5"},
1447 };
1448
1449 static void layout_menu_foreach_func(
1450                                         gpointer data,
1451                                         const gchar *accel_path,
1452                                         guint accel_key,
1453                                         GdkModifierType accel_mods,
1454                                         gboolean)
1455 {
1456         gchar *path;
1457         gchar *name;
1458         gchar *key_name;
1459         gchar *menu_name;
1460         gchar **subset_lt_arr;
1461         gchar **subset_gt_arr;
1462         gchar *subset_lt;
1463         gchar *converted_name;
1464         auto array = static_cast<GPtrArray *>(data);
1465
1466         path = g_strescape(accel_path, nullptr);
1467         name = gtk_accelerator_name(accel_key, accel_mods);
1468
1469         menu_name = g_strdup(g_strrstr(path, "/")+1);
1470
1471         if (g_strrstr(name, ">"))
1472                 {
1473                 subset_lt_arr = g_strsplit_set(name,"<", 4);
1474                 subset_lt = g_strjoinv("&lt;", subset_lt_arr);
1475                 subset_gt_arr = g_strsplit_set(subset_lt,">", 4);
1476                 converted_name = g_strjoinv("&gt;", subset_gt_arr);
1477                 key_name = g_strdup(converted_name);
1478
1479                 g_free(converted_name);
1480                 g_free(subset_lt);
1481                 g_strfreev(subset_lt_arr);
1482                 g_strfreev(subset_gt_arr);
1483                 }
1484         else
1485                 key_name = g_strdup(name);
1486
1487         g_ptr_array_add(array, menu_name);
1488         g_ptr_array_add(array, key_name);
1489
1490         g_free(name);
1491         g_free(path);
1492 }
1493
1494 static void layout_menu_kbd_map_cb(GtkAction *, gpointer)
1495 {
1496         gint fd = -1;
1497         GPtrArray *array;
1498         char * tmp_file;
1499         GError *error = nullptr;
1500         GIOChannel *channel;
1501         char **pre_key;
1502         char **post_key;
1503         const char *key_name;
1504         char *converted_line;
1505         int keymap_index;
1506         guint index;
1507
1508         fd = g_file_open_tmp("geeqie_keymap_XXXXXX.svg", &tmp_file, &error);
1509         if (error)
1510                 {
1511                 log_printf("Error: Keyboard Map - cannot create file:%s\n",error->message);
1512                 g_error_free(error);
1513                 }
1514         else
1515                 {
1516                 array = g_ptr_array_new();
1517
1518                 gtk_accel_map_foreach(array, layout_menu_foreach_func);
1519
1520                 channel = g_io_channel_unix_new(fd);
1521
1522                 keymap_index = 0;
1523                 while (keymap_template[keymap_index])
1524                         {
1525                         if (g_strrstr(keymap_template[keymap_index], ">key:"))
1526                                 {
1527                                 pre_key = g_strsplit(keymap_template[keymap_index],">key:",2);
1528                                 post_key = g_strsplit(pre_key[1],"<",2);
1529
1530                                 index=0;
1531                                 key_name = " ";
1532                                 for (index=0; index < array->len-2; index=index+2)
1533                                         {
1534                                         if (!(g_ascii_strcasecmp(static_cast<const gchar *>(g_ptr_array_index(array,index+1)), post_key[0])))
1535                                                 {
1536                                                 key_name = static_cast<const gchar *>(g_ptr_array_index(array,index+0));
1537                                                 break;
1538                                                 }
1539                                         }
1540
1541                                 for (const auto& m : keyboard_map_hardcoded)
1542                                         {
1543                                         if (!(g_strcmp0(m[1], post_key[0])))
1544                                                 {
1545                                                 key_name = m[0];
1546                                                 break;
1547                                                 }
1548                                         }
1549
1550                                 converted_line = g_strconcat(pre_key[0], ">", key_name, "<", post_key[1], "\n", NULL);
1551                                 g_io_channel_write_chars(channel, converted_line, -1, nullptr, &error);
1552                                 if (error) {log_printf("Warning: Keyboard Map:%s\n",error->message); g_error_free(error);}
1553
1554                                 g_free(converted_line);
1555                                 g_strfreev(pre_key);
1556                                 g_strfreev(post_key);
1557                                 }
1558                         else
1559                                 {
1560                                 g_io_channel_write_chars(channel, keymap_template[keymap_index], -1, nullptr, &error);
1561                                 if (error) {log_printf("Warning: Keyboard Map:%s\n",error->message); g_error_free(error);}
1562                                 g_io_channel_write_chars(channel, "\n", -1, nullptr, &error);
1563                                 if (error) {log_printf("Warning: Keyboard Map:%s\n",error->message); g_error_free(error);}
1564                                 }
1565                         keymap_index++;
1566                         }
1567
1568                 g_io_channel_flush(channel, &error);
1569                 if (error) {log_printf("Warning: Keyboard Map:%s\n",error->message); g_error_free(error);}
1570                 g_io_channel_unref(channel);
1571
1572                 index=0;
1573                 for (index=0; index < array->len-2; index=index+2)
1574                         {
1575                         g_free(g_ptr_array_index(array,index));
1576                         g_free(g_ptr_array_index(array,index+1));
1577                         }
1578                 g_ptr_array_unref(array);
1579
1580                 view_window_new(file_data_new_simple(tmp_file));
1581                 g_free(tmp_file);
1582                 }
1583 }
1584
1585 static void layout_menu_about_cb(GtkAction *, gpointer data)
1586 {
1587         auto lw = static_cast<LayoutWindow *>(data);
1588
1589         layout_exit_fullscreen(lw);
1590         show_about_window(lw);
1591 }
1592
1593 static void layout_menu_log_window_cb(GtkAction *, gpointer data)
1594 {
1595         auto lw = static_cast<LayoutWindow *>(data);
1596
1597         layout_exit_fullscreen(lw);
1598         log_window_new(lw);
1599 }
1600
1601
1602 /*
1603  *-----------------------------------------------------------------------------
1604  * select menu
1605  *-----------------------------------------------------------------------------
1606  */
1607
1608 static void layout_menu_select_all_cb(GtkAction *, gpointer data)
1609 {
1610         auto lw = static_cast<LayoutWindow *>(data);
1611
1612         layout_select_all(lw);
1613 }
1614
1615 static void layout_menu_unselect_all_cb(GtkAction *, gpointer data)
1616 {
1617         auto lw = static_cast<LayoutWindow *>(data);
1618
1619         layout_select_none(lw);
1620 }
1621
1622 static void layout_menu_invert_selection_cb(GtkAction *, gpointer data)
1623 {
1624         auto lw = static_cast<LayoutWindow *>(data);
1625
1626         layout_select_invert(lw);
1627 }
1628
1629 static void layout_menu_file_filter_cb(GtkToggleAction *action, gpointer data)
1630 {
1631         auto lw = static_cast<LayoutWindow *>(data);
1632
1633         layout_file_filter_set(lw, gtk_toggle_action_get_active(action));
1634 }
1635
1636 static void layout_menu_marks_cb(GtkToggleAction *action, gpointer data)
1637 {
1638         auto lw = static_cast<LayoutWindow *>(data);
1639
1640         layout_marks_set(lw, gtk_toggle_action_get_active(action));
1641 }
1642
1643
1644 static void layout_menu_set_mark_sel_cb(GtkAction *action, gpointer data)
1645 {
1646         auto lw = static_cast<LayoutWindow *>(data);
1647         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
1648         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
1649
1650         layout_selection_to_mark(lw, mark, STM_MODE_SET);
1651 }
1652
1653 static void layout_menu_res_mark_sel_cb(GtkAction *action, gpointer data)
1654 {
1655         auto lw = static_cast<LayoutWindow *>(data);
1656         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
1657         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
1658
1659         layout_selection_to_mark(lw, mark, STM_MODE_RESET);
1660 }
1661
1662 static void layout_menu_toggle_mark_sel_cb(GtkAction *action, gpointer data)
1663 {
1664         auto lw = static_cast<LayoutWindow *>(data);
1665         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
1666         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
1667
1668         layout_selection_to_mark(lw, mark, STM_MODE_TOGGLE);
1669 }
1670
1671 static void layout_menu_sel_mark_cb(GtkAction *action, gpointer data)
1672 {
1673         auto lw = static_cast<LayoutWindow *>(data);
1674         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
1675         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
1676
1677         layout_mark_to_selection(lw, mark, MTS_MODE_SET);
1678 }
1679
1680 static void layout_menu_sel_mark_or_cb(GtkAction *action, gpointer data)
1681 {
1682         auto lw = static_cast<LayoutWindow *>(data);
1683         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
1684         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
1685
1686         layout_mark_to_selection(lw, mark, MTS_MODE_OR);
1687 }
1688
1689 static void layout_menu_sel_mark_and_cb(GtkAction *action, gpointer data)
1690 {
1691         auto lw = static_cast<LayoutWindow *>(data);
1692         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
1693         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
1694
1695         layout_mark_to_selection(lw, mark, MTS_MODE_AND);
1696 }
1697
1698 static void layout_menu_sel_mark_minus_cb(GtkAction *action, gpointer data)
1699 {
1700         auto lw = static_cast<LayoutWindow *>(data);
1701         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
1702         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
1703
1704         layout_mark_to_selection(lw, mark, MTS_MODE_MINUS);
1705 }
1706
1707 static void layout_menu_mark_filter_toggle_cb(GtkAction *action, gpointer data)
1708 {
1709         auto lw = static_cast<LayoutWindow *>(data);
1710         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
1711         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
1712
1713         layout_marks_set(lw, TRUE);
1714         layout_mark_filter_toggle(lw, mark);
1715 }
1716
1717
1718 /*
1719  *-----------------------------------------------------------------------------
1720  * go menu
1721  *-----------------------------------------------------------------------------
1722  */
1723
1724 static void layout_menu_image_first_cb(GtkAction *, gpointer data)
1725 {
1726         auto lw = static_cast<LayoutWindow *>(data);
1727         layout_image_first(lw);
1728 }
1729
1730 static void layout_menu_image_prev_cb(GtkAction *, gpointer data)
1731 {
1732         auto lw = static_cast<LayoutWindow *>(data);
1733         gint i;
1734
1735         if (lw->options.split_pane_sync)
1736                 {
1737                 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1738                         {
1739                         if (lw->split_images[i])
1740                                 {
1741                                 if (i != -1)
1742                                         {
1743                                         DEBUG_1("image activate scroll %d", i);
1744                                         layout_image_activate(lw, i, FALSE);
1745                                         layout_image_prev(lw);
1746                                         }
1747                                 }
1748                         }
1749                 }
1750         else
1751                 {
1752                 layout_image_prev(lw);
1753                 }
1754 }
1755
1756 static void layout_menu_image_next_cb(GtkAction *, gpointer data)
1757 {
1758         auto lw = static_cast<LayoutWindow *>(data);
1759         gint i;
1760
1761         if (lw->options.split_pane_sync)
1762                 {
1763                 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1764                         {
1765                         if (lw->split_images[i])
1766                                 {
1767                                 if (i != -1)
1768                                         {
1769                                         DEBUG_1("image activate scroll %d", i);
1770                                         layout_image_activate(lw, i, FALSE);
1771                                         layout_image_next(lw);
1772                                         }
1773                                 }
1774                         }
1775                 }
1776         else
1777                 {
1778                 layout_image_next(lw);
1779                 }
1780 }
1781
1782 static void layout_menu_page_first_cb(GtkAction *, gpointer data)
1783 {
1784         auto lw = static_cast<LayoutWindow *>(data);
1785         FileData *fd = layout_image_get_fd(lw);
1786
1787         if (fd->page_total > 0)
1788                 {
1789                 file_data_set_page_num(fd, 1);
1790                 }
1791 }
1792
1793 static void layout_menu_page_last_cb(GtkAction *, gpointer data)
1794 {
1795         auto lw = static_cast<LayoutWindow *>(data);
1796         FileData *fd = layout_image_get_fd(lw);
1797
1798         if (fd->page_total > 0)
1799                 {
1800                 file_data_set_page_num(fd, -1);
1801                 }
1802 }
1803
1804 static void layout_menu_page_next_cb(GtkAction *, gpointer data)
1805 {
1806         auto lw = static_cast<LayoutWindow *>(data);
1807         FileData *fd = layout_image_get_fd(lw);
1808
1809         if (fd->page_total > 0)
1810                 {
1811                 file_data_inc_page_num(fd);
1812                 }
1813 }
1814
1815 static void layout_menu_page_previous_cb(GtkAction *, gpointer data)
1816 {
1817         auto lw = static_cast<LayoutWindow *>(data);
1818         FileData *fd = layout_image_get_fd(lw);
1819
1820         if (fd->page_total > 0)
1821                 {
1822                 file_data_dec_page_num(fd);
1823                 }
1824 }
1825
1826 static void layout_menu_image_forward_cb(GtkAction *, gpointer data)
1827 {
1828         auto lw = static_cast<LayoutWindow *>(data);
1829
1830         /* Obtain next image */
1831         layout_set_path(lw, image_chain_forward());
1832 }
1833
1834 static void layout_menu_image_back_cb(GtkAction *, gpointer data)
1835 {
1836         auto lw = static_cast<LayoutWindow *>(data);
1837
1838         /* Obtain previous image */
1839         layout_set_path(lw, image_chain_back());
1840 }
1841
1842 static void layout_menu_split_pane_next_cb(GtkAction *, gpointer data)
1843 {
1844         auto lw = static_cast<LayoutWindow *>(data);
1845         gint active_frame;
1846
1847         active_frame = lw->active_split_image;
1848
1849         if (active_frame < MAX_SPLIT_IMAGES-1 && lw->split_images[active_frame+1] )
1850                 {
1851                 active_frame++;
1852                 }
1853         else
1854                 {
1855                 active_frame = 0;
1856                 }
1857         layout_image_activate(lw, active_frame, FALSE);
1858 }
1859
1860 static void layout_menu_split_pane_prev_cb(GtkAction *, gpointer data)
1861 {
1862         auto lw = static_cast<LayoutWindow *>(data);
1863         gint active_frame;
1864
1865         active_frame = lw->active_split_image;
1866
1867         if (active_frame >=1 && lw->split_images[active_frame-1] )
1868                 {
1869                 active_frame--;
1870                 }
1871         else
1872                 {
1873                 active_frame = MAX_SPLIT_IMAGES-1;
1874                 while (!lw->split_images[active_frame])
1875                         {
1876                         active_frame--;
1877                         }
1878                 }
1879         layout_image_activate(lw, active_frame, FALSE);
1880 }
1881
1882 static void layout_menu_split_pane_updown_cb(GtkAction *, gpointer data)
1883 {
1884         auto lw = static_cast<LayoutWindow *>(data);
1885         gint active_frame;
1886
1887         active_frame = lw->active_split_image;
1888
1889         if (lw->split_images[MAX_SPLIT_IMAGES-1] )
1890                 {
1891                 active_frame = active_frame ^ 2;
1892                 }
1893         else
1894                 {
1895                 active_frame = active_frame ^ 1;
1896                 }
1897         layout_image_activate(lw, active_frame, FALSE);
1898 }
1899
1900 static void layout_menu_image_last_cb(GtkAction *, gpointer data)
1901 {
1902         auto lw = static_cast<LayoutWindow *>(data);
1903         layout_image_last(lw);
1904 }
1905
1906 static void layout_menu_back_cb(GtkAction *, gpointer data)
1907 {
1908         auto lw = static_cast<LayoutWindow *>(data);
1909         FileData *dir_fd;
1910
1911         /* Obtain previous path */
1912         dir_fd = file_data_new_dir(history_chain_back());
1913         layout_set_fd(lw, dir_fd);
1914         file_data_unref(dir_fd);
1915 }
1916
1917 static void layout_menu_forward_cb(GtkAction *, gpointer data)
1918 {
1919         auto lw = static_cast<LayoutWindow *>(data);
1920         FileData *dir_fd;
1921
1922         /* Obtain next path */
1923         dir_fd = file_data_new_dir(history_chain_forward());
1924         layout_set_fd(lw, dir_fd);
1925         file_data_unref(dir_fd);
1926 }
1927
1928 static void layout_menu_home_cb(GtkAction *, gpointer data)
1929 {
1930         auto lw = static_cast<LayoutWindow *>(data);
1931         const gchar *path;
1932
1933         if (lw->options.home_path && *lw->options.home_path)
1934                 path = lw->options.home_path;
1935         else
1936                 path = homedir();
1937
1938         if (path)
1939                 {
1940                 FileData *dir_fd = file_data_new_dir(path);
1941                 layout_set_fd(lw, dir_fd);
1942                 file_data_unref(dir_fd);
1943                 }
1944 }
1945
1946 static void layout_menu_up_cb(GtkAction *, gpointer data)
1947 {
1948         auto lw = static_cast<LayoutWindow *>(data);
1949         ViewDir *vd = lw->vd;
1950         gchar *path;
1951
1952         if (!vd->dir_fd || strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) == 0) return;
1953         path = remove_level_from_path(vd->dir_fd->path);
1954
1955         if (vd->select_func)
1956                 {
1957                 FileData *fd = file_data_new_dir(path);
1958                 vd->select_func(vd, fd, vd->select_data);
1959                 file_data_unref(fd);
1960                 }
1961
1962         g_free(path);
1963 }
1964
1965
1966 /*
1967  *-----------------------------------------------------------------------------
1968  * edit menu
1969  *-----------------------------------------------------------------------------
1970  */
1971
1972 static void layout_menu_edit_cb(GtkAction *action, gpointer data)
1973 {
1974         auto lw = static_cast<LayoutWindow *>(data);
1975         const gchar *key = gtk_action_get_name(action);
1976
1977         if (!editor_window_flag_set(key))
1978                 layout_exit_fullscreen(lw);
1979
1980         file_util_start_editor_from_filelist(key, layout_selection_list(lw), layout_get_path(lw), lw->window);
1981 }
1982
1983
1984 static void layout_menu_metadata_write_cb(GtkAction *, gpointer)
1985 {
1986         metadata_write_queue_confirm(TRUE, nullptr, nullptr);
1987 }
1988
1989 static GtkWidget *last_focussed = nullptr;
1990 static void layout_menu_keyword_autocomplete_cb(GtkAction *, gpointer data)
1991 {
1992         auto lw = static_cast<LayoutWindow *>(data);
1993         GtkWidget *tmp;
1994         gboolean auto_has_focus;
1995
1996         tmp = gtk_window_get_focus(GTK_WINDOW(lw->window));
1997         auto_has_focus = bar_keywords_autocomplete_focus(lw);
1998
1999         if (auto_has_focus)
2000                 {
2001                 gtk_widget_grab_focus(last_focussed);
2002                 }
2003         else
2004                 {
2005                 last_focussed = tmp;
2006                 }
2007 }
2008
2009 /*
2010  *-----------------------------------------------------------------------------
2011  * color profile button (and menu)
2012  *-----------------------------------------------------------------------------
2013  */
2014 #if HAVE_LCMS
2015 static void layout_color_menu_enable_cb(GtkToggleAction *action, gpointer data)
2016 {
2017         auto lw = static_cast<LayoutWindow *>(data);
2018
2019         if (layout_image_color_profile_get_use(lw) == gtk_toggle_action_get_active(action)) return;
2020
2021         layout_image_color_profile_set_use(lw, gtk_toggle_action_get_active(action));
2022         layout_util_sync_color(lw);
2023         layout_image_refresh(lw);
2024 }
2025
2026 static void layout_color_menu_use_image_cb(GtkToggleAction *action, gpointer data)
2027 {
2028         auto lw = static_cast<LayoutWindow *>(data);
2029         gint input;
2030         gboolean use_image;
2031
2032         if (!layout_image_color_profile_get(lw, &input, &use_image)) return;
2033         if (use_image == gtk_toggle_action_get_active(action)) return;
2034         layout_image_color_profile_set(lw, input, gtk_toggle_action_get_active(action));
2035         layout_util_sync_color(lw);
2036         layout_image_refresh(lw);
2037 }
2038
2039 static void layout_color_menu_input_cb(GtkRadioAction *action, GtkRadioAction *, gpointer data)
2040 {
2041         auto lw = static_cast<LayoutWindow *>(data);
2042         gint type;
2043         gint input;
2044         gboolean use_image;
2045
2046         type = gtk_radio_action_get_current_value(action);
2047         if (type < 0 || type >= COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS) return;
2048
2049         if (!layout_image_color_profile_get(lw, &input, &use_image)) return;
2050         if (type == input) return;
2051
2052         layout_image_color_profile_set(lw, type, use_image);
2053         layout_image_refresh(lw);
2054 }
2055 #else
2056 static void layout_color_menu_enable_cb()
2057 {
2058 }
2059
2060 static void layout_color_menu_use_image_cb()
2061 {
2062 }
2063
2064 static void layout_color_menu_input_cb()
2065 {
2066 }
2067 #endif
2068
2069
2070 /*
2071  *-----------------------------------------------------------------------------
2072  * recent menu
2073  *-----------------------------------------------------------------------------
2074  */
2075
2076 static void layout_menu_recent_cb(GtkWidget *widget, gpointer)
2077 {
2078         gint n;
2079         gchar *path;
2080
2081         n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "recent_index"));
2082
2083         path = static_cast<gchar *>(g_list_nth_data(history_list_get_by_key("recent"), n));
2084
2085         if (!path) return;
2086
2087         /* make a copy of it */
2088         path = g_strdup(path);
2089         collection_window_new(path);
2090         g_free(path);
2091 }
2092
2093 static void layout_menu_collection_recent_update(LayoutWindow *lw)
2094 {
2095         GtkWidget *menu;
2096         GtkWidget *recent;
2097         GtkWidget *item;
2098         GList *list;
2099         gint n;
2100
2101         if (!lw->ui_manager) return;
2102
2103         list = history_list_get_by_key("recent");
2104         n = 0;
2105
2106         menu = gtk_menu_new();
2107
2108         while (list)
2109                 {
2110                 const gchar *filename = filename_from_path(static_cast<gchar *>(list->data));
2111                 gchar *name;
2112                 gboolean free_name = FALSE;
2113
2114                 if (file_extension_match(filename, GQ_COLLECTION_EXT))
2115                         {
2116                         name = remove_extension_from_path(filename);
2117                         free_name = TRUE;
2118                         }
2119                 else
2120                         {
2121                         name = const_cast<gchar *>(filename);
2122                         }
2123
2124                 item = menu_item_add_simple(menu, name, G_CALLBACK(layout_menu_recent_cb), lw);
2125                 if (free_name) g_free(name);
2126                 g_object_set_data(G_OBJECT(item), "recent_index", GINT_TO_POINTER(n));
2127                 list = list->next;
2128                 n++;
2129                 }
2130
2131         if (n == 0)
2132                 {
2133                 menu_item_add(menu, _("Empty"), nullptr, nullptr);
2134                 }
2135
2136         recent = gtk_ui_manager_get_widget(lw->ui_manager, options->hamburger_menu ? "/MainMenu/OpenMenu/FileMenu/OpenRecent" : "/MainMenu/FileMenu/OpenRecent");
2137         gtk_menu_item_set_submenu(GTK_MENU_ITEM(recent), menu);
2138         gtk_widget_set_sensitive(recent, (n != 0));
2139 }
2140
2141 static void layout_menu_collection_open_update(LayoutWindow *lw)
2142 {
2143         gboolean free_name = FALSE;
2144         gchar *name;
2145         gint n;
2146         GList *collection_list = nullptr;
2147         GList *work;
2148         GtkWidget *item;
2149         GtkWidget *menu;
2150         GtkWidget *recent;
2151
2152         if (!lw->ui_manager) return;
2153
2154         collect_manager_list(&collection_list, nullptr, nullptr);
2155
2156         n = 0;
2157
2158         menu = gtk_menu_new();
2159
2160         work = collection_list;
2161         while (work)
2162                 {
2163                 const gchar *filename = static_cast<gchar *>(work->data);
2164
2165                 if (file_extension_match(filename, GQ_COLLECTION_EXT))
2166                         {
2167                         name = remove_extension_from_path(filename);
2168                         free_name = TRUE;
2169                         }
2170                 else
2171                         {
2172                         name = const_cast<gchar *>(filename);
2173                         }
2174
2175                 item = menu_item_add_simple(menu, name, G_CALLBACK(layout_menu_open_cb), lw);
2176                 if (free_name)
2177                         {
2178                         g_free(name);
2179                         }
2180                 g_object_set_data(G_OBJECT(item), "recent_index", GINT_TO_POINTER(n));
2181                 work = work->next;
2182                 n++;
2183                 }
2184
2185         string_list_free(collection_list);
2186
2187         if (n == 0)
2188                 {
2189                 menu_item_add(menu, _("Empty"), nullptr, nullptr);
2190                 }
2191
2192         recent = gtk_ui_manager_get_widget(lw->ui_manager, options->hamburger_menu ? "/MainMenu/OpenMenu/FileMenu/OpenCollection" : "/MainMenu/FileMenu/OpenCollection");
2193         gtk_menu_item_set_submenu(GTK_MENU_ITEM(recent), menu);
2194         gtk_widget_set_sensitive(recent, (n != 0));
2195 }
2196
2197 void layout_recent_update_all()
2198 {
2199         GList *work;
2200
2201         work = layout_window_list;
2202         while (work)
2203                 {
2204                 auto lw = static_cast<LayoutWindow *>(work->data);
2205                 work = work->next;
2206
2207                 layout_menu_collection_recent_update(lw);
2208                 layout_menu_collection_open_update(lw);
2209                 }
2210 }
2211
2212 void layout_recent_add_path(const gchar *path)
2213 {
2214         if (!path) return;
2215
2216         history_list_add_to_key("recent", path, options->open_recent_list_maxsize);
2217
2218         layout_recent_update_all();
2219 }
2220
2221 /*
2222  *-----------------------------------------------------------------------------
2223  * window layout menu
2224  *-----------------------------------------------------------------------------
2225  */
2226 struct WindowNames
2227 {
2228         gboolean displayed;
2229         gchar *name;
2230         gchar *path;
2231 };
2232
2233 struct RenameWindow
2234 {
2235         GenericDialog *gd;
2236         LayoutWindow *lw;
2237
2238         GtkWidget *button_ok;
2239         GtkWidget *window_name_entry;
2240 };
2241
2242 struct DeleteWindow
2243 {
2244         GenericDialog *gd;
2245         LayoutWindow *lw;
2246
2247         GtkWidget *button_ok;
2248         GtkWidget *group;
2249 };
2250
2251 static gint layout_window_menu_list_sort_cb(gconstpointer a, gconstpointer b)
2252 {
2253         auto wna = static_cast<const WindowNames *>(a);
2254         auto wnb = static_cast<const WindowNames *>(b);
2255
2256         return g_strcmp0(wna->name, wnb->name);
2257 }
2258
2259 static GList *layout_window_menu_list(GList *listin)
2260 {
2261         GList *list;
2262         WindowNames *wn;
2263         gboolean dupe;
2264         DIR *dp;
2265         struct dirent *dir;
2266         gchar *pathl;
2267
2268         pathl = path_from_utf8(get_window_layouts_dir());
2269         dp = opendir(pathl);
2270         if (!dp)
2271                 {
2272                 /* dir not found */
2273                 g_free(pathl);
2274                 return listin;
2275                 }
2276
2277         while ((dir = readdir(dp)) != nullptr)
2278                 {
2279                 gchar *name_file = dir->d_name;
2280
2281                 if (g_str_has_suffix(name_file, ".xml"))
2282                         {
2283                         LayoutWindow *lw_tmp ;
2284                         gchar *name_utf8 = path_to_utf8(name_file);
2285                         gchar *name_base = g_strndup(name_utf8, strlen(name_utf8) - 4);
2286                         list = layout_window_list;
2287                         dupe = FALSE;
2288                         while (list)
2289                                 {
2290                                 lw_tmp = static_cast<LayoutWindow *>(list->data);
2291                                 if (g_strcmp0(lw_tmp->options.id, name_base) == 0)
2292                                         {
2293                                         dupe = TRUE;
2294                                         }
2295                                 list = list->next;
2296                                 }
2297                         gchar *dpath = g_build_filename(pathl, name_utf8, NULL);
2298                         wn  = g_new0(WindowNames, 1);
2299                         wn->displayed = dupe;
2300                         wn->name = g_strdup(name_base);
2301                         wn->path = g_strdup(dpath);
2302                         listin = g_list_append(listin, wn);
2303
2304                         g_free(dpath);
2305                         g_free(name_utf8);
2306                         g_free(name_base);
2307                         }
2308                 }
2309         closedir(dp);
2310
2311         g_free(pathl);
2312
2313         return g_list_sort(listin, layout_window_menu_list_sort_cb);
2314 }
2315
2316 static void layout_menu_new_window_cb(GtkWidget *, gpointer data)
2317 {
2318         gint n;
2319
2320         n = GPOINTER_TO_INT(data);
2321         GList *menulist = nullptr;
2322
2323         menulist = layout_window_menu_list(menulist);
2324         auto wn = static_cast<WindowNames *>(g_list_nth(menulist, n )->data);
2325
2326         if (wn->path)
2327                 {
2328                 load_config_from_file(wn->path, FALSE);
2329                 }
2330         else
2331                 {
2332                 log_printf(_("Error: window layout name: %s does not exist\n"), wn->path);
2333                 }
2334 }
2335
2336 static void layout_menu_new_window_update(LayoutWindow *lw)
2337 {
2338         GtkWidget *menu;
2339         GtkWidget *sub_menu;
2340         GtkWidget *item;
2341         GList *children;
2342         GList *iter;
2343         gint n;
2344         GList *list = nullptr;
2345         gint i = 0;
2346         WindowNames *wn;
2347
2348         if (!lw->ui_manager) return;
2349
2350         list = layout_window_menu_list(list);
2351
2352         menu = gtk_ui_manager_get_widget(lw->ui_manager, options->hamburger_menu ? "/MainMenu/OpenMenu/WindowsMenu/NewWindow" : "/MainMenu/WindowsMenu/NewWindow");
2353         sub_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu));
2354
2355         children = gtk_container_get_children(GTK_CONTAINER(sub_menu));
2356         for (iter = children; iter != nullptr; iter = g_list_next(iter), i++)
2357                 {
2358                 if (i >= 4) // separator, default, from current, separator
2359                         {
2360                         gq_gtk_widget_destroy(GTK_WIDGET(iter->data));
2361                         }
2362                 }
2363         g_list_free(children);
2364
2365         menu_item_add_divider(sub_menu);
2366
2367         n = 0;
2368         while (list)
2369                 {
2370                 wn = static_cast<WindowNames *>(list->data);
2371                 item = menu_item_add_simple(sub_menu, wn->name, G_CALLBACK(layout_menu_new_window_cb), GINT_TO_POINTER(n));
2372                 if (wn->displayed)
2373                         {
2374                         gtk_widget_set_sensitive(item, FALSE);
2375                         }
2376                 list = list->next;
2377                 n++;
2378                 }
2379 }
2380
2381 static void window_rename_cancel_cb(GenericDialog *, gpointer data)
2382 {
2383         auto rw = static_cast<RenameWindow *>(data);
2384
2385         generic_dialog_close(rw->gd);
2386         g_free(rw);
2387 }
2388
2389 static void window_rename_ok(GenericDialog *, gpointer data)
2390 {
2391         auto rw = static_cast<RenameWindow *>(data);
2392         gchar *path;
2393         gboolean window_layout_name_exists = FALSE;
2394         GList *list = nullptr;
2395         gchar *xml_name;
2396         gchar *new_id;
2397
2398         new_id = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(rw->window_name_entry)));
2399
2400         list = layout_window_menu_list(list);
2401         while (list)
2402                 {
2403                 auto ln = static_cast<WindowNames *>(list->data);
2404                 if (g_strcmp0(ln->name, new_id) == 0)
2405                         {
2406                         gchar *buf;
2407                         buf = g_strdup_printf(_("Window layout name \"%s\" already exists."), new_id);
2408                         warning_dialog(_("Rename window"), buf, GQ_ICON_DIALOG_WARNING, rw->gd->dialog);
2409                         g_free(buf);
2410                         window_layout_name_exists = TRUE;
2411                         break;
2412                         }
2413                 list = list->next;
2414                 }
2415
2416         if (!window_layout_name_exists)
2417                 {
2418                 xml_name = g_strdup_printf("%s.xml", rw->lw->options.id);
2419                 path = g_build_filename(get_window_layouts_dir(), xml_name, NULL);
2420
2421                 if (isfile(path))
2422                         {
2423                         unlink_file(path);
2424                         }
2425                 g_free(xml_name);
2426                 g_free(path);
2427
2428                 g_free(rw->lw->options.id);
2429                 rw->lw->options.id = g_strdup(new_id);
2430                 layout_menu_new_window_update(rw->lw);
2431                 layout_refresh(rw->lw);
2432                 image_update_title(rw->lw->image);
2433                 }
2434
2435         save_layout(rw->lw);
2436
2437         g_free(new_id);
2438         generic_dialog_close(rw->gd);
2439         g_free(rw);
2440 }
2441
2442 static void window_rename_ok_cb(GenericDialog *gd, gpointer data)
2443 {
2444         auto rw = static_cast<RenameWindow *>(data);
2445
2446         window_rename_ok(gd, rw);
2447 }
2448
2449 static void window_rename_entry_activate_cb(GenericDialog *gd, gpointer data)
2450 {
2451         auto rw = static_cast<RenameWindow *>(data);
2452
2453         window_rename_ok(gd, rw);
2454 }
2455
2456 static void window_delete_cancel_cb(GenericDialog *, gpointer data)
2457 {
2458         auto dw = static_cast<DeleteWindow *>(data);
2459
2460         g_free(dw);
2461 }
2462
2463 static void window_delete_ok_cb(GenericDialog *, gpointer data)
2464 {
2465         auto dw = static_cast<DeleteWindow *>(data);
2466         gchar *path;
2467         gchar *xml_name;
2468
2469         xml_name = g_strdup_printf("%s.xml", dw->lw->options.id);
2470         path = g_build_filename(get_window_layouts_dir(), xml_name, NULL);
2471
2472         layout_close(dw->lw);
2473         g_free(dw);
2474
2475         if (isfile(path))
2476                 {
2477                 unlink_file(path);
2478                 }
2479         g_free(xml_name);
2480         g_free(path);
2481 }
2482
2483 static void layout_menu_window_default_cb(GtkWidget *, gpointer)
2484 {
2485         layout_new_from_default();
2486 }
2487
2488 static void layout_menu_windows_menu_cb(GtkWidget *, gpointer data)
2489 {
2490         auto lw = static_cast<LayoutWindow *>(data);
2491         GtkWidget *menu;
2492         GtkWidget *sub_menu;
2493         gchar *menu_label;
2494         GList *children;
2495         GList *iter;
2496         gint i;
2497
2498         menu = gtk_ui_manager_get_widget(lw->ui_manager, options->hamburger_menu ? "/MainMenu/OpenMenu/WindowsMenu/" : "/MainMenu/WindowsMenu/");
2499
2500         sub_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu));
2501
2502         /* disable Delete for temporary windows */
2503         if (g_str_has_prefix(lw->options.id, "lw"))
2504                 {
2505                 i = 0;
2506                 children = gtk_container_get_children(GTK_CONTAINER(sub_menu));
2507                 for (iter = children; iter != nullptr; iter = g_list_next(iter), i++)
2508                         {
2509                         menu_label = g_strdup(gtk_menu_item_get_label(GTK_MENU_ITEM(iter->data)));
2510                         if (g_strcmp0(menu_label, _("Delete window")) == 0)
2511                                 {
2512                                 gtk_widget_set_sensitive(GTK_WIDGET(iter->data), FALSE);
2513                                 }
2514                         g_free(menu_label);
2515                         }
2516                 g_list_free(children);
2517                 }
2518 }
2519
2520 static void layout_menu_view_menu_cb(GtkWidget *, gpointer data)
2521 {
2522         auto lw = static_cast<LayoutWindow *>(data);
2523         GtkWidget *menu;
2524         GtkWidget *sub_menu;
2525         gchar *menu_label;
2526         GList *children;
2527         GList *iter;
2528         gint i;
2529         FileData *fd;
2530
2531         menu = gtk_ui_manager_get_widget(lw->ui_manager, options->hamburger_menu ? "/MainMenu/OpenMenu/ViewMenu/" : "/MainMenu/ViewMenu/");
2532         sub_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu));
2533
2534         fd = layout_image_get_fd(lw);
2535
2536         i = 0;
2537         children = gtk_container_get_children(GTK_CONTAINER(sub_menu));
2538         for (iter = children; iter != nullptr; iter = g_list_next(iter), i++)
2539                 {
2540                 menu_label = g_strdup(gtk_menu_item_get_label(GTK_MENU_ITEM(iter->data)));
2541                 if (g_strcmp0(menu_label, _("Open archive")) == 0)
2542                         {
2543                         if (fd && fd->format_class == FORMAT_CLASS_ARCHIVE)
2544                                 {
2545                                 gtk_widget_set_sensitive(GTK_WIDGET(iter->data), TRUE);
2546                                 }
2547                         else
2548                                 {
2549                                 gtk_widget_set_sensitive(GTK_WIDGET(iter->data), FALSE);
2550                                 }
2551                         }
2552                 g_free(menu_label);
2553                 }
2554         g_list_free(children);
2555 }
2556
2557 static void change_window_id(const gchar *infile, const gchar *outfile)
2558 {
2559         GFile *in_file;
2560         GFile *out_file;
2561         GFileInputStream *in_file_stream;
2562         GFileOutputStream *out_file_stream;
2563         GDataInputStream *in_data_stream;
2564         GDataOutputStream *out_data_stream;
2565         gchar *line;
2566         gchar *id_name;
2567
2568         id_name = layout_get_unique_id();
2569
2570         in_file = g_file_new_for_path(infile);
2571         in_file_stream = g_file_read(in_file, nullptr, nullptr);
2572         in_data_stream = g_data_input_stream_new(G_INPUT_STREAM(in_file_stream));
2573
2574         out_file = g_file_new_for_path(outfile);
2575         out_file_stream = g_file_append_to(out_file, G_FILE_CREATE_PRIVATE, nullptr, nullptr);
2576         out_data_stream = g_data_output_stream_new(G_OUTPUT_STREAM(out_file_stream));
2577
2578         while ((line = g_data_input_stream_read_line(in_data_stream, nullptr, nullptr, nullptr)))
2579                 {
2580                 if (g_str_has_suffix(line, "<layout"))
2581                         {
2582                         g_data_output_stream_put_string(out_data_stream, line, nullptr, nullptr);
2583                         g_data_output_stream_put_string(out_data_stream, "\n", nullptr, nullptr);
2584                         g_free(line);
2585
2586                         line = g_data_input_stream_read_line(in_data_stream, nullptr, nullptr, nullptr);
2587                         g_data_output_stream_put_string(out_data_stream, "id = \"", nullptr, nullptr);
2588                         g_data_output_stream_put_string(out_data_stream, id_name, nullptr, nullptr);
2589                         g_data_output_stream_put_string(out_data_stream, "\"\n", nullptr, nullptr);
2590                         }
2591                 else
2592                         {
2593                         g_data_output_stream_put_string(out_data_stream, line, nullptr, nullptr);
2594                         g_data_output_stream_put_string(out_data_stream, "\n", nullptr, nullptr);
2595                         }
2596                 g_free(line);
2597                 }
2598
2599         g_free(id_name);
2600         g_object_unref(out_data_stream);
2601         g_object_unref(in_data_stream);
2602         g_object_unref(out_file_stream);
2603         g_object_unref(in_file_stream);
2604         g_object_unref(out_file);
2605         g_object_unref(in_file);
2606 }
2607
2608 static void layout_menu_window_from_current_cb(GtkWidget *, gpointer data)
2609 {
2610         auto lw = static_cast<LayoutWindow *>(data);
2611         gint fd_in = -1;
2612         gint fd_out = -1;
2613         char * tmp_file_in;
2614         char * tmp_file_out;
2615         GError *error = nullptr;
2616
2617         fd_in = g_file_open_tmp("geeqie_layout_name_XXXXXX.xml", &tmp_file_in, &error);
2618         if (error)
2619                 {
2620                 log_printf("Error: Window layout - cannot create file:%s\n",error->message);
2621                 g_error_free(error);
2622                 return;
2623                 }
2624         close(fd_in);
2625         fd_out = g_file_open_tmp("geeqie_layout_name_XXXXXX.xml", &tmp_file_out, &error);
2626         if (error)
2627                 {
2628                 log_printf("Error: Window layout - cannot create file:%s\n",error->message);
2629                 g_error_free(error);
2630                 return;
2631                 }
2632         close(fd_out);
2633
2634         save_config_to_file(tmp_file_in, options, lw);
2635         change_window_id(tmp_file_in, tmp_file_out);
2636         load_config_from_file(tmp_file_out, FALSE);
2637
2638         unlink_file(tmp_file_in);
2639         unlink_file(tmp_file_out);
2640         g_free(tmp_file_in);
2641         g_free(tmp_file_out);
2642 }
2643
2644 static void layout_menu_window_cb(GtkWidget *, gpointer data)
2645 {
2646         auto lw = static_cast<LayoutWindow *>(data);
2647
2648         layout_menu_new_window_update(lw);
2649 }
2650
2651 static void layout_menu_window_rename_cb(GtkWidget *, gpointer data)
2652 {
2653         auto lw = static_cast<LayoutWindow *>(data);
2654         RenameWindow *rw;
2655         GtkWidget *hbox;
2656
2657         rw = g_new0(RenameWindow, 1);
2658         rw->lw = lw;
2659
2660         rw->gd = generic_dialog_new(_("Rename window"), "rename_window", nullptr, FALSE, window_rename_cancel_cb, rw);
2661         rw->button_ok = generic_dialog_add_button(rw->gd, GQ_ICON_OK, _("OK"), window_rename_ok_cb, TRUE);
2662
2663         generic_dialog_add_message(rw->gd, nullptr, _("rename window"), nullptr, FALSE);
2664
2665         hbox = pref_box_new(rw->gd->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, 0);
2666         pref_spacer(hbox, PREF_PAD_INDENT);
2667
2668         hbox = pref_box_new(rw->gd->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
2669
2670         rw->window_name_entry = gtk_entry_new();
2671         gtk_widget_set_can_focus(rw->window_name_entry, TRUE);
2672         gtk_editable_set_editable(GTK_EDITABLE(rw->window_name_entry), TRUE);
2673         gq_gtk_entry_set_text(GTK_ENTRY(rw->window_name_entry), lw->options.id);
2674         gq_gtk_box_pack_start(GTK_BOX(hbox), rw->window_name_entry, TRUE, TRUE, 0);
2675         gtk_widget_grab_focus(GTK_WIDGET(rw->window_name_entry));
2676         gtk_widget_show(rw->window_name_entry);
2677         g_signal_connect(rw->window_name_entry, "activate", G_CALLBACK(window_rename_entry_activate_cb), rw);
2678
2679         gtk_widget_show(rw->gd->dialog);
2680 }
2681
2682 static void layout_menu_window_delete_cb(GtkWidget *, gpointer data)
2683 {
2684         auto lw = static_cast<LayoutWindow *>(data);
2685         DeleteWindow *dw;
2686         GtkWidget *hbox;
2687
2688         dw = g_new0(DeleteWindow, 1);
2689         dw->lw = lw;
2690
2691         dw->gd = generic_dialog_new(_("Delete window"), "delete_window", nullptr, TRUE, window_delete_cancel_cb, dw);
2692         dw->button_ok = generic_dialog_add_button(dw->gd, GQ_ICON_OK, _("OK"), window_delete_ok_cb, TRUE);
2693
2694         generic_dialog_add_message(dw->gd, nullptr, _("Delete window layout"), nullptr, FALSE);
2695
2696         hbox = pref_box_new(dw->gd->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, 0);
2697         pref_spacer(hbox, PREF_PAD_INDENT);
2698         dw->group = pref_box_new(hbox, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
2699
2700         hbox = pref_box_new(dw->group, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
2701         pref_label_new(hbox, (lw->options.id));
2702
2703         gtk_widget_show(dw->gd->dialog);
2704 }
2705
2706 /*
2707  *-----------------------------------------------------------------------------
2708  * menu
2709  *-----------------------------------------------------------------------------
2710  */
2711
2712 #define CB G_CALLBACK
2713 /**
2714  * tooltip is used as the description field in the Help manual shortcuts documentation
2715  *
2716  * struct GtkActionEntry:
2717  *  name, stock_id, label, accelerator, tooltip, callback
2718  */
2719 static GtkActionEntry menu_entries[] = {
2720   { "About",                 GQ_ICON_ABOUT,                     N_("_About"),                                           nullptr,               N_("About"),                                           CB(layout_menu_about_cb) },
2721   { "AlterNone",             PIXBUF_INLINE_ICON_ORIGINAL,       N_("_Original state"),                                  "<shift>O",            N_("Image rotate Original state"),                     CB(layout_menu_alter_none_cb) },
2722   { "Back",                  GQ_ICON_GO_PREV,                   N_("_Back"),                                            nullptr,               N_("Back in folder history"),                          CB(layout_menu_back_cb) },
2723   { "ClearMarks",            nullptr,                           N_("Clear Marks..."),                                   nullptr,               N_("Clear Marks"),                                     CB(layout_menu_clear_marks_cb) },
2724   { "CloseWindow",           GQ_ICON_CLOSE,                     N_("C_lose window"),                                    "<control>W",          N_("Close window"),                                    CB(layout_menu_close_cb) },
2725   { "ColorMenu",             nullptr,                           N_("_Color Management"),                                nullptr,               nullptr,                                               nullptr },
2726   { "ConnectZoom100Alt1",    GQ_ICON_ZOOM_100,                  N_("Zoom _1:1"),                                        "<shift>KP_Divide",    N_("Connected Zoom 1:1"),                              CB(layout_menu_connect_zoom_1_1_cb) },                 
2727   { "ConnectZoom100",        GQ_ICON_ZOOM_100,                  N_("Zoom _1:1"),                                        "<shift>Z",            N_("Connected Zoom 1:1"),                              CB(layout_menu_connect_zoom_1_1_cb) },
2728   { "ConnectZoom200",        nullptr,                           N_("Zoom _2:1"),                                        nullptr,               N_("Connected Zoom 2:1"),                              CB(layout_menu_connect_zoom_2_1_cb) },
2729   { "ConnectZoom25",         nullptr,                           N_("Zoom 1:4"),                                         nullptr,               N_("Connected Zoom 1:4"),                              CB(layout_menu_connect_zoom_1_4_cb) },
2730   { "ConnectZoom300",        nullptr,                           N_("Zoom _3:1"),                                        nullptr,               N_("Connected Zoom 3:1"),                              CB(layout_menu_connect_zoom_3_1_cb) },
2731   { "ConnectZoom33",         nullptr,                           N_("Zoom 1:3"),                                         nullptr,               N_("Connected Zoom 1:3"),                              CB(layout_menu_connect_zoom_1_3_cb) },
2732   { "ConnectZoom400",        nullptr,                           N_("Zoom _4:1"),                                        nullptr,               N_("Connected Zoom 4:1"),                              CB(layout_menu_connect_zoom_4_1_cb) },
2733   { "ConnectZoom50",         nullptr,                           N_("Zoom 1:2"),                                         nullptr,               N_("Connected Zoom 1:2"),                              CB(layout_menu_connect_zoom_1_2_cb) },
2734   { "ConnectZoomFillHor",    nullptr,                           N_("Fit _Horizontally"),                                "<shift>H",            N_("Connected Fit Horizontally"),                      CB(layout_menu_connect_zoom_fit_hor_cb) },             
2735   { "ConnectZoomFillVert",   nullptr,                           N_("Fit _Vertically"),                                  "<shift>W",            N_("Connected Fit Vertically"),                        CB(layout_menu_connect_zoom_fit_vert_cb) },            
2736   { "ConnectZoomFitAlt1",    GQ_ICON_ZOOM_FIT,                  N_("_Zoom to fit"),                                     "<shift>KP_Multiply",  N_("Connected Zoom to fit"),                           CB(layout_menu_connect_zoom_fit_cb) },                 
2737   { "ConnectZoomFit",        GQ_ICON_ZOOM_FIT,                  N_("_Zoom to fit"),                                     "<shift>X",            N_("Connected Zoom to fit"),                           CB(layout_menu_connect_zoom_fit_cb) },
2738   { "ConnectZoomInAlt1",     GQ_ICON_ZOOM_IN,                   N_("Zoom _in"),                                         "<shift>KP_Add",       N_("Connected Zoom in"),                               CB(layout_menu_connect_zoom_in_cb) },                  
2739   { "ConnectZoomIn",         GQ_ICON_ZOOM_IN,                   N_("Zoom _in"),                                         "plus",                N_("Connected Zoom in"),                               CB(layout_menu_connect_zoom_in_cb) },
2740   { "ConnectZoomMenu",       nullptr,                           N_("_Connected Zoom"),                                  nullptr,               nullptr,                                               nullptr },
2741   { "ConnectZoomOutAlt1",    GQ_ICON_ZOOM_OUT,                  N_("Zoom _out"),                                        "<shift>KP_Subtract",  N_("Connected Zoom out"),                              CB(layout_menu_connect_zoom_out_cb) },                 
2742   { "ConnectZoomOut",        GQ_ICON_ZOOM_OUT,                  N_("Zoom _out"),                                        "underscore",          N_("Connected Zoom out"),                              CB(layout_menu_connect_zoom_out_cb) },
2743   { "Copy",                  GQ_ICON_COPY,                      N_("_Copy..."),                                         "<control>C",          N_("Copy..."),                                         CB(layout_menu_copy_cb) },
2744   { "CopyPath",              nullptr,                           N_("_Copy to clipboard"),                          nullptr,               N_("Copy to clipboard"),                          CB(layout_menu_copy_path_cb) },
2745   { "CopyPathUnquoted",      nullptr,                           N_("_Copy to clipboard (unquoted)"),                 nullptr,               N_("Copy to clipboard (unquoted)"),                 CB(layout_menu_copy_path_unquoted_cb) },
2746   { "CutPath",               nullptr,                           N_("_Cut to clipboard"),                           "<control>X",          N_("Cut to clipboard"),                           CB(layout_menu_cut_path_cb) },
2747   { "DeleteAlt1",            GQ_ICON_USER_TRASH,                N_("Move to Trash..."),                                 "Delete",              N_("Move to Trash..."),                                CB(layout_menu_move_to_trash_key_cb) },                
2748   { "DeleteAlt2",            GQ_ICON_USER_TRASH,                N_("Move to Trash..."),                                 "KP_Delete",           N_("Move to Trash..."),                                CB(layout_menu_move_to_trash_key_cb) },                
2749   { "Delete",                GQ_ICON_USER_TRASH,                N_("Move to Trash..."),                                 "<control>D",          N_("Move to Trash..."),                                CB(layout_menu_move_to_trash_cb) },
2750   { "DeleteWindow",          GQ_ICON_DELETE,                    N_("Delete window"),                                    nullptr,               N_("Delete window"),                                   CB(layout_menu_window_delete_cb) },
2751   { "DisableGrouping",       nullptr,                           N_("Disable file groupi_ng"),                           nullptr,               N_("Disable file grouping"),                           CB(layout_menu_disable_grouping_cb) },
2752   { "EditMenu",              nullptr,                           N_("_Edit"),                                            nullptr,               nullptr,                                               nullptr },
2753   { "EnableGrouping",        nullptr,                           N_("Enable file _grouping"),                            nullptr,               N_("Enable file grouping"),                            CB(layout_menu_enable_grouping_cb) },
2754   { "EscapeAlt1",            GQ_ICON_LEAVE_FULLSCREEN,          N_("_Leave full screen"),                               "Q",                   N_("Leave full screen"),                               CB(layout_menu_escape_cb) },                           
2755   { "Escape",                GQ_ICON_LEAVE_FULLSCREEN,          N_("_Leave full screen"),                              "Escape",               N_("Leave full screen"),                               CB(layout_menu_escape_cb) },                           
2756   { "ExifWin",               PIXBUF_INLINE_ICON_EXIF,           N_("_Exif window"),                                     "<control>E",          N_("Exif window"),                                     CB(layout_menu_bar_exif_cb) },
2757   { "FileDirMenu",           nullptr,                           N_("_Files and Folders"),                               nullptr,               nullptr,                                               nullptr },
2758   { "FileMenu",              nullptr,                           N_("_File"),                                            nullptr,               nullptr,                                               nullptr },
2759   { "FindDupes",             GQ_ICON_FIND,                      N_("_Find duplicates..."),                              "D",                   N_("Find duplicates..."),                              CB(layout_menu_dupes_cb) },
2760   { "FirstImage",            GQ_ICON_GO_TOP,                    N_("_First Image"),                                     "Home",                N_("First Image"),                                     CB(layout_menu_image_first_cb) },
2761   { "FirstPage",             GQ_ICON_PREV_PAGE,                 N_("_First Page"),                                      nullptr,               N_( "First Page of multi-page image"),                 CB(layout_menu_page_first_cb) },                       
2762   { "Flip",                  GQ_ICON_FLIP_VERTICAL,             N_("_Flip"),                                            "<shift>F",            N_("Image Flip"),                                      CB(layout_menu_alter_flip_cb) },
2763   { "Forward",               GQ_ICON_GO_NEXT,                   N_("_Forward"),                                         nullptr,               N_("Forward in folder history"),                       CB(layout_menu_forward_cb) },
2764   { "FullScreenAlt1",        GQ_ICON_FULLSCREEN,                N_("F_ull screen"),                                     "V",                   N_("Full screen"),                                     CB(layout_menu_fullscreen_cb) },
2765   { "FullScreenAlt2",        GQ_ICON_FULLSCREEN,                N_("F_ull screen"),                                     "F11",                 N_("Full screen"),                                     CB(layout_menu_fullscreen_cb) },
2766   { "FullScreen",            GQ_ICON_FULLSCREEN,                N_("F_ull screen"),                                     "F",                   N_("Full screen"),                                     CB(layout_menu_fullscreen_cb) },
2767   { "GoMenu",                nullptr,                           N_("_Go"),                                              nullptr,               nullptr,                                               nullptr },
2768   { "HelpChangeLog",         nullptr,                           N_("_ChangeLog"),                                       nullptr,               N_("ChangeLog notes"),                                 CB(layout_menu_changelog_cb) },
2769   { "HelpContents",          GQ_ICON_HELP,                      N_("_Help manual"),                                     "F1",                  N_("Help manual"),                                     CB(layout_menu_help_cb) },
2770   { "HelpKbd",               nullptr,                           N_("_Keyboard map"),                                    nullptr,               N_("Keyboard map"),                                    CB(layout_menu_kbd_map_cb) },
2771   { "HelpMenu",              nullptr,                           N_("_Help"),                                            nullptr,               nullptr,                                               nullptr },
2772   { "HelpNotes",             nullptr,                           N_("_Readme"),                                          nullptr,               N_("Readme"),                                          CB(layout_menu_notes_cb) },
2773   { "HelpSearch",            nullptr,                           N_("On-line help search"),                              nullptr,               N_("On-line help search"),                             CB(layout_menu_help_search_cb) },
2774   { "HelpShortcuts",         nullptr,                           N_("_Keyboard shortcuts"),                              nullptr,               N_("Keyboard shortcuts"),                              CB(layout_menu_help_keys_cb) },
2775   { "HideTools",             PIXBUF_INLINE_ICON_HIDETOOLS,      N_("_Hide file list"),                                  "<control>H",          N_("Hide file list"),                                  CB(layout_menu_hide_cb) },
2776   { "HistogramChanCycle",    nullptr,                           N_("Cycle through histogram ch_annels"),                "K",                   N_("Cycle through histogram channels"),                CB(layout_menu_histogram_toggle_channel_cb) },                                                         
2777   { "HistogramModeCycle",    nullptr,                           N_("Cycle through histogram mo_des"),                   "J",                   N_("Cycle through histogram modes"),                   CB(layout_menu_histogram_toggle_mode_cb) },            
2778   { "Home",                  GQ_ICON_HOME,                      N_("_Home"),                                            nullptr,               N_("Home"),                                            CB(layout_menu_home_cb) },
2779   { "ImageBack",             GQ_ICON_GO_FIRST,                  N_("Image Back"),                                       nullptr,               N_("Back in image history"),                           CB(layout_menu_image_back_cb) },
2780   { "ImageForward",          GQ_ICON_GO_LAST,                   N_("Image Forward"),                                    nullptr,               N_("Forward in image history"),                        CB(layout_menu_image_forward_cb) },
2781   { "ImageOverlayCycle",     nullptr,                           N_("_Cycle through overlay modes"),                     "I",                   N_("Cycle through Overlay modes"),                     CB(layout_menu_overlay_toggle_cb) },                   
2782   { "KeywordAutocomplete",   nullptr,                           N_("Keyword autocomplete"),                             "<alt>K",              N_("Keyword Autocomplete"),                            CB(layout_menu_keyword_autocomplete_cb) },
2783   { "LastImage",             GQ_ICON_GO_BOTTOM,                 N_("_Last Image"),                                      "End",                 N_("Last Image"),                                      CB(layout_menu_image_last_cb) },
2784   { "LastPage",              GQ_ICON_NEXT_PAGE,                 N_("_Last Page"),                                       nullptr,               N_("Last Page of multi-page image"),                   CB(layout_menu_page_last_cb) },
2785   { "LayoutConfig",          GQ_ICON_PREFERENCES,               N_("_Configure this window..."),                        nullptr,               N_("Configure this window..."),                        CB(layout_menu_layout_config_cb) },
2786   { "LogWindow",             nullptr,                           N_("_Log Window"),                                      nullptr,               N_("Log Window"),                                      CB(layout_menu_log_window_cb) },
2787   { "Maintenance",           PIXBUF_INLINE_ICON_MAINTENANCE,    N_("_Cache maintenance..."),                            nullptr,               N_("Cache maintenance..."),                            CB(layout_menu_remove_thumb_cb) },
2788   { "Mirror",                GQ_ICON_FLIP_HORIZONTAL,           N_("_Mirror"),                                          "<shift>M",            N_("Image Mirror"),                                    CB(layout_menu_alter_mirror_cb) },
2789   { "Move",                  PIXBUF_INLINE_ICON_MOVE,           N_("_Move..."),                                         "<control>M",          N_("Move..."),                                         CB(layout_menu_move_cb) },
2790   { "NewCollection",         GQ_ICON_COLLECTION,                N_("_New collection"),                                  "C",                   N_("New collection"),                                  CB(layout_menu_new_cb) },
2791   { "NewFolder",             GQ_ICON_DIRECTORY,                 N_("N_ew folder..."),                                   "<control>F",          N_("New folder..."),                                   CB(layout_menu_dir_cb) },
2792   { "NewWindowDefault",      nullptr,                           N_("default"),                                          "<control>N",          N_("New window (default)"),                            CB(layout_menu_window_default_cb)  },
2793   { "NewWindowFromCurrent",  nullptr,                           N_("from current"),                                     nullptr,               N_("from current"),                                    CB(layout_menu_window_from_current_cb)  },
2794   { "NewWindow",             nullptr,                           N_("New window"),                                       nullptr,               N_("New window"),                                      CB(layout_menu_window_cb) },
2795   { "NextImageAlt1",         GQ_ICON_GO_DOWN,                   N_("_Next Image"),                                      "Page_Down",           N_("Next Image"),                                      CB(layout_menu_image_next_cb) },
2796   { "NextImageAlt2",         GQ_ICON_GO_DOWN,                   N_("_Next Image"),                                      "KP_Page_Down",        N_("Next Image"),                                      CB(layout_menu_image_next_cb) },
2797   { "NextImage",             GQ_ICON_GO_DOWN,                   N_("_Next Image"),                                      "space",               N_("Next Image"),                                      CB(layout_menu_image_next_cb) },
2798   { "NextPage",              GQ_ICON_FORWARD_PAGE,              N_("_Next Page"),                                       nullptr,               N_("Next Page of multi-page image"),                   CB(layout_menu_page_next_cb) },
2799   { "OpenArchive",           GQ_ICON_OPEN,                      N_("Open archive"),                                     nullptr,               N_("Open archive"),                                    CB(layout_menu_open_archive_cb) },
2800   { "OpenCollection",        GQ_ICON_OPEN,                      N_("_Open collection..."),                              "O",                   N_("Open collection..."),                              nullptr },
2801   { "OpenMenu",              nullptr,                           N_("☰"),                                                nullptr,               nullptr,                                               nullptr },
2802   { "OpenRecent",            nullptr,                           N_("Open recen_t"),                                     nullptr,               N_("Open recent collection"),                          nullptr },
2803   { "OpenWith",              GQ_ICON_OPEN_WITH,                 N_("Open With..."),                                     nullptr,               N_("Open With..."),                                    CB(layout_menu_open_with_cb) },
2804   { "OrientationMenu",       nullptr,                           N_("_Orientation"),                                     nullptr,               nullptr,                                               nullptr },
2805   { "OverlayMenu",           nullptr,                           N_("Image _Overlay"),                                   nullptr,               nullptr,                                               nullptr },
2806   { "PanView",               PIXBUF_INLINE_ICON_PANORAMA,       N_("Pa_n view"),                                        "<control>J",          N_("Pan view"),                                        CB(layout_menu_pan_cb) },
2807   { "PermanentDelete",       GQ_ICON_DELETE,                    N_("Delete..."),                                        "<shift>Delete",       N_("Delete..."),                                       CB(layout_menu_delete_cb) },                           
2808   { "Plugins",               GQ_ICON_PREFERENCES,               N_("Configure _Plugins..."),                            nullptr,               N_("Configure Plugins..."),                            CB(layout_menu_editors_cb) },
2809   { "PluginsMenu",           nullptr,                           N_("_Plugins"),                                         nullptr,               nullptr,                                               nullptr },
2810   { "Preferences",           GQ_ICON_PREFERENCES,               N_("P_references..."),                                  "<control>O",          N_("Preferences..."),                                  CB(layout_menu_config_cb) },
2811   { "PreferencesMenu",       nullptr,                           N_("P_references"),                                     nullptr,               nullptr,                                               nullptr },
2812   { "PrevImageAlt1",         GQ_ICON_GO_UP,                     N_("_Previous Image"),                                  "Page_Up",             N_("Previous Image"),                                  CB(layout_menu_image_prev_cb) },
2813   { "PrevImageAlt2",         GQ_ICON_GO_UP,                     N_("_Previous Image"),                                  "KP_Page_Up",          N_("Previous Image"),                                  CB(layout_menu_image_prev_cb) },
2814   { "PrevImage",             GQ_ICON_GO_UP,                     N_("_Previous Image"),                                  "BackSpace",           N_("Previous Image"),                                  CB(layout_menu_image_prev_cb) },
2815   { "PrevPage",              GQ_ICON_BACK_PAGE,                 N_("_Previous Page"),                                   nullptr,               N_("Previous Page of multi-page image"),               CB(layout_menu_page_previous_cb) },
2816   { "Print",                 GQ_ICON_PRINT,                     N_("_Print..."),                                        "<shift>P",            N_("Print..."),                                        CB(layout_menu_print_cb) },
2817   { "Quit",                  GQ_ICON_QUIT,                      N_("_Quit"),                                            "<control>Q",          N_("Quit"),                                            CB(layout_menu_exit_cb) },
2818   { "Rating0",               nullptr,                           N_("_Rating 0"),                                        "<alt>KP_0",           N_("Rating 0"),                                        CB(layout_menu_rating_0_cb) },
2819   { "Rating1",               nullptr,                           N_("_Rating 1"),                                        "<alt>KP_1",           N_("Rating 1"),                                        CB(layout_menu_rating_1_cb) },
2820   { "Rating2",               nullptr,                           N_("_Rating 2"),                                        "<alt>KP_2",           N_("Rating 2"),                                        CB(layout_menu_rating_2_cb) },
2821   { "Rating3",               nullptr,                           N_("_Rating 3"),                                        "<alt>KP_3",           N_("Rating 3"),                                        CB(layout_menu_rating_3_cb) },
2822   { "Rating4",               nullptr,                           N_("_Rating 4"),                                        "<alt>KP_4",           N_("Rating 4"),                                        CB(layout_menu_rating_4_cb) },
2823   { "Rating5",               nullptr,                           N_("_Rating 5"),                                        "<alt>KP_5",           N_("Rating 5"),                                        CB(layout_menu_rating_5_cb) },
2824   { "RatingM1",              nullptr,                           N_("_Rating -1"),                                       "<alt>KP_Subtract",    N_("Rating -1"),                                       CB(layout_menu_rating_m1_cb) },
2825   { "RatingMenu",            nullptr,                           N_("_Rating"),                                          nullptr,               nullptr,                                               nullptr },
2826   { "Refresh",               GQ_ICON_REFRESH,                   N_("_Refresh"),                                         "R",                   N_("Refresh"),                                         CB(layout_menu_refresh_cb) },
2827   { "Rename",                PIXBUF_INLINE_ICON_RENAME,         N_("_Rename..."),                                       "<control>R",          N_("Rename..."),                                       CB(layout_menu_rename_cb) },
2828   { "RenameWindow",          GQ_ICON_EDIT,                      N_("Rename window"),                                    nullptr,               N_("Rename window"),                                   CB(layout_menu_window_rename_cb) },
2829   { "Rotate180",             PIXBUF_INLINE_ICON_180,            N_("Rotate 1_80°"),                                     "<shift>R",            N_("Image Rotate 180°"),                               CB(layout_menu_alter_180_cb) },
2830   { "RotateCCW",             GQ_ICON_ROTATE_LEFT,               N_("Rotate _counterclockwise 90°"),                     "bracketleft",         N_("Rotate counterclockwise 90°"),                     CB(layout_menu_alter_90cc_cb) },
2831   { "RotateCW",              GQ_ICON_ROTATE_RIGHT,              N_("_Rotate clockwise 90°"),                            "bracketright",        N_("Image Rotate clockwise 90°"),                      CB(layout_menu_alter_90_cb) },
2832   { "SaveMetadata",          GQ_ICON_SAVE,                      N_("_Save metadata"),                                   "<control>S",          N_("Save metadata"),                                   CB(layout_menu_metadata_write_cb) },
2833   { "SearchAndRunCommand",   GQ_ICON_FIND,                      N_("Search and Run command"),                           "slash",               N_("Search commands by keyword and run them"),         CB(layout_menu_search_and_run_cb) },
2834   { "Search",                GQ_ICON_FIND,                      N_("_Search..."),                                       "F3",                  N_("Search..."),                                       CB(layout_menu_search_cb) },
2835   { "SelectAll",             PIXBUF_INLINE_ICON_SELECT_ALL,     N_("Select _all"),                                      "<control>A",          N_("Select all"),                                      CB(layout_menu_select_all_cb) },
2836   { "SelectInvert",          PIXBUF_INLINE_ICON_SELECT_INVERT,  N_("_Invert Selection"),                                "<control><shift>I",   N_("Invert Selection"),                                CB(layout_menu_invert_selection_cb) },
2837   { "SelectMenu",            nullptr,                           N_("_Select"),                                          nullptr,               nullptr,                                               nullptr },
2838   { "SelectNone",            PIXBUF_INLINE_ICON_SELECT_NONE,    N_("Select _none"),                                     "<control><shift>A",   N_("Select none"),                                     CB(layout_menu_unselect_all_cb) },
2839   { "SlideShowFaster",       GQ_ICON_GENERIC,                   N_("Faster"),                                           "<control>equal",      N_("Slideshow Faster"),                                CB(layout_menu_slideshow_faster_cb) },
2840   { "SlideShowPause",        GQ_ICON_PAUSE,                     N_("_Pause slideshow"),                                 "P",                   N_("Pause slideshow"),                                 CB(layout_menu_slideshow_pause_cb) },
2841   { "SlideShowSlower",       GQ_ICON_GENERIC,                   N_("Slower"),                                           "<control>minus",      N_("Slideshow Slower"),                                CB(layout_menu_slideshow_slower_cb) },
2842   { "SplitDownPane",         nullptr,                           N_("_Down Pane"),                                       "<alt>Down",           N_("Down Split Pane"),                                 CB(layout_menu_split_pane_updown_cb) },
2843   { "SplitMenu",             nullptr,                           N_("Spli_t"),                                           nullptr,               nullptr,                                               nullptr },
2844   { "SplitNextPane",         nullptr,                           N_("_Next Pane"),                                       "<alt>Right",          N_("Next Split Pane"),                                 CB(layout_menu_split_pane_next_cb) },
2845   { "SplitPreviousPane",     nullptr,                           N_("_Previous Pane"),                                   "<alt>Left",           N_("Previous Split Pane"),                             CB(layout_menu_split_pane_prev_cb) },
2846   { "SplitUpPane",           nullptr,                           N_("_Up Pane"),                                         "<alt>Up",             N_("Up Split Pane"),                                   CB(layout_menu_split_pane_updown_cb) },
2847   { "StereoCycle",           nullptr,                           N_("_Cycle through stereo modes"),                      nullptr,               N_("Cycle through stereo modes"),                      CB(layout_menu_stereo_mode_next_cb) },
2848   { "StereoMenu",            nullptr,                           N_("Stere_o"),                                          nullptr,               nullptr,                                               nullptr },
2849   { "Up",                    GQ_ICON_GO_UP,                     N_("_Up"),                                              nullptr,               N_("Up one folder"),                                   CB(layout_menu_up_cb) },
2850   { "ViewInNewWindow",       nullptr,                           N_("_View in new window"),                              "<control>V",          N_("View in new window"),                              CB(layout_menu_view_in_new_window_cb) },
2851   { "ViewMenu",              nullptr,                           N_("_View"),                                            nullptr,               nullptr,                                               CB(layout_menu_view_menu_cb)  },
2852   { "Wallpaper",             nullptr,                           N_("Set as _wallpaper"),                                nullptr,               N_("Set as wallpaper"),                                CB(layout_menu_wallpaper_cb) },
2853   { "WindowsMenu",           nullptr,                           N_("_Windows"),                                         nullptr,               nullptr,                                               CB(layout_menu_windows_menu_cb)  },
2854   { "WriteRotationKeepDate", nullptr,                           N_("_Write orientation to file (preserve timestamp)"),  nullptr,               N_("Write orientation to file (preserve timestamp)"),  CB(layout_menu_write_rotate_keep_date_cb) },
2855   { "WriteRotation",         nullptr,                           N_("_Write orientation to file"),                       nullptr,               N_("Write orientation to file"),                       CB(layout_menu_write_rotate_cb) },
2856   { "Zoom100Alt1",           GQ_ICON_ZOOM_100,                  N_("Zoom _1:1"),                                        "KP_Divide",           N_("Zoom 1:1"),                                        CB(layout_menu_zoom_1_1_cb) },
2857   { "Zoom100",               GQ_ICON_ZOOM_100,                  N_("Zoom _1:1"),                                        "Z",                   N_("Zoom 1:1"),                                        CB(layout_menu_zoom_1_1_cb) },
2858   { "Zoom200",               GQ_ICON_GENERIC,                   N_("Zoom _2:1"),                                        nullptr,               N_("Zoom 2:1"),                                        CB(layout_menu_zoom_2_1_cb) },
2859   { "Zoom25",                GQ_ICON_GENERIC,                   N_("Zoom 1:4"),                                         nullptr,               N_("Zoom 1:4"),                                        CB(layout_menu_zoom_1_4_cb) },
2860   { "Zoom300",               GQ_ICON_GENERIC,                   N_("Zoom _3:1"),                                        nullptr,               N_("Zoom 3:1"),                                        CB(layout_menu_zoom_3_1_cb) },
2861   { "Zoom33",                GQ_ICON_GENERIC,                   N_("Zoom 1:3"),                                         nullptr,               N_("Zoom 1:3"),                                        CB(layout_menu_zoom_1_3_cb) },
2862   { "Zoom400",               GQ_ICON_GENERIC,                   N_("Zoom _4:1"),                                        nullptr,               N_("Zoom 4:1"),                                        CB(layout_menu_zoom_4_1_cb) },
2863   { "Zoom50",                GQ_ICON_GENERIC,                   N_("Zoom 1:2"),                                         nullptr,               N_("Zoom 1:2"),                                        CB(layout_menu_zoom_1_2_cb) },
2864   { "ZoomFillHor",           PIXBUF_INLINE_ICON_ZOOMFILLHOR,    N_("Fit _Horizontally"),                                "H",                   N_("Fit Horizontally"),                                CB(layout_menu_zoom_fit_hor_cb) },
2865   { "ZoomFillVert",          PIXBUF_INLINE_ICON_ZOOMFILLVERT,   N_("Fit _Vertically"),                                  "W",                   N_("Fit Vertically"),                                  CB(layout_menu_zoom_fit_vert_cb) },
2866   { "ZoomFitAlt1",           GQ_ICON_ZOOM_FIT,                  N_("_Zoom to fit"),                                     "KP_Multiply",         N_("Zoom to fit"),                                     CB(layout_menu_zoom_fit_cb) },
2867   { "ZoomFit",               GQ_ICON_ZOOM_FIT,                  N_("_Zoom to fit"),                                     "X",                   N_("Zoom to fit"),                                     CB(layout_menu_zoom_fit_cb) },
2868   { "ZoomInAlt1",            GQ_ICON_ZOOM_IN,                   N_("Zoom _in"),                                         "KP_Add",              N_("Zoom in"),                                         CB(layout_menu_zoom_in_cb) },
2869   { "ZoomIn",                GQ_ICON_ZOOM_IN,                   N_("Zoom _in"),                                         "equal",               N_("Zoom in"),                                         CB(layout_menu_zoom_in_cb) },
2870   { "ZoomMenu",              nullptr,                           N_("_Zoom"),                                            nullptr,               nullptr,                                               nullptr },
2871   { "ZoomOutAlt1",           GQ_ICON_ZOOM_OUT,                  N_("Zoom _out"),                                        "KP_Subtract",         N_("Zoom out"),                                        CB(layout_menu_zoom_out_cb) },
2872   { "ZoomToRectangle",       nullptr,                           N_("Zoom to rectangle"),                                nullptr,               N_("Zoom to rectangle"),                               CB(layout_menu_zoom_to_rectangle_cb) },
2873   { "ZoomOut",               GQ_ICON_ZOOM_OUT,                  N_("Zoom _out"),                                        "minus",               N_("Zoom out"),                                        CB(layout_menu_zoom_out_cb) }
2874 };
2875
2876 static GtkToggleActionEntry menu_toggle_entries[] = {
2877   { "Animate",                 nullptr,                              N_("_Animation"),               "A",               N_("Toggle animation"),              CB(layout_menu_animate_cb),                  FALSE  },
2878   { "DrawRectangle",           PIXBUF_INLINE_ICON_DRAW_RECTANGLE,    N_("Draw Rectangle"),           nullptr,           N_("Draw Rectangle"),                CB(layout_menu_select_rectangle_cb),         FALSE  },
2879   { "ExifRotate",              GQ_ICON_ROTATE_LEFT,                  N_("_Exif rotate"),             "<alt>X",          N_("Toggle Exif rotate"),            CB(layout_menu_exif_rotate_cb),              FALSE  },
2880   { "FloatTools",              PIXBUF_INLINE_ICON_FLOAT,             N_("_Float file list"),         "L",               N_("Float file list"),               CB(layout_menu_float_cb),                    FALSE  },
2881   { "Grayscale",               PIXBUF_INLINE_ICON_GRAYSCALE,         N_("Toggle _grayscale"),        "<shift>G",        N_("Toggle grayscale"),              CB(layout_menu_alter_desaturate_cb),         FALSE  },
2882   { "HideBars",                nullptr,                              N_("Hide Bars and Files"),      "grave",           N_("Hide Bars and Files"),           CB(layout_menu_hide_bars_cb),                FALSE  },
2883   { "HideSelectableToolbars",  nullptr,                              N_("Hide Selectable Bars"),     "<control>grave",  N_("Hide Selectable Bars"),          CB(layout_menu_selectable_toolbars_cb),      FALSE  },
2884   { "IgnoreAlpha",             GQ_ICON_STRIKETHROUGH,                N_("Hide _alpha"),              "<shift>A",        N_("Hide alpha channel"),            CB(layout_menu_alter_ignore_alpha_cb),       FALSE  },
2885   { "ImageHistogram",          nullptr,                              N_("_Show Histogram"),          nullptr,           N_("Show Histogram"),                CB(layout_menu_histogram_cb),                FALSE  },
2886   { "ImageOverlay",            nullptr,                              N_("Image _Overlay"),           nullptr,           N_("Image Overlay"),                 CB(layout_menu_overlay_cb),                  FALSE  },
2887   { "OverUnderExposed",        PIXBUF_INLINE_ICON_EXPOSURE,          N_("Over/Under Exposed"),       "<shift>E",        N_("Highlight over/under exposed"),  CB(layout_menu_select_overunderexposed_cb),  FALSE  },
2888   { "RectangularSelection",    PIXBUF_INLINE_ICON_SELECT_RECTANGLE,  N_("Rectangular Selection"),    "<alt>R",          N_("Rectangular Selection"),         CB(layout_menu_rectangular_selection_cb),    FALSE  },
2889   { "SBar",                    PIXBUF_INLINE_ICON_PROPERTIES,        N_("_Info sidebar"),            "<control>K",      N_("Info sidebar"),                  CB(layout_menu_bar_cb),                      FALSE  },
2890   { "SBarSort",                PIXBUF_INLINE_ICON_SORT,              N_("Sort _manager"),            "<shift>S",        N_("Sort manager"),                  CB(layout_menu_bar_sort_cb),                 FALSE  },
2891   { "ShowFileFilter",          GQ_ICON_FILE_FILTER,                  N_("Show File Filter"),         nullptr,           N_("Show File Filter"),              CB(layout_menu_file_filter_cb),              FALSE  },
2892   { "ShowInfoPixel",           GQ_ICON_SELECT_COLOR,                 N_("Pi_xel Info"),              nullptr,           N_("Show Pixel Info"),               CB(layout_menu_info_pixel_cb),               FALSE  },
2893   { "ShowMarks",               PIXBUF_INLINE_ICON_MARKS,             N_("Show _Marks"),              "M",               N_("Show Marks"),                    CB(layout_menu_marks_cb),                    FALSE  },
2894   { "SlideShow",               GQ_ICON_PLAY,                         N_("Toggle _slideshow"),        "S",               N_("Toggle slideshow"),              CB(layout_menu_slideshow_cb),                FALSE  },
2895   { "SplitPaneSync",           PIXBUF_INLINE_SPLIT_PANE_SYNC,        N_("Split Pane Sync"),          nullptr,           N_("Split Pane Sync"),               CB(layout_menu_split_pane_sync_cb),          FALSE  },
2896   { "Thumbnails",              PIXBUF_INLINE_ICON_THUMB,             N_("Show _Thumbnails"),         "T",               N_("Show Thumbnails"),               CB(layout_menu_thumb_cb),                    FALSE  },
2897   { "UseColorProfiles",        GQ_ICON_COLOR_MANAGEMENT,             N_("Use _color profiles"),      nullptr,           N_("Use color profiles"),            CB(layout_color_menu_enable_cb),             FALSE  },
2898   { "UseImageProfile",         nullptr,                              N_("Use profile from _image"),  nullptr,           N_("Use profile from image"),        CB(layout_color_menu_use_image_cb),          FALSE  }
2899 };
2900
2901 static GtkRadioActionEntry menu_radio_entries[] = {
2902   { "ViewIcons",  nullptr,  N_("Images as I_cons"),  "<control>I",  N_("View Images as Icons"),  FILEVIEW_ICON },
2903   { "ViewList",   nullptr,  N_("Images as _List"),   "<control>L",  N_("View Images as List"),   FILEVIEW_LIST }
2904 };
2905
2906 static GtkToggleActionEntry menu_view_dir_toggle_entries[] = {
2907   { "FolderTree",  nullptr,  N_("T_oggle Folder View"),  "<control>T",  N_("Toggle Folders View"),  CB(layout_menu_view_dir_as_cb),FALSE },
2908 };
2909
2910 static GtkRadioActionEntry menu_split_radio_entries[] = {
2911   { "SplitHorizontal",  nullptr,  N_("_Horizontal"),  "E",      N_("Split panes horizontal."),  SPLIT_HOR },
2912   { "SplitQuad",        nullptr,  N_("_Quad"),        nullptr,  N_("Split panes quad"),         SPLIT_QUAD },
2913   { "SplitSingle",      nullptr,  N_("_Single"),      "Y",      N_("Single pane"),              SPLIT_NONE },
2914   { "SplitTriple",      nullptr,  N_("_Triple"),      nullptr,  N_("Split panes triple"),       SPLIT_TRIPLE },
2915   { "SplitVertical",    nullptr,  N_("_Vertical"),    "U",      N_("Split panes vertical"),     SPLIT_VERT }
2916 };
2917
2918 static GtkRadioActionEntry menu_color_radio_entries[] = {
2919   { "ColorProfile0",  nullptr,  N_("Input _0: sRGB"),                 nullptr,  N_("Input 0: sRGB"),                 COLOR_PROFILE_SRGB },
2920   { "ColorProfile1",  nullptr,  N_("Input _1: AdobeRGB compatible"),  nullptr,  N_("Input 1: AdobeRGB compatible"),  COLOR_PROFILE_ADOBERGB },
2921   { "ColorProfile2",  nullptr,  N_("Input _2"),                       nullptr,  N_("Input 2"),                       COLOR_PROFILE_FILE },
2922   { "ColorProfile3",  nullptr,  N_("Input _3"),                       nullptr,  N_("Input 3"),                       COLOR_PROFILE_FILE + 1 },
2923   { "ColorProfile4",  nullptr,  N_("Input _4"),                       nullptr,  N_("Input 4"),                       COLOR_PROFILE_FILE + 2 },
2924   { "ColorProfile5",  nullptr,  N_("Input _5"),                       nullptr,  N_("Input 5"),                       COLOR_PROFILE_FILE + 3 }
2925 };
2926
2927 static GtkRadioActionEntry menu_histogram_channel[] = {
2928   { "HistogramChanB",    nullptr,  N_("Histogram on _Blue"),   nullptr,  N_("Histogram on Blue"),   HCHAN_B },
2929   { "HistogramChanG",    nullptr,  N_("Histogram on _Green"),  nullptr,  N_("Histogram on Green"),  HCHAN_G },
2930   { "HistogramChanRGB",  nullptr,  N_("_Histogram on RGB"),    nullptr,  N_("Histogram on RGB"),    HCHAN_RGB },
2931   { "HistogramChanR",    nullptr,  N_("Histogram on _Red"),    nullptr,  N_("Histogram on Red"),    HCHAN_R },
2932   { "HistogramChanV",    nullptr,  N_("Histogram on _Value"),  nullptr,  N_("Histogram on Value"),  HCHAN_MAX }
2933 };
2934
2935 static GtkRadioActionEntry menu_histogram_mode[] = {
2936   { "HistogramModeLin",  nullptr,  N_("Li_near Histogram"),  nullptr,  N_("Linear Histogram"),  0 },
2937   { "HistogramModeLog",  nullptr,  N_("_Log Histogram"),     nullptr,  N_("Log Histogram"),     1 },
2938 };
2939
2940 static GtkRadioActionEntry menu_stereo_mode_entries[] = {
2941   { "StereoAuto",   nullptr,  N_("_Auto"),          nullptr,  N_("Stereo Auto"),          STEREO_PIXBUF_DEFAULT },
2942   { "StereoCross",  nullptr,  N_("_Cross"),         nullptr,  N_("Stereo Cross"),         STEREO_PIXBUF_CROSS },
2943   { "StereoOff",    nullptr,  N_("_Off"),           nullptr,  N_("Stereo Off"),           STEREO_PIXBUF_NONE },
2944   { "StereoSBS",    nullptr,  N_("_Side by Side"),  nullptr,  N_("Stereo Side by Side"),  STEREO_PIXBUF_SBS }
2945 };
2946 #undef CB
2947
2948 static gchar *menu_translate(const gchar *path, gpointer)
2949 {
2950         return static_cast<gchar *>(_(path));
2951 }
2952
2953 static void layout_actions_setup_mark(LayoutWindow *lw, gint mark, const gchar *name_tmpl,
2954                                       const gchar *label_tmpl, const gchar *accel_tmpl, const gchar *tooltip_tmpl, GCallback cb)
2955 {
2956         gchar name[50];
2957         gchar label[100];
2958         gchar accel[50];
2959         gchar tooltip[100];
2960         GtkActionEntry entry = { name, nullptr, label, accel, tooltip, cb };
2961         GtkAction *action;
2962
2963         g_snprintf(name, sizeof(name), name_tmpl, mark);
2964         g_snprintf(label, sizeof(label), label_tmpl, mark);
2965
2966         if (accel_tmpl)
2967                 g_snprintf(accel, sizeof(accel), accel_tmpl, mark % 10);
2968         else
2969                 entry.accelerator = nullptr;
2970
2971         if (tooltip_tmpl)
2972                 g_snprintf(tooltip, sizeof(tooltip), tooltip_tmpl, mark);
2973         else
2974                 entry.tooltip = nullptr;
2975
2976         gtk_action_group_add_actions(lw->action_group, &entry, 1, lw);
2977         action = gtk_action_group_get_action(lw->action_group, name);
2978         g_object_set_data(G_OBJECT(action), "mark_num", GINT_TO_POINTER(mark > 0 ? mark : 10));
2979 }
2980
2981 static void layout_actions_setup_marks(LayoutWindow *lw)
2982 {
2983         gint mark;
2984         GError *error;
2985         GString *desc = g_string_new(
2986                                 "<ui>"
2987                                 "  <menubar name='MainMenu'>");
2988
2989         if (options->hamburger_menu)
2990                 {
2991                 g_string_append(desc, "    <menu action='OpenMenu'>");
2992                 }
2993         g_string_append(desc, "      <menu action='SelectMenu'>");
2994
2995         for (mark = 1; mark <= FILEDATA_MARKS_SIZE; mark++)
2996                 {
2997                 gint i = (mark < 10 ? mark : 0);
2998
2999                 layout_actions_setup_mark(lw, i, "Mark%d",              _("Mark _%d"), nullptr, nullptr, nullptr);
3000                 layout_actions_setup_mark(lw, i, "SetMark%d",   _("_Set mark %d"),                      nullptr,                _("Set mark %d"), G_CALLBACK(layout_menu_set_mark_sel_cb));
3001                 layout_actions_setup_mark(lw, i, "ResetMark%d", _("_Reset mark %d"),                    nullptr,                _("Reset mark %d"), G_CALLBACK(layout_menu_res_mark_sel_cb));
3002                 layout_actions_setup_mark(lw, i, "ToggleMark%d",        _("_Toggle mark %d"),                   "%d",           _("Toggle mark %d"), G_CALLBACK(layout_menu_toggle_mark_sel_cb));
3003                 layout_actions_setup_mark(lw, i, "ToggleMark%dAlt1",    _("_Toggle mark %d"),                   "KP_%d",        _("Toggle mark %d"), G_CALLBACK(layout_menu_toggle_mark_sel_cb));
3004                 layout_actions_setup_mark(lw, i, "SelectMark%d",        _("Se_lect mark %d"),                   "<control>%d",  _("Select mark %d"), G_CALLBACK(layout_menu_sel_mark_cb));
3005                 layout_actions_setup_mark(lw, i, "SelectMark%dAlt1",    _("_Select mark %d"),                   "<control>KP_%d", _("Select mark %d"), G_CALLBACK(layout_menu_sel_mark_cb));
3006                 layout_actions_setup_mark(lw, i, "AddMark%d",   _("_Add mark %d"),                      nullptr,                _("Add mark %d"), G_CALLBACK(layout_menu_sel_mark_or_cb));
3007                 layout_actions_setup_mark(lw, i, "IntMark%d",   _("_Intersection with mark %d"),        nullptr,                _("Intersection with mark %d"), G_CALLBACK(layout_menu_sel_mark_and_cb));
3008                 layout_actions_setup_mark(lw, i, "UnselMark%d", _("_Unselect mark %d"),                 nullptr,                _("Unselect mark %d"), G_CALLBACK(layout_menu_sel_mark_minus_cb));
3009                 layout_actions_setup_mark(lw, i, "FilterMark%d",        _("_Filter mark %d"),                   nullptr,                _("Filter mark %d"), G_CALLBACK(layout_menu_mark_filter_toggle_cb));
3010
3011                 g_string_append_printf(desc,
3012                                 "      <menu action='Mark%d'>"
3013                                 "        <menuitem action='ToggleMark%d'/>"
3014                                 "        <menuitem action='SetMark%d'/>"
3015                                 "        <menuitem action='ResetMark%d'/>"
3016                                 "        <separator/>"
3017                                 "        <menuitem action='SelectMark%d'/>"
3018                                 "        <menuitem action='AddMark%d'/>"
3019                                 "        <menuitem action='IntMark%d'/>"
3020                                 "        <menuitem action='UnselMark%d'/>"
3021                                 "        <separator/>"
3022                                 "        <menuitem action='FilterMark%d'/>"
3023                                 "      </menu>",
3024                                 i, i, i, i, i, i, i, i, i);
3025                 }
3026
3027         g_string_append(desc,
3028                                 "      </menu>");
3029         if (options->hamburger_menu)
3030                 {
3031                 g_string_append(desc, "    </menu>");
3032                 }
3033         g_string_append(desc, "  </menubar>");
3034
3035         for (mark = 1; mark <= FILEDATA_MARKS_SIZE; mark++)
3036                 {
3037                 gint i = (mark < 10 ? mark : 0);
3038
3039                 g_string_append_printf(desc,
3040                                 "<accelerator action='ToggleMark%dAlt1'/>"
3041                                 "<accelerator action='SelectMark%dAlt1'/>",
3042                                 i, i);
3043                 }
3044         g_string_append(desc,   "</ui>" );
3045
3046         error = nullptr;
3047         if (!gtk_ui_manager_add_ui_from_string(lw->ui_manager, desc->str, -1, &error))
3048                 {
3049                 g_message("building menus failed: %s", error->message);
3050                 g_error_free(error);
3051                 exit(EXIT_FAILURE);
3052                 }
3053         g_string_free(desc, TRUE);
3054 }
3055
3056 static GList *layout_actions_editor_menu_path(EditorDescription *editor)
3057 {
3058         gchar **split = g_strsplit(editor->menu_path, "/", 0);
3059         gint i = 0;
3060         GList *ret = nullptr;
3061
3062         if (split[0] == nullptr)
3063                 {
3064                 g_strfreev(split);
3065                 return nullptr;
3066                 }
3067
3068         while (split[i])
3069                 {
3070                 ret = g_list_prepend(ret, g_strdup(split[i]));
3071                 i++;
3072                 }
3073
3074         g_strfreev(split);
3075
3076         ret = g_list_prepend(ret, g_strdup(editor->key));
3077
3078         return g_list_reverse(ret);
3079 }
3080
3081 static void layout_actions_editor_add(GString *desc, GList *path, GList *old_path)
3082 {
3083         gint to_open;
3084         gint to_close;
3085         gint i;
3086         while (path && old_path && strcmp(static_cast<gchar *>(path->data), static_cast<gchar *>(old_path->data)) == 0)
3087                 {
3088                 path = path->next;
3089                 old_path = old_path->next;
3090                 }
3091         to_open = g_list_length(path) - 1;
3092         to_close = g_list_length(old_path) - 1;
3093
3094         if (to_close > 0)
3095                 {
3096                 old_path = g_list_last(old_path);
3097                 old_path = old_path->prev;
3098                 }
3099
3100         for (i =  0; i < to_close; i++)
3101                 {
3102                 auto name = static_cast<gchar *>(old_path->data);
3103                 if (g_str_has_suffix(name, "Section"))
3104                         {
3105                         g_string_append(desc,   "      </placeholder>");
3106                         }
3107                 else if (g_str_has_suffix(name, "Menu"))
3108                         {
3109                         g_string_append(desc,   "    </menu>");
3110                         }
3111                 else
3112                         {
3113                         g_warning("invalid menu path item %s", name);
3114                         }
3115                 old_path = old_path->prev;
3116                 }
3117
3118         for (i =  0; i < to_open; i++)
3119                 {
3120                 auto name = static_cast<gchar *>(path->data);
3121                 if (g_str_has_suffix(name, "Section"))
3122                         {
3123                         g_string_append_printf(desc,    "      <placeholder name='%s'>", name);
3124                         }
3125                 else if (g_str_has_suffix(name, "Menu"))
3126                         {
3127                         g_string_append_printf(desc,    "    <menu action='%s'>", name);
3128                         }
3129                 else
3130                         {
3131                         g_warning("invalid menu path item %s", name);
3132                         }
3133                 path = path->next;
3134                 }
3135
3136         if (path)
3137                 g_string_append_printf(desc, "      <menuitem action='%s'/>", static_cast<gchar *>(path->data));
3138 }
3139
3140 static void layout_actions_setup_editors(LayoutWindow *lw)
3141 {
3142         GError *error;
3143         GList *editors_list;
3144         GList *work;
3145         GList *old_path;
3146         GString *desc;
3147
3148         if (lw->ui_editors_id)
3149                 {
3150                 gtk_ui_manager_remove_ui(lw->ui_manager, lw->ui_editors_id);
3151                 }
3152
3153         if (lw->action_group_editors)
3154                 {
3155                 gtk_ui_manager_remove_action_group(lw->ui_manager, lw->action_group_editors);
3156                 g_object_unref(lw->action_group_editors);
3157                 }
3158         lw->action_group_editors = gtk_action_group_new("MenuActionsExternal");
3159         gtk_ui_manager_insert_action_group(lw->ui_manager, lw->action_group_editors, 1);
3160
3161         /* lw->action_group_editors contains translated entries, no translate func is required */
3162         desc = g_string_new(
3163                                 "<ui>"
3164                                 "  <menubar name='MainMenu'>");
3165
3166         if (options->hamburger_menu)
3167                 {
3168                 g_string_append(desc, "    <menu action='OpenMenu'>");
3169                 }
3170
3171         editors_list = editor_list_get();
3172
3173         old_path = nullptr;
3174         work = editors_list;
3175         while (work)
3176                 {
3177                 GList *path;
3178                 auto editor = static_cast<EditorDescription *>(work->data);
3179                 GtkActionEntry entry = { editor->key,
3180                                          nullptr,
3181                                          editor->name,
3182                                          editor->hotkey,
3183                                          editor->comment ? editor->comment : editor->name,
3184                                          G_CALLBACK(layout_menu_edit_cb) };
3185
3186                 if (editor->icon)
3187                         {
3188                         entry.stock_id = editor->key;
3189                         }
3190                 gtk_action_group_add_actions(lw->action_group_editors, &entry, 1, lw);
3191
3192                 path = layout_actions_editor_menu_path(editor);
3193                 layout_actions_editor_add(desc, path, old_path);
3194
3195                 g_list_free_full(old_path, g_free);
3196                 old_path = path;
3197                 work = work->next;
3198                 }
3199
3200         layout_actions_editor_add(desc, nullptr, old_path);
3201         g_list_free_full(old_path, g_free);
3202
3203         if (options->hamburger_menu)
3204                 {
3205                 g_string_append(desc, "</menu>");
3206                 }
3207
3208         g_string_append(desc,"  </menubar>"
3209                                 "</ui>" );
3210
3211         error = nullptr;
3212
3213         lw->ui_editors_id = gtk_ui_manager_add_ui_from_string(lw->ui_manager, desc->str, -1, &error);
3214         if (!lw->ui_editors_id)
3215                 {
3216                 g_message("building menus failed: %s", error->message);
3217                 g_error_free(error);
3218                 exit(EXIT_FAILURE);
3219                 }
3220         g_string_free(desc, TRUE);
3221         g_list_free(editors_list);
3222 }
3223
3224 void create_toolbars(LayoutWindow *lw)
3225 {
3226         gint i;
3227
3228         for (i = 0; i < TOOLBAR_COUNT; i++)
3229                 {
3230                 layout_actions_toolbar(lw, static_cast<ToolbarType>(i));
3231                 layout_toolbar_clear(lw, static_cast<ToolbarType>(i));
3232                 layout_toolbar_add_default(lw, static_cast<ToolbarType>(i));
3233                 }
3234 }
3235
3236 void layout_actions_setup(LayoutWindow *lw)
3237 {
3238         GError *error;
3239
3240         DEBUG_1("%s layout_actions_setup: start", get_exec_time());
3241         if (lw->ui_manager) return;
3242
3243         lw->action_group = gtk_action_group_new("MenuActions");
3244         gtk_action_group_set_translate_func(lw->action_group, menu_translate, nullptr, nullptr);
3245
3246         gtk_action_group_add_actions(lw->action_group,
3247                                      menu_entries, G_N_ELEMENTS(menu_entries), lw);
3248         gtk_action_group_add_toggle_actions(lw->action_group,
3249                                             menu_toggle_entries, G_N_ELEMENTS(menu_toggle_entries), lw);
3250         gtk_action_group_add_radio_actions(lw->action_group,
3251                                            menu_radio_entries, G_N_ELEMENTS(menu_radio_entries),
3252                                            0, G_CALLBACK(layout_menu_list_cb), lw);
3253         gtk_action_group_add_radio_actions(lw->action_group,
3254                                            menu_split_radio_entries, G_N_ELEMENTS(menu_split_radio_entries),
3255                                            0, G_CALLBACK(layout_menu_split_cb), lw);
3256         gtk_action_group_add_toggle_actions(lw->action_group,
3257                                            menu_view_dir_toggle_entries, G_N_ELEMENTS(menu_view_dir_toggle_entries),
3258                                             lw);
3259         gtk_action_group_add_radio_actions(lw->action_group,
3260                                            menu_color_radio_entries, COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS,
3261                                            0, G_CALLBACK(layout_color_menu_input_cb), lw);
3262         gtk_action_group_add_radio_actions(lw->action_group,
3263                                            menu_histogram_channel, G_N_ELEMENTS(menu_histogram_channel),
3264                                            0, G_CALLBACK(layout_menu_histogram_channel_cb), lw);
3265         gtk_action_group_add_radio_actions(lw->action_group,
3266                                            menu_histogram_mode, G_N_ELEMENTS(menu_histogram_mode),
3267                                            0, G_CALLBACK(layout_menu_histogram_mode_cb), lw);
3268         gtk_action_group_add_radio_actions(lw->action_group,
3269                                            menu_stereo_mode_entries, G_N_ELEMENTS(menu_stereo_mode_entries),
3270                                            0, G_CALLBACK(layout_menu_stereo_mode_cb), lw);
3271
3272
3273         lw->ui_manager = gtk_ui_manager_new();
3274         gtk_ui_manager_set_add_tearoffs(lw->ui_manager, TRUE);
3275         gtk_ui_manager_insert_action_group(lw->ui_manager, lw->action_group, 0);
3276
3277         DEBUG_1("%s layout_actions_setup: add menu", get_exec_time());
3278         error = nullptr;
3279
3280         if (!gtk_ui_manager_add_ui_from_resource(lw->ui_manager, options->hamburger_menu ? GQ_RESOURCE_PATH_UI "/menu-hamburger.ui" : GQ_RESOURCE_PATH_UI "/menu-classic.ui" , &error))
3281                 {
3282                 g_message("building menus failed: %s", error->message);
3283                 g_error_free(error);
3284                 exit(EXIT_FAILURE);
3285                 }
3286
3287         DEBUG_1("%s layout_actions_setup: marks", get_exec_time());
3288         layout_actions_setup_marks(lw);
3289
3290         DEBUG_1("%s layout_actions_setup: editors", get_exec_time());
3291         layout_actions_setup_editors(lw);
3292
3293         DEBUG_1("%s layout_actions_setup: status_update_write", get_exec_time());
3294         layout_util_status_update_write(lw);
3295
3296         DEBUG_1("%s layout_actions_setup: actions_add_window", get_exec_time());
3297         layout_actions_add_window(lw, lw->window);
3298         DEBUG_1("%s layout_actions_setup: end", get_exec_time());
3299 }
3300
3301 static gint layout_editors_reload_idle_id = -1;
3302 static GList *layout_editors_desktop_files = nullptr;
3303
3304 static gboolean layout_editors_reload_idle_cb(gpointer)
3305 {
3306         if (!layout_editors_desktop_files)
3307                 {
3308                 DEBUG_1("%s layout_editors_reload_idle_cb: get_desktop_files", get_exec_time());
3309                 layout_editors_desktop_files = editor_get_desktop_files();
3310                 return G_SOURCE_CONTINUE;
3311                 }
3312
3313         editor_read_desktop_file(static_cast<const gchar *>(layout_editors_desktop_files->data));
3314         g_free(layout_editors_desktop_files->data);
3315         layout_editors_desktop_files = g_list_delete_link(layout_editors_desktop_files, layout_editors_desktop_files);
3316
3317
3318         if (!layout_editors_desktop_files)
3319                 {
3320                 GList *work;
3321                 DEBUG_1("%s layout_editors_reload_idle_cb: setup_editors", get_exec_time());
3322                 editor_table_finish();
3323
3324                 work = layout_window_list;
3325                 while (work)
3326                         {
3327                         auto lw = static_cast<LayoutWindow *>(work->data);
3328                         work = work->next;
3329                         layout_actions_setup_editors(lw);
3330                         if (lw->bar_sort_enabled)
3331                                 {
3332                                 layout_bar_sort_toggle(lw);
3333                                 }
3334                         }
3335
3336                 DEBUG_1("%s layout_editors_reload_idle_cb: setup_editors done", get_exec_time());
3337
3338                 layout_editors_reload_idle_id = -1;
3339                 return G_SOURCE_REMOVE;
3340                 }
3341         return G_SOURCE_CONTINUE;
3342 }
3343
3344 void layout_editors_reload_start()
3345 {
3346         DEBUG_1("%s layout_editors_reload_start", get_exec_time());
3347
3348         if (layout_editors_reload_idle_id != -1)
3349                 {
3350                 g_source_remove(layout_editors_reload_idle_id);
3351                 g_list_free_full(layout_editors_desktop_files, g_free);
3352                 }
3353
3354         editor_table_clear();
3355         layout_editors_reload_idle_id = g_idle_add(layout_editors_reload_idle_cb, nullptr);
3356 }
3357
3358 void layout_editors_reload_finish()
3359 {
3360         if (layout_editors_reload_idle_id != -1)
3361                 {
3362                 DEBUG_1("%s layout_editors_reload_finish", get_exec_time());
3363                 g_source_remove(layout_editors_reload_idle_id);
3364                 while (layout_editors_reload_idle_id != -1)
3365                         {
3366                         layout_editors_reload_idle_cb(nullptr);
3367                         }
3368                 }
3369 }
3370
3371 void layout_actions_add_window(LayoutWindow *lw, GtkWidget *window)
3372 {
3373         GtkAccelGroup *group;
3374
3375         if (!lw->ui_manager) return;
3376
3377         group = gtk_ui_manager_get_accel_group(lw->ui_manager);
3378         gtk_window_add_accel_group(GTK_WINDOW(window), group);
3379 }
3380
3381 GtkWidget *layout_actions_menu_bar(LayoutWindow *lw)
3382 {
3383         if (lw->menu_bar) return lw->menu_bar;
3384         lw->menu_bar = gtk_ui_manager_get_widget(lw->ui_manager, "/MainMenu");
3385         g_object_ref(lw->menu_bar);
3386         return lw->menu_bar;
3387 }
3388
3389 GtkWidget *layout_actions_toolbar(LayoutWindow *lw, ToolbarType type)
3390 {
3391         if (lw->toolbar[type]) return lw->toolbar[type];
3392
3393         lw->toolbar[type] = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
3394
3395         gtk_widget_show(lw->toolbar[type]);
3396         g_object_ref(lw->toolbar[type]);
3397         return lw->toolbar[type];
3398 }
3399
3400 GtkWidget *layout_actions_menu_tool_bar(LayoutWindow *lw)
3401 {
3402         GtkWidget *menu_bar;
3403         GtkWidget *toolbar;
3404
3405         if (lw->menu_tool_bar) return lw->menu_tool_bar;
3406
3407         toolbar = layout_actions_toolbar(lw, TOOLBAR_MAIN);
3408         DEBUG_NAME(toolbar);
3409         lw->menu_tool_bar = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3410
3411         if (!options->hamburger_menu)
3412                 {
3413                 menu_bar = layout_actions_menu_bar(lw);
3414                 DEBUG_NAME(menu_bar);
3415                 gq_gtk_box_pack_start(GTK_BOX(lw->menu_tool_bar), menu_bar, FALSE, FALSE, 0);
3416                 }
3417
3418         gq_gtk_box_pack_start(GTK_BOX(lw->menu_tool_bar), toolbar, FALSE, FALSE, 0);
3419
3420         g_object_ref(lw->menu_tool_bar);
3421         return lw->menu_tool_bar;
3422 }
3423
3424 void toolbar_clear_cb(GtkWidget *widget, gpointer)
3425 {
3426         GtkAction *action;
3427
3428         if (GTK_IS_BUTTON(widget))
3429                 {
3430                 action = static_cast<GtkAction *>(g_object_get_data(G_OBJECT(widget), "action"));
3431                 if (g_object_get_data(G_OBJECT(widget), "id") )
3432                         {
3433                         g_signal_handler_disconnect(action, GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(widget), "id")));
3434                         }
3435                 }
3436         gtk_widget_destroy(widget);
3437 }
3438
3439 void layout_toolbar_clear(LayoutWindow *lw, ToolbarType type)
3440 {
3441         if (lw->toolbar_merge_id[type])
3442                 {
3443                 gtk_ui_manager_remove_ui(lw->ui_manager, lw->toolbar_merge_id[type]);
3444                 gtk_ui_manager_ensure_update(lw->ui_manager);
3445                 }
3446         g_list_free_full(lw->toolbar_actions[type], g_free);
3447         lw->toolbar_actions[type] = nullptr;
3448
3449         lw->toolbar_merge_id[type] = gtk_ui_manager_new_merge_id(lw->ui_manager);
3450
3451         if (lw->toolbar[type])
3452                 {
3453                 gtk_container_foreach(GTK_CONTAINER(lw->toolbar[type]), (GtkCallback)G_CALLBACK(toolbar_clear_cb), nullptr);
3454                 }
3455 }
3456
3457 void action_radio_changed_cb(GtkAction *action, GtkAction *current, gpointer data)
3458 {
3459         auto button = static_cast<GtkToggleButton *>(data);
3460
3461         if (action == current )
3462                 {
3463                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
3464                 }
3465         else
3466                 {
3467                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
3468                 }
3469 }
3470
3471 void action_toggle_activate_cb(GtkAction* self, gpointer data)
3472 {
3473         auto button = static_cast<GtkToggleButton *>(data);
3474
3475         if (gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(self)) != gtk_toggle_button_get_active(button))
3476                 {
3477                 gtk_toggle_button_set_active(button, gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(self)));
3478                 }
3479 }
3480
3481 gboolean toolbar_button_press_event_cb(GtkWidget *, GdkEvent *, gpointer data)
3482 {
3483         gtk_action_activate(GTK_ACTION(data));
3484
3485         return TRUE;
3486 }
3487
3488 void layout_toolbar_add(LayoutWindow *lw, ToolbarType type, const gchar *action_name)
3489 {
3490         const gchar *path = nullptr;
3491         const gchar *tooltip_text = nullptr;
3492         GtkAction *action;
3493         GtkWidget *action_icon = nullptr;
3494         GtkWidget *button;
3495         gulong id;
3496
3497         if (!action_name || !lw->ui_manager) return;
3498
3499         if (!lw->toolbar[type])
3500                 {
3501                 return;
3502                 }
3503
3504         switch (type)
3505                 {
3506                 case TOOLBAR_MAIN:
3507                         path = "/ToolBar";
3508                         break;
3509                 case TOOLBAR_STATUS:
3510                         path = "/StatusBar";
3511                         break;
3512                 default:
3513                         break;
3514                 }
3515
3516         if (g_str_has_suffix(action_name, ".desktop"))
3517                 {
3518                 /* this may be called before the external editors are read
3519                    create a dummy action for now */
3520                 if (!lw->action_group_editors)
3521                         {
3522                         lw->action_group_editors = gtk_action_group_new("MenuActionsExternal");
3523                         gtk_ui_manager_insert_action_group(lw->ui_manager, lw->action_group_editors, 1);
3524                         }
3525                 if (!gtk_action_group_get_action(lw->action_group_editors, action_name))
3526                         {
3527                         GtkActionEntry entry = { action_name,
3528                                                  GQ_ICON_MISSING_IMAGE,
3529                                                  action_name,
3530                                                  nullptr,
3531                                                  nullptr,
3532                                                  nullptr
3533                                                };
3534                         DEBUG_1("Creating temporary action %s", action_name);
3535                         gtk_action_group_add_actions(lw->action_group_editors, &entry, 1, lw);
3536                         }
3537                 }
3538
3539         if (g_strcmp0(action_name, "Separator") == 0)
3540                 {
3541                 button = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
3542                 }
3543         else
3544                 {
3545                 action = gtk_action_group_get_action(lw->action_group, action_name);
3546
3547                 action_icon = gtk_action_create_icon(action, GTK_ICON_SIZE_SMALL_TOOLBAR);
3548                 tooltip_text = gtk_action_get_tooltip(action);
3549
3550                 gtk_ui_manager_add_ui(lw->ui_manager, lw->toolbar_merge_id[type], path, action_name, action_name, GTK_UI_MANAGER_TOOLITEM, FALSE);
3551
3552                 if (GTK_IS_RADIO_ACTION(action) || GTK_IS_TOGGLE_ACTION(action))
3553                         {
3554                         button = gtk_toggle_button_new();
3555                         }
3556                 else
3557                         {
3558                         button = gtk_button_new();
3559                         }
3560
3561                 if (GTK_IS_TOGGLE_ACTION(action) || GTK_IS_RADIO_ACTION(action))
3562                         {
3563                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)));
3564                         }
3565
3566                 if (action_icon)
3567                         {
3568                         gtk_button_set_image(GTK_BUTTON(button), action_icon);
3569                         }
3570                 else
3571                         {
3572                         gtk_button_set_label(GTK_BUTTON(button), action_name);
3573                         }
3574
3575                 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
3576                 gtk_widget_set_tooltip_text(button, tooltip_text);
3577
3578                 if (GTK_IS_RADIO_ACTION(action))
3579                         {
3580                         id = g_signal_connect(G_OBJECT(action), "changed", G_CALLBACK(action_radio_changed_cb), button);
3581                         g_object_set_data(G_OBJECT(button), "id", GUINT_TO_POINTER(id));
3582                         }
3583                 else if (GTK_IS_TOGGLE_ACTION(action))
3584                         {
3585                         id = g_signal_connect(G_OBJECT(action), "activate", G_CALLBACK(action_toggle_activate_cb), button);
3586                         g_object_set_data(G_OBJECT(button), "id", GUINT_TO_POINTER(id));
3587                         }
3588
3589                 g_signal_connect(G_OBJECT(button), "button_press_event", G_CALLBACK(toolbar_button_press_event_cb), action);
3590                 g_object_set_data(G_OBJECT(button), "action", action);
3591                 }
3592
3593         gq_gtk_container_add(GTK_WIDGET(lw->toolbar[type]), GTK_WIDGET(button));
3594         gtk_widget_show(GTK_WIDGET(button));
3595
3596         lw->toolbar_actions[type] = g_list_append(lw->toolbar_actions[type], g_strdup(action_name));
3597 }
3598
3599 void layout_toolbar_add_default(LayoutWindow *lw, ToolbarType type)
3600 {
3601         LayoutWindow *lw_first;
3602         GList *work_action;
3603
3604         switch (type)
3605                 {
3606                 case TOOLBAR_MAIN:
3607                         if (layout_window_list)
3608                                 {
3609                                 lw_first = static_cast<LayoutWindow *>(layout_window_list->data);
3610                                 if (lw_first->toolbar_actions[TOOLBAR_MAIN])
3611                                         {
3612                                         work_action = lw_first->toolbar_actions[type];
3613                                         while (work_action)
3614                                                 {
3615                                                 auto action = static_cast<gchar *>(work_action->data);
3616                                                 work_action = work_action->next;
3617                                                 layout_toolbar_add(lw, type, action);
3618                                                 }
3619                                         }
3620                                 else
3621                                         {
3622                                         layout_toolbar_add(lw, type, "Thumbnails");
3623                                         layout_toolbar_add(lw, type, "Back");
3624                                         layout_toolbar_add(lw, type, "Forward");
3625                                         layout_toolbar_add(lw, type, "Up");
3626                                         layout_toolbar_add(lw, type, "Home");
3627                                         layout_toolbar_add(lw, type, "Refresh");
3628                                         layout_toolbar_add(lw, type, "ZoomIn");
3629                                         layout_toolbar_add(lw, type, "ZoomOut");
3630                                         layout_toolbar_add(lw, type, "ZoomFit");
3631                                         layout_toolbar_add(lw, type, "Zoom100");
3632                                         layout_toolbar_add(lw, type, "Preferences");
3633                                         layout_toolbar_add(lw, type, "FloatTools");
3634                                         }
3635                                 }
3636                         else
3637                                 {
3638                                 layout_toolbar_add(lw, type, "Thumbnails");
3639                                 layout_toolbar_add(lw, type, "Back");
3640                                 layout_toolbar_add(lw, type, "Forward");
3641                                 layout_toolbar_add(lw, type, "Up");
3642                                 layout_toolbar_add(lw, type, "Home");
3643                                 layout_toolbar_add(lw, type, "Refresh");
3644                                 layout_toolbar_add(lw, type, "ZoomIn");
3645                                 layout_toolbar_add(lw, type, "ZoomOut");
3646                                 layout_toolbar_add(lw, type, "ZoomFit");
3647                                 layout_toolbar_add(lw, type, "Zoom100");
3648                                 layout_toolbar_add(lw, type, "Preferences");
3649                                 layout_toolbar_add(lw, type, "FloatTools");
3650                                 }
3651                         break;
3652                 case TOOLBAR_STATUS:
3653                         if (layout_window_list)
3654                                 {
3655                                 lw_first = static_cast<LayoutWindow *>(layout_window_list->data);
3656                                 if (lw_first->toolbar_actions[TOOLBAR_MAIN])
3657                                         {
3658                                         work_action = lw_first->toolbar_actions[type];
3659                                         while (work_action)
3660                                                 {
3661                                                 auto action = static_cast<gchar *>(work_action->data);
3662                                                 work_action = work_action->next;
3663                                                 layout_toolbar_add(lw, type, action);
3664                                                 }
3665                                         }
3666                                 else
3667                                         {
3668                                         layout_toolbar_add(lw, type, "ExifRotate");
3669                                         layout_toolbar_add(lw, type, "ShowInfoPixel");
3670                                         layout_toolbar_add(lw, type, "UseColorProfiles");
3671                                         layout_toolbar_add(lw, type, "SaveMetadata");
3672                                         }
3673                                 }
3674                         else
3675                                 {
3676                                 layout_toolbar_add(lw, type, "ExifRotate");
3677                                 layout_toolbar_add(lw, type, "ShowInfoPixel");
3678                                 layout_toolbar_add(lw, type, "UseColorProfiles");
3679                                 layout_toolbar_add(lw, type, "SaveMetadata");
3680                                 }
3681                         break;
3682                 default:
3683                         break;
3684                 }
3685 }
3686
3687
3688
3689 void layout_toolbar_write_config(LayoutWindow *lw, ToolbarType type, GString *outstr, gint indent)
3690 {
3691         const gchar *name = nullptr;
3692         GList *work = lw->toolbar_actions[type];
3693
3694         switch (type)
3695                 {
3696                 case TOOLBAR_MAIN:
3697                         name = "toolbar";
3698                         break;
3699                 case TOOLBAR_STATUS:
3700                         name = "statusbar";
3701                         break;
3702                 default:
3703                         break;
3704                 }
3705
3706         WRITE_NL(); WRITE_STRING("<%s>", name);
3707         indent++;
3708         WRITE_NL(); WRITE_STRING("<clear/>");
3709         while (work)
3710                 {
3711                 auto action = static_cast<gchar *>(work->data);
3712                 work = work->next;
3713                 WRITE_NL(); WRITE_STRING("<toolitem ");
3714                 write_char_option(outstr, indent + 1, "action", action);
3715                 WRITE_STRING("/>");
3716                 }
3717         indent--;
3718         WRITE_NL(); WRITE_STRING("</%s>", name);
3719 }
3720
3721 void layout_toolbar_add_from_config(LayoutWindow *lw, ToolbarType type, const char **attribute_names, const gchar **attribute_values)
3722 {
3723         gchar *action = nullptr;
3724
3725         while (*attribute_names)
3726                 {
3727                 const gchar *option = *attribute_names++;
3728                 const gchar *value = *attribute_values++;
3729
3730                 if (READ_CHAR_FULL("action", action)) continue;
3731
3732                 log_printf("unknown attribute %s = %s\n", option, value);
3733                 }
3734
3735         layout_toolbar_add(lw, type, action);
3736         g_free(action);
3737 }
3738
3739 /*
3740  *-----------------------------------------------------------------------------
3741  * misc
3742  *-----------------------------------------------------------------------------
3743  */
3744
3745 void layout_util_status_update_write(LayoutWindow *lw)
3746 {
3747         GtkAction *action;
3748         gint n = metadata_queue_length();
3749         action = gtk_action_group_get_action(lw->action_group, "SaveMetadata");
3750         gtk_action_set_sensitive(action, n > 0);
3751         if (n > 0)
3752                 {
3753                 gchar *buf = g_strdup_printf(_("Number of files with unsaved metadata: %d"), n);
3754                 g_object_set(G_OBJECT(action), "tooltip", buf, NULL);
3755                 g_free(buf);
3756                 }
3757         else
3758                 {
3759                 g_object_set(G_OBJECT(action), "tooltip", _("No unsaved metadata"), NULL);
3760                 }
3761 }
3762
3763 void layout_util_status_update_write_all()
3764 {
3765         GList *work;
3766
3767         work = layout_window_list;
3768         while (work)
3769                 {
3770                 auto lw = static_cast<LayoutWindow *>(work->data);
3771                 work = work->next;
3772
3773                 layout_util_status_update_write(lw);
3774                 }
3775 }
3776
3777 static gchar *layout_color_name_parse(const gchar *name)
3778 {
3779         if (!name || !*name) return g_strdup(_("Empty"));
3780         return g_strdelimit(g_strdup(name), "_", '-');
3781 }
3782
3783 void layout_util_sync_color(LayoutWindow *lw)
3784 {
3785         GtkAction *action;
3786         gint input = 0;
3787         gboolean use_color;
3788         gboolean use_image = FALSE;
3789         gint i;
3790         gchar action_name[15];
3791 #if HAVE_LCMS
3792         gchar *image_profile;
3793         gchar *screen_profile;
3794 #endif
3795
3796         if (!lw->action_group) return;
3797         if (!layout_image_color_profile_get(lw, &input, &use_image)) return;
3798
3799         use_color = layout_image_color_profile_get_use(lw);
3800
3801         action = gtk_action_group_get_action(lw->action_group, "UseColorProfiles");
3802 #if HAVE_LCMS
3803         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), use_color);
3804         if (layout_image_color_profile_get_status(lw, &image_profile, &screen_profile))
3805                 {
3806                 gchar *buf;
3807                 buf = g_strdup_printf(_("Image profile: %s\nScreen profile: %s"), image_profile, screen_profile);
3808                 g_object_set(G_OBJECT(action), "tooltip", buf, NULL);
3809                 g_free(image_profile);
3810                 g_free(screen_profile);
3811                 g_free(buf);
3812                 }
3813         else
3814                 {
3815                 g_object_set(G_OBJECT(action), "tooltip", _("Click to enable color management"), NULL);
3816                 }
3817 #else
3818         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), FALSE);
3819         gtk_action_set_sensitive(action, FALSE);
3820         g_object_set(G_OBJECT(action), "tooltip", _("Color profiles not supported"), NULL);
3821 #endif
3822
3823         action = gtk_action_group_get_action(lw->action_group, "UseImageProfile");
3824         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), use_image);
3825         gtk_action_set_sensitive(action, use_color);
3826
3827         for (i = 0; i < COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS; i++)
3828                 {
3829                 sprintf(action_name, "ColorProfile%d", i);
3830                 action = gtk_action_group_get_action(lw->action_group, action_name);
3831
3832                 if (i >= COLOR_PROFILE_FILE)
3833                         {
3834                         const gchar *name = options->color_profile.input_name[i - COLOR_PROFILE_FILE];
3835                         const gchar *file = options->color_profile.input_file[i - COLOR_PROFILE_FILE];
3836                         gchar *end;
3837                         gchar *buf;
3838
3839                         if (!name || !name[0]) name = filename_from_path(file);
3840
3841                         end = layout_color_name_parse(name);
3842                         buf = g_strdup_printf(_("Input _%d: %s"), i, end);
3843                         g_free(end);
3844
3845                         g_object_set(G_OBJECT(action), "label", buf, NULL);
3846                         g_free(buf);
3847
3848                         gtk_action_set_visible(action, file && file[0]);
3849                         }
3850
3851                 gtk_action_set_sensitive(action, !use_image);
3852                 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), (i == input));
3853                 }
3854
3855         action = gtk_action_group_get_action(lw->action_group, "Grayscale");
3856         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), layout_image_get_desaturate(lw));
3857 }
3858
3859 void layout_util_sync_file_filter(LayoutWindow *lw)
3860 {
3861         GtkAction *action;
3862
3863         if (!lw->action_group) return;
3864
3865         action = gtk_action_group_get_action(lw->action_group, "ShowFileFilter");
3866         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.show_file_filter);
3867 }
3868
3869 void layout_util_sync_marks(LayoutWindow *lw)
3870 {
3871         GtkAction *action;
3872
3873         if (!lw->action_group) return;
3874
3875         action = gtk_action_group_get_action(lw->action_group, "ShowMarks");
3876         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.show_marks);
3877 }
3878
3879 static void layout_util_sync_views(LayoutWindow *lw)
3880 {
3881         GtkAction *action;
3882         OsdShowFlags osd_flags = image_osd_get(lw->image);
3883
3884         if (!lw->action_group) return;
3885
3886         action = gtk_action_group_get_action(lw->action_group, "FolderTree");
3887         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.dir_view_type);
3888
3889         action = gtk_action_group_get_action(lw->action_group, "SplitSingle");
3890         gtk_radio_action_set_current_value(GTK_RADIO_ACTION(action), lw->split_mode);
3891
3892         action = gtk_action_group_get_action(lw->action_group, "SplitNextPane");
3893         gtk_action_set_sensitive(action, !(lw->split_mode == SPLIT_NONE));
3894         action = gtk_action_group_get_action(lw->action_group, "SplitPreviousPane");
3895         gtk_action_set_sensitive(action, !(lw->split_mode == SPLIT_NONE));
3896         action = gtk_action_group_get_action(lw->action_group, "SplitUpPane");
3897         gtk_action_set_sensitive(action, !(lw->split_mode == SPLIT_NONE));
3898         action = gtk_action_group_get_action(lw->action_group, "SplitDownPane");
3899         gtk_action_set_sensitive(action, !(lw->split_mode == SPLIT_NONE));
3900
3901         action = gtk_action_group_get_action(lw->action_group, "SplitPaneSync");
3902         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.split_pane_sync);
3903
3904         action = gtk_action_group_get_action(lw->action_group, "ViewIcons");
3905         gtk_radio_action_set_current_value(GTK_RADIO_ACTION(action), lw->options.file_view_type);
3906
3907         action = gtk_action_group_get_action(lw->action_group, "FloatTools");
3908         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.tools_float);
3909
3910         action = gtk_action_group_get_action(lw->action_group, "SBar");
3911         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), layout_bar_enabled(lw));
3912
3913         action = gtk_action_group_get_action(lw->action_group, "SBarSort");
3914         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), layout_bar_sort_enabled(lw));
3915
3916         action = gtk_action_group_get_action(lw->action_group, "HideSelectableToolbars");
3917         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.selectable_toolbars_hidden);
3918
3919         action = gtk_action_group_get_action(lw->action_group, "ShowInfoPixel");
3920         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.show_info_pixel);
3921
3922         action = gtk_action_group_get_action(lw->action_group, "SlideShow");
3923         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), layout_image_slideshow_active(lw));
3924
3925         action = gtk_action_group_get_action(lw->action_group, "IgnoreAlpha");
3926         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.ignore_alpha);
3927
3928         action = gtk_action_group_get_action(lw->action_group, "Animate");
3929         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.animate);
3930
3931         action = gtk_action_group_get_action(lw->action_group, "ImageOverlay");
3932         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), osd_flags != OSD_SHOW_NOTHING);
3933
3934         action = gtk_action_group_get_action(lw->action_group, "ImageHistogram");
3935         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), osd_flags & OSD_SHOW_HISTOGRAM);
3936
3937         action = gtk_action_group_get_action(lw->action_group, "ExifRotate");
3938         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), options->image.exif_rotate_enable);
3939
3940         action = gtk_action_group_get_action(lw->action_group, "OverUnderExposed");
3941         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), options->overunderexposed);
3942
3943         action = gtk_action_group_get_action(lw->action_group, "DrawRectangle");
3944         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), options->draw_rectangle);
3945
3946         action = gtk_action_group_get_action(lw->action_group, "RectangularSelection");
3947         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), options->collections.rectangular_selection);
3948
3949         action = gtk_action_group_get_action(lw->action_group, "ShowFileFilter");
3950         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.show_file_filter);
3951
3952         action = gtk_action_group_get_action(lw->action_group, "HideBars");
3953         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), (lw->options.bars_state.hidden));
3954
3955         if (osd_flags & OSD_SHOW_HISTOGRAM)
3956                 {
3957                 action = gtk_action_group_get_action(lw->action_group, "HistogramChanR");
3958                 gtk_radio_action_set_current_value(GTK_RADIO_ACTION(action), image_osd_histogram_get_channel(lw->image));
3959
3960                 action = gtk_action_group_get_action(lw->action_group, "HistogramModeLin");
3961                 gtk_radio_action_set_current_value(GTK_RADIO_ACTION(action), image_osd_histogram_get_mode(lw->image));
3962                 }
3963
3964         action = gtk_action_group_get_action(lw->action_group, "ConnectZoomMenu");
3965         gtk_action_set_sensitive(action, lw->split_mode != SPLIT_NONE);
3966
3967         // @todo `which` is deprecated, use command -v
3968         gboolean is_write_rotation = !runcmd("which exiftran >/dev/null 2>&1")
3969                                   && !runcmd("which mogrify >/dev/null 2>&1")
3970                                   && !options->metadata.write_orientation;
3971         action = gtk_action_group_get_action(lw->action_group, "WriteRotation");
3972         gtk_action_set_sensitive(action, is_write_rotation);
3973         action = gtk_action_group_get_action(lw->action_group, "WriteRotationKeepDate");
3974         gtk_action_set_sensitive(action, is_write_rotation);
3975
3976         action = gtk_action_group_get_action(lw->action_group, "StereoAuto");
3977         gtk_radio_action_set_current_value(GTK_RADIO_ACTION(action), layout_image_stereo_pixbuf_get(lw));
3978
3979         layout_util_sync_marks(lw);
3980         layout_util_sync_color(lw);
3981         layout_image_set_ignore_alpha(lw, lw->options.ignore_alpha);
3982 }
3983
3984 void layout_util_sync_thumb(LayoutWindow *lw)
3985 {
3986         GtkAction *action;
3987
3988         if (!lw->action_group) return;
3989
3990         action = gtk_action_group_get_action(lw->action_group, "Thumbnails");
3991         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.show_thumbnails);
3992         g_object_set(action, "sensitive", (lw->options.file_view_type == FILEVIEW_LIST), NULL);
3993 }
3994
3995 void layout_util_sync(LayoutWindow *lw)
3996 {
3997         layout_util_sync_views(lw);
3998         layout_util_sync_thumb(lw);
3999         layout_menu_collection_recent_update(lw);
4000         layout_menu_collection_open_update(lw);
4001 }
4002
4003 /**
4004  * @brief Checks if event key is mapped to Help
4005  * @param event
4006  * @returns
4007  *
4008  * Used to check if the user has re-mapped the Help key
4009  * in Preferences/Keyboard
4010  *
4011  * Note: help_key.accel_mods and event->state
4012  * differ in the higher bits
4013  */
4014 gboolean is_help_key(GdkEventKey *event)
4015 {
4016         GtkAccelKey help_key;
4017         gboolean ret = FALSE;
4018         guint mask = GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK;
4019
4020         if (gtk_accel_map_lookup_entry("<Actions>/MenuActions/HelpContents", &help_key))
4021                 {
4022                 if (help_key.accel_key == event->keyval &&
4023                                         (help_key.accel_mods & mask) == (event->state & mask))
4024                         {
4025                         ret = TRUE;
4026                         }
4027                 }
4028
4029         return ret;
4030 }
4031
4032 /*
4033  *-----------------------------------------------------------------------------
4034  * sidebars
4035  *-----------------------------------------------------------------------------
4036  */
4037
4038 static gboolean layout_bar_enabled(LayoutWindow *lw)
4039 {
4040         return lw->bar && gtk_widget_get_visible(lw->bar);
4041 }
4042
4043 static void layout_bar_destroyed(GtkWidget *, gpointer data)
4044 {
4045         auto lw = static_cast<LayoutWindow *>(data);
4046
4047         lw->bar = nullptr;
4048 /*
4049     do not call layout_util_sync_views(lw) here
4050     this is called either when whole layout is destroyed - no need for update
4051     or when the bar is replaced - sync is called by upper function at the end of whole operation
4052
4053 */
4054 }
4055
4056 static void layout_bar_set_default(LayoutWindow *lw)
4057 {
4058         GtkWidget *bar;
4059
4060         if (!lw->utility_box) return;
4061
4062         bar = bar_new(lw);
4063         DEBUG_NAME(bar);
4064
4065         layout_bar_set(lw, bar);
4066
4067         bar_populate_default(bar);
4068 }
4069
4070 static void layout_bar_close(LayoutWindow *lw)
4071 {
4072         if (lw->bar)
4073                 {
4074                 bar_close(lw->bar);
4075                 lw->bar = nullptr;
4076                 }
4077 }
4078
4079
4080 void layout_bar_set(LayoutWindow *lw, GtkWidget *bar)
4081 {
4082         if (!lw->utility_box) return;
4083
4084         layout_bar_close(lw); /* if any */
4085
4086         if (!bar) return;
4087         lw->bar = bar;
4088
4089         g_signal_connect(G_OBJECT(lw->bar), "destroy",
4090                          G_CALLBACK(layout_bar_destroyed), lw);
4091
4092         gtk_paned_pack2(GTK_PANED(lw->utility_paned), lw->bar, FALSE, TRUE);
4093
4094         bar_set_fd(lw->bar, layout_image_get_fd(lw));
4095 }
4096
4097
4098 void layout_bar_toggle(LayoutWindow *lw)
4099 {
4100         if (layout_bar_enabled(lw))
4101                 {
4102                 gtk_widget_hide(lw->bar);
4103                 }
4104         else
4105                 {
4106                 if (!lw->bar)
4107                         {
4108                         layout_bar_set_default(lw);
4109                         }
4110                 gtk_widget_show(lw->bar);
4111                 bar_set_fd(lw->bar, layout_image_get_fd(lw));
4112                 }
4113         layout_util_sync_views(lw);
4114 }
4115
4116 static void layout_bar_new_image(LayoutWindow *lw)
4117 {
4118         if (!layout_bar_enabled(lw)) return;
4119
4120         bar_set_fd(lw->bar, layout_image_get_fd(lw));
4121 }
4122
4123 static void layout_bar_new_selection(LayoutWindow *lw, gint count)
4124 {
4125         if (!layout_bar_enabled(lw)) return;
4126
4127         bar_notify_selection(lw->bar, count);
4128 }
4129
4130 static gboolean layout_bar_sort_enabled(LayoutWindow *lw)
4131 {
4132         return lw->bar_sort && gtk_widget_get_visible(lw->bar_sort);
4133 }
4134
4135
4136 static void layout_bar_sort_destroyed(GtkWidget *, gpointer data)
4137 {
4138         auto lw = static_cast<LayoutWindow *>(data);
4139
4140         lw->bar_sort = nullptr;
4141
4142 /*
4143     do not call layout_util_sync_views(lw) here
4144     this is called either when whole layout is destroyed - no need for update
4145     or when the bar is replaced - sync is called by upper function at the end of whole operation
4146
4147 */
4148 }
4149
4150 static void layout_bar_sort_set_default(LayoutWindow *lw)
4151 {
4152         GtkWidget *bar;
4153
4154         if (!lw->utility_box) return;
4155
4156         bar = bar_sort_new_default(lw);
4157
4158         layout_bar_sort_set(lw, bar);
4159 }
4160
4161 static void layout_bar_sort_close(LayoutWindow *lw)
4162 {
4163         if (lw->bar_sort)
4164                 {
4165                 bar_sort_close(lw->bar_sort);
4166                 lw->bar_sort = nullptr;
4167                 }
4168 }
4169
4170 void layout_bar_sort_set(LayoutWindow *lw, GtkWidget *bar)
4171 {
4172         if (!lw->utility_box) return;
4173
4174         layout_bar_sort_close(lw); /* if any */
4175
4176         if (!bar) return;
4177         lw->bar_sort = bar;
4178
4179         g_signal_connect(G_OBJECT(lw->bar_sort), "destroy",
4180                          G_CALLBACK(layout_bar_sort_destroyed), lw);
4181
4182         gq_gtk_box_pack_end(GTK_BOX(lw->utility_box), lw->bar_sort, FALSE, FALSE, 0);
4183 }
4184
4185 void layout_bar_sort_toggle(LayoutWindow *lw)
4186 {
4187         if (layout_bar_sort_enabled(lw))
4188                 {
4189                 gtk_widget_hide(lw->bar_sort);
4190                 }
4191         else
4192                 {
4193                 if (!lw->bar_sort)
4194                         {
4195                         layout_bar_sort_set_default(lw);
4196                         }
4197                 gtk_widget_show(lw->bar_sort);
4198                 }
4199         layout_util_sync_views(lw);
4200 }
4201
4202 static void layout_bars_hide_toggle(LayoutWindow *lw)
4203 {
4204         if (lw->options.bars_state.hidden)
4205                 {
4206                 lw->options.bars_state.hidden = FALSE;
4207                 if (lw->options.bars_state.sort)
4208                         {
4209                         if (lw->bar_sort)
4210                                 {
4211                                 gtk_widget_show(lw->bar_sort);
4212                                 }
4213                         else
4214                                 {
4215                                 layout_bar_sort_set_default(lw);
4216                                 }
4217                         }
4218                 if (lw->options.bars_state.info)
4219                         {
4220                         gtk_widget_show(lw->bar);
4221                         }
4222                 layout_tools_float_set(lw, lw->options.tools_float,
4223                                                                         lw->options.bars_state.tools_hidden);
4224                 }
4225         else
4226                 {
4227                 lw->options.bars_state.hidden = TRUE;
4228                 lw->options.bars_state.sort = layout_bar_sort_enabled(lw);
4229                 lw->options.bars_state.info = layout_bar_enabled(lw);
4230                 lw->options.bars_state.tools_float = lw->options.tools_float;
4231                 lw->options.bars_state.tools_hidden = lw->options.tools_hidden;
4232
4233                 if (lw->bar)
4234                         {
4235                         gtk_widget_hide(lw->bar);
4236                         }
4237
4238                 if (lw->bar_sort)
4239                         gtk_widget_hide(lw->bar_sort);
4240                 layout_tools_float_set(lw, lw->options.tools_float, TRUE);
4241                 }
4242
4243         layout_util_sync_views(lw);
4244 }
4245
4246 void layout_bars_new_image(LayoutWindow *lw)
4247 {
4248         layout_bar_new_image(lw);
4249
4250         if (lw->exif_window) advanced_exif_set_fd(lw->exif_window, layout_image_get_fd(lw));
4251
4252         /* this should be called here to handle the metadata edited in bars */
4253         if (options->metadata.confirm_on_image_change)
4254                 metadata_write_queue_confirm(FALSE, nullptr, nullptr);
4255 }
4256
4257 void layout_bars_new_selection(LayoutWindow *lw, gint count)
4258 {
4259         layout_bar_new_selection(lw, count);
4260 }
4261
4262 GtkWidget *layout_bars_prepare(LayoutWindow *lw, GtkWidget *image)
4263 {
4264         if (lw->utility_box) return lw->utility_box;
4265         lw->utility_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
4266         lw->utility_paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
4267         DEBUG_NAME(lw->utility_paned);
4268         gq_gtk_box_pack_start(GTK_BOX(lw->utility_box), lw->utility_paned, TRUE, TRUE, 0);
4269
4270         gtk_paned_pack1(GTK_PANED(lw->utility_paned), image, TRUE, FALSE);
4271         gtk_widget_show(lw->utility_paned);
4272
4273         gtk_widget_show(image);
4274
4275         g_object_ref(lw->utility_box);
4276         return lw->utility_box;
4277 }
4278
4279 void layout_bars_close(LayoutWindow *lw)
4280 {
4281         layout_bar_sort_close(lw);
4282         layout_bar_close(lw);
4283 }
4284
4285 static gboolean layout_exif_window_destroy(GtkWidget *, gpointer data)
4286 {
4287         auto lw = static_cast<LayoutWindow *>(data);
4288         lw->exif_window = nullptr;
4289
4290         return TRUE;
4291 }
4292
4293 void layout_exif_window_new(LayoutWindow *lw)
4294 {
4295         if (lw->exif_window) return;
4296
4297         lw->exif_window = advanced_exif_new(lw);
4298         if (!lw->exif_window) return;
4299         g_signal_connect(G_OBJECT(lw->exif_window), "destroy",
4300                          G_CALLBACK(layout_exif_window_destroy), lw);
4301         advanced_exif_set_fd(lw->exif_window, layout_image_get_fd(lw));
4302 }
4303
4304 static void layout_search_and_run_window_new(LayoutWindow *lw)
4305 {
4306         if (lw->sar_window)
4307                 {
4308                 gtk_window_present(GTK_WINDOW(lw->sar_window));
4309                 return;
4310                 }
4311
4312         lw->sar_window = search_and_run_new(lw);
4313 }
4314
4315 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */