File: commands.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 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 (1560 lines) | stat: -rw-r--r-- 40,148 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
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson *
 * 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.
 */

#include <config.h>

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <math.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtk.h>

#ifdef G_OS_WIN32
#include <io.h>
#endif

#include <textedit.h>
#include <focus.h>
#include "confirm.h"
#include "dia-application.h"
#include "menus.h"

/** Functions called on menu selects.
 *  Note that GTK (at least up to 2.12) doesn't disable the keyboard shortcuts
 *  when the menu is made insensitive, so we have to check the constrains again
 *  in the functions.
 */
#ifdef G_OS_WIN32
/*
 * Instead of polluting the Dia namespace with windoze headers, declare the
 * required prototype here. This is bad style, but not as bad as namespace
 * clashes to be resolved without C++   --hb
 */
long __stdcall
ShellExecuteA (long        hwnd,
               const char* lpOperation,
               const char* lpFile,
               const char* lpParameters,
               const char* lpDirectory,
               int         nShowCmd);
#endif

#include "intl.h"
#include "commands.h"
#include "app_procs.h"
#include "diagram.h"
#include "display.h"
#include "object_ops.h"
#include "cut_n_paste.h"
#include "interface.h"
#include "load_save.h"
#include "utils.h"
#include "message.h"
#include "grid.h"
#include "properties-dialog.h"
#include "propinternals.h"
#include "preferences.h"
#include "layer_dialog.h"
#include "connectionpoint_ops.h"
#include "undo.h"
#include "pagesetup.h"
#include "text.h"
#include "dia_dirs.h"
#include "focus.h"
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include "lib/properties.h"
#include "lib/parent.h"
#include "dia-props.h"
#include "authors.h"                /* master contributors data */
#include "object.h"

void 
file_quit_callback (GtkAction *action)
{
  app_exit();
}

void
file_pagesetup_callback (GtkAction *action)
{
  Diagram *dia;

  dia = ddisplay_active_diagram();
  if (!dia) return;
  create_page_setup_dlg(dia);
}

void
file_print_callback (GtkAction *_action)
{
  Diagram *dia;
  DDisplay *ddisp;
  GtkAction *action;

  dia = ddisplay_active_diagram();
  if (!dia) return;
  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  action = menus_get_action ("FilePrintGTK");
  if (!action)
    action = menus_get_action ("FilePrintGDI");
  if (!action)
    action = menus_get_action ("FilePrintPS");

  if (action) {
    if (confirm_export_size (dia, GTK_WINDOW(ddisp->shell), CONFIRM_PRINT|CONFIRM_PAGES))
      gtk_action_activate (action);
  } else {
    message_error (_("No print plugin found!"));
  }
}

void
file_close_callback (GtkAction *action)
{
  /* some people use tear-off menus and insist to close non existing displays */
  if (ddisplay_active())
    ddisplay_close(ddisplay_active());
}

void
file_new_callback (GtkAction *action)
{
  Diagram *dia;
  static int untitled_nr = 1;
  gchar *name, *filename;

  name = g_strdup_printf(_("Diagram%d.dia"), untitled_nr++);
  filename = g_filename_from_utf8(name, -1, NULL, NULL, NULL);
  dia = new_diagram(filename);
  new_display(dia);
  dia_diagram_add (dia); /* notify DiagramTree etc. */
  g_free (name);
  g_free (filename);
}

void
file_preferences_callback (GtkAction *action)
{
  prefs_show();
}



/* Signal handler for getting the clipboard contents */
/* Note that the clipboard is for M$-style cut/copy/paste copying, while
   the selection is for Unix-style mark-and-copy.  We can't really do
   mark-and-copy.
*/

static void
insert_text(DDisplay *ddisp, Focus *focus, const gchar *text)
{
  ObjectChange *change = NULL;
  int modified = FALSE, any_modified = FALSE;
  DiaObject *obj = focus_get_object(focus);

  while (text != NULL) {
    gchar *next_line = g_utf8_strchr(text, -1, '\n');
    if (next_line != text) {
      gint len = g_utf8_strlen(text, (next_line-text));
      modified = (*focus->key_event)(focus, 0, GDK_A, text, len, &change);
    }
    if (next_line != NULL) {
      modified = (*focus->key_event)(focus, 0, GDK_Return, "\n", 1, &change);
      text = g_utf8_next_char(next_line);
    } else {
      text = NULL;
    }
    { /* Make sure object updates its data: */
      Point p = obj->position;
      (obj->ops->move)(obj,&p);  }

    /* Perhaps this can be improved */
    object_add_updates(obj, ddisp->diagram);
    
    if (modified && (change != NULL)) {
      undo_object_change(ddisp->diagram, obj, change);
      any_modified = TRUE;
    }
    
    diagram_flush(ddisp->diagram);
  }

  if (any_modified) {
    diagram_modified(ddisp->diagram);
    diagram_update_extents(ddisp->diagram);
    undo_set_transactionpoint(ddisp->diagram->undo);
  }
}


static void
received_clipboard_text_handler(GtkClipboard *clipboard, 
			        const gchar *text,
			        gpointer data)
{
  DDisplay *ddisp = (DDisplay *)data;
  Focus *focus = get_active_focus((DiagramData *) ddisp->diagram);
  
  if (text == NULL) return;

  if ((focus == NULL) || (!focus->has_focus)) return;

  if (!g_utf8_validate(text, -1, NULL)) {
    message_error("Not valid UTF8");
    return;
  }

  insert_text(ddisp, focus, text);
}

/*
 * Callback for gtk_clipboard_request_image
 */
static void
received_clipboard_image_handler(GtkClipboard *clipboard, 
			        GdkPixbuf *pixbuf,
			        gpointer data)
{
  DDisplay *ddisp = (DDisplay *)data;
  Diagram  *dia = ddisp->diagram;
  GList *list = dia->data->selected;
  ObjectChange *change = NULL;

  if (!pixbuf) {
    message_error (_("No image from Clipboard to paste."));
    return;
  }

  while (list) {
    DiaObject *obj = (DiaObject *)list->data;

    /* size before ... */
    object_add_updates (obj, dia);
    change = dia_object_set_pixbuf (obj, pixbuf);
    if (change) {
      undo_object_change(dia, obj, change);
      /* ... and after the change */
      object_add_updates (obj, dia);
      diagram_modified (dia);
      diagram_flush (dia);
      break;
    }
    list = g_list_next (list);
  }

  if (!change) {
    Point pt;
    DiaObjectType *type;
    Handle *handle1;
    Handle *handle2;
    DiaObject *obj;

    pt = ddisplay_get_clicked_position(ddisp);
    snap_to_grid(ddisp, &pt.x, &pt.y);

    if (   ((type = object_get_type ("Standard - Image")) != NULL)
        && ((obj = dia_object_default_create (type, &pt, 
					     type->default_user_data,
					     &handle1, &handle2)) != NULL)) {
      /* as above, transfer the data */
      change = dia_object_set_pixbuf (obj, pixbuf);
      if (change) { /* ... but drop undo info */
	change->free (change);
	g_free (change);
      }
      /* allow undo of the whole thing */
      undo_insert_objects(dia, g_list_prepend(NULL, obj), 1); 

      diagram_add_object (dia, obj);
      diagram_select(dia, obj);
      object_add_updates(obj, dia);

      ddisplay_do_update_menu_sensitivity(ddisp);
      diagram_flush(dia);
    } else {
      message_warning (_("No selected object can take an image."));
    }
  }
  /* although freed above it is still the indicator of diagram modification */
  if (change) {
    diagram_update_extents(dia);
    undo_set_transactionpoint(dia->undo);
    diagram_modified(dia);
  }
}

static void
received_clipboard_content_handler (GtkClipboard     *clipboard,
				    GtkSelectionData *selection_data,
				    gpointer          user_data)
{
  DDisplay *ddisp = (DDisplay *)user_data;
  GdkAtom type_atom;
  gchar *type_name;
  gint len;

  if ((len = gtk_selection_data_get_length (selection_data)) > 0) {
    const guchar *data = gtk_selection_data_get_data (selection_data);
    type_atom = gtk_selection_data_get_data_type (selection_data);
    type_name = gdk_atom_name (type_atom);
    if (type_name && (   strcmp (type_name, "image/svg") == 0
		      || strcmp (type_name, "image/svg+xml") == 0
		      || strcmp (type_name, "UTF8_STRING") == 0)) {
      DiaImportFilter *ifilter = filter_import_get_by_name ("dia-svg");
      DiaContext *ctx = dia_context_new (_("Clipboard Paste"));

      if (ifilter->import_mem_func) {
	Change *change = undo_import_change_setup (ddisp->diagram);

	if (!ifilter->import_mem_func (data, len, DIA_DIAGRAM_DATA (ddisp->diagram),
				      ctx, ifilter->user_data)) {
	  /* might become more right some day ;) */
	  dia_context_add_message (ctx, _("Failed to import '%s' as SVG."), type_name);
	  dia_context_release (ctx);
	}
	if (undo_import_change_done (ddisp->diagram, change)) {
	  undo_set_transactionpoint(ddisp->diagram->undo);
	  diagram_modified(ddisp->diagram);
	  diagram_flush(ddisp->diagram);
	}
      }
    } else {
      /* fallback to pixbuf loader */
      GdkPixbuf *pixbuf = gtk_selection_data_get_pixbuf (selection_data);
      if (pixbuf) {
	received_clipboard_image_handler (clipboard, pixbuf, ddisp);
	g_object_unref (pixbuf);
      } else {
        message_error (_("Paste failed: %s"), type_name);
      }
    }
    dia_log_message ("Content is %s (size=%d)", type_name, len);
    g_free (type_name);
  }
}

void
edit_paste_image_callback (GtkAction *action)
{
  GtkClipboard *clipboard = gtk_clipboard_get(GDK_NONE);
  DDisplay *ddisp;
  GdkAtom *targets;
  gint n_targets;
  gboolean done = FALSE;

  ddisp = ddisplay_active();
  if (!ddisp) return;

  if (gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets)) {
    int i;
    for (i = 0; i < n_targets; ++i) {
      gchar *aname = gdk_atom_name (targets[i]);
      if (   strcmp (aname, "image/svg") == 0
	  || strcmp (aname, "image/svg+xml") == 0
	  || strcmp (aname, "UTF8_STRING") == 0) {
	gtk_clipboard_request_contents (clipboard, targets[i],
					received_clipboard_content_handler, ddisp);
	done = TRUE;
      }
      dia_log_message ("clipboard-targets %d: %s", i, aname);
      g_free (aname);
      if (done)
	break;
    }
    if (!done)
      gtk_clipboard_request_image (clipboard, received_clipboard_image_handler, ddisp);
    g_free (targets);
  }
}

static PropDescription text_prop_singleton_desc[] = {
    { "text", PROP_TYPE_TEXT },
    PROP_DESC_END};

static void 
make_text_prop_singleton(GPtrArray **props, TextProperty **prop) 
{
  *props = prop_list_from_descs(text_prop_singleton_desc,pdtpp_true);
  g_assert((*props)->len == 1);

  *prop = g_ptr_array_index((*props),0);
  g_free((*prop)->text_data);
  (*prop)->text_data = NULL;
}

static GtkTargetEntry target_entries[] = {
  { "image/svg", GTK_TARGET_OTHER_APP, 1 },
  { "image/svg+xml", GTK_TARGET_OTHER_APP, 2 },
  { "image/png", GTK_TARGET_OTHER_APP, 3 },
  { "image/bmp", GTK_TARGET_OTHER_APP, 4 },
  { "image/tiff", GTK_TARGET_OTHER_APP, 5 },
#ifdef G_OS_WIN32
  /* this is not working on win32 either, maybe we need to register it with
   * CF_ENHMETAFILE in Gtk+? Change order? Direct use of SetClipboardData()?
   */
  { "image/emf", GTK_TARGET_OTHER_APP, 6 },
  { "image/wmf", GTK_TARGET_OTHER_APP, 7 },
#endif
};

/** This gets called by Gtk+ if some other application is asking
 * for the clipboard content.
 */
static void
_clipboard_get_data_callback (GtkClipboard     *clipboard,
			      GtkSelectionData *selection_data,
			      guint             info,
			      gpointer          owner_or_user_data)
{
  DiaContext *ctx = dia_context_new (_("Clipboard Copy"));
  DiagramData *dia = owner_or_user_data; /* todo: check it's still valid */
  const gchar *ext = strchr (target_entries[info-1].target, '/')+1;
  gchar *tmplate;
  gchar *outfname = NULL;
  GError *error = NULL;
  DiaExportFilter *ef = NULL;
  int fd;

  /* Although asked for bmp, use png here because of potentially better renderer
   * Dropping 'bmp' in target would exclude many win32 programs, but gtk+ can
   * convert from png on demand ... */
  if (strcmp (ext, "bmp") == 0) {
    tmplate = g_strdup ("dia-cb-XXXXXX.png"); 
  } else if (strcmp (ext, "tiff") == 0) {
    /* pixbuf on OS X offers qtif and qif - both look like a mistake to me ;) */
    tmplate = g_strdup ("dia-cb-XXXXXX.png");
  } else if (g_str_has_suffix (ext, "+xml")) {
    gchar *ext2 = g_strndup (ext, strlen (ext) - 4);
    tmplate = g_strdup_printf ("dia-cb-XXXXXX.%s", ext2);
    g_free (ext2);
  } else {
    tmplate = g_strdup_printf ("dia-cb-XXXXXX.%s", ext);
  }

  if ((fd = g_file_open_tmp (tmplate, &outfname, &error)) != -1) {
    ef = filter_guess_export_filter (outfname);
    close (fd);
  } else {
    g_warning ("%s", error->message);
    g_error_free (error);
    error = NULL;
  }
  g_free (tmplate);

  if (ef) {
    /* for png use alpha-rendering if available */
    if (strcmp(ext, "png") == 0 && filter_export_get_by_name ("cairo-alpha-png") != NULL)
      ef = filter_export_get_by_name ("cairo-alpha-png");
    dia_context_set_filename (ctx, outfname);
    ef->export_func(DIA_DIAGRAM_DATA(dia), ctx,
		    outfname, "clipboard-copy", ef->user_data);
    /* If we have a vector format, don't convert it to pixbuf. 
     * Or even better: only use pixbuf transport when asked
     * for 'OS native bitmaps' BMP (win32), TIFF(osx), ...?
     */
    if (strcmp (ext, "bmp") == 0 || strcmp (ext, "tiff") == 0) {
      GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(outfname, &error);
      if (pixbuf) {
	gtk_selection_data_set_pixbuf (selection_data, pixbuf);
	g_object_unref (pixbuf);
      }
    } else {
      struct stat st;
      FILE *f;
      
      if (   g_stat (outfname, &st) == 0 
	  && ((f = g_fopen (outfname, "rb")) != NULL)) {
	gchar *buf = g_try_malloc (st.st_size);

	if (buf) {
	  if (fread (buf, 1, st.st_size, f) == st.st_size)
	    gtk_selection_data_set (selection_data,
	                            gdk_atom_intern_static_string (target_entries[info-1].target), 8,
	                            (guint8 *)buf, st.st_size);
	}
	g_free (buf);
	fclose (f);
	g_unlink (outfname);
      }
    }
  }
  if (error) {
    dia_context_add_message (ctx, "%s", error->message);
    g_error_free (error);
  }
  dia_context_release (ctx);
}

/** GtkClipboardClearFunc */
static void
_clipboard_clear_data_callback (GtkClipboard *clipboard,
                                gpointer      owner_or_user_data)
{
  DiagramData *dia = owner_or_user_data; /* todo: check it's still valid */

  if (dia)
    g_object_unref (dia);
}

void
edit_copy_callback (GtkAction *action)
{
  GList *copy_list;
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  if (textedit_mode(ddisp)) {
    Focus *focus = get_active_focus((DiagramData *) ddisp->diagram);
    DiaObject *obj = focus_get_object(focus);
    GPtrArray *textprops;
    TextProperty *prop;
    
    if (obj->ops->get_props == NULL) 
      return;
    
    make_text_prop_singleton(&textprops,&prop);
    /* Get the first text property */
    obj->ops->get_props(obj, textprops);
    
    /* GTK docs claim the selection clipboard is ignored on Win32.
     * The "clipboard" clipboard is (was) mostly ignored in Unix.
     * Nowadays the notation of one system global clipboard is common
     * for many programs on Linux, too.
     */
#ifndef GDK_WINDOWING_X11
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_NONE),
			   prop->text_data, -1);
#else
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
			   prop->text_data, -1);
#endif
    prop_list_free(textprops);
  } else {
    /* create a diagram of currently selected to the clipboard - it's rendered on request */
    DiagramData *data = diagram_data_clone_selected (ddisp->diagram->data);

    /* arbitrary scaling from the display, deliberately ignoring
     * the paper scaling, like the display code does
     */
    data->paper.scaling = (ddisp->zoom_factor / 20.0);

    gtk_clipboard_set_with_data (gtk_clipboard_get(GDK_NONE),
                                 target_entries, G_N_ELEMENTS (target_entries),
				 _clipboard_get_data_callback,
				 _clipboard_clear_data_callback,
				 data);

    /* just internal copy of the selected objects */
    copy_list = parent_list_affected(diagram_get_sorted_selected(ddisp->diagram));
    
    cnp_store_objects(object_copy_list(copy_list), 1);
    g_list_free(copy_list);
    
    ddisplay_do_update_menu_sensitivity(ddisp);
  }
}

void
edit_cut_callback (GtkAction *action)
{
  GList *cut_list;
  DDisplay *ddisp;
  Change *change;

  ddisp = ddisplay_active();
  if (!ddisp) return;

  if (textedit_mode(ddisp)) {
  } else {
    diagram_selected_break_external(ddisp->diagram);

    cut_list = parent_list_affected(diagram_get_sorted_selected(ddisp->diagram));

    cnp_store_objects(object_copy_list(cut_list), 0);

    change = undo_delete_objects_children(ddisp->diagram, cut_list);
    (change->apply)(change, ddisp->diagram);
  
    ddisplay_do_update_menu_sensitivity(ddisp);
    diagram_flush(ddisp->diagram);

    diagram_modified(ddisp->diagram);
    diagram_update_extents(ddisp->diagram);
    undo_set_transactionpoint(ddisp->diagram->undo);
  }
}

void
edit_paste_callback (GtkAction *action)
{
  GList *paste_list;
  DDisplay *ddisp;
  Point paste_corner;
  Point delta;
  Change *change;
  int generation = 0;
  
  ddisp = ddisplay_active();
  if (!ddisp) return;
  if (textedit_mode(ddisp)) {
#ifndef GDK_WINDOWING_X11
    gtk_clipboard_request_text(gtk_clipboard_get(GDK_NONE), 
			       received_clipboard_text_handler, ddisp);
#else
    gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), 
			       received_clipboard_text_handler, ddisp);
#endif
  } else {
    if (!cnp_exist_stored_objects()) {
      message_warning(_("No existing object to paste.\n"));
      return;
    }
  
    paste_list = cnp_get_stored_objects(&generation); /* Gets a copy */

    paste_corner = object_list_corner(paste_list);
  
    delta.x = ddisp->visible.left - paste_corner.x;
    delta.y = ddisp->visible.top - paste_corner.y;

    /* Move down some 10% of the visible area. */
    delta.x += (ddisp->visible.right - ddisp->visible.left) * 0.1 * generation;
    delta.y += (ddisp->visible.bottom - ddisp->visible.top) * 0.1 * generation;

    if (generation)
      object_list_move_delta(paste_list, &delta);

    change = undo_insert_objects(ddisp->diagram, paste_list, 0);
    (change->apply)(change, ddisp->diagram);

    diagram_modified(ddisp->diagram);
    undo_set_transactionpoint(ddisp->diagram->undo);
  
    diagram_remove_all_selected(ddisp->diagram, TRUE);
    diagram_select_list(ddisp->diagram, paste_list);

    diagram_update_extents(ddisp->diagram);
    diagram_flush(ddisp->diagram);
  }
}

/*
 * ALAN: Paste should probably paste to different position, feels
 * wrong somehow.  ALAN: The offset should increase a little each time
 * if you paste/duplicate several times in a row, because it is
 * clearer what is happening than if you were to piling them all in
 * one place.
 *
 * completely untested, basically it is copy+paste munged together
 */
void
edit_duplicate_callback (GtkAction *action)
{ 
  GList *duplicate_list;
  DDisplay *ddisp;
  Point delta;
  Change *change;

  ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;
  duplicate_list = object_copy_list(diagram_get_sorted_selected(ddisp->diagram));
  
  /* Move down some 10% of the visible area. */
  delta.x = (ddisp->visible.right - ddisp->visible.left)*0.05;
  delta.y = (ddisp->visible.bottom - ddisp->visible.top)*0.05;

  object_list_move_delta(duplicate_list, &delta);

  change = undo_insert_objects(ddisp->diagram, duplicate_list, 0);
  (change->apply)(change, ddisp->diagram);

  diagram_modified(ddisp->diagram);
  undo_set_transactionpoint(ddisp->diagram->undo);
  
  diagram_remove_all_selected(ddisp->diagram, TRUE);
  diagram_select_list(ddisp->diagram, duplicate_list);

  diagram_flush(ddisp->diagram);
  
  ddisplay_do_update_menu_sensitivity(ddisp);
}

void
objects_move_up_layer(GtkAction *action)
{
  DDisplay *ddisp = ddisplay_active();
  GList *selected_list;
  Change *change;

  if (!ddisp || textedit_mode(ddisp)) return;
  selected_list = diagram_get_sorted_selected(ddisp->diagram);

  change = undo_move_object_other_layer(ddisp->diagram, selected_list, TRUE);
  
  (change->apply)(change, ddisp->diagram);

  diagram_modified(ddisp->diagram);
  undo_set_transactionpoint(ddisp->diagram->undo);
  
  diagram_flush(ddisp->diagram);
  
  ddisplay_do_update_menu_sensitivity(ddisp);
}

 void
objects_move_down_layer(GtkAction *action)
{
  DDisplay *ddisp = ddisplay_active();
  GList *selected_list;
  Change *change;

  if (!ddisp || textedit_mode(ddisp)) return;
  selected_list = diagram_get_sorted_selected(ddisp->diagram);

  /* Must check if move is legal here */

  change = undo_move_object_other_layer(ddisp->diagram, selected_list, FALSE);
  
  (change->apply)(change, ddisp->diagram);

  diagram_modified(ddisp->diagram);
  undo_set_transactionpoint(ddisp->diagram->undo);
  
  diagram_flush(ddisp->diagram);
  
  ddisplay_do_update_menu_sensitivity(ddisp);
}

void
edit_copy_text_callback (GtkAction *action)
{
  Focus *focus;
  DDisplay *ddisp = ddisplay_active();
  DiaObject *obj;
  GPtrArray *textprops;
  TextProperty *prop;

  if (ddisp == NULL) return;

  focus = get_active_focus((DiagramData *) ddisp->diagram);

  if ((focus == NULL) || (!focus->has_focus)) return;

  obj = focus_get_object(focus);

  if (obj->ops->get_props == NULL) 
    return;

  make_text_prop_singleton(&textprops,&prop);
  /* Get the first text property */
  obj->ops->get_props(obj, textprops);
  
  /* GTK docs claim the selection clipboard is ignored on Win32.
   * The "clipboard" clipboard is mostly ignored in Unix
   */
#ifndef GDK_WINDOWING_X11
  gtk_clipboard_set_text(gtk_clipboard_get(GDK_NONE),
			 prop->text_data, -1);
#else
  gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
			 prop->text_data, -1);
#endif
  prop_list_free(textprops);
}

void
edit_cut_text_callback (GtkAction *action)
{
  Focus *focus;
  DDisplay *ddisp;
  DiaObject *obj;
  GPtrArray *textprops;
  TextProperty *prop;
  ObjectChange *change;

  ddisp = ddisplay_active();
  if (!ddisp) return;

  focus = get_active_focus((DiagramData *) ddisp->diagram);
  if ((focus == NULL) || (!focus->has_focus)) return;

  obj = focus_get_object(focus);

  if (obj->ops->get_props == NULL) 
    return;

  make_text_prop_singleton(&textprops,&prop);
  /* Get the first text property */
  obj->ops->get_props(obj, textprops);
  
  /* GTK docs claim the selection clipboard is ignored on Win32.
   * The "clipboard" clipboard is mostly ignored in Unix
   */
#ifndef GDK_WINDOWING_X11
  gtk_clipboard_set_text(gtk_clipboard_get(GDK_NONE),
			 prop->text_data, -1);
#else
  gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
			 prop->text_data, -1);
#endif

  prop_list_free(textprops);

  if (text_delete_all(focus->text, &change, obj)) { 
    object_add_updates(obj, ddisp->diagram);
    undo_object_change(ddisp->diagram, obj, change);
    undo_set_transactionpoint(ddisp->diagram->undo);
    diagram_modified(ddisp->diagram);
    diagram_flush(ddisp->diagram);
  }
}

void
edit_paste_text_callback (GtkAction *action)
{
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;

#ifndef GDK_WINDOWING_X11
  gtk_clipboard_request_text(gtk_clipboard_get(GDK_NONE), 
			     received_clipboard_text_handler, ddisp);
#else
  gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), 
			     received_clipboard_text_handler, ddisp);
#endif
}

void
edit_delete_callback (GtkAction *action)
{
  GList *delete_list;
  DDisplay *ddisp;

  /* Avoid crashing while moving or resizing and deleting ... */
  if (gdk_pointer_is_grabbed ()) {
    gdk_beep ();    /* ... no matter how much sense it makes. */
    return;
  }

  ddisp = ddisplay_active();
  if (!ddisp) return;
  if (textedit_mode(ddisp)) {
    ObjectChange *change = NULL;
    Focus *focus = get_active_focus((DiagramData *) ddisp->diagram);
    if (!text_delete_key_handler(focus, &change)) {
      return;
    }
    object_add_updates(focus->obj, ddisp->diagram);    
  } else {
    Change *change = NULL;
    diagram_selected_break_external(ddisp->diagram);
    
    delete_list = diagram_get_sorted_selected(ddisp->diagram);
    change = undo_delete_objects_children(ddisp->diagram, delete_list);
    g_list_free(delete_list);
    (change->apply)(change, ddisp->diagram);
  }
  diagram_modified(ddisp->diagram);
  diagram_update_extents(ddisp->diagram);
  
  ddisplay_do_update_menu_sensitivity(ddisp);
  diagram_flush(ddisp->diagram);
  
  undo_set_transactionpoint(ddisp->diagram->undo);
} 

void
edit_undo_callback (GtkAction *action)
{
  Diagram *dia;
  
  dia = ddisplay_active_diagram();
  if (!dia) return;

/* Handle text undo edit here! */
  undo_revert_to_last_tp(dia->undo);
  diagram_modified(dia);
  diagram_update_extents(dia);

  diagram_flush(dia);
} 

void
edit_redo_callback (GtkAction *action)
{
  Diagram *dia;
  
/* Handle text undo edit here! */
  dia = ddisplay_active_diagram();
  if (!dia) return;

  undo_apply_to_next_tp(dia->undo);
  diagram_modified(dia);
  diagram_update_extents(dia);

  diagram_flush(dia);
} 

void
help_manual_callback (GtkAction *action)
{
  char *helpdir, *helpindex = NULL, *command;
  guint bestscore = G_MAXINT;
  GDir *dp;
  const char *dentry;
  GError *error = NULL;
  GdkScreen *screen;
  DDisplay *ddisp;
  ddisp = ddisplay_active();
  screen = ddisp ? gtk_widget_get_screen (GTK_WIDGET(ddisp->shell))
         : gdk_screen_get_default ();
  if (gtk_show_uri(screen, "ghelp:dia", gtk_get_current_event_time (), NULL)) {
    return;
  }

  helpdir = g_strdup("/usr/share/doc/dia/html");
  if (!helpdir) {
    message_warning(_("Could not find help directory"));
    return;
  }

  /* search through helpdir for the helpfile that matches the user's locale */
  dp = g_dir_open (helpdir, 0, &error);
  if (!dp) {
    message_warning(_("Could not open help directory:\n%s"),
                    error->message);
    g_error_free (error);
    return;
  }
  
  while ((dentry = g_dir_read_name(dp)) != NULL) {
    guint score;

    score = intl_score_locale(dentry);
    if (score < bestscore) {
      if (helpindex)
	g_free(helpindex);
#ifdef G_OS_WIN32
      /* use HTML Help on win32 if available */
      helpindex = g_strconcat(helpdir, G_DIR_SEPARATOR_S, dentry,
			      G_DIR_SEPARATOR_S "dia-manual.chm", NULL);
      if (!g_file_test(helpindex, G_FILE_TEST_EXISTS)) {
	helpindex = g_strconcat(helpdir, G_DIR_SEPARATOR_S, dentry,
			      G_DIR_SEPARATOR_S "index.html", NULL);
      }
#else
      helpindex = g_strconcat(helpdir, G_DIR_SEPARATOR_S, dentry,
			      G_DIR_SEPARATOR_S "index.html", NULL);
#endif
      bestscore = score;
    }
  }
  g_dir_close (dp);
  g_free(helpdir);
  if (!helpindex) {
    message_warning(_("Could not find help directory"));
    return;
  }

#ifdef G_OS_WIN32
# define SW_SHOWNORMAL 1
  ShellExecuteA (0, "open", helpindex, NULL, helpdir, SW_SHOWNORMAL);
#else
  command = g_strdup_printf("file://%s", helpindex);
  gtk_show_uri(screen, command, gtk_get_current_event_time (), NULL);
  g_free(command);
#endif

  g_free(helpindex);
}

void 
activate_url (GtkWidget   *parent,
              const gchar *link,
	      gpointer     data)
{
#ifdef G_OS_WIN32
  ShellExecuteA (0, "open", link, NULL, NULL, SW_SHOWNORMAL);
#else
  GdkScreen *screen;
  
  if (parent)
    screen = gtk_widget_get_screen (GTK_WIDGET(parent));
  else
    screen = gdk_screen_get_default ();
  gtk_show_uri(screen, link, gtk_get_current_event_time (), NULL);
#endif
}

void
help_about_callback (GtkAction *action)
{
  const gchar *translators = _("translator_credits-PLEASE_ADD_YOURSELF_HERE");
  const gchar *license = _(
	"This program is free software; you can redistribute it and/or modify\n"
	"it under the terms of the GNU General Public License as published by\n"
	"the Free Software Foundation; either version 2 of the License, or\n"
	"(at your option) any later version.\n"
	"\n"
	"This program is distributed in the hope that it will be useful,\n"
	"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
	"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
	"GNU General Public License for more details.\n"
	"\n"
	"You should have received a copy of the GNU General Public License\n"
	"along with this program; if not, write to the Free Software\n"
	"Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");

  gchar *dirname = dia_get_data_directory("");
  gchar *filename = g_build_filename (dirname, "dia-splash.png", NULL);
  GdkPixbuf *logo = gdk_pixbuf_new_from_file(filename, NULL);

#if GTK_CHECK_VERSION(2,24,0)
  /* rely on gtk_show_uri doing the right thing internally */
#else
  gtk_about_dialog_set_url_hook ((GtkAboutDialogActivateLinkFunc)activate_url, NULL, NULL);
#endif
  gtk_show_about_dialog (NULL,
	"logo", logo,
        "name", "Dia",
	"version", VERSION,
	"comments", _("A program for drawing structured diagrams."),
	"copyright", "(C) 1998-2011 The Free Software Foundation and the authors",
	"website", "http://live.gnome.org/Dia",
	"authors", authors,
	"documenters", documentors,
	"translator-credits", strcmp (translators, "translator_credits-PLEASE_ADD_YOURSELF_HERE")
			? translators : NULL,
	"license", license,
	NULL);
  g_free (dirname);
  g_free (filename);
  if (logo)
    g_object_unref (logo);
}

void
view_zoom_in_callback (GtkAction *action)
{
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  ddisplay_zoom_middle(ddisp, M_SQRT2);
}

void
view_zoom_out_callback (GtkAction *action)
{
  DDisplay *ddisp;
  
  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  ddisplay_zoom_middle(ddisp, M_SQRT1_2);
}

void 
view_zoom_set_callback (GtkAction *action)
{
  int factor;
  /* HACK the actual factor is a suffix to the action name */
  factor = atoi (gtk_action_get_name (action) + strlen ("ViewZoom"));
  view_zoom_set (factor);
}

void
view_show_cx_pts_callback (GtkToggleAction *action)
{
  DDisplay *ddisp;
  int old_val;

  ddisp = ddisplay_active();
  if (!ddisp) return;

  old_val = ddisp->show_cx_pts;
  ddisp->show_cx_pts = gtk_toggle_action_get_active (action);
  
  if (old_val != ddisp->show_cx_pts) {
    ddisplay_add_update_all(ddisp);
    ddisplay_flush(ddisp);
  }
}

void 
view_unfullscreen (void)
{
  DDisplay *ddisp;
  GtkToggleAction *item;

  ddisp = ddisplay_active();
  if (!ddisp) return;

  /* find the menuitem */
  item = GTK_TOGGLE_ACTION (menus_get_action ("ViewFullscreen"));
  if (item && gtk_toggle_action_get_active (item)) {
    gtk_toggle_action_set_active (item, FALSE);
  }
}

void
view_fullscreen_callback (GtkToggleAction *action)
{
  DDisplay *ddisp;
  int fs;

  ddisp = ddisplay_active();
  if (!ddisp) return;

  fs = gtk_toggle_action_get_active (action);
  
  if (fs) /* it is already toggled */
    gtk_window_fullscreen(GTK_WINDOW(ddisp->shell));
  else
    gtk_window_unfullscreen(GTK_WINDOW(ddisp->shell));
}

void
view_aa_callback (GtkToggleAction *action)
{
  DDisplay *ddisp;
  int aa;

  ddisp = ddisplay_active();
  if (!ddisp) return;
 
  aa = gtk_toggle_action_get_active (action);
  
  if (aa != ddisp->aa_renderer) {
    ddisplay_set_renderer(ddisp, aa);
    ddisplay_add_update_all(ddisp);
    ddisplay_flush(ddisp);
  }
}

void
view_visible_grid_callback (GtkToggleAction *action)
{
  DDisplay *ddisp;
  guint old_val;

  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  old_val = ddisp->grid.visible;
  ddisp->grid.visible = gtk_toggle_action_get_active (action); 

  if (old_val != ddisp->grid.visible) {
    ddisplay_add_update_all(ddisp);
    ddisplay_flush(ddisp);
  }
}

void
view_snap_to_grid_callback (GtkToggleAction *action)
{
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  ddisplay_set_snap_to_grid(ddisp, gtk_toggle_action_get_active (action));
}

void
view_snap_to_objects_callback (GtkToggleAction *action)
{
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  ddisplay_set_snap_to_objects(ddisp, gtk_toggle_action_get_active (action));
}

void 
view_toggle_rulers_callback (GtkToggleAction *action)
{
  DDisplay *ddisp;
  
  ddisp = ddisplay_active();
  if (!ddisp) return;

  if (!gtk_toggle_action_get_active (action)) {
    if (display_get_rulers_showing(ddisp)) {
      display_rulers_hide (ddisp);
    }
  } else {
    if (!display_get_rulers_showing(ddisp)) {
      display_rulers_show (ddisp);
    }
  }
}

void
view_toggle_scrollbars_callback (GtkToggleAction *action)
{
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;

  if (gtk_toggle_action_get_active (action)) {
    gtk_widget_show (ddisp->hsb);
    gtk_widget_show (ddisp->vsb);
  } else {
    gtk_widget_hide (ddisp->hsb);
    gtk_widget_hide (ddisp->vsb);
  }
}
extern void
view_new_view_callback (GtkAction *action)
{
  Diagram *dia;

  dia = ddisplay_active_diagram();
  if (!dia) return;
  
  new_display(dia);
}

extern void
view_clone_view_callback (GtkAction *action)
{
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  copy_display(ddisp);
}

void
view_show_all_callback (GtkAction *action)
{
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;
  
  ddisplay_show_all (ddisp);
}

void
view_redraw_callback (GtkAction *action)
{
  DDisplay *ddisp;
  ddisp = ddisplay_active();
  if (!ddisp) return;
  ddisplay_add_update_all(ddisp);
  ddisplay_flush(ddisp);
}

void
view_diagram_properties_callback (GtkAction *action)
{
  DDisplay *ddisp;

  ddisp = ddisplay_active();
  if (!ddisp) return;
  diagram_properties_show(ddisp->diagram);
}

void
view_main_toolbar_callback (GtkAction *action)
{
  integrated_ui_toolbar_show (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION(action)));
}

void
view_main_statusbar_callback (GtkAction *action)
{
  integrated_ui_statusbar_show (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION(action)));
}

void
view_layers_callback (GtkAction *action)
{
  integrated_ui_layer_view_show (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION(action)));
}

void 
layers_add_layer_callback (GtkAction *action)
{
  Diagram *dia;

  dia = ddisplay_active_diagram();
  if (!dia) return;

  diagram_edit_layer (dia, NULL);
}

void 
layers_rename_layer_callback (GtkAction *action)
{
  Diagram *dia;

  dia = ddisplay_active_diagram();
  if (!dia) return;

  diagram_edit_layer (dia, dia->data->active_layer);
}

void
objects_place_over_callback (GtkAction *action)
{
  diagram_place_over_selected(ddisplay_active_diagram());
}

void
objects_place_under_callback (GtkAction *action)
{
  diagram_place_under_selected(ddisplay_active_diagram());
}

void
objects_place_up_callback (GtkAction *action)
{
  diagram_place_up_selected(ddisplay_active_diagram());
}

void
objects_place_down_callback (GtkAction *action)
{
  DDisplay *ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;

  diagram_place_down_selected(ddisplay_active_diagram());
}

void
objects_parent_callback (GtkAction *action)
{
  DDisplay *ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;

  diagram_parent_selected(ddisplay_active_diagram());
}

void
objects_unparent_callback (GtkAction *action)
{
  DDisplay *ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;

  diagram_unparent_selected(ddisplay_active_diagram());
}

void
objects_unparent_children_callback (GtkAction *action)
{
  DDisplay *ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;

  diagram_unparent_children_selected(ddisplay_active_diagram());
}

void
objects_group_callback (GtkAction *action)
{
  DDisplay *ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;

  diagram_group_selected(ddisplay_active_diagram());
  ddisplay_do_update_menu_sensitivity(ddisp);
} 

void
objects_ungroup_callback (GtkAction *action)
{
  DDisplay *ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;

  diagram_ungroup_selected(ddisplay_active_diagram());
  ddisplay_do_update_menu_sensitivity(ddisp);
} 

void
dialogs_properties_callback (GtkAction *action)
{
  Diagram *dia;

  dia = ddisplay_active_diagram();
  if (!dia || textedit_mode(ddisplay_active())) return;

  if (dia->data->selected != NULL) {
    object_list_properties_show(dia, dia->data->selected);
  } else {
    diagram_properties_show(dia);
  } 
}

void
dialogs_layers_callback (GtkAction *action)
{
  layer_dialog_set_diagram(ddisplay_active_diagram());
  layer_dialog_show();
}


void
objects_align_h_callback (GtkAction *action)
{
  const gchar *a;
  int align = DIA_ALIGN_LEFT;
  Diagram *dia;
  GList *objects;

  DDisplay *ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;

  /* HACK align is suffix to action name */
  a = gtk_action_get_name (action) + strlen ("ObjectsAlign");
  if (0 == strcmp ("Left", a)) {
	align = DIA_ALIGN_LEFT;
  } 
  else if (0 == strcmp ("Center", a)) {
	align = DIA_ALIGN_CENTER;
  } 
  else if (0 == strcmp ("Right", a)) {
	align = DIA_ALIGN_RIGHT;
  } 
  else if (0 == strcmp ("Spreadouthorizontally", a)) {
	align = DIA_ALIGN_EQUAL;
  } 
  else if (0 == strcmp ("Adjacent", a)) {
	align = DIA_ALIGN_ADJACENT;
  }
  else {
	g_warning ("objects_align_v_callback() called without appropriate align");
	return;
  }

  dia = ddisplay_active_diagram();
  if (!dia) return;
  objects = dia->data->selected;
  
  object_add_updates_list(objects, dia);
  object_list_align_h(objects, dia, align);
  diagram_update_connections_selection(dia);
  object_add_updates_list(objects, dia);
  diagram_modified(dia);
  diagram_flush(dia);     

  undo_set_transactionpoint(dia->undo);
}

void
objects_align_v_callback (GtkAction *action)
{
  const gchar *a;
  int align;
  Diagram *dia;
  GList *objects;

  DDisplay *ddisp = ddisplay_active();
  if (!ddisp || textedit_mode(ddisp)) return;

  /* HACK align is suffix to action name */
  a = gtk_action_get_name (action) + strlen ("ObjectsAlign");
  if (0 == strcmp ("Top", a)) {
	align = DIA_ALIGN_TOP;
  } 
  else if (0 == strcmp ("Middle", a)) {
	align = DIA_ALIGN_CENTER;
  } 
  else if (0 == strcmp ("Bottom", a)) {
	align = DIA_ALIGN_BOTTOM;
  } 
  else if (0 == strcmp ("Spreadoutvertically", a)) {
	align = DIA_ALIGN_EQUAL;
  } 
  else if (0 == strcmp ("Stacked", a)) {
	align = DIA_ALIGN_ADJACENT;
  }
  else {
	g_warning ("objects_align_v_callback() called without appropriate align");
	return;
  }

  dia = ddisplay_active_diagram();
  if (!dia) return;
  objects = dia->data->selected;

  object_add_updates_list(objects, dia);
  object_list_align_v(objects, dia, align);
  diagram_update_connections_selection(dia);
  object_add_updates_list(objects, dia);
  diagram_modified(dia);
  diagram_flush(dia);     

  undo_set_transactionpoint(dia->undo);
}

void
objects_align_connected_callback (GtkAction *action)
{
  Diagram *dia;
  GList *objects;

  dia = ddisplay_active_diagram();
  if (!dia) return;
  objects = dia->data->selected;

  object_add_updates_list(objects, dia);
  object_list_align_connected(objects, dia, 0);
  diagram_update_connections_selection(dia);
  object_add_updates_list(objects, dia);
  diagram_modified(dia);
  diagram_flush(dia);

  undo_set_transactionpoint(dia->undo);
}

/*! Open a file and show it in a new display */
void
dia_file_open (const gchar *filename,
               DiaImportFilter *ifilter)
{
  Diagram *diagram;

  if (!ifilter)
    ifilter = filter_guess_import_filter(filename);

  diagram = diagram_load(filename, ifilter);
  if (diagram != NULL) {
    diagram_update_extents(diagram);
    layer_dialog_set_diagram(diagram);
    new_display(diagram);
  }
}