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