<num> toggles mark num
[geeqie.git] / src / layout_util.c
1 /*
2  * GQview
3  * (C) 2004 John Ellis
4  *
5  * Author: John Ellis
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12
13 #include "gqview.h"
14 #include "layout_util.h"
15
16 #include "bar_info.h"
17 #include "bar_exif.h"
18 #include "bar_sort.h"
19 #include "cache_maint.h"
20 #include "collect.h"
21 #include "collect-dlg.h"
22 #include "dupe.h"
23 #include "editors.h"
24 #include "img-view.h"
25 #include "info.h"
26 #include "layout_image.h"
27 #include "pan-view.h"
28 #include "pixbuf_util.h"
29 #include "preferences.h"
30 #include "print.h"
31 #include "search.h"
32 #include "utilops.h"
33 #include "ui_bookmark.h"
34 #include "ui_fileops.h"
35 #include "ui_menu.h"
36 #include "ui_misc.h"
37 #include "ui_tabcomp.h"
38
39 #include <gdk/gdkkeysyms.h> /* for keyboard values */
40
41
42 #define MENU_EDIT_ACTION_OFFSET 16
43
44
45 /*
46  *-----------------------------------------------------------------------------
47  * keyboard handler
48  *-----------------------------------------------------------------------------
49  */
50
51 static guint tree_key_overrides[] = {
52         GDK_Page_Up,    GDK_KP_Page_Up,
53         GDK_Page_Down,  GDK_KP_Page_Down,
54         GDK_Home,       GDK_KP_Home,
55         GDK_End,        GDK_KP_End
56 };
57
58 static gint layout_key_match(guint keyval)
59 {
60         gint i;
61
62         for (i = 0; i < sizeof(tree_key_overrides) / sizeof(guint); i++)
63                 {
64                 if (keyval == tree_key_overrides[i]) return TRUE;
65                 }
66
67         return FALSE;
68 }
69
70 gint layout_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
71 {
72         LayoutWindow *lw = data;
73         gint stop_signal = FALSE;
74         gint x = 0;
75         gint y = 0;
76
77         if (lw->path_entry && GTK_WIDGET_HAS_FOCUS(lw->path_entry))
78                 {
79                 if (event->keyval == GDK_Escape && lw->path)
80                         {
81                         gtk_entry_set_text(GTK_ENTRY(lw->path_entry), lw->path);
82                         }
83
84                 /* the gtkaccelgroup of the window is stealing presses before they get to the entry (and more),
85                  * so when the some widgets have focus, give them priority (HACK)
86                  */
87                 if (gtk_widget_event(lw->path_entry, (GdkEvent *)event))
88                         {
89                         return TRUE;
90                         }
91                 }
92         if (lw->vdt && GTK_WIDGET_HAS_FOCUS(lw->vdt->treeview) &&
93             !layout_key_match(event->keyval) &&
94             gtk_widget_event(lw->vdt->treeview, (GdkEvent *)event))
95                 {
96                 return TRUE;
97                 }
98         if (lw->bar_info &&
99             bar_info_event(lw->bar_info, (GdkEvent *)event))
100                 {
101                 return TRUE;
102                 }
103
104 /*
105         if (event->type == GDK_KEY_PRESS && lw->full_screen &&
106             gtk_accel_groups_activate(G_OBJECT(lw->window), event->keyval, event->state))
107                 return TRUE;
108 */
109
110         if (lw->image &&
111             (GTK_WIDGET_HAS_FOCUS(lw->image->widget) || (lw->tools && widget == lw->window) || lw->full_screen) )
112                 {
113                 stop_signal = TRUE;
114                 switch (event->keyval)
115                         {
116                         case GDK_Left: case GDK_KP_Left:
117                                 x -= 1;
118                                 break;
119                         case GDK_Right: case GDK_KP_Right:
120                                 x += 1;
121                                 break;
122                         case GDK_Up: case GDK_KP_Up:
123                                 y -= 1;
124                                 break;
125                         case GDK_Down: case GDK_KP_Down:
126                                 y += 1;
127                                 break;
128                         default:
129                                 stop_signal = FALSE;
130                                 break;
131                         }
132
133                 if (!stop_signal &&
134                     !(event->state & GDK_CONTROL_MASK))
135                         {
136                         stop_signal = TRUE;
137                         switch (event->keyval)
138                                 {
139                                 case GDK_Menu:
140                                         layout_image_menu_popup(lw);
141                                         break;
142                                 default:
143                                         stop_signal = FALSE;
144                                         break;
145                                 }
146                         }
147                 }
148
149         if (x != 0 || y!= 0)
150                 {
151                 if (event->state & GDK_SHIFT_MASK)
152                         {
153                         x *= 3;
154                         y *= 3;
155                         }
156
157                 keyboard_scroll_calc(&x, &y, event);
158                 layout_image_scroll(lw, x, y);
159                 }
160
161         if (stop_signal) return stop_signal;
162
163         if (event->state & GDK_CONTROL_MASK)
164                 {
165                 stop_signal = FALSE;
166                 }
167         else
168                 {
169                 stop_signal = TRUE;
170                 switch (event->keyval)
171                         {
172                         case '+': case GDK_KP_Add:
173                                 layout_image_zoom_adjust(lw, get_zoom_increment());
174                                 break;
175                         case GDK_KP_Subtract:
176                                 layout_image_zoom_adjust(lw, -get_zoom_increment());
177                                 break;
178                         case GDK_KP_Multiply:
179                                 layout_image_zoom_set(lw, 0.0);
180                                 break;
181                         case GDK_KP_Divide:
182                                 layout_image_zoom_set(lw, 1.0);
183                                 break;
184                         case GDK_Page_Up: case GDK_KP_Page_Up:
185                                 layout_image_prev(lw);
186                                 break;
187                         case GDK_Page_Down: case GDK_KP_Page_Down:
188                                 layout_image_next(lw);
189                                 break;
190                         case GDK_Delete: case GDK_KP_Delete:
191                                 if (enable_delete_key)
192                                         {
193                                         file_util_delete(NULL, layout_selection_list(lw), widget);
194                                         }
195                                 break;
196                         case GDK_Escape:
197                                 /* FIXME:interrupting thumbs no longer allowed */
198 #if 0
199                                 interrupt_thumbs();
200 #endif
201                                 break;
202                         case 'P': case 'p':
203                                 if (!event->state & GDK_SHIFT_MASK)
204                                         {
205                                         layout_image_slideshow_pause_toggle(lw);
206                                         }
207                                 else
208                                         {
209                                         stop_signal = FALSE;
210                                         }
211                                 break;
212                         case 'V': case 'v':
213                         case GDK_F11:
214                                 layout_image_full_screen_toggle(lw);
215                                 break;
216                         default:
217                                 stop_signal = FALSE;
218                                 break;
219                         }
220                 }
221
222 #if 0
223         if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
224 #endif
225
226         return stop_signal;
227 }
228
229 void layout_keyboard_init(LayoutWindow *lw, GtkWidget *window)
230 {
231         g_signal_connect(G_OBJECT(window), "key_press_event",
232                          G_CALLBACK(layout_key_press_cb), lw);
233 }
234
235 /*
236  *-----------------------------------------------------------------------------
237  * menu callbacks
238  *-----------------------------------------------------------------------------
239  */
240  
241  
242 static GtkWidget *layout_window(LayoutWindow *lw)
243 {
244         return lw->full_screen ? lw->full_screen->window : lw->window;
245 }
246
247 static void layout_menu_new_window_cb(GtkAction *action, gpointer data)
248 {
249         LayoutWindow *lw = data;
250         LayoutWindow *nw;
251
252         if (lw->full_screen)
253                 layout_image_full_screen_stop(lw);
254
255         nw = layout_new(NULL, FALSE, FALSE);
256         layout_sort_set(nw, file_sort_method, file_sort_ascending);
257         layout_set_path(nw, layout_get_path(lw));
258 }
259
260 static void layout_menu_new_cb(GtkAction *action, gpointer data)
261 {
262         LayoutWindow *lw = data;
263         if (lw->full_screen)
264                 layout_image_full_screen_stop(lw);
265         collection_window_new(NULL);
266 }
267
268 static void layout_menu_open_cb(GtkAction *action, gpointer data)
269 {
270         LayoutWindow *lw = data;
271         if (lw->full_screen)
272                 layout_image_full_screen_stop(lw);
273         collection_dialog_load(NULL);
274 }
275
276 static void layout_menu_search_cb(GtkAction *action, gpointer data)
277 {
278         LayoutWindow *lw = data;
279         if (lw->full_screen)
280                 layout_image_full_screen_stop(lw);
281
282         search_new(lw->path, layout_image_get_path(lw));
283 }
284
285 static void layout_menu_dupes_cb(GtkAction *action, gpointer data)
286 {
287         LayoutWindow *lw = data;
288         if (lw->full_screen)
289                 layout_image_full_screen_stop(lw);
290
291         dupe_window_new(DUPE_MATCH_NAME);
292 }
293
294 static void layout_menu_pan_cb(GtkAction *action, gpointer data)
295 {
296         LayoutWindow *lw = data;
297         if (lw->full_screen)
298                 layout_image_full_screen_stop(lw);
299
300         pan_window_new(layout_get_path(lw));
301 }
302
303 static void layout_menu_print_cb(GtkAction *action, gpointer data)
304 {
305         LayoutWindow *lw = data;
306
307         print_window_new(layout_image_get_fd(lw), layout_selection_list(lw), layout_list(lw), layout_window(lw));
308 }
309
310 static void layout_menu_dir_cb(GtkAction *action, gpointer data)
311 {
312         LayoutWindow *lw = data;
313
314         file_util_create_dir(lw->path, layout_window(lw));
315 }
316
317 static void layout_menu_copy_cb(GtkAction *action, gpointer data)
318 {
319         LayoutWindow *lw = data;
320
321         file_util_copy(NULL, layout_selection_list(lw), NULL, layout_window(lw));
322 }
323
324 static void layout_menu_move_cb(GtkAction *action, gpointer data)
325 {
326         LayoutWindow *lw = data;
327
328         file_util_move(NULL, layout_selection_list(lw), NULL, layout_window(lw));
329 }
330
331 static void layout_menu_rename_cb(GtkAction *action, gpointer data)
332 {
333         LayoutWindow *lw = data;
334
335         file_util_rename(NULL, layout_selection_list(lw), layout_window(lw));
336 }
337
338 static void layout_menu_delete_cb(GtkAction *action, gpointer data)
339 {
340         LayoutWindow *lw = data;
341
342         file_util_delete(NULL, layout_selection_list(lw), layout_window(lw));
343 }
344
345 static void layout_menu_close_cb(GtkAction *action, gpointer data)
346 {
347         LayoutWindow *lw = data;
348         if (lw->full_screen)
349                 layout_image_full_screen_stop(lw);
350
351         layout_close(lw);
352 }
353
354 static void layout_menu_exit_cb(GtkAction *action, gpointer data)
355 {
356         exit_gqview();
357 }
358
359 static void layout_menu_alter_90_cb(GtkAction *action, gpointer data)
360 {
361         LayoutWindow *lw = data;
362
363         layout_image_alter(lw, ALTER_ROTATE_90);
364 }
365
366 static void layout_menu_alter_90cc_cb(GtkAction *action, gpointer data)
367 {
368         LayoutWindow *lw = data;
369
370         layout_image_alter(lw, ALTER_ROTATE_90_CC);
371 }
372
373 static void layout_menu_alter_180_cb(GtkAction *action, gpointer data)
374 {
375         LayoutWindow *lw = data;
376
377         layout_image_alter(lw, ALTER_ROTATE_180);
378 }
379
380 static void layout_menu_alter_mirror_cb(GtkAction *action, gpointer data)
381 {
382         LayoutWindow *lw = data;
383
384         layout_image_alter(lw, ALTER_MIRROR);
385 }
386
387 static void layout_menu_alter_flip_cb(GtkAction *action, gpointer data)
388 {
389         LayoutWindow *lw = data;
390
391         layout_image_alter(lw, ALTER_FLIP);
392 }
393
394 static void layout_menu_alter_desaturate_cb(GtkAction *action, gpointer data)
395 {
396         LayoutWindow *lw = data;
397
398         layout_image_alter(lw, ALTER_DESATURATE);
399 }
400
401 static void layout_menu_info_cb(GtkAction *action, gpointer data)
402 {
403         LayoutWindow *lw = data;
404         GList *list;
405         FileData *fd = NULL;
406
407         list = layout_selection_list(lw);
408         if (!list) fd = layout_image_get_fd(lw);
409
410         info_window_new(fd, list);
411 }
412
413
414 static void layout_menu_config_cb(GtkAction *action, gpointer data)
415 {
416         LayoutWindow *lw = data;
417         if (lw->full_screen)
418                 layout_image_full_screen_stop(lw);
419
420         show_config_window();
421 }
422
423 static void layout_menu_remove_thumb_cb(GtkAction *action, gpointer data)
424 {
425         LayoutWindow *lw = data;
426         if (lw->full_screen)
427                 layout_image_full_screen_stop(lw);
428
429         cache_manager_show();
430 }
431
432 static void layout_menu_wallpaper_cb(GtkAction *action, gpointer data)
433 {
434         LayoutWindow *lw = data;
435
436         layout_image_to_root(lw);
437 }
438
439 static void layout_menu_zoom_in_cb(GtkAction *action, gpointer data)
440 {
441         LayoutWindow *lw = data;
442
443         layout_image_zoom_adjust(lw, get_zoom_increment());
444 }
445
446 static void layout_menu_zoom_out_cb(GtkAction *action, gpointer data)
447 {
448         LayoutWindow *lw = data;
449
450         layout_image_zoom_adjust(lw, -get_zoom_increment());
451 }
452
453 static void layout_menu_zoom_1_1_cb(GtkAction *action, gpointer data)
454 {
455         LayoutWindow *lw = data;
456
457         layout_image_zoom_set(lw, 1.0);
458 }
459
460 static void layout_menu_zoom_fit_cb(GtkAction *action, gpointer data)
461 {
462         LayoutWindow *lw = data;
463
464         layout_image_zoom_set(lw, 0.0);
465 }
466
467 static void layout_menu_zoom_fit_hor_cb(GtkAction *action, gpointer data)
468 {
469         LayoutWindow *lw = data;
470
471         layout_image_zoom_set_fill_geometry(lw, TRUE);
472 }
473
474 static void layout_menu_zoom_fit_vert_cb(GtkAction *action, gpointer data)
475 {
476         LayoutWindow *lw = data;
477
478         layout_image_zoom_set_fill_geometry(lw, FALSE);
479 }
480
481 static void layout_menu_zoom_2_1_cb(GtkAction *action, gpointer data)
482 {
483         LayoutWindow *lw = data;
484
485         layout_image_zoom_set(lw, 2.0);
486 }
487
488 static void layout_menu_zoom_3_1_cb(GtkAction *action, gpointer data)
489 {
490         LayoutWindow *lw = data;
491
492         layout_image_zoom_set(lw, 3.0);
493 }
494 static void layout_menu_zoom_4_1_cb(GtkAction *action, gpointer data)
495 {
496         LayoutWindow *lw = data;
497
498         layout_image_zoom_set(lw, 4.0);
499 }
500
501 static void layout_menu_zoom_1_2_cb(GtkAction *action, gpointer data)
502 {
503         LayoutWindow *lw = data;
504
505         layout_image_zoom_set(lw, -2.0);
506 }
507
508 static void layout_menu_zoom_1_3_cb(GtkAction *action, gpointer data)
509 {
510         LayoutWindow *lw = data;
511
512         layout_image_zoom_set(lw, -3.0);
513 }
514
515 static void layout_menu_zoom_1_4_cb(GtkAction *action, gpointer data)
516 {
517         LayoutWindow *lw = data;
518
519         layout_image_zoom_set(lw, -4.0);
520 }
521
522
523 static void layout_menu_split_cb(GtkRadioAction *action, GtkRadioAction *current, gpointer data)
524 {
525         LayoutWindow *lw = data;
526         if (lw->full_screen)
527                 layout_image_full_screen_stop(lw);
528
529         ImageSplitMode mode = gtk_radio_action_get_current_value(action);
530         
531         if (mode == lw->split_mode) mode = 0; /* toggle back */
532
533         layout_split_change(lw, mode);
534 }
535
536 static void layout_menu_connect_scroll_cb(GtkToggleAction *action, gpointer data)
537 {
538         LayoutWindow *lw = data;
539         lw->connect_scroll = gtk_toggle_action_get_active(action);
540 }
541
542 static void layout_menu_connect_zoom_cb(GtkToggleAction *action, gpointer data)
543 {
544         LayoutWindow *lw = data;
545         lw->connect_zoom = gtk_toggle_action_get_active(action);
546 }
547
548
549 static void layout_menu_thumb_cb(GtkToggleAction *action, gpointer data)
550 {
551         LayoutWindow *lw = data;
552
553         layout_thumb_set(lw, gtk_toggle_action_get_active(action));
554 }
555
556
557 static void layout_menu_list_cb(GtkRadioAction *action, GtkRadioAction *current, gpointer data)
558 {
559         LayoutWindow *lw = data;
560         if (lw->full_screen)
561                 layout_image_full_screen_stop(lw);
562
563         layout_views_set(lw, lw->tree_view, (gtk_radio_action_get_current_value(action) == 1));
564 }
565
566 static void layout_menu_tree_cb(GtkToggleAction *action, gpointer data)
567 {
568         LayoutWindow *lw = data;
569         if (lw->full_screen)
570                 layout_image_full_screen_stop(lw);
571
572         layout_views_set(lw, gtk_toggle_action_get_active(action), lw->icon_view);
573 }
574
575 static void layout_menu_view_in_new_window_cb(GtkAction *action, gpointer data)
576 {
577         LayoutWindow *lw = data;
578
579         if (lw->full_screen)
580                 layout_image_full_screen_stop(lw);
581         view_window_new(layout_image_get_fd(lw));
582 }
583
584 static void layout_menu_fullscreen_cb(GtkAction *action, gpointer data)
585 {
586         LayoutWindow *lw = data;
587
588         layout_image_full_screen_toggle(lw);
589 }
590
591 static void layout_menu_overlay_cb(GtkAction *action, gpointer data)
592 {
593         LayoutWindow *lw = data;
594
595         layout_image_overlay_toggle(lw);
596 }
597
598 static void layout_menu_refresh_cb(GtkAction *action, gpointer data)
599 {
600         LayoutWindow *lw = data;
601
602         layout_refresh(lw);
603 }
604
605 static void layout_menu_float_cb(GtkToggleAction *action, gpointer data)
606 {
607         LayoutWindow *lw = data;
608         if (lw->full_screen)
609                 layout_image_full_screen_stop(lw);
610
611         if (lw->tools_float != gtk_toggle_action_get_active(action))
612                 {
613                 layout_tools_float_toggle(lw);
614                 }
615 }
616
617 static void layout_menu_hide_cb(GtkAction *action, gpointer data)
618 {
619         LayoutWindow *lw = data;
620         if (lw->full_screen)
621                 layout_image_full_screen_stop(lw);
622
623         layout_tools_hide_toggle(lw);
624 }
625
626 static void layout_menu_toolbar_cb(GtkToggleAction *action, gpointer data)
627 {
628         LayoutWindow *lw = data;
629         if (lw->full_screen)
630                 layout_image_full_screen_stop(lw);
631
632         if (lw->toolbar_hidden != gtk_toggle_action_get_active(action))
633                 {
634                 layout_toolbar_toggle(lw);
635                 }
636 }
637
638 static void layout_menu_bar_info_cb(GtkToggleAction *action, gpointer data)
639 {
640         LayoutWindow *lw = data;
641         if (lw->full_screen)
642                 layout_image_full_screen_stop(lw);
643
644         if (lw->bar_info_enabled != gtk_toggle_action_get_active(action))
645                 {
646                 layout_bar_info_toggle(lw);
647                 }
648 }
649
650 static void layout_menu_bar_exif_cb(GtkToggleAction *action, gpointer data)
651 {
652         LayoutWindow *lw = data;
653         if (lw->full_screen)
654                 layout_image_full_screen_stop(lw);
655
656         if (lw->bar_exif_enabled != gtk_toggle_action_get_active(action))
657                 {
658                 layout_bar_exif_toggle(lw);
659                 }
660 }
661
662 static void layout_menu_bar_sort_cb(GtkToggleAction *action, gpointer data)
663 {
664         LayoutWindow *lw = data;
665         if (lw->full_screen)
666                 layout_image_full_screen_stop(lw);
667
668         if (lw->bar_sort_enabled != gtk_toggle_action_get_active(action))
669                 {
670                 layout_bar_sort_toggle(lw);
671                 }
672 }
673
674 static void layout_menu_slideshow_cb(GtkAction *action, gpointer data)
675 {
676         LayoutWindow *lw = data;
677
678         layout_image_slideshow_toggle(lw);
679 }
680
681 static void layout_menu_help_cb(GtkAction *action, gpointer data)
682 {
683         LayoutWindow *lw = data;
684         if (lw->full_screen)
685                 layout_image_full_screen_stop(lw);
686         help_window_show("html_contents");
687 }
688
689 static void layout_menu_help_keys_cb(GtkAction *action, gpointer data)
690 {
691         LayoutWindow *lw = data;
692         if (lw->full_screen)
693                 layout_image_full_screen_stop(lw);
694         help_window_show("documentation");
695 }
696
697 static void layout_menu_notes_cb(GtkAction *action, gpointer data)
698 {
699         LayoutWindow *lw = data;
700         if (lw->full_screen)
701                 layout_image_full_screen_stop(lw);
702         help_window_show("release_notes");
703 }
704
705 static void layout_menu_about_cb(GtkAction *action, gpointer data)
706 {
707         LayoutWindow *lw = data;
708         if (lw->full_screen)
709                 layout_image_full_screen_stop(lw);
710         show_about_window();
711 }
712
713
714 /*
715  *-----------------------------------------------------------------------------
716  * select menu
717  *-----------------------------------------------------------------------------
718  */ 
719
720 static void layout_menu_select_all_cb(GtkAction *action, gpointer data)
721 {
722         LayoutWindow *lw = data;
723
724         layout_select_all(lw);
725 }
726
727 static void layout_menu_unselect_all_cb(GtkAction *action, gpointer data)
728 {
729         LayoutWindow *lw = data;
730
731         layout_select_none(lw);
732 }
733
734 static void layout_menu_marks_cb(GtkToggleAction *action, gpointer data)
735 {
736         LayoutWindow *lw = data;
737
738         layout_marks_set(lw, gtk_toggle_action_get_active(action));
739 }
740
741
742 static void layout_menu_set_mark_sel_cb(GtkAction *action, gpointer data)
743 {
744         LayoutWindow *lw = data;
745         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
746         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
747         mark--;
748         
749         layout_selection_to_mark(lw, mark, STM_MODE_SET);
750 }
751
752 static void layout_menu_res_mark_sel_cb(GtkAction *action, gpointer data)
753 {
754         LayoutWindow *lw = data;
755         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
756         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
757         mark--;
758         
759         layout_selection_to_mark(lw, mark, STM_MODE_RESET);
760 }
761
762 static void layout_menu_toggle_mark_sel_cb(GtkAction *action, gpointer data)
763 {
764         LayoutWindow *lw = data;
765         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
766         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
767         mark--;
768         
769         layout_selection_to_mark(lw, mark, STM_MODE_TOGGLE);
770 }
771
772 static void layout_menu_sel_mark_cb(GtkAction *action, gpointer data)
773 {
774         LayoutWindow *lw = data;
775         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
776         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
777         mark--;
778         
779         layout_mark_to_selection(lw, mark, MTS_MODE_SET);
780 }
781
782 static void layout_menu_sel_mark_or_cb(GtkAction *action, gpointer data)
783 {
784         LayoutWindow *lw = data;
785         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
786         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
787         mark--;
788         
789         layout_mark_to_selection(lw, mark, MTS_MODE_OR);
790 }
791
792 static void layout_menu_sel_mark_and_cb(GtkAction *action, gpointer data)
793 {
794         LayoutWindow *lw = data;
795         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
796         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
797         mark--;
798         
799         layout_mark_to_selection(lw, mark, MTS_MODE_AND);
800 }
801
802 static void layout_menu_sel_mark_minus_cb(GtkAction *action, gpointer data)
803 {
804         LayoutWindow *lw = data;
805         gint mark = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "mark_num"));
806         g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
807         mark--;
808         
809         layout_mark_to_selection(lw, mark, MTS_MODE_MINUS);
810 }
811
812
813 /*
814  *-----------------------------------------------------------------------------
815  * go menu
816  *-----------------------------------------------------------------------------
817  */ 
818
819 static void layout_menu_image_first_cb(GtkAction *action, gpointer data)
820 {
821         LayoutWindow *lw = data;
822         layout_image_first(lw);
823 }
824
825 static void layout_menu_image_prev_cb(GtkAction *action, gpointer data)
826 {
827         LayoutWindow *lw = data;
828         layout_image_prev(lw);
829 }
830
831 static void layout_menu_image_next_cb(GtkAction *action, gpointer data)
832 {
833         LayoutWindow *lw = data;
834         layout_image_next(lw);
835 }
836
837 static void layout_menu_image_last_cb(GtkAction *action, gpointer data)
838 {
839         LayoutWindow *lw = data;
840         layout_image_last(lw);
841 }
842
843
844 /*
845  *-----------------------------------------------------------------------------
846  * edit menu
847  *-----------------------------------------------------------------------------
848  */ 
849
850 static void layout_menu_edit_cb(GtkAction *action, gpointer data)
851 {
852         LayoutWindow *lw = data;
853         GList *list;
854         gint n;
855
856         n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "edit_index"));
857
858
859         if (lw->full_screen && !editor_window_flag_set(n))
860                 {
861                 layout_image_full_screen_stop(lw);
862                 }
863         list = layout_selection_list(lw);
864         start_editor_from_filelist(n, list);
865         filelist_free(list);
866 }
867
868 static void layout_menu_edit_update(LayoutWindow *lw)
869 {
870         gint i;
871
872         /* main edit menu */
873
874         if (!lw->action_group) return;
875
876         for (i = 0; i < 10; i++)
877                 {
878                 gchar *key;
879                 GtkAction *action;
880
881                 key = g_strdup_printf("Editor%d", i);
882
883                 action = gtk_action_group_get_action(lw->action_group, key);
884                 g_object_set_data(G_OBJECT(action), "edit_index", GINT_TO_POINTER(i));
885
886                 if (editor_command[i] && strlen(editor_command[i]) > 0)
887                         {
888                         gchar *text;
889
890                         if (editor_name[i] && strlen(editor_name[i]) > 0)
891                                 {
892                                 text = g_strdup_printf(_("in %s..."), editor_name[i]);
893                                 }
894                         else
895                                 {
896                                 text = g_strdup(_("in (unknown)..."));
897                                 }
898                         g_object_set(action, "label", text,
899                                              "sensitive", TRUE, NULL);
900                         g_free(text);
901                         }
902                 else
903                         {
904                         g_object_set(action, "label", _("empty"),
905                                              "sensitive", FALSE, NULL);
906                         }
907
908                 g_free(key);
909                 }
910 }
911
912 void layout_edit_update_all(void)
913 {
914         GList *work;
915
916         work = layout_window_list;
917         while (work)
918                 {
919                 LayoutWindow *lw = work->data;
920                 work = work->next;
921
922                 layout_menu_edit_update(lw);
923                 }
924 }
925
926 /*
927  *-----------------------------------------------------------------------------
928  * recent menu
929  *-----------------------------------------------------------------------------
930  */
931
932 static void layout_menu_recent_cb(GtkWidget *widget, gpointer data)
933 {
934         gint n;
935         gchar *path;
936
937         n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "recent_index"));
938
939         path = g_list_nth_data(history_list_get_by_key("recent"), n);
940
941         if (!path) return;
942
943         /* make a copy of it */
944         path = g_strdup(path);
945         collection_window_new(path);
946         g_free(path);
947 }
948
949 static void layout_menu_recent_update(LayoutWindow *lw)
950 {
951         GtkWidget *menu;
952         GtkWidget *recent;
953         GtkWidget *item;
954         GList *list;
955         gint n;
956
957         if (!lw->ui_manager) return;
958
959         list = history_list_get_by_key("recent");
960         n = 0;
961
962         menu = gtk_menu_new();
963
964         while (list)
965                 {
966                 item = menu_item_add_simple(menu, filename_from_path((gchar *)list->data),
967                                             G_CALLBACK(layout_menu_recent_cb), lw);
968                 g_object_set_data(G_OBJECT(item), "recent_index", GINT_TO_POINTER(n));
969                 list = list->next;
970                 n++;
971                 }
972
973         if (n == 0)
974                 {
975                 menu_item_add(menu, _("Empty"), NULL, NULL);
976                 }
977
978         recent = gtk_ui_manager_get_widget(lw->ui_manager, "/MainMenu/FileMenu/OpenRecent");
979         gtk_menu_item_set_submenu(GTK_MENU_ITEM(recent), menu);
980         gtk_widget_set_sensitive(recent, (n != 0));
981 }
982
983 void layout_recent_update_all(void)
984 {
985         GList *work;
986
987         work = layout_window_list;
988         while (work)
989                 {
990                 LayoutWindow *lw = work->data;
991                 work = work->next;
992
993                 layout_menu_recent_update(lw);
994                 }
995 }
996
997 void layout_recent_add_path(const gchar *path)
998 {
999         if (!path) return;
1000
1001         history_list_add_to_key("recent", path, recent_list_max);
1002
1003         layout_recent_update_all();
1004 }
1005
1006 /*
1007  *-----------------------------------------------------------------------------
1008  * menu
1009  *-----------------------------------------------------------------------------
1010  */ 
1011
1012 #define CB G_CALLBACK
1013
1014 static GtkActionEntry menu_entries[] = {
1015   { "FileMenu",         NULL,           N_("_File") },
1016   { "GoMenu",           NULL,           N_("_Go") },
1017   { "EditMenu",         NULL,           N_("_Edit") },
1018   { "SelectMenu",       NULL,           N_("_Select") },
1019   { "AdjustMenu",       NULL,           N_("_Adjust") },
1020   { "ViewMenu",         NULL,           N_("_View") },
1021   { "ZoomMenu",         NULL,           N_("_Zoom") },
1022   { "SplitMenu",        NULL,           N_("_Split") },
1023   { "HelpMenu",         NULL,           N_("_Help") },
1024
1025   { "FirstImage",       GTK_STOCK_GOTO_TOP,     N_("_First Image"),     "Home",         NULL,   CB(layout_menu_image_first_cb) },
1026   { "PrevImage",        GTK_STOCK_GO_UP,        N_("_Previous Image"),  "BackSpace",    NULL,   CB(layout_menu_image_prev_cb) },
1027   { "NextImage",        GTK_STOCK_GO_DOWN,      N_("_Next Image"),      "space",        NULL,   CB(layout_menu_image_next_cb) },
1028   { "LastImage",        GTK_STOCK_GOTO_BOTTOM,  N_("_Last Image"),      "End",          NULL,   CB(layout_menu_image_last_cb) },
1029
1030
1031   { "NewWindow",        GTK_STOCK_NEW,  N_("New _window"),      NULL,           NULL,   CB(layout_menu_new_window_cb) },
1032   { "NewCollection",    GTK_STOCK_INDEX,N_("_New collection"),  "C",            NULL,   CB(layout_menu_new_cb) },
1033   { "OpenCollection",   GTK_STOCK_OPEN, N_("_Open collection..."),"O",          NULL,   CB(layout_menu_open_cb) },
1034   { "OpenRecent",       NULL,           N_("Open _recent") },
1035   { "Search",           GTK_STOCK_FIND, N_("_Search..."),       "F3",           NULL,   CB(layout_menu_search_cb) },
1036   { "FindDupes",        GTK_STOCK_FIND, N_("_Find duplicates..."),"D",          NULL,   CB(layout_menu_dupes_cb) },
1037   { "PanView",          NULL,           N_("Pan _view"),        "<control>J",   NULL,   CB(layout_menu_pan_cb) },
1038   { "Print",            GTK_STOCK_PRINT,N_("_Print..."),        "<shift>P",     NULL,   CB(layout_menu_print_cb) },
1039   { "NewFolder",        NULL,           N_("N_ew folder..."),   "<control>F",   NULL,   CB(layout_menu_dir_cb) },
1040   { "Copy",             NULL,           N_("_Copy..."),         "<control>C",   NULL,   CB(layout_menu_copy_cb) },
1041   { "Move",             NULL,           N_("_Move..."),         "<control>M",   NULL,   CB(layout_menu_move_cb) },
1042   { "Rename",           NULL,           N_("_Rename..."),       "<control>R",   NULL,   CB(layout_menu_rename_cb) },
1043   { "Delete",   GTK_STOCK_DELETE,       N_("_Delete..."),       "<control>D",   NULL,   CB(layout_menu_delete_cb) },
1044   { "CloseWindow",      GTK_STOCK_CLOSE,N_("C_lose window"),    "<control>W",   NULL,   CB(layout_menu_close_cb) },
1045   { "Quit",             GTK_STOCK_QUIT, N_("_Quit"),            "<control>Q",   NULL,   CB(layout_menu_exit_cb) },
1046
1047   { "Editor0",          NULL,           "editor0",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1048   { "Editor1",          NULL,           "editor1",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1049   { "Editor2",          NULL,           "editor2",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1050   { "Editor3",          NULL,           "editor3",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1051   { "Editor4",          NULL,           "editor4",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1052   { "Editor5",          NULL,           "editor5",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1053   { "Editor6",          NULL,           "editor6",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1054   { "Editor7",          NULL,           "editor7",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1055   { "Editor8",          NULL,           "editor8",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1056   { "Editor9",          NULL,           "editor9",              NULL,           NULL,   CB(layout_menu_edit_cb) },
1057   { "RotateCW",         NULL,   N_("_Rotate clockwise"),        "bracketright", NULL,   CB(layout_menu_alter_90_cb) },
1058   { "RotateCCW",        NULL,   N_("Rotate _counterclockwise"), "bracketleft",  NULL,   CB(layout_menu_alter_90cc_cb) },
1059   { "Rotate180",        NULL,           N_("Rotate 1_80"),      "<shift>R",     NULL,   CB(layout_menu_alter_180_cb) },
1060   { "Mirror",           NULL,           N_("_Mirror"),          "<shift>M",     NULL,   CB(layout_menu_alter_mirror_cb) },
1061   { "Flip",             NULL,           N_("_Flip"),            "<shift>F",     NULL,   CB(layout_menu_alter_flip_cb) },
1062   { "Grayscale",        NULL,           N_("_Grayscale"),       "<shift>G",     NULL,   CB(layout_menu_alter_desaturate_cb) },
1063   { "Properties",GTK_STOCK_PROPERTIES,  N_("_Properties"),      "<control>P",   NULL,   CB(layout_menu_info_cb) },
1064   { "SelectAll",        NULL,           N_("Select _all"),      "<control>A",   NULL,   CB(layout_menu_select_all_cb) },
1065   { "SelectNone",       NULL,           N_("Select _none"), "<control><shift>A",NULL,   CB(layout_menu_unselect_all_cb) },
1066   { "Preferences",GTK_STOCK_PREFERENCES,N_("P_references..."),  "<control>O",   NULL,   CB(layout_menu_config_cb) },
1067   { "Maintenance",      NULL,           N_("_Thumbnail maintenance..."),NULL,   NULL,   CB(layout_menu_remove_thumb_cb) },
1068   { "Wallpaper",        NULL,           N_("Set as _wallpaper"),NULL,           NULL,   CB(layout_menu_wallpaper_cb) },
1069
1070   { "ZoomIn",   GTK_STOCK_ZOOM_IN,      N_("Zoom _in"),         "equal",        NULL,   CB(layout_menu_zoom_in_cb) },
1071   { "ZoomOut",  GTK_STOCK_ZOOM_OUT,     N_("Zoom _out"),        "minus",        NULL,   CB(layout_menu_zoom_out_cb) },
1072   { "Zoom100",  GTK_STOCK_ZOOM_100,     N_("Zoom _1:1"),        "Z",            NULL,   CB(layout_menu_zoom_1_1_cb) },
1073   { "ZoomFit",  GTK_STOCK_ZOOM_FIT,     N_("_Zoom to fit"),     "X",            NULL,   CB(layout_menu_zoom_fit_cb) },
1074   { "ZoomFillHor",      NULL,           N_("Fit _Horizontally"),"H",            NULL,   CB(layout_menu_zoom_fit_hor_cb) },
1075   { "ZoomFillVert",     NULL,           N_("Fit _Vorizontally"),"W",            NULL,   CB(layout_menu_zoom_fit_vert_cb) },
1076   { "Zoom200",          NULL,           N_("Zoom _2:1"),        NULL,           NULL,   CB(layout_menu_zoom_2_1_cb) },
1077   { "Zoom300",          NULL,           N_("Zoom _3:1"),        NULL,           NULL,   CB(layout_menu_zoom_3_1_cb) },
1078   { "Zoom400",          NULL,           N_("Zoom _4:1"),        NULL,           NULL,   CB(layout_menu_zoom_4_1_cb) },
1079   { "Zoom50",           NULL,           N_("Zoom 1:2"),         NULL,           NULL,   CB(layout_menu_zoom_1_2_cb) },
1080   { "Zoom33",           NULL,           N_("Zoom 1:3"),         NULL,           NULL,   CB(layout_menu_zoom_1_3_cb) },
1081   { "Zoom25",           NULL,           N_("Zoom 1:4"),         NULL,           NULL,   CB(layout_menu_zoom_1_4_cb) },
1082
1083
1084   { "ViewInNewWindow",  NULL,           N_("_View in new window"),      "<control>V",           NULL,   CB(layout_menu_view_in_new_window_cb) },
1085
1086   { "FullScreen",       NULL,           N_("F_ull screen"),     "F",            NULL,   CB(layout_menu_fullscreen_cb) },
1087   { "ImageOverlay",     NULL,           N_("_Image Overlay"),   "I",            NULL,   CB(layout_menu_overlay_cb) },
1088   { "HideTools",        NULL,           N_("_Hide file list"),  "<control>H",   NULL,   CB(layout_menu_hide_cb) },
1089   { "SlideShow",        NULL,           N_("Toggle _slideshow"),"S",            NULL,   CB(layout_menu_slideshow_cb) },
1090   { "Refresh",  GTK_STOCK_REFRESH,      N_("_Refresh"),         "R",            NULL,   CB(layout_menu_refresh_cb) },
1091
1092   { "HelpContents",     GTK_STOCK_HELP, N_("_Contents"),        "F1",           NULL,   CB(layout_menu_help_cb) },
1093   { "HelpShortcuts",    NULL,           N_("_Keyboard shortcuts"),NULL,         NULL,   CB(layout_menu_help_keys_cb) },
1094   { "HelpNotes",        NULL,           N_("_Release notes"),   NULL,           NULL,   CB(layout_menu_notes_cb) },
1095   { "About",            NULL,           N_("_About"),           NULL,           NULL,   CB(layout_menu_about_cb) }
1096 };
1097
1098 static GtkToggleActionEntry menu_toggle_entries[] = {
1099   { "Thumbnails",       NULL,           N_("_Thumbnails"),      "T",            NULL,   CB(layout_menu_thumb_cb) },
1100   { "ShowMarks",        NULL,           N_("Show _Marks"),      "M",            NULL,   CB(layout_menu_marks_cb) },  
1101   { "FolderTree",       NULL,           N_("Tr_ee"),            "<control>T",   NULL,   CB(layout_menu_tree_cb) },
1102   { "FloatTools",       NULL,           N_("_Float file list"), "L",            NULL,   CB(layout_menu_float_cb) },
1103   { "HideToolbar",      NULL,           N_("Hide tool_bar"),    NULL,           NULL,   CB(layout_menu_toolbar_cb) },
1104   { "SBarKeywords",     NULL,           N_("_Keywords"),        "<control>K",   NULL,   CB(layout_menu_bar_info_cb) },
1105   { "SBarExif",         NULL,           N_("E_xif data"),       "<control>E",   NULL,   CB(layout_menu_bar_exif_cb) },
1106   { "SBarSort",         NULL,           N_("Sort _manager"),    "<control>S",   NULL,   CB(layout_menu_bar_sort_cb) },
1107   { "ConnectScroll",    NULL,           N_("Connected scroll"), "<control>U",   NULL,   CB(layout_menu_connect_scroll_cb) },
1108   { "ConnectZoom",      NULL,           N_("Connected zoom"),   "<control>Y",   NULL,   CB(layout_menu_connect_zoom_cb) }
1109 };
1110
1111 static GtkRadioActionEntry menu_radio_entries[] = {
1112   { "ViewList",         NULL,           N_("_List"),            "<control>L",   NULL,   0 },
1113   { "ViewIcons",        NULL,           N_("I_cons"),           "<control>I",   NULL,   1 }
1114 };
1115
1116 static GtkRadioActionEntry menu_split_radio_entries[] = {
1117   { "SplitHorizontal",  NULL,           N_("Horizontal"),       "E",            NULL,   SPLIT_HOR },
1118   { "SplitVertical",    NULL,           N_("Vertical"),         "U",            NULL,   SPLIT_VERT },
1119   { "SplitQuad",        NULL,           N_("Quad"),             "Q",            NULL,   SPLIT_QUAD },
1120   { "SplitSingle",      NULL,           N_("Single"),           "Y",            NULL,   SPLIT_NONE }
1121 };
1122
1123 #undef CB
1124
1125 static const char *menu_ui_description =
1126 "<ui>"
1127 "  <menubar name='MainMenu'>"
1128 "    <menu action='FileMenu'>"
1129 "      <menuitem action='NewWindow'/>"
1130 "      <menuitem action='NewCollection'/>"
1131 "      <menuitem action='OpenCollection'/>"
1132 "      <menuitem action='OpenRecent'/>"
1133 "      <separator/>"
1134 "      <menuitem action='Search'/>"
1135 "      <menuitem action='FindDupes'/>"
1136 "      <menuitem action='PanView'/>"
1137 "      <separator/>"
1138 "      <menuitem action='Print'/>"
1139 "      <menuitem action='NewFolder'/>"
1140 "      <separator/>"
1141 "      <menuitem action='Copy'/>"
1142 "      <menuitem action='Move'/>"
1143 "      <menuitem action='Rename'/>"
1144 "      <menuitem action='Delete'/>"
1145 "      <separator/>"
1146 "      <menuitem action='CloseWindow'/>"
1147 "      <menuitem action='Quit'/>"
1148 "    </menu>"
1149 "    <menu action='GoMenu'>"
1150 "      <menuitem action='FirstImage'/>"
1151 "      <menuitem action='PrevImage'/>"
1152 "      <menuitem action='NextImage'/>"
1153 "      <menuitem action='LastImage'/>"
1154 "    </menu>"
1155 "    <menu action='SelectMenu'>"
1156 "      <menuitem action='SelectAll'/>"
1157 "      <menuitem action='SelectNone'/>"
1158 "      <separator/>"
1159 "      <menuitem action='ShowMarks'/>"
1160 "      <separator/>"
1161 "    </menu>"
1162 "    <menu action='EditMenu'>"
1163 "      <menuitem action='Editor0'/>"
1164 "      <menuitem action='Editor1'/>"
1165 "      <menuitem action='Editor2'/>"
1166 "      <menuitem action='Editor3'/>"
1167 "      <menuitem action='Editor4'/>"
1168 "      <menuitem action='Editor5'/>"
1169 "      <menuitem action='Editor6'/>"
1170 "      <menuitem action='Editor7'/>"
1171 "      <menuitem action='Editor8'/>"
1172 "      <menuitem action='Editor9'/>"
1173 "      <separator/>"
1174 "      <menu action='AdjustMenu'>"
1175 "        <menuitem action='RotateCW'/>"
1176 "        <menuitem action='RotateCCW'/>"
1177 "        <menuitem action='Rotate180'/>"
1178 "        <menuitem action='Mirror'/>"
1179 "        <menuitem action='Flip'/>"
1180 "        <menuitem action='Grayscale'/>"
1181 "      </menu>"
1182 "      <menuitem action='Properties'/>"
1183 "      <separator/>"
1184 "      <menuitem action='Preferences'/>"
1185 "      <menuitem action='Maintenance'/>"
1186 "      <separator/>"
1187 "      <menuitem action='Wallpaper'/>"
1188 "    </menu>"
1189 "    <menu action='ViewMenu'>"
1190 "      <menuitem action='ViewInNewWindow'/>"
1191 "      <separator/>"
1192 "      <menu action='ZoomMenu'>"
1193 "        <menuitem action='ZoomIn'/>"
1194 "        <menuitem action='ZoomOut'/>"
1195 "        <menuitem action='ZoomFit'/>"
1196 "        <menuitem action='ZoomFillHor'/>"
1197 "        <menuitem action='ZoomFillVert'/>"
1198 "        <menuitem action='Zoom100'/>"
1199 "        <menuitem action='Zoom200'/>"
1200 "        <menuitem action='Zoom300'/>"
1201 "        <menuitem action='Zoom400'/>"
1202 "        <menuitem action='Zoom50'/>"
1203 "        <menuitem action='Zoom33'/>"
1204 "        <menuitem action='Zoom25'/>"
1205 "      </menu>"
1206 "      <separator/>"
1207 "      <menu action='SplitMenu'>"
1208 "        <menuitem action='SplitHorizontal'/>"
1209 "        <menuitem action='SplitVertical'/>"
1210 "        <menuitem action='SplitQuad'/>"
1211 "        <menuitem action='SplitSingle'/>"
1212 "      </menu>"
1213 "      <menuitem action='ConnectScroll'/>"
1214 "      <menuitem action='ConnectZoom'/>"
1215 "      <separator/>"
1216 "      <menuitem action='Thumbnails'/>"
1217 "      <menuitem action='ViewList'/>"
1218 "      <menuitem action='ViewIcons'/>"
1219 "      <separator/>"
1220 "      <menuitem action='FolderTree'/>"
1221 "      <menuitem action='ImageOverlay'/>"
1222 "      <menuitem action='FullScreen'/>"
1223 "      <separator/>"
1224 "      <menuitem action='FloatTools'/>"
1225 "      <menuitem action='HideTools'/>"
1226 "      <menuitem action='HideToolbar'/>"
1227 "      <separator/>"
1228 "      <menuitem action='SBarKeywords'/>"
1229 "      <menuitem action='SBarExif'/>"
1230 "      <menuitem action='SBarSort'/>"
1231 "      <separator/>"
1232 "      <menuitem action='SlideShow'/>"
1233 "      <menuitem action='Refresh'/>"
1234 "    </menu>"
1235 "    <menu action='HelpMenu'>"
1236 "      <separator/>"
1237 "      <menuitem action='HelpContents'/>"
1238 "      <menuitem action='HelpShortcuts'/>"
1239 "      <menuitem action='HelpNotes'/>"
1240 "      <separator/>"
1241 "      <menuitem action='About'/>"
1242 "    </menu>"
1243 "  </menubar>"
1244 "</ui>";
1245
1246
1247 static gchar *menu_translate(const gchar *path, gpointer data)
1248 {
1249         return _(path);
1250 }
1251
1252 static void layout_actions_setup_mark(LayoutWindow *lw, gint mark, char *name_tmpl, char *label_tmpl, char *accel_tmpl,  GCallback cb)
1253 {
1254         char name[50];
1255         char label[100];
1256         char accel[50];
1257         GtkActionEntry entry = { name, NULL, label, accel, NULL, cb };
1258         GtkAction *action;
1259
1260         g_snprintf(name, sizeof(name), name_tmpl, mark);
1261         g_snprintf(label, sizeof(label), label_tmpl, mark);
1262         if (accel_tmpl)
1263                 g_snprintf(accel, sizeof(accel), accel_tmpl, mark % 10);
1264         else
1265                 accel[0] = 0;
1266         gtk_action_group_add_actions(lw->action_group, &entry, 1, lw);
1267         action = gtk_action_group_get_action(lw->action_group, name);
1268         g_object_set_data(G_OBJECT(action), "mark_num", GINT_TO_POINTER(mark));
1269 }
1270
1271 static void layout_actions_setup_marks(LayoutWindow *lw)
1272 {
1273         int mark;
1274         GError *error;
1275         GString *desc = g_string_new(
1276                                 "<ui>"
1277                                 "  <menubar name='MainMenu'>"
1278                                 "    <menu action='SelectMenu'>");
1279         
1280         for (mark = 1; mark <= FILEDATA_MARKS_SIZE; mark++)
1281                 {
1282                 layout_actions_setup_mark(lw, mark, "Mark%d",           _("Mark _%d"), NULL, NULL);
1283                 layout_actions_setup_mark(lw, mark, "SetMark%d",        _("_Set mark %d"),                      NULL, G_CALLBACK(layout_menu_set_mark_sel_cb));
1284                 layout_actions_setup_mark(lw, mark, "ResetMark%d",      _("_Reset mark %d"),                    NULL, G_CALLBACK(layout_menu_res_mark_sel_cb));
1285                 layout_actions_setup_mark(lw, mark, "ToggleMark%d",     _("_Toggle mark %d"),                   "%d", G_CALLBACK(layout_menu_toggle_mark_sel_cb));
1286                 layout_actions_setup_mark(lw, mark, "SelectMark%d",     _("_Select mark %d"),                   "<control>%d", G_CALLBACK(layout_menu_sel_mark_cb));
1287                 layout_actions_setup_mark(lw, mark, "AddMark%d",        _("_Add mark %d"),                      NULL, G_CALLBACK(layout_menu_sel_mark_or_cb));
1288                 layout_actions_setup_mark(lw, mark, "IntMark%d",        _("_Intersection with mark %d"),        NULL, G_CALLBACK(layout_menu_sel_mark_and_cb));
1289                 layout_actions_setup_mark(lw, mark, "UnselMark%d",      _("_Unselect mark %d"),                 NULL, G_CALLBACK(layout_menu_sel_mark_minus_cb));
1290
1291                 g_string_append_printf(desc,
1292                                 "      <menu action='Mark%d'>"
1293                                 "        <menuitem action='ToggleMark%d'/>"
1294                                 "        <menuitem action='SetMark%d'/>"
1295                                 "        <menuitem action='ResetMark%d'/>"
1296                                 "        <separator/>"
1297                                 "        <menuitem action='SelectMark%d'/>"
1298                                 "        <menuitem action='AddMark%d'/>"
1299                                 "        <menuitem action='IntMark%d'/>"
1300                                 "        <menuitem action='UnselMark%d'/>"
1301                                 "      </menu>",
1302                                 mark, mark, mark, mark, mark, mark, mark, mark);
1303                 }
1304
1305         g_string_append(desc,
1306                                 "    </menu>"
1307                                 "  </menubar>"
1308                                 "</ui>" );
1309
1310         error = NULL;
1311         if (!gtk_ui_manager_add_ui_from_string(lw->ui_manager, desc->str, -1, &error))
1312                 {
1313                 g_message ("building menus failed: %s", error->message);
1314                 g_error_free (error);
1315                 exit (EXIT_FAILURE);
1316                 }
1317         g_string_free(desc, TRUE);
1318 }
1319
1320 void layout_actions_setup(LayoutWindow *lw)
1321 {
1322         GError *error;
1323
1324         if (lw->ui_manager) return;
1325
1326         lw->action_group = gtk_action_group_new ("MenuActions");
1327         gtk_action_group_set_translate_func(lw->action_group, menu_translate, NULL, NULL);
1328
1329         gtk_action_group_add_actions(lw->action_group,
1330                                      menu_entries, G_N_ELEMENTS(menu_entries), lw);
1331         gtk_action_group_add_toggle_actions(lw->action_group,
1332                                             menu_toggle_entries, G_N_ELEMENTS(menu_toggle_entries), lw);
1333         gtk_action_group_add_radio_actions(lw->action_group,
1334                                            menu_radio_entries, G_N_ELEMENTS(menu_radio_entries),
1335                                            0, G_CALLBACK(layout_menu_list_cb), lw);
1336         gtk_action_group_add_radio_actions(lw->action_group,
1337                                            menu_split_radio_entries, G_N_ELEMENTS(menu_split_radio_entries),
1338                                            0, G_CALLBACK(layout_menu_split_cb), lw);
1339
1340         lw->ui_manager = gtk_ui_manager_new();
1341         gtk_ui_manager_set_add_tearoffs(lw->ui_manager, TRUE);
1342         gtk_ui_manager_insert_action_group(lw->ui_manager, lw->action_group, 0);
1343
1344         error = NULL;
1345         if (!gtk_ui_manager_add_ui_from_string(lw->ui_manager, menu_ui_description, -1, &error))
1346                 {
1347                 g_message ("building menus failed: %s", error->message);
1348                 g_error_free (error);
1349                 exit (EXIT_FAILURE);
1350                 }
1351
1352         layout_actions_setup_marks(lw);
1353 }
1354
1355 void layout_actions_add_window(LayoutWindow *lw, GtkWidget *window)
1356 {
1357         GtkAccelGroup *group;
1358
1359         if (!lw->ui_manager) return;
1360
1361         group = gtk_ui_manager_get_accel_group(lw->ui_manager);
1362         gtk_window_add_accel_group(GTK_WINDOW(window), group);
1363 }
1364
1365 GtkWidget *layout_actions_menu_bar(LayoutWindow *lw)
1366 {
1367         return gtk_ui_manager_get_widget(lw->ui_manager, "/MainMenu");
1368 }
1369
1370
1371 /*
1372  *-----------------------------------------------------------------------------
1373  * toolbar
1374  *-----------------------------------------------------------------------------
1375  */ 
1376
1377 static void layout_button_thumb_cb(GtkWidget *widget, gpointer data)
1378 {
1379         LayoutWindow *lw = data;
1380
1381         layout_thumb_set(lw, gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(widget)));
1382 }
1383
1384 static void layout_button_home_cb(GtkWidget *widget, gpointer data)
1385 {
1386         LayoutWindow *lw = data;
1387         const gchar *path = homedir();
1388
1389         if (path) layout_set_path(lw, path);
1390 }
1391
1392 static void layout_button_refresh_cb(GtkWidget *widget, gpointer data)
1393 {
1394         LayoutWindow *lw = data;
1395
1396         layout_refresh(lw);
1397 }
1398
1399 static void layout_button_zoom_in_cb(GtkWidget *widget, gpointer data)
1400 {
1401         LayoutWindow *lw = data;
1402
1403         layout_image_zoom_adjust(lw, get_zoom_increment());
1404 }
1405
1406 static void layout_button_zoom_out_cb(GtkWidget *widget, gpointer data)
1407 {
1408         LayoutWindow *lw = data;
1409
1410         layout_image_zoom_adjust(lw, -get_zoom_increment());
1411 }
1412
1413 static void layout_button_zoom_fit_cb(GtkWidget *widget, gpointer data)
1414 {
1415         LayoutWindow *lw = data;
1416
1417         layout_image_zoom_set(lw, 0.0);
1418 }
1419
1420 static void layout_button_zoom_1_1_cb(GtkWidget *widget, gpointer data)
1421 {
1422         LayoutWindow *lw = data;
1423
1424         layout_image_zoom_set(lw, 1.0);
1425 }
1426
1427 static void layout_button_config_cb(GtkWidget *widget, gpointer data)
1428 {
1429         show_config_window();
1430 }
1431
1432 static void layout_button_float_cb(GtkWidget *widget, gpointer data)
1433 {
1434         LayoutWindow *lw = data;
1435
1436         layout_tools_float_toggle(lw);
1437 }
1438
1439 static void layout_button_custom_icon(GtkWidget *button, const gchar *key)
1440 {
1441         GtkWidget *icon;
1442         GdkPixbuf *pixbuf;
1443
1444         pixbuf = pixbuf_inline(key);
1445         if (!pixbuf) return;
1446
1447         icon = gtk_image_new_from_pixbuf(pixbuf);
1448         g_object_unref(pixbuf);
1449
1450         pref_toolbar_button_set_icon(button, icon, NULL);
1451         gtk_widget_show(icon);
1452 }
1453
1454 GtkWidget *layout_button_bar(LayoutWindow *lw)
1455 {
1456         GtkWidget *box;
1457         GtkWidget *button;
1458
1459         box =  pref_toolbar_new(NULL, GTK_TOOLBAR_ICONS);
1460
1461         button = pref_toolbar_button(box, NULL, _("_Thumbnails"), TRUE,
1462                                      _("Show thumbnails"), G_CALLBACK(layout_button_thumb_cb), lw);
1463         layout_button_custom_icon(button, PIXBUF_INLINE_ICON_THUMB);
1464         lw->thumb_button = button;
1465
1466         pref_toolbar_button(box, GTK_STOCK_HOME, NULL, FALSE,
1467                             _("Change to home folder"), G_CALLBACK(layout_button_home_cb), lw);
1468         pref_toolbar_button(box, GTK_STOCK_REFRESH, NULL, FALSE,
1469                             _("Refresh file list"), G_CALLBACK(layout_button_refresh_cb), lw);
1470         pref_toolbar_button(box, GTK_STOCK_ZOOM_IN, NULL, FALSE,
1471                             _("Zoom in"), G_CALLBACK(layout_button_zoom_in_cb), lw);
1472         pref_toolbar_button(box, GTK_STOCK_ZOOM_OUT, NULL, FALSE,
1473                             _("Zoom out"), G_CALLBACK(layout_button_zoom_out_cb), lw);
1474         pref_toolbar_button(box, GTK_STOCK_ZOOM_FIT, NULL, FALSE,
1475                             _("Fit image to window"), G_CALLBACK(layout_button_zoom_fit_cb), lw);
1476         pref_toolbar_button(box, GTK_STOCK_ZOOM_100, NULL, FALSE,
1477                             _("Set zoom 1:1"), G_CALLBACK(layout_button_zoom_1_1_cb), lw);
1478         pref_toolbar_button(box, GTK_STOCK_PREFERENCES, NULL, FALSE,
1479                             _("Configure options"), G_CALLBACK(layout_button_config_cb), lw);
1480         button = pref_toolbar_button(box, NULL, _("_Float"), FALSE,
1481                                      _("Float Controls"), G_CALLBACK(layout_button_float_cb), lw);
1482         layout_button_custom_icon(button, PIXBUF_INLINE_ICON_FLOAT);
1483
1484         return box;
1485 }
1486
1487 /*
1488  *-----------------------------------------------------------------------------
1489  * misc
1490  *-----------------------------------------------------------------------------
1491  */
1492
1493 static void layout_util_sync_views(LayoutWindow *lw)
1494 {
1495         GtkAction *action;
1496
1497         if (!lw->action_group) return;
1498
1499         action = gtk_action_group_get_action(lw->action_group, "FolderTree");
1500         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->tree_view);
1501
1502         action = gtk_action_group_get_action(lw->action_group, "ViewIcons");
1503         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->icon_view);
1504
1505         action = gtk_action_group_get_action(lw->action_group, "FloatTools");
1506         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->tools_float);
1507
1508         action = gtk_action_group_get_action(lw->action_group, "SBarKeywords");
1509         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->bar_info_enabled);
1510
1511         action = gtk_action_group_get_action(lw->action_group, "SBarExif");
1512         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->bar_exif_enabled);
1513
1514         action = gtk_action_group_get_action(lw->action_group, "SBarSort");
1515         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->bar_sort_enabled);
1516
1517         action = gtk_action_group_get_action(lw->action_group, "HideToolbar");
1518         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->toolbar_hidden);
1519 }
1520
1521 void layout_util_sync_thumb(LayoutWindow *lw)
1522 {
1523         GtkAction *action;
1524
1525         if (!lw->action_group) return;
1526
1527         action = gtk_action_group_get_action(lw->action_group, "Thumbnails");
1528         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->thumbs_enabled);
1529         g_object_set(action, "sensitive", !lw->icon_view, NULL);
1530
1531         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(lw->thumb_button), lw->thumbs_enabled);
1532         gtk_widget_set_sensitive(lw->thumb_button, !lw->icon_view);
1533 }
1534
1535 void layout_util_sync(LayoutWindow *lw)
1536 {
1537         layout_util_sync_views(lw);
1538         layout_util_sync_thumb(lw);
1539         layout_menu_recent_update(lw);
1540         layout_menu_edit_update(lw);
1541 }
1542
1543 /*
1544  *-----------------------------------------------------------------------------
1545  * icons (since all the toolbar icons are included here, best place as any)
1546  *-----------------------------------------------------------------------------
1547  */
1548
1549 PixmapFolders *folder_icons_new(void)
1550 {
1551         PixmapFolders *pf;
1552
1553         pf = g_new0(PixmapFolders, 1);
1554
1555         pf->close = pixbuf_inline(PIXBUF_INLINE_FOLDER_CLOSED);
1556         pf->open = pixbuf_inline(PIXBUF_INLINE_FOLDER_OPEN);
1557         pf->deny = pixbuf_inline(PIXBUF_INLINE_FOLDER_LOCKED);
1558         pf->parent = pixbuf_inline(PIXBUF_INLINE_FOLDER_UP);
1559
1560         return pf;
1561 }
1562
1563 void folder_icons_free(PixmapFolders *pf)
1564 {
1565         if (!pf) return;
1566
1567         g_object_unref(pf->close);
1568         g_object_unref(pf->open);
1569         g_object_unref(pf->deny);
1570         g_object_unref(pf->parent);
1571
1572         g_free(pf);
1573 }
1574
1575 /*
1576  *-----------------------------------------------------------------------------
1577  * sidebars
1578  *-----------------------------------------------------------------------------
1579  */
1580
1581 #define SIDEBAR_WIDTH 288
1582
1583 static void layout_bar_info_destroyed(GtkWidget *widget, gpointer data)
1584 {
1585         LayoutWindow *lw = data;
1586
1587         lw->bar_info = NULL;
1588
1589         if (lw->utility_box)
1590                 {
1591                 /* destroyed from within itself */
1592                 lw->bar_info_enabled = FALSE;
1593                 layout_util_sync_views(lw);
1594                 }
1595 }
1596
1597 static GList *layout_bar_info_list_cb(gpointer data)
1598 {
1599         LayoutWindow *lw = data;
1600
1601         return layout_selection_list(lw);
1602 }
1603
1604 static void layout_bar_info_new(LayoutWindow *lw)
1605 {
1606         if (!lw->utility_box) return;
1607                                                                                                                     
1608         lw->bar_info = bar_info_new(layout_image_get_fd(lw), FALSE, lw->utility_box);
1609         bar_info_set_selection_func(lw->bar_info, layout_bar_info_list_cb, lw);
1610         bar_info_selection(lw->bar_info, layout_selection_count(lw, NULL) - 1);
1611         bar_info_size_request(lw->bar_info, SIDEBAR_WIDTH * 3 / 4);
1612         g_signal_connect(G_OBJECT(lw->bar_info), "destroy",
1613                          G_CALLBACK(layout_bar_info_destroyed), lw);
1614         lw->bar_info_enabled = TRUE;
1615
1616         gtk_box_pack_start(GTK_BOX(lw->utility_box), lw->bar_info, FALSE, FALSE, 0);
1617         gtk_widget_show(lw->bar_info);
1618 }
1619                                                                                                                     
1620 static void layout_bar_info_close(LayoutWindow *lw)
1621 {
1622         if (lw->bar_info)
1623                 {
1624                 bar_info_close(lw->bar_info);
1625                 lw->bar_info = NULL;
1626                 }
1627         lw->bar_info_enabled = FALSE;
1628 }
1629
1630 void layout_bar_info_toggle(LayoutWindow *lw)
1631 {
1632         if (lw->bar_info_enabled)
1633                 {
1634                 layout_bar_info_close(lw);
1635                 }
1636         else
1637                 {
1638                 layout_bar_info_new(lw);
1639                 }
1640 }
1641
1642 static void layout_bar_info_new_image(LayoutWindow *lw)
1643 {
1644         if (!lw->bar_info || !lw->bar_info_enabled) return;
1645
1646         bar_info_set(lw->bar_info, layout_image_get_fd(lw));
1647 }
1648
1649 static void layout_bar_info_new_selection(LayoutWindow *lw, gint count)
1650 {
1651         if (!lw->bar_info || !lw->bar_info_enabled) return;
1652
1653         bar_info_selection(lw->bar_info, count - 1);
1654 }
1655
1656 static void layout_bar_info_maint_renamed(LayoutWindow *lw)
1657 {
1658         if (!lw->bar_info || !lw->bar_info_enabled) return;
1659
1660         bar_info_maint_renamed(lw->bar_info, layout_image_get_fd(lw));
1661 }
1662
1663 static void layout_bar_exif_destroyed(GtkWidget *widget, gpointer data)
1664 {
1665         LayoutWindow *lw = data;
1666
1667         if (lw->bar_exif)
1668                 {
1669                 lw->bar_exif_advanced = bar_exif_is_advanced(lw->bar_exif);
1670                 }
1671
1672         lw->bar_exif = NULL;
1673         if (lw->utility_box)
1674                 {
1675                 /* destroyed from within itself */
1676                 lw->bar_exif_enabled = FALSE;
1677                 layout_util_sync_views(lw);
1678                 }
1679 }
1680
1681 static void layout_bar_exif_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
1682 {
1683         LayoutWindow *lw = data;
1684
1685         if (lw->bar_exif)
1686                 {
1687                 lw->bar_exif_size = allocation->width;
1688                 }
1689 }
1690
1691 static void layout_bar_exif_new(LayoutWindow *lw)
1692 {
1693         if (!lw->utility_box) return;
1694
1695         lw->bar_exif = bar_exif_new(TRUE, layout_image_get_fd(lw),
1696                                     lw->bar_exif_advanced, lw->utility_box);
1697         g_signal_connect(G_OBJECT(lw->bar_exif), "destroy",
1698                          G_CALLBACK(layout_bar_exif_destroyed), lw);
1699         g_signal_connect(G_OBJECT(lw->bar_exif), "size_allocate",
1700                          G_CALLBACK(layout_bar_exif_sized), lw);
1701         lw->bar_exif_enabled = TRUE;
1702
1703         if (lw->bar_exif_size < 1) lw->bar_exif_size = SIDEBAR_WIDTH;
1704         gtk_widget_set_size_request(lw->bar_exif, lw->bar_exif_size, -1);
1705         gtk_box_pack_start(GTK_BOX(lw->utility_box), lw->bar_exif, FALSE, FALSE, 0);
1706         if (lw->bar_info) gtk_box_reorder_child(GTK_BOX(lw->utility_box), lw->bar_exif, 1);
1707         gtk_widget_show(lw->bar_exif);
1708 }
1709
1710 static void layout_bar_exif_close(LayoutWindow *lw)
1711 {
1712         if (lw->bar_exif)
1713                 {
1714                 bar_exif_close(lw->bar_exif);
1715                 lw->bar_exif = NULL;
1716                 }
1717         lw->bar_exif_enabled = FALSE;
1718 }
1719
1720 void layout_bar_exif_toggle(LayoutWindow *lw)
1721 {
1722         if (lw->bar_exif_enabled)
1723                 {
1724                 layout_bar_exif_close(lw);
1725                 }
1726         else
1727                 {
1728                 layout_bar_exif_new(lw);
1729                 }
1730 }
1731
1732 static void layout_bar_exif_new_image(LayoutWindow *lw)
1733 {
1734         if (!lw->bar_exif || !lw->bar_exif_enabled) return;
1735
1736         bar_exif_set(lw->bar_exif, layout_image_get_fd(lw));
1737 }
1738
1739 static void layout_bar_sort_destroyed(GtkWidget *widget, gpointer data)
1740 {
1741         LayoutWindow *lw = data;
1742
1743         lw->bar_sort = NULL;
1744
1745         if (lw->utility_box)
1746                 {
1747                 /* destroyed from within itself */
1748                 lw->bar_sort_enabled = FALSE;
1749
1750                 layout_util_sync_views(lw);
1751                 }
1752 }
1753
1754 static void layout_bar_sort_new(LayoutWindow *lw)
1755 {
1756         if (!lw->utility_box) return;
1757
1758         lw->bar_sort = bar_sort_new(lw);
1759         g_signal_connect(G_OBJECT(lw->bar_sort), "destroy",
1760                          G_CALLBACK(layout_bar_sort_destroyed), lw);
1761         lw->bar_sort_enabled = TRUE;
1762
1763         gtk_box_pack_end(GTK_BOX(lw->utility_box), lw->bar_sort, FALSE, FALSE, 0);
1764         gtk_widget_show(lw->bar_sort);
1765 }
1766
1767 static void layout_bar_sort_close(LayoutWindow *lw)
1768 {
1769         if (lw->bar_sort)
1770                 {
1771                 bar_sort_close(lw->bar_sort);
1772                 lw->bar_sort = NULL;
1773                 }
1774         lw->bar_sort_enabled = FALSE;
1775 }
1776
1777 void layout_bar_sort_toggle(LayoutWindow *lw)
1778 {
1779         if (lw->bar_sort_enabled)
1780                 {
1781                 layout_bar_sort_close(lw);
1782                 }
1783         else
1784                 {
1785                 layout_bar_sort_new(lw);
1786                 }
1787 }
1788
1789 void layout_bars_new_image(LayoutWindow *lw)
1790 {
1791         layout_bar_info_new_image(lw);
1792         layout_bar_exif_new_image(lw);
1793 }
1794
1795 void layout_bars_new_selection(LayoutWindow *lw, gint count)
1796 {
1797         layout_bar_info_new_selection(lw, count);
1798 }
1799
1800 GtkWidget *layout_bars_prepare(LayoutWindow *lw, GtkWidget *image)
1801 {
1802         lw->utility_box = gtk_hbox_new(FALSE, PREF_PAD_GAP);
1803         gtk_box_pack_start(GTK_BOX(lw->utility_box), image, TRUE, TRUE, 0);
1804         gtk_widget_show(image);
1805
1806         if (lw->bar_sort_enabled)
1807                 {
1808                 layout_bar_sort_new(lw);
1809                 }
1810
1811         if (lw->bar_info_enabled)
1812                 {
1813                 layout_bar_info_new(lw);
1814                 }
1815
1816         if (lw->bar_exif_enabled)
1817                 {
1818                 layout_bar_exif_new(lw);
1819                 }
1820
1821         return lw->utility_box;
1822 }
1823
1824 void layout_bars_close(LayoutWindow *lw)
1825 {
1826         layout_bar_sort_close(lw);
1827         layout_bar_exif_close(lw);
1828         layout_bar_info_close(lw);
1829 }
1830
1831 void layout_bars_maint_renamed(LayoutWindow *lw)
1832 {
1833         layout_bar_info_maint_renamed(lw);
1834 }
1835