File: gui.tcl

package info (click to toggle)
ferret 0.7-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,820 kB
  • ctags: 1,494
  • sloc: tcl: 22,364; makefile: 112
file content (3481 lines) | stat: -rw-r--r-- 86,459 bytes parent folder | download
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
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
;# gui.tcl -- Gerwin
;#
;# GUI implementation
;#
;# Copyright (C) 2003-2008 Jose E. Marchesi
;#
;# Time-stamp: "08/11/16 19:23:34 jemarch"

;# 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 3 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, see
;# <http://www.gnu.org/licenses/>.


;#
;# gui_create_fonts 
;#
;# Creates the default fonts for Gerwin

proc gui_create_fonts {} {

    global gfonts


    ;# Default application font
    set gfonts(default) \
	[font create -family Helvetica -size 11]


    ;# Fonts for the ER Diagram
    set gfonts(ercanvas_default) \
	[font create -family Courier -size 8]
    set gfonts(ercanvas_titles) \
	[font create -family Courier -size 11]
    set gfonts(ercanvas_minilabels) \
	[font create -family Times -size 8]
}


;# 
;# gui_gcanvas_make_zoom CANVAS FACTOR
;#
;# Makes a zoom on CANVAS, by FACTOR
;# Manages fonts sizes
;#
;# CANVAS can be er or td

proc gui_gcanvas_make_zoom {what_canvas factor} {
    
    global gcanvas
    global gcanvas_td
    global gfonts

    switch $what_canvas {

	er {

	    ;# First of all, scale the canvas itself
	    ${gcanvas} scale all 0.0 0.0 $factor $factor

	    ;# Scale the fonts 
	    set fontfactor [expr ([font actual $gfonts(ercanvas_default) -size] \
				 * $factor)]

	    regsub {([0-9]+)\.[0-9]} $fontfactor "\\1" fontfactor
	    
	    font configure $gfonts(ercanvas_default) \
		-size $fontfactor

	    set fontfactor [expr ([font actual $gfonts(ercanvas_titles) -size] \
				 * $factor)]

	    regsub {([0-9]+)\.[0-9]} $fontfactor "\\1" fontfactor
	    
	    font configure $gfonts(ercanvas_titles) \
		-size $fontfactor

	    ;# Reconfigure all entities and relations dimensions
	    
	}

	td {


	}

    }
    

}



;#
;# gui_init WIDGET
;#
;# Initializes the gerwin widgets, into WIDGET

proc gui_init {widget} {

    global fontselwidget
    global gmainframe
    global gcanvas
    global gerwin_version
    global gerwin_editionarea
    global apparea

    set gmainframe ${widget}
    
    ;# Title
    wm title . "Ferret $gerwin_version"

    ;# Create the fonts
    gui_create_fonts

    ;# Make the mainframe
    MainFrame ${gmainframe} -separator both \
	-textvariable gerwin_state -menu [gui_create_mainmenu]

    ${gmainframe} addindicator -text "GNU Ferret"
    ${gmainframe} addindicator -text $gerwin_version

    ;# Create the button-area
    set fbarea [${gmainframe} addtoolbar]
    gui_create_button_area $fbarea


    set userframe [${gmainframe} getframe]


    ;# Create the app area
    gui_create_app_area ${userframe}.apparea



    ;# Create the edition area
    ;# The edition area is a notebook widget, that contain frames.
    ;# The first frame is that one with the project properties
    
    ;#set gerwin_editionarea ${userframe}.editionarea
    ;#gui_create_edition_area ${userframe}.editionarea    

    ;# Launch the bindings
    ;#gui_launch_bindings

    ;#pack ${userframe}.editionarea -side top -fill x
    pack ${gmainframe} -fill both -expand true

    ;# Now there is not any project opened, so disable all the buttons
    ;# except the one with new-project and the one with load-project
    gui_no_project_opened

}


;#
;# gui_create_app_area WIDGET
;#
;# Create the app area with all the pages

proc gui_create_app_area {widget} {

    global gmainframe
    global apparea
    global apptree


    set userframe [${gmainframe} getframe]

    ;# Create the apptree
    ;# gui_create_apptree ${userframe}.apptree

    ;# Create the notebook
    NoteBook ${userframe}.apparea
    set apparea ${userframe}.apparea

    ;# Pack the apptree
    ;# pack ${apptree} -fill y -side left

    ;# Pack the apparea
    pack ${apparea} -fill both -expand true -side right

}


;#
;# gui_create_apptree PATH
;#
;# Create the apptree at PATH 
;#

proc gui_create_apptree {path} {
    
    global apptree

    set apptree $path

    ;# Create the Tree widget
    Tree ${path} -showlines true -dragenabled false

    ;##############################
    ;# Create the application nodes
    ;##############################

    ;# Gerwin -> Settings at right
    gui_apptree_insert 0 root Gerwin

}

;#
;# gui_apptree_insert INDEX PARENT NODE
;#
;# Insert NODe into the apptree

proc gui_apptree_insert {index parent node} {

    global apptree
    global gfonts

    ${apptree} insert $index $parent apptree-{$node} \
	-text $node -font $gfonts(default) -image [Bitmap::get save]


}


;#
;# gui_app_area_raise_Project_page 
;#
;# Raises the project page on the app area

proc gui_app_area_raise_Project_page {} {

    global apparea

    ${apparea} raise Project


}

;#
;# gui_app_area_destroy_ER_page
;#
;# Destroys the app area TD page

proc gui_app_area_destroy_ER_page {} {

    global apparea

    ${apparea} delete ER


}


;#
;# gui_app_area_destroy_TD_page
;#
;# Destroys the app area TD page

proc gui_app_area_destroy_TD_page {} {

    global apparea

    ${apparea} delete TD


}

;#
;# gui_app_area_destroy_Output_page
;#
;# Destroys the app area Output page

proc gui_app_area_destroy_Output_page {} {

    global apparea

    ${apparea} delete Output


}

;#
;# gui_app_area_destroy_Project_page
;#
;# Destroys the app area Project page

proc gui_app_area_destroy_Project_page {} {

    global apparea

    ${apparea} delete Project

}

;#
;# gui_app_area_create_Project_page 
;#
;# Creates the app area Project page, with page information

proc gui_app_area_create_Project_page {} {

    global apparea

    global gerwin_cproject_name
    global gerwin_cproject_file
    global gerwin_cproject_author

    ;# Create the new page, if it is not created
    ${apparea} insert 0 Project -text "Project"
    ${apparea} raise Project

    ;# Put here the project attributes
    set widget [${apparea} getframe Project]

    TitleFrame ${widget}.editframe -side center -text "Project Properties"

    set editwidget [${widget}.editframe getframe]
    
    ;# Project name 
    frame ${editwidget}.nameline 
    LabelEntry ${editwidget}.nameline.label -label "Project Name:" -labelwidth 16 \
	-textvariable gerwin_cproject_name \
	-helptext "Name of the current project" -width 50

    ;# Project file
    frame ${editwidget}.fileline
    LabelEntry ${editwidget}.fileline.label -label "Project File:" -labelwidth 16 \
	-textvariable gerwin_cproject_file \
	-helptext "Storage file of the current project" -editable false \
	-width 50

    Button ${editwidget}.fileline.button -text "select file" \
	-helptext "Select a file to store the project in" \
	-command "set tempfile \[gui_select_file_to_save\] ; \

                  if {\$tempfile != {}} then {
                      set gerwin_cproject_file \$tempfile
                      gerwin_save_project_file
                  } " \
	-relief groove


    ;# Project author
    frame ${editwidget}.authorline
    LabelEntry ${editwidget}.authorline.label -label "Project Author:" -labelwidth 16 \
	-textvariable gerwin_cproject_author \
	-helptext "Author of the current project" -width 50
    
    pack ${editwidget}.nameline.label -side left -anchor w
    pack ${editwidget}.nameline -side top -anchor w

    pack ${editwidget}.fileline.label -side left -anchor w
    pack ${editwidget}.fileline.button -side left
    pack ${editwidget}.fileline -side top -anchor w

    pack ${editwidget}.authorline.label -side left -anchor w
    pack ${editwidget}.authorline -side top -anchor w

    pack ${editwidget} -side left

    pack ${widget}.editframe -side left -fill both -expand true
    
    ;# Scale the notebook
    NoteBook::compute_size ${apparea}


}

;#
;# gui_app_area_raise_ER_page
;#
;# Raises the ER page

proc gui_app_area_raise_ER_page {} {

    global apparea

    ${apparea} raise ER

}

;#
;# gui_app_area_raise_TD_page
;#
;# Raises the TD page

proc gui_app_area_raise_TD_page {} {

    global apparea

    ${apparea} raise TD


}



;#
;# gui_app_area_raise_Output_page
;#
;# Raises the Output page

proc gui_app_area_raise_Output_page {} {

    global apparea

    ${apparea} raise Output

}


;#
;# gui_app_area_create_Domain_page
;#
;# Create the app area Domain page, with the Domain Manager

proc gui_app_area_create_Domain_page {} {

    global apparea
    global gcanvas

    ;# Create the new page
    ${apparea} insert 0 Domain -text "Domains and Types"


    ;# Include here the new 

}


;#
;# gui_app_area_destroy_Domain_page
;#
;# Delete the Domain page from the app area, and destroy its contents

proc gui_app_area_destroy_Domain_page {} {

    global apparea
    global gerwin_domains
    global gerwin_domain

    ;# Delete the page and destroy the contents
    ${apparea} delete Domain

    ;# Purge all domains on the system
    foreach d $gerwin_domains {

	gob_domain_delete $d

    }
    set gerwin_domains {}

}

;#
;# gui_app_area_create_ER_page
;# 
;# Create the app area ER page

proc gui_app_area_create_ER_page {} {

    global apparea
    global gcanvas
    global gerwin_editionarea
    
    ;# Buttons globals
    global gui_button_bbox_elements
    global gui_button_bbox_tools
    global gui_button_bbox_print_ER
    global gui_button_bbox_zoom_ER


    ;# Create the new page
    ${apparea} insert end ER -text "ER Diagram"

    ;# Now, the scrolled canvas and the editionarea into two paned windows
    set pw [${apparea} getframe ER].pw
    PanedWindow $pw -side left -weights extra

    ;# Frame for the ER toolbar and scrolled canvas
    ;# Create the toolbar for ER
    set erframe [${pw} add]
    frame ${erframe}.toolbar
    set bbox [ButtonBox ${erframe}.toolbar.tb2 -spacing 0 -padx 1 -pady 1]
    set gui_button_bbox_elements $bbox

    $bbox add -text entity \
        -helptype balloon -helptext "Insert a new entity object" \
	-relief groove \
	-command {gerwin_set_state "Inserting entity"}

    $bbox add -text relation \
	-helptype balloon -helptext "Insert a new relation object" \
	-relief groove \
	-command {gerwin_set_state "Inserting relation begin"}

    Separator ${erframe}.toolbar.sep2 -orient vertical


    set bbox [ButtonBox ${erframe}.toolbar.tb3 -spacing 0 -padx 1 -pady 1]
    set gui_button_bbox_tools $bbox

    $bbox add -text move \
	-helptype balloon -helptext "Move an object" \
	-relief groove \
	-command {gerwin_set_state "Moving"}

    $bbox add -text edit \
	-helptype balloon -helptext "Edit the properties of an object" \
	-relief groove \
	-command {gerwin_set_state "Editing"}

    $bbox add -text delete \
	-helptype balloon -helptext "Delete an object" \
	-command {gerwin_set_state "Deleting"} -relief groove
	;#-image [Bitmap::get cut] -relief groove \

    Separator  ${erframe}.toolbar.sep1 -orient vertical

    set bbox [ButtonBox ${erframe}.toolbar.tb7 -spacing 0 -padx 1 -pady 1]
    set gui_button_bbox_print_ER $bbox

    $bbox add -text "print" -helptype balloon \
	-helptext "Print the ER diagram" \
	-relief groove -command gerwin_print_ER

#     set bbox [ButtonBox ${erframe}.toolbar.tb8 -spacing 0 -padx 1 -pady 1]
#     set gui_button_bbox_zoom_ER $bbox

#     $bbox add -text "+" -helptype balloon \
# 	-helptext "Positive zoom" \
# 	-relief groove -command [list gui_gcanvas_make_zoom er 1.1]
#     $bbox add -text "-" -helptype balloon \
# 	-helptext "Negative zoom" \
# 	-relief groove -command [list gui_gcanvas_make_zoom er 0.9]


#     Separator ${erframe}.toolbar.sep3 -orient vertical

    pack ${erframe}.toolbar.tb2 -side left -anchor w
    pack ${erframe}.toolbar.sep2 -side left -padx 4 -fill y
    pack ${erframe}.toolbar.tb3 -side left -anchor w
    pack ${erframe}.toolbar.sep1 -side left -padx 4 -fill y
    pack ${erframe}.toolbar.tb7 -side left -anchor w
#     pack ${erframe}.toolbar.sep3 -side left -padx 4 -fill y
#     pack ${erframe}.toolbar.tb8 -side left -anchor w

    gui_create_scrolled_canvas [frame ${erframe}.fcanvas] ;#-bg white
    set gcanvas ${erframe}.fcanvas.canvas

    pack ${erframe}.toolbar -side top -fill x
    pack ${erframe}.fcanvas -side top -fill both -expand true

    ;# Set the canvas enter and leave bindings
    bind ${gcanvas} <Enter> gui_enter_canvas_bind
    bind ${gcanvas} <Leave> gui_leave_canvas_bind

    ;# Set the canvas dimensions bindings
    bind . <Key-Right> gui_expand_canvas_to_right
    bind . <Key-Down> gui_expand_canvas_to_down
    bind . <Key-Left> gui_shrink_canvas_to_left
    bind . <Key-Up> gui_shrink_canvas_to_up

    ;# Create the editionarea
    set gerwin_editionarea [${pw} add].editionarea
    gui_create_edition_area ${gerwin_editionarea}
    

    ;# Pack it
    pack ${gerwin_editionarea} -side bottom -fill x

    ;# Pack it
    pack ${erframe} -fill both -expand true
    pack ${pw} -fill both -expand true

    ;# Update the notebook size
    NoteBook::compute_size ${apparea}
}

;#
;# gui_app_area_create_TD_page
;#
;# Create the app area TD page

proc gui_app_area_create_TD_page {} {

    global apparea
    global gcanvas_td

    ;# Buttons globals
    global gui_button_bbox_print_TD
    global gui_button_bbox_gen_TD

    ;# Create the new page
    ${apparea} insert end TD -text "Table Diagram"

    set bbutton_frame [frame [${apparea} getframe TD].toolbar]

    set bbox [ButtonBox ${bbutton_frame}.tb1 -spacing 0 -padx 1 -pady 1]
    set gui_button_bbox_gen_TD $bbox

    $bbox add -text "Update" \
	-helptype balloon -helptext "Generate the table diagram" \
	-relief groove -command {gen_ER_to_TD}

    Separator  ${bbutton_frame}.sep1 -orient vertical

    set bbox [ButtonBox ${bbutton_frame}.tb2 -spacing 0 -padx 1 -pady 1]
    set gui_button_bbox_print_TD $bbox

    $bbox add -text "print" -helptype balloon \
	-helptext "Print the Table diagram" \
	-relief groove -command gerwin_print_TD

    pack ${bbutton_frame}.tb1 -side left -anchor w
    pack ${bbutton_frame}.sep1 -side left -padx 4 -fill y
    pack ${bbutton_frame}.tb2 -side left -anchor w


    set erframe [frame [${apparea} getframe TD].fcanvas]
    gui_create_scrolled_canvas ${erframe}

    set gcanvas_td ${erframe}.canvas

    ;# Set the canvas enter and leave bindings
    bind ${gcanvas_td} <Enter> {. configure -cursor fleur}
    bind ${gcanvas_td} <Leave> {. configure -cursor ""}

    ;# Set the bind for marking
    ${gcanvas_td} bind all <Button-1> {gui_mark_td %x %y}

    ;# Pack it 
    pack ${bbutton_frame} -fill x
    pack ${erframe} -fill both -expand true

}

;#
;# gui_app_area_create_Output_page 
;#
;# Create the app area Output page

proc gui_app_area_create_Output_page {} {

    global apparea
    global gtext_output
    global gerwin_output_formats
    global gerwin_output_active_format
    global output_pages
    global output_pages_widget

    ;# Buttons globals
    global gui_button_bbox_gen_output

    ;# Create the new page
    ${apparea} insert end Output -text "Output"

    ;# Create the toolbar
    set bbutton_frame [frame [${apparea} getframe Output].toolbar]
    set bbox [ButtonBox ${bbutton_frame}.tb1 -spacing 0 -padx 1 -pady 1]
    set gui_button_bbox_gen_output $bbox

    $bbox add -text "Create output" \
	-helptype balloon -helptext "Generate the selected output format" \
	-relief groove -command "gen_TD_to_Output \$gerwin_output_active_format"


    ;# Replace it with a ComboBox
    Label ${bbutton_frame}.label -text "output format: "
    ComboBox ${bbutton_frame}.formats -values $gerwin_output_formats -expand tab \
	-editable false \
	-modifycmd "set gerwin_output_active_format \[lindex \$gerwin_output_formats \[${bbutton_frame}.formats getvalue\]\]"


    pack ${bbutton_frame}.label -side left -anchor w
    pack ${bbutton_frame}.formats -side left 
    pack ${bbutton_frame}.tb1 -side left -anchor w -padx 4

    ;# Now, create the notebook with the several output gtexts
    set outputs [NoteBook [${apparea} getframe Output].nb]
    set output_pages_widget $outputs
    
    ;# Pack it
    pack ${bbutton_frame} -fill x
    pack ${outputs} -fill both -expand true
    ;#pack ${erframe} -fill both -expand true
}


;#
;# gui_destroy_output_page FORMAT
;#
;# Destroy the FORMAT output page

proc gui_destroy_output_page {format} {

    global output_pages
    global output_pages_widget
    global gtext_output

    ;# First of all, clean output_pages and gtext_output(FORMAT)
    set index [lsearch $output_pages $format]
    set output_pages [lreplace $output_pages $index $index]

    unset gtext_output($format)

    ;# Ok, then destroy the widget
    ${output_pages_widget} delete $format


}

;#
;# gui_create_output_page FORMAT
;#
;# Creates a new output page for FORMAT on the output_pages_widget notebook

proc gui_create_output_page {format} {

    global output_pages
    global output_pages_widget
    global gtext_output
    global gerwin_output_active_format


    ;# First of all, update output_pages
    lappend output_pages $format

    ;# Second, create the page into the notebook
    ${output_pages_widget} insert end $format -text $format

    ;# Good. Next, create the button bar
    set toolbar [frame [${output_pages_widget} getframe $format].toolbar]    

    ;# Update button
    set bbox [ButtonBox ${toolbar}.tb3 -spacing 0 -padx 1 -pady 1]
    
    $bbox add -text update \
	-helptype balloon -helptext "Update this output from the tables" \
	-relief groove \
	-command "set ftemp \$gerwin_output_active_format 
                  set gerwin_output_active_format $format
                  gen_TD_to_Output $format
                  set gerwin_output_active_format \$ftemp"

    Separator ${toolbar}.sep3 -orient vertical

    ;# Save and print buttons
    set bbox [ButtonBox ${toolbar}.tb1 -spacing 0 -padx 1 -pady 1]

    $bbox add -text save \
        -helptype balloon -helptext "Save this output to a file" \
	-relief groove \
	-command [list gerwin_save_output_file $format]

#     $bbox add -text print \
#         -helptype balloon -helptext "Print this output to a printer" \
# 	-relief groove \
# 	-command [list gerwin_print_output $format]
    
    ;# Close page button
    set bbox [ButtonBox ${toolbar}.tb2 -spacing 0 -padx 1 -pady 1]

    $bbox add -text close \
        -helptype balloon -helptext "Close this output page" \
	-relief groove \
	-command [list gui_destroy_output_page $format]


    pack ${toolbar}.tb3 -side left -anchor w
    pack ${toolbar}.sep3 -side left
    pack ${toolbar}.tb1 -side left -anchor w
    pack ${toolbar}.tb2 -side right -anchor w

    ;# Now create the scrolled text
    set stext [frame [${output_pages_widget} getframe $format].stext]

    gui_create_scrolled_text ${stext}

    ;# Pack it
    pack ${toolbar} -side top -fill x
    pack ${stext} -side top -fill both -expand true

    ;# Finally, raise the page and update gtext_output
    ${output_pages_widget} raise $format
    set gtext_output($format) ${stext}.text
    
    ;# Put the background
    $gtext_output($format) configure -bg white

    ;# Make the text widget not editable
    $gtext_output($format) configure -state disabled
}


;#
;# gui_enter_canvas_bind 
;#

proc gui_enter_canvas_bind {} {
    
    global gerwin_state

    ;# Change the cursor 
    switch $gerwin_state {

	"Inserting entity" {

	    . configure -cursor crosshair

	}
	"Inserting relation begin" {

	    . configure -cursor left_side

	}

	"Inserting relation end" {
	    
	    . configure -cursor right_side

	}

	Deleting {
	    
	    . configure -cursor pirate

	}
	
	Moving {

	    . configure -cursor fleur

	}

	Editing {

	    . configure -cursor hand1
	}

	
	default {
	    
	    . configure -cursor ""
	}

    }


}

;#
;# gui_leave_canvas_bind 
;#

proc gui_leave_canvas_bind {} {

    . configure -cursor ""

}



;#
;# gui_update_font FONT
;#
;# Updates the application global font

proc gui_update_font {newfont} {

    global gui_font
    global fontselwidget
    global gmainframe

    . configure -cursor watch 

    ;# Update the fonts of all widgets
    if { $gui_font != $newfont } then {
	
	$fontselwidget configure -font $newfont
	set gui_font $newfont
    }

    . configure -cursor ""

}

;# 
;# gui_create_edition_area WIDGET
;#
;# Creates the edition area

proc gui_create_edition_area {widget} {

    ;# Create the notebook
    NoteBook ${widget}

}




;#
;# gui_create_mainmenu 
;#
;# Create the main menu string

proc gui_create_mainmenu {} {


    ;# Prepare the menu
    set mainmenu {
	"&Ferret" {} {} 0 {
	    {separator}
	    {command "&Exit Ferret" {} "exit the application" {Ctrl e} -command gerwin_quit}
	}
         
	"&Project" {} {} 0 {
	    {command "&New Project" {} "new project" {Ctrl n} -command gerwin_new_project}
	    {command "&Open Project" {} "open a new project" {Ctrl o} -command gerwin_open_project}
	    {command "&Save Project" {} "save the current project" {Ctrl s} -command gerwin_save_project}
	    {command "&Close Project" {} "close the current project" {Ctrl c} -command gerwin_close_project}
	}
	
    }

    return $mainmenu

}

;#
;# gui_create_button_area WIDGET
;#
;# Create the button area widget
;#

proc gui_create_button_area {widget} {

    global gui_button_bbox_project

    global gerwin_output_formats

    ;# Project area
    set bbox [ButtonBox ${widget}.tb1 -spacing 0 -padx 1 -pady 1]
    set gui_button_bbox_project $bbox
    
    $bbox add -text "new" -helptype balloon \
	-helptext "New project" \
	-relief groove \
	-command "gerwin_new_project"

    $bbox add -text "open" -helptype balloon \
	-helptext "Open a project" \
	-relief groove \
	-command "gerwin_open_project"

    $bbox add -text "save" -helptype balloon \
	-helptext "Save the current project" \
	-relief groove \
	-command "gerwin_save_project"


    pack ${widget}.tb1 -side left -anchor w 


#     set bbox [ButtonBox ${widget}.tb5 -spacing 0 -padx 1 -pady 1]
#     set gui_button_bbox_ssql $bbox

#     $bbox add -text "save sql" \
# 	-helptype balloon -helptext "Saves the SQL list" \
# 	-relief groove -command {gerwin_save_sql}

}

;#
;# gui_create_scrolled_text FRAME ARGS
;#
;# Create a scrolled text widget

proc gui_create_scrolled_text {widget args} {


    ;# Create the text widget
    eval {text ${widget}.text \
	      -xscrollcommand [list ${widget}.xscroll set] \
	      -yscrollcommand [list ${widget}.yscroll set] \
	      -borderwidth 0} $args

    ;# Create the scroll widgets
    scrollbar ${widget}.xscroll -orient horizontal \
	-command [list ${widget}.text xview]
    scrollbar ${widget}.yscroll -orient vertical \
	-command [list ${widget}.text yview]
    
    ;# Pack the widgets
    grid ${widget}.text ${widget}.yscroll -sticky news
    grid ${widget}.xscroll -sticky ew
    grid rowconfigure ${widget} 0 -weight 1
    grid columnconfigure ${widget} 0 -weight 1

}


;#
;# gui_shrink_canvas_to_left
;#

proc gui_shrink_canvas_to_left {} {

    global gcanvas

    set sarea [${gcanvas} cget -scrollregion]

    ${gcanvas} configure -scrollregion \
	[list [lindex $sarea 0] [lindex $sarea 1] \
	     [expr [lindex $sarea 2] - 100] [lindex $sarea 3]]


}



;#
;# gui_expand_canvas_to_right 
;#

proc gui_expand_canvas_to_right {} {

    global gcanvas

    set sarea [${gcanvas} cget -scrollregion]

    ${gcanvas} configure -scrollregion \
	[list [lindex $sarea 0] [lindex $sarea 1] \
	     [expr [lindex $sarea 2] + 100] [lindex $sarea 3]]


}


;#
;# gui_shrink_canvas_to_up
;#

proc gui_shrink_canvas_to_up {} {

    global gcanvas
 
    set sarea [${gcanvas} cget -scrollregion]

    ${gcanvas} configure -scrollregion \
	[list [lindex $sarea 0] [lindex $sarea 1] \
	     [lindex $sarea 2] [expr [lindex $sarea 3] - 100]]


}


;#
;# gui_expand_canvas_to_down
;#

proc gui_expand_canvas_to_down {} {

    global gcanvas
 
    set sarea [${gcanvas} cget -scrollregion]

    ${gcanvas} configure -scrollregion \
	[list [lindex $sarea 0] [lindex $sarea 1] \
	     [lindex $sarea 2] [expr [lindex $sarea 3] + 100]]


}

;#
;# gui_create_scrolled_canvas FRAME ARGS
;#
;# Create a scrolled canvas widget

proc gui_create_scrolled_canvas { widget args } {


    ;# Create the canvas widget
    eval {canvas ${widget}.canvas \
	      -xscrollcommand [list ${widget}.xscroll set] \
	      -yscrollcommand [list ${widget}.yscroll set] \
	      -highlightthickness 0 \
	      -borderwidth 0 -bg white} $args

    ;# Create the scroll widgets
    scrollbar ${widget}.xscroll -orient horizontal \
	-command [list ${widget}.canvas xview] 
    scrollbar ${widget}.yscroll -orient vertical \
	-command [list ${widget}.canvas yview]

    ;# Pack the widgets (with grid)
    grid ${widget}.canvas ${widget}.yscroll -sticky news
    grid ${widget}.xscroll -sticky ew
    grid rowconfigure ${widget} 0 -weight 1
    grid columnconfigure ${widget} 0 -weight 1

}


;#
;# gui_change_state TEXT
;#
;# Change the state to TEXT
;# Now this is a nop

proc gui_change_state {text} {


}

;#
;# gui_yes_no_cancel TEXT
;#

proc gui_yes_no_cancel {text} {

    global gmainframe

    MessageDlg ${gmainframe}.notice -title "I am asking you, dude!" -justify left -icon info \
	-message $text -type yesnocancel

}


;# 
;# gui_notice TEXT
;#
;# Notice about something.

proc gui_notice {text} {

    global gmainframe

    MessageDlg ${gmainframe}.notice -title "Notice" -justify left -icon info \
	-message $text -type ok
}

;# 
;# gui_fatal TEXT
;#
;# Notice a fatal error and exit

proc gui_fatal {text} {

    global gmainframe

    toplevel ${gmainframe}.fatal -bg red

    label ${gmainframe}.fatal.label -text "Fatal Error: $text" \
	-bg red -fg white
    button ${gmainframe}.fatal.button -text "Quit Gerwin" \
	-command {exit}

    pack ${gmainframe}.fatal.label -side top
    pack ${gmainframe}.fatal.button -side top

    focus ${gmainframe}.fatal
}


;#
;# gui_project_opened
;#
;# Enables some buttons and canvas and text backgrounds

proc gui_project_opened {} {

    global gui_button_bbox_project
    global gui_button_bbox_elements
    global gui_button_bbox_tools
    global gui_button_bbox_gen_TD
    global gui_button_bbox_gen_output
    global gui_button_bbox_gen_GerwinML
    global gui_button_bbox_ssql
    global gui_button_bbox_print_ER
    global gui_button_bbox_print_TD

    global gcanvas
    global gcanvas_td
    global gtext_sql

    ;# Enable some buttons

#     $gui_button_bbox_project itemconfigure 0 -state active ;# New Project
#     $gui_button_bbox_project itemconfigure 1 -state active ;# Open Project
     $gui_button_bbox_project itemconfigure 2 -state active ;# Save project
#     ;#$gui_button_bbox_project itemconfigure 3 -state active ;# Close project
    
#     $gui_button_bbox_elements itemconfigure 0 -state active ;# Insert entity
#     $gui_button_bbox_elements itemconfigure 1 -state active ;# Insert relation
#     ;#$gui_button_bbox_elements itemconfigure 2 -state active ;# Insert label

#     $gui_button_bbox_tools itemconfigure  0 -state active ;# Move
#     $gui_button_bbox_tools itemconfigure  1 -state active ;# Edit properties
#     $gui_button_bbox_tools itemconfigure  2 -state active ;# Delete

#     $gui_button_bbox_gen_TD itemconfigure 0 -state active ;# Generate tables
#     $gui_button_bbox_gen_output itemconfigure 0 -state active ;# Generate Output
    
#     ;#$gui_button_bbox_ssql itemconfigure 0 -state active ;# Save SQL

#     $gui_button_bbox_print_ER itemconfigure 0 -state active ;# Print ER
#     $gui_button_bbox_print_TD itemconfigure 0 -state active ;# Print TD

#     ;# Change some backgrounds
#     ${gcanvas} configure -bg white
#     ${gcanvas_td} configure -bg white


    ;# Create the app area pages
    ;#gui_app_area_create_Domain_page
    gui_app_area_create_Project_page
    gui_app_area_create_ER_page
    gui_app_area_create_TD_page
    gui_app_area_create_Output_page
    
    ;# Launch the bindings
    gui_launch_bindings

    ;# Set the editing state
    gerwin_set_state Editing
}



;#
;# gui_no_project_opened
;#
;# Disables some buttons and the color of some objects

proc gui_no_project_opened {} {

    global gui_button_bbox_project
    global gui_button_bbox_elements
    global gui_button_bbox_tools
    global gui_button_bbox_gen_TD
    global gui_button_bbox_gen_output
    global gui_button_bbox_gen_GerwinML
    global gui_button_bbox_ssql
    global gui_button_bbox_print_ER
    global gui_button_bbox_print_TD

    global gcanvas
    global gcanvas_td
    global gtext_sql

    ;# Disable some buttons

    $gui_button_bbox_project itemconfigure 2 -state disabled ;# Save project
    ;#$gui_button_bbox_project itemconfigure 3 -state disabled ;# Close project
    
#     $gui_button_bbox_elements itemconfigure 0 -state disabled ;# Insert entity
#     $gui_button_bbox_elements itemconfigure 1 -state disabled ;# Insert relation
    ;#$gui_button_bbox_elements itemconfigure 2 -state disabled ;# Insert label

#     $gui_button_bbox_tools itemconfigure  0 -state disabled ;# Move
#     $gui_button_bbox_tools itemconfigure  1 -state disabled ;# Edit properties
#     $gui_button_bbox_tools itemconfigure  2 -state disabled ;# Delete

#     $gui_button_bbox_gen_TD itemconfigure 0 -state disabled ;# Gen TD
#     $gui_button_bbox_gen_output itemconfigure 0 -state disabled  ;# Gen Output

#     ;#$gui_button_bbox_ssql itemconfigure 0 -state disabled

#     $gui_button_bbox_print_ER itemconfigure 0 -state disabled ;# Print ER
#     $gui_button_bbox_print_TD itemconfigure 0 -state disabled ;# Print TD


#     ;# Change the color of the canvas and text widget to grey
#     ${gcanvas} configure -bg grey
#     ${gcanvas_td} configure -bg grey
    ;#${gtext_sql} configure -bg grey
}

;#
;# gui_launch_bindings
;#
;# Launch the bindings of the canvas

proc gui_launch_bindings {} {

    global gcanvas
    global gmainframe

    set userframe [${gmainframe} getframe]

    ;# The button-1 (mark) binding
    ${gcanvas} bind all <Button-1> {gui_mark %x %y}

}

;#
;# gui_drag XPOS YPOS
;#
;# Event to mouse-drag-1 on the canvas

proc gui_drag {xpos ypos} {

    global gerwin_state
    global gmainframe
    global gerwin_premov_x
    global gerwin_premov_y
    global gerwin_premov_object
    global gerwin_obj_moving
    global gerwin_obj_type_moving
    global gm_entity
    global gm_relation
    global gerwin_relation
    global gcanvas

    ;# See if we are really moving
    if {$gerwin_state != "Moving"} then {
	return
    }

    set userframe [${gmainframe} getframe]

    ;# Map from view coordinates to canvas coordinates
    set xpos [${gcanvas} canvasx $xpos]
    set ypos [${gcanvas} canvasy $ypos]

    
    ;# Move the current object
    set dx [expr $xpos - $gerwin_premov_x]
    set dy [expr $ypos - $gerwin_premov_y]

    ;# Move the widget
    ${gcanvas} move $gerwin_premov_object $dx $dy


    ;# Actualize premov coordinates
    set gerwin_premov_x $xpos
    set gerwin_premov_y $ypos
 
    ;# Actualize the widget coordinates
    switch $gerwin_obj_type_moving {


	"Relation" {
	    set gm_relation($gerwin_obj_moving,xpos) [lindex [${gcanvas} bbox \
								  $gm_relation($gerwin_obj_moving,grouptag)] 0]
	    set gm_relation($gerwin_obj_moving,ypos) [lindex [${gcanvas} bbox \
								  $gm_relation($gerwin_obj_moving,grouptag)] 1]


	    ;# Update the links
	    if {$gerwin_relation($gerwin_obj_moving,reflexive)} then {
		;# It is a reflexive relation
		gm_relation_update_links_reflexive $gerwin_obj_moving
	    } else {
		;# Non-reflexive relation
		gm_relation_update_links $gerwin_obj_moving
	    }
	 
	}

	"Entity" {
	    set gm_entity($gerwin_obj_moving,xpos) [lindex [${gcanvas} bbox \
								     $gm_entity($gerwin_obj_moving,grouptag)] 0]
	    set gm_entity($gerwin_obj_moving,ypos) [lindex [${gcanvas} bbox \
								     $gm_entity($gerwin_obj_moving,grouptag)] 1]

	    ;# Update all the links
	    foreach r [gob_entity_get_relations $gerwin_obj_moving] {

		if {$gerwin_relation($r,reflexive)} then {
		    ;# Reflexive relation
		    gm_relation_update_links_reflexive $r
		} else {
		    gm_relation_update_links $r
		}

	    }
	    
	}

    }
    
}


;#
;# gui_drag_td XPOS YPOS
;#
;# Event to mouse-drag-1 on the TD canvas

proc gui_drag_td {xpos ypos} {

    global gmainframe
    global gerwin_premov_x
    global gerwin_premov_y
    global gerwin_premov_object
    global gerwin_obj_moving
    global gerwin_obj_type_moving
    global gm_table

    global gcanvas_td

    ;# We are always moving here
    ;# (for now :P) [jemarch]

    set userframe [${gmainframe} getframe]

    ;# Map from view coordinates to canvas coordinates
    set xpos [${gcanvas_td} canvasx $xpos]
    set ypos [${gcanvas_td} canvasy $ypos]

    ;# Move the current object
    set dx [expr $xpos - $gerwin_premov_x]
    set dy [expr $ypos - $gerwin_premov_y]

    ;# Actualize premov coordinates
    set gerwin_premov_x $xpos
    set gerwin_premov_y $ypos

    ;# Move the widget
    ${gcanvas_td} move $gerwin_premov_object $dx $dy

    ;# Actualize the coordinates
    switch $gerwin_obj_type_moving {

	"Flkey" {
	    ;# Get the name of the two linked tables
	    regsub {(.+)-([^-]+)} $gerwin_obj_moving "\\1" rtable
	    regsub {(.+)-([^-]+)} $gerwin_obj_moving "\\2" table

	    ;# Update the position of the corresponding flkey
	    gm_flkey_set_xpos $rtable $table [lindex [${gcanvas_td} bbox \
							  "taggroup-Flkey-$rtable-$table"] 0]
	    gm_flkey_set_ypos $rtable $table [lindex [${gcanvas_td} bbox \
							  "taggroup-Flkey-$rtable-$table"] 1]

	    ;# Update the links of the flkey

	    ;# Delete old links
	    foreach t [gob_table_get_foreign_keys $table] {
	
		set rtable [lindex $t 1]
	
		;#${gcanvas_td} delete "taggroup-Flkey-$rtable-$table"
		${gcanvas_td} delete "taggroup-Flkey-link-$rtable-$table"

	    }

	    ;# For each foreign key
	    foreach t [gob_table_get_foreign_keys $table] {

		set nrtable [lindex $t 1]
		set rattribute [lindex $t 0]

		if {$nrtable == $rtable} then {
		    lappend rattributes($rtable) $rattribute
		}

	    }

	    gm_draw_link_td $table $rtable $rattributes($rtable)

	}


	"Table" {



	    ;# Actualize the table coordinates
	    set gm_table($gerwin_obj_moving,xpos) [lindex [${gcanvas_td} bbox \
							       $gm_table($gerwin_obj_moving,grouptag)] 0]
	    set gm_table($gerwin_obj_moving,ypos) [lindex [${gcanvas_td} bbox \
							       $gm_table($gerwin_obj_moving,grouptag)] 1]

	    ;# Update the table links
	    gm_table_update_links $gerwin_obj_moving
	}
    }


}


;# 
;# gui_mark_td XPOS YPOS
;#
;# Evento to mouse-pointer-1 on the td canvas

proc gui_mark_td {xpos ypos} {

    global gmainframe
    global gerwin_tables
    global gerwin_premov_x
    global gerwin_premov_y
    global gerwin_premov_object
    global gerwin_obj_moving
    global gerwin_obj_type_moving
    
    global gcanvas_td

    set userframe [${gmainframe} getframe]

    ;# Get the coords onto the widget
    set xpos [${gcanvas_td} canvasx $xpos]
    set ypos [${gcanvas_td} canvasy $ypos]

    ;# Now, figure out what WGOB we want to delete
    set wgob [${gcanvas_td} find closest $xpos $ypos]

    ;# Get the taggroup from the WGOB
    set wgobtags [${gcanvas_td} gettags $wgob]
    set index [lsearch -glob $wgobtags "taggroup-*"]
    set taggroup [lindex $wgobtags $index]
    
    ;# Get the object name
    regsub {taggroup-([^-]+)-.+} $taggroup "\\1" gm_type

    if {$gm_type == "Flkey"} then {
	
	regsub {taggroup-[^-]+-(.+)} $taggroup "\\1" gm_name

    } else {
	regsub {taggroup-.*-(.+)} $taggroup "\\1" gm_name
    }

    set gerwin_obj_type_moving $gm_type
    set gerwin_obj_moving $gm_name

    set gerwin_premov_x $xpos
    set gerwin_premov_y $ypos
    set gerwin_premov_object $taggroup
}



;#
;# gui_mark XPOS YPOS
;#
;# Event to mouse-pointer-1 on the canvas

proc gui_mark {xpos ypos} {

    global gerwin_state
    global gmainframe
    global gerwin_entities
    global gerwin_relations
    global gerwin_relation
    global gerwin_premov_x
    global gerwin_premov_y
    global gerwin_premov_object
    global gerwin_entity_seq
    global gerwin_relation_seq
    global gerwin_obj_moving
    global gerwin_obj_type_moving
    global gui_entity1
    global gui_entity2
    global gcanvas
    global gm_entity
    global earea_pages


    set userframe [${gmainframe} getframe]

    ;# Switch over the actual state
    switch $gerwin_state {

	"Inserting relation begin" {

	    ;# Get the coords onto the widget
	    set xpos [${gcanvas} canvasx $xpos]
	    set ypos [${gcanvas} canvasy $ypos]

	    ;# Figure out what WGOB is the first entity
	    set wgob [${gcanvas} find closest $xpos $ypos]

	    ;# Get the taggroup tag from the WGOB
	    set wgobtags [${gcanvas} gettags $wgob]
	    set index [lsearch -glob $wgobtags "taggroup-*"]
	    set taggroup [lindex $wgobtags $index]

	    ;# Get the object name and type from the taggroup
	    regsub {taggroup-([^-]+)-.+} $taggroup "\\1" gm_type
	    regsub {taggroup-[^-]+-(.+)} $taggroup "\\1" gm_name

	    ;# Do nothing if this is not an entity
	    if {$gm_type != "Entity"} then {

		return

	    }

	    ;# Set the first entity, and then switch to "Inserting relation end" state
	    set gui_entity1 $gm_name

	    gerwin_set_state "Inserting relation end"
	    
	    . configure -cursor right_side
	}

	"Inserting relation end" {

	    ;# Get the coords onto the widget
	    set xpos [${gcanvas} canvasx $xpos]
	    set ypos [${gcanvas} canvasy $ypos]

	    ;# Figure out what WGOB is the first entity
	    set wgob [${gcanvas} find closest $xpos $ypos]

	    ;# Get the taggroup tag from the WGOB
	    set wgobtags [${gcanvas} gettags $wgob]
	    set index [lsearch -glob $wgobtags "taggroup-*"]
	    set taggroup [lindex $wgobtags $index]

	    ;# Get the object name and type from the taggroup
	    regsub {taggroup-([^-]+)-.+} $taggroup "\\1" gm_type
	    regsub {taggroup-[^-]+-(.+)} $taggroup "\\1" gm_name

	    if {$gm_type != "Entity"} then {

		;# See if we are adding an entity to a n-relation
		if {$gm_type == "Relation"} then {

		    ;# If the relation is reflexive, abort inmediatly
		    if {$gerwin_relation($gm_name,reflexive)} then {
			gui_notice "You cannot add a new entity to a reflexive relation!"

			;# Goto Editing state
			gerwin_set_state "Editing"

			. configure -cursor hand1
			return 
		    }

		    ;# If the entity is already part of the relation, abort inmediatly
		    if {[lsearch -glob [gob_relation_get_entities $gm_name] "$gui_entity1 *"] != -1} then {
			gui_notice "Sorry, $gui_entity1 already have a link to $gm_name"
			
			;# Goto Editing state
			gerwin_set_state "Editing"

			. configure -cursor hand1
			return
		    }

		    ;# Add a link to gui_entity1 to the relation
		    ;# Cardinality is always _/N
		    gob_relation_add_entity $gm_name $gui_entity1 1 N

		    ;# Add the relation to the entity's relation list
		    gob_entity_add_relation $gui_entity1 $gm_name

		    ;# Now, all entities have cardinality 1/N
		    ;# The _ is a fake value
		    foreach e [gob_relation_get_entities $gm_name] {
			set e [lindex $e 0]

			gob_relation_set_entity_min_card $gm_name $e _
			gob_relation_set_entity_max_card $gm_name $e N


		    }

		    ;# Actualize the edition page for the relation
		    ;# But only if it visible
		    if {[lsearch $earea_pages $gm_name] != -1} then {
			gui_edition_area_delete_page $gm_name
			gui_edit_relation $gm_name
		    }

		    ;# Update the links in the screen
		    gm_relation_update_links $gm_name

		    ;# Goto Editing state
		    gerwin_set_state "Editing"

		    . configure -cursor hand1
		}

		return
	    }

	    ;# Set the second entity
	    set gui_entity2 $gm_name

	    ;# Create the new relation
	    set rname "relation$gerwin_relation_seq"
	    incr gerwin_relation_seq
	    gob_create_relation $rname {} {}
	    gob_relation_add_entity $rname $gui_entity1 1 1
	    gob_relation_add_entity $rname $gui_entity2 1 1

	    ;# Add the relations to the entities
	    if {$gui_entity1 == $gui_entity2} then {
		;# Reflexive relation
		set gerwin_relation($rname,reflexive) 1
		gob_entity_add_relation $gui_entity1 $rname
	    } else {
		;# Non-reflexive relation
		gob_entity_add_relation $gui_entity1 $rname
		gob_entity_add_relation $gui_entity2 $rname
	    }

	    ;# Draw it
	    ;# Find an approximative good starting point

	    if {$gui_entity1 != $gui_entity2} then {
		;# Non reflexive relation
		set bbox1 [$gcanvas bbox taggroup-Entity-${gui_entity1}]
		set bbox2 [$gcanvas bbox taggroup-Entity-${gui_entity2}]

		set rp1x [expr \
			      [lindex $bbox1 0] + \
			      (([lindex $bbox1 2] - [lindex $bbox1 0]) / 2)]
						 
		set rp1y [expr \
			      [lindex $bbox1 1] +  \
			      (([lindex $bbox1 3] - [lindex $bbox1 1]) / 2)]

		set rp2x [expr \
			      [lindex $bbox2 0] + \
			      (([lindex $bbox2 2] - [lindex $bbox2 0]) / 2)]
						 
		set rp2y [expr \
			      [lindex $bbox2 1] +  \
			      (([lindex $bbox2 3] - [lindex $bbox2 1]) / 2)]

		set spx \
		    [expr ($rp1x + (($rp2x - $rp1x) / 2))]
		set spy \
		    [expr ($rp1y + (($rp2y - $rp1y) / 2))]

	    } else {
		;#  Reflexive relation
		set spx [expr $gm_entity($gui_entity1,xpos) + 100]
		set spy [expr $gm_entity($gui_entity1,ypos) - 50]

	    }

	    gm_draw_relation $rname $spx $spy

	    if {$gui_entity1 == $gui_entity2} then {
		;# Reflexive relation
		gm_relation_update_links_reflexive $rname
	    } else {
		;# No reflexive relation
		gm_relation_update_links $rname
	    }

	    ;# Goto Editing state
	    gerwin_set_state "Editing"

	    . configure -cursor hand1
	}

	"Inserting entity" {
	    
	    ;# Get the coords onto the widget
	    set xpos [${gcanvas} canvasx $xpos]
	    set ypos [${gcanvas} canvasy $ypos]

	    ;# Create a new entity, with a sequential name and 
	    ;# Without any attribute, and with the key empty.
	    set ename "entity$gerwin_entity_seq"
	    incr gerwin_entity_seq
	    gob_create_entity $ename {} {} {}

	    ;# Draw it
	    gm_draw_entity $ename $xpos $ypos

	    ;# Goto Editing state
	    gerwin_set_state "Editing"

	    . configure -cursor hand1	    

	}

	"Moving" {
	    
	    ;# Get the coords onto the widget
	    set xpos [${gcanvas} canvasx $xpos]
	    set ypos [${gcanvas} canvasy $ypos]

	    ;# Now, figure out what WGOB we want to delete
	    set wgob [${gcanvas} find closest $xpos $ypos]
			

	    ;# Get the taggroup tag from the WGOB
	    set wgobtags [${gcanvas} gettags $wgob]
	    set index [lsearch -glob $wgobtags "taggroup-*"]
	    set taggroup [lindex $wgobtags $index]

	    ;# Get the object name and type from the taggroup
	    regsub {taggroup-([^-]+)-.+} $taggroup "\\1" gm_type
	    regsub {taggroup-[^-]+-(.+)} $taggroup "\\1" gm_name

	    set gerwin_obj_moving $gm_name
	    set gerwin_obj_type_moving $gm_type

	    set gerwin_premov_x $xpos
	    set gerwin_premov_y $ypos
	    set gerwin_premov_object $taggroup

	}

	"Editing" {

	    ;# Get the coords onto the widget
	    set xpos [${gcanvas} canvasx $xpos]
	    set ypos [${gcanvas} canvasy $ypos]

	    ;# Now, figure out what WGOB we want to delete
	    set wgob [${gcanvas} find closest $xpos $ypos]
			

	    ;# Get the taggroup tag from the WGOB
	    set wgobtags [${gcanvas} gettags $wgob]
	    set index [lsearch -glob $wgobtags "taggroup-*"]
	    set taggroup [lindex $wgobtags $index]
	    
	    ;# Get the object name and type from the taggroup
	    regsub {taggroup-([^-]+)-.+} $taggroup "\\1" gm_type
	    regsub {taggroup-[^-]+-(.+)} $taggroup "\\1" gm_name

	    ;# Switch over the type
	    switch $gm_type {

		"Relation" {

		    gui_edit_relation $gm_name

		}

		"Entity" {

		    gui_edit_entity $gm_name

		}
	    }

	}

	"Deleting" {

	    ;# Get the coords onto the widget
	    set xpos [${gcanvas} canvasx $xpos]
	    set ypos [${gcanvas} canvasy $ypos]

	    ;# Now, figure out what WGOB we want to delete
	    set wgob [${gcanvas} find closest $xpos $ypos]
			

	    ;# Get the taggroup tag from the WGOB
	    set wgobtags [${gcanvas} gettags $wgob]
	    set index [lsearch -glob $wgobtags "taggroup-*"]
	    set taggroup [lindex $wgobtags $index]

	    ;# Get the object name and type from the taggroup
	    regsub {taggroup-([^-]+)-.+} $taggroup "\\1" gm_type
	    regsub {taggroup-[^-]+-(.+)} $taggroup "\\1" gm_name

	    ;# Switch over the type
	    switch $gm_type {
		
		"Relation" {
		    ;# Delete the relation from the canvas
		    gm_delete_relation $gm_name

		    ;# Delete the relation from all entities that haves that relation registered
		    foreach e [gob_relation_get_entities $gm_name] {
			
			gob_entity_delete_relation [lindex $e 0] $gm_name

		    }
		    
		    ;# Delete relation's links
		    ${gcanvas} delete "taggroup-Link-$gm_name"

		    ;# Delete the GOB
		    gob_delete_relation $gm_name

		    ;# Delete the relation page from the editionarea
		    gui_edition_area_delete_page $gm_name
		}

		"Entity" {
		    ;# Delete the entity from the canvas
		    gm_delete_entity $gm_name

		    ;# Delete all relations with num_entities equal to two
		    foreach r [gob_entity_get_relations $gm_name] {

			if {[gob_relation_get_num_entities $r] == 2} then {

			    ;# Delete the relation from the canvas
			    gm_delete_relation $r

			    ;# Delete the relation from all entities that haves that relation registered
			    foreach e [gob_relation_get_entities $r] {
			
				gob_entity_delete_relation [lindex $e 0] $r

			    }
		    
			    ;# Delete relation's links
			    ${gcanvas} delete "taggroup-Link-$r"

			    ;# Delete the GOB
			    gob_delete_relation $r

			    ;# Delete the relation page from the editionarea
			    gui_edition_area_delete_page $r
			

			} else {

			    ;# Quit the entity from the relation
			    gob_relation_delete_entity $r $gm_name

			    ;# Update the relation links
			    ;# Note that this relation can not be reflexive
			    gm_relation_update_links $r

			}

		    }

		    ;# Delete the GOB
		    gob_delete_entity $gm_name

		    ;# Delete the entity page from the editionarea
		    gui_edition_area_delete_page $gm_name

		}

	    
	    }
	}
    }

}

;#
;# gui_edition_area_delete_page PNAME
;#
;# Deletes the page named PNAME from the edition area

proc gui_edition_area_delete_page {pname} {

    global gmainframe
    global earea_pages
    global gerwin_editionarea

    set userframe [${gmainframe} getframe]

    ;# Find the page to delete
    set index [lsearch $earea_pages $pname]
    
    if {$index != -1} then {

	set w [${gerwin_editionarea} getframe $pname]
	;#set w [${userframe}.editionarea getframe $pname]


	;# Delete the page from the list
	set earea_pages [lreplace $earea_pages $index $index]

	;# Update the notebook
	${gerwin_editionarea} delete $pname
	;#${userframe}.editionarea delete $pname

	;# Destroy the widgets
	destroy $w
    }

}


;#
;# gui_attribute_is_not_key LISTBOX ENTITY
;#
;# Remove the current LISTBOX selection from the ENTITY key

proc gui_attribute_is_not_key {lb entity} {

    global gm_entity
    global gerwin_relation

    set attribute [$lb curselection]

    if {! [string equal $attribute ""]} then {
	
	set attribute [$lb get $attribute]

	gob_entity_delete_attribute_from_key $entity $attribute
	set xpos $gm_entity($entity,xpos)
	set ypos $gm_entity($entity,ypos)
	gm_delete_entity $entity
	gm_draw_entity $entity $xpos $ypos

	foreach r [gob_entity_get_relations $entity] {

	    if {$gerwin_relation($r,reflexive)} then {
		gm_relation_update_links_reflexive $r
	    } else {
		gm_relation_update_links $r
	    }

	}
    }
}

;#
;# gui_attribute_delete_relation LISTBOX RELATION
;#
;# Delete the LISTBOX selected attribute from RELATION

proc gui_attribute_delete_relation {lb relation} {

    global gm_relation
    global gerwin_relation
    global gerwin_editionarea

    set attribute [$lb curselection]

    if {! [string equal $attribute ""]} then {

	set attribute [$lb get $attribute]
	set vattribute $attribute

	# Support delete or Edit
	set editframe [${gerwin_editionarea} getframe $relation]
	set aframe [${editframe}.attributes getframe]

	${aframe}.nattribute.faux.ftop.label configure -text $vattribute

	set dfr [${aframe}.nattribute.faux.fdown.domframe getframe]
	${dfr}.label configure -text [gob_relation_get_attribute_domain $relation $vattribute]



	gob_relation_delete_attribute $relation $attribute
	set xpos $gm_relation($relation,xpos)
	set ypos $gm_relation($relation,ypos)
	gm_delete_relation $relation
	gm_draw_relation $relation $xpos $ypos
	gui_edition_area_delete_page $relation
	gui_edit_relation $relation

	if {$gerwin_relation($relation,reflexive)} then {
	    gm_relation_update_links_reflexive $relation
	} else {
	    gm_relation_update_links $relation
	}


    }

}


;#
;# gui_attribute_delete_entity LISTBOX ENTITY
;#
;# Delete the LISTBOX selected attribute from ENTITY

proc gui_attribute_delete_entity {lb entity} {

    global gm_entity 
    global gerwin_relation
    global gerwin_editionarea

    set attribute [$lb curselection]



    if {! [string equal $attribute ""]} then {
    
	set vattribute [$lb get $attribute]

	# Support delete or Edit
	set editframe [${gerwin_editionarea} getframe $entity]
	set aframe [${editframe}.attributes getframe]

	${aframe}.nattribute.faux.ftop.label configure -text $vattribute

	set dfr [${aframe}.nattribute.faux.fdown.domframe getframe]
	${dfr}.label configure -text [gob_entity_get_attribute_domain $entity $vattribute]


	set attribute [$lb get $attribute]

	gob_entity_delete_attribute $entity $attribute
	set xpos $gm_entity($entity,xpos)
	set ypos $gm_entity($entity,ypos)
	gm_delete_entity $entity
	gm_draw_entity $entity $xpos $ypos
	gui_edition_area_delete_page $entity
	gui_edit_entity $entity

	foreach r [gob_entity_get_relations $entity] {

	    if {$gerwin_relation($r,reflexive)} then {
		gm_relation_update_links_reflexive $r
	    } else {
		gm_relation_update_links $r
	    }

	}

    }
}


;#
;# gui_attribute_is_key LISTBOX ENTITY
;#
;# Add the attribute selected in LISTBOX as a new key on ENTITY

proc gui_attribute_is_key {lb entity} {

    global gm_entity 
    global gerwin_relation

    set attribute [$lb curselection]

    if {! [string equal $attribute ""]} then {

	set attribute [$lb get $attribute]

	;# We exit if the attribute is already a key
	if {[gob_entity_is_key $entity $attribute]} then {
	    return
	}


	gob_entity_add_attribute_to_key $entity $attribute
	set xpos $gm_entity($entity,xpos)
	set ypos $gm_entity($entity,ypos)
	gm_delete_entity $entity
	gm_draw_entity $entity $xpos $ypos

	foreach r [gob_entity_get_relations $entity] {

	    if {$gerwin_relation($r,reflexive)} then {
		gm_relation_update_links_reflexive $r
	    } else {
		gm_relation_update_links $r
	    }

	}

    }
}


;#
;# gui_edit_relation_add_attribute RELATION
;#
;# Add a new attribute with ANAME and ADOMAIN to RELATION
;#
;# ANAME and ADOMAIN cannot be emtpy.
;# All blanks in ANAME and ADOMAIN are transformed to "-" characters.

proc gui_edit_relation_add_attribute {relation} {

    global gm_relation
    global gerwin_relation

    set aname $gm_relation($relation,nattribute)
    set adomain $gm_relation($relation,ndomain)

    ;# First of all, make sure both ANAME and ADOMAIN are not empty
    if {($aname == "")} then {
	
	gui_notice "Please fill the Name field if you want to add an attribute to the relation."
	return
    }

    if {($adomain == "")} then {

	gui_notice "Please fill the Domain field if you want to add an attribute to the relation."
	return
    }

    ;# Replace all blanks with scores
    set aname [string map {{ } -} $aname]
    set adomain [string map {{ } -} $adomain]

    ;# Add the attribute to the gob
    gob_relation_add_attribute $relation $aname $adomain
    
    set gm_relation($relation,ndomain) {}
    set gm_relation($relation,nattribute) {}

    set xpos $gm_relation($relation,xpos)
    set ypos $gm_relation($relation,ypos)

    gm_delete_relation $relation
    gm_draw_relation $relation $xpos $ypos

    if {$gerwin_relation($relation,reflexive)} then {

	gm_relation_update_links_reflexive $relation
             
    } else {
	
	gm_relation_update_links $relation
        
    }

    gui_edition_area_delete_page $relation
    gui_edit_relation $relation

}



;#
;# gui_edit_entity_add_attribute ENTITY
;#
;# Add a new attribute with ANAME and ADOMAIN to ENTITY
;#
;# ANAME and ADOMAIN cannot be emtpy.
;# All blanks in ANAME and ADOMAIN are transformed to "-" characters.

proc gui_edit_entity_add_attribute {entity} {

    global gm_entity
    global gerwin_relation

    set aname $gm_entity($entity,nattribute)
    set adomain $gm_entity($entity,ndomain)

    ;# First of all, make sure both ANAME and ADOMAIN are not empty
    if {($aname == "")} then {
	
	gui_notice "Please fill the Name field if you want to add an attribute to the entity."
	return
    }

    if {($adomain == "")} then {

	gui_notice "Please fill the Domain field if you want to add an attribute to the entity."
	return
    }

    ;# Replace all blanks with scores
    set aname [string map {{ } -} $aname]
    set adomain [string map {{ } -} $adomain]

    ;# Add the attribute to the gob
    gob_entity_add_attribute $entity $aname $adomain

    set gm_entity($entity,ndomain) {}
    set gm_entity($entity,nattribute) {}

    set xpos $gm_entity($entity,xpos)
    set ypos $gm_entity($entity,ypos)

    gm_delete_entity $entity
    gm_draw_entity $entity $xpos $ypos

    foreach r [gob_entity_get_relations $entity] {

	if {$gerwin_relation($r,reflexive)} then {
	    
	    gm_relation_update_links_reflexive $r
	    
	} else {
	    

	    gm_relation_update_links $r

	}
    }

    gui_edition_area_delete_page $entity
    gui_edit_entity $entity

}




;#
;# gui_edit_relation_change_name RELATION
;#
;# Changes the name of RELATION to gm_relation(RELATION,nname)

proc gui_edit_relation_change_name {relation} {

    global gcanvas
    global gm_relation
    global earea_pages
    global gerwin_relation

    ;# See if the new name is empty
    if {$gm_relation($relation,nname) == ""} then {

	gui_notice "The name of the relation cannot be empty!"
	return
    }

    ;# Change spaces for underscores
    set gm_relation($relation,nname) [string map {{ } _} $gm_relation($relation,nname)]

    if {([gob_entity_exist $gm_relation($relation,nname)] ||
	 [gob_relation_exist $gm_relation($relation,nname)])} then {

	gui_notice "The name of any relation or entity must be unique in all the model!"
	return
    }

    ;# Change the name of the gob
    gob_relation_change_name $relation $gm_relation($relation,nname)
    set nname $gm_relation($relation,nname)
    gui_edition_area_delete_page $relation
    
    gm_relation_change_name $relation $nname
    set gm_relation($relation,nname) {}
    
    gui_edit_relation $nname

    set xpos $gm_relation($nname,xpos)
    set ypos $gm_relation($nname,ypos)

    gm_delete_relation $nname
    ${gcanvas} delete taggroup-Link-$relation
    gm_draw_relation $nname $xpos $ypos

    if {$gerwin_relation($nname,reflexive)} then {

	gm_relation_update_links_reflexive $nname

    } else {

	gm_relation_update_links $nname
    }


}

;#
;# gui_edit_entity_change_name ENTITY
;#
;# Changes the name of ENTITY to gm_entity(ENTITY,nname)


proc gui_edit_entity_change_name {entity} {

    global gm_entity
    global earea_pages
    global gerwin_relation

    ;# See if the new name is empty
    if {$gm_entity($entity,nname) == ""} then {

	gui_notice "The name of the entity cannot be empty!"
	return
    }

    ;# Change spaces for underscores
    set gm_entity($entity,nname) [string map {{ } _} $gm_entity($entity,nname)]

    if {([gob_entity_exist $gm_entity($entity,nname)]) || 
	([gob_relation_exist $gm_entity($entity,nname)])} then {

	gui_notice "The name of any entity or relation must be unique in all the model!"
	return 
    }

    ;# Change the name of the gob
    gob_entity_change_name $entity $gm_entity($entity,nname)

    set nname $gm_entity($entity,nname)
    gui_edition_area_delete_page $entity

    ;# Change the name in all relation edition areas
    foreach r [gob_entity_get_relations $nname] {

	if {[lsearch $earea_pages $r] != -1} then {

	    gui_edition_area_delete_page $r
	    gui_edit_relation $r
	}
    }


    gui_edit_entity $nname

    set xpos $gm_entity($entity,xpos)
    set ypos $gm_entity($entity,ypos)

    
    gm_entity_change_name $entity $nname
    gm_delete_entity $nname
    gm_draw_entity $nname $xpos $ypos

    foreach r [gob_entity_get_relations $nname] {

	;# TODO: change to simply gm_relation_update_links
	if {$gerwin_relation($r,reflexive)} then {
	    gm_relation_update_links_reflexive $r
	} else {
	    gm_relation_update_links $r
	    
	}

    }
}


;#
;# gui_edit_entity ENTITY
;#
;# Edit all aspects of a given entity, in a page on the 
;# editionarea

proc gui_edit_entity {entity} {

    global gmainframe
    global gerwin_entity
    global gm_entity
    global gm_relation
    global gerwin_relation

    global earea_pages 
    global gerwin_editionarea

    set userframe [${gmainframe} getframe]
    
    ;# See if the entity already have a page in the edition area
    set index [lsearch $earea_pages $entity]
    if {$index != -1} then {
	
	;# Make the page visible
	${gerwin_editionarea} raise [lindex $earea_pages $index]
	;#${userframe}.editionarea raise [lindex $earea_pages $index]

	return

    }

    ;# Create a new page for this entity
    lappend earea_pages $entity
    ${gerwin_editionarea} insert end $entity -text $entity
    ;#${userframe}.editionarea insert end $entity -text $entity

    ;# Make the entries into the frame
    set editframe [${gerwin_editionarea} getframe $entity]
    ;#set editframe [${userframe}.editionarea getframe $entity]


    ;#####    PROPERTIES FRAME

    TitleFrame ${editframe}.properties -side center -text "Entity Properties"
    set pframe [${editframe}.properties getframe]

    ;# ENTITY NAME
    frame ${pframe}.nameline

    LabelEntry ${pframe}.nameline.label -label "Name: " -labelwidth 6 \
	-text $entity -textvariable gm_entity($entity,nname) \
	-helptext "Name of the entity"

    button ${pframe}.nameline.button -text "change" -relief groove \
	-command [list gui_edit_entity_change_name $entity]


    frame ${pframe}.typeline
    LabelEntry ${pframe}.typeline.label -label "Type:" -labelwidth 6 \
	-textvariable gm_entity($entity,ntype) \
	-helptext "Type of the entity"
    button ${pframe}.typeline.button -text "change" -relief groove \
	-command "gob_entity_change_type $entity \$gm_entity($entity,ntype); \
                  set ttype \$gm_entity($entity,ntype) ; \
                  gui_edition_area_delete_page $entity ; \
                  gm_entity_change_type $entity \$gm_entity($entity,ntype); \
                  gui_edit_entity $entity ; \
                  set xpos \$gm_entity($entity,xpos); \
                  set ypos \$gm_entity($entity,ypos); \
                  gm_delete_entity $entity; \
                  gm_draw_entity $entity \$xpos \$ypos ; " \
	-state disabled



    ;# pack it
    pack ${pframe}.nameline.label -side left
    pack ${pframe}.nameline.button -side left


    pack ${pframe}.typeline.label -side left
    pack ${pframe}.typeline.button -side left

    pack ${pframe}.nameline -side top
    pack ${pframe}.typeline -side top
    pack ${pframe} -side left -anchor nw

    ;####   ATTRIBUTES FRAME

    TitleFrame ${editframe}.attributes -side center -text "Attributes"
    set aframe [${editframe}.attributes getframe]

    frame ${aframe}.nattribute
    label ${aframe}.nattribute.label -text "New Attribute"
    frame ${aframe}.nattribute.faux
    frame ${aframe}.nattribute.faux.ftop
    frame ${aframe}.nattribute.faux.fdown
    LabelEntry ${aframe}.nattribute.faux.ftop.label -label "Name:" -labelwidth 7 \
	-labelanchor w -textvariable gm_entity($entity,nattribute) \
	-helptext "name for the new attribute"

#     LabelEntry ${aframe}.nattribute.faux.fdown.label -label "Domain:" -labelwidth 7 \
# 	-labelanchor w -textvariable gm_entity($entity,ndomain) \
# 	-helptext "domain for the new attribute"

    LabelFrame ${aframe}.nattribute.faux.fdown.domframe -text "Domain:" \
    -side left

    set dfr [${aframe}.nattribute.faux.fdown.domframe getframe]
    
    ComboBox $dfr.label -editable true \
	-textvariable gm_entity($entity,ndomain) \
	-helptext "domain for the new attribute" \
        -width 18 \
        -values {varchar(8) varchar(16) varchar(32) int bigint text date} 

    pack $dfr.label -side top -anchor e



    Button ${aframe}.nattribute.button -text "Add" -relief groove \
	-command [list gui_edit_entity_add_attribute $entity]


    Separator ${aframe}.sep1 -orient vertical

    frame ${aframe}.attributes

    frame ${aframe}.attributes.frame
    scrollbar ${aframe}.attributes.frame.scrollbar \
	-command [list ${aframe}.attributes.frame.lbox yview]



    set lb [listbox ${aframe}.attributes.frame.lbox \
		-yscrollcommand [list ${aframe}.attributes.frame.scrollbar set]]

    ;# Insert the listbox elements
    foreach a [gob_entity_get_attributes $entity] {

	set aname [lindex $a 0]
	;# Make the embedded window
	$lb insert end $aname

    }
    

    ;# make the bbox
    set bbox [ButtonBox ${aframe}.bbox -spacing 0 -padx 1 -pady 0 \
		 -orient vertical]

    ${bbox} add -text "Is key" -command [list gui_attribute_is_key $lb $entity] -relief groove
    ${bbox} add -text "Is not key" -command [list gui_attribute_is_not_key $lb $entity] -relief groove
    ${bbox} add -text "Del/Edit" -command [list gui_attribute_delete_entity $lb $entity] -relief groove 

    pack ${aframe}.nattribute.label -side top
    pack ${aframe}.nattribute.faux.ftop.label -side top -anchor w
    pack ${aframe}.nattribute.faux.ftop -side top
    pack ${aframe}.nattribute.faux.fdown.domframe -side top -anchor w
    pack ${aframe}.nattribute.faux.fdown -side top 
    pack ${aframe}.nattribute.faux -side left
    pack ${aframe}.nattribute.button -side right


    pack ${aframe}.nattribute -side left

    pack ${aframe}.sep1 -fill y -padx 8 -side left

    pack ${aframe}.attributes.frame.scrollbar -side right -fill y
    pack ${aframe}.attributes.frame.lbox -side left
    pack ${aframe}.attributes.frame

    pack ${aframe}.attributes -side left

    pack ${aframe}.bbox -side right

    button ${editframe}.quit -text "Close page" -relief groove \
	-command "destroy ${editframe} ; \
                  gui_edition_area_delete_page $entity"

    ;# Pack the frames
    pack ${editframe}.properties -side left -fill both -padx 10
    pack ${editframe}.attributes -side left -fill both -padx 10
    pack ${editframe}.quit -side right
    pack ${editframe}


    ;# Make the page visible
    ${gerwin_editionarea} raise $entity

    ;# Scale the notebook
    NoteBook::compute_size ${gerwin_editionarea}
}

;#
;# gui_edit_relation
;#
;# Edit all aspects of a given relation, in a page on the
;# editionarea

proc gui_edit_relation {relation} {

    global gmainframe
    global gerwin_relation
    global gm_relation
    global gcanvas

    global earea_pages
    global gerwin_editionarea

    set userframe [${gmainframe} getframe]
    
    ;# See if the relation already have a page in the edition area
    set index [lsearch $earea_pages $relation]
    if {$index != -1} then {

	;# Make the page visible
	${gerwin_editionarea} raise [lindex $earea_pages $index]
	;#${userframe}.editionarea raise [lindex $earea_pages $index]

	return
    }

    ;# Create a new page for this relation
    lappend earea_pages $relation
    ${gerwin_editionarea} insert end $relation -text $relation
    ;#${userframe}.editionarea insert end $relation -text $relation

    ;# Make the entries into the frame
    set editframe [${gerwin_editionarea} getframe $relation]
    ;#set editframe [${userframe}.editionarea getframe $relation]

    ;##### PROPERTIES FRAME

    TitleFrame ${editframe}.properties -side center -text "Relation Properties"
    set pframe [${editframe}.properties getframe]

    ;# RELATION NAME
    frame ${pframe}.nameline

    LabelEntry ${pframe}.nameline.label -label "Name: " -labelwidth 6 \
	-text $relation -textvariable gm_relation($relation,nname) \
	-helptext "Name of the relation"

    button ${pframe}.nameline.button -text "change" -relief groove \
	-command [list gui_edit_relation_change_name $relation]

    
    ;# pack it
    pack ${pframe}.nameline.label -side left
    pack ${pframe}.nameline.button -side left
    pack ${pframe}.nameline -side top


    ;# RELATION ENTITIES and cardinality (entities 0 and 1 only)
    frame ${pframe}.cards


    if {[gob_relation_get_num_entities $relation] > 2} then {
	;# Show a short label 
	;#label ${pframe}.cards.label -text "All entitities cardinality X/N"
	;#pack ${pframe}.cards.label

    } else {
	;# Binary relation: show cardinality
    
	frame ${pframe}.cards.entity1

	set entities [gob_relation_get_entities $relation]
	set entity1_name [lindex [lindex $entities 0] 0]
	set entity1_mincard [lindex [lindex [lindex $entities 0] 1] 0]
	set entity1_maxcard [lindex [lindex [lindex $entities 0] 1] 1]
	set entity2_name [lindex [lindex $entities 1] 0]
	set entity2_mincard [lindex [lindex [lindex $entities 1] 1] 0]
	set entity2_maxcard [lindex [lindex [lindex $entities 1] 1] 1]

	Label ${pframe}.cards.entity1.ename -text $entity1_name

	;# Set up the variables
	set gerwin_relation($relation,e1mincard) $entity1_mincard
	set gerwin_relation($relation,e1maxcard) $entity1_maxcard

	;# Minimal card
	radiobutton ${pframe}.cards.entity1.rbmin0 -text "0" \
	    -variable gerwin_relation($relation,e1mincard) -value 0 \
	    -command "gob_relation_set_entity_min_card $relation $entity1_name \$gerwin_relation($relation,e1mincard) ; \
                  set xpos \$gm_relation($relation,xpos); \
                  set ypos \$gm_relation($relation,ypos); \
                  gm_delete_relation $relation ; \
                  gm_draw_relation $relation \$xpos \$ypos ; \
                  if {\$gerwin_relation($relation,reflexive)} then {
                         gm_relation_update_links_reflexive $relation
                  } else {
                         gm_relation_update_links $relation
                  }"

	radiobutton ${pframe}.cards.entity1.rbmin1 -text "1" \
	    -variable gerwin_relation($relation,e1mincard) -value 1 \
	    -command "gob_relation_set_entity_min_card $relation $entity1_name \$gerwin_relation($relation,e1mincard) ; \
                  set xpos \$gm_relation($relation,xpos); \
                  set ypos \$gm_relation($relation,ypos); \
                  gm_delete_relation $relation ; \
                  gm_draw_relation $relation \$xpos \$ypos ; \
                  if {\$gerwin_relation($relation,reflexive)} then {
                         gm_relation_update_links_reflexive $relation
                  } else {
                         gm_relation_update_links $relation
                  }"

	;# Separator
	label ${pframe}.cards.entity1.sep -text "/"

	;# Maximal card
	radiobutton ${pframe}.cards.entity1.rbmax1 -text "1" \
	    -variable gerwin_relation($relation,e1maxcard) -value 1 \
	    -command "gob_relation_set_entity_max_card $relation $entity1_name \$gerwin_relation($relation,e1maxcard) ; \
                  set xpos \$gm_relation($relation,xpos); \
                  set ypos \$gm_relation($relation,ypos); \
                  gm_delete_relation $relation ; \
                  gm_draw_relation $relation \$xpos \$ypos ; \
                  if {\$gerwin_relation($relation,reflexive)} then {
                         gm_relation_update_links_reflexive $relation
                  } else {
                         gm_relation_update_links $relation
                  }"


	radiobutton ${pframe}.cards.entity1.rbmaxN -text "N" \
	    -variable gerwin_relation($relation,e1maxcard) -value N \
	    -command "gob_relation_set_entity_max_card $relation $entity1_name \$gerwin_relation($relation,e1maxcard) ; \
                  set xpos \$gm_relation($relation,xpos); \
                  set ypos \$gm_relation($relation,ypos); \
                  gm_delete_relation $relation ; \
                  gm_draw_relation $relation \$xpos \$ypos ; \
                  if {\$gerwin_relation($relation,reflexive)} then {
                         gm_relation_update_links_reflexive $relation
                  } else {
                         gm_relation_update_links $relation
                  }"

	;# Set gui_e1mincard and gui_e1maxcard as appropiate
	set qui_e1mincard [gob_relation_get_entity_min_card $relation $entity1_name]
	set gui_e1maxcard [gob_relation_get_entity_max_card $relation $entity1_name]


	pack ${pframe}.cards.entity1.ename -side top
	pack ${pframe}.cards.entity1.rbmin0 -side left
	pack ${pframe}.cards.entity1.rbmin1 -side left
	pack ${pframe}.cards.entity1.sep -side left
	pack ${pframe}.cards.entity1.rbmax1 -side left
	pack ${pframe}.cards.entity1.rbmaxN -side left

	pack ${pframe}.cards.entity1 -side left

	frame ${pframe}.cards.entity2

	;# Set up the variables
	set gerwin_relation($relation,e2mincard) $entity2_mincard
	set gerwin_relation($relation,e2maxcard) $entity2_maxcard

	Label ${pframe}.cards.entity2.ename -text $entity2_name

	;# Minimal card
	radiobutton ${pframe}.cards.entity2.rbmin0 -text "0" \
	    -variable gerwin_relation($relation,e2mincard) -value 0 \
	    -command "
                  set entities \[gob_relation_get_entities $relation\]
                  set e1 \[lindex \$entities 0\]
                  set e2 \[lindex \$entities 1\]
                  set e2name \[lindex \$e2 0\]
                  set e2cards \[lindex \$e2 1\]
                  set gerwin_relation($relation,entities) \[list \$e1 \[list \$e2name \[list \$gerwin_relation($relation,e2mincard) \[lindex \$e2cards 1\]\]\]\]
                  set xpos \$gm_relation($relation,xpos); \
                  set ypos \$gm_relation($relation,ypos); \
                  gm_delete_relation $relation ; \
                  gm_draw_relation $relation \$xpos \$ypos ; \
                  if {\$gerwin_relation($relation,reflexive)} then {
                         gm_relation_update_links_reflexive $relation
                  } else {
                         gm_relation_update_links $relation
                  }"

	radiobutton ${pframe}.cards.entity2.rbmin1 -text "1" \
	    -variable gerwin_relation($relation,e2mincard) -value 1 \
	    -command "
                  set entities \[gob_relation_get_entities $relation\]
                  set e1 \[lindex \$entities 0\]
                  set e2 \[lindex \$entities 1\]
                  set e2name \[lindex \$e2 0\]
                  set e2cards \[lindex \$e2 1\]
                  set gerwin_relation($relation,entities) \[list \$e1 \[list \$e2name \[list \$gerwin_relation($relation,e2mincard) \[lindex \$e2cards 1\]\]\]\]
                  set xpos \$gm_relation($relation,xpos); \
                  set ypos \$gm_relation($relation,ypos); \
                  gm_delete_relation $relation ; \
                  gm_draw_relation $relation \$xpos \$ypos ; \
                  if {\$gerwin_relation($relation,reflexive)} then {
                         gm_relation_update_links_reflexive $relation
                  } else {
                         gm_relation_update_links $relation
                  }"

	;# Separator
	label ${pframe}.cards.entity2.sep -text "/"

	;# Maximal card
	radiobutton ${pframe}.cards.entity2.rbmax1 -text "1" \
	    -variable gerwin_relation($relation,e2maxcard) -value 1 \
	    -command "
                  set entities \[gob_relation_get_entities $relation\]
                  set e1 \[lindex \$entities 0\]
                  set e2 \[lindex \$entities 1\]
                  set e2name \[lindex \$e2 0\]
                  set e2cards \[lindex \$e2 1\]
                  set gerwin_relation($relation,entities) \[list \$e1 \[list \$e2name \[list \[lindex \$e2cards 0\] \$gerwin_relation($relation,e2maxcard) \]\]\]
                  set xpos \$gm_relation($relation,xpos); \
                  set ypos \$gm_relation($relation,ypos); \
                  gm_delete_relation $relation ; \
                  gm_draw_relation $relation \$xpos \$ypos ; \
                  if {\$gerwin_relation($relation,reflexive)} then {
                         gm_relation_update_links_reflexive $relation
                  } else {
                         gm_relation_update_links $relation
                  }"


	radiobutton ${pframe}.cards.entity2.rbmaxN -text "N" \
	    -variable gerwin_relation($relation,e2maxcard) -value N \
	    -command "
                  set entities \[gob_relation_get_entities $relation\]
                  set e1 \[lindex \$entities 0\]
                  set e2 \[lindex \$entities 1\]
                  set e2name \[lindex \$e2 0\]
                  set e2cards \[lindex \$e2 1\]
                  set gerwin_relation($relation,entities) \[list \$e1 \[list \$e2name \[list \[lindex \$e2cards 0\] \$gerwin_relation($relation,e2maxcard) \]\]\]
                  set xpos \$gm_relation($relation,xpos); \
                  set ypos \$gm_relation($relation,ypos); \
                  gm_delete_relation $relation ; \
                  gm_draw_relation $relation \$xpos \$ypos ; \

                  if {\$gerwin_relation($relation,reflexive)} then {
                         gm_relation_update_links_reflexive $relation
                  } else {
                         gm_relation_update_links $relation
                  }"


	;# Set gui_e2maxcard as appropiate
	set gui_e2maxcard [gob_relation_get_entity_card $relation $entity2_name]

	pack ${pframe}.cards.entity2.ename -side top
	pack ${pframe}.cards.entity2.rbmin0 -side left
	pack ${pframe}.cards.entity2.rbmin1 -side left
	pack ${pframe}.cards.entity2.sep -side left
	pack ${pframe}.cards.entity2.rbmax1 -side left
	pack ${pframe}.cards.entity2.rbmaxN -side left

	Separator ${pframe}.cards.sep2 -orient vertical
	pack ${pframe}.cards.sep2 -fill y -padx 4 -side left

	pack ${pframe}.cards.entity2 -side right


	pack ${pframe}.cards -side left

	pack ${pframe}.cards -side top
    }

    pack ${pframe} -side left -anchor w

    ;#### ATTRIBUTES FRAME

    TitleFrame ${editframe}.attributes -side center -text "Attributes"
    set aframe [${editframe}.attributes getframe]

    frame ${aframe}.nattribute
    label ${aframe}.nattribute.label -text "New Attribute"
    frame ${aframe}.nattribute.faux
    frame ${aframe}.nattribute.faux.ftop
    frame ${aframe}.nattribute.faux.fdown
    LabelEntry ${aframe}.nattribute.faux.ftop.label -label "Name:" -labelwidth 7 \
	-labelanchor w -textvariable gm_relation($relation,nattribute) \
	-helptext "name for the new attribute"
#     LabelEntry ${aframe}.nattribute.faux.fdown.label -label "Domain:" -labelwidth 7 \
# 	-labelanchor w -textvariable gm_relation($relation,ndomain) \
# 	-helptext "domain for the new attribute"

    LabelFrame ${aframe}.nattribute.faux.fdown.domframe -text "Domain:" \
    -side left

    set dfr [${aframe}.nattribute.faux.fdown.domframe getframe]
    
    ComboBox $dfr.label -editable true \
	-textvariable gm_relation($relation,ndomain) \
	-helptext "domain for the new attribute" \
        -width 18 \
        -values {varchar(8) varchar(16) varchar(32) int bigint text date} 

    pack $dfr.label -side top -anchor e

    Button ${aframe}.nattribute.button -text "Add" -relief groove \
	-command [list gui_edit_relation_add_attribute $relation]


    Separator ${aframe}.sep1 -orient vertical

    frame ${aframe}.attributes

    frame ${aframe}.attributes.frame
    scrollbar ${aframe}.attributes.frame.scrollbar \
	-command [list ${aframe}.attributes.frame.lbox yview]



    set lb [listbox ${aframe}.attributes.frame.lbox \
		-yscrollcommand [list ${aframe}.attributes.frame.scrollbar set]]

    ;# Insert the listbox elements
    foreach a [gob_relation_get_attributes $relation] {

	set aname [lindex $a 0]
	;# Make the embedded window
	$lb insert end $aname

    }
    

    ;# make the bbox
    set bbox [ButtonBox ${aframe}.bbox -spacing 0 -padx 1 -pady 0 \
		 -orient vertical]

    ${bbox} add -text "Del/Edit" -command [list gui_attribute_delete_relation $lb $relation] -relief groove 

    pack ${aframe}.nattribute.label -side top
    pack ${aframe}.nattribute.faux.ftop.label -side top -anchor w
    pack ${aframe}.nattribute.faux.ftop -side top
    pack ${aframe}.nattribute.faux.fdown.domframe -side top -anchor w
    pack ${aframe}.nattribute.faux.fdown -side top 
    pack ${aframe}.nattribute.faux -side left
    pack ${aframe}.nattribute.button -side right


    pack ${aframe}.nattribute -side left

    pack ${aframe}.sep1 -fill y -padx 8 -side left

    pack ${aframe}.attributes.frame.scrollbar -side right -fill y
    pack ${aframe}.attributes.frame.lbox -side left
    pack ${aframe}.attributes.frame

    pack ${aframe}.attributes -side left

    pack ${aframe}.bbox -side right

    button ${editframe}.quit -text "Close page" -relief groove \
	-command "destroy ${editframe} ; \
                  gui_edition_area_delete_page $relation"

    ;# Pack the frames
    pack ${editframe}.properties -side left -fill both -padx 10
    pack ${editframe}.attributes -side left -fill both -padx 10
    pack ${editframe}.quit -side right
    pack ${editframe}


    ;# Make the page visible
    ${gerwin_editionarea} raise $relation
    ;#${userframe}.editionarea raise $relation

    ;# Scale the notebook
    NoteBook::compute_size ${gerwin_editionarea}
}


;#
;# gui_select_file_to_save_sql
;#
;# Select a file to save the generated SQL

proc gui_select_file_to_save_sql {} {

    set filetypes {{"SQL File" {.sql}}}

    set f [tk_getSaveFile -initialdir "." \
	       -filetypes $filetypes -title "Select a SQL file"]

    ;# Switch over the result
    switch $f {

	"" {
	    ;# Do nothing
	    return
	}
	default {
	    ;# Put the .sql suffix if it is not done
	    if {! [string match "*.sql" $f] } then {
		append f ".sql"
	    }

	    return $f

	}

    }
}


;#
;# gui_select_file_to_save
;#
;# Select a file to save the project

proc gui_select_file_to_save {} {

    set filetypes {{"Gerwin Project" {.ger}}}

    set f [tk_getSaveFile -initialdir "." \
	       -filetypes $filetypes -title "Select a project file"]

    ;# Switch over the result
    switch $f {

	"" {
	    ;# Do nothing
	    return
	}
	default {
	    ;# Put the .ger suffix if it is not done
	    if {! [string match "*.ger" $f] } then {
		append f ".ger"
	    }

	    return $f

	}

    }
}

;# 
;# gui_edit_project 
;#
;# Edit the actual Project parameters
;# Another edit widget can be put at the right of that.

proc gui_edit_project {} {

    global gerwin_cproject_name
    global gerwin_cproject_file
    global gerwin_editionarea
    global earea_pages

    lappend earea_pages Project

    ${gerwin_editionarea} insert end Project -text Project
    ${gerwin_editionarea} raise Project

    set widget [${gerwin_editionarea} getframe Project]

    TitleFrame ${widget}.editframe -side center -text "Project Properties"

    set editwidget [${widget}.editframe getframe]
    
    ;# Project name 
    frame ${editwidget}.nameline 
    LabelEntry ${editwidget}.nameline.label -label "Project Name:" -labelwidth 16 \
	-textvariable gerwin_cproject_name \
	-helptext "Name of the current project" -width 50

    ;# Project file
    frame ${editwidget}.fileline
    LabelEntry ${editwidget}.fileline.label -label "Project File:" -labelwidth 16 \
	-textvariable gerwin_cproject_file \
	-helptext "Storage file of the current project" -editable false \
	-width 50

    Button ${editwidget}.fileline.button -text "select file" \
	-helptext "Select a file to store the project in" \
	-command "set tempfile \[gui_select_file_to_save\] ; \

                  if {\$tempfile != {}} then {
                      set gerwin_cproject_file \$tempfile
                      gerwin_save_project_file
                  } " \
	-relief groove


    ;# Project author
    frame ${editwidget}.authorline
    LabelEntry ${editwidget}.authorline.label -label "Project Author:" -labelwidth 16 \
	-textvariable gerwin_cproject_author \
	-helptext "Author of the current project" -width 50
    
    pack ${editwidget}.nameline.label -side left -anchor w
    pack ${editwidget}.nameline -side top -anchor w

    pack ${editwidget}.fileline.label -side left -anchor w
    pack ${editwidget}.fileline.button -side left
    pack ${editwidget}.fileline -side top -anchor w

    pack ${editwidget}.authorline.label -side left -anchor w
    pack ${editwidget}.authorline -side top -anchor w

    pack ${editwidget} -side top -fill both -expand true

    pack ${widget}.editframe -side top ;#-fill both -expand true
    
    ;# Scale the notebook
    NoteBook::compute_size ${gerwin_editionarea}

}
    

;#
;# gui_get_free_oct OBJ TYPE CUAD
;#
;# Get a free oct on CUAD for OBJ-TYPE

proc gui_get_free_oct {obj type cuad} {

    global gerwin_entity

    switch $cuad {

	1 {
	    ;# oct for obj must be o2 or o1
	    if {[gui_oct_is_busy $obj $type 1]} then {
		
		return 2

	    } else {

		return 1
	    }

	}

	2 {

	    ;# oct for obj must be o3 or o4 
	    if {[gui_oct_is_busy $obj $type 3]} then {
		
		return 4

	    } else {

		return 3
	    }
	}

	3 {

	    ;# oct for object 1 must be o5 or o6
	    if {[gui_oct_is_busy $obj $type 5]} then {
		
		return 6

	    } else {

		return 5
	    }

	}


	4 {

	    ;# oct for object 1 must be o7 or o8
	    if {[gui_oct_is_busy $obj $type 8]} then {
		
		return 7

	    } else {
		
		return 8
	    }

	}

    }

}




;#
;# gui_oct_is_busy OBJ TYPE OCT
;#
;# See if OCT is busy for OBJ-TYPE

proc gui_oct_is_busy {obj type oct} {

    global gerwin_entity

    switch $type {


	"Entity" {

	    ;# Get the oct array from the entity
	    set octarray $gerwin_entity($obj,octs)

	    if {[lindex $octarray [expr $oct - 1]] != ""} then {

		;# oct busy
		return 1

	    } else {
		
		return 0

	    }
	}
	    

    }
}

;#
;# gui_get_link_point_from_oct {obj type oct} 
;#

proc gui_get_link_point_from_oct {obj type oct} {

    global gerwin_entity
    global gcanvas


    switch $type {


	"Entity" {

	    ;# Get the bbox of the entity
	    set bbox [${gcanvas} bbox taggroup-$type-$obj]
	    set x1 [lindex $bbox 0]
	    set y1 [lindex $bbox 1]
	    set x2 [lindex $bbox 2]
	    set y2 [lindex $bbox 3]
	    

	    ;# Calculate the link point based on the oct
	    switch $oct {

		1 {
		    return [list $x2 [expr ($y1 + (($y2 - $y1) / 4))]]
		}
		2 {
		    return [list [expr ($x1 + ((($x2 - $x1) / 4) * 3))] $y1]
		}
		3 {
		    return [list [expr ($x1 + (($x2 - $x1) / 4))] $y1]
		}
		4 {
		    return [list $x1 [expr ($y1 + (($y2 - $y1) / 4))]]
		}
		5 {
		    return [list $x1 [expr ($y1 + ((($y2 - $y1) /4) * 3))]]
		}
		6 {
		    return [list [expr ($x1 + (($x2 - $x1) / 4))] $y2]
		}
		7 {
		    return [list [expr ($x1 + ((($x2 - $x1) / 4) * 3))]]
		}
		8 {
		    return [list $x2 [expr ($y1 + ((($y2 - $y1) / 4) * 3))]]
		}

	    }

	}

    }

}


;#
;# gui_add_link_to_oct TYPE1 OBJ1 TYPE2 OBJ2 OCT
;#
;# Add a link to OBJ2 to OBJ1's OCT

proc gui_add_link_to_oct {type1 obj1 type2 obj2 oct} {

    global gerwin_entity
    global gerwin_links

    ;# Add the new link to gerwin_links
    lappend gerwin_links [list $type1 $obj1 $type2 $obj2]
    lappend gerwin_links [list $type2 $obj2 $type1 $obj1]

    switch $type1 {


	Entity {
	    
	    set temp [lindex $gerwin_entity($obj1,octs) [expr $oct - 1]]
	    lappend temp [list $type2 $obj2]
	    set gerwin_entity($obj1,octs) \
		[lreplace $gerwin_entity($obj1,octs) [expr $oct - 1] [expr $oct - 1] $temp]

	}


    }

}

;#
;# gui_redraw_oct TYPE OBJ OCT
;#
;# Redraw all links on the OBJ oct named OCT

proc gui_redraw_oct {type obj oct} {

    global gerwin_entity


    switch $type {

	Entity {
	    
	    set oct_content [lindex $gerwin_entity($obj,octs) [expr $oct - 1]]

	    ;# Get the internal pad
	    set nlinks [llength $oct_content]
	    set ipad 

	}
    }


}

;#
;# gui_link {TYPE1 OBJ1} {TYPE2 OBJ2}
;#
;# Makes and draw a link between OBJ1 and OBJ2


proc gui_link {elem1 elem2} {

    global gerwin_entity
    global gm_entity
    global gcanvas


    set type1 [lindex $elem1 0]
    set type2 [lindex $elem2 0]
    set object1 [lindex $elem1 1]
    set object2 [lindex $elem2 1]

    ;# Get bboxes
    set bbox1 [${gcanvas} bbox "taggroup-$type1-$object1"]
    set bbox2 [${gcanvas} bbox "taggroup-$type2-$object2"]

    ;# Obtain the cuads
    set refpoint1x [lindex $bbox1 2]
    set refpoint1y [lindex $bbox1 3]
    set refpoint2x [lindex $bbox2 2]
    set refpoint2y [lindex $bbox2 3]


    set cuad1 [gui_get_cuad $refpoint1x $refpoint1y \
		  $refpoint2x $refpoint2y]

    switch $cuad1 {

	1 { set cuad2 3 }
	2 { set cuad2 4 }
	3 { set cuad2 1 }
	4 { set cuad2 2 }
    }

    ;# Depending of the cuad, we must find an oct link point for both
    ;# objects.

    set oct1 [gui_get_free_oct $object1 $type1 $cuad1]
    ;# We get oct2 depending of oct1
    switch $oct1 {
	8 { set oct2 3}
	7 { set oct2 4}
	6 { set oct2 1}
	5 { set oct2 2}
	4 { set oct2 7}
	3 { set oct2 8}
	2 { set oct2 5}
	1 { set oct2 6}
    }



    ;# Add the new link to the links of both objects octs
    gui_add_link_to_oct $type1 $obj1 $type2 $obj2 $oct1
    gui_add_link_to_oct $type2 $obj2 $type1 $obj1 $oct2

    ;# Redraw the octs links for both objects
    ;#gui_redraw_oct $type1 $obj1 $oct1
    ;#gui_redraw_oct $type2 $obj2 $oct2

    ;# Get the link points
    set lpoint1 [gui_get_link_point_from_oct $object1 $type1 $oct1]
    set lp1x [lindex $lpoint1 0]
    set lp1y [lindex $lpoint1 1]
    set lpoint2 [gui_get_link_point_from_oct $object2 $type2 $oct2]
    set lp2x [lindex $lpoint2 0]
    set lp2y [lindex $lpoint2 1]

    ;# Get the link's intermediate point
    ;# It depend of the cuad

    switch $cuad1 {
	1 {
	    set intpx $lp2x
	    set intpy $lp1y
	}
	2 {
	    set intpx $lp1x
	    set intpy $lp2y
	}
	3 {
	    set intpx $lp2x
	    set intpy $lp1y
	}
	4 {
	    set intpx $lp2x
	    set intpy $lp1y
	}
    }

    ;# make the link
    ${gcanvas} create line $lp1x $lp1y \
	$intpx $intpy \
	$lp2x $lp2y \
	-tag $type1-$object1-$type2-$object2

}


;#
;# gui_link_delete type1 object1 type2 object2
;#
;# Deletes the link


proc gui_link_delete {type1 obj1 type2 obj2} {

    global gcanvas

    ${gcanvas} delete $type1-$obj1-$type2-$obj2



}