File: table.c

package info (click to toggle)
dia 0.98%2Bgit20250827-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,064 kB
  • sloc: ansic: 155,731; xml: 14,062; python: 6,250; cpp: 3,635; sh: 447; perl: 137; makefile: 29
file content (1641 lines) | stat: -rw-r--r-- 54,784 bytes parent folder | download | duplicates (2)
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
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
/* 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.
 *
 * File:    table.c
 *
 * Purpose: This file contains implementation of the "table" code.
 */

/*
 * The code listed here is very similar to the one for the UML class
 * object ... at least in its core. Indeed, I've used it as a
 * template. -- pn
 */

#include "config.h"

#include <glib/gi18n-lib.h>

#include <string.h>
#include <ctype.h>

#include "database.h"
#include "propinternals.h"
#include "diarenderer.h"
#include "element.h"
#include "attributes.h"
#include "pixmaps/table.xpm"

#define TABLE_UNDERLINE_WIDTH 0.05
#define TABLE_COMMENT_MAXWIDTH 40
#define TABLE_ATTR_NAME_TYPE_GAP 0.5
#define TABLE_ATTR_NAME_OFFSET 0.3
#define TABLE_ATTR_COMMENT_OFFSET 0.25
#define TABLE_ATTR_INDIC_WIDTH 0.20
#define TABLE_ATTR_INDIC_LINE_WIDTH 0.01
#define TABLE_NORMAL_FONT_HEIGHT 0.8

/* ----------------------------------------------------------------------- */

static double           table_calculate_namebox_data    (Table             *);
static double           table_init_attributesbox_height (Table             *);
static DiaObject       *table_create                    (Point             *,
                                                         void              *,
                                                         Handle           **,
                                                         Handle           **);
static DiaObject       *table_load                      (ObjectNode         obj_node,
                                                         int                version,
                                                         DiaContext        *ctx);
static void             table_save                      (Table             *,
                                                         ObjectNode,
                                                         DiaContext        *ctx);
static void             table_destroy                   (Table             *);
static double           table_distance_from             (Table             *,
                                                         Point             *);
static void             table_select                    (Table             *,
                                                         Point             *,
                                                         DiaRenderer       *);
static DiaObject       *table_copy                      (Table             *table);
static DiaObjectChange *table_move                      (Table             *,
                                                         Point             *);
static DiaObjectChange *table_move_handle               (Table             *,
                                                         Handle            *,
                                                         Point             *,
                                                         ConnectionPoint   *,
                                                         HandleMoveReason,
                                                         ModifierKeys);
static PropDescription *table_describe_props            (Table             *);
static void             table_get_props                 (Table             *,
                                                         GPtrArray         *);
static void             table_set_props                 (Table             *,
                                                         GPtrArray         *);
static void             table_draw                      (Table             *,
                                                         DiaRenderer       *);
static double           table_draw_namebox              (Table             *,
                                                         DiaRenderer       *,
                                                         Element           *);
static double           table_draw_attributesbox        (Table             *,
                                                         DiaRenderer       *,
                                                         Element           *,
                                                         double);
static DiaMenu         *table_object_menu               (DiaObject         *,
                                                         Point             *);
static DiaObjectChange *table_show_comments_cb          (DiaObject         *,
                                                         Point             *,
                                                         gpointer);
static void             underline_table_attribute       (DiaRenderer       *,
                                                         Point,
                                                         TableAttribute    *,
                                                         Table             *);
static void             fill_diamond                    (DiaRenderer       *,
                                                         double,
                                                         double,
                                                         Point             *,
                                                         Color             *);
static void             table_init_fonts                (Table             *);

static TableAttribute *table_attribute_new (void);
static void table_attribute_free (TableAttribute *);
static TableAttribute *table_attribute_copy (TableAttribute *);
static void table_update_connectionpoints (Table *);
static void table_update_positions (Table *);
static void table_compute_width_height (Table *);
static TableState *table_state_new (Table *);
static DiaObjectChange *table_change_new                (Table             *,
                                                         TableState        *,
                                                         GList             *,
                                                         GList             *,
                                                         GList             *);
static void table_update_primary_key_font (Table *);

static char *create_documentation_tag (char     *comment,
                                       gboolean  tagging,
                                       int       WrapPoint,
                                       int      *NumberOfLines);
static void draw_comments (DiaRenderer *renderer,
                           DiaFont     *font,
                           double       font_height,
                           Color       *text_color,
                           char        *comment,
                           gboolean     comment_tagging,
                           int          Comment_line_length,
                           Point       *p,
                           int          alignment);

/* ----------------------------------------------------------------------- */

static ObjectTypeOps table_type_ops =
  {
    (CreateFunc) table_create,
    (LoadFunc) table_load,
    (SaveFunc) table_save
  };

DiaObjectType table_type =
  {
    "Database - Table", /* name */
    0,               /* version */
    table_xpm,        /* pixmap */
    &table_type_ops      /* ops */
  };

static ObjectOps table_ops =
  {
    (DestroyFunc)               table_destroy,
    (DrawFunc)                  table_draw,
    (DistanceFunc)              table_distance_from,
    (SelectFunc)                table_select,
    (CopyFunc)                  table_copy,
    (MoveFunc)                  table_move,
    (MoveHandleFunc)            table_move_handle,
    (GetPropertiesFunc) object_create_props_dialog,
    (ApplyPropertiesDialogFunc) object_apply_props_from_dialog,
    (ObjectMenuFunc)            table_object_menu,
    (DescribePropsFunc)         table_describe_props,
    (GetPropsFunc)              table_get_props,
    (SetPropsFunc)              table_set_props,
    (TextEditFunc) 0,
    (ApplyPropertiesListFunc) object_apply_props,
};

static PropDescription table_attribute_props[] =
  {
    { "name", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Name"), NULL, NULL },
    { "type", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Type"), NULL, NULL },
    { "comment", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Comment"), NULL, NULL },
    { "primary_key", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Primary"), NULL, N_("Primary key") },
    { "nullable", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Nullable"), NULL, NULL },
    { "unique", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Unique"), NULL, NULL },
    { "default_value", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Default"), NULL, N_("Default value") },

    PROP_DESC_END
  };

static PropOffset table_attribute_offsets[] = {
  { "name", PROP_TYPE_STRING, offsetof(TableAttribute, name) },
  { "type", PROP_TYPE_STRING, offsetof(TableAttribute, type) },
  { "comment", PROP_TYPE_STRING, offsetof(TableAttribute, comment) },
  { "primary_key", PROP_TYPE_BOOL, offsetof(TableAttribute, primary_key) },
  { "nullable", PROP_TYPE_BOOL, offsetof(TableAttribute, nullable) },
  { "unique", PROP_TYPE_BOOL, offsetof(TableAttribute, unique) },
  { "default_value", PROP_TYPE_STRING, offsetof(TableAttribute, default_value) },

  { NULL, 0, 0 },
};

static PropDescDArrayExtra table_attribute_extra =
  {
    { table_attribute_props, table_attribute_offsets, "table_attribute" },
    (NewRecordFunc) table_attribute_new,
    (FreeRecordFunc) table_attribute_free
  };

static PropDescription table_props[] =
  {
    ELEMENT_COMMON_PROPERTIES,

    PROP_STD_NOTEBOOK_BEGIN,
    PROP_NOTEBOOK_PAGE("table", PROP_FLAG_DONT_MERGE, N_("Table")),
    { "name", PROP_TYPE_STRING, PROP_FLAG_VISIBLE,
      N_("Name"), NULL, NULL },
    { "comment", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Comment"), NULL, NULL },

    PROP_MULTICOL_BEGIN("visibilities"),
    PROP_MULTICOL_COLUMN("first"),
    { "visible_comment", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Visible comments"), NULL, NULL },
    { "underline_primary_key", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Underline primary keys"), NULL, NULL },
    PROP_MULTICOL_COLUMN("second"),
    { "tagging_comment", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Comment tagging"), NULL, NULL },
    { "bold_primary_keys", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Use bold font for primary keys"), NULL, NULL },
    PROP_MULTICOL_END("visibilities"),

    PROP_NOTEBOOK_PAGE("attribute", PROP_FLAG_DONT_MERGE | PROP_FLAG_NO_DEFAULTS, N_("Attributes")),
    { "attributes", PROP_TYPE_DARRAY, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL | PROP_FLAG_DONT_MERGE | PROP_FLAG_NO_DEFAULTS,
      "", NULL, &table_attribute_extra },

    PROP_NOTEBOOK_PAGE("style", PROP_FLAG_DONT_MERGE, N_("Style")),
    PROP_FRAME_BEGIN("fonts", 0, N_("Fonts")),
    PROP_MULTICOL_BEGIN("table"),
    PROP_MULTICOL_COLUMN("font"),
    { "normal_font", PROP_TYPE_FONT, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Normal"), NULL, NULL },
    { "name_font", PROP_TYPE_FONT, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Table name"), NULL, NULL },
    { "comment_font", PROP_TYPE_FONT, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_("Comment"), NULL, NULL },
    PROP_MULTICOL_COLUMN("height"),
    { "normal_font_height", PROP_TYPE_REAL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_(" "), NULL, NULL },
    { "name_font_height", PROP_TYPE_REAL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_(" "), NULL, NULL },
    { "comment_font_height", PROP_TYPE_REAL, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
      N_(" "), NULL, NULL },
    PROP_MULTICOL_END("table"),
    PROP_FRAME_END("fonts", 0),

    PROP_STD_TEXT_COLOUR_OPTIONS(PROP_FLAG_VISIBLE | PROP_FLAG_STANDARD | PROP_FLAG_OPTIONAL),
    PROP_STD_LINE_COLOUR_OPTIONAL,
    PROP_STD_FILL_COLOUR_OPTIONAL,
    PROP_STD_LINE_WIDTH_OPTIONAL,

    PROP_STD_NOTEBOOK_END,

    PROP_DESC_END
  };

static PropOffset table_offsets[] = {
  ELEMENT_COMMON_PROPERTIES_OFFSETS,

  { "text_colour", PROP_TYPE_COLOUR, offsetof(Table, text_color) },
  { "line_colour", PROP_TYPE_COLOUR, offsetof(Table, line_color) },
  { "fill_colour", PROP_TYPE_COLOUR, offsetof(Table, fill_color) },
  { PROP_STDNAME_LINE_WIDTH, PROP_STDTYPE_LINE_WIDTH, offsetof(Table, border_width) },
  { "name", PROP_TYPE_STRING, offsetof(Table, name) },
  { "comment", PROP_TYPE_STRING, offsetof(Table, comment) },
  { "visible_comment", PROP_TYPE_BOOL, offsetof(Table, visible_comment) },
  { "tagging_comment", PROP_TYPE_BOOL, offsetof(Table, tagging_comment) },
  { "underline_primary_key", PROP_TYPE_BOOL, offsetof(Table, underline_primary_key) },
  { "bold_primary_keys", PROP_TYPE_BOOL, offsetof(Table, bold_primary_key) },

  PROP_OFFSET_MULTICOL_BEGIN("table"),
  PROP_OFFSET_MULTICOL_COLUMN("font"),
  { "normal_font", PROP_TYPE_FONT, offsetof(Table, normal_font) },
  { "name_font", PROP_TYPE_FONT, offsetof(Table, name_font) },
  { "comment_font", PROP_TYPE_FONT, offsetof(Table, comment_font) },

  PROP_OFFSET_MULTICOL_COLUMN("height"),
  { "normal_font_height", PROP_TYPE_REAL, offsetof(Table, normal_font_height) },
  { "name_font_height", PROP_TYPE_REAL, offsetof(Table, name_font_height) },
  { "comment_font_height", PROP_TYPE_REAL, offsetof(Table, comment_font_height) },
  PROP_OFFSET_MULTICOL_END("table"),

  { "attributes", PROP_TYPE_DARRAY, offsetof(Table, attributes) },

  { NULL, 0, 0 }
};

static DiaMenuItem table_menu_items[] = {
  { N_("Show comments"), table_show_comments_cb, NULL,
    DIAMENU_ACTIVE | DIAMENU_TOGGLE },
};

static DiaMenu table_menu = {
  N_("Table"),
  sizeof (table_menu_items)/sizeof (DiaMenuItem),
  table_menu_items,
  NULL
};

/* ----------------------------------------------------------------------- */

/**
 * Create a new TableAttribute
 */
static TableAttribute *
table_attribute_new (void)
{
  TableAttribute * attr;

  attr = g_new0 (TableAttribute, 1);
  if (attr != NULL)
    {
      attr->name = g_strdup ("");
      attr->type = g_strdup ("");
      attr->comment = g_strdup ("");
      attr->primary_key = FALSE;
      /* by default nullable */
      attr->nullable = TRUE;
      /* by default not unique */
      attr->unique = FALSE;

      /* empty default value by default */
      attr->default_value = g_strdup ("");

      attr->left_connection = NULL;
      attr->right_connection = NULL;
    }
  return attr;
}

static void
table_attribute_ensure_connection_points (TableAttribute * attr,
                                          DiaObject * obj)
{
  if (attr->left_connection == NULL)
    attr->left_connection = g_new0 (ConnectionPoint, 1);
  g_assert (attr->left_connection != NULL);
  attr->left_connection->object = obj;
  if (attr->right_connection == NULL)
    attr->right_connection = g_new0 (ConnectionPoint, 1);
  g_assert (attr->right_connection != NULL);
  attr->right_connection->object = obj;
}


/**
 * Free a TableAttribute and its allocated resources. Upon return of
 * this function the passed pointer will not be valid anymore.
 */
static void
table_attribute_free (TableAttribute * attr)
{
  g_clear_pointer (&attr->name, g_free);
  g_clear_pointer (&attr->type, g_free);
  g_clear_pointer (&attr->comment, g_free);

  /* do not free the connection points here as they may be shared */

  g_free (attr);
}


/**
 * Create a copy of the passed attribute. The returned copy of the
 * attribute needs to be freed using g_free when it is no longer needed.
 */
static TableAttribute *
table_attribute_copy (TableAttribute * orig)
{
  TableAttribute * copy;

  copy = g_new0 (TableAttribute, 1);
  copy->name = g_strdup (orig->name);
  copy->type = g_strdup (orig->type);
  copy->comment = g_strdup (orig->comment);
  copy->primary_key = orig->primary_key;
  copy->nullable = orig->nullable;
  copy->unique = orig->unique;
  copy->default_value = g_strdup (orig->default_value);

  return copy;
}

/* ----------------------------------------------------------------------- */

/**
 * Construct and initialize a new table object.
 */
static DiaObject *
table_create (Point * startpoint,
                void * user_data,
                Handle **handle1,
                Handle **handle2)
{
  Table *table;
  Element *elem;
  DiaObject *obj;
  gint i;

  table = g_new0 (Table, 1);
  elem = &table->element;
  obj = &elem->object;

  /* init data */
  table->name = g_strdup (_("Table"));
  table->comment = NULL;
  table->visible_comment = FALSE;
  table->tagging_comment = FALSE;
  table->underline_primary_key = TRUE;
  table->bold_primary_key = FALSE;
  table->attributes = NULL;

  /* init colors */
  table->text_color = attributes_get_foreground ();
  table->line_color = attributes_get_foreground ();
  table->fill_color = attributes_get_background ();

  table->border_width = attributes_get_default_linewidth ();

  /* init the fonts */
  table_init_fonts (table);

  /* now init inherited `element' */
  elem->corner = *startpoint;
  element_init (elem, 8, TABLE_CONNECTIONPOINTS);

  /* init the inherited `object' */
  obj->type = &table_type;
  obj->ops = &table_ops;

  /* init the connection points */
  for (i = 0; i < TABLE_CONNECTIONPOINTS; i++) {
    obj->connections[i] = &(table->connections[i]);
    table->connections[i].object = obj;
    table->connections[i].connected = NULL;
  }

  /* init the 8 handles placed around the table object */
  for (i = 0; i < 8; i++) {
    obj->handles[i]->type = HANDLE_NON_MOVABLE;
  }

  *handle1 = NULL;
  *handle2 = NULL;

  table_update_primary_key_font (table);
  table_compute_width_height (table);
  table_update_positions (table);

  return obj;
}

static DiaObject *
table_load (ObjectNode obj_node, int version, DiaContext *ctx)
{
  Table * table;
  Element * elem;
  DiaObject * obj;
  gint i;

  table = g_new0 (Table, 1);
  elem = &table->element;
  obj = &elem->object;

  obj->type = &table_type;
  obj->ops = &table_ops;

  element_load (elem, obj_node, ctx);
  element_init (elem, 8, TABLE_CONNECTIONPOINTS);

  object_load_props(obj,obj_node, ctx);

  /* fill in defaults if not given in the loaded file */
  if (object_find_attribute (obj_node, "line_colour") == NULL)
    table->line_color = attributes_get_foreground ();
  if (object_find_attribute (obj_node, "text_colour") == NULL)
    table->text_color = attributes_get_foreground ();
  if (object_find_attribute (obj_node, "fill_colour") == NULL)
    table->fill_color = attributes_get_background ();
  if (object_find_attribute (obj_node, PROP_STDNAME_LINE_WIDTH) == NULL)
    table->border_width = attributes_get_default_linewidth ();
  if (object_find_attribute (obj_node, "underline_primary_key") == NULL)
    table->underline_primary_key = TRUE;

  /* init the fonts */
  table_init_fonts (table);

  /* init the connection points */
  for (i = 0; i < TABLE_CONNECTIONPOINTS; i++) {
    obj->connections[i] = &(table->connections[i]);
    table->connections[i].object = obj;
    table->connections[i].connected = NULL;
  }

  /* init the 8 handles placed around the table object */
  for (i = 0; i < 8; i++) {
    obj->handles[i]->type = HANDLE_NON_MOVABLE;
  }

  table_update_primary_key_font (table);
  table_compute_width_height (table);
  table_update_positions (table);

  return &table->element.object;
}

static void
table_save (Table *table, ObjectNode obj_node, DiaContext *ctx)
{
  object_save_props (&table->element.object, obj_node, ctx);
}

static void
table_destroy (Table * table)
{
  GList * list;

  table->destroyed = TRUE;

  element_destroy (&table->element);

  g_clear_pointer (&table->name, g_free);
  g_clear_pointer (&table->comment, g_free);

  list = table->attributes;
  while (list != NULL) {
    TableAttribute * attr = (TableAttribute *) list->data;
    table_attribute_free (attr);

    list = g_list_next (list);
  }
  g_list_free (table->attributes);

  g_clear_object (&table->normal_font);
  g_clear_object (&table->primary_key_font);
  g_clear_object (&table->name_font);
  g_clear_object (&table->comment_font);
}

static void
table_draw (Table *table, DiaRenderer *renderer)
{
  real y = 0.0;
  Element * elem;

  dia_renderer_set_linewidth (renderer, table->border_width);
  dia_renderer_set_fillstyle (renderer, DIA_FILL_STYLE_SOLID);
  dia_renderer_set_linestyle (renderer, DIA_LINE_STYLE_SOLID, 0.0);

  elem = &table->element;

  y = table_draw_namebox (table, renderer, elem);
  table_draw_attributesbox (table, renderer, elem, y);
}

static real
table_draw_namebox (Table *table, DiaRenderer *renderer, Element *elem)
{
  Point startP;
  Point endP;

  /* upper left corner */
  startP.x = elem->corner.x;
  startP.y = elem->corner.y;
  /* lower right corner */
  endP.x = startP.x + elem->width;
  endP.y = startP.y + table->namebox_height;

  /* first draw the outer box and fill for the class name object */
  dia_renderer_draw_rect (renderer, &startP, &endP, &table->fill_color, &table->line_color);

  if (IS_NOT_EMPTY (table->name)) {
    startP.x += elem->width / 2.0;
    startP.y += table->name_font_height;
    dia_renderer_set_font (renderer,
                           table->name_font,
                           table->name_font_height);
    dia_renderer_draw_string (renderer,
                              table->name,
                              &startP,
                              DIA_ALIGN_CENTRE,
                              &table->text_color);
  }

  if (table->visible_comment && IS_NOT_EMPTY (table->comment)) {
    draw_comments (renderer,
                   table->comment_font,
                   table->comment_font_height,
                   &table->text_color,
                   table->comment,
                   table->tagging_comment,
                   TABLE_COMMENT_MAXWIDTH,
                   &startP,
                   DIA_ALIGN_CENTRE);
  }

  return endP.y;
}


/**
 *
 * pn: This function is "borrowed" from the source code for the UML
 * class object.
 *
 * Draw the comment at the point, p, using the comment font from the
 * class defined by umlclass. When complete update the point to reflect
 * the size of data drawn.
 * The comment will have been word wrapped using the function
 * create_documenattion_tag, so it may have more than one line on the
 * display.
 *
 * @param   renderer            The Renderer on which the comment is being drawn
 * @param   font                The font to render the comment in.
 * @param   font_height         The Y size of the font used to render the comment
 * @param   text_color          A pointer to the color to use to render the comment
 * @param   comment             The comment string to render
 * @param   comment_tagging     If the {documentation = } tag should be enforced
 * @param   Comment_line_length The maximum length of any one line in the comment
 * @param   p                   The point at which the comment is to start
 * @param   alignment           The method to use for alignment of the font
 * @see   uml_create_documentation
 */
static void
draw_comments (DiaRenderer *renderer,
               DiaFont     *font,
               real         font_height,
               Color       *text_color,
               char        *comment,
               gboolean     comment_tagging,
               gint         Comment_line_length,
               Point       *p,
               int          alignment)
{
  int   NumberOfLines = 0;
  int   Index;
  char *CommentString = 0;
  char *NewLineP= NULL;
  char *RenderP;

  CommentString =
    create_documentation_tag (comment, comment_tagging, Comment_line_length, &NumberOfLines);
  RenderP = CommentString;
  dia_renderer_set_font (renderer, font, font_height);
  for ( Index=0; Index < NumberOfLines; Index++) {
    p->y += font_height;                    /* Advance to the next line */
    NewLineP = strchr (RenderP, '\n');
    if ( NewLineP != NULL) {
      *NewLineP++ = '\0';
    }
    dia_renderer_draw_string (renderer, RenderP, p, alignment, text_color);
    RenderP = NewLineP;
    if ( NewLineP == NULL){
      break;
    }
  }
  g_clear_pointer (&CommentString, g_free);
}


static void
fill_diamond (DiaRenderer *renderer,
              real         half_height,
              real         width,
              Point       *lower_midpoint,
              Color       *color)
{
  Point poly[4];

  poly[0].x = lower_midpoint->x - width/2.0;
  poly[0].y = lower_midpoint->y;
  poly[1].x = lower_midpoint->x;
  poly[1].y = lower_midpoint->y + half_height;
  poly[2].x = lower_midpoint->x + width/2.0;
  poly[2].y = lower_midpoint->y;
  poly[3].x = poly[1].x;
  poly[3].y = lower_midpoint->y - half_height;

  dia_renderer_set_fillstyle (renderer, DIA_FILL_STYLE_SOLID);
  dia_renderer_set_linejoin (renderer, DIA_LINE_JOIN_MITER);

  dia_renderer_draw_polygon (renderer, poly, 4, color, NULL);
}

static real
table_draw_attributesbox (Table         *table,
                          DiaRenderer   *renderer,
                          Element       *elem,
                          real           Yoffset)
{
  Point startP, startTypeP;
  Point endP;
  Point indicP;
  GList * list;
  Color * text_color = &table->text_color;
  Color * fill_color = &table->fill_color;
  Color * line_color = &table->line_color;
  DiaFont * attr_font;
  real attr_font_height;
  real scale;

  startP.x = elem->corner.x;
  startP.y = Yoffset;

  endP.x = startP.x + elem->width;
  endP.y = startP.y + table->attributesbox_height;

  dia_renderer_draw_rect (renderer, &startP, &endP, fill_color, line_color);

  startP.x += TABLE_ATTR_NAME_OFFSET;
  startP.x += (table->border_width/2.0 + 0.1);

  list = table->attributes;
  /* adjust size of symbols with font height */
  scale = table->normal_font_height/TABLE_NORMAL_FONT_HEIGHT;
  while (list != NULL) {
    TableAttribute * attr = (TableAttribute *) list->data;

    if (attr->primary_key) {
      attr_font = table->primary_key_font;
      attr_font_height = table->primary_key_font_height;
    } else {
      attr_font = table->normal_font;
      attr_font_height = table->normal_font_height;
    }

    startP.y += attr_font_height;
    dia_renderer_set_font (renderer,
                           attr_font,
                           attr_font_height);

    dia_renderer_set_linewidth (renderer, TABLE_ATTR_INDIC_LINE_WIDTH);
    indicP = startP;
    indicP.x -= ((TABLE_ATTR_NAME_OFFSET/2.0)
                  + (TABLE_ATTR_INDIC_WIDTH*scale/4.0));
    indicP.y -= (attr_font_height/2.0);
    indicP.y += TABLE_ATTR_INDIC_WIDTH*scale/2.0;
    if (attr->primary_key) {
      fill_diamond (renderer,
                    0.75*TABLE_ATTR_INDIC_WIDTH*scale,
                    TABLE_ATTR_INDIC_WIDTH*scale,
                    &indicP,
                    &table->line_color);
    } else if (attr->nullable) {
      dia_renderer_draw_ellipse (renderer,
                                 &indicP,
                                 TABLE_ATTR_INDIC_WIDTH*scale,
                                 TABLE_ATTR_INDIC_WIDTH*scale,
                                 NULL,
                                 &table->line_color);
    } else {
      dia_renderer_draw_ellipse (renderer,
                                 &indicP,
                                 TABLE_ATTR_INDIC_WIDTH*scale,
                                 TABLE_ATTR_INDIC_WIDTH*scale,
                                 &table->line_color,
                                 NULL);
    }

    if (IS_NOT_EMPTY (attr->name)) {
      dia_renderer_draw_string (renderer,
                                attr->name,
                                &startP,
                                DIA_ALIGN_LEFT,
                                text_color);
    }

    if (IS_NOT_EMPTY (attr->type)) {
      startTypeP = startP;
      startTypeP.x += table->maxwidth_attr_name + TABLE_ATTR_NAME_TYPE_GAP;
      dia_renderer_draw_string (renderer,
                                attr->type,
                                &startTypeP,
                                DIA_ALIGN_LEFT,
                                text_color);
    }

    if (table->underline_primary_key && attr->primary_key)
      underline_table_attribute (renderer, startP, attr, table);

    if (table->visible_comment && IS_NOT_EMPTY (attr->comment)) {
      startP.x += TABLE_ATTR_COMMENT_OFFSET;
      draw_comments (renderer,
                     table->comment_font,
                     table->comment_font_height,
                     text_color,
                     attr->comment,
                     table->tagging_comment,
                     TABLE_COMMENT_MAXWIDTH,
                     &startP,
                     DIA_ALIGN_LEFT);
      startP.x -= TABLE_ATTR_COMMENT_OFFSET;
      startP.y += table->comment_font_height/2;
    }

    list = g_list_next (list);
  }

  return Yoffset;
}

static real
table_distance_from (Table * table, Point *point)
{
  const DiaRectangle * rect;
  DiaObject * obj;

  obj = &table->element.object;
  rect = dia_object_get_bounding_box (obj);
  return distance_rectangle_point (rect, point);
}

static void
table_select (Table * table, Point * clicked_point,
                DiaRenderer * interactive_renderer)
{
  element_update_handles (&table->element);
}

static DiaObject *
table_copy(Table * orig)
{
  Table * copy;
  Element * orig_elem, * copy_elem;
  DiaObject * copy_obj;
  GList * list;
  gint i;

  orig_elem = &orig->element;

  copy = g_new0 (Table, 1);
  copy_elem = &copy->element;
  copy_obj = &copy_elem->object;
  element_copy (orig_elem, copy_elem);

  for (i = 0; i < TABLE_CONNECTIONPOINTS; i++)
    {
      copy_obj->connections[i] = &copy->connections[i];
      copy->connections[i].object = copy_obj;
      copy->connections[i].connected = NULL;
      copy->connections[i].pos = orig->connections[i].pos;
    }

  copy->name = g_strdup (orig->name);
  copy->comment = g_strdup (orig->comment);
  copy->visible_comment = orig->visible_comment;
  copy->tagging_comment = orig->tagging_comment;
  copy->underline_primary_key = orig->underline_primary_key;
  copy->bold_primary_key = orig->bold_primary_key;
  list = orig->attributes;
  i = TABLE_CONNECTIONPOINTS;
  while (list != NULL)
    {
      TableAttribute * orig_attr = (TableAttribute *) list->data;
      TableAttribute * copy_attr = table_attribute_copy (orig_attr);
      table_attribute_ensure_connection_points (copy_attr, copy_obj);
      copy_obj->connections[i++] = copy_attr->left_connection;
      copy_obj->connections[i++] = copy_attr->right_connection;

      copy->attributes = g_list_append (copy->attributes, copy_attr);
      list = g_list_next (list);
    }
  copy->normal_font_height = orig->normal_font_height;
  copy->normal_font = g_object_ref (orig->normal_font);
  copy->name_font_height = orig->name_font_height;
  copy->name_font = g_object_ref (orig->name_font);
  copy->comment_font_height = orig->comment_font_height;
  copy->comment_font = g_object_ref (orig->comment_font);
  copy->text_color = orig->text_color;
  copy->line_color = orig->line_color;
  copy->fill_color = orig->fill_color;
  copy->border_width = orig->border_width;

  table_update_primary_key_font (copy);
  table_compute_width_height (copy);
  table_update_positions (copy);

  return &copy->element.object;
}


static DiaObjectChange *
table_move (Table *table, Point *to)
{
  table->element.corner = *to;
  table_update_positions (table);
  return NULL;
}


static DiaObjectChange *
table_move_handle (Table            *table,
                   Handle           *handle,
                   Point            *to,
                   ConnectionPoint  *cp,
                   HandleMoveReason  reason,
                   ModifierKeys      modifiers)
{
  /* ignore this event */
  return NULL;
}

static PropDescription *
table_describe_props (Table *table)
{
  if (table_props[0].quark == 0) {
    prop_desc_list_calculate_quarks (table_props);
  }

  return table_props;
}

static void
table_get_props (Table * table, GPtrArray *props)
{
  object_get_props_from_offsets (&table->element.object, table_offsets, props);
}

static void
table_set_props (Table *table, GPtrArray *props)
{
  object_set_props_from_offsets (&table->element.object, table_offsets, props);

  if (find_prop_by_name (props, "normal_font_height") != NULL)
    table->primary_key_font_height = table->normal_font_height;

  if (find_prop_by_name (props, "normal_font") != NULL)
    table_update_primary_key_font (table);

  /* the following routines depend on the fonts, and we can get called
   * here during load and the fonts are optional ...
   */
  if (table->normal_font && table->name_font && table->comment_font)
    {
      table_update_connectionpoints (table);
      table_compute_width_height (table);
      table_update_positions (table);
    }
}

/**
 * Init the height and width of the underlying element. This routine
 * uses `table_calculate_namebox_data' and
 * `table_init_attributesbox_height' which require the table's font
 * members to be initialized, so be sure to call this routine after the
 * font members were already initialized.
 */
static void
table_compute_width_height (Table * table)
{
  real width = 0.0;
  real maxwidth = 0.0;

  width = table_calculate_namebox_data (table);
  table->element.height = table->namebox_height;
  maxwidth = MAX(width, maxwidth);

  width = table_init_attributesbox_height (table);
  table->element.height += table->attributesbox_height;
  maxwidth = MAX(width, maxwidth);

  table->element.width = maxwidth + 0.5;
}

/**
 * Compute the height of the box surrounding the table's attributes,
 * store it in the passed table structure and return the width into
 * which all attributes of the table fit. While traversing the table's
 * attributes to compute the height, store also the width of the longest
 * attribute name in the passed table structure. This function makes use
 * of the fonts defined in the passed table structure, so be sure to
 * initialize them before calling this routine.
 */
static real
table_init_attributesbox_height (Table * table)
{
  real maxwidth_name = 0.0;
  real maxwidth_type = 0.0;
  real maxwidth_comment = 0.0;
  real width = 0.0;
  GList * list = table->attributes;
  DiaFont * comment_font = table->comment_font;
  real comment_font_height = table->comment_font_height;
  DiaFont * attr_font;
  real attr_font_height;

  /* height of an empty attributesbox */
  table->attributesbox_height = 2*0.1;

  while (list != NULL) {
    TableAttribute * attrib = (TableAttribute *) list->data;

    if (attrib->primary_key)
      {
        attr_font = table->primary_key_font;
        attr_font_height = table->primary_key_font_height;
      }
    else
      {
        attr_font = table->normal_font;
        attr_font_height = table->normal_font_height;
      }

    if (IS_NOT_EMPTY(attrib->name))
      {
        width = dia_font_string_width (attrib->name,
                                       attr_font, attr_font_height);
        maxwidth_name = MAX(maxwidth_name, width);
      }
    if (IS_NOT_EMPTY(attrib->type))
      {
        width = dia_font_string_width (attrib->type,
                                       attr_font, attr_font_height);
        maxwidth_type = MAX(maxwidth_type, width);
      }
    table->attributesbox_height += attr_font_height;

    if (table->visible_comment && IS_NOT_EMPTY(attrib->comment))
      {
        int num_of_lines = 0;
        char * cmt_str = create_documentation_tag (attrib->comment,
                                                   table->tagging_comment,
                                                   TABLE_COMMENT_MAXWIDTH,
                                                   &num_of_lines);
        width = dia_font_string_width (cmt_str,
                                       comment_font,
                                       comment_font_height);
        width += TABLE_ATTR_COMMENT_OFFSET;
        g_clear_pointer (&cmt_str, g_free);

        table->attributesbox_height += (comment_font_height * num_of_lines);
        table->attributesbox_height += comment_font_height / 2;
        maxwidth_comment = MAX(maxwidth_comment, width);
      }

    list = g_list_next (list);
  }
  table->maxwidth_attr_name = maxwidth_name;
  width = TABLE_ATTR_NAME_OFFSET
    + maxwidth_name
    + maxwidth_type
    + TABLE_ATTR_NAME_TYPE_GAP;
  return MAX(width, maxwidth_comment);
}

static void
underline_table_attribute (DiaRenderer     *renderer,
                           Point            StartPoint,
                           TableAttribute  *attr,
                           Table           *table)
{
  Point    UnderlineStartPoint;
  Point    UnderlineEndPoint;
  DiaFont * font;
  real font_height;

  if (attr->primary_key) {
    font = table->primary_key_font;
    font_height = table->primary_key_font_height;
  } else {
    font = table->normal_font;
    font_height = table->normal_font_height;
  }

  UnderlineStartPoint = StartPoint;
  UnderlineStartPoint.y += font_height * 0.1;
  UnderlineEndPoint = UnderlineStartPoint;
  UnderlineEndPoint.x += table->maxwidth_attr_name + TABLE_ATTR_NAME_TYPE_GAP;
  if (IS_NOT_EMPTY (attr->type)) {
    UnderlineEndPoint.x += dia_font_string_width (attr->type,
                                                  font,
                                                  font_height);
  }
  dia_renderer_set_linewidth (renderer, TABLE_UNDERLINE_WIDTH);
  dia_renderer_draw_line (renderer,
                          &UnderlineStartPoint,
                          &UnderlineEndPoint,
                          &table->text_color);
}

/**
 * pn: This function is "borrowed" from the source code for the UML
 * class object.
 *
 * Create a documentation tag from a comment.
 *
 * First a string is created containing only the text
 * "{documentation = ". Then the contents of the comment string
 * are added but wrapped. This is done by first looking for any
 * New Line characters. If the line segment is longer than the
 * WrapPoint would allow, the line is broken at either the
 * first whitespace before the WrapPoint or if there are no
 * whitespaces in the segment, at the WrapPoint.  This
 * continues until the entire string has been processed and
 * then the resulting new string is returned. No attempt is
 * made to rejoin any of the segments, that is all New Lines
 * are treated as hard newlines. No syllable matching is done
 * either so breaks in words will sometimes not make real
 * sense.
 * <p>
 * Finally, since this function returns newly created dynamic
 * memory the caller must free the memory to prevent memory
 * leaks.
 *
 * @param  comment       The comment to be wrapped to the line length limit
 * @param  WrapPoint     The maximum line length allowed for the line.
 * @param  NumberOfLines The number of comment lines after the wrapping.
 * @return               a pointer to the wrapped documentation
 *
 *  NOTE:
 *      This function should most likely be move to a source file for
 *      handling global UML functionallity at some point.
 */
static char *
create_documentation_tag (char     *comment,
                          gboolean  tagging,
                          int       WrapPoint,
                          int      *NumberOfLines)
{
  char *CommentTag = tagging ? "{documentation = " : "";
  int   TagLength  = strlen (CommentTag);
  /* Make sure that there is at least some value greater then zero for the WrapPoint to
   * support diagrams from earlier versions of Dia. So if the WrapPoint is zero then use
   * the taglength as the WrapPoint. If the Tag has been changed such that it has a length
   * of 0 then use 1.
   */
  int   WorkingWrapPoint = (TagLength<WrapPoint) ? WrapPoint : ((TagLength<=0)?1:TagLength);
  int   RawLength        = TagLength + strlen(comment) + (tagging?1:0);
  int   MaxCookedLength  = RawLength + RawLength/WorkingWrapPoint;
  char *WrappedComment   = g_new0 (char, MaxCookedLength + 1);
  int   AvailSpace       = WorkingWrapPoint - TagLength;
  char *Scan;
  char *BreakCandidate;
  gunichar ScanChar;
  gboolean AddNL            = FALSE;

  if (tagging)
    strcat(WrappedComment, CommentTag);
  *NumberOfLines = 1;

  while ( *comment ) {
    /* Skip spaces */
    while ( *comment && g_unichar_isspace(g_utf8_get_char(comment)) ) {
        comment = g_utf8_next_char(comment);
    }
    /* Copy chars */
    if ( *comment ){
      /* Scan to \n or avalable space exhausted */
      Scan = comment;
      BreakCandidate = NULL;
      while ( *Scan && *Scan != '\n' && AvailSpace > 0 ) {
        ScanChar = g_utf8_get_char(Scan);
        /* We known, that g_unichar_isspace() is not recommended for word breaking;
         * but Pango usage seems too complex.
         */
        if ( g_unichar_isspace(ScanChar) )
          BreakCandidate = Scan;
        AvailSpace--; /* not valid for nonspacing marks */
        Scan = g_utf8_next_char(Scan);
      }
      if ( AvailSpace==0 && BreakCandidate != NULL )
        Scan = BreakCandidate;
      if ( AddNL ){
        strcat(WrappedComment, "\n");
        *NumberOfLines+=1;
      }
      AddNL = TRUE;
      strncat(WrappedComment, comment, Scan-comment);
        AvailSpace = WorkingWrapPoint;
      comment = Scan;
    }
  }
  if (tagging)
    strcat(WrappedComment, "}");

  g_return_val_if_fail (strlen (WrappedComment) <= MaxCookedLength, NULL);

  return WrappedComment;
}


/**
 * Compute the dimension of the box surrounding the table's name and its
 * comment if any and if it is visible, store it (the height) in the
 * passed table structure and return the width of the namebox. This
 * function makes use of the fonts defined in the passed table
 * structure, so be sure to initialize them before calling this routine.
 */
static double
table_calculate_namebox_data (Table * table)
{
  double maxwidth = 0.0;

  if (IS_NOT_EMPTY (table->name)) {
    maxwidth = dia_font_string_width (table->name,
                                      table->name_font,
                                      table->name_font_height);
  }
  table->namebox_height = table->name_font_height + 2*0.1;

  if (table->visible_comment && IS_NOT_EMPTY (table->comment)) {
    double width;
    int numOfCommentLines = 0;
    char * wrapped_box = create_documentation_tag (table->comment,
                                                   table->tagging_comment,
                                                   TABLE_COMMENT_MAXWIDTH,
                                                   &numOfCommentLines);
    width = dia_font_string_width (wrapped_box,
                                   table->comment_font,
                                   table->comment_font_height);
    g_clear_pointer (&wrapped_box, g_free);
    table->namebox_height += table->comment_font_height * numOfCommentLines;
    maxwidth = MAX(width, maxwidth);
  }

  return maxwidth;
}


static void
table_update_positions (Table *table)
{
  ConnectionPoint * connections = table->connections;
  Element * elem = &table->element;
  GList * list;
  double x, y;
  real pointspacing;
  gint i;
  gint pointswide;
  gint southWestIndex;
  real attr_font_height;

  x = elem->corner.x;
  y = elem->corner.y;

  /* north-west */
  connpoint_update (&connections[0], x, y, DIR_NORTHWEST);

  /* dynamic number of connection points between north-west and north-east */
  pointswide = (TABLE_CONNECTIONPOINTS - 6) / 2;
  pointspacing = elem->width / (pointswide + 1.0);

  for (i = 1; i <= pointswide; i++) {
    connpoint_update (&connections[i], x + (pointspacing * i), y,
                      DIR_NORTH);
  }

  /* north-east */
  i = (TABLE_CONNECTIONPOINTS / 2) - 2;
  connpoint_update (&connections[i], x + elem->width, y,
                    DIR_NORTHEAST);

  /* west */
  i = (TABLE_CONNECTIONPOINTS / 2) - 1;
  connpoint_update (&connections[i], x, y + table->namebox_height / 2.0,
                    DIR_WEST);

  /* east */
  i = (TABLE_CONNECTIONPOINTS / 2);
  connpoint_update (&connections[i],
                    x + elem->width,
                    y + table->namebox_height / 2.0,
                    DIR_EAST);

  /* south west */
  southWestIndex = i = (TABLE_CONNECTIONPOINTS / 2) + 1;
  connpoint_update (&connections[i], x, y + elem->height,
                    DIR_SOUTHWEST);

  /* dynamic number of connection points between south-west and south-east */
  for (i = 1; i <= pointswide; i++) {
    connpoint_update (&connections[southWestIndex + i],
                      x + (pointspacing * i),
                      y + elem->height,
                      DIR_SOUTH);
  }

  /* south east */
  i = (TABLE_CONNECTIONPOINTS - 1);
  connpoint_update (&connections[i], x + elem->width, y + elem->height,
                    DIR_SOUTHEAST);

  y += table->namebox_height + 0.1 + table->normal_font_height/2;

  list = table->attributes;
  while (list != NULL)
    {
      TableAttribute * attr = (TableAttribute *) list->data;

      attr_font_height = (attr->primary_key == TRUE)
        ? table->primary_key_font_height
        : table->normal_font_height;

      if (attr->left_connection != NULL)
        connpoint_update (attr->left_connection, x, y, DIR_WEST);
      if (attr->right_connection != NULL)
        connpoint_update (attr->right_connection, x + elem->width, y, DIR_EAST);

      y += attr_font_height;

      if (table->visible_comment && IS_NOT_EMPTY (attr->comment)) {
        int num_of_lines = 0;
        char * str = create_documentation_tag (attr->comment,
                                               table->tagging_comment,
                                               TABLE_COMMENT_MAXWIDTH,
                                               &num_of_lines);
        y += table->comment_font_height * num_of_lines;
        y += table->comment_font_height / 2.0;
        g_clear_pointer (&str, g_free);
      }

      list = g_list_next (list);
    }

  element_update_boundingbox (elem);
  elem->object.position = elem->corner;
  element_update_handles (elem);
}

/**
 * Fix the object's connectionpoints array to be as large as neccessary to
 * hold the required number of connection points and (re)initialize it. This
 * routine reflects changes to the table->attributes list.
 */
void
table_update_connectionpoints (Table * table)
{
  DiaObject * obj;
  GList * list;
  gint index;
  gint num_connections, num_attrs;

  obj = &table->element.object;
  num_attrs = g_list_length (table->attributes);
  num_connections = TABLE_CONNECTIONPOINTS + 2*num_attrs;

  if (num_connections != obj->num_connections) {
    obj->num_connections = num_connections;
    obj->connections = g_renew (ConnectionPoint *,
                                obj->connections,
                                num_connections);
  }

  list = table->attributes;
  index = TABLE_CONNECTIONPOINTS;
  while (list != NULL)
    {
      TableAttribute * attr = (TableAttribute *) list->data;
      table_attribute_ensure_connection_points (attr, obj);
      obj->connections[index++] = attr->left_connection;
      obj->connections[index++] = attr->right_connection;
      list = g_list_next (list);
    }
}


static DiaMenu *
table_object_menu (DiaObject *obj, Point *p)
{
  table_menu_items[0].active = DIAMENU_ACTIVE|DIAMENU_TOGGLE|
    (((Table *)obj)->visible_comment ? DIAMENU_TOGGLE_ON : 0);

  return &table_menu;
}


static DiaObjectChange *
table_show_comments_cb (DiaObject *obj, Point *pos, gpointer data)
{
  TableState * state;
  Table * table = (Table *) obj;

  state = table_state_new (table);
  table->visible_comment = !table->visible_comment;
  table_compute_width_height (table);
  table_update_positions (table);

  return table_change_new (table, state, NULL, NULL, NULL);
}


/**
 * This routine updates the font for primary keys. It depends on
 * `normal_font' and `bold_primary_key' properties of the passed table.
 * This routine should be called when at least one of these properties
 * have been changed.
 */
static void
table_update_primary_key_font (Table * table)
{
  g_clear_object (&table->primary_key_font);

  if (!table->bold_primary_key
      || (DIA_FONT_STYLE_GET_WEIGHT (dia_font_get_style (table->normal_font))
          == DIA_FONT_BOLD)) {
    table->primary_key_font = g_object_ref (table->normal_font);
  } else {
    table->primary_key_font = dia_font_copy (table->normal_font);
    dia_font_set_weight (table->primary_key_font, DIA_FONT_BOLD);
  }

  table->primary_key_font_height = table->normal_font_height;
}

static void
table_init_fonts (Table * table)
{
  if (table->normal_font == NULL)
    {
      table->normal_font_height = TABLE_NORMAL_FONT_HEIGHT;
      table->normal_font = dia_font_new_from_style (DIA_FONT_MONOSPACE, TABLE_NORMAL_FONT_HEIGHT);
    }
  if (table->name_font == NULL)
    {
      table->name_font_height = 0.7;
      table->name_font =
        dia_font_new_from_style (DIA_FONT_SANS | DIA_FONT_BOLD, 0.7);
    }
  if (table->comment_font == NULL)
    {
      table->comment_font_height = 0.7;
      table->comment_font =
        dia_font_new_from_style (DIA_FONT_SANS | DIA_FONT_ITALIC, 0.7);
    }
}

/* TableState & TableChange functions ------------------------------------- */

TableState *
table_state_new (Table * table)
{
  TableState * state = g_new0 (TableState, 1);
  GList * list;

  state->name = g_strdup (table->name);
  state->comment = g_strdup (table->comment);
  state->visible_comment = table->visible_comment;
  state->tagging_comment = table->tagging_comment;
  state->underline_primary_key = table->underline_primary_key;
  state->bold_primary_key = table->bold_primary_key;
  state->border_width = table->border_width;

  list = table->attributes;
  while (list != NULL)
    {
      TableAttribute * attr = (TableAttribute *) list->data;
      TableAttribute * copy = table_attribute_copy (attr);

      copy->left_connection = attr->left_connection;
      copy->right_connection = attr->right_connection;

      state->attributes = g_list_append (state->attributes, copy);
      list = g_list_next (list);
    }

  return state;
}

/**
 * Set the values stored in state to the passed table and reinit the table
 * object. The table-state object will be free.
 */
static void
table_state_set (TableState * state, Table * table)
{
  table->name = state->name;
  table->comment = state->comment;
  table->visible_comment = state->visible_comment;
  table->tagging_comment = state->tagging_comment;
  table->border_width = state->border_width;
  table->underline_primary_key = state->underline_primary_key;
  table->bold_primary_key = state->bold_primary_key;
  table->border_width = state->border_width;
  table->attributes = state->attributes;

  g_free (state);

  table_update_connectionpoints (table);
  table_update_primary_key_font (table);
  table_compute_width_height (table);
  table_update_positions (table);
}

static void
table_state_free (TableState * state)
{
  GList * list;

  g_clear_pointer (&state->name, g_free);
  g_clear_pointer (&state->comment, g_free);

  list = state->attributes;
  while (list != NULL)
    {
      TableAttribute * attr = (TableAttribute *) list->data;
      table_attribute_free (attr);
      list = g_list_next (list);
    }
  g_list_free (state->attributes);

  g_free (state);
}


struct _DiaDBTableObjectChange {
  DiaObjectChange obj_change;

  Table *obj;

  GList *added_cp;
  GList *deleted_cp;
  GList *disconnected;

  int applied;

  TableState *saved_state;
};


DIA_DEFINE_OBJECT_CHANGE (DiaDBTableObjectChange, dia_db_table_object_change)


/**
 * Called to UNDO a change on the table object.
 */
static void
dia_db_table_object_change_revert (DiaObjectChange *self, DiaObject *obj)
{
  DiaDBTableObjectChange *change = DIA_DB_TABLE_OBJECT_CHANGE (self);
  TableState *old_state;
  GList *list;

  old_state = table_state_new (change->obj);

  table_state_set (change->saved_state, change->obj);

  list = change->disconnected;
  while (list) {
    Disconnect *dis = (Disconnect *)list->data;

    object_connect (dis->other_object, dis->other_handle, dis->cp);

    list = g_list_next (list);
  }

  change->saved_state = old_state;
  change->applied = FALSE;
}


static void
dia_db_table_object_change_free (DiaObjectChange *self)
{
  DiaDBTableObjectChange *change = DIA_DB_TABLE_OBJECT_CHANGE (self);
  GList * free_list, * lst;

  table_state_free (change->saved_state);

  free_list = (change->applied == TRUE)
    ? change->deleted_cp
    : change->added_cp;

  lst = free_list;
  while (lst) {
    ConnectionPoint * cp = (ConnectionPoint *) lst->data;
    g_assert (cp->connected == NULL);
    object_remove_connections_to (cp);
    g_clear_pointer (&cp, g_free);

    lst = g_list_next (lst);
  }
  g_list_free (free_list);
}


/**
 * Called to REDO a change on the table object.
 */
static void
dia_db_table_object_change_apply (DiaObjectChange *self, DiaObject *obj)
{
  DiaDBTableObjectChange *change = DIA_DB_TABLE_OBJECT_CHANGE (self);
  TableState *old_state;
  GList *lst;

  g_print ("apply (o: 0x%08x) (c: 0x%08x)\n",
           GPOINTER_TO_UINT (obj),
           GPOINTER_TO_UINT (change));

  /* first the get the current state for later use */
  old_state = table_state_new (change->obj);
  /* now apply the change */
  table_state_set (change->saved_state, change->obj);

  lst = change->disconnected;
  while (lst) {
    Disconnect * dis = (Disconnect *) lst->data;
    object_unconnect (dis->other_object, dis->other_handle);
    lst = g_list_next (lst);
  }

  change->saved_state = old_state;
  change->applied = TRUE;
}


static DiaObjectChange *
table_change_new (Table       *table,
                  TableState *saved_state,
                  GList      *added,
                  GList      *deleted,
                  GList      *disconnects)
{
  DiaDBTableObjectChange * change;

  change = dia_object_change_new (DIA_DB_TYPE_TABLE_OBJECT_CHANGE);

  change->obj = table;
  change->added_cp = added;
  change->deleted_cp = deleted;
  change->disconnected = disconnects;
  change->applied = TRUE;
  change->saved_state = saved_state;

  return DIA_OBJECT_CHANGE (change);
}