File: menus.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-8.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 54,328 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (1309 lines) | stat: -rw-r--r-- 48,535 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 * Copyright (C) 2006 Robert Staudinger <robert.staudinger@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>

#include "intl.h"
#include "menus.h"
#include "tool.h"
#include "commands.h"
#include "message.h"
#include "interface.h"
#include "display.h"
#include "filedlg.h"
#include "find-and-replace.h"
#include "plugin-manager.h"
#include "select.h"
#include "dia_dirs.h"
#include "object_ops.h"
#include "dia-app-icons.h"
#include "widgets.h"
#include "preferences.h"
#include "filter.h"
#include "objchange.h"
#include "toolbox.h"
#include "diagram_tree.h"

#define DIA_STOCK_GROUP "dia-stock-group"
#define DIA_STOCK_UNGROUP "dia-stock-ungroup"
#define DIA_STOCK_LAYER_ADD "dia-stock-layer-add"
#define DIA_STOCK_LAYER_RENAME "dia-stock-layer-rename"
#define DIA_STOCK_OBJECTS_LAYER_ABOVE "dia-stock-objects-layer-above"
#define DIA_STOCK_OBJECTS_LAYER_BELOW "dia-stock-objects-layer-below"
#define DIA_STOCK_LAYERS "dia-stock-layers"

#define DIA_SHOW_TEAROFFS TRUE

/* Integrated UI Toolbar Constants */
#define DIA_INTEGRATED_TOOLBAR_ZOOM_COMBO  "dia-integrated-toolbar-zoom-combo_entry"
#define DIA_INTEGRATED_TOOLBAR_SNAP_GRID   "dia-integrated-toolbar-snap-grid"
#define DIA_INTEGRATED_TOOLBAR_OBJECT_SNAP "dia-integrated-toolbar-object-snap"

#define ZOOM_FIT        _("Fit")

static void plugin_callback (GtkWidget *widget, gpointer data);

static GtkWidget * 
create_integrated_ui_toolbar (void);

static void add_plugin_actions (GtkUIManager *ui_manager, const char *base_path);

gchar *build_ui_filename (const gchar* name);

/* Active/inactive state is set in diagram_update_menu_sensitivity()
 * in diagram.c */

#ifdef HAVE_MAC_INTEGRATION
# define FIRST_MODIFIER "<Primary>"
/* On OSX/Quartz the unmodified tool accelerators are in conflict with the global menu */
# define TOOL_MODIFIER "<Control>"
#else
# define FIRST_MODIFIER "<Control>"
/**/
# define TOOL_MODIFIER ""
#endif

/* Actions common to toolbox and diagram window */
static const GtkActionEntry common_entries[] =
{
  { "File", NULL, N_("_File"), NULL, NULL, NULL },
    { "FileNew", GTK_STOCK_NEW, NULL, FIRST_MODIFIER "N", N_("Create a new diagram"), G_CALLBACK (file_new_callback) },
    { "FileOpen", GTK_STOCK_OPEN, N_("_Open\342\200\246"),FIRST_MODIFIER "O", N_("Open a diagram file"), G_CALLBACK (file_open_callback) },
    { "FileQuit", GTK_STOCK_QUIT, NULL, FIRST_MODIFIER "Q", N_("Quit Dia"), G_CALLBACK (file_quit_callback) }, 
  { "Help", NULL, N_("_Help"), NULL, NULL, NULL },
    { "HelpContents", GTK_STOCK_HELP, NULL, "F1", N_("Dia help"), G_CALLBACK (help_manual_callback) },
    { "HelpAbout", GTK_STOCK_ABOUT, NULL, NULL, N_("Dia version, authors, license"), G_CALLBACK (help_about_callback) }
};

/* Actions for toolbox menu */
static const GtkActionEntry toolbox_entries[] = 
{
    { "FileSheets", NULL, N_("Sheets and Objects\342\200\246"), "F9", N_("Manage sheets and their objects"), G_CALLBACK (sheets_dialog_show_callback) },
    { "FilePrefs", GTK_STOCK_PREFERENCES, NULL, NULL, N_("Dia preferences"), G_CALLBACK (file_preferences_callback) },
    { "FilePlugins", NULL, N_("Plugins\342\200\246"), NULL, N_("Manage plug-ins"), G_CALLBACK (file_plugins_callback) },
    { "FileTree", NULL, N_("_Diagram Tree"), "F8", N_("Tree representation of diagrams"), G_CALLBACK (diagram_tree_show) }
};

static const GtkToggleActionEntry integrated_ui_view_toggle_entries[] = 
{
    { VIEW_MAIN_TOOLBAR_ACTION,   NULL, N_("Show Toolbar"),   NULL, N_("Show or hide the toolbar"), G_CALLBACK (view_main_toolbar_callback) },
    { VIEW_MAIN_STATUSBAR_ACTION, NULL, N_("Show Statusbar"), NULL, N_("Show or hide the statusbar"), G_CALLBACK (view_main_statusbar_callback) },
    { VIEW_LAYERS_ACTION,    NULL, N_("Show Layers"), FIRST_MODIFIER "L", N_("Show or hide the layers toolwindow"), G_CALLBACK (view_layers_callback) }
};

/* Actions for diagram window */
static const GtkActionEntry display_entries[] =
{
    { "FileSave", GTK_STOCK_SAVE, NULL, FIRST_MODIFIER "S", N_("Save the diagram"), G_CALLBACK (file_save_callback) },
    { "FileSaveas", GTK_STOCK_SAVE_AS, N_("Save _As\342\200\246"), FIRST_MODIFIER "<shift>S", N_("Save the diagram with a new name"), G_CALLBACK (file_save_as_callback) },
    { "FileExport", GTK_STOCK_CONVERT, N_("_Export\342\200\246"), NULL, N_("Export the diagram"), G_CALLBACK (file_export_callback) },
    { "DiagramProperties", GTK_STOCK_PROPERTIES, N_("_Diagram Properties"), "<shift><alt>Return", N_("Modify diagram properties (grid, background)"), G_CALLBACK (view_diagram_properties_callback) },
    { "FilePagesetup", NULL, N_("Page Set_up\342\200\246"), NULL, N_("Modify the diagram pagination"), G_CALLBACK (file_pagesetup_callback) },
    { "FilePrint", GTK_STOCK_PRINT, N_("_Print\342\200\246"), FIRST_MODIFIER "P", N_("Print the diagram"), G_CALLBACK (file_print_callback) },
    { "FileClose", GTK_STOCK_CLOSE, NULL, FIRST_MODIFIER "W", N_("Close the diagram"), G_CALLBACK (file_close_callback) },

  { "Edit", NULL, N_("_Edit"), NULL, NULL, NULL },
    { "EditUndo", GTK_STOCK_UNDO, NULL, FIRST_MODIFIER "Z", N_("Undo"), G_CALLBACK (edit_undo_callback) },
    { "EditRedo", GTK_STOCK_REDO, NULL, FIRST_MODIFIER "<shift>Z", N_("Redo"), G_CALLBACK (edit_redo_callback) },

    { "EditCopy", GTK_STOCK_COPY, NULL, FIRST_MODIFIER "C", N_("Copy selection"), G_CALLBACK (edit_copy_callback) },
    { "EditCut", GTK_STOCK_CUT, NULL, FIRST_MODIFIER "X", N_("Cut selection"), G_CALLBACK (edit_cut_callback) },
    { "EditPaste", GTK_STOCK_PASTE, NULL, FIRST_MODIFIER "V", N_("Paste selection"), G_CALLBACK (edit_paste_callback) },
    { "EditDuplicate", NULL, N_("_Duplicate"), FIRST_MODIFIER "D", N_("Duplicate selection"), G_CALLBACK (edit_duplicate_callback) },
    { "EditDelete", GTK_STOCK_DELETE, NULL, "Delete", N_("Delete selection"), G_CALLBACK (edit_delete_callback) },

    { "EditFind", GTK_STOCK_FIND, N_("_Find\342\200\246"), FIRST_MODIFIER "F", N_("Search for text"), G_CALLBACK (edit_find_callback) },
    { "EditReplace", GTK_STOCK_FIND_AND_REPLACE, N_("_Replace\342\200\246"), FIRST_MODIFIER "H", N_("Search and replace text"), G_CALLBACK (edit_replace_callback) },

    /* the following used to bind to <control><shift>C which collides with Unicode input. 
     * <control><alt> doesn't work either */
    { "EditCopytext", NULL, N_("Copy Text"), NULL, N_("Copy object's text to clipboard"), G_CALLBACK (edit_copy_text_callback) },
    { "EditCuttext", NULL, N_("Cut Text"), FIRST_MODIFIER "<shift>X", N_("Cut object's text to clipboard"), G_CALLBACK (edit_cut_text_callback) },
    { "EditPastetext", NULL, N_("Paste _Text"), FIRST_MODIFIER "<shift>V", N_("Insert text from clipboard"), G_CALLBACK (edit_paste_text_callback) } ,

    { "EditPasteImage", NULL, N_("Paste _Image"), FIRST_MODIFIER "<alt>V", N_("Insert image from clipboard"), G_CALLBACK (edit_paste_image_callback) },

  { "Layers", NULL, N_("_Layers"), NULL, NULL, NULL }, 
    { "LayerAdd", DIA_STOCK_LAYER_ADD, N_("Add Layer\342\200\246"), NULL, NULL, G_CALLBACK (layers_add_layer_callback) },
    { "LayerRename", DIA_STOCK_LAYER_RENAME, N_("Rename Layer\342\200\246"), NULL, NULL, G_CALLBACK (layers_rename_layer_callback) },
    { "ObjectsLayerAbove", DIA_STOCK_OBJECTS_LAYER_ABOVE, N_("Move Selection to Layer above"), NULL, NULL, G_CALLBACK (objects_move_up_layer) },
    { "ObjectsLayerBelow", DIA_STOCK_OBJECTS_LAYER_BELOW, N_("Move Selection to Layer below"), NULL, NULL, G_CALLBACK (objects_move_down_layer) },
    { "DiagramLayers", DIA_STOCK_LAYERS, N_("_Layers\342\200\246"), FIRST_MODIFIER "L", NULL, G_CALLBACK (dialogs_layers_callback) },
  { "View", NULL, N_("_View"), NULL, NULL, NULL },
    { "ViewZoomin", GTK_STOCK_ZOOM_IN, NULL, FIRST_MODIFIER "plus", N_("Zoom in"), G_CALLBACK (view_zoom_in_callback) },
    { "ViewZoomout", GTK_STOCK_ZOOM_OUT, NULL, FIRST_MODIFIER "minus", N_("Zoom out"), G_CALLBACK (view_zoom_out_callback) },
    { "ViewZoom", NULL, N_("_Zoom"), NULL, NULL, NULL },
      { "ViewZoom16000", NULL, N_("1600%"), NULL, NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom8000", NULL, N_("800%"), NULL, NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom4000", NULL, N_("400%"), "<alt>4", NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom2830", NULL, N_("283"), NULL, NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom2000", NULL, N_("200"), "<alt>2", NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom1410", NULL, N_("141"), NULL, NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom1000", GTK_STOCK_ZOOM_100, NULL, "<alt>1", NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom850", NULL, N_("85"), NULL, NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom707", NULL, N_("70.7"), NULL, NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom500", NULL, N_("50"), "<alt>5", NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom354", NULL, N_("35.4"), NULL, NULL, G_CALLBACK (view_zoom_set_callback) },
      { "ViewZoom250", NULL, N_("25"), NULL, NULL, G_CALLBACK (view_zoom_set_callback) },
    /* Show All, Best Fit.  Same as the Gimp, Ctrl+E */
    { "ViewShowall", GTK_STOCK_ZOOM_FIT, NULL, FIRST_MODIFIER "E", N_("Zoom fit"), G_CALLBACK (view_show_all_callback) },

  /* "display_toggle_entries" items go here */

    { "ViewNewview", NULL, N_("New _View"), NULL, NULL, G_CALLBACK (view_new_view_callback) },
    { "ViewCloneview", NULL, N_("C_lone View"), NULL, NULL, G_CALLBACK (view_clone_view_callback) },
    { "ViewRedraw", GTK_STOCK_REFRESH, NULL, NULL, NULL, G_CALLBACK (view_redraw_callback) },

  { "Objects", NULL, N_("_Objects"), NULL, NULL },
    { "ObjectsSendtoback", GTK_STOCK_GOTO_BOTTOM, N_("Send to _Back"), FIRST_MODIFIER "<shift>B", N_("Move selection to the bottom"), G_CALLBACK (objects_place_under_callback) },
    { "ObjectsBringtofront", GTK_STOCK_GOTO_TOP, N_("Bring to _Front"), FIRST_MODIFIER "<shift>F", N_("Move selection to the top"), G_CALLBACK (objects_place_over_callback) },
    { "ObjectsSendbackwards", GTK_STOCK_GO_DOWN, N_("Send Backwards"), NULL, NULL, G_CALLBACK (objects_place_down_callback) },
    { "ObjectsBringforwards", GTK_STOCK_GO_UP, N_("Bring Forwards"), NULL, NULL, G_CALLBACK (objects_place_up_callback) },

    { "ObjectsGroup", DIA_STOCK_GROUP, N_("_Group"), FIRST_MODIFIER "G", N_("Group selected objects"), G_CALLBACK (objects_group_callback) },
    /* deliberately not using Ctrl+U for Ungroup */
    { "ObjectsUngroup", DIA_STOCK_UNGROUP, N_("_Ungroup"), FIRST_MODIFIER "<shift>G", N_("Ungroup selected groups"), G_CALLBACK (objects_ungroup_callback) }, 

    { "ObjectsParent", NULL, N_("_Parent"), FIRST_MODIFIER "K", NULL, G_CALLBACK (objects_parent_callback) },
    { "ObjectsUnparent", NULL, N_("_Unparent"), FIRST_MODIFIER "<shift>K", NULL, G_CALLBACK (objects_unparent_callback) },
    { "ObjectsUnparentchildren", NULL, N_("_Unparent Children"), NULL, NULL, G_CALLBACK (objects_unparent_children_callback) },

    { "ObjectsAlign", NULL, N_("Align"), NULL, NULL, NULL },
      { "ObjectsAlignLeft", GTK_STOCK_JUSTIFY_LEFT, NULL, "<alt><shift>L", NULL, G_CALLBACK (objects_align_h_callback) },
      { "ObjectsAlignCenter", GTK_STOCK_JUSTIFY_CENTER, NULL, "<alt><shift>C", NULL, G_CALLBACK (objects_align_h_callback) },
      { "ObjectsAlignRight", GTK_STOCK_JUSTIFY_RIGHT, NULL, "<alt><shift>R", NULL, G_CALLBACK (objects_align_h_callback) },

      { "ObjectsAlignTop", NULL, N_("Top"), "<alt><shift>T", NULL, G_CALLBACK (objects_align_v_callback) },
      { "ObjectsAlignMiddle", NULL, N_("Middle"), "<alt><shift>M", NULL, G_CALLBACK (objects_align_v_callback) },
      { "ObjectsAlignBottom", NULL, N_("Bottom"), "<alt><shift>B", NULL, G_CALLBACK (objects_align_v_callback) },

      { "ObjectsAlignSpreadouthorizontally", NULL, N_("Spread Out Horizontally"), "<alt><shift>H", NULL, G_CALLBACK (objects_align_h_callback) },
      { "ObjectsAlignSpreadoutvertically", NULL, N_("Spread Out Vertically"), "<alt><shift>V", NULL, G_CALLBACK (objects_align_v_callback) },
      { "ObjectsAlignAdjacent", NULL, N_("Adjacent"), "<alt><shift>A", NULL, G_CALLBACK (objects_align_h_callback) },
      { "ObjectsAlignStacked", NULL, N_("Stacked"), "<alt><shift>S", NULL, G_CALLBACK (objects_align_v_callback) },
      { "ObjectsAlignConnected", NULL, N_("Connected"), "<alt><shift>O", NULL, G_CALLBACK (objects_align_connected_callback) },

      { "ObjectsProperties", GTK_STOCK_PROPERTIES, NULL, "<alt>Return", NULL, G_CALLBACK (dialogs_properties_callback) },

  { "Select", NULL, N_("_Select"), NULL, NULL, NULL },
    { "SelectAll", NULL, N_("All"), FIRST_MODIFIER "A", NULL, G_CALLBACK (select_all_callback) },
    { "SelectNone", NULL, N_("None"), FIRST_MODIFIER "<shift>A", NULL, G_CALLBACK (select_none_callback) },
    { "SelectInvert", NULL, N_("Invert"), FIRST_MODIFIER "I", NULL, G_CALLBACK (select_invert_callback) },

    { "SelectTransitive", NULL, N_("Transitive"), FIRST_MODIFIER "T", NULL, G_CALLBACK (select_transitive_callback) },
    { "SelectConnected", NULL, N_("Connected"), FIRST_MODIFIER "<shift>T", NULL, G_CALLBACK (select_connected_callback) },
    { "SelectSametype", NULL, N_("Same Type"), NULL, NULL, G_CALLBACK (select_same_type_callback) },

    /* display_select_radio_entries go here */

    { "SelectBy", NULL, N_("Select By"), NULL, NULL, NULL },

  /* For placment of the toplevel Layout menu and it's accelerator */
  { "Layout", NULL, N_("L_ayout"), NULL, NULL, NULL },

  { "Dialogs", NULL, N_("D_ialogs"), NULL, NULL, NULL },

  { "Debug", NULL, N_("D_ebug"), NULL, NULL, NULL }
};

/* Standard-Tool entries */
static const GtkActionEntry tool_entries[] = 
{
  { "Tools", NULL, N_("_Tools"), NULL, NULL, NULL },
    { "ToolsModify", NULL, N_("Modify"), TOOL_MODIFIER "N", NULL, NULL },
    { "ToolsMagnify", NULL, N_("Magnify"), TOOL_MODIFIER "M", NULL, NULL },
    { "ToolsTextedit", NULL, N_("Edit Text"), "F2", NULL, NULL },
    { "ToolsScroll", NULL, N_("Scroll"), TOOL_MODIFIER "S", NULL, NULL },
    { "ToolsText", NULL, N_("Text"), TOOL_MODIFIER "T", NULL, NULL },
    { "ToolsBox", NULL, N_("Box"), TOOL_MODIFIER "R", NULL, NULL },
    { "ToolsEllipse", NULL, N_("Ellipse"), TOOL_MODIFIER "E", NULL, NULL },
    { "ToolsPolygon", NULL, N_("Polygon"), TOOL_MODIFIER "P", NULL, NULL },
    { "ToolsBeziergon", NULL, N_("Beziergon"), TOOL_MODIFIER "B", NULL, NULL },

    { "ToolsLine", NULL, N_("Line"), TOOL_MODIFIER "L", NULL, NULL },
    { "ToolsArc", NULL, N_("Arc"), TOOL_MODIFIER "A", NULL, NULL },
    { "ToolsZigzagline", NULL, N_("Zigzagline"), TOOL_MODIFIER "Z", NULL, NULL },
    { "ToolsPolyline", NULL, N_("Polyline"), TOOL_MODIFIER "Y", NULL },
    { "ToolsBezierline", NULL, N_("Bezierline"), TOOL_MODIFIER "C", NULL, NULL },
    { "ToolsOutline", NULL, N_("Outline"), TOOL_MODIFIER "O", NULL, NULL },

    { "ToolsImage", NULL, N_("Image"), TOOL_MODIFIER "I", NULL, NULL },
};

/* Toggle-Actions for diagram window */
static const GtkToggleActionEntry display_toggle_entries[] = 
{
    { "ViewFullscreen", GTK_STOCK_FULLSCREEN, NULL, "F11", NULL, G_CALLBACK (view_fullscreen_callback) },
    { "ViewAntialiased", NULL, N_("_Antialiased"), NULL, NULL, G_CALLBACK (view_aa_callback) },
    { "ViewShowgrid", NULL, N_("Show _Grid"), NULL, NULL, G_CALLBACK (view_visible_grid_callback) },
    { "ViewSnaptogrid", NULL, N_("_Snap to Grid"), NULL, NULL, G_CALLBACK (view_snap_to_grid_callback) },
    { "ViewSnaptoobjects", NULL, N_("Snap to _Objects"), NULL, NULL, G_CALLBACK (view_snap_to_objects_callback) },
    { "ViewShowrulers", NULL, N_("Show _Rulers"), NULL, NULL, G_CALLBACK (view_toggle_rulers_callback)  },
    { "ViewShowscrollbars", NULL, N_("Show Scrollbars"), NULL, N_("Show or hide the toolbar"), G_CALLBACK (view_toggle_scrollbars_callback) },
    { "ViewShowconnectionpoints", NULL, N_("Show _Connection Points"), NULL, NULL, G_CALLBACK (view_show_cx_pts_callback) }
};

/* Radio-Actions for the diagram window's "Select"-Menu */
static const GtkRadioActionEntry display_select_radio_entries[] =
{
  { "SelectReplace", NULL, N_("Replace"), NULL, NULL, SELECT_REPLACE },
  { "SelectUnion", NULL, N_("Union"), NULL, NULL, SELECT_UNION },
  { "SelectIntersection", NULL, N_("Intersection"), NULL, NULL, SELECT_INTERSECTION },
  { "SelectRemove", NULL, N_("Remove"), NULL, NULL, SELECT_REMOVE },
  /* Cannot also be called Invert, duplicate names caused keybinding problems */
  { "SelectInverse", NULL, N_("Inverse"), NULL, NULL, SELECT_INVERT }
};

/* need initialisation? */
static gboolean initialise = TRUE;

/* shared between toolbox and integrated code

 - One ui_manager from toolbox_ui_manager and integrated_ui_manager
 - Two action groups for toolbox and display. In the integrated ui case
   the latter should be disabled (or hidden) if there is no diagram

 */
static GtkUIManager *_ui_manager = NULL;

/* toolbox */
static GtkActionGroup *toolbox_actions = NULL;

static GtkActionGroup *tool_actions = NULL;
GtkActionGroup *recent_actions = NULL;
static GSList *recent_merge_ids = NULL;

/* diagram */
static GtkUIManager *display_ui_manager = NULL;
static GtkActionGroup *display_actions = NULL;
static GtkAccelGroup *display_accels = NULL;
static GtkWidget *display_menubar = NULL;

static gchar*
_dia_translate (const gchar* term, gpointer data)
{
  gchar* trans = (gchar*) term;
  
  if (term && *term) {
    /* first try our own ... */
    trans = dgettext (GETTEXT_PACKAGE, term);
    /* ... than gtk */
    if (term == trans)
      trans = dgettext ("gtk20", term);
#if 0
    /* FIXME: final fallback */
    if (term == trans) { /* FIXME: translation to be updated */
      gchar* kludge = g_strdup_printf ("/%s", term);
      trans = dgettext (GETTEXT_PACKAGE, kludge);
      if (kludge == trans)
	trans = term;
      else
	++trans;
      g_free (kludge);
    }
    if (term == trans)
      trans = g_strdup_printf ("XXX: %s", term);
#endif
  }
  return trans;
}

static void
tool_menu_select(GtkWidget *w, gpointer   data) {
  ToolButtonData *tooldata = (ToolButtonData *) data;

  if (tooldata == NULL) {
    g_warning(_("NULL tooldata in tool_menu_select"));
    return;
  }

  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tooldata->widget),TRUE);
}

static void
load_accels(void)
{
  gchar *accelfilename;
  /* load accelerators and prepare to later save them */
  accelfilename = dia_config_filename("menurc");

  if (accelfilename) {
    gtk_accel_map_load(accelfilename);
    g_free(accelfilename);
  }
}
static gint
save_accels(gpointer data)
{
  gchar *accelfilename;

  accelfilename = dia_config_filename("menurc");
  if (accelfilename) {
    gtk_accel_map_save (accelfilename);
    g_free (accelfilename);
  }
  return TRUE;
}

/**
 * Synchronized the Object snap property button with the display.
 * @param param Display to synchronize to.
 */
void
integrated_ui_toolbar_object_snap_synchronize_to_display(gpointer param)
{
  DDisplay *ddisp = param;
  if (ddisp && ddisp->common_toolbar)
  {
    GtkToggleButton *b = g_object_get_data (G_OBJECT (ddisp->common_toolbar), 
                                            DIA_INTEGRATED_TOOLBAR_OBJECT_SNAP);
    gboolean active = ddisp->mainpoint_magnetism? TRUE : FALSE;
    gtk_toggle_button_set_active (b, active);
  }
}

/**
 * Sets the Object-snap property for the active display.
 * @param b Object snap toggle button.
 * @param not_used
 */
static void
integrated_ui_toolbar_object_snap_toggle(GtkToggleButton *b, gpointer *not_used)
{
  DDisplay *ddisp = ddisplay_active ();
  if (ddisp)
  {
    ddisplay_set_snap_to_objects (ddisp, gtk_toggle_button_get_active (b));
  }
}

/**
 * Synchronizes the Snap-to-grid property button with the display.
 * @param param Display to synchronize to.
 */
void
integrated_ui_toolbar_grid_snap_synchronize_to_display(gpointer param)
{
  DDisplay *ddisp = param;
  if (ddisp && ddisp->common_toolbar)
  {
    GtkToggleButton *b = g_object_get_data (G_OBJECT (ddisp->common_toolbar), 
                                            DIA_INTEGRATED_TOOLBAR_SNAP_GRID);
    gboolean active = ddisp->grid.snap? TRUE : FALSE;
    gtk_toggle_button_set_active (b, active);
  }
}

/**
 * Sets the Snap-to-grid property for the active display.
 * @param b Snap to grid toggle button.
 * @param not_used
 */
static void
integrated_ui_toolbar_grid_snap_toggle(GtkToggleButton *b, gpointer *not_used)
{
  DDisplay *ddisp = ddisplay_active ();
  if (ddisp)
  {
    ddisplay_set_snap_to_grid (ddisp, gtk_toggle_button_get_active (b));
  }
}

/**
 * Sets the zoom text for the toolbar
 * @param toolbar Integrated UI toolbar.
 * @param text Current zoom percentage for the active window 
 */
void integrated_ui_toolbar_set_zoom_text (GtkToolbar *toolbar, const gchar * text)
{
  if (toolbar)
  {
    GtkComboBox *combo_entry = g_object_get_data (G_OBJECT (toolbar), 
                                                  DIA_INTEGRATED_TOOLBAR_ZOOM_COMBO);
    
    if (combo_entry)
    {
        GtkWidget * entry = gtk_bin_get_child (GTK_BIN (combo_entry));

        gtk_entry_set_text (GTK_ENTRY (entry), text);
    }
  }
}

/** 
 * Adds a widget to the toolbar making sure that it doesn't take any excess space, and
 * vertically centers it.
 * @param toolbar The toolbar to add the widget to.
 * @param w       The widget to add to the toolbar.
 */
static void integrated_ui_toolbar_add_custom_item (GtkToolbar *toolbar, GtkWidget *w)
{
    GtkToolItem *tool_item; 
    GtkWidget   *c; /* container */

    tool_item = gtk_tool_item_new ();
    c = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (tool_item), c);
    gtk_box_set_homogeneous (GTK_BOX (c), TRUE);            /* Centers the button */
    gtk_box_pack_start (GTK_BOX (c), w, FALSE, FALSE, 0);
    gtk_toolbar_insert (toolbar, tool_item, -1);
    gtk_widget_show (GTK_WIDGET (tool_item));
    gtk_widget_show (c);
    gtk_widget_show (w);
}

static void
integrated_ui_toolbar_zoom_activate (GtkWidget *item, 
                                     gpointer   user_data)
{
    const gchar *text =  gtk_entry_get_text (GTK_ENTRY (item));
    float        zoom_percent;

    if (sscanf (text, "%f", &zoom_percent) == 1)
    {
        view_zoom_set (10.0 * zoom_percent);
    }
}

/* "DiaZoomCombo" probably could work for both UI cases */
static void
integrated_ui_toolbar_zoom_combo_selection_changed (GtkComboBox *combo,
                                                    gpointer     user_data)
{
    /* 
     * We call gtk_combo_get_get_active() so that typing in the combo entry
     * doesn't get handled as a selection change                           
     */
    if (gtk_combo_box_get_active (combo) != -1)
    {
        float zoom_percent;
        gchar * text;
#if GTK_CHECK_VERSION(2,24,0)
	text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT (combo));
#else
	text = gtk_combo_box_get_active_text (combo);
#endif
        if (sscanf (text, "%f", &zoom_percent) == 1)
        {
            view_zoom_set (zoom_percent * 10.0);
        }
        else if (g_ascii_strcasecmp (text, ZOOM_FIT) == 0)
        {
            view_show_all_callback (NULL);
        }
        
        g_free (text);
    }
}

static guint
ensure_menu_path (GtkUIManager *ui_manager, GtkActionGroup *actions, const gchar *path, gboolean end)
{
  guint id = gtk_ui_manager_new_merge_id (ui_manager);

  if (!gtk_ui_manager_get_widget (ui_manager, path)) {
    gchar *subpath = g_strdup (path);

    if (strrchr (subpath, '/')) {
      const gchar *action_name;
      gchar *sep;

      GtkAction *action;

      sep = strrchr (subpath, '/');
      *sep = '\0'; /* cut subpath */
      action_name = sep + 1;
      
      ensure_menu_path (ui_manager, actions, subpath, FALSE);

      action = gtk_action_new (action_name, sep + 1, NULL, NULL);
      if (!gtk_action_group_get_action (actions, action_name))
        gtk_action_group_add_action (actions, action);
      g_object_unref (G_OBJECT (action));

      gtk_ui_manager_add_ui (ui_manager, id, subpath, 
	                     action_name, action_name,
			     end ? GTK_UI_MANAGER_SEPARATOR : GTK_UI_MANAGER_MENU,
			     FALSE); /* FALSE=add-to-end */
    } else {
      g_warning ("ensure_menu_path() invalid menu path: %s.", subpath ? subpath : "NULL");
    }
    g_free (subpath);
  }
  return id;
}

/**
 * Create the toolbar for the integrated UI
 * @return Main toolbar (GtkToolbar*) for the integrated UI main window
 */
static GtkWidget * 
create_integrated_ui_toolbar (void)
{
  GtkToolbar  *toolbar;
  GtkToolItem *sep;
  GtkWidget   *w;
  GError      *error = NULL;
  gchar *uifile;

  uifile = build_ui_filename ("ui/toolbar-ui.xml");
  if (!gtk_ui_manager_add_ui_from_file (_ui_manager, uifile, &error)) {
    g_warning ("building menus failed: %s", error->message);
    g_error_free (error);
    error = NULL;
    toolbar = GTK_TOOLBAR (gtk_toolbar_new ());
  }
  else {
    toolbar =  GTK_TOOLBAR(gtk_ui_manager_get_widget (_ui_manager, "/Toolbar"));
  }
  g_free (uifile);  

  /* Zoom Combo Box Entry */
#if GTK_CHECK_VERSION(2,24,0)
  w = gtk_combo_box_text_new_with_entry ();
#else
  w = gtk_combo_box_entry_new_text ();
#endif

  g_object_set_data (G_OBJECT (toolbar), 
                     DIA_INTEGRATED_TOOLBAR_ZOOM_COMBO,
                     w);
  integrated_ui_toolbar_add_custom_item (toolbar, w);
 
#if GTK_CHECK_VERSION(2,24,0)
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), ZOOM_FIT);
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("800%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("400%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("300%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("200%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("150%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("100%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("75%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("50%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("25%"));
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (w), _("10%"));
#else
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), ZOOM_FIT);
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("800%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("400%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("300%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("200%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("150%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("100%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("75%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("50%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("25%"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (w), _("10%"));
#endif

  g_signal_connect (G_OBJECT (w), 
                    "changed",
		            G_CALLBACK (integrated_ui_toolbar_zoom_combo_selection_changed), 
                    NULL);
  
  /* Get the combo's GtkEntry child to set the width for the widget */
  w = gtk_bin_get_child (GTK_BIN (w));
  gtk_entry_set_width_chars (GTK_ENTRY (w), 6);
  
  g_signal_connect (G_OBJECT (w), "activate",
		    G_CALLBACK(integrated_ui_toolbar_zoom_activate),
		    NULL);
  
  /* Seperator */
  sep = gtk_separator_tool_item_new ();
  gtk_toolbar_insert (toolbar, sep, -1);
  gtk_widget_show (GTK_WIDGET (sep));

  /* Snap to grid */
  w = dia_toggle_button_new_with_icons (dia_on_grid_icon,
                                        dia_off_grid_icon);
  g_signal_connect (G_OBJECT (w), "toggled",
		   G_CALLBACK (integrated_ui_toolbar_grid_snap_toggle), toolbar);
  gtk_widget_set_tooltip_text (w, _("Toggles snap-to-grid."));
  g_object_set_data (G_OBJECT (toolbar), 
                     DIA_INTEGRATED_TOOLBAR_SNAP_GRID,
                     w);
  integrated_ui_toolbar_add_custom_item (toolbar, w);
 
  /* Object Snapping */
  w = dia_toggle_button_new_with_icons (dia_mainpoints_on_icon,
                                        dia_mainpoints_off_icon);
  g_signal_connect (G_OBJECT (w), "toggled",
		   G_CALLBACK (integrated_ui_toolbar_object_snap_toggle), toolbar);
  gtk_widget_set_tooltip_text (w, _("Toggles object snapping."));
  g_object_set_data (G_OBJECT (toolbar), 
                     DIA_INTEGRATED_TOOLBAR_OBJECT_SNAP,
                     w);
  integrated_ui_toolbar_add_custom_item (toolbar, w);

  sep = gtk_separator_tool_item_new ();
  gtk_toolbar_insert (toolbar, sep, -1);
  gtk_widget_show (GTK_WIDGET (sep));

  return GTK_WIDGET (toolbar);
}

/* 
 * Initialise tool actions.
 * The caller owns the return value.
 */
static GtkActionGroup *
create_or_ref_tool_actions (void)
{
  GtkIconFactory *icon_factory;
  GtkActionGroup *actions;
  GtkAction      *action;
  int           i;

  if (tool_actions)
    return g_object_ref (tool_actions);

  actions = gtk_action_group_new ("tool-actions");
  gtk_action_group_set_translation_domain (actions, NULL);
  gtk_action_group_set_translate_func (actions, _dia_translate, NULL, NULL);

  gtk_action_group_add_actions (actions, tool_entries, 
				G_N_ELEMENTS (tool_entries), NULL);

  icon_factory = gtk_icon_factory_new ();

  for (i = 0; i < num_tools; i++) {
    action = gtk_action_group_get_action (actions, tool_data[i].action_name);
    if (action != NULL) {
      g_signal_connect (G_OBJECT (action), "activate",
			G_CALLBACK (tool_menu_select),
			&tool_data[i].callback_data);

      gtk_action_set_tooltip (action, tool_data[i].tool_desc);

      {
        GdkPixbuf *pb = tool_get_pixbuf (&tool_data[i]);
	GtkIconSet *is = gtk_icon_set_new_from_pixbuf (pb);

	/* not sure if the action name is unique enough */
	gtk_icon_factory_add (icon_factory, tool_data[i].action_name, is);
	gtk_action_set_stock_id (action, tool_data[i].action_name);

	g_object_unref (pb);
      }
    }
    else {
      g_warning ("couldn't find tool menu item %s", tool_data[i].action_name);
    }
  }
  gtk_icon_factory_add_default (icon_factory);
  return actions;
}

/* initialize callbacks from plug-ins */
static void
add_plugin_actions (GtkUIManager *ui_manager, const gchar *base_path)
{
  GtkActionGroup    *actions;
  GtkAction         *action;
  GList             *cblist;
  DiaCallbackFilter *cbf = NULL;
  gchar             *name;
  guint              id;
  static guint       cnt = 0;

  name = g_strdup_printf ("plugin-actions-%d", cnt);
  actions = gtk_action_group_new (name);
  gtk_action_group_set_translation_domain (actions, NULL);
  gtk_action_group_set_translate_func (actions, _dia_translate, NULL, NULL);
  g_free (name);
  name = NULL;
  cnt++;

  gtk_ui_manager_insert_action_group (ui_manager, actions, 5 /* "back" */);
  g_object_unref (actions);

  for (cblist = filter_get_callbacks(); cblist; cblist = cblist->next) {
    gchar *menu_path = NULL;

    cbf = cblist->data;

    if (cbf == NULL) {
      g_warning ("missing DiaCallbackFilter instance");
      continue;
    }

    if (cbf->action == NULL) {
      g_warning ("Plugin '%s': doesn't specify action. Loading failed.", 
		cbf->description);
      continue;
    }

    if (   base_path != NULL 
        && strncmp (cbf->menupath, base_path, strlen (base_path)) != 0) {
      /* hook for wrong base path, skip */
      continue;
    } else if (!base_path) {
      /* only replace what we know */
      if (strncmp (cbf->menupath, DISPLAY_MENU, strlen (DISPLAY_MENU)) == 0)
        menu_path = g_strdup_printf ("%s%s", INTEGRATED_MENU, cbf->menupath + strlen (DISPLAY_MENU));
      else if (strncmp (cbf->menupath, TOOLBOX_MENU, strlen (TOOLBOX_MENU)) == 0)
        menu_path = g_strdup_printf ("%s%s", INTEGRATED_MENU, cbf->menupath + strlen (TOOLBOX_MENU));
    }

    action = gtk_action_new (cbf->action, cbf->description, NULL, NULL);
    g_signal_connect (G_OBJECT (action), "activate", 
		      G_CALLBACK (plugin_callback), (gpointer) cbf);

    gtk_action_group_add_action (actions, action);
    g_object_unref (G_OBJECT (action));

    id = ensure_menu_path (ui_manager, actions, menu_path ? menu_path : cbf->menupath, TRUE);
    gtk_ui_manager_add_ui (ui_manager, id, 
			   menu_path ? menu_path : cbf->menupath, 
			   cbf->description, 
			   cbf->action, 
			   GTK_UI_MANAGER_AUTO, 
			   FALSE);
    g_free (menu_path);
  }
}

static void
_add_stock_icon (GtkIconFactory *factory, const char *name, const guint8 *data, const size_t size)
{
  GdkPixbuf      *pixbuf;
  GtkIconSet     *set;
  GError         *err = NULL;

  pixbuf = gdk_pixbuf_new_from_inline (size, data, FALSE, &err);
  if (err) {
    g_warning ("%s", err->message);
    g_error_free (err);
    err = NULL;
  }
  set = gtk_icon_set_new_from_pixbuf (pixbuf);
  gtk_icon_factory_add (factory, name, set);
  g_object_unref (pixbuf);
  pixbuf = NULL;
}

static void
register_stock_icons (void)
{
  GtkIconFactory *factory;

  factory = gtk_icon_factory_new ();

  _add_stock_icon (factory, DIA_STOCK_GROUP, dia_group_icon, sizeof(dia_group_icon));
  _add_stock_icon (factory, DIA_STOCK_UNGROUP, dia_ungroup_icon, sizeof(dia_ungroup_icon));

  _add_stock_icon (factory, DIA_STOCK_LAYER_ADD, dia_layer_add, sizeof(dia_layer_add));
  _add_stock_icon (factory, DIA_STOCK_LAYER_RENAME, dia_layer_rename, sizeof(dia_layer_rename));
  _add_stock_icon (factory, DIA_STOCK_OBJECTS_LAYER_ABOVE, dia_objects_layer_above, sizeof(dia_objects_layer_above));
  _add_stock_icon (factory, DIA_STOCK_OBJECTS_LAYER_BELOW, dia_objects_layer_below, sizeof(dia_objects_layer_below));
  _add_stock_icon (factory, DIA_STOCK_LAYERS, dia_layers, sizeof(dia_layers));

  gtk_icon_factory_add_default (factory);
  g_object_unref (factory);
  factory = NULL;
}

gchar*
build_ui_filename (const gchar* name)
{
  gchar* uifile;

  if (g_getenv ("DIA_BASE_PATH") != NULL) {
    /* a small hack cause the final destination and the local path differ */
    const gchar* p = strrchr (name, '/');
    if (p != NULL)
      name = p+1;
    uifile = g_build_filename (g_getenv ("DIA_BASE_PATH"), "data", name, NULL);
  } else
    uifile = dia_get_data_directory (name);

  return uifile;
}

/*!
 * Not sure why this service is not provided by GTK+. 
 * We are passing tooltips into the actions (especially recent file menu).
 * But they were not shown without explicit setting on connect.
 */
static void
_ui_manager_connect_proxy (GtkUIManager *manager,
                           GtkAction    *action,
                           GtkWidget    *proxy)
{
  if (GTK_IS_MENU_ITEM (proxy))
    {
      gchar *tooltip;

      g_object_get (action, "tooltip", &tooltip, NULL);

      if (tooltip)
        {
	  gtk_widget_set_tooltip_text (proxy, tooltip);
	  g_free (tooltip);
	}
      else
	{
	  const gchar *name = gtk_action_get_name (action);

	  dia_log_message ("Action '%s' missing tooltip\n", name);
	}
    }
}

static GtkActionGroup *
create_or_ref_display_actions (gboolean include_common)
{
  if (display_actions)
    return g_object_ref (display_actions);
  display_actions = gtk_action_group_new ("display-actions");
  gtk_action_group_set_translation_domain (display_actions, NULL);
  gtk_action_group_set_translate_func (display_actions, _dia_translate, NULL, NULL);
  if (include_common)
    gtk_action_group_add_actions (display_actions, common_entries,
                  G_N_ELEMENTS (common_entries), NULL);
  gtk_action_group_add_actions (display_actions, display_entries, 
                G_N_ELEMENTS (display_entries), NULL);
  gtk_action_group_add_toggle_actions (display_actions, display_toggle_entries,
                G_N_ELEMENTS (display_toggle_entries), 
                NULL);
  gtk_action_group_add_radio_actions (display_actions,
                display_select_radio_entries,
                G_N_ELEMENTS (display_select_radio_entries),
                0, /* SELECT_REPLACE - first radio entry */
                G_CALLBACK (select_style_callback),
                NULL);
  /* the initial reference */
  return display_actions;
}

/* Very minimal fallback menu info for ui-files missing 
 * as well as to register the InvisibleMenu */
static const gchar *ui_info =
"<ui>\n"
"  <popup name=\"InvisibleMenu\">\n"
"    <menu name=\"File\" action=\"File\">\n"
"       <menuitem name=\"FilePrint\" action=\"FilePrint\" />\n"
"       <menuitem name=\"FileQuit\" action=\"FileQuit\" />\n"
"    </menu>\n"
"  </popup>\n"
"</ui>";

static void
_action_start (GtkActionGroup *action_group,
	       GtkAction *action,
	       gpointer user_data)
{
  dia_log_message ("Start '%s'\n", gtk_action_get_name (action));
}
static void
_action_done (GtkActionGroup *action_group,
	       GtkAction *action,
	       gpointer user_data)
{
  dia_log_message ("Done '%s'\n", gtk_action_get_name (action));
}

static void
_setup_global_actions (void)
{
  if (tool_actions)
    return;
  g_return_if_fail (_ui_manager == NULL);
  g_return_if_fail (toolbox_actions == NULL);

  register_stock_icons ();

  /* for the toolbox menu */
  toolbox_actions = gtk_action_group_new ("toolbox-actions");
  gtk_action_group_set_translation_domain (toolbox_actions, NULL);
  gtk_action_group_set_translate_func (toolbox_actions, _dia_translate, NULL, NULL);
  gtk_action_group_add_actions (toolbox_actions, common_entries, 
                G_N_ELEMENTS (common_entries), NULL);
  gtk_action_group_add_actions (toolbox_actions, toolbox_entries, 
                G_N_ELEMENTS (toolbox_entries), NULL);

  _ui_manager = gtk_ui_manager_new ();
  g_signal_connect (G_OBJECT (_ui_manager), 
                    "connect_proxy",
		    G_CALLBACK (_ui_manager_connect_proxy),
		    NULL);
  g_signal_connect (G_OBJECT (_ui_manager), 
                    "pre-activate",
		    G_CALLBACK (_action_start),
		    NULL);
  g_signal_connect (G_OBJECT (_ui_manager), 
                    "post-activate",
		    G_CALLBACK (_action_done),
		    NULL);

  gtk_ui_manager_set_add_tearoffs (_ui_manager, DIA_SHOW_TEAROFFS);
  gtk_ui_manager_insert_action_group (_ui_manager, toolbox_actions, 0);

  tool_actions = create_or_ref_tool_actions ();
}

static void
menus_init(void)
{
  GError *error = NULL;
  gchar  *uifile;

  if (!initialise)
    return;
  
  initialise = FALSE;
  _setup_global_actions ();

  uifile = build_ui_filename ("ui/toolbox-ui.xml");
  if (!gtk_ui_manager_add_ui_from_file (_ui_manager, uifile, &error)) {
    g_warning ("building menus failed: %s", error->message);
    g_error_free (error);
    error = NULL;
  }
  g_free (uifile);

  /* the display menu */
  display_actions = create_or_ref_display_actions (TRUE);

  display_ui_manager = gtk_ui_manager_new ();
  g_signal_connect (G_OBJECT (display_ui_manager), 
                    "connect_proxy",
		    G_CALLBACK (_ui_manager_connect_proxy),
		    NULL);
  gtk_ui_manager_set_add_tearoffs (display_ui_manager, DIA_SHOW_TEAROFFS);
  gtk_ui_manager_insert_action_group (display_ui_manager, display_actions, 0);
  gtk_ui_manager_insert_action_group (display_ui_manager, tool_actions, 0);
  if (!gtk_ui_manager_add_ui_from_string (display_ui_manager, ui_info, -1, &error)) {
    g_warning ("built-in menus failed: %s", error->message);
    g_error_free (error);
    error = NULL;
  }

  uifile = build_ui_filename ("ui/popup-ui.xml");
  /* TODO it would be more elegant if we had only one definition of the 
   * menu hierarchy and merge it into a popup somehow. */
  if (!gtk_ui_manager_add_ui_from_file (display_ui_manager, uifile, &error)) {
    g_warning ("building menus failed: %s", error->message);
    g_error_free (error);
    error = NULL;
  }
  g_free (uifile);

  display_accels = gtk_ui_manager_get_accel_group (display_ui_manager);
  display_menubar = gtk_ui_manager_get_widget (display_ui_manager, DISPLAY_MENU);
  g_assert (display_menubar);

  add_plugin_actions (_ui_manager, TOOLBOX_MENU);
  add_plugin_actions (display_ui_manager, DISPLAY_MENU);
  add_plugin_actions (display_ui_manager, INVISIBLE_MENU);

  /* after creating all menu items */
  load_accels ();
#if GTK_CHECK_VERSION(2,24,0)
  g_print ("TODO: Check accels being saved ...");
#else
  gtk_quit_add(1, save_accels, NULL);
#endif
}

void
menus_get_integrated_ui_menubar (GtkWidget     **menubar,
                                 GtkWidget     **toolbar,
			         GtkAccelGroup **accel_group)
{
  GError *error = NULL;
  gchar  *uifile;

  _setup_global_actions ();
  g_return_if_fail (_ui_manager != NULL);

  /* the integrated ui menu */
  display_actions = create_or_ref_display_actions (FALSE);
  g_return_if_fail (tool_actions != NULL);

  /* maybe better to put this into toolbox_actions? */
  gtk_action_group_add_toggle_actions (display_actions, integrated_ui_view_toggle_entries, 
				       G_N_ELEMENTS (integrated_ui_view_toggle_entries), NULL);

  /* for stand-alone they are per display */
  gtk_ui_manager_insert_action_group (_ui_manager, display_actions, 0);
  tool_actions = create_or_ref_tool_actions ();
  gtk_ui_manager_insert_action_group (_ui_manager, tool_actions, 0);

  uifile = build_ui_filename ("ui/integrated-ui.xml");
  if (!gtk_ui_manager_add_ui_from_file (_ui_manager, uifile, &error)) {
    g_warning ("building integrated ui menus failed: %s", error->message);
    g_error_free (error);
    error = NULL;
  }
  g_free (uifile);
  if (!gtk_ui_manager_add_ui_from_string (_ui_manager, ui_info, -1, &error)) {
    g_warning ("built-in menus failed: %s", error->message);
    g_error_free (error);
    error = NULL;
  }

  add_plugin_actions (_ui_manager, NULL);
  /* after creating all menu items */
  load_accels ();

  if (menubar)
    *menubar = gtk_ui_manager_get_widget (_ui_manager, INTEGRATED_MENU);
  if (toolbar)
    *toolbar = create_integrated_ui_toolbar ();
  if (accel_group)
    *accel_group = gtk_ui_manager_get_accel_group (_ui_manager);
}

void
menus_get_toolbox_menubar (GtkWidget     **menubar,
			   GtkAccelGroup **accel_group)
{
  if (initialise)
    menus_init();

  if (menubar)
    *menubar = gtk_ui_manager_get_widget (_ui_manager, TOOLBOX_MENU);
  if (accel_group)
    *accel_group = gtk_ui_manager_get_accel_group (_ui_manager);
}

GtkWidget * 
menus_get_display_popup (void)
{
  if (initialise)
    menus_init();

  return display_menubar;
}

GtkAccelGroup * 
menus_get_display_accels (void)
{
  /* for integrated-ui the accels are delivered by menus_get_integrated_ui_menubar() */
  g_return_val_if_fail (is_integrated_ui () == FALSE, NULL);

  if (initialise)
    menus_init();

  return display_accels;
}

GtkWidget *
menus_create_display_menubar (GtkUIManager   **ui_manager, 
			      GtkActionGroup **actions)
{
  GtkWidget      *menu_bar;
  GError         *error = NULL;
  gchar          *uifile;

  
  *actions = create_or_ref_display_actions (TRUE);
  tool_actions = create_or_ref_tool_actions (); 

  *ui_manager = gtk_ui_manager_new ();
  gtk_ui_manager_set_add_tearoffs (*ui_manager, DIA_SHOW_TEAROFFS);
  gtk_ui_manager_insert_action_group (*ui_manager, *actions, 0);
  gtk_ui_manager_insert_action_group (*ui_manager, tool_actions, 0);
  g_object_unref (G_OBJECT (tool_actions));
  
  uifile = build_ui_filename ("ui/display-ui.xml");
  if (!gtk_ui_manager_add_ui_from_file (*ui_manager, uifile, &error)) {
    g_warning ("building menus failed: %s", error->message);
    g_error_free (error);
    g_free (uifile);
    return NULL;
  }
  g_free (uifile);

  add_plugin_actions (*ui_manager, DISPLAY_MENU);
  menu_bar = gtk_ui_manager_get_widget (*ui_manager, DISPLAY_MENU);
  return menu_bar;
}

GtkActionGroup *
menus_get_tool_actions (void)
{
  return tool_actions;
}

GtkActionGroup *
menus_get_display_actions (void)
{
  return display_actions;
}

GtkAction *
menus_get_action (const gchar *name)
{
  GtkAction *action;

  action = gtk_action_group_get_action (toolbox_actions, name);
  if (!action)
    action = gtk_action_group_get_action (display_actions, name);
  if (!action)
    action = gtk_action_group_get_action (tool_actions, name);
  if (!action) {
    GList *groups, *list;

    /* search everything there is, could probably replace the above */
    if (display_ui_manager) /* classic mode */
      groups = gtk_ui_manager_get_action_groups (display_ui_manager);
    else
      groups = gtk_ui_manager_get_action_groups (_ui_manager);
    for (list = groups; list != NULL; list = list->next) {
      action = gtk_action_group_get_action (GTK_ACTION_GROUP (list->data), name);
      if (action)
	break;
    }
  }

  return action;
}

GtkWidget *
menus_get_widget (const gchar *name)
{
  g_return_val_if_fail (_ui_manager != NULL, NULL);

  return gtk_ui_manager_get_widget (_ui_manager, name);
}

static int
cmp_action_names (const void *a, const void *b)
{
  return strcmp (gtk_action_get_name (GTK_ACTION (a)), gtk_action_get_name (GTK_ACTION (b)));
}

void
menus_set_recent (GtkActionGroup *actions)
{
  GList *list;
  guint id;
  const char   *recent_path;

  if (is_integrated_ui ())
    recent_path = INTEGRATED_MENU "/File/FileRecentEnd";
  else
    recent_path = TOOLBOX_MENU "/File/FileRecentEnd";

  if (recent_actions) {
    menus_clear_recent ();
  }

  list = gtk_action_group_list_actions (actions);
  g_return_if_fail (list);

  /* sort it by the action name to preserve out order */
  list = g_list_sort (list, cmp_action_names);

  recent_actions = actions;
  g_object_ref (G_OBJECT (recent_actions));
  gtk_ui_manager_insert_action_group (_ui_manager, 
                    recent_actions, 
                    10 /* insert at back */ );

  do {
    const gchar* aname = gtk_action_get_name (GTK_ACTION (list->data));

    id = gtk_ui_manager_new_merge_id (_ui_manager);
    recent_merge_ids = g_slist_prepend (recent_merge_ids, (gpointer)(intptr_t) id);

    gtk_ui_manager_add_ui (_ui_manager, id, 
                 recent_path, 
                 aname, 
                 aname, 
                 GTK_UI_MANAGER_AUTO, 
                 TRUE);

  } while (NULL != (list = list->next));
}

void
menus_clear_recent (void)
{
  GSList *id;

  if (recent_merge_ids) {
    id = recent_merge_ids;
    do {
      gtk_ui_manager_remove_ui (_ui_manager, (guint) id->data);
      
    } while (NULL != (id = id->next));
    
    g_slist_free (recent_merge_ids);
    recent_merge_ids = NULL;
  }

  if (recent_actions) {
    gtk_ui_manager_remove_action_group (_ui_manager, recent_actions);
    g_object_unref (G_OBJECT (recent_actions));
    recent_actions = NULL;
  }
}

static void
plugin_callback (GtkWidget *widget, gpointer data)
{
  DiaCallbackFilter *cbf = data;

  /* check if the callback filter is still available */
  if (!g_list_find (filter_get_callbacks (), cbf)) {
    message_error (_("The function is not available anymore."));
    return;
  }
  /* and finally invoke it */
  if (cbf->callback) {
    DDisplay *ddisp = NULL;
    DiagramData* diadata = NULL;
    ObjectChange *change;
    /* stuff from the toolbox menu should never get a diagram to modify */
    if (strncmp (cbf->menupath, TOOLBOX_MENU, strlen (TOOLBOX_MENU)) != 0) {
      ddisp = ddisplay_active();
      diadata = ddisp ? ddisp->diagram->data : NULL;
    }
    change = cbf->callback (diadata, ddisp ? ddisp->diagram->filename : NULL, 0, cbf->user_data);
    if (change != NULL) {
      if (ddisp) {
        undo_object_change(ddisp->diagram, NULL, change);
	/*
	 * - can not call object_add_update() w/o object
	 * - could call object_add_updates_list() with the selected objects,
	 *   but that would just be an educated guess (layout working on selection)
	 */
	diagram_add_update_all(ddisp->diagram);
        diagram_modified(ddisp->diagram);
        diagram_update_extents(ddisp->diagram);
        undo_set_transactionpoint(ddisp->diagram->undo);
      } else { /* no diagram to keep the change, throw it away */
        if (change->free)
          change->free(change);
        g_free(change);
      }   
    }
  }
}