Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
summaryrefslogtreecommitdiff
blob: da14d6c86a9d316653813ac87dd4cc24401b7294 (plain)
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
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
#!/bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"

global widget;

image create bitmap dnarw -data  {
#define down_arrow_width 15
#define down_arrow_height 15
static char down_arrow_bits[] = {
	0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,
	0x00,0x80,0xf8,0x8f,0xf0,0x87,0xe0,0x83,
	0xc0,0x81,0x80,0x80,0x00,0x80,0x00,0x80,
	0x00,0x80,0x00,0x80,0x00,0x80
	}
}

proc {set_default_fonts} {} {
global pref tcl_platform
if {[string toupper $tcl_platform(platform)]=="WINDOWS"} {
	set pref(font_normal) {"MS Sans Serif" 8}
	set pref(font_bold) {"MS Sans Serif" 8 bold}
	set pref(font_fix) {Terminal 8}
	set pref(font_italic) {"MS Sans Serif" 8 italic}
} else {
	set pref(font_normal) -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*
	set pref(font_bold) -Adobe-Helvetica-Bold-R-Normal-*-*-120-*-*-*-*-*
	set pref(font_italic) -Adobe-Helvetica-Medium-O-Normal-*-*-120-*-*-*-*-*
	set pref(font_fix) -*-Clean-Medium-R-Normal-*-*-130-*-*-*-*-*
}
}

proc {set_gui_pref} {} {
global pref
foreach wid {Label Text Button Listbox Checkbutton Radiobutton} {
	option add *$wid.font $pref(font_normal)
}
option add *Entry.background #fefefe
option add *Entry.foreground #000000
}

proc {load_pref} {} {
global pref
set_default_fonts
set_gui_pref
set retval [catch {set fid [open "~/.pgaccessrc" r]}]
if {$retval} {
	set pref(rows) 200
	set pref(tvfont) clean
	set pref(autoload) 1
	set pref(lastdb) {}
	set pref(lasthost) localhost
	set pref(lastport) 5432
	set pref(username) {}
	set pref(password) {}
} else {
	while {![eof $fid]} {
		set pair [gets $fid]
		set pref([lindex $pair 0]) [lindex $pair 1]
	}
	close $fid
	set_gui_pref
}
}

proc init {argc argv} {
global dbc host pport tablist mw fldval activetab qlvar mwcount pref
load_pref
set host localhost
set pport 5432
set dbc {}
set tablist [list Tables Queries Views Sequences Functions Reports Forms Scripts Users]
set activetab {}
set qlvar(yoffs) 360
set qlvar(xoffs) 50
set qlvar(reswidth) 150
set qlvar(resfields) {}
set qlvar(ressort) {}
set qlvar(resreturn) {}
set qlvar(rescriteria) {}
set qlvar(restables) {}
set qlvar(critedit) 0
set qlvar(links) {}
set qlvar(ntables) 0
set qlvar(newtablename) {}
set mwcount 0
}

init $argc $argv

proc {sqlw_display} {msg} {
	if {![winfo exists .sqlw]} {return}
	.sqlw.f.t insert end "$msg\n\n"
	.sqlw.f.t see end
	set nrlines [lindex [split [.sqlw.f.t index end] .] 0]
	if {$nrlines>50} {
		.sqlw.f.t delete 1.0 3.0
	}
}

proc {wpg_exec} {db cmd} {
global pgsql
	set pgsql(cmd) "never executed"
	set pgsql(status) "no status yet"
	set pgsql(errmsg) "no error message yet"
	if {[catch {
		sqlw_display $cmd
		set pgsql(cmd) $cmd
		set pgsql(res) [pg_exec $db $cmd]
		set pgsql(status) [pg_result $pgsql(res) -status]
		set pgsql(errmsg) [pg_result $pgsql(res) -error]
	} tclerrmsg]} {
		show_error "Tcl error executing pg_exec $cmd\n\n$tclerrmsg"
		return 0
	}
	return $pgsql(res)
}

proc {wpg_select} {args} {
	sqlw_display "[lindex $args 1]"
	uplevel pg_select $args
}

proc {anfw:add} {} {
global anfw pgsql tiw
	if {$anfw(name)==""} {
		show_error "Empty field name ?"
		focus .anfw.e1
		return
	}		
	if {$anfw(type)==""} {
		show_error "No field type ?"
		focus .anfw.e2
		return
	}
	if {![sql_exec quiet "alter table \"$tiw(tablename)\" add column \"$anfw(name)\" $anfw(type)"]} {
		show_error "Cannot add column\n\nPostgreSQL error: $pgsql(errmsg)"
		return
	}
	Window destroy .anfw
	sql_exec quiet "update pga_layout set colnames=colnames || ' {$anfw(name)}', colwidth=colwidth || ' 150',nrcols=nrcols+1 where tablename='$tiw(tablename)'"
	show_table_information $tiw(tablename)
}

proc {add_new_field} {} {
global ntw
if {$ntw(fldname)==""} {
	show_error "Enter a field name"
	focus .nt.e2
	return
}
if {$ntw(fldtype)==""} {
	show_error "The field type is not specified!"
	return
}
if {($ntw(fldtype)=="varchar")&&($ntw(fldsize)=="")} {
	focus .nt.e3
	show_error "You must specify field size!"
	return
}
if {$ntw(fldsize)==""} then {set sup ""} else {set sup "($ntw(fldsize))"}
if {[regexp $ntw(fldtype) "varchartextdatetime"]} {set supc "'"} else {set supc ""}
if {$ntw(defaultval)==""} then {set sup2 ""} else {set sup2 " DEFAULT $supc$ntw(defaultval)$supc"}
# Checking for field name collision
set inspos end
for {set i 0} {$i<[.nt.lb size]} {incr i} {
	set linie [.nt.lb get $i]
	if {$ntw(fldname)==[string trim [string range $linie 2 33]]} {
		if {[tk_messageBox -title Warning -parent .nt -message "There is another field with the same name: \"$ntw(fldname)\"!\n\nReplace it ?" -type yesno -default yes]=="no"} return
		.nt.lb delete $i
		set inspos $i
		break
	}	 
  }
.nt.lb insert $inspos [format "%1s %-32.32s %-14s%-16s" $ntw(pk) $ntw(fldname) $ntw(fldtype)$sup $sup2$ntw(notnull)]
focus .nt.e2
set ntw(fldname) {}
set ntw(fldsize) {}
set ntw(defaultval) {}
set ntw(pk) " "
}

proc {create_table} {} {
global dbc ntw
if {$ntw(newtablename)==""} then {
	show_error "You must supply a name for your table!"
	focus .nt.etabn
	return
}
if {[.nt.lb size]==0} then {
	show_error "Your table has no fields!"
	focus .nt.e2
	return
}
set fl {}
set pkf {}
foreach line [.nt.lb get 0 end] {
	set fldname "\"[string trim [string range $line 2 33]]\""
	lappend fl "$fldname [string trim [string range $line 35 end]]"
	if {[string range $line 0 0]=="*"} {
		lappend pkf "$fldname"
	}
}
set temp "create table \"$ntw(newtablename)\" ([join $fl ,]"
if {$ntw(constraint)!=""} then {set temp "$temp, constraint \"$ntw(constraint)\""}
if {$ntw(check)!=""} then {set temp "$temp check ($ntw(check))"}
if {[llength $pkf]>0} then {set temp "$temp, primary key([join $pkf ,])"}
set temp "$temp)"
if {$ntw(fathername)!=""} then {set temp "$temp inherits ($ntw(fathername))"}
cursor_clock
if {[sql_exec noquiet $temp]} {
	Window destroy .nt
	cmd_Tables
}
cursor_normal
}

proc {cmd_Delete} {} {
global dbc activetab
if {$dbc==""} return;
set objtodelete [get_dwlb_Selection]
if {$objtodelete==""} return;
set temp {}
switch $activetab {
	Tables {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete table:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			sql_exec noquiet "drop table \"$objtodelete\""
			sql_exec quiet "delete from pga_layout where tablename='$objtodelete'"
			cmd_Tables
		}
	}
	Views {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete view:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			sql_exec noquiet "drop view \"$objtodelete\""
			sql_exec quiet "delete from pga_layout where tablename='$objtodelete'"
			cmd_Views
		}
	}
	Queries {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete query:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			sql_exec quiet "delete from pga_queries where queryname='$objtodelete'"
			sql_exec quiet "delete from pga_layout where tablename='$objtodelete'"
			cmd_Queries
		}
	}
	Scripts {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete script:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			sql_exec quiet "delete from pga_scripts where scriptname='$objtodelete'"
			cmd_Scripts
		}
	}
	Forms {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete form:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			sql_exec quiet "delete from pga_forms where formname='$objtodelete'"
			cmd_Forms
		}
	}
	Sequences {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete sequence:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			sql_exec quiet "drop sequence \"$objtodelete\""
			cmd_Sequences
		}
	}
	Functions {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete function:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			delete_function $objtodelete
			cmd_Functions
		}
	}
	Reports {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete report:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			sql_exec noquiet "delete from pga_reports where reportname='$objtodelete'"
			cmd_Reports
		}
	}
	Users {
		if {[tk_messageBox -title "FINAL WARNING" -parent .dw -message "You are going to delete user:\n\n$objtodelete\n\nProceed ?" -type yesno -default no]=="yes"} {
			sql_exec noquiet "drop user \"$objtodelete\""
			cmd_Users
		}
	}
}
if {$temp==""} return;
}

proc {cmd_Design} {} {
global dbc activetab rbvar uw
if {$dbc==""} return;
if {[.dw.lb curselection]==""} return;
set objname [.dw.lb get [.dw.lb curselection]]
set tablename $objname
switch $activetab {
	Queries {open_query design}
	Views {open_view_design}
	Scripts {design_script $objname}
	Forms {fd_load_form $objname design}
	Reports {
		Window show .rb
		tkwait visibility .rb
		rb_init
		set rbvar(reportname) $objname
		rb_load_report
		set rbvar(justpreview) 0
	}
	Users {
		Window show .uw
		tkwait visibility .uw
		wm transient .uw .dw
		wm title .uw "Design user"
		set uw(username) $objname
		set uw(password) {} ; set uw(verify) {}
		pg_select $dbc "select *,date(valuntil) as valdata from pg_user where usename='$objname'" tup {
			if {$tup(usesuper)=="t"} {
				set uw(createuser) CREATEUSER
			} else {
				set uw(createuser) NOCREATEUSER
			}
			if {$tup(usecreatedb)=="t"} {
				set uw(createdb) CREATEDB
			} else {
				set uw(createdb) NOCREATEDB
			}
			if {$tup(valuntil)!=""} {
				set uw(valid) $tup(valdata)
			} else {
				set uw(valid) {}
			}
		}
		.uw.e1 configure -state disabled
		.uw.b1 configure -text Alter
		focus .uw.e2
	}
}
}

proc {cmd_Forms} {} {
global dbc
cursor_clock
.dw.lb delete 0 end
catch {
	wpg_select $dbc "select formname from pga_forms order by formname" rec {
		.dw.lb insert end $rec(formname)
	}
}
cursor_normal
}

proc {cmd_Functions} {} {
global dbc
set maxim 16384
cursor_clock
catch {
	wpg_select $dbc "select oid from pg_database where datname='template1'" rec {
		set maxim $rec(oid)
	}
}
.dw.lb delete 0 end
catch {
	wpg_select $dbc "select proname from pg_proc where prolang=14 and oid>$maxim order by proname" rec {
		.dw.lb insert end $rec(proname)
	}	
}
cursor_normal
}

proc {cmd_Import_Export} {how} {
global dbc ie_tablename ie_filename activetab
if {$dbc==""} return;
Window show .iew
set ie_tablename {}
set ie_filename {}
set ie_delimiter {}
if {$activetab=="Tables"} {
	set tn [get_dwlb_Selection]
	set ie_tablename $tn
	if {$tn!=""} {set ie_filename "$tn.txt"}
}
.iew.expbtn configure -text $how
}

proc {cmd_Information} {} {
global dbc tiw activetab
if {$dbc==""} return;
if {$activetab!="Tables"} return;
show_table_information [get_dwlb_Selection]
}

proc {cmd_New} {} {
global dbc activetab queryname queryoid cbv funcpar funcname funcret rbvar uw
if {$dbc==""} return;
switch $activetab {
	Tables {
		Window show .nt
		focus .nt.etabn
	}
	Queries {
		Window show .qb
		set queryoid 0
		set queryname {}
		set cbv 0
		.qb.cbv configure -state normal
	}
	Users {
		Window show .uw
		wm transient .uw .dw
		set uw(username) {}
		set uw(password) {}
		set uw(createdb) NOCREATEDB
		set uw(createuser) NOCREATEUSER
		set uw(verify) {}
		set uw(valid) {}
		focus .uw.e1
	}
	Views {
	set queryoid 0
	set queryname {}
		Window show .qb
		set cbv 1
		.qb.cbv configure -state disabled
	}
	Sequences {
	Window show .sqf
	focus .sqf.e1
	}
	Reports {
	Window show .rb ; tkwait visibility .rb ; rb_init ; set rbvar(reportname) {} ; set rbvar(justpreview) 0
	focus .rb.e2
	}
	Forms {
		Window show .fd
		Window show .fdtb
		Window show .fdmenu
		Window show .fda
		fd_init
	}
	Scripts {
		design_script {}
	}
	Functions {
	Window show .fw
	set funcname {}
	set funcpar {}
	set funcret {}
	place .fw.okbtn -y 255
	.fw.okbtn configure -state normal
	.fw.okbtn configure -text Define
	.fw.text1 delete 1.0 end
	focus .fw.e1
	}
}
}

proc {cmd_Open} {} {
global dbc activetab
if {$dbc==""} return;
set objname [get_dwlb_Selection]
if {$objname==""} return;
switch $activetab {
	Tables {open_table $objname}
	Forms {open_form $objname}
	Scripts {execute_script $objname}
	Queries {open_query view}
	Views {open_view}
	Sequences {open_sequence $objname}
	Functions {open_function $objname}
	Reports {open_report $objname}
}
}

proc {cmd_Preferences} {} {
Window show .pw
}

proc {cmd_Queries} {} {
global dbc
.dw.lb delete 0 end
catch {
	wpg_select $dbc "select queryname from pga_queries order by queryname" rec {
		.dw.lb insert end $rec(queryname)
	}
}
}

proc {uw:create_user} {} {
global dbc uw
set uw(username) [string trim $uw(username)]
set uw(password) [string trim $uw(password)]
set uw(verify) [string trim $uw(verify)]
if {$uw(username)==""} {
	show_error "User without name!"
	focus .uw.e1
	return
}
if {$uw(password)!=$uw(verify)} {
	show_error "Passwords do not match!"
	set uw(password) {} ; set uw(verify) {}
	focus .uw.e2
	return
}
set cmd "[.uw.b1 cget -text] user \"$uw(username)\""
if {$uw(password)!=""} {
	set cmd "$cmd WITH PASSWORD \"$uw(password)\" "
}
set cmd "$cmd $uw(createdb) $uw(createuser)"
if {$uw(valid)!=""} {
	set cmd "$cmd VALID UNTIL '$uw(valid)'"
}
if {[sql_exec noquiet $cmd]} {
	Window destroy .uw
	cmd_Users
}
}

proc {cmd_Rename} {} {
global dbc oldobjname activetab
if {$dbc==""} return;
if {$activetab=="Views"} return;
if {$activetab=="Sequences"} return;
if {$activetab=="Functions"} return;
if {$activetab=="Users"} return;
set temp [get_dwlb_Selection]
if {$temp==""} {
	tk_messageBox -title Warning -parent .dw -message "Please select an object first !"
	return;
}
set oldobjname $temp
Window show .rf
}

proc {cmd_Reports} {} {
global dbc
cursor_clock
catch {
	wpg_select $dbc "select reportname from pga_reports order by reportname" rec {
	.dw.lb insert end "$rec(reportname)"
	}
}
cursor_normal
}

proc {cmd_Users} {} {
global dbc
cursor_clock
.dw.lb delete 0 end
catch {
	wpg_select $dbc "select * from pg_user order by usename" rec {
		.dw.lb insert end $rec(usename)
	}
}
cursor_normal
}

proc {cmd_Scripts} {} {
global dbc
cursor_clock
.dw.lb delete 0 end
catch {
	wpg_select $dbc "select scriptname from pga_scripts order by scriptname" rec {
	.dw.lb insert end $rec(scriptname)
	}
}
cursor_normal
}

proc {cmd_Sequences} {} {
global dbc

cursor_clock
.dw.lb delete 0 end
catch {
	wpg_select $dbc "select relname from pg_class where (relname not like 'pg_%') and (relkind='S') order by relname" rec {
		.dw.lb insert end $rec(relname)
	}
}
cursor_normal
}

proc {cmd_Tables} {} {
global dbc
cursor_clock
.dw.lb delete 0 end
foreach tbl [get_tables] {.dw.lb insert end $tbl}
cursor_normal
}

proc {cmd_Views} {} {
global dbc
cursor_clock
.dw.lb delete 0 end
catch {
	wpg_select $dbc "select c.relname,count(c.relname) from pg_class C, pg_rewrite R where (relname !~ '^pg_') and (r.ev_class = C.oid) and (r.ev_type = '1') group by relname" rec {
		if {$rec(count)!=0} {
			set itsaview($rec(relname)) 1
		}
	}
	wpg_select $dbc "select relname from pg_class where (relname !~ '^pg_') and (relkind='r') and (relhasrules) order by relname" rec {
		if {[info exists itsaview($rec(relname))]} {
			.dw.lb insert end $rec(relname)
		}
	}
}
cursor_normal
}

proc {create_drop_down} {base x y w} {
global pref
if {[winfo exists $base.ddf]} {
    return
}
frame $base.ddf -borderwidth 1 -height 75 -relief raised -width 55
listbox $base.ddf.lb -background #fefefe -foreground #000000 -selectbackground #c3c3c3 -borderwidth 1  -font $pref(font_normal)  -highlightthickness 0 -selectborderwidth 0 -yscrollcommand [subst {$base.ddf.sb set}]
scrollbar $base.ddf.sb -borderwidth 1 -command [subst {$base.ddf.lb yview}] -highlightthickness 0 -orient vert
place $base.ddf -x $x -y $y -width $w -height 185 -anchor nw -bordermode ignore
place $base.ddf.lb -x 1 -y 1 -width [expr $w-18] -height 182 -anchor nw -bordermode ignore
place $base.ddf.sb -x [expr $w-15] -y 1 -width 14 -height 183 -anchor nw -bordermode ignore
}

proc {cursor_normal} {} {
	foreach wn [winfo children .] {
		catch {$wn configure -cursor left_ptr}
	}
	update ; update idletasks 
}

proc {cursor_clock} {} {
	foreach wn [winfo children .] {
		catch {$wn configure -cursor watch}
	}
	update ; update idletasks 
}

proc {delete_function} {objname} {
global dbc
wpg_select $dbc "select proargtypes,pronargs from pg_proc where proname='$objname'" rec {
	set funcpar $rec(proargtypes)
	set nrpar $rec(pronargs)
}
set lispar {}
for {set i 0} {$i<$nrpar} {incr i} {
	lappend lispar [get_pgtype [lindex $funcpar $i]]
}
set lispar [join $lispar ,]
sql_exec noquiet "drop function $objname ($lispar)"
}

proc {design_script} {sname} {
global dbc scriptname
Window show .sw
set scriptname $sname
.sw.src delete 1.0 end
if {[string length $sname]==0} return;
wpg_select $dbc "select * from pga_scripts where scriptname='$sname'" rec {
	.sw.src insert end $rec(scriptsource)    
}
}

proc {drag_it} {w x y} {
global draglocation
	set dlo ""
	catch { set dlo $draglocation(obj) }
	if {$dlo != ""} {
		set dx [expr $x - $draglocation(x)]
		set dy [expr $y - $draglocation(y)]
		$w move $dlo $dx $dy
		set draglocation(x) $x
		set draglocation(y) $y
	}
}

proc {drag_start} {wn w x y} {
global draglocation
catch {unset draglocation}
set object [$w find closest $x $y]
if {[lsearch [$wn.c gettags $object] movable]==-1} return;
$wn.c bind movable <Leave> {}
set draglocation(obj) $object
set draglocation(x) $x
set draglocation(y) $y
set draglocation(start) $x
}

proc {drag_stop} {wn w x y} {
global draglocation mw dbc
	set dlo ""
	catch { set dlo $draglocation(obj) }
	if {$dlo != ""} {
		$wn.c bind movable <Leave> "$wn configure -cursor left_ptr"
		$wn configure -cursor left_ptr
		set ctr [get_tag_info $wn $draglocation(obj) v]
		set diff [expr $x-$draglocation(start)]
		if {$diff==0} return;
		set newcw {}
		for {set i 0} {$i<$mw($wn,colcount)} {incr i} {
			if {$i==$ctr} {
				lappend newcw [expr [lindex $mw($wn,colwidth) $i]+$diff]
			} else {
				lappend newcw [lindex $mw($wn,colwidth) $i]
			}
		}
		set mw($wn,colwidth) $newcw
		$wn.c itemconfigure c$ctr -width [expr [lindex $mw($wn,colwidth) $ctr]-5]
		mw_draw_headers $wn
		mw_draw_hgrid $wn
		if {$mw($wn,crtrow)!=""} {mw_show_record $wn $mw($wn,crtrow)}
		for {set i [expr $ctr+1]} {$i<$mw($wn,colcount)} {incr i} {
			$wn.c move c$i $diff 0
		}
		cursor_clock
		sql_exec quiet "update pga_layout set colwidth='$mw($wn,colwidth)' where tablename='$mw($wn,layout_name)'"
		cursor_normal
	}
}

proc {draw_tabs} {} {
global tablist activetab
set ypos 85
foreach tab $tablist {
	label .dw.tab$tab -borderwidth 1  -anchor w -relief raised -text $tab
	place .dw.tab$tab -x 10 -y $ypos -height 25 -width 82 -anchor nw -bordermode ignore
	lower .dw.tab$tab
	bind .dw.tab$tab <Button-1> {tab_click %W}
	incr ypos 25
}
set activetab ""
}

proc {execute_script} {scriptname} {
global dbc
	set ss {}
	wpg_select $dbc "select * from pga_scripts where scriptname='$scriptname'" rec {
		set ss $rec(scriptsource)
	}
    if {[string length $ss] > 0} {
		eval $ss
    }
}

proc {fd_change_coord} {} {
global fdvar fdobj
set i $fdvar(moveitemobj)
set c $fdobj($i,c)
set c [list $fdvar(c_left) $fdvar(c_top) [expr $fdvar(c_left)+$fdvar(c_width)] [expr $fdvar(c_top)+$fdvar(c_height)]]
set fdobj($i,c) $c
.fd.c delete o$i
fd_draw_object $i
fd_draw_hookers $i
}

proc {fd_delete_object} {} {
global fdvar
set i $fdvar(moveitemobj)
.fd.c delete o$i
.fd.c delete hook
set j [lsearch $fdvar(objlist) $i]
set fdvar(objlist) [lreplace $fdvar(objlist) $j $j]
}

proc {fd_draw_hook} {x y} {
.fd.c create rectangle [expr $x-2] [expr $y-2] [expr $x+2] [expr $y+2] -fill black -tags hook
}

proc {fd_draw_hookers} {i} {
global fdobj
foreach {x1 y1 x2 y2} $fdobj($i,c) {}
.fd.c delete hook
fd_draw_hook $x1 $y1
fd_draw_hook $x1 $y2
fd_draw_hook $x2 $y1
fd_draw_hook $x2 $y2
}

proc {fd_draw_object} {i} {
global fdvar fdobj pref
set c $fdobj($i,c)
foreach {x1 y1 x2 y2} $c {}
.fd.c delete o$i
switch $fdobj($i,t) {
	button {
		fd_draw_rectangle $x1 $y1 $x2 $y2 raised #a0a0a0 o$i
		.fd.c create text [expr ($x1+$x2)/2] [expr ($y1+$y2)/2] -text $fdobj($i,l) -font $pref(font_normal) -tags o$i
	}
	text {
		fd_draw_rectangle $x1 $y1 $x2 $y2 sunken #a0a0a0 o$i
	}
	entry {
		fd_draw_rectangle $x1 $y1 $x2 $y2 sunken white o$i
	}
	label {
		.fd.c create text $x1 $y1 -text $fdobj($i,l) -font $pref(font_normal) -anchor nw -tags o$i
	}
	checkbox {
		fd_draw_rectangle [expr $x1+2] [expr $y1+5] [expr $x1+12] [expr $y1+15] raised #a0a0a0 o$i
		.fd.c create text [expr $x1+20] [expr $y1+3] -text $fdobj($i,l) -anchor nw -font $pref(font_normal) -tags o$i
	}
	radio {
		.fd.c create oval [expr $x1+4] [expr $y1+5] [expr $x1+14] [expr $y1+15] -fill white -tags o$i
		.fd.c create text [expr $x1+24] [expr $y1+3] -text $fdobj($i,l) -anchor nw -font $pref(font_normal) -tags o$i
	}
	query {
		.fd.c create oval $x1 $y1 [expr $x1+20] [expr $y1+20] -fill white -tags o$i
		.fd.c create text [expr $x1+5] [expr $y1+4] -text Q  -anchor nw -font $pref(font_normal) -tags o$i
	}
	listbox {
		fd_draw_rectangle $x1 $y1 [expr $x2-12] $y2 sunken white o$i
		fd_draw_rectangle [expr $x2-11] $y1 $x2 $y2 sunken gray o$i
		.fd.c create line [expr $x2-5] $y1 $x2 [expr $y1+10] -fill #808080 -tags o$i
		.fd.c create line [expr $x2-10] [expr $y1+9] $x2 [expr $y1+9] -fill #808080 -tags o$i
		.fd.c create line [expr $x2-10] [expr $y1+9] [expr $x2-5] $y1 -fill white -tags o$i
		.fd.c create line [expr $x2-5] $y2 $x2 [expr $y2-10] -fill #808080 -tags o$i
		.fd.c create line [expr $x2-10] [expr $y2-9] $x2 [expr $y2-9] -fill white -tags o$i
		.fd.c create line [expr $x2-10] [expr $y2-9] [expr $x2-5] $y2 -fill white -tags o$i
	}
}
.fd.c raise hook
}

proc {fd_draw_rectangle} {x1 y1 x2 y2 relief color tag} {
if {$relief=="raised"} {
	set c1 white
	set c2 #606060
} else {
	set c1 #606060
	set c2 white
}
if {$color != "none"} {
	.fd.c create rectangle $x1 $y1 $x2 $y2 -outline "" -fill $color -tags $tag
}
.fd.c create line $x1 $y1 $x2 $y1 -fill $c1 -tags $tag
.fd.c create line $x1 $y1 $x1 $y2 -fill $c1 -tags $tag
.fd.c create line $x1 $y2 $x2 $y2 -fill $c2 -tags $tag
.fd.c create line $x2 $y1 $x2 [expr 1+$y2] -fill $c2 -tags $tag
}

proc {fd_init} {} {
global fdvar fdobj
catch {unset fdvar}
catch {unset fdobj}
catch {.fd.c delete all}
set fdvar(forminame) {udf0}
set fdvar(formname) "New form"
set fdvar(objnum) 0
set fdvar(objlist) {}
set fdvar(oper) none
set fdvar(tool) point
}

proc {fd_item_click} {x y} {
global fdvar fdobj
set fdvar(oper) none
set fdvar(moveitemobj) {}
set il [.fd.c find overlapping $x $y $x $y]
if {[llength $il]==0} return
set tl [.fd.c gettags [lindex $il 0]]
set i [lsearch -glob $tl o*]
if {$i==-1} return
set objnum [string range [lindex $tl $i] 1 end]
set fdvar(moveitemobj) $objnum
set fdvar(moveitemx) $x
set fdvar(moveitemy) $y
set fdvar(oper) move
fd_show_attributes $objnum
fd_draw_hookers $objnum
}

proc {fd_load_form} {name mode} {
global fdvar fdobj dbc
fd_init
set fdvar(formname) $name
if {$mode=="design"} {
	Window show .fd
	Window show .fdmenu
	Window show .fda
	Window show .fdtb
}
#set fid [open "$name.form" r]
#set info [gets $fid]
#close $fid
set res [wpg_exec $dbc "select * from pga_forms where formname='$fdvar(formname)'"]
set info [lindex [pg_result $res -getTuple 0] 1]
pg_result $res -clear
set fdvar(forminame) [lindex $info 0]
set fdvar(objnum) [lindex $info 1]
set fdvar(objlist) [lindex $info 2]
set fdvar(geometry) [lindex $info 3]
set j 0
foreach objinfo [lrange $info 4 end] {
	foreach {t n c x l v} $objinfo {}
	set i [lindex $fdvar(objlist) $j]
	set fdobj($i,t) $t
	set fdobj($i,n) $n
	set fdobj($i,c) $c
	set fdobj($i,l) $l
	set fdobj($i,x) $x
	set fdobj($i,v) $v
	if {$mode=="design"} {fd_draw_object $i}
	incr j
}
if {$mode=="design"} {wm geometry .fd $fdvar(geometry)}
}

proc {fd_mouse_down} {x y} {
global fdvar
set x [expr 3*int($x/3)]
set y [expr 3*int($y/3)]
set fdvar(xstart) $x
set fdvar(ystart) $y
if {$fdvar(tool)=="point"} {
	fd_item_click $x $y
	return
}
set fdvar(oper) draw
}

proc {fd_mouse_move} {x y} {
global fdvar
#set fdvar(msg) "x=$x y=$y"
set x [expr 3*int($x/3)]
set y [expr 3*int($y/3)]
set oper ""
catch {set oper $fdvar(oper)}
if {$oper=="draw"} {
	catch {.fd.c delete curdraw}
	.fd.c create rectangle $fdvar(xstart) $fdvar(ystart) $x $y -tags curdraw
	return
}
if {$oper=="move"} {
	set dx [expr $x-$fdvar(moveitemx)]
	set dy [expr $y-$fdvar(moveitemy)]
	.fd.c move o$fdvar(moveitemobj) $dx $dy
	.fd.c move hook $dx $dy
	set fdvar(moveitemx) $x
	set fdvar(moveitemy) $y
}
}

proc {fd_mouse_up} {x y} {
global fdvar fdobj
set x [expr 3*int($x/3)]
set y [expr 3*int($y/3)]
if {$fdvar(oper)=="move"} {
	set fdvar(moveitem) {}
	set fdvar(oper) none
	set oc $fdobj($fdvar(moveitemobj),c)
	set dx [expr $x - $fdvar(xstart)]
	set dy [expr $y - $fdvar(ystart)]
	set newcoord [list [expr $dx+[lindex $oc 0]] [expr $dy+[lindex $oc 1]] [expr $dx+[lindex $oc 2]] [expr $dy+[lindex $oc 3]]]
	set fdobj($fdvar(moveitemobj),c) $newcoord
	fd_show_attributes $fdvar(moveitemobj)
	fd_draw_hookers $fdvar(moveitemobj)
	return
}
if {$fdvar(oper)!="draw"} return
set fdvar(oper) none
.fd.c delete curdraw
# Check for x2<x1 or y2<y1
if {$x<$fdvar(xstart)} {set temp $x ; set x $fdvar(xstart) ; set fdvar(xstart) $temp}
if {$y<$fdvar(ystart)} {set temp $y ; set y $fdvar(ystart) ; set fdvar(ystart) $temp}
# Check for too small sizes
if {[expr $x-$fdvar(xstart)]<20} {set x [expr $fdvar(xstart)+20]}
if {[expr $y-$fdvar(ystart)]<10} {set y [expr $fdvar(ystart)+10]}
incr fdvar(objnum)
set i $fdvar(objnum)
lappend fdvar(objlist) $i
# t=type , c=coords , n=name , l=label
set fdobj($i,t) $fdvar(tool)
set fdobj($i,c) [list $fdvar(xstart) $fdvar(ystart) $x $y]
set fdobj($i,n) $fdvar(tool)$i
set fdobj($i,l) $fdvar(tool)$i
set fdobj($i,x) {}
set fdobj($i,v) {}
fd_draw_object $i
fd_show_attributes $i
set fdvar(moveitemobj) $i
fd_draw_hookers $i
set fdvar(tool) point
}

proc {fd_save_form} {name} {
global fdvar fdobj dbc
if {[tk_messageBox -title Warning -message "Do you want to save the form into the database ?" -type yesno -default yes]=="no"} {return 1}
if {[string length $fdvar(forminame)]==0} {
	tk_messageBox -title Warning -message "Forms need an internal name, only literals, low case"
	return 0
}
if {[string length $fdvar(formname)]==0} {
	tk_messageBox -title Warning -message "Form must have a name"
	return 0
}
set info [list $fdvar(forminame) $fdvar(objnum) $fdvar(objlist) [wm geometry .fd]]
foreach i $fdvar(objlist) {
	lappend info [list $fdobj($i,t) $fdobj($i,n) $fdobj($i,c) $fdobj($i,x) $fdobj($i,l) $fdobj($i,v)]
}
sql_exec noquiet "delete from pga_forms where formname='$fdvar(formname)'"
regsub -all "'" $info "''" info
sql_exec noquiet "insert into pga_forms values ('$fdvar(formname)','$info')"
cmd_Forms
return 1
}

proc {fd_set_command} {} {
global fdobj fdvar
set i $fdvar(moveitemobj)
set fdobj($i,x) $fdvar(c_cmd)
}

proc {fd_set_name} {} {
global fdvar fdobj
set i $fdvar(moveitemobj)
foreach k $fdvar(objlist) {
	if {($fdobj($k,n)==$fdvar(c_name)) && ($i!=$k)} {
		tk_messageBox -title Warning -message "There is another object (a $fdobj($k,t)) with the same name. Please change it!"
		return
	}
}
set fdobj($i,n) $fdvar(c_name)
fd_show_attributes $i
}

proc {fd_set_text} {} {
global fdvar fdobj
set fdobj($fdvar(moveitemobj),l) $fdvar(c_text)
fd_draw_object $fdvar(moveitemobj)
}

proc {fd_show_attributes} {i} {
global fdvar fdobj
set fdvar(c_info) "$fdobj($i,t) .$fdvar(forminame).$fdobj($i,n)"
set fdvar(c_name) $fdobj($i,n)
set c $fdobj($i,c)
set fdvar(c_top) [lindex $c 1]
set fdvar(c_left) [lindex $c 0]
set fdvar(c_width) [expr [lindex $c 2]-[lindex $c 0]]
set fdvar(c_height) [expr [lindex $c 3]-[lindex $c 1]]
set fdvar(c_cmd) {}
catch {set fdvar(c_cmd) $fdobj($i,x)}
set fdvar(c_var) {}
catch {set fdvar(c_var) $fdobj($i,v)}
set fdvar(c_text) {}
catch {set fdvar(c_text) $fdobj($i,l)}
}

proc {fd_test} {} {
global fdvar fdobj dbc datasets pref
set basewp $fdvar(forminame)
set base .$fdvar(forminame)
if {[winfo exists $base]} {
   wm deiconify $base; return
}
toplevel $base -class Toplevel
wm focusmodel $base passive
wm geometry $base $fdvar(geometry)
wm maxsize $base 785 570
wm minsize $base 1 1
wm overrideredirect $base 0
wm resizable $base 1 1
wm deiconify $base
wm title $base $fdvar(formname)
foreach item $fdvar(objlist) {
set coord $fdobj($item,c)
set name $fdobj($item,n)
set wh "-width [expr 3+[lindex $coord 2]-[lindex $coord 0]]  -height [expr 3+[lindex $coord 3]-[lindex $coord 1]]"
set visual 1
switch $fdobj($item,t) {
	button {
		set cmd {}
		catch {set cmd $fdobj($item,x)}
		button $base.$name  -borderwidth 1 -padx 0 -pady 0 -text "$fdobj($item,l)" -font $pref(font_normal) -command [subst {$cmd}]
	}
	checkbox {
		checkbutton  $base.$name -onvalue t -offvalue f -font $pref(font_normal) -text "$fdobj($item,l)" -variable "$fdobj($item,v)" -borderwidth 1
		set wh {}
	}
	query {
		set visual 0
	set datasets($base.$name,sql) $fdobj($item,x)
		eval "proc $base.$name:open {} {\
			global dbc datasets tup$basewp$name ;\
			catch {unset tup$basewp$name} ;\
			set wn \[focus\] ; cursor_clock ;\
			set res \[wpg_exec \$dbc \"\$datasets($base.$name,sql)\"\] ;\
			pg_result \$res -assign tup$basewp$name ;\
			set fl {} ;\
			foreach fd \[pg_result \$res -lAttributes\] {lappend fl \[lindex \$fd 0\]} ;\
			set datasets($base.$name,fields) \$fl ;\
			set datasets($base.$name,recno) 0 ;\
			set datasets($base.$name,nrecs) \[pg_result \$res -numTuples\] ;\
			cursor_normal ;\
		}"
		eval "proc $base.$name:setsql {sqlcmd} {\
			global datasets ;\
			set datasets($base.$name,sql) \$sqlcmd ;\
		}"
		eval "proc $base.$name:nrecords {} {\
			global datasets ;\
			return \$datasets($base.$name,nrecs) ;\
		}"
		eval "proc $base.$name:crtrecord {} {\
			global datasets ;\
			return \$datasets($base.$name,recno) ;\
		}"
		eval "proc $base.$name:moveto {newrecno} {\
			global datasets ;\
			set datasets($base.$name,recno) \$newrecno ;\
		}"
		eval "proc $base.$name:close {} {
			global tup$basewp$name ;\
			catch {unset tup$basewp$name };\
		}"
		eval "proc $base.$name:fields {} {\
			global datasets ;\
			return \$datasets($base.$name,fields) ;\
		}"
		eval "proc $base.$name:fill {lb fld} {\
			global datasets tup$basewp$name ;\
			\$lb delete 0 end ;\
			for {set i 0} {\$i<\$datasets($base.$name,nrecs)} {incr i} {\
				\$lb insert end \$tup$basewp$name\(\$i,\$fld\) ;\
			}
		}"
		eval "proc $base.$name:movefirst {} {global datasets ; set datasets($base.$name,recno) 0}"
		eval "proc $base.$name:movenext {} {global datasets ; incr datasets($base.$name,recno) ; if {\$datasets($base.$name,recno)==\[$base.$name:nrecords\]} {$base.$name:movelast}}"
		eval "proc $base.$name:moveprevious {} {global datasets ; incr datasets($base.$name,recno) -1 ; if {\$datasets($base.$name,recno)==-1} {$base.$name:movefirst}}"
		eval "proc $base.$name:movelast {} {global datasets ; set datasets($base.$name,recno) \[expr \[$base.$name:nrecords\] -1\]}"
		eval "proc $base.$name:updatecontrols {} {\
			global datasets tup$basewp$name ;\
			set i \$datasets($base.$name,recno) ;\
			foreach fld \$datasets($base.$name,fields) {\
				catch {\
					upvar $basewp$name\(\$fld\) dbvar ;\
					set dbvar \$tup$basewp$name\(\$i,\$fld\) ;\
				}\
			}\
		}"
		eval "proc $base.$name:clearcontrols {} {\
			global datasets ;\
			catch { foreach fld \$datasets($base.$name,fields) {\
				catch {\
					upvar $basewp$name\(\$fld\) dbvar ;\
					set dbvar {} ;\
				}\
			}}\
		}"
	}
	radio {
		radiobutton  $base.$name -font $pref(font_normal) -text "$fdobj($item,l)" -variable "$fdobj($item,v)" -value "$name" -borderwidth 1
		set wh {}
	}
	entry {
		set var {} ; catch {set var $fdobj($item,v)}
		entry $base.$name -bo 1 -ba white -selectborderwidth 0  -highlightthickness 0 
		if {$var!=""} {$base.$name configure -textvar $var}
	}
	text {
		text $base.$name -font $pref(font_normal) -borderwidth 1
	}
	label {
		set wh {}
		label $base.$name -font $pref(font_normal) -anchor nw -padx 0 -pady 0 -text $fdobj($item,l)
	set var {} ; catch {set var $fdobj($item,v)}
	if {$var!=""} {$base.$name configure -textvar $var}
	}
	listbox {
		listbox $base.$name -borderwidth 1 -background white  -highlightthickness 0 -selectborderwidth 0 -font $pref(font_normal) -yscrollcommand [subst {$base.sb$name set}]
	scrollbar $base.sb$name -borderwidth 1 -command [subst {$base.$name yview}] -orient vert  -highlightthickness 0
	eval [subst "place $base.sb$name -x [expr [lindex $coord 2]-14] -y [expr [lindex $coord 1]-1] -width 16 -height [expr 3+[lindex $coord 3]-[lindex $coord 1]] -anchor nw -bordermode ignore"]
	}
}
if $visual {eval [subst "place $base.$name  -x [expr [lindex $coord 0]-1] -y [expr [lindex $coord 1]-1] -anchor nw $wh -bordermode ignore"]}
}
}



proc {get_dwlb_Selection} {} {
set temp [.dw.lb curselection]
if {$temp==""} return "";
return [.dw.lb get $temp]
}

proc {get_pgtype} {oid} {
global dbc
set temp "unknown"
wpg_select $dbc "select typname from pg_type where oid=$oid" rec {
	set temp $rec(typname)
}
return $temp
}

proc {get_tables} {} {
global dbc
set tbl {}
if {[catch {
	wpg_select $dbc "select c.relname,count(c.relname) from pg_class C, pg_rewrite R where (relname !~ '^pg_') and (r.ev_class = C.oid) and (r.ev_type = '1') group by relname" rec {
		if {$rec(count)!=0} {
			set itsaview($rec(relname)) 1
		}
	}
	wpg_select $dbc "select relname from pg_class where (relname !~ '^pg_') and (relkind='r') order by relname" rec {
		if {![regexp "^pga_" $rec(relname)]} then {
			if {![info exists itsaview($rec(relname))]} {
				lappend tbl $rec(relname)
			}
		}
	}
} gterrmsg]} {
	show_error $gterrmsg
}
return $tbl
}

proc {get_tag_info} {wn itemid prefix} {
set taglist [$wn.c itemcget $itemid -tags]
set i [lsearch -glob $taglist $prefix*]
set thetag [lindex $taglist $i]
return [string range $thetag 1 end]
}

proc {mw_canvas_click} {wn x y} {
global mw
if {![mw_exit_edit $wn]} return
# Determining row
for {set row 0} {$row<$mw($wn,nrecs)} {incr row} {
	if {[lindex $mw($wn,rowy) $row]>$y} break
}
incr row -1
if {$y>[lindex $mw($wn,rowy) $mw($wn,last_rownum)]} {set row $mw($wn,last_rownum)}
if {$row<0} return
set mw($wn,row_edited) $row
set mw($wn,crtrow) $row
mw_show_record $wn $row
if {$mw($wn,errorsavingnew)} return
# Determining column
set posx [expr -$mw($wn,leftoffset)]
set col 0
foreach cw $mw($wn,colwidth) {
	incr posx [expr $cw+2]
	if {$x<$posx} break
	incr col
}
set itlist [$wn.c find withtag r$row]
foreach item $itlist {
	if {[get_tag_info $wn $item c]==$col} {
		mw_start_edit $wn $item $x $y
		break
	}
}
}

proc {mw_delete_record} {wn} {
global dbc mw
if {!$mw($wn,updatable)} return;
if {![mw_exit_edit $wn]} return;
set taglist [$wn.c gettags hili]
if {[llength $taglist]==0} return;
set rowtag [lindex $taglist [lsearch -regexp $taglist "^r"]]
set row [string range $rowtag 1 end]
set oid [lindex $mw($wn,keylist) $row]
if {[tk_messageBox -title "FINAL WARNING" -icon question -parent $wn -message "Delete current record ?" -type yesno -default no]=="no"} return
if {[sql_exec noquiet "delete from \"$mw($wn,tablename)\" where oid=$oid"]} {
	$wn.c delete hili
}
}

proc {mw_draw_headers} {wn} {
global mw pref
$wn.c delete header
set posx [expr 5-$mw($wn,leftoffset)]
for {set i 0} {$i<$mw($wn,colcount)} {incr i} {
	set xf [expr $posx+[lindex $mw($wn,colwidth) $i]]
	$wn.c create rectangle $posx 1 $xf 22 -fill #CCCCCC -outline "" -width 0 -tags header
	$wn.c create text [expr $posx+[lindex $mw($wn,colwidth) $i]*1.0/2] 14 -text [lindex $mw($wn,colnames) $i] -tags header -fill navy -font $pref(font_normal)
	$wn.c create line $posx 22 [expr $xf-1] 22 -fill #AAAAAA -tags header
	$wn.c create line [expr $xf-1] 5 [expr $xf-1] 22 -fill #AAAAAA -tags header
	$wn.c create line [expr $xf+1] 5 [expr $xf+1] 22 -fill white -tags header
	$wn.c create line $xf -15000 $xf 15000 -fill #CCCCCC -tags [subst {header movable v$i}]
	set posx [expr $xf+2]
}
set mw($wn,r_edge) $posx
$wn.c bind movable <Button-1> "drag_start $wn %W %x %y"
$wn.c bind movable <B1-Motion> {drag_it %W %x %y}
$wn.c bind movable <ButtonRelease-1> "drag_stop $wn %W %x %y"
$wn.c bind movable <Enter> "$wn configure -cursor left_side"
$wn.c bind movable <Leave> "$wn configure -cursor left_ptr"
}

proc {mw_draw_hgrid} {wn} {
global mw
$wn.c delete hgrid
set posx 10
for {set j 0} {$j<$mw($wn,colcount)} {incr j} {
	set ledge($j) $posx
	incr posx [expr [lindex $mw($wn,colwidth) $j]+2]
	set textwidth($j) [expr [lindex $mw($wn,colwidth) $j]-5]
}
incr posx -6
for {set i 0} {$i<$mw($wn,nrecs)} {incr i} {
	$wn.c create line [expr -$mw($wn,leftoffset)] [lindex $mw($wn,rowy) [expr $i+1]] [expr $posx-$mw($wn,leftoffset)] [lindex $mw($wn,rowy) [expr $i+1]] -fill gray -tags [subst {hgrid g$i}]
}
if {$mw($wn,updatable)} {
	set i $mw($wn,nrecs)
	set posy [expr 14+[lindex $mw($wn,rowy) $mw($wn,nrecs)]]
	$wn.c create line [expr -$mw($wn,leftoffset)] $posy [expr $posx-$mw($wn,leftoffset)] $posy -fill gray -tags [subst {hgrid g$i}]
}
}

proc {mw_draw_new_record} {wn} {
global mw pref
set posx [expr 10-$mw($wn,leftoffset)]
set posy [lindex $mw($wn,rowy) $mw($wn,last_rownum)]
if {$pref(tvfont)=="helv"} {
	set tvfont $pref(font_normal)
} else {
	set tvfont $pref(font_fix)
}
if {$mw($wn,updatable)} {
  for {set j 0} {$j<$mw($wn,colcount)} {incr j} {
	$wn.c create text $posx $posy -text * -tags [subst {r$mw($wn,nrecs) c$j q new unt}]  -anchor nw -font $tvfont -width [expr [lindex $mw($wn,colwidth) $j]-5]
	incr posx [expr [lindex $mw($wn,colwidth) $j]+2]
  }
  incr posy 14
  $wn.c create line [expr -$mw($wn,leftoffset)] $posy [expr $mw($wn,r_edge)-$mw($wn,leftoffset)] $posy -fill gray -tags [subst {hgrid g$mw($wn,nrecs)}]
}
}

proc {mw_edit_text} {wn c k} {
global mw
set bbin [$wn.c bbox r$mw($wn,row_edited)]
switch $k {
	BackSpace { set dp [expr [$wn.c index $mw($wn,id_edited) insert]-1];if {$dp>=0} {$wn.c dchars $mw($wn,id_edited) $dp $dp; set mw($wn,dirtyrec) 1}}
	Home {$wn.c icursor $mw($wn,id_edited) 0}
	End {$wn.c icursor $mw($wn,id_edited) end}
	Left {$wn.c icursor $mw($wn,id_edited) [expr [$wn.c index $mw($wn,id_edited) insert]-1]}
	Delete {}
	Right {$wn.c icursor $mw($wn,id_edited) [expr [$wn.c index $mw($wn,id_edited) insert]+1]}
	Return {if {[mw_exit_edit $wn]} {$wn.c focus {}}}
	Escape {set mw($wn,dirtyrec) 0; $wn.c itemconfigure $mw($wn,id_edited) -text $mw($wn,text_initial_value); $wn.c focus {}}
	default {if {[string compare $c " "]>-1} {$wn.c insert $mw($wn,id_edited) insert $c;set mw($wn,dirtyrec) 1}}
}
set bbout [$wn.c bbox r$mw($wn,row_edited)]
set dy [expr [lindex $bbout 3]-[lindex $bbin 3]]
if {$dy==0} return
set re $mw($wn,row_edited)
$wn.c move g$re 0 $dy
for {set i [expr 1+$re]} {$i<=$mw($wn,nrecs)} {incr i} {
	$wn.c move r$i 0 $dy
	$wn.c move g$i 0 $dy
	set rh [lindex $mw($wn,rowy) $i]
	incr rh $dy
	set mw($wn,rowy) [lreplace $mw($wn,rowy) $i $i $rh]
}
mw_show_record $wn $mw($wn,row_edited)
# Delete is trapped by window interpreted as record delete
#    Delete {$wn.c dchars $mw($wn,id_edited) insert insert; set mw($wn,dirtyrec) 1}
}

proc {mw_exit_edit} {wn} {
global mw dbc
# User has edited the text ?
if {!$mw($wn,dirtyrec)} {
	# No, unfocus text
	$wn.c focus {}
	# For restoring * to the new record position
	if {$mw($wn,id_edited)!=""} {
		if {[lsearch [$wn.c gettags $mw($wn,id_edited)] new]!=-1} {
			$wn.c itemconfigure $mw($wn,id_edited) -text $mw($wn,text_initial_value)
		}
	}
	set mw($wn,id_edited) {};set mw($wn,text_initial_value) {}
	return 1
}
# Trimming the spaces
set fldval [string trim [$wn.c itemcget $mw($wn,id_edited) -text]]
$wn.c itemconfigure $mw($wn,id_edited) -text $fldval
if {[string compare $mw($wn,text_initial_value) $fldval]==0} {
	set mw($wn,dirtyrec) 0
	$wn.c focus {}
	set mw($wn,id_edited) {};set mw($wn,text_initial_value) {}
	return 1
}
cursor_clock
set oid [lindex $mw($wn,keylist) $mw($wn,row_edited)]
set fld [lindex $mw($wn,colnames) [get_tag_info $wn $mw($wn,id_edited) c]]
set fillcolor black
if {$mw($wn,row_edited)==$mw($wn,last_rownum)} {
	set fillcolor red
	set sfp [lsearch $mw($wn,newrec_fields) "\"$fld\""]
	if {$sfp>-1} {
		set mw($wn,newrec_fields) [lreplace $mw($wn,newrec_fields) $sfp $sfp]
		set mw($wn,newrec_values) [lreplace $mw($wn,newrec_values) $sfp $sfp]
	}			
	lappend mw($wn,newrec_fields) "\"$fld\""
	lappend mw($wn,newrec_values) '$fldval'
	# Remove the untouched tag from the object
	$wn.c dtag $mw($wn,id_edited) unt
		$wn.c itemconfigure $mw($wn,id_edited) -fill red
	set retval 1
} else {
	set mw($wn,msg) "Updating record ..."
	after 1000 "set mw($wn,msg) {}"
	regsub -all ' $fldval  \\' sqlfldval
	set retval [sql_exec noquiet "update \"$mw($wn,tablename)\" set \"$fld\"='$sqlfldval' where oid=$oid"]
}
cursor_normal
if {!$retval} {
	set mw($wn,msg) ""
	focus $wn.c
	return 0
}
set mw($wn,dirtyrec) 0
$wn.c focus {}
set mw($wn,id_edited) {};set mw($wn,text_initial_value) {}
return 1
}

proc {mw_load_layout} {wn layoutname} {
global dbc mw
cursor_clock
set mw($wn,layout_name) $layoutname
catch {unset mw($wn,colcount) mw($wn,colnames) mw($wn,colwidth)}
set mw($wn,layout_found) 0
set pgres [wpg_exec $dbc "select *,oid from pga_layout where tablename='$layoutname' order by oid desc"]
set pgs [pg_result $pgres -status]
if {$pgs!="PGRES_TUPLES_OK"} {
	# Probably table pga_layout isn't yet defined
	sql_exec noquiet "create table pga_layout (tablename varchar(64),nrcols int2,colnames text,colwidth text)"
	sql_exec quiet "grant ALL on pga_layout to PUBLIC"
} else {
	set nrlay [pg_result $pgres -numTuples]
	if {$nrlay>=1} {
		set layoutinfo [pg_result $pgres -getTuple 0]
		set mw($wn,colcount) [lindex $layoutinfo 1]
		set mw($wn,colnames)  [lindex $layoutinfo 2]
		set mw($wn,colwidth) [lindex $layoutinfo 3]
		set goodoid [lindex $layoutinfo 4]
		set mw($wn,layout_found) 1
	}
	if {$nrlay>1} {
		show_error "Multiple ($nrlay) layout info found\n\nPlease report the bug!"
		sql_exec quiet "delete from pga_layout where (tablename='$mw($wn,tablename)') and (oid<>$goodoid)"
	}
}
pg_result $pgres -clear
}

proc {mw_pan_left} {wn } {
global mw
if {![mw_exit_edit $wn]} return;
if {$mw($wn,leftcol)==[expr $mw($wn,colcount)-1]} return;
set diff [expr 2+[lindex $mw($wn,colwidth) $mw($wn,leftcol)]]
incr mw($wn,leftcol)
incr mw($wn,leftoffset) $diff
$wn.c move header -$diff 0
$wn.c move q -$diff 0
$wn.c move hgrid -$diff 0
}

proc {mw_pan_right} {wn} {
global mw
if {![mw_exit_edit $wn]} return;
if {$mw($wn,leftcol)==0} return;
incr mw($wn,leftcol) -1
set diff [expr 2+[lindex $mw($wn,colwidth) $mw($wn,leftcol)]]
incr mw($wn,leftoffset) -$diff
$wn.c move header $diff 0
$wn.c move q $diff 0
$wn.c move hgrid $diff 0
}

proc {mw_save_new_record} {wn} {
global dbc mw
if {![mw_exit_edit $wn]} {return 0}
if {$mw($wn,newrec_fields)==""} {return 1}
set mw($wn,msg) "Saving new record ..."
after 1000 "set mw($wn,msg) {}"
set pgres [wpg_exec $dbc "insert into \"$mw($wn,tablename)\" ([join $mw($wn,newrec_fields) ,]) values ([join $mw($wn,newrec_values) ,])" ]
if {[pg_result $pgres -status]!="PGRES_COMMAND_OK"} {
	set errmsg [pg_result $pgres -error]
	show_error "Error inserting new record\n\n$errmsg"
	return 0
}
set oid [pg_result $pgres -oid]
lappend mw($wn,keylist) $oid
pg_result $pgres -clear
# Get bounds of the last record
set lrbb [$wn.c bbox new]
lappend mw($wn,rowy) [lindex $lrbb 3]
$wn.c itemconfigure new -fill black
$wn.c dtag q new
# Replace * from untouched new row elements with "  "
foreach item [$wn.c find withtag unt] {
	$wn.c itemconfigure $item -text "  "
}
$wn.c dtag q unt
incr mw($wn,last_rownum)
incr mw($wn,nrecs)
mw_draw_new_record $wn
set mw($wn,newrec_fields) {}
set mw($wn,newrec_values) {}
return 1
}

proc {mw_scroll_window} {wn par1 args} {
global mw
if {![mw_exit_edit $wn]} return;
if {$par1=="scroll"} {
	set newtop $mw($wn,toprec)
	if {[lindex $args 1]=="units"} {
		incr newtop [lindex $args 0]
	} else {
		incr newtop [expr [lindex $args 0]*25]
		if {$newtop<0} {set newtop 0}
		if {$newtop>=[expr $mw($wn,nrecs)-1]} {set newtop [expr $mw($wn,nrecs)-1]}
	}
} elseif {$par1=="moveto"} {
	set newtop [expr int([lindex $args 0]*$mw($wn,nrecs))]
} else {
	return
}
if {$newtop<0} return;
if {$newtop>=[expr $mw($wn,nrecs)-1]} return;
set dy [expr [lindex $mw($wn,rowy) $mw($wn,toprec)]-[lindex $mw($wn,rowy) $newtop]]
$wn.c move q 0 $dy
$wn.c move hgrid 0 $dy
set newrowy {}
foreach y $mw($wn,rowy) {lappend newrowy [expr $y+$dy]}
set mw($wn,rowy) $newrowy
set mw($wn,toprec) $newtop
mw_set_scrollbar $wn
}

proc {mw_select_records} {wn sql} {
global dbc field mw pgsql pref
set mw($wn,newrec_fields) {}
set mw($wn,newrec_values) {}
if {![mw_exit_edit $wn]} return;
$wn.c delete q
$wn.c delete header
$wn.c delete hgrid
$wn.c delete new
set mw($wn,leftcol) 0
set mw($wn,leftoffset) 0
set mw($wn,crtrow) {}
set mw($wn,msg) "Accessing data. Please wait ..."
$wn.f1.b1 configure -state disabled
cursor_clock
set is_error 1
if {[sql_exec noquiet "BEGIN"]} {
	if {[sql_exec noquiet "declare mycursor cursor for $sql"]} {
		set pgres [wpg_exec $dbc "fetch $pref(rows) in mycursor"]
		if {$pgsql(status)=="PGRES_TUPLES_OK"} {
			set is_error 0
		}
	}
}
if {$is_error} {
	sql_exec quiet "END"
	set mw($wn,msg) {}
	$wn.f1.b1 configure -state normal
	cursor_normal
	set mw($wn,msg) "Error executing : $sql"
	return
}
if {$mw($wn,updatable)} then {set shift 1} else {set shift 0}
#
# checking at least the numer of fields
set attrlist [pg_result $pgres -lAttributes]
if {$mw($wn,layout_found)} then {
	if {  ($mw($wn,colcount) != [expr [llength $attrlist]-$shift]) ||
		  ($mw($wn,colcount) != [llength $mw($wn,colnames)]) ||
		  ($mw($wn,colcount) != [llength $mw($wn,colwidth)]) } then {
		# No. of columns don't match, something is wrong
		# tk_messageBox -title Information -message "Layout info changed !\nRescanning..."
		set mw($wn,layout_found) 0
		sql_exec quiet "delete from pga_layout where tablename='$mw($wn,layout_name)'"
	}
}
# Always take the col. names from the result
set mw($wn,colcount) [llength $attrlist]
if {$mw($wn,updatable)} then {incr mw($wn,colcount) -1}
set mw($wn,colnames) {}
# In defmw($wn,colwidth) prepare mw($wn,colwidth) (in case that not layout_found)
set defmw($wn,colwidth) {}
for {set i 0} {$i<$mw($wn,colcount)} {incr i} {
	lappend mw($wn,colnames) [lindex [lindex $attrlist [expr {$i+$shift}]] 0]
	lappend defmw($wn,colwidth) 150
}
if {!$mw($wn,layout_found)} {
	set mw($wn,colwidth) $defmw($wn,colwidth)
	sql_exec quiet "insert into pga_layout values ('$mw($wn,layout_name)',$mw($wn,colcount),'$mw($wn,colnames)','$mw($wn,colwidth)')"
	set mw($wn,layout_found) 1
}
set mw($wn,nrecs) [pg_result $pgres -numTuples]
if {$mw($wn,nrecs)>$pref(rows)} {
	set mw($wn,msg) "Only first $pref(rows) records from $mw($wn,nrecs) have been loaded"
	set mw($wn,nrecs) $pref(rows)
}
set tagoid {}
if {$pref(tvfont)=="helv"} {
	set tvfont $pref(font_normal)
} else {
	set tvfont $pref(font_fix)
}
# Computing column's left edge
set posx 10
for {set j 0} {$j<$mw($wn,colcount)} {incr j} {
	set ledge($j) $posx
	incr posx [expr {[lindex $mw($wn,colwidth) $j]+2}]
	set textwidth($j) [expr {[lindex $mw($wn,colwidth) $j]-5}]
}
incr posx -6
set posy 24
mw_draw_headers $wn
set mw($wn,updatekey) oid
set mw($wn,keylist) {}
set mw($wn,rowy) {24}
set mw($wn,msg) "Loading maximum $pref(rows) records ..."
set wupdatable $mw($wn,updatable)
for {set i 0} {$i<$mw($wn,nrecs)} {incr i} {
	set curtup [pg_result $pgres -getTuple $i]
	if {$wupdatable} then {lappend mw($wn,keylist) [lindex $curtup 0]}
	for {set j 0} {$j<$mw($wn,colcount)} {incr j} {
		$wn.c create text $ledge($j) $posy -text [lindex $curtup [expr {$j+$shift}]] -tags [subst {r$i c$j q}] -anchor nw -font $tvfont -width $textwidth($j) -fill black
	}
	set bb [$wn.c bbox r$i]
	incr posy [expr {[lindex $bb 3]-[lindex $bb 1]}]
	lappend mw($wn,rowy) $posy
	$wn.c create line 0 [lindex $bb 3] $posx [lindex $bb 3] -fill gray -tags [subst {hgrid g$i}]
	if {$i==25} {update; update idletasks}
}
after 3000 "set mw($wn,msg) {}"
set mw($wn,last_rownum) $i
# Defining position for input data
mw_draw_new_record $wn
pg_result $pgres -clear
sql_exec quiet "END"
set mw($wn,toprec) 0
mw_set_scrollbar $wn
if {$mw($wn,updatable)} then {
	$wn.c bind q <Key> "mw_edit_text $wn %A %K"
} else {
	$wn.c bind q <Key> {}
}
set mw($wn,dirtyrec) 0
$wn.c raise header
$wn.f1.b1 configure -state normal
cursor_normal
}

proc {mw_set_scrollbar} {wn} {
global mw
if {$mw($wn,nrecs)==0} return;
$wn.sb set [expr $mw($wn,toprec)*1.0/$mw($wn,nrecs)] [expr ($mw($wn,toprec)+27.0)/$mw($wn,nrecs)]
}

proc {mw_reload} {wn} {
global mw
set nq $mw($wn,query)
if {($mw($wn,isaquery)) && ("$mw($wn,filter)$mw($wn,sortfield)"!="")} {
	show_error "Sorting and filtering not (yet) available from queries!\n\nPlease enter them in the query definition!"
	set mw($wn,sortfield) {}
	set mw($wn,filter) {}
} else {
	if {$mw($wn,filter)!=""} {
		set nq "$mw($wn,query) where ($mw($wn,filter))"
	} else {
		set nq $mw($wn,query)
	}
	if {$mw($wn,sortfield)!=""} {
		set nq "$nq order by $mw($wn,sortfield)"
	}
}
if {[mw_save_new_record $wn]} {mw_select_records $wn $nq}
}

proc {mw_show_record} {wn row} {
global mw
set mw($wn,errorsavingnew) 0
if {$mw($wn,newrec_fields)!=""} {
	if {$row!=$mw($wn,last_rownum)} {
		if {![mw_save_new_record $wn]} {
					set mw($wn,errorsavingnew) 1
					return
				}
	}
}
set y1 [lindex $mw($wn,rowy) $row]
set y2 [lindex $mw($wn,rowy) [expr $row+1]]
if {$y2==""} {set y2 [expr $y1+14]}
$wn.c dtag hili hili
$wn.c addtag hili withtag r$row
# Making a rectangle arround the record
set x 3
foreach wi $mw($wn,colwidth) {incr x [expr $wi+2]}
$wn.c delete crtrec
$wn.c create rectangle [expr -1-$mw($wn,leftoffset)] $y1 [expr $x-$mw($wn,leftoffset)] $y2 -fill #EEEEEE -outline {} -tags {q crtrec}
$wn.c lower crtrec
}

proc {mw_start_edit} {wn id x y} {
global mw
if {!$mw($wn,updatable)} return
set mw($wn,id_edited) $id
set mw($wn,dirtyrec) 0
set mw($wn,text_initial_value) [$wn.c itemcget $id -text]
focus $wn.c
$wn.c focus $id
$wn.c icursor $id @$x,$y
if {$mw($wn,row_edited)==$mw($wn,nrecs)} {
	if {[$wn.c itemcget $id -text]=="*"} {
		$wn.c itemconfigure $id -text ""
		$wn.c icursor $id 0
	}
}
}

proc {open_database} {} {
global dbc host pport dbname username password newusername newpassword sdbname newdbname newhost newpport pref pgsql
cursor_clock
if {$newusername!=""} {
	set connres [catch {set newdbc [pg_connect -conninfo "host=$newhost port=$newpport dbname=$newdbname user=$newusername password=$newpassword"]} msg]
} else {
	set connres [catch {set newdbc [pg_connect $newdbname -host $newhost -port $newpport]} msg]
}
if {$connres} {
	cursor_normal
	show_error "Error trying to connect to database \"$newdbname\" on host $newhost\n\nPostgreSQL error message: $msg"
	return $msg
} else {
	catch {pg_disconnect $dbc}
	set dbc $newdbc
	set host $newhost
	set pport $newpport
	set dbname $newdbname
	set username $newusername
	set password $newpassword
	set sdbname $dbname
	set pref(lastdb) $dbname
	set pref(lasthost) $host
	set pref(lastport) $pport
	set pref(lastusername) $username
	save_pref
	catch {cursor_normal ; Window hide .dbod}
	tab_click .dw.tabTables
	# Check for pga_ tables
	foreach {table structure} { pga_queries {queryname varchar(64),querytype char(1),querycommand text} pga_forms {formname varchar(64),formsource text} pga_scripts {scriptname varchar(64),scriptsource text} pga_reports {reportname varchar(64),reportsource text,reportbody text,reportprocs text,reportoptions text}} {
		set pgres [wpg_exec $dbc "select relname from pg_class where relname='$table'"]
		if {$pgsql(status)!="PGRES_TUPLES_OK"} {
			show_error "FATAL ERROR searching for PgAccess system tables : $pgsql(errmsg)\nStatus:$pgsql(status)"
			catch {pg_disconnect $dbc}
			exit
		} elseif {[pg_result $pgres -numTuples]==0} {
			pg_result $pgres -clear
			sql_exec quiet "create table $table ($structure)"
			sql_exec quiet "grant ALL on $table to PUBLIC"
		}
		catch {pg_result $pgres -clear}
	}
	# searching for autoexec script
	wpg_select $dbc "select * from pga_scripts where scriptname ~* '^autoexec$'" recd {
		eval $recd(scriptsource)
	}
	return ""
}
}

proc {open_form} {formname} {
	 fd_load_form $formname run
	 fd_test
}

proc {open_function} {objname} {
global dbc funcname funcpar funcret
Window show .fw
place .fw.okbtn -y 400
.fw.okbtn configure -state disabled
.fw.text1 delete 1.0 end
wpg_select $dbc "select * from pg_proc where proname='$objname'" rec {
	set funcname $objname
	set temppar $rec(proargtypes)
	set funcret [get_pgtype $rec(prorettype)]
	set funcnrp $rec(pronargs)
	.fw.text1 insert end $rec(prosrc)
}
set funcpar {}
for {set i 0} {$i<$funcnrp} {incr i} {
	lappend funcpar [get_pgtype [lindex $temppar $i]]
}
set funcpar [join $funcpar ,]
}

proc {open_report} {objname} {
global dbc rbvar
Window show .rb
#tkwait visibility .rb
Window hide .rb
Window show .rpv
rb_init
set rbvar(reportname) $objname
rb_load_report
tkwait visibility .rpv
set rbvar(justpreview) 1
rb_preview
}

proc {open_view_design} {} {
global dbc cbv queryname
set viewname [.dw.lb get [.dw.lb curselection]]
set vd {}
wpg_select $dbc "select pg_get_viewdef('$viewname')as vd" tup {
	set vd $tup(vd)
}
if {$vd==""} {
	show_error "Error retrieving view definition for '$viewname'!"
	return
}
Window show .qb
.qb.text1 delete 0.0 end
.qb.text1 insert end $vd
set cbv 1
.qb.cbv configure -state disabled
set queryname $viewname
}

proc {open_query} {how} {
global dbc queryname mw queryoid

if {[.dw.lb curselection]==""} return;
set queryname [.dw.lb get [.dw.lb curselection]]
if {[set pgres [wpg_exec $dbc "select querycommand,querytype,oid from pga_queries where queryname='$queryname'"]]==0} then {
	show_error "Error retrieving query definition"
	return
}
if {[pg_result $pgres -numTuples]==0} {
	show_error "Query $queryname was not found!"
	pg_result $pgres -clear
	return
}
set tuple [pg_result $pgres -getTuple 0]
set qcmd [lindex $tuple 0]
set qtype [lindex $tuple 1]
set queryoid [lindex $tuple 2]
pg_result $pgres -clear
if {$how=="design"} {
	Window show .qb
	.qb.text1 delete 0.0 end
	.qb.text1 insert end $qcmd
} else {
	if {$qtype=="S"} then {
		set wn [mw_get_new_name]
		set mw($wn,query) [subst $qcmd]
		set mw($wn,updatable) 0
		set mw($wn,isaquery) 1
		mw_create_window
		wm title $wn "Query result: $queryname"
		mw_load_layout $wn $queryname
		mw_select_records $wn $mw($wn,query)
	} else {
		set answ [tk_messageBox -title Warning -type yesno -message "This query is an action query!\n\n[string range $qcmd 0 30] ...\n\nDo you want to execute it?"]
		if {$answ} {
			if {[sql_exec noquiet $qcmd]} {
				tk_messageBox -title Information -message "Your query has been executed without error!"
			}
		}
	}
}
}

proc {mw_free_variables} {wn} {
global mw
	foreach varname [array names mw $wn,*] {
		unset mw($varname)
	}
}

proc {mw_get_new_name} {} {
global mw mwcount
incr mwcount
set wn .mw$mwcount
set mw($wn,dirtyrec) 0
set mw($wn,id_edited) {}
set mw($wn,filter) {}
set mw($wn,sortfield) {}
return .mw$mwcount
}

proc {open_sequence} {objname} {
global dbc seq_name seq_inc seq_start seq_minval seq_maxval
Window show .sqf
set flag 1
wpg_select $dbc "select * from \"$objname\"" rec {
	set flag 0
	set seq_name $objname
	set seq_inc $rec(increment_by)
	set seq_start $rec(last_value)
	.sqf.l3 configure -text "Last value"
	set seq_minval $rec(min_value)
	set seq_maxval $rec(max_value)
	.sqf.defbtn configure -state disabled
	place .sqf.defbtn -x 40 -y 300
}
if {$flag} {
	show_error "Sequence $objname not found!"
} else {
	for {set i 1} {$i<6} {incr i} {
		.sqf.e$i configure -state disabled
	}
	focus .sqf.closebtn
}
}

proc {open_table} {objname} {
global mw sortfield filter
set sortfield {}
set filter {}
set wn [mw_get_new_name]
mw_create_window
set mw($wn,tablename) $objname
mw_load_layout $wn $objname
set mw($wn,query) "select oid,\"$objname\".* from \"$objname\""
set mw($wn,updatable) 1
set mw($wn,isaquery) 0
mw_select_records $wn $mw($wn,query)
catch {wm title $wn "Table viewer : $objname"}
}

proc {open_view} {} {
global mw
set vn [get_dwlb_Selection]
if {$vn==""} return;
set wn [mw_get_new_name]
mw_create_window
set mw($wn,query) "select * from \"$vn\""
set mw($wn,isaquery) 0
set mw($wn,updatable) 0
mw_load_layout $wn $vn
mw_select_records $wn $mw($wn,query)
}

proc {rename_column} {} {
global dbc tiw
	if {[string length [string trim $tiw(new_cn)]]==0} {
		show_error "Field name not entered!"
		return
	}
	set old_name [string trim [string range $tiw(old_cn) 0 31]]
	set tiw(new_cn) [string trim $tiw(new_cn)]
	if {$old_name == $tiw(new_cn)} {
		show_error "New name is the same as the old one !"
		return
	}
	foreach line [.tiw.lb get 0 end] {
		if {[string trim [string range $line 0 31]]==$tiw(new_cn)} {
			show_error "Colum name \"$tiw(new_cn)\" already exists in this table!"
			return
		}
	}
	if {[sql_exec noquiet "alter table \"$tiw(tablename)\" rename column \"$old_name\" to \"$tiw(new_cn)\""]} {
		set temp $tiw(col_id)
		.tiw.lb delete $temp $temp
		.tiw.lb insert $temp "[format %-32.32s $tiw(new_cn)] [string range $tiw(old_cn) 33 end]"
		Window destroy .rcw
	}
}

proc {parameter} {msg} {
global gpw
Window show .gpw
focus .gpw.e1
set gpw(var) ""
set gpw(flag) 0
set gpw(msg) $msg
bind .gpw <Destroy> "set gpw(flag) 1"
grab .gpw
tkwait variable gpw(flag)
if {$gpw(result)} {
	return $gpw(var)
} else {
	return ""
}
}

proc {ql_add_new_table} {} {
global qlvar dbc

if {$qlvar(newtablename)==""} return
set fldlist {}
cursor_clock
wpg_select $dbc "select attnum,attname from pg_class,pg_attribute where (pg_class.relname='$qlvar(newtablename)') and (pg_class.oid=pg_attribute.attrelid) and (attnum>0) order by attnum" rec {
		lappend fldlist $rec(attname)
}
cursor_normal
if {$fldlist==""} {
	show_error "Table $qlvar(newtablename) not found!"
	return
}
set qlvar(tablename$qlvar(ntables)) $qlvar(newtablename)
set qlvar(tablestruct$qlvar(ntables)) $fldlist
set qlvar(tablealias$qlvar(ntables)) "t$qlvar(ntables)"
set qlvar(ali_t$qlvar(ntables)) $qlvar(newtablename)
incr qlvar(ntables)
if {$qlvar(ntables)==1} {
   ql_draw_lizzard
} else {
   ql_draw_table [expr $qlvar(ntables)-1]
}
set qlvar(newtablename) {}
focus .ql.entt
}

proc {ql_compute_sql} {} {
global qlvar
set sqlcmd "select "
#rjr 8Mar1999 added logical return state for results
for {set i 0} {$i<[llength $qlvar(resfields)]} {incr i} {
	if {[lindex $qlvar(resreturn) $i]} {
	if {$sqlcmd!="select "} {set sqlcmd "$sqlcmd, "}
	set sqlcmd "$sqlcmd[lindex $qlvar(restables) $i].\"[lindex $qlvar(resfields) $i]\""
}
}
set tables {}
for {set i 0} {$i<$qlvar(ntables)} {incr i} {
	set thename {}
	catch {set thename $qlvar(tablename$i)}
	if {$thename!=""} {lappend tables "\"$qlvar(tablename$i)\" $qlvar(tablealias$i)"}
}
set sqlcmd "$sqlcmd from [join $tables ,] "
set sup1 {}
if {[llength $qlvar(links)]>0} {
	set sup1 "where "
	foreach link $qlvar(links) {
		if {$sup1!="where "} {set sup1 "$sup1 and "}
		set sup1 "$sup1 ([lindex $link 0].\"[lindex $link 1]\"=[lindex $link 2].\"[lindex $link 3]\")"
	}
}
for {set i 0} {$i<[llength $qlvar(resfields)]} {incr i} {
	set crit [lindex $qlvar(rescriteria) $i]
	if {$crit!=""} {
		if {$sup1==""} {set sup1 "where "}
		if {[string length $sup1]>6} {set sup1 "$sup1 and "}
		set sup1 "$sup1 ([lindex $qlvar(restables) $i].\"[lindex $qlvar(resfields) $i]\" $crit) "        
	}        
}
set sqlcmd "$sqlcmd $sup1"
set sup2 {}
for {set i 0} {$i<[llength $qlvar(ressort)]} {incr i} {
	set how [lindex $qlvar(ressort) $i]
	if {$how!="unsorted"} {
		if {$how=="Ascending"} {set how asc} else {set how desc}
		if {$sup2==""} {set sup2 " order by "} else {set sup2 "$sup2,"}
		set sup2 "$sup2 [lindex $qlvar(restables) $i].\"[lindex $qlvar(resfields) $i]\" $how "
	}
}
set sqlcmd "$sqlcmd $sup2"
set qlvar(sql) $sqlcmd
#tk_messageBox -message $sqlcmd
return $sqlcmd
}

proc {ql_delete_object} {} {
global qlvar
# Checking if there 
set obj [.ql.c find withtag hili]
if {$obj==""} return
# Is object a link ?
if {[ql_get_tag_info $obj link]=="s"} {
	if {[tk_messageBox -title WARNING -icon question -parent .ql -message "Remove link ?" -type yesno -default no]=="no"} return
	set linkid [ql_get_tag_info $obj lkid]
	set qlvar(links) [lreplace $qlvar(links) $linkid $linkid]
	.ql.c delete links
	ql_draw_links
	return
}
# Is object a result field ?
if {[ql_get_tag_info $obj res]=="f"} {
	set col [ql_get_tag_info $obj col]
	if {$col==""} return
	if {[tk_messageBox -title WARNING -icon question -parent .ql -message "Remove field from result ?" -type yesno -default no]=="no"} return
	set qlvar(resfields) [lreplace $qlvar(resfields) $col $col]
	set qlvar(ressort) [lreplace $qlvar(ressort) $col $col]
	set qlvar(resreturn) [lreplace $qlvar(resreturn) $col $col]
	set qlvar(restables) [lreplace $qlvar(restables) $col $col]
	set qlvar(rescriteria) [lreplace $qlvar(rescriteria) $col $col]
	ql_draw_res_panel
	return
}
# Is object a table ?
set tablealias [ql_get_tag_info $obj tab]
set tablename $qlvar(ali_$tablealias)
if {"$tablename"==""} return
if {[tk_messageBox -title WARNING -icon question -parent .ql -message "Remove table $tablename from query ?" -type yesno -default no]=="no"} return
for {set i [expr [llength $qlvar(restables)]-1]} {$i>=0} {incr i -1} {
	if {"$tablename"==[lindex $qlvar(restables) $i]} {
	   set qlvar(resfields) [lreplace $qlvar(resfields) $i $i]
	   set qlvar(ressort) [lreplace $qlvar(ressort) $i $i]
	   set qlvar(resreturn) [lreplace $qlvar(resreturn) $i $i]
	   set qlvar(restables) [lreplace $qlvar(restables) $i $i]
	   set qlvar(rescriteria) [lreplace $qlvar(rescriteria) $i $i]
	}
}
for {set i [expr [llength $qlvar(links)]-1]} {$i>=0} {incr i -1} {
	set thelink [lindex $qlvar(links) $i]
	if {($tablealias==[lindex $thelink 0]) || ($tablealias==[lindex $thelink 2])} {
		set qlvar(links) [lreplace $qlvar(links) $i $i]
	}
}
for {set i 0} {$i<$qlvar(ntables)} {incr i} {
	set temp {}
	catch {set temp $qlvar(tablename$i)}
	if {"$temp"=="$tablename"} {
		unset qlvar(tablename$i)
		unset qlvar(tablestruct$i)
		unset qlvar(tablealias$i)
		break
	}
}
#incr qlvar(ntables) -1
.ql.c delete tab$tablealias
.ql.c delete links
ql_draw_links
ql_draw_res_panel
}

proc {ql_dragit} {w x y} {
global draginfo
if {"$draginfo(obj)" != ""} {
	set dx [expr $x - $draginfo(x)]
	set dy [expr $y - $draginfo(y)]
	if {$draginfo(is_a_table)} {
		set taglist [.ql.c gettags $draginfo(obj)]
		set tabletag [lindex $taglist [lsearch -regexp $taglist "^tab"]]
		$w move $tabletag $dx $dy
		ql_draw_links
	} else {
		$w move $draginfo(obj) $dx $dy
	}
	set draginfo(x) $x
	set draginfo(y) $y
}
}

proc {ql_dragstart} {w x y} {
global draginfo
catch {unset draginfo}
set draginfo(obj) [$w find closest $x $y]
if {[ql_get_tag_info $draginfo(obj) r]=="ect"} {
	# If it'a a rectangle, exit
	set draginfo(obj) {}
	return
}
.ql configure -cursor hand1
.ql.c raise $draginfo(obj)
set draginfo(table) 0
if {[ql_get_tag_info $draginfo(obj) table]=="header"} {
	set draginfo(is_a_table) 1
	.ql.c itemconfigure [.ql.c find withtag hili] -fill black
	.ql.c dtag [.ql.c find withtag hili] hili
	.ql.c addtag hili withtag $draginfo(obj)
	.ql.c itemconfigure hili -fill blue
} else {
	set draginfo(is_a_table) 0
}
set draginfo(x) $x
set draginfo(y) $y
set draginfo(sx) $x
set draginfo(sy) $y
}

proc {ql_dragstop} {x y} {
global draginfo qlvar
# when click Close, ql window is destroyed but event ButtonRelease-1 is fired
if {![winfo exists .ql]} return;
.ql configure -cursor left_ptr
set este {}
catch {set este $draginfo(obj)}
if {$este==""} return
# Re-establish the normal paint order so
# information won't be overlapped by table rectangles
# or link linkes
.ql.c lower $draginfo(obj)
.ql.c lower rect
.ql.c lower links
set qlvar(panstarted) 0
if {$draginfo(is_a_table)} {
	set draginfo(obj) {}
	.ql.c delete links
	ql_draw_links
	return
}
.ql.c move $draginfo(obj) [expr $draginfo(sx)-$x] [expr $draginfo(sy)-$y]
if {($y>$qlvar(yoffs)) && ($x>$qlvar(xoffs))} {
	# Drop position : inside the result panel
	# Compute the offset of the result panel due to panning
	set resoffset [expr [lindex [.ql.c bbox resmarker] 0]-$qlvar(xoffs)]
	set newfld [.ql.c itemcget $draginfo(obj) -text]
	set tabtag [ql_get_tag_info $draginfo(obj) tab]
	set col [expr int(($x-$qlvar(xoffs)-$resoffset)/$qlvar(reswidth))]
	set qlvar(resfields) [linsert $qlvar(resfields) $col $newfld]
	set qlvar(ressort) [linsert $qlvar(ressort) $col unsorted]
	set qlvar(rescriteria) [linsert $qlvar(rescriteria) $col {}]
	set qlvar(restables) [linsert $qlvar(restables) $col $tabtag]
	set qlvar(resreturn) [linsert $qlvar(resreturn) $col yes]
	ql_draw_res_panel    
} else {
	# Drop position : in the table panel
	set droptarget [.ql.c find overlapping $x $y $x $y]
	set targettable {}
	foreach item $droptarget {
		set targettable [ql_get_tag_info $item tab]
		set targetfield [ql_get_tag_info $item f-]
		if {($targettable!="") && ($targetfield!="")} {
			set droptarget $item
			break
		}
	}
	# check if target object isn't a rectangle
	if {[ql_get_tag_info $droptarget rec]=="t"} {set targettable {}}
	if {$targettable!=""} {
		# Target has a table
		# See about originate table
		set sourcetable [ql_get_tag_info $draginfo(obj) tab]
		if {$sourcetable!=""} {
			# Source has also a tab .. tag
			set sourcefield [ql_get_tag_info $draginfo(obj) f-]
			if {$sourcetable!=$targettable} {
				lappend qlvar(links) [list $sourcetable $sourcefield $targettable $targetfield $draginfo(obj) $droptarget]
				ql_draw_links
			}
		}
	}
}
# Erase information about onbject beeing dragged
set draginfo(obj) {}
}

proc {ql_draw_links} {} {
global qlvar
.ql.c delete links
set i 0
foreach link $qlvar(links) {
	# Compute the source and destination right edge
	set sre [lindex [.ql.c bbox tab[lindex $link 0]] 2]
	set dre [lindex [.ql.c bbox tab[lindex $link 2]] 2]
	# Compute field bound boxes
	set sbbox [.ql.c bbox [lindex $link 4]]
	set dbbox [.ql.c bbox [lindex $link 5]]
	# Compute the auxiliary lines
	if {[lindex $sbbox 2] < [lindex $dbbox 0]} {
		# Source object is on the left of target object
		set x1 $sre
		set y1 [expr ([lindex $sbbox 1]+[lindex $sbbox 3])/2]
		.ql.c create line $x1 $y1 [expr $x1+10] $y1 -tags [subst {links lkid$i}] -width 3
		set x2 [lindex $dbbox 0]
		set y2 [expr ([lindex $dbbox 1]+[lindex $dbbox 3])/2]
		.ql.c create line [expr $x2-10] $y2 $x2 $y2 -tags [subst {links lkid$i}] -width 3
		.ql.c create line [expr $x1+10] $y1 [expr $x2-10] $y2 -tags [subst {links lkid$i}] -width 2
	} else {
		# source object is on the right of target object
		set x1 [lindex $sbbox 0]
		set y1 [expr ([lindex $sbbox 1]+[lindex $sbbox 3])/2]
		.ql.c create line $x1 $y1 [expr $x1-10] $y1 -tags [subst {links lkid$i}] -width 3
		set x2 $dre
		set y2 [expr ([lindex $dbbox 1]+[lindex $dbbox 3])/2]
		.ql.c create line $x2 $y2 [expr $x2+10] $y2 -width 3 -tags [subst {links lkid$i}]
		.ql.c create line [expr $x1-10] $y1 [expr $x2+10] $y2 -tags [subst {links lkid$i}] -width 2
	}
	incr i
}
.ql.c lower links
.ql.c bind links <Button-1> {ql_link_click %x %y}
}

proc {ql_draw_lizzard} {} {
global qlvar pref
.ql.c delete all
set posx 20
for {set it 0} {$it<$qlvar(ntables)} {incr it} {
	ql_draw_table $it
}
.ql.c lower rect
.ql.c create line 0 $qlvar(yoffs) 10000 $qlvar(yoffs) -width 3
.ql.c create rectangle 0 $qlvar(yoffs) 10000 5000 -fill #FFFFFF
for {set i [expr 15+$qlvar(yoffs)]} {$i<500} {incr i 15} {
	.ql.c create line $qlvar(xoffs) $i 10000 $i -fill #CCCCCC -tags {resgrid}
}    
for {set i $qlvar(xoffs)} {$i<10000} {incr i $qlvar(reswidth)} {
	.ql.c create line $i [expr 1+$qlvar(yoffs)] $i 10000 -fill #cccccc -tags {resgrid}
}
# Make a marker for result panel offset calculations (due to panning)
.ql.c create line $qlvar(xoffs) $qlvar(yoffs) $qlvar(xoffs) 500 -tags {resmarker resgrid}
.ql.c create rectangle 0 $qlvar(yoffs) $qlvar(xoffs) 5000 -fill #EEEEEE -tags {reshdr}
.ql.c create text 5 [expr 1+$qlvar(yoffs)] -text Field: -anchor nw -font $pref(font_normal) -tags {reshdr}
.ql.c create text 5 [expr 16+$qlvar(yoffs)] -text Table: -anchor nw -font $pref(font_normal) -tags {reshdr}
.ql.c create text 5 [expr 31+$qlvar(yoffs)] -text Sort: -anchor nw -font $pref(font_normal) -tags {reshdr}
.ql.c create text 5 [expr 46+$qlvar(yoffs)] -text Criteria: -anchor nw -font $pref(font_normal) -tags {reshdr}
.ql.c create text 5 [expr 61+$qlvar(yoffs)] -text Return: -anchor nw -font $pref(font_normal) -tags {reshdr}
.ql.c bind mov <Button-1> {ql_dragstart %W %x %y}
.ql.c bind mov <B1-Motion> {ql_dragit %W %x %y}
bind .ql <ButtonRelease-1> {ql_dragstop %x %y}
bind .ql <Button-1> {qlc_click %x %y %W}
bind .ql <B1-Motion> {ql_pan %x %y}
bind .ql <Key-Delete> {ql_delete_object}
}

proc {ql_draw_res_panel} {} {
global qlvar pref
# Compute the offset of the result panel due to panning
set resoffset [expr [lindex [.ql.c bbox resmarker] 0]-$qlvar(xoffs)]
.ql.c delete resp
for {set i 0} {$i<[llength $qlvar(resfields)]} {incr i} {
	.ql.c create text [expr $resoffset+4+$qlvar(xoffs)+$i*$qlvar(reswidth)] [expr 1+$qlvar(yoffs)] -text [lindex $qlvar(resfields) $i] -anchor nw -tags [subst {resf resp col$i}] -font $pref(font_normal)
	.ql.c create text [expr $resoffset+4+$qlvar(xoffs)+$i*$qlvar(reswidth)] [expr 16+$qlvar(yoffs)] -text $qlvar(ali_[lindex $qlvar(restables) $i]) -anchor nw -tags {resp rest} -font $pref(font_normal)
	.ql.c create text [expr $resoffset+4+$qlvar(xoffs)+$i*$qlvar(reswidth)] [expr 31+$qlvar(yoffs)] -text [lindex $qlvar(ressort) $i] -anchor nw -tags {resp sort} -font $pref(font_normal)
	if {[lindex $qlvar(rescriteria) $i]!=""} {
		.ql.c create text [expr $resoffset+4+$qlvar(xoffs)+$i*$qlvar(reswidth)]  [expr $qlvar(yoffs)+46+15*0] -anchor nw -text [lindex $qlvar(rescriteria) $i]  -font $pref(font_normal) -tags [subst {resp cr-c$i-r0}]
	}
	.ql.c create text [expr $resoffset+4+$qlvar(xoffs)+$i*$qlvar(reswidth)] [expr 61+$qlvar(yoffs)] -text [lindex $qlvar(resreturn) $i] -anchor nw -tags {resp retval} -font $pref(font_normal)
}
.ql.c raise reshdr
.ql.c bind resf <Button-1> {ql_resfield_click %x %y}
.ql.c bind sort <Button-1> {ql_swap_sort %W %x %y}
.ql.c bind retval <Button-1> {ql_toggle_return %W %x %y}
}

proc {ql_draw_table} {it} {
global qlvar pref

set posy 10
set allbox [.ql.c bbox rect]
if {$allbox==""} {set posx 10} else {set posx [expr 20+[lindex $allbox 2]]}
set tablename $qlvar(tablename$it)
set tablealias $qlvar(tablealias$it)
.ql.c create text $posx $posy -text "$tablename" -anchor nw -tags [subst {tab$tablealias f-oid mov tableheader}] -font $pref(font_bold)
incr posy 16
foreach fld $qlvar(tablestruct$it) {
   .ql.c create text $posx $posy -text $fld -fill #010101 -anchor nw -tags [subst {f-$fld tab$tablealias mov}] -font $pref(font_normal)
   incr posy 14
}
set reg [.ql.c bbox tab$tablealias]
.ql.c create rectangle [lindex $reg 0] [lindex $reg 1] [lindex $reg 2] [lindex $reg 3] -fill #EEEEEE -tags [subst {rect tab$tablealias}]
.ql.c create line [lindex $reg 0] [expr [lindex $reg 1]+15] [lindex $reg 2] [expr [lindex $reg 1]+15] -tags [subst {rect tab$tablealias}]
.ql.c lower tab$tablealias
.ql.c lower rect
}

proc {ql_get_tag_info} {obj prefix} {
set taglist [.ql.c gettags $obj]
set tagpos [lsearch -regexp $taglist "^$prefix"]
if {$tagpos==-1} {return ""}
set thattag [lindex $taglist $tagpos]
return [string range $thattag [string length $prefix] end]
}

proc {ql_init} {} {
global qlvar
catch {unset qlvar}
set qlvar(yoffs) 360
set qlvar(xoffs) 50
set qlvar(reswidth) 150
set qlvar(resfields) {}
set qlvar(resreturn) {}
set qlvar(ressort) {}
set qlvar(rescriteria) {}
set qlvar(restables) {}
set qlvar(critedit) 0
set qlvar(links) {}
set qlvar(ntables) 0
set qlvar(newtablename) {}
}

proc {ql_link_click} {x y} {
global qlvar

set obj [.ql.c find closest $x $y 1 links]
if {[ql_get_tag_info $obj link]!="s"} return
.ql.c itemconfigure [.ql.c find withtag hili] -fill black
.ql.c dtag [.ql.c find withtag hili] hili
.ql.c addtag hili withtag $obj
.ql.c itemconfigure $obj -fill blue
}

proc {ql_pan} {x y} {
global qlvar
set panstarted 0
catch {set panstarted $qlvar(panstarted) }
if {!$panstarted} return
set dx [expr $x-$qlvar(panstartx)]
set dy [expr $y-$qlvar(panstarty)]
set qlvar(panstartx) $x
set qlvar(panstarty) $y
if {$qlvar(panobject)=="tables"} {
	.ql.c move mov $dx $dy
	.ql.c move links $dx $dy
	.ql.c move rect $dx $dy
} else {
	.ql.c move resp $dx 0
	.ql.c move resgrid $dx 0
	.ql.c raise reshdr
}
}

proc {ql_resfield_click} {x y} {
global qlvar

set obj [.ql.c find closest $x $y]
if {[ql_get_tag_info $obj res]!="f"} return
.ql.c itemconfigure [.ql.c find withtag hili] -fill black
.ql.c dtag [.ql.c find withtag hili] hili
.ql.c addtag hili withtag $obj
.ql.c itemconfigure $obj -fill blue
}

proc {ql_show_sql} {} {
global qlvar pref

set sqlcmd [ql_compute_sql]
.ql.c delete sqlpage
.ql.c create rectangle 0 0 2000 [expr $qlvar(yoffs)-1] -fill #ffffff -tags {sqlpage}
.ql.c create text 10 10 -text $sqlcmd -anchor nw -width 550 -tags {sqlpage} -font $pref(font_normal)
.ql.c bind sqlpage <Button-1> {.ql.c delete sqlpage}
}

proc {ql_swap_sort} {w x y} {
global qlvar
set obj [$w find closest $x $y]
set taglist [.ql.c gettags $obj]
if {[lsearch $taglist sort]==-1} return
set cum [.ql.c itemcget $obj -text]
if {$cum=="unsorted"} {
	set cum Ascending
} elseif {$cum=="Ascending"} {
	set cum Descending
} else {
	set cum unsorted
}
set col [expr int(($x-$qlvar(xoffs))/$qlvar(reswidth))]
set qlvar(ressort) [lreplace $qlvar(ressort) $col $col $cum]
.ql.c itemconfigure $obj -text $cum
}

#rjr 8Mar1999 toggle logical return state for result
proc {ql_toggle_return} {w x y} {
global qlvar
set obj [$w find closest $x $y]
set taglist [.ql.c gettags $obj]
if {[lsearch $taglist retval]==-1} return
set cum [.ql.c itemcget $obj -text]
if {$cum} {
	set cum no
} else {
	set cum yes
} 
set col [expr int(($x-$qlvar(xoffs))/$qlvar(reswidth))]
set qlvar(resreturn) [lreplace $qlvar(resreturn) $col $col $cum]
.ql.c itemconfigure $obj -text $cum
}

proc {qlc_click} {x y w} {
global qlvar pref
set qlvar(panstarted) 0
if {$w==".ql.c"} {
	set canpan 1
	if {$y<$qlvar(yoffs)} {
		if {[llength [.ql.c find overlapping $x $y $x $y]]!=0} {set canpan 0}
			set qlvar(panobject) tables
	} else {
		set qlvar(panobject) result
	}
	if {$canpan} {
		.ql configure -cursor hand1
		set qlvar(panstartx) $x
		set qlvar(panstarty) $y
		set qlvar(panstarted) 1
	}
}
set isedit 0
catch {set isedit $qlvar(critedit)}
# Compute the offset of the result panel due to panning
set resoffset [expr [lindex [.ql.c bbox resmarker] 0]-$qlvar(xoffs)]
if {$isedit} {
	set qlvar(rescriteria) [lreplace $qlvar(rescriteria) $qlvar(critcol) $qlvar(critcol) $qlvar(critval)]
	.ql.c delete cr-c$qlvar(critcol)-r$qlvar(critrow)
	.ql.c create text [expr $resoffset+4+$qlvar(xoffs)+$qlvar(critcol)*$qlvar(reswidth)] [expr $qlvar(yoffs)+46+15*$qlvar(critrow)] -anchor nw -text $qlvar(critval) -font $pref(font_normal) -tags [subst {resp cr-c$qlvar(critcol)-r$qlvar(critrow)}]
	set qlvar(critedit) 0
}
catch {destroy .ql.entc}
if {$y<[expr $qlvar(yoffs)+46]} return
if {$x<[expr $qlvar(xoffs)+5]} return
set col [expr int(($x-$qlvar(xoffs)-$resoffset)/$qlvar(reswidth))]
if {$col>=[llength $qlvar(resfields)]} return
set nx [expr $col*$qlvar(reswidth)+8+$qlvar(xoffs)+$resoffset]
set ny [expr $qlvar(yoffs)+76]
# Get the old criteria value
set qlvar(critval) [lindex $qlvar(rescriteria) $col]
entry .ql.entc -textvar qlvar(critval) -borderwidth 0 -background #FFFFFF -highlightthickness 0 -selectborderwidth 0  -font $pref(font_normal)
place .ql.entc -x $nx -y $ny -height 14
focus .ql.entc
bind .ql.entc <Button-1> {set qlvar(panstarted) 0}
set qlvar(critcol) $col
set qlvar(critrow) 0
set qlvar(critedit) 1
}

proc {rb_add_field} {} {
global rbvar pref
set fldname [.rb.lb get [.rb.lb curselection]]
set newid [.rb.c create text $rbvar(xf_auto) [expr $rbvar(y_rpthdr)+5] -text $fldname -tags [subst {t_l mov ro}] -anchor nw -font $pref(font_normal)]
.rb.c create text $rbvar(xf_auto) [expr $rbvar(y_pghdr)+5] -text $fldname -tags [subst {f-$fldname t_f rg_detail mov ro}] -anchor nw -font $pref(font_normal)
set bb [.rb.c bbox $newid]
incr rbvar(xf_auto) [expr 5+[lindex $bb 2]-[lindex $bb 0]]
}

proc {rb_add_label} {} {
global rbvar pref
set fldname $rbvar(labeltext)
set newid [.rb.c create text $rbvar(xl_auto) [expr $rbvar(y_rpthdr)+5] -text $fldname -tags [subst {t_l mov ro}] -anchor nw -font $pref(font_normal)]
set bb [.rb.c bbox $newid]
incr rbvar(xl_auto) [expr 5+[lindex $bb 2]-[lindex $bb 0]]
}

proc {rb_change_object_font} {} {
global rbvar
.rb.c itemconfigure hili -font -Adobe-[.rb.bfont cget -text]-[rb_get_bold]-[rb_get_italic]-Normal--*-$rbvar(pointsize)-*-*-*-*-*-*
}

proc {rb_delete_object} {} {
if {[tk_messageBox -title Warning -parent .rb -message "Delete current report object?" -type yesno -default no]=="no"} return;
.rb.c delete hili
}

proc {rb_dragit} {w x y} {
global draginfo rbvar
# Showing current region
foreach rg $rbvar(regions) {
	set rbvar(msg) $rbvar(e_$rg)
	if {$rbvar(y_$rg)>$y} break;
}
set temp {}
catch {set temp $draginfo(obj)}
if {"$temp" != ""} {
	set dx [expr $x - $draginfo(x)]
	set dy [expr $y - $draginfo(y)]
	if {$draginfo(region)!=""} {
		set x $draginfo(x) ; $w move bg_$draginfo(region) 0 $dy
	} else {
		$w move $draginfo(obj) $dx $dy
	}
	set draginfo(x) $x
	set draginfo(y) $y
}
}

proc {rb_dragstart} {w x y} {
global draginfo rbvar
focus .rb.c
catch {unset draginfo}
set obj {}
# Only movable objects start dragging
foreach id [$w find overlapping $x $y $x $y] {
	if {[rb_has_tag $id mov]} {
		set obj $id
		break
	}
}
if {$obj==""} return;
set draginfo(obj) $obj
set taglist [.rb.c itemcget $obj -tags]
set i [lsearch -glob $taglist bg_*]
if {$i==-1} {
	set draginfo(region) {}
} else {
	set draginfo(region) [string range [lindex $taglist $i] 3 64]
} 
.rb configure -cursor hand1
.rb.c itemconfigure [.rb.c find withtag hili] -fill black
.rb.c dtag [.rb.c find withtag hili] hili
.rb.c addtag hili withtag $draginfo(obj)
.rb.c itemconfigure hili -fill blue
set draginfo(x) $x
set draginfo(y) $y
set draginfo(sx) $x
set draginfo(sy) $y
# Setting font information
if {[.rb.c type hili]=="text"} {
	set fnta [split [.rb.c itemcget hili -font] -]
	.rb.bfont configure -text [lindex $fnta 2]
	if {[lindex $fnta 3]=="Medium"} then {.rb.lbold configure -relief raised} else {.rb.lbold configure -relief sunken}
	if {[lindex $fnta 4]=="R"} then {.rb.lita configure -relief raised} else {.rb.lita configure -relief sunken}
	set rbvar(pointsize) [lindex $fnta 8]
	if {[rb_has_tag $obj t_f]} {set rbvar(info) "Database field"}
	if {[rb_has_tag $obj t_l]} {set rbvar(info) "Label"}
	if {[.rb.c itemcget $obj -anchor]=="nw"} then {.rb.balign configure -text left} else {.rb.balign configure -text right}
}
}

proc {rb_dragstop} {x y} {
global draginfo rbvar
# when click Close, ql window is destroyed but event ButtonRelease-1 is fired
if {![winfo exists .rb]} return;
.rb configure -cursor left_ptr
set este {}
catch {set este $draginfo(obj)}
if {$este==""} return
# Erase information about object beeing dragged
if {$draginfo(region)!=""} {
	set dy 0
	foreach rg $rbvar(regions) {
		.rb.c move rg_$rg 0 $dy
		if {$rg==$draginfo(region)} {
			set dy [expr $y-$rbvar(y_$draginfo(region))]
		}
		incr rbvar(y_$rg) $dy
	}
#    .rb.c move det 0 [expr $y-$rbvar(y_$draginfo(region))]
	set rbvar(y_$draginfo(region)) $y
	rb_draw_regions
} else {
	# Check if object beeing dragged is inside the canvas
	set bb [.rb.c bbox $draginfo(obj)]
	if {[lindex $bb 0] < 5} {
		.rb.c move $draginfo(obj) [expr 5-[lindex $bb 0]] 0
	}
}
set draginfo(obj) {}
unset draginfo
}

proc {rb_draw_regions} {} {
global rbvar
foreach rg $rbvar(regions) {
	.rb.c delete bg_$rg
	.rb.c create line 0 $rbvar(y_$rg) 5000 $rbvar(y_$rg) -tags [subst {bg_$rg}]
	.rb.c create rectangle 6 [expr $rbvar(y_$rg)-3] 12 [expr $rbvar(y_$rg)+3] -fill black -tags [subst {bg_$rg mov reg}]
	.rb.c lower bg_$rg
}
}

proc {rb_flip_align} {} {
set bb [.rb.c bbox hili]
if {[.rb.balign cget -text]=="left"} then {
	.rb.balign configure -text right
	.rb.c itemconfigure hili -anchor ne
	.rb.c move hili [expr [lindex $bb 2]-[lindex $bb 0]-3] 0
} else {
	.rb.balign configure -text left
	.rb.c itemconfigure hili -anchor nw
	.rb.c move hili [expr [lindex $bb 0]-[lindex $bb 2]+3] 0
}
}

proc {rb_get_bold} {} {
if {[.rb.lbold cget -relief]=="raised"} then {return Medium} else {return Bold}
}

proc {rb_get_italic} {} {
if {[.rb.lita cget -relief]=="raised"} then {return R} else {return O}
}

proc {rb_get_report_fields} {} {
global dbc rbvar
.rb.lb delete 0 end
if {$rbvar(tablename)==""} return ;
#cursor_clock
wpg_select $dbc "select attnum,attname from pg_class,pg_attribute where (pg_class.relname='$rbvar(tablename)') and (pg_class.oid=pg_attribute.attrelid) and (attnum>0) order by attnum" rec {
	.rb.lb insert end $rec(attname)
}
#cursor_normal
}

proc {rb_has_tag} {id tg} {
if {[lsearch [.rb.c itemcget $id -tags] $tg]==-1} then {return 0 } else {return 1}
}

proc {rb_init} {} {
global rbvar
set rbvar(xl_auto) 10
set rbvar(xf_auto) 10
set rbvar(regions) {rpthdr pghdr detail pgfoo rptfoo}
set rbvar(y_rpthdr) 30
set rbvar(y_pghdr) 60
set rbvar(y_detail) 90
set rbvar(y_pgfoo) 120
set rbvar(y_rptfoo) 150
set rbvar(e_rpthdr) {Report header}
set rbvar(e_pghdr) {Page header}
set rbvar(e_detail) {Detail record}
set rbvar(e_pgfoo) {Page footer}
set rbvar(e_rptfoo) {Report footer}
rb_draw_regions
}

proc {rb_load_report} {} {
global rbvar dbc
.rb.c delete all
wpg_select $dbc "select * from pga_reports where reportname='$rbvar(reportname)'" rcd {
	eval $rcd(reportbody)
}
rb_get_report_fields
rb_draw_regions
}

proc {rb_preview} {} {
global dbc rbvar
Window show .rpv
.rpv.fr.c delete all
set ol [.rb.c find withtag ro]
set fields {}
foreach objid $ol {
	set tags [.rb.c itemcget $objid -tags]
	lappend fields [string range [lindex $tags [lsearch -glob $tags f-*]] 2 64]
	lappend fields [lindex [.rb.c coords $objid] 0]
	lappend fields [lindex [.rb.c coords $objid] 1]
	lappend fields $objid
	lappend fields [lindex $tags [lsearch -glob $tags t_*]]
}
# Parsing page header
set py 10
foreach {field x y objid objtype} $fields {
	if {$objtype=="t_l"} {
		.rpv.fr.c create text $x [expr $py+$y] -text [.rb.c itemcget $objid -text]  -font [.rb.c itemcget $objid -font] -anchor nw
	}
}
incr py [expr $rbvar(y_pghdr)-$rbvar(y_rpthdr)]
# Parsing detail group
set di [lsearch $rbvar(regions) detail]
set y_hi $rbvar(y_detail)
set y_lo $rbvar(y_[lindex $rbvar(regions) [expr $di-1]])
wpg_select $dbc "select * from \"$rbvar(tablename)\"" rec {
	foreach {field x y objid objtype} $fields {
		if {($y>=$y_lo) && ($y<=$y_hi)} then {
		if {$objtype=="t_f"} {
			.rpv.fr.c create text $x [expr $py+$y] -text $rec($field) -font [.rb.c itemcget $objid -font] -anchor [.rb.c itemcget $objid -anchor]
		}
		if {$objtype=="t_l"} {
			.rpv.fr.c create text $x [expr $py+$y] -text [.rb.c itemcget $objid -text]  -font [.rb.c itemcget $objid -font] -anchor nw
		}
		}
	}
	incr py [expr $rbvar(y_detail)-$rbvar(y_pghdr)]
}
.rpv.fr.c configure -scrollregion [subst {0 0 1000 $py}]
}

proc {rb_print_report} {} {
set bb [.rpv.fr.c bbox all]
.rpv.fr.c postscript -file "pgaccess-report.ps" -width [expr 10+[lindex $bb 2]-[lindex $bb 0]] -height [expr 10+[lindex $bb 3]-[lindex $bb 1]]
tk_messageBox -title Information -parent .rb -message "The printed image in Postscript is in the file pgaccess-report.ps"
}

proc {rb_save_report} {} {
global rbvar
set prog "set rbvar(tablename) \"$rbvar(tablename)\""
foreach region $rbvar(regions) {
	set prog "$prog ; set rbvar(y_$region) $rbvar(y_$region)"
}
foreach obj [.rb.c find all] {
	if {[.rb.c type $obj]=="text"} {
		set bb [.rb.c bbox $obj]
		if {[.rb.c itemcget $obj -anchor]=="nw"} then {set x [expr [lindex $bb 0]+1]} else {set x [expr [lindex $bb 2]-2]}
		set prog "$prog ; .rb.c create text $x [lindex $bb 1] -font [.rb.c itemcget $obj -font] -anchor [.rb.c itemcget $obj -anchor] -text {[.rb.c itemcget $obj -text]} -tags {[.rb.c itemcget $obj -tags]}"
	}
}
sql_exec noquiet "delete from pga_reports where reportname='$rbvar(reportname)'"
sql_exec noquiet "insert into pga_reports (reportname,reportsource,reportbody) values ('$rbvar(reportname)','$rbvar(tablename)','$prog')"
}

proc {save_pref} {} {
global pref
catch {
	set fid [open "~/.pgaccessrc" w]
	foreach {opt val} [array get pref] { puts $fid "$opt {$val}" }
	close $fid
}
}

proc {show_error} {emsg} {
   bell ; tk_messageBox -title Error -icon error -message $emsg
}

proc {show_table_information} {tblname} {
global dbc tiw activetab indexlist
set tiw(tablename) $tblname
if {$tiw(tablename)==""} return;
Window show .tiw
.tiw.lb delete 0 end
.tiw.ilb delete 0 end
set tiw(isunique) {}
set tiw(isclustered) {}
set tiw(indexfields) {}
wpg_select $dbc "select attnum,attname,typname,attlen,atttypmod,usename,pg_class.oid from pg_class,pg_user,pg_attribute,pg_type where (pg_class.relname='$tiw(tablename)') and (pg_class.oid=pg_attribute.attrelid) and (pg_class.relowner=pg_user.usesysid) and (pg_attribute.atttypid=pg_type.oid) order by attnum" rec {
	set fsize $rec(attlen)
	set fsize1 $rec(atttypmod)
	set ftype $rec(typname)
	if { $fsize=="-1" && $fsize1!="-1" } {
		set fsize $rec(atttypmod)
		incr fsize -4
	}
	if { $fsize1=="-1" && $fsize=="-1" } {
		set fsize ""
	}
	if {$rec(attnum)>0} {.tiw.lb insert end [format "%-33s %-14s %-4s" $rec(attname) $ftype $fsize]}
	set tiw(owner) $rec(usename)
	set tiw(tableoid) $rec(oid)
	set tiw(f$rec(attnum)) $rec(attname)
}
set tiw(indexlist) {}
wpg_select $dbc "select oid,indexrelid from pg_index where (pg_class.relname='$tiw(tablename)') and (pg_class.oid=pg_index.indrelid)" rec {
	lappend tiw(indexlist) $rec(oid)
	wpg_select $dbc "select relname from pg_class where oid=$rec(indexrelid)" rec1 {
		.tiw.ilb insert end $rec1(relname)
	}
}
}

proc {sql_exec} {how cmd} {
global dbc pgsql
if {[set pgr [wpg_exec $dbc $cmd]]==0} {
	return 0
}
if {($pgsql(status)=="PGRES_COMMAND_OK") || ($pgsql(status)=="PGRES_TUPLES_OK")} {
	pg_result $pgr -clear
	return 1
}	
if {$how != "quiet"} {
	show_error "Error executing query\n\n$cmd\n\nPostgreSQL error message:\n$pgsql(errmsg)\nPostgreSQL status:$pgsql(status)"
}
pg_result $pgr -clear
return 0
}

proc {tab_click} {w} {
global dbc tablist activetab pref
if {$dbc==""} return;
set curtab [$w cget -text]
#if {$activetab==$curtab} return;
.dw.btndesign configure -state disabled
if {$activetab!=""} {
	place .dw.tab$activetab -x 10
	.dw.tab$activetab configure -font $pref(font_normal)
}
$w configure -font $pref(font_bold)
place $w -x 7
place .dw.lmask -x 80 -y [expr 86+25*[lsearch -exact $tablist $curtab]]
set activetab $curtab
# Tabs where button Design is enabled
if {[lsearch {Scripts Queries Views Reports Forms Users} $activetab]!=-1} {
	.dw.btndesign configure -state normal
}
.dw.lb delete 0 end
cmd_$curtab
}

proc {tiw_show_index} {} {
global tiw dbc
set cs [.tiw.ilb curselection]
if {$cs==""} return
set idxname [.tiw.ilb get $cs]
wpg_select $dbc "select pg_index.*,pg_class.oid from pg_index,pg_class where pg_class.relname='$idxname' and pg_class.oid=pg_index.indexrelid" rec {
	if {$rec(indisunique)=="t"} {
		set tiw(isunique) Yes
	} else {
		set tiw(isunique) No
	}
	if {$rec(indisclustered)=="t"} {
		set tiw(isclustered) Yes
	} else {
		set tiw(isclustered) No
	}
	set tiw(indexfields) {}
	foreach field $rec(indkey) {
		if {$field!=0} {
#            wpg_select $dbc "select attname from pg_attribute where attrelid=$tiw(tableoid) and attnum=$field" rec1 {
#                set tiw(indexfields) "$tiw(indexfields) $rec1(attname)"
#            }
		set tiw(indexfields) "$tiw(indexfields) $tiw(f$field)"
		}

	}
}
set tiw(indexfields) [string trim $tiw(indexfields)]
}

proc {vacuum} {} {
global dbc dbname sdbname pgsql
if {$dbc==""} return;
set sdbname "vacuuming database $dbname ..."
cursor_clock
set pgres [wpg_exec $dbc "vacuum;"]
catch {pg_result $pgres -clear}
cursor_normal
set sdbname $dbname
}

proc {main} {argc argv} {
global pref newdbname newpport newhost newusername newpassword dbc tcl_platform
if {[string toupper $tcl_platform(platform)]=="WINDOWS"} {
	load libpgtcl.dll
} else {
	load libpgtcl.so
}
catch {draw_tabs}
set newusername {}
set newpassword {}
if {$argc>0} {
	set newdbname [lindex $argv 0]
	set newhost localhost
	set newpport 5432
	open_database
} elseif {$pref(autoload) && ($pref(lastdb)!="")} {
	set newdbname $pref(lastdb)
	set newhost $pref(lasthost)
	set newpport $pref(lastport)
	catch {set newusername $pref(lastusername)}
	if {[set openmsg [open_database]]!=""} {
		if {[regexp "no password supplied" $openmsg]} {
			Window show .dbod
			focus .dbod.epassword
			wm transient .dbod .dw
		}
	}
	
}
wm protocol .dw WM_DELETE_WINDOW {
	catch {pg_disconnect $dbc}
	exit }
}

proc {Window} {args} {
global vTcl
	set cmd [lindex $args 0]
	set name [lindex $args 1]
	set newname [lindex $args 2]
	set rest [lrange $args 3 end]
	if {$name == "" || $cmd == ""} {return}
	if {$newname == ""} {
		set newname $name
	}
	set exists [winfo exists $newname]
	switch $cmd {
		show {
			if {$exists == "1" && $name != "."} {wm deiconify $name; return}
			if {[info procs vTclWindow(pre)$name] != ""} {
				eval "vTclWindow(pre)$name $newname $rest"
			}
			if {[info procs vTclWindow$name] != ""} {
				eval "vTclWindow$name $newname $rest"
			}
			if {[info procs vTclWindow(post)$name] != ""} {
				eval "vTclWindow(post)$name $newname $rest"
			}
		}
		hide    { if $exists {wm withdraw $newname; return} }
		iconify { if $exists {wm iconify $newname; return} }
		destroy { if $exists {destroy $newname; return} }
	}
}

proc vTclWindow. {base} {
	if {$base == ""} {
		set base .
	}
	wm focusmodel $base passive
	wm geometry $base 1x1+0+0
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm withdraw $base
	wm title $base "vt.tcl"
}

proc vTclWindow.about {base} {
	if {$base == ""} {
		set base .about
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 471x177+168+243
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm title $base "About"
	label $base.l1  -borderwidth 3 -font -Adobe-Helvetica-Bold-R-Normal-*-*-180-*-*-*-*-*  -relief ridge -text PgAccess 
	label $base.l2  -relief groove  -text {A Tcl/Tk interface to
PostgreSQL
by Constantin Teodorescu} 
	label $base.l3  -borderwidth 0 -relief sunken -text {v 0.96}
	label $base.l4  -relief groove  -text {You will always get the latest version at:
http://www.flex.ro/pgaccess

Suggestions : teo@flex.ro} 
	button $base.b1  -borderwidth 1 -command {Window destroy .about} -text Ok 
	place $base.l1  -x 10 -y 10 -width 196 -height 103 -anchor nw -bordermode ignore 
	place $base.l2  -x 10 -y 115 -width 198 -height 55 -anchor nw -bordermode ignore 
	place $base.l3  -x 145 -y 80 -anchor nw -bordermode ignore 
	place $base.l4  -x 215 -y 10 -width 246 -height 103 -anchor nw -bordermode ignore 
	place $base.b1  -x 295 -y 130 -width 105 -height 28 -anchor nw -bordermode ignore
}

proc vTclWindow.dbod {base} {
	if {$base == ""} {
		set base .dbod
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel \
		-cursor left_ptr
	wm focusmodel $base passive
	wm geometry $base 282x180+358+333
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base "Open database"
	label $base.lhost \
		-borderwidth 0 -text Host 
	entry $base.ehost \
		-background #fefefe -borderwidth 1 -highlightthickness 1 \
		-selectborderwidth 0 -textvariable newhost 
	bind $base.ehost <Key-Return> {
		focus .dbod.epport
	}
	label $base.lport \
		-borderwidth 0 -text Port 
	entry $base.epport \
		-background #fefefe -borderwidth 1 -highlightthickness 1 \
		-selectborderwidth 0 -textvariable newpport 
	bind $base.epport <Key-Return> {
		focus .dbod.edbname
	}
	label $base.ldbname \
		-borderwidth 0 -text Database 
	entry $base.edbname \
		-background #fefefe -borderwidth 1 -highlightthickness 1 \
		-selectborderwidth 0 -textvariable newdbname 
	bind $base.edbname <Key-Return> {
		focus .dbod.eusername
	.dbod.eusername selection range 0 end
	}
	label $base.lusername \
		-borderwidth 0 -text Username 
	entry $base.eusername \
		-background #fefefe -borderwidth 1 -highlightthickness 1 \
		-selectborderwidth 0 -textvariable newusername 
	bind $base.eusername <Key-Return> {
		focus .dbod.epassword
	}
	label $base.lpassword \
		-borderwidth 0 -text Password 
	entry $base.epassword \
		-background #fefefe -borderwidth 1 -highlightthickness 1 \
		-selectborderwidth 0 -textvariable newpassword -show "*"
	bind $base.epassword <Key-Return> {
		focus .dbod.opbtu
	}
	button $base.opbtu \
		-borderwidth 1 -command open_database -text Open 
	bind $base.opbtu <Key-Return> {
		open_database
	}
	button $base.canbut \
		-borderwidth 1 -command {Window hide .dbod} -text Cancel 
	place $base.lhost \
		-x 35 -y 7 -anchor nw -bordermode ignore 
	place $base.ehost \
		-x 100 -y 5 -anchor nw -bordermode ignore 
	place $base.lport \
		-x 35 -y 32 -anchor nw -bordermode ignore 
	place $base.epport \
		-x 100 -y 30 -anchor nw -bordermode ignore 
	place $base.ldbname \
		-x 35 -y 57 -anchor nw -bordermode ignore 
	place $base.edbname \
		-x 100 -y 55 -anchor nw -bordermode ignore 
	place $base.lusername \
		-x 35 -y 82 -anchor nw -bordermode ignore 
	place $base.eusername \
		-x 100 -y 80 -anchor nw -bordermode ignore 
	place $base.lpassword \
		-x 35 -y 107 -anchor nw -bordermode ignore 
	place $base.epassword \
		-x 100 -y 105 -anchor nw -bordermode ignore 
	place $base.opbtu \
		-x 70 -y 140 -width 60 -height 26 -anchor nw -bordermode ignore 
	place $base.canbut \
		-x 150 -y 140 -width 60 -height 26 -anchor nw -bordermode ignore 
}

proc vTclWindow.dw {base} {
global pref
	if {$base == ""} {
		set base .dw
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel \
		-background #efefef -cursor left_ptr
	wm focusmodel $base passive
	wm geometry $base 322x355+96+172
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base "PostgreSQL access"
	label $base.labframe \
		-relief raised 
	listbox $base.lb \
		-background #fefefe \
		-selectbackground #c3c3c3 \
		-foreground black -highlightthickness 0 -selectborderwidth 0 \
		-yscrollcommand {.dw.sb set} 
	bind $base.lb <Double-Button-1> {
		cmd_Open
	}
	button $base.btnnew \
		-borderwidth 1 -command cmd_New -text New 
	button $base.btnopen \
		-borderwidth 1 -command cmd_Open -text Open 
	button $base.btndesign \
		-borderwidth 1 -command cmd_Design -text Design 
	label $base.lmask \
		-borderwidth 0 \
		-text {  } 
	label $base.label22 \
		-borderwidth 1 \
		-relief raised 
	menubutton $base.menubutton23 \
		-borderwidth 1 -font $pref(font_normal) \
		-menu .dw.menubutton23.01 -padx 4 -pady 3 -text Database 
	menu $base.menubutton23.01 \
		-borderwidth 1 -font $pref(font_normal) \
		-tearoff 0 
	$base.menubutton23.01 add command \
		\
		-command {
Window show .dbod
set newhost $host
set newpport $pport
focus .dbod.edbname
.dbod.edbname selection range 0 end} \
		-label Open -font $pref(font_normal)
	$base.menubutton23.01 add command \
		\
		-command {.dw.lb delete 0 end
set dbc {}
set dbname {}
set sdbname {}} \
		-label Close 
	$base.menubutton23.01 add command \
		-command vacuum -label Vacuum 
	$base.menubutton23.01 add separator
	$base.menubutton23.01 add command \
		-command {cmd_Import_Export Import} -label {Import table} 
	$base.menubutton23.01 add command \
		-command {cmd_Import_Export Export} -label {Export table} 
	$base.menubutton23.01 add separator
	$base.menubutton23.01 add command \
		-command cmd_Preferences -label Preferences 
	$base.menubutton23.01 add command \
		-command "Window show .sqlw" -label "SQL window" 
	$base.menubutton23.01 add separator
	$base.menubutton23.01 add command \
		-command {catch {pg_disconnect $dbc}
save_pref
exit} -label Exit 
	label $base.lshost \
		-relief groove -text localhost -textvariable host 
	label $base.lsdbname \
		-anchor w \
		-relief groove -textvariable sdbname 
	scrollbar $base.sb \
		-borderwidth 1 -command {.dw.lb yview} -orient vert 
	menubutton $base.mnob \
		-borderwidth 1 \
		-menu .dw.mnob.m -font $pref(font_normal) -text Object 
	menu $base.mnob.m \
		-borderwidth 1 -font $pref(font_normal) \
		-tearoff 0 
	$base.mnob.m add command \
		-command cmd_New -font $pref(font_normal) -label New 
	$base.mnob.m add command \
		-command {cmd_Delete } -label Delete 
	$base.mnob.m add command \
		-command {cmd_Rename } -label Rename 
	$base.mnob.m add command \
		-command cmd_Information -label Information 
	menubutton $base.mhelp \
		-borderwidth 1 \
		-menu .dw.mhelp.m -font $pref(font_normal) -text Help 
	menu $base.mhelp.m \
		-borderwidth 1 -font $pref(font_normal) \
		-tearoff 0 
	$base.mhelp.m add command \
		-label Contents 
	$base.mhelp.m add command \
		-label PostgreSQL 
	$base.mhelp.m add separator
	$base.mhelp.m add command \
		-command {Window show .about} -label About 
	place $base.labframe \
		-x 80 -y 30 -width 236 -height 300 -anchor nw -bordermode ignore 
	place $base.lb \
		-x 90 -y 75 -width 205 -height 243 -anchor nw -bordermode ignore 
	place $base.btnnew \
		-x 90 -y 40 -width 60 -height 25 -anchor nw -bordermode ignore 
	place $base.btnopen \
		-x 165 -y 40 -width 60 -height 25 -anchor nw -bordermode ignore 
	place $base.btndesign \
		-x 235 -y 40 -width 60 -height 25 -anchor nw -bordermode ignore 
	place $base.lmask \
		-x 155 -y 45 -width 10 -height 23 -anchor nw -bordermode ignore 
	place $base.label22 \
		-x 0 -y 0 -width 396 -height 23 -anchor nw -bordermode ignore 
	place $base.menubutton23 \
		-x 0 -y 3 -width 63 -height 17 -anchor nw -bordermode ignore 
	place $base.lshost \
		-x 3 -y 335 -width 91 -height 20 -anchor nw -bordermode ignore 
	place $base.lsdbname \
		-x 95 -y 335 -width 223 -height 20 -anchor nw -bordermode ignore 
	place $base.sb \
		-x 295 -y 74 -width 18 -height 245 -anchor nw -bordermode ignore 
	place $base.mnob \
		-x 70 -y 2 -width 44 -height 19 -anchor nw -bordermode ignore 
	place $base.mhelp \
		-x 280 -y 1 -height 20 -anchor nw -bordermode ignore 
}

proc vTclWindow.fw {base} {
	if {$base == ""} {
		set base .fw
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 306x288+233+130
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm title $base "Function"
	label $base.l1  -borderwidth 0 -text Name 
	entry $base.e1  -background #fefefe -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable funcname 
	label $base.l2  -borderwidth 0 -text Parameters 
	entry $base.e2  -background #fefefe -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable funcpar 
	label $base.l3  -borderwidth 0 -text Returns 
	entry $base.e3  -background #fefefe -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable funcret 
	text $base.text1  -background #fefefe -borderwidth 1  -highlightthickness 1 -selectborderwidth 0 -wrap word 
	button $base.okbtn  -borderwidth 1  -command {
			if {$funcname==""} {
				show_error "You must supply a name for this function!"
			} elseif {$funcret==""} {
				show_error "You must supply a return type!"
			} else {
				set funcbody [.fw.text1 get 1.0 end]
				regsub -all "\n" $funcbody " " funcbody
				if {[sql_exec noquiet "create function $funcname ($funcpar) returns $funcret as '$funcbody' language 'sql'"]} {
					Window destroy .fw
					tk_messageBox -title PostgreSQL -message "Function created!"
					tab_click .dw.tabFunctions
				}
								
			}
		}  -state disabled -text Define 
	button $base.cancelbtn  -borderwidth 1 -command {Window destroy .fw} -text Close 
	place $base.l1  -x 15 -y 18 -anchor nw -bordermode ignore 
	place $base.e1  -x 95 -y 15 -width 198 -height 22 -anchor nw -bordermode ignore 
	place $base.l2  -x 15 -y 48 -anchor nw -bordermode ignore 
	place $base.e2  -x 95 -y 45 -width 198 -height 22 -anchor nw -bordermode ignore 
	place $base.l3  -x 15 -y 78 -anchor nw -bordermode ignore 
	place $base.e3  -x 95 -y 75 -width 198 -height 22 -anchor nw -bordermode ignore 
	place $base.text1  -x 15 -y 105 -width 275 -height 141 -anchor nw -bordermode ignore 
	place $base.okbtn  -x 90 -y 400 -anchor nw -bordermode ignore 
	place $base.cancelbtn  -x 160 -y 255 -anchor nw -bordermode ignore
}

proc vTclWindow.iew {base} {
	if {$base == ""} {
		set base .iew
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 287x151+259+304
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm title $base "Import-Export table"
	label $base.l1  -borderwidth 0 -text {Table name} 
	entry $base.e1  -background #fefefe -borderwidth 1 -textvariable ie_tablename 
	label $base.l2  -borderwidth 0 -text {File name} 
	entry $base.e2  -background #fefefe -borderwidth 1 -textvariable ie_filename 
	label $base.l3  -borderwidth 0 -text {Field delimiter} 
	entry $base.e3  -background #fefefe -borderwidth 1 -textvariable ie_delimiter 
	button $base.expbtn  -borderwidth 1  -command {if {$ie_tablename==""} {
	show_error "You have to supply a table name!"
} elseif {$ie_filename==""} {
	show_error "You have to supply a external file name!"
} else {
	if {$ie_delimiter==""} {
		set sup ""
	} else {
		set sup " USING DELIMITERS '$ie_delimiter'"
	}
	if {[.iew.expbtn cget -text]=="Import"} {
		set oper "FROM"
	} else {
		set oper "TO"
	}
		if {$oicb} {
				set sup2 " WITH OIDS "
		} else {
				set sup2 ""
		}
	set sqlcmd "COPY $ie_tablename $sup2 $oper '$ie_filename'$sup"
	cursor_clock
	if {[sql_exec noquiet $sqlcmd]} {
		tk_messageBox -title Information -parent .iew -message "Operation completed!"
		Window destroy .iew
	}
	cursor_normal
}}  -text Export 
	button $base.cancelbtn  -borderwidth 1 -command {Window destroy .iew} -text Cancel 
	checkbutton $base.oicb  -borderwidth 1  -text {with OIDs} -variable oicb 
	place $base.l1  -x 25 -y 15 -anchor nw -bordermode ignore 
	place $base.e1  -x 115 -y 10 -height 22 -anchor nw -bordermode ignore 
	place $base.l2  -x 25 -y 45 -anchor nw -bordermode ignore 
	place $base.e2  -x 115 -y 40 -height 22 -anchor nw -bordermode ignore 
	place $base.l3  -x 25 -y 75 -height 18 -anchor nw -bordermode ignore 
	place $base.e3  -x 115 -y 74 -width 33 -height 22 -anchor nw -bordermode ignore 
	place $base.expbtn  -x 60 -y 110 -height 25 -width 75 -anchor nw -bordermode ignore 
	place $base.cancelbtn  -x 155 -y 110 -height 25 -width 75 -anchor nw -bordermode ignore 
	place $base.oicb  -x 170 -y 75 -anchor nw -bordermode ignore
}

proc {mw_canvas_paste} {wn x y} {
	   global mw
	   $wn.c insert $mw($wn,id_edited) insert [selection get]
	   set mw($wn,dirtyrec) 1
}

proc {mw_create_window} {} {
global mwcount
	set base .mw$mwcount
	set wn .mw$mwcount
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 550x400
	wm maxsize $base 1009 738
	wm minsize $base 550 400
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "Table browser"
	bind $base <Key-Delete> "mw_delete_record $wn"
	frame $base.f1  -borderwidth 2 -height 75 -relief groove -width 125 
	label $base.f1.l1  -borderwidth 0 -text {Sort field} 
	entry $base.f1.e1  -background #fefefe -borderwidth 1 -width 14  -highlightthickness 1 -textvariable mw($wn,sortfield)
	bind $base.f1.e1 <Key-Return> "mw_reload $wn"	
	bind $base.f1.e1 <Key-KP_Enter> "mw_reload $wn"	
	label $base.f1.lb1  -borderwidth 0 -text {     } 
	label $base.f1.l2  -borderwidth 0 -text {Filter conditions} 
	entry $base.f1.e2  -background #fefefe -borderwidth 1  -highlightthickness 1 -textvariable mw($wn,filter)
	bind $base.f1.e2 <Key-Return> "mw_reload $wn"	
	bind $base.f1.e2 <Key-KP_Enter> "mw_reload $wn"	
	button $base.f1.b1  -borderwidth 1 -text Close -command "
if {\[mw_save_new_record $wn\]} {
	$wn.c delete rows
	$wn.c delete header
	set sortfield {}
	set filter {}
	Window destroy $wn
	mw_free_variables $wn
}
	"
	button $base.f1.b2  -borderwidth 1 -text Reload -command "mw_reload $wn"
	frame $base.frame20  -borderwidth 2 -height 75 -relief groove -width 125 
	button $base.frame20.01  -borderwidth 1 -text < -command "mw_pan_right $wn"
	label $base.frame20.02  -anchor w -borderwidth 1 -height 1  -relief sunken -text {} -textvariable mw($wn,msg) 
	button $base.frame20.03  -borderwidth 1 -text > -command "mw_pan_left $wn"
	canvas $base.c  -background #fefefe -borderwidth 2 -height 207 -highlightthickness 0  -relief ridge -selectborderwidth 0 -takefocus 1 -width 295 
	scrollbar $base.sb  -borderwidth 1 -orient vert -width 12  -command "mw_scroll_window $wn"
	bind $base.c <Button-1> "mw_canvas_click $wn %x %y"
	bind $base.c <Button-2> "mw_canvas_paste $wn %x %y"
	bind $base.c <Button-3> "if {[mw_exit_edit $wn]} \"mw_save_new_record $wn\""
	pack $base.f1  -in $wn -anchor center -expand 0 -fill x -side top 
	pack $base.f1.l1  -in $wn.f1 -anchor center -expand 0 -fill none -side left 
	pack $base.f1.e1  -in $wn.f1 -anchor center -expand 0 -fill none -side left 
	pack $base.f1.lb1  -in $wn.f1 -anchor center -expand 0 -fill none -side left 
	pack $base.f1.l2  -in $wn.f1 -anchor center -expand 0 -fill none -side left 
	pack $base.f1.e2  -in $wn.f1 -anchor center -expand 0 -fill none -side left 
	pack $base.f1.b1  -in $wn.f1 -anchor center -expand 0 -fill none -side right 
	pack $base.f1.b2  -in $wn.f1 -anchor center -expand 0 -fill none -side right 
	pack $base.frame20  -in $wn -anchor s -expand 0 -fill x -side bottom 
	pack $base.frame20.01  -in $wn.frame20 -anchor center -expand 0 -fill none -side left 
	pack $base.frame20.02  -in $wn.frame20 -anchor center -expand 1 -fill x -side left 
	pack $base.frame20.03  -in $wn.frame20 -anchor center -expand 0 -fill none -side right 
	pack $base.c -in $wn -anchor w -expand 1 -fill both -side left 
	pack $base.sb -in $wn -anchor e -expand 0 -fill y -side right
}

proc vTclWindow.nt {base} {
global pref
    if {$base == ""} {
        set base .nt
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    toplevel $base -class Toplevel
    wm focusmodel $base passive
    wm geometry $base 614x392+78+181
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 0 0
    wm deiconify $base
    wm title $base "Create new table"
    entry $base.etabn \
        -background #fefefe -borderwidth 1 -selectborderwidth 0 \
        -textvariable ntw(newtablename) 
    bind $base.etabn <Key-Return> {
        focus .nt.einh
    }
    label $base.li \
        -anchor w -borderwidth 0 -text Inherits 
    entry $base.einh \
        -background #fefefe -borderwidth 1 -selectborderwidth 0 \
        -textvariable ntw(fathername) 
    bind $base.einh <Key-Return> {
        focus .nt.e2
    }
    button $base.binh \
        -borderwidth 1 \
        -command {if {[winfo exists .nt.ddf]} {
	destroy .nt.ddf
} else {
	create_drop_down .nt 386 23 220
	focus .nt.ddf.sb
	foreach tbl [get_tables] {.nt.ddf.lb insert end $tbl}
	bind .nt.ddf.lb <ButtonRelease-1> {
		set i [.nt.ddf.lb curselection]
		if {$i!=""} {
			if {$ntw(fathername)==""} {
				set ntw(fathername) "\"[.nt.ddf.lb get $i]\""
			} else {
				set ntw(fathername) "$ntw(fathername),\"[.nt.ddf.lb get $i]\""
			}
		}
		if {$i!=""} {focus .nt.e2}
		destroy .nt.ddf
		break
	}
}} \
        -highlightthickness 0 -takefocus 0 -image dnarw
    entry $base.e2 \
        -background #fefefe -borderwidth 1 -selectborderwidth 0 \
        -textvariable ntw(fldname) 
    bind $base.e2 <Key-Return> {
        focus .nt.e1
    }
    entry $base.e1 \
        -background #fefefe -borderwidth 1 -selectborderwidth 0 \
        -textvariable ntw(fldtype) 
    bind $base.e1 <Key-Return> {
        focus .nt.e5
    }
    entry $base.e3 \
        -background #fefefe -borderwidth 1 -selectborderwidth 0 \
        -textvariable ntw(fldsize) 
    bind $base.e3 <Key-Return> {
        focus .nt.e5
    }
    entry $base.e5 \
        -background #fefefe -borderwidth 1 -selectborderwidth 0 \
        -textvariable ntw(defaultval) 
    bind $base.e5 <Key-Return> {
        focus .nt.cb1
    }
    checkbutton $base.cb1 \
        -borderwidth 1 \
        -offvalue { } -onvalue { NOT NULL} -text {field cannot be null} \
        -variable ntw(notnull) 
    label $base.lab1 \
        -borderwidth 0 -text type 
    label $base.lab2 \
        -borderwidth 0 -anchor w -text {Field name} 
    label $base.lab3 \
        -borderwidth 0 -text size 
    label $base.lab4 \
        -borderwidth 0 -anchor w -text {Default value} 
    button $base.addfld \
        -borderwidth 1 -command add_new_field \
        -text {Add field} 
    button $base.delfld \
        -borderwidth 1 -command {catch {.nt.lb delete [.nt.lb curselection]}} \
        -text {Delete field} 
    button $base.emptb \
        -borderwidth 1 -command {.nt.lb delete 0 [.nt.lb size]} \
        -text {Delete all} 
    button $base.maketbl \
        -borderwidth 1 -command create_table \
        -text Create 
    listbox $base.lb \
        -background #fefefe -borderwidth 1 \
	-selectbackground #c3c3c3 \
        -font $pref(font_fix) \
        -selectborderwidth 0 -yscrollcommand {.nt.sb set} 
    bind $base.lb <ButtonRelease-1> {
        if {[.nt.lb curselection]!=""} {
	set fldname [string trim [lindex [split [.nt.lb get [.nt.lb curselection]]] 0]]
}
    }
    button $base.exitbtn \
        -borderwidth 1 -command {Window destroy .nt} \
        -text Cancel 
    label $base.l1 \
        -anchor w -borderwidth 1 \
        -relief raised -text {       field name} 
    label $base.l2 \
        -borderwidth 1 \
        -relief raised -text type 
    label $base.l3 \
        -borderwidth 1 \
        -relief raised -text options 
    scrollbar $base.sb \
        -borderwidth 1 -command {.nt.lb yview} -orient vert 
    label $base.l93 \
        -anchor w -borderwidth 0 -text {Table name} 
    button $base.mvup \
        -borderwidth 1 \
        -command {if {[.nt.lb size]>1} {
	set i [.nt.lb curselection]
	if {($i!="")&&($i>0)} {
		.nt.lb insert [expr $i-1] [.nt.lb get $i]
		.nt.lb delete [expr $i+1]
		.nt.lb selection set [expr $i-1]
	}
}} \
        -text {Move up} 
    button $base.mvdn \
        -borderwidth 1 \
        -command {if {[.nt.lb size]>1} {
	set i [.nt.lb curselection]
	if {($i!="")&&($i<[expr [.nt.lb size]-1])} {
		.nt.lb insert [expr $i+2] [.nt.lb get $i]
		.nt.lb delete $i
		.nt.lb selection set [expr $i+1]
	}
}} \
        -text {Move down} 
    button $base.button17 \
        -borderwidth 1 \
        -command {
if {[winfo exists .nt.ddf]} {
	destroy .nt.ddf
} else {
	create_drop_down .nt 291 80 97
	focus .nt.ddf.sb
	.nt.ddf.lb insert end char varchar text int2 int4 serial float4 float8 money abstime date datetime interval reltime time timespan timestamp boolean box circle line lseg path point polygon
	bind .nt.ddf.lb <ButtonRelease-1> {
		set i [.nt.ddf.lb curselection]
		if {$i!=""} {set ntw(fldtype) [.nt.ddf.lb get $i]}
		destroy .nt.ddf
		if {$i!=""} {focus .nt.e3}
		break
	}
}} \
        -highlightthickness 0 -takefocus 0 -image dnarw 
    label $base.lco \
        -borderwidth 0 -anchor w -text Constraint 
    entry $base.eco \
        -background #fefefe -borderwidth 1 -textvariable ntw(constraint) 
    label $base.lch \
        -borderwidth 0 -text check 
    entry $base.ech \
        -background #fefefe -borderwidth 1 -textvariable ntw(check) 
    label $base.ll \
        -borderwidth 1 \
        -relief raised 
    checkbutton $base.pk \
        -borderwidth 1 \
        -offvalue { } -onvalue * -text {primary key} -variable ntw(pk) 
    label $base.lpk \
        -borderwidth 1 \
        -relief raised -text K 
    place $base.etabn \
        -x 85 -y 5 -width 156 -height 20 -anchor nw -bordermode ignore 
    place $base.li \
        -x 245 -y 7 -width 42 -height 16 -anchor nw -bordermode ignore 
    place $base.einh \
        -x 290 -y 5 -width 318 -height 20 -anchor nw -bordermode ignore 
    place $base.binh \
        -x 590 -y 7 -width 16 -height 16 -anchor nw -bordermode ignore 
    place $base.e2 \
        -x 85 -y 60 -width 156 -height 20 -anchor nw -bordermode ignore 
    place $base.e1 \
        -x 291 -y 60 -width 98 -height 20 -anchor nw -bordermode ignore 
    place $base.e3 \
        -x 445 -y 60 -width 46 -height 20 -anchor nw -bordermode ignore 
    place $base.e5 \
        -x 85 -y 82 -width 156 -height 20 -anchor nw -bordermode ignore 
    place $base.cb1 \
        -x 245 -y 83 -width 131 -height 20 -anchor nw -bordermode ignore 
    place $base.lab1 \
        -x 247 -y 62 -width 26 -height 16 -anchor nw -bordermode ignore 
    place $base.lab2 \
        -x 4 -y 62 -width 64 -height 16 -anchor nw -bordermode ignore 
    place $base.lab3 \
        -x 410 -y 62 -width 24 -height 16 -anchor nw -bordermode ignore 
    place $base.lab4 \
        -x 5 -y 83 -width 76 -height 16 -anchor nw -bordermode ignore 
    place $base.addfld \
        -x 534 -y 60 -width 75 -height 26 -anchor nw -bordermode ignore 
    place $base.delfld \
        -x 534 -y 190 -width 75 -height 26 -anchor nw -bordermode ignore 
    place $base.emptb \
        -x 534 -y 220 -width 75 -height 26 -anchor nw -bordermode ignore 
    place $base.maketbl \
        -x 534 -y 365 -width 75 -height 26 -anchor nw -bordermode ignore 
    place $base.lb \
        -x 4 -y 121 -width 506 -height 269 -anchor nw -bordermode ignore 
    place $base.exitbtn \
        -x 534 -y 335 -width 75 -height 26 -anchor nw -bordermode ignore 
    place $base.l1 \
        -x 18 -y 105 -width 195 -height 18 -anchor nw -bordermode ignore 
    place $base.l2 \
        -x 213 -y 105 -width 88 -height 18 -anchor nw -bordermode ignore 
    place $base.l3 \
        -x 301 -y 105 -width 225 -height 18 -anchor nw -bordermode ignore 
    place $base.sb \
        -x 509 -y 121 -width 18 -height 269 -anchor nw -bordermode ignore 
    place $base.l93 \
        -x 4 -y 7 -width 67 -height 16 -anchor nw -bordermode ignore 
    place $base.mvup \
        -x 534 -y 120 -width 75 -height 26 -anchor nw -bordermode ignore 
    place $base.mvdn \
        -x 534 -y 150 -width 75 -height 26 -anchor nw -bordermode ignore 
    place $base.button17 \
        -x 371 -y 62 -width 16 -height 16 -anchor nw -bordermode ignore 
    place $base.lco \
        -x 5 -y 28 -width 58 -height 16 -anchor nw -bordermode ignore 
    place $base.eco \
        -x 85 -y 27 -width 156 -height 20 -anchor nw -bordermode ignore 
    place $base.lch \
        -x 245 -y 30 -anchor nw -bordermode ignore 
    place $base.ech \
        -x 290 -y 27 -width 318 -height 22 -anchor nw -bordermode ignore 
    place $base.ll \
        -x 5 -y 53 -width 603 -height 2 -anchor nw -bordermode ignore 
    place $base.pk \
        -x 407 -y 83 -width 93 -height 20 -anchor nw -bordermode ignore 
    place $base.lpk \
        -x 4 -y 105 -width 14 -height 18 -anchor nw -bordermode ignore 
}

proc vTclWindow.pw {base} {
global pref
	if {$base == ""} {
		set base .pw
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 322x227+210+219
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm title $base "Preferences"
	label $base.l1  -borderwidth 0 -text {Max rows displayed in table/query view} 
	entry $base.e1  -background #fefefe -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable pref(rows) 
	label $base.l2  -borderwidth 0 -text "Table viewer font"
	radiobutton $base.tvf  -borderwidth 1 -text {fixed width} -value clean -variable pref(tvfont)
	radiobutton $base.tvfv  -borderwidth 1 -text proportional -value helv -variable pref(tvfont)
	label $base.lfn -borderwidth 0 -anchor w -text "Font normal"
	label $base.lfb -borderwidth 0 -anchor w -text "Font bold"
	label $base.lfi -borderwidth 0 -anchor w -text "Font italic"
	label $base.lff -borderwidth 0 -anchor w -text "Font fixed"
	entry $base.efn -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable pref(font_normal)
	entry $base.efb -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable pref(font_bold)
	entry $base.efi -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable pref(font_italic)
	entry $base.eff -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable pref(font_fix)
	label $base.ll  -borderwidth 1 -relief sunken 
	checkbutton $base.alcb  -borderwidth 1 -text {Auto-load the last opened database at startup}  -variable pref(autoload) 
	button $base.okbtn  -borderwidth 1  -command {
if {$pref(rows)>200} {
	tk_messageBox -title Warning -parent .pw -message "A big number of rows displayed in table view will take a lot of memory!"
}
save_pref
Window destroy .pw
tk_messageBox -title Warning -message "Changed fonts may appear in the next working session!"
} -text Ok 
	place $base.l1  -x 10 -y 10 -anchor nw -bordermode ignore 
	place $base.e1  -x 240 -y 8 -width 65 -height 20 -anchor nw -bordermode ignore 
	place $base.l2  -x 10 -y 38 -anchor nw -bordermode ignore 
	place $base.tvf  -x 115 -y 34 -anchor nw -bordermode ignore 
	place $base.tvfv  -x 205 -y 34 -anchor nw -bordermode ignore 
	place $base.lfn -x 10 -y 65 -anchor nw
	place $base.lfb -x 10 -y 86 -anchor nw
	place $base.lfi -x 10 -y 107 -anchor nw
	place $base.lff -x 10 -y 128 -anchor nw
	place $base.efn -x 80 -y 63 -width 230 -height 20
	place $base.efb -x 80 -y 84 -width 230 -height 20
	place $base.efi -x 80 -y 105 -width 230 -height 20
	place $base.eff -x 80 -y 126 -width 230 -height 20
	place $base.ll  -x 10 -y 150 -width 301 -height 2 -anchor nw -bordermode ignore 
	place $base.alcb  -x 10 -y 155 -anchor nw -bordermode ignore 
	place $base.okbtn  -x 125 -y 195 -width 80 -height 26 -anchor nw -bordermode ignore
}

proc vTclWindow.qb {base} {
global pref
	if {$base == ""} {
		set base .qb
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 442x344+150+150
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base "Query builder"
	label $base.lqn  -borderwidth 0 -text {Query name} 
	entry $base.eqn  -background #fefefe -borderwidth 1 -foreground #000000  -highlightthickness 1 -selectborderwidth 0 -textvariable queryname 
	button $base.savebtn  -borderwidth 1  -command {if {$queryname==""} then {
	show_error "You have to supply a name for this query!"
	focus .qb.eqn
} else {
	set qcmd [.qb.text1 get 1.0 end]
	regsub -all "\n" $qcmd " " qcmd
	if {$qcmd==""} then {
	show_error "This query has no commands ?"
	} else {
		if { [lindex [split [string toupper [string trim $qcmd]]] 0] == "SELECT" } {
			set qtype S
		} else {
			set qtype A
		}
		if {$cbv} {
			wpg_select $dbc "select pg_get_viewdef('$queryname') as vd" tup {
				if {$tup(vd)!="Not a view"} {
					if {[tk_messageBox -title Warning -message "View '$queryname' already exists! Delete ?" -type yesno -default no]=="yes"} {
						set pg_res [wpg_exec $dbc "drop view \"$queryname\""]
						if {$pgsql(status)!="PGRES_COMMAND_OK"} {
							show_error "Error deleting view '$queryname'"
						}
					}
				}
			}
			set pgres [wpg_exec $dbc "create view \"$queryname\" as $qcmd"]
			if {$pgsql(status)!="PGRES_COMMAND_OK"} {
				show_error "Error defining view\n\n$pgsql(errmsg)"
			} else {
				tab_click .dw.tabViews
				Window destroy .qb
			}
			catch {pg_result $pgres -clear}
		} else {
			regsub -all "'" $qcmd "''" qcmd
			cursor_clock
			if {$queryoid==0} then {
				set pgres [wpg_exec $dbc "insert into pga_queries values ('$queryname','$qtype','$qcmd')"]
			} else {
				set pgres [wpg_exec $dbc "update pga_queries set queryname='$queryname',querytype='$qtype',querycommand='$qcmd' where oid=$queryoid"]
			}
			cursor_normal
			if {$pgsql(status)!="PGRES_COMMAND_OK"} then {
				show_error "Error executing query\n$pgres(errmsg)"
			} else {
				tab_click .dw.tabQueries
				if {$queryoid==0} {set queryoid [pg_result $pgres -oid]}
			}
		}
		catch {pg_result $pgres -clear}
	}
}}  -text {Save query definition} 
	button $base.execbtn  -borderwidth 1  -command {
set qcmd [.qb.text1 get 0.0 end]
regsub -all "\n" [string trim $qcmd] " " qcmd
if {[lindex [split [string toupper $qcmd]] 0]!="SELECT"} {
	if {[tk_messageBox -title Warning -parent .qb -message "This is an action query!\n\nExecute it?" -type yesno -default no]=="yes"} {
		sql_exec noquiet $qcmd
	}
} else {
	set wn [mw_get_new_name]
	set mw($wn,query) [subst $qcmd]
	set mw($wn,updatable) 0
	set mw($wn,isaquery) 1
	mw_create_window
	mw_load_layout $wn $queryname
	mw_select_records $wn $mw($wn,query)
}
} -text {Execute query} 
	button $base.termbtn  -borderwidth 1  -command {.qb.cbv configure -state normal
set cbv 0
set queryname {}
.qb.text1 delete 1.0 end
Window destroy .qb} -text Close 
	text $base.text1  -background #fefefe -borderwidth 1  -font $pref(font_normal) -foreground #000000 -highlightthickness 1 -wrap word 
	checkbutton $base.cbv  -borderwidth 1  -text {Save this query as a view} -variable cbv 
	button $base.qlshow  -borderwidth 1  -command {Window show .ql
ql_draw_lizzard
focus .ql.entt} -text {Visual designer} 
	place $base.lqn  -x 5 -y 5 -anchor nw -bordermode ignore 
	place $base.eqn  -x 80 -y 1 -width 355 -height 24 -anchor nw -bordermode ignore 
	place $base.savebtn  -x 5 -y 60 -height 25 -anchor nw -bordermode ignore 
	place $base.execbtn  -x 150 -y 60 -height 25 -anchor nw -bordermode ignore 
	place $base.termbtn  -x 375 -y 60 -width 50 -height 25 -anchor nw -bordermode ignore 
	place $base.text1  -x 5 -y 90 -width 430 -height 246 -anchor nw -bordermode ignore 
	place $base.cbv  -x 5 -y 30 -height 25 -anchor nw -bordermode ignore 
	place $base.qlshow  -x 255 -y 60 -height 25 -anchor nw -bordermode ignore
}

proc vTclWindow.ql {base} {
global pref
	if {$base == ""} {
		set base .ql
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 759x530+10+13
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "Visual query designer"
	bind $base <B1-Motion> {
		ql_pan %x %y
	}
	bind $base <Button-1> {
		qlc_click %x %y %W
	}
	bind $base <ButtonRelease-1> {
		ql_dragstop %x %y
	}
	bind $base <Key-Delete> {
		ql_delete_object
	}
	canvas $base.c  -background #fefefe -borderwidth 2 -height 207 -relief ridge  -takefocus 0 -width 295 
	button $base.exitbtn  -borderwidth 1 -command {
ql_init
Window destroy .ql} -text Close 
	button $base.showbtn  -borderwidth 1 -command ql_show_sql -text {Show SQL} 
	label $base.l12  -borderwidth 0 -text {Add table} 
	entry $base.entt  -background #fefefe -borderwidth 1 -highlightthickness 1  -selectborderwidth 0 -textvariable qlvar(newtablename) 
	bind $base.entt <Key-Return> {
		ql_add_new_table
	}
	button $base.execbtn  -borderwidth 1  -command {
set qcmd [ql_compute_sql]
set wn [mw_get_new_name]
set mw($wn,query) [subst $qcmd]
set mw($wn,updatable) 0
set mw($wn,isaquery) 1
mw_create_window
mw_load_layout $wn nolayoutneeded
mw_select_records $wn $mw($wn,query)} -text {Execute SQL} 
	button $base.stoqb  -borderwidth 1  -command {Window show .qb
.qb.text1 delete 1.0 end
.qb.text1 insert end [ql_compute_sql]
focus .qb} -text {Save to query builder} 
	button $base.bdd  -borderwidth 1  -command {if {[winfo exists .ql.ddf]} {
	destroy .ql.ddf
} else {
	create_drop_down .ql 70 27 200
	focus .ql.ddf.sb
	foreach tbl [get_tables] {.ql.ddf.lb insert end $tbl}
	bind .ql.ddf.lb <ButtonRelease-1> {
		set i [.ql.ddf.lb curselection]
		if {$i!=""} {
			set qlvar(newtablename) [.ql.ddf.lb get $i]
			ql_add_new_table
		}
		destroy .ql.ddf
		break
	}
}}  -image dnarw 
	place $base.c  -x 5 -y 30 -width 748 -height 500 -anchor nw -bordermode ignore 
	place $base.exitbtn  -x 695 -y 5 -height 25 -anchor nw -bordermode ignore 
	place $base.showbtn  -x 367 -y 5 -height 25 -anchor nw -bordermode ignore 
	place $base.l12  -x 10 -y 8 -width 53 -height 16 -anchor nw -bordermode ignore 
	place $base.entt  -x 70 -y 7 -width 126 -height 20 -anchor nw -bordermode ignore 
	place $base.execbtn  -x 452 -y 5 -height 25 -anchor nw -bordermode ignore 
	place $base.stoqb  -x 550 -y 5 -height 25 -anchor nw -bordermode ignore 
	place $base.bdd  -x 200 -y 7 -width 17 -height 20 -anchor nw -bordermode ignore
}


proc vTclWindow.rf {base} {
	if {$base == ""} {
		set base .rf
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 272x105+294+262
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm title $base "Rename"
	label $base.l1  -borderwidth 0 -text {New name} 
	entry $base.e1  -background #fefefe -borderwidth 1 -textvariable newobjname 
	button $base.b1  -borderwidth 1  -command {
			if {$newobjname==""} {
				show_error "You must give object a new name!"
			} elseif {$activetab=="Tables"} {
				set retval [sql_exec noquiet "alter table \"$oldobjname\" rename to \"$newobjname\""]
				if {$retval} {
					sql_exec quiet "update pga_layout set tablename='$newobjname' where tablename='$oldobjname'"
					cmd_Tables
					Window destroy .rf
				}			
			} elseif {$activetab=="Queries"} {
				set pgres [wpg_exec $dbc "select * from pga_queries where queryname='$newobjname'"]
				if {$pgsql(status)!="PGRES_TUPLES_OK"} {
					show_error "Error retrieving from pga_queries\n$pgsql(errmsg)\n$pgsql(status)"
				} elseif {[pg_result $pgres -numTuples]>0} {
					show_error "Query \"$newobjname\" already exists!"
				} else {
					sql_exec noquiet "update pga_queries set queryname='$newobjname' where queryname='$oldobjname'"
					sql_exec noquiet "update pga_layout set tablename='$newobjname' where tablename='$oldobjname'"
					cmd_Queries
					Window destroy .rf
				}
				catch {pg_result $pgres -clear}
			}
	   } -text Rename 
	button $base.b2  -borderwidth 1 -command {Window destroy .rf} -text Cancel 
	place $base.l1  -x 15 -y 28 -anchor nw -bordermode ignore 
	place $base.e1  -x 100 -y 25 -anchor nw -bordermode ignore 
	place $base.b1  -x 65 -y 65 -width 70 -anchor nw -bordermode ignore 
	place $base.b2  -x 145 -y 65 -width 70 -anchor nw -bordermode ignore
}

proc vTclWindow.rb {base} {
global pref
	if {$base == ""} {
		set base .rb
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 652x426+96+120
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base "Report builder"
	label $base.l1 \
		-borderwidth 1 \
		-relief raised -text {Report fields} 
	listbox $base.lb \
		-background #fefefe -borderwidth 1 \
		-selectbackground #c3c3c3 \
		-highlightthickness 1 -selectborderwidth 0 \
		-yscrollcommand {.rb.sb set} 
	bind $base.lb <ButtonRelease-1> {
		rb_add_field
	}
	canvas $base.c \
		-background #fffeff -borderwidth 2 -height 207 -highlightthickness 0 \
		-relief ridge -takefocus 1 -width 295 
	bind $base.c <Button-1> {
		rb_dragstart %W %x %y
	}
	bind $base.c <ButtonRelease-1> {
		rb_dragstop %x %y
	}
	bind $base.c <Key-Delete> {
		rb_delete_object
	}
	bind $base.c <Motion> {
		rb_dragit %W %x %y
	}
	button $base.bt2 \
		-borderwidth 1 \
		-command {if {[tk_messageBox -title Warning -parent .rb -message "All report information will be deleted.\n\nProceed ?" -type yesno -default no]=="yes"} then {
.rb.c delete all
rb_init
rb_draw_regions
}} \
		-text {Clear all} 
	button $base.bt4 \
		-borderwidth 1 -command rb_preview \
		-text Preview 
	button $base.bt5 \
		-borderwidth 1 -command {Window destroy .rb} \
		-text Quit 
	scrollbar $base.sb \
		-borderwidth 1 -command {.rb.lb yview} -orient vert 
	label $base.lmsg \
		-anchor w \
		-relief groove -text {Report header} -textvariable rbvar(msg) 
	entry $base.e2 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-textvariable rbvar(tablename) 
	bind $base.e2 <Key-Return> {
		rb_get_report_fields
	}
	entry $base.elab \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-textvariable rbvar(labeltext) 
	button $base.badl \
		-borderwidth 1 -command rb_add_label \
		-text {Add label} 
	label $base.lbold \
		-borderwidth 1 -relief raised -text B 
	bind $base.lbold <Button-1> {
		if {[rb_get_bold]=="Bold"} {
   .rb.lbold configure -relief raised
} else {
   .rb.lbold configure -relief sunken
}
rb_change_object_font
	}
	label $base.lita \
		-borderwidth 1 \
		-font $pref(font_italic) \
		-relief raised -text i 
	bind $base.lita <Button-1> {
		if {[rb_get_italic]=="O"} {
   .rb.lita configure -relief raised
} else {
   .rb.lita configure -relief sunken
}
rb_change_object_font
	}
	entry $base.eps \
		-background #fefefe -highlightthickness 0 -relief groove \
		-textvariable rbvar(pointsize) 
	bind $base.eps <Key-Return> {
		rb_change_object_font
	}
	label $base.linfo \
		-anchor w  \
		-relief groove -text {Database field} -textvariable rbvar(info) 
	label $base.llal \
		-borderwidth 0 -text Align 
	button $base.balign \
		-borderwidth 0 -command rb_flip_align \
		-relief groove -text right 
	button $base.savebtn \
		-borderwidth 1 -command rb_save_report \
		-text Save 
	label $base.lfn \
		-borderwidth 0 -text Font 
	button $base.bfont \
		-borderwidth 0 \
		-command {set temp [.rb.bfont cget -text]
if {$temp=="Courier"} then {
  .rb.bfont configure -text Helvetica
} else {
  .rb.bfont configure -text Courier
}
rb_change_object_font} \
		-relief groove -text Courier 
	button $base.bdd \
		-borderwidth 1 \
		-command {if {[winfo exists .rb.ddf]} {
	destroy .rb.ddf
} else {
	create_drop_down .rb 405 22 200
	focus .rb.ddf.sb
	foreach tbl [get_tables] {.rb.ddf.lb insert end $tbl}
	bind .rb.ddf.lb <ButtonRelease-1> {
		set i [.rb.ddf.lb curselection]
		if {$i!=""} {set rbvar(tablename) [.rb.ddf.lb get $i]}
		destroy .rb.ddf
		rb_get_report_fields
		break
	}
}} \
		-highlightthickness 0 -image dnarw 
	label $base.lrn \
		-borderwidth 0 -text {Report name} 
	entry $base.ern \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-textvariable rbvar(reportname) 
	bind $base.ern <Key-F5> {
		rb_load_report
	}
	label $base.lrs \
		-borderwidth 0 -text {Report source} 
	label $base.ls \
		-borderwidth 1 -relief raised 
	entry $base.ef \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-textvariable rbvar(formula) 
	button $base.baf \
		-borderwidth 1 \
		-text {Add formula} 
	place $base.l1 \
		-x 5 -y 55 -width 131 -height 18 -anchor nw -bordermode ignore 
	place $base.lb \
		-x 5 -y 70 -width 118 -height 121 -anchor nw -bordermode ignore 
	place $base.c \
		-x 140 -y 75 -width 508 -height 345 -anchor nw -bordermode ignore 
	place $base.bt2 \
		-x 5 -y 365 -width 64 -height 26 -anchor nw -bordermode ignore 
	place $base.bt4 \
		-x 70 -y 365 -width 66 -height 26 -anchor nw -bordermode ignore 
	place $base.bt5 \
		-x 70 -y 395 -width 66 -height 26 -anchor nw -bordermode ignore 
	place $base.sb \
		-x 120 -y 70 -width 18 -height 122 -anchor nw -bordermode ignore 
	place $base.lmsg \
		-x 142 -y 55 -width 151 -height 18 -anchor nw -bordermode ignore 
	place $base.e2 \
		-x 405 -y 4 -width 129 -height 18 -anchor nw -bordermode ignore 
	place $base.elab \
		-x 5 -y 225 -width 130 -height 18 -anchor nw -bordermode ignore 
	place $base.badl \
		-x 5 -y 243 -width 132 -height 26 -anchor nw -bordermode ignore 
	place $base.lbold \
		-x 535 -y 55 -width 18 -height 18 -anchor nw -bordermode ignore 
	place $base.lita \
		-x 555 -y 55 -width 18 -height 18 -anchor nw -bordermode ignore 
	place $base.eps \
		-x 500 -y 55 -width 30 -height 18 -anchor nw -bordermode ignore 
	place $base.linfo \
		-x 295 -y 55 -width 91 -height 18 -anchor nw -bordermode ignore 
	place $base.llal \
		-x 575 -y 56 -anchor nw -bordermode ignore 
	place $base.balign \
		-x 610 -y 54 -width 35 -height 21 -anchor nw -bordermode ignore 
	place $base.savebtn \
		-x 5 -y 395 -width 64 -height 26 -anchor nw -bordermode ignore 
	place $base.lfn \
		-x 405 -y 56 -anchor nw -bordermode ignore 
	place $base.bfont \
		-x 435 -y 54 -width 65 -height 21 -anchor nw -bordermode ignore 
	place $base.bdd \
		-x 535 -y 4 -width 15 -height 20 -anchor nw -bordermode ignore 
	place $base.lrn \
		-x 5 -y 5 -anchor nw -bordermode ignore 
	place $base.ern \
		-x 80 -y 4 -width 219 -height 18 -anchor nw -bordermode ignore 
	place $base.lrs \
		-x 320 -y 5 -anchor nw -bordermode ignore 
	place $base.ls \
		-x 5 -y 30 -width 641 -height 2 -anchor nw -bordermode ignore 
	place $base.ef \
		-x 5 -y 280 -width 130 -height 18 -anchor nw -bordermode ignore 
	place $base.baf \
		-x 5 -y 298 -width 132 -height 26 -anchor nw -bordermode ignore 
}

proc vTclWindow.rpv {base} {
	if {$base == ""} {
		set base .rpv
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 495x500+230+50
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm title $base "Report preview"
	frame $base.fr \
		-borderwidth 2 -height 75 -relief groove -width 125 
	canvas $base.fr.c \
		-background #fcfefe -borderwidth 2 -height 207 -relief ridge \
		-scrollregion {0 0 1000 824} -width 295 \
		-yscrollcommand {.rpv.fr.sb set} 
	scrollbar $base.fr.sb \
		-borderwidth 1 -command {.rpv.fr.c yview} -highlightthickness 0 \
		-orient vert -width 12 
	frame $base.f1 \
		-borderwidth 2 -height 75 -width 125 
	button $base.f1.button18 \
		-borderwidth 1 -command {if {$rbvar(justpreview)} then {Window destroy .rb} ; Window destroy .rpv} \
		-text Close 
	button $base.f1.button17 \
		-borderwidth 1 -command rb_print_report \
		-text Print 
	pack $base.fr \
		-in .rpv -anchor center -expand 1 -fill both -side top 
	pack $base.fr.c \
		-in .rpv.fr -anchor center -expand 1 -fill both -side left 
	pack $base.fr.sb \
		-in .rpv.fr -anchor center -expand 0 -fill y -side right 
	pack $base.f1 \
		-in .rpv -anchor center -expand 0 -fill none -side top 
	pack $base.f1.button18 \
		-in .rpv.f1 -anchor center -expand 0 -fill none -side right 
	pack $base.f1.button17 \
		-in .rpv.f1 -anchor center -expand 0 -fill none -side left 
}

proc vTclWindow.sqf {base} {
	if {$base == ""} {
		set base .sqf
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 310x223+245+158
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm title $base "Sequence"
	label $base.l1  -anchor w -borderwidth 0 -text {Sequence name} 
	entry $base.e1  -borderwidth 1 -highlightthickness 1 -textvariable seq_name 
	label $base.l2  -borderwidth 0 -text Increment 
	entry $base.e2  -borderwidth 1 -highlightthickness 1 -selectborderwidth 0  -textvariable seq_inc 
	label $base.l3  -borderwidth 0 -text {Start value} 
	entry $base.e3  -borderwidth 1 -highlightthickness 1 -selectborderwidth 0  -textvariable seq_start 
	label $base.l4  -borderwidth 0 -text Minvalue 
	entry $base.e4  -borderwidth 1 -highlightthickness 1 -selectborderwidth 0  -textvariable seq_minval 
	label $base.l5  -borderwidth 0 -text Maxvalue 
	entry $base.e5  -borderwidth 1 -highlightthickness 1 -selectborderwidth 0  -textvariable seq_maxval 
	button $base.defbtn  -borderwidth 1  -command {
		if {$seq_name==""} {
			show_error "You should supply a name for this sequence"
		} else {
			set s1 {};set s2 {};set s3 {};set s4 {};
			if {$seq_inc!=""} {set s1 "increment $seq_inc"};
			if {$seq_start!=""} {set s2 "start $seq_start"};
			if {$seq_minval!=""} {set s3 "minvalue $seq_minval"};
			if {$seq_maxval!=""} {set s4 "maxvalue $seq_maxval"};
			set sqlcmd "create sequence \"$seq_name\" $s1 $s2 $s3 $s4"
			if {[sql_exec noquiet $sqlcmd]} {
				cmd_Sequences
				tk_messageBox -title Information -parent .sqf -message "Sequence created!"
			}
		}
	} -text {Define sequence} 
	button $base.closebtn  -borderwidth 1  -command {for {set i 1} {$i<6} {incr i} {
	.sqf.e$i configure -state normal
	.sqf.e$i delete 0 end
	.sqf.defbtn configure -state normal
	.sqf.l3 configure -text {Start value}
}
place .sqf.defbtn -x 40 -y 175
Window destroy .sqf
} -text Close 
	place $base.l1  -x 20 -y 20 -width 111 -height 18 -anchor nw -bordermode ignore 
	place $base.e1  -x 135 -y 19 -anchor nw -bordermode ignore 
	place $base.l2  -x 20 -y 50 -anchor nw -bordermode ignore 
	place $base.e2  -x 135 -y 49 -anchor nw -bordermode ignore 
	place $base.l3  -x 20 -y 80 -anchor nw -bordermode ignore 
	place $base.e3  -x 135 -y 79 -anchor nw -bordermode ignore 
	place $base.l4  -x 20 -y 110 -anchor nw -bordermode ignore 
	place $base.e4  -x 135 -y 109 -anchor nw -bordermode ignore 
	place $base.l5  -x 20 -y 140 -anchor nw -bordermode ignore 
	place $base.e5  -x 135 -y 139 -anchor nw -bordermode ignore 
	place $base.defbtn  -x 40 -y 175 -anchor nw -bordermode ignore 
	place $base.closebtn  -x 195 -y 175 -anchor nw -bordermode ignore
}

proc vTclWindow.sw {base} {
global pref
	if {$base == ""} {
		set base .sw
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 594x416+192+152
	wm maxsize $base 1009 738
	wm minsize $base 300 300
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm title $base "Design script"
	frame $base.f1  -height 55 -relief groove -width 125 
	label $base.f1.l1  -borderwidth 0 -text {Script name} 
	entry $base.f1.e1  -background #fefefe -borderwidth 1 -highlightthickness 0  -textvariable scriptname -width 32 
	text $base.src  -background #fefefe  -font $pref(font_normal) -height 2  -highlightthickness 1 -selectborderwidth 0 -width 2 
	frame $base.f2  -height 75 -relief groove -width 125 
	button $base.f2.b1  -borderwidth 1 -command {Window destroy .sw} -text Cancel 
	button $base.f2.b2  -borderwidth 1  -command {if {$scriptname==""} {
	tk_messageBox -title Warning -parent .sw -message "The script must have a name!"
} else {
   sql_exec noquiet "delete from pga_scripts where scriptname='$scriptname'"
   regsub -all {\\} [.sw.src get 1.0 end] {\\\\} scriptsource
   regsub -all ' $scriptsource  \\' scriptsource
   sql_exec noquiet "insert into pga_scripts values ('$scriptname','$scriptsource')"
   cmd_Scripts
}}  -text Save -width 6 
	pack $base.f1  -in .sw -anchor center -expand 0 -fill x -pady 2 -side top 
	pack $base.f1.l1  -in .sw.f1 -anchor center -expand 0 -fill none -ipadx 2 -side left 
	pack $base.f1.e1  -in .sw.f1 -anchor center -expand 0 -fill none -side left 
	pack $base.src  -in .sw -anchor center -expand 1 -fill both -padx 2 -side top 
	pack $base.f2  -in .sw -anchor center -expand 0 -fill none -side top 
	pack $base.f2.b1  -in .sw.f2 -anchor center -expand 0 -fill none -side right 
	pack $base.f2.b2  -in .sw.f2 -anchor center -expand 0 -fill none -side right
}

proc vTclWindow.tiw {base} {
global pref
	if {$base == ""} {
		set base .tiw
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 390x460+243+20
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm title $base "Table information"
	label $base.l1  -borderwidth 0 -text {Table name} 
	label $base.l2  -anchor w -borderwidth 0 -text conturi -textvariable tiw(tablename) 
	label $base.l3  -borderwidth 0 -text Owner 
	label $base.l4  -anchor w -borderwidth 1  -textvariable tiw(owner) 
	listbox $base.lb  -background #fefefe -selectbackground #c3c3c3 -borderwidth 1  -font $pref(font_fix)  -highlightthickness 1 -selectborderwidth 0  -yscrollcommand {.tiw.sb set} 
	scrollbar $base.sb  -activebackground #d9d9d9 -activerelief sunken -borderwidth 1  -command {.tiw.lb yview} -orient vert 
	button $base.closebtn  -borderwidth 1 -command {Window destroy .tiw}  -pady 3 -text Close
	button $base.renbtn -borderwidth 1 -command {
	if {[set tiw(col_id) [.tiw.lb curselection]]==""} then {bell} else {set tiw(old_cn) [.tiw.lb get [.tiw.lb curselection]] ; set tiw(new_cn) {} ; Window show .rcw ; tkwait visibility .rcw ; wm transient .rcw .tiw ; focus .rcw.e1}} -text {Rename field}
	button $base.addbtn -borderwidth 1 -command "Window show .anfw ; set anfw(name) {} ; set anfw(type) {} ; wm transient .anfw .tiw ; focus .anfw.e1" -text "Add new field"
	label $base.l10  -borderwidth 1  -relief raised -text {field name}
	label $base.l11  -borderwidth 1  -relief raised -text {field type}
	label $base.l12  -borderwidth 1  -relief raised -text size
	label $base.lfi  -borderwidth 0 -text {Field information}
	label $base.lii  -borderwidth 1  -relief raised -text {Indexes defined}
	listbox $base.ilb  -background #fefefe -borderwidth 1  -highlightthickness 1 -selectborderwidth 0 -selectbackground #c3c3c3
	bind $base.ilb <ButtonRelease-1> {
		tiw_show_index
	}
	label $base.lip  -borderwidth 1  -relief raised -text {index properties}
	frame $base.fr11  -borderwidth 1 -height 75 -relief sunken -width 125
	label $base.fr11.l9  -borderwidth 0 -text {Is clustered ?} 
	label $base.fr11.l2  -borderwidth 0 -text {Is unique ?} 
	label $base.fr11.liu  -anchor nw -borderwidth 0 -text Yes -textvariable tiw(isunique) 
	label $base.fr11.lic  -anchor nw -borderwidth 0 -text No -textvariable tiw(isclustered) 
	label $base.fr11.l5  -borderwidth 0 -text {Fields :} 
	label $base.fr11.lif  -anchor nw -borderwidth 1  -justify left -relief sunken -text cont  -textvariable tiw(indexfields) -wraplength 170 
	place $base.l1  -x 20 -y 15 -anchor nw -bordermode ignore 
	place $base.l2  -x 100 -y 14 -width 161 -height 18 -anchor nw -bordermode ignore 
	place $base.l3  -x 20 -y 35 -anchor nw -bordermode ignore 
	place $base.l4  -x 100 -y 34 -width 226 -height 18 -anchor nw -bordermode ignore 
	place $base.lb  -x 20 -y 91 -width 338 -height 171 -anchor nw -bordermode ignore 
	place $base.renbtn -x 20 -y 263 -height 25
	place $base.addbtn -x 120 -y 263 -height 25
	place $base.sb  -x 355 -y 90 -width 18 -height 173 -anchor nw -bordermode ignore 
	place $base.closebtn  -x 325 -y 5 -height 25 -anchor nw -bordermode ignore 
	place $base.l10  -x 21 -y 75 -width 204 -height 18 -anchor nw -bordermode ignore 
	place $base.l11  -x 225 -y 75 -width 90 -height 18 -anchor nw -bordermode ignore 
	place $base.l12  -x 315 -y 75 -width 41 -height 18 -anchor nw -bordermode ignore 
	place $base.lfi  -x 20 -y 55 -anchor nw -bordermode ignore 
	place $base.lii  -x 20 -y 290 -width 151 -height 18 -anchor nw -bordermode ignore 
	place $base.ilb  -x 20 -y 306 -width 150 -height 148 -anchor nw -bordermode ignore 
	place $base.lip  -x 171 -y 290 -width 198 -height 18 -anchor nw -bordermode ignore 
	place $base.fr11  -x 170 -y 307 -width 199 -height 147 -anchor nw -bordermode ignore 
	place $base.fr11.l9  -x 10 -y 30 -anchor nw -bordermode ignore 
	place $base.fr11.l2  -x 10 -y 10 -anchor nw -bordermode ignore 
	place $base.fr11.liu  -x 95 -y 10 -width 27 -height 16 -anchor nw -bordermode ignore 
	place $base.fr11.lic  -x 95 -y 30 -width 32 -height 16 -anchor nw -bordermode ignore 
	place $base.fr11.l5  -x 10 -y 55 -anchor nw -bordermode ignore 
	place $base.fr11.lif  -x 10 -y 70 -width 178 -height 68 -anchor nw -bordermode ignore
}

proc vTclWindow.fd {base} {
	if {$base == ""} {
		set base .fd
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 377x315+103+101
	wm maxsize $base 785 570
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "Form design"
	bind $base <Key-Delete> {
		fd_delete_object
	}
	canvas $base.c \
		-background #828282 -height 207 -highlightthickness 0 -relief ridge \
		-selectborderwidth 0 -width 295 
	bind $base.c <Button-1> {
		fd_mouse_down %x %y
	}
	bind $base.c <ButtonRelease-1> {
		fd_mouse_up %x %y
	}
	bind $base.c <Motion> {
		fd_mouse_move %x %y
	}
	pack $base.c \
		-in .fd -anchor center -expand 1 -fill both -side top 
}

proc vTclWindow.fda {base} {
	if {$base == ""} {
		set base .fda
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 225x197+561+0
	wm maxsize $base 785 570
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "Attributes"
	label $base.l1 \
		-anchor nw -borderwidth 0 \
		-justify left -text Name -width 8 
	entry $base.e1 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(c_name) 
	bind $base.e1 <Key-Return> {
		fd_set_name
	}
	label $base.l2 \
		-anchor nw -borderwidth 0 \
		-justify left -text Top -width 8 
	entry $base.e2 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(c_top) 
	bind $base.e2 <Key-Return> {
		fd_change_coord
	}
	label $base.l3 \
		-anchor w -borderwidth 0 \
		-text Left -width 8 
	entry $base.e3 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(c_left) 
	bind $base.e3 <Key-Return> {
		fd_change_coord
	}
	label $base.l4 \
		-anchor w -borderwidth 0 \
		-text Width \
		-width 8 
	entry $base.e4 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(c_width) 
	bind $base.e4 <Key-Return> {
		fd_change_coord
	}
	label $base.l5 \
		-anchor w -borderwidth 0 -padx 0 -text Height -width 8 
	entry $base.e5 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(c_height) 
	bind $base.e5 <Key-Return> {
		fd_change_coord
	}
	label $base.l6 \
		-borderwidth 0 -text Command 
	entry $base.e6 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(c_cmd) 
	bind $base.e6 <Key-Return> {
		fd_set_command
	}
	button $base.bcmd \
		-borderwidth 1 \
		-command {Window show .fdcmd
.fdcmd.f.txt delete 1.0 end
.fdcmd.f.txt insert end $fdvar(c_cmd)} \
		-text ... -width 1 
	label $base.l7 \
		-anchor w -borderwidth 0 \
		-text Variable -width 8 
	entry $base.e7 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(c_var) 
	bind $base.e7 <Key-Return> {
		set fdobj($fdvar(moveitemobj),v) $fdvar(c_var)
	}
	label $base.l8 \
		-anchor w -borderwidth 0 \
		-text Text -width 8 
	entry $base.e8 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(c_text) 
	bind $base.e8 <Key-Return> {
		fd_set_text
	}
	label $base.l0 \
		-borderwidth 1 -relief raised -text {checkbox .udf0.checkbox17} \
		-textvariable fdvar(c_info) -width 28 
	grid $base.l1 \
		-in .fda -column 0 -row 1 -columnspan 1 -rowspan 1 
	grid $base.e1 \
		-in .fda -column 1 -row 1 -columnspan 1 -rowspan 1 -pady 2 
	grid $base.l2 \
		-in .fda -column 0 -row 2 -columnspan 1 -rowspan 1 
	grid $base.e2 \
		-in .fda -column 1 -row 2 -columnspan 1 -rowspan 1 
	grid $base.l3 \
		-in .fda -column 0 -row 3 -columnspan 1 -rowspan 1 
	grid $base.e3 \
		-in .fda -column 1 -row 3 -columnspan 1 -rowspan 1 -pady 2 
	grid $base.l4 \
		-in .fda -column 0 -row 4 -columnspan 1 -rowspan 1 
	grid $base.e4 \
		-in .fda -column 1 -row 4 -columnspan 1 -rowspan 1 
	grid $base.l5 \
		-in .fda -column 0 -row 5 -columnspan 1 -rowspan 1 
	grid $base.e5 \
		-in .fda -column 1 -row 5 -columnspan 1 -rowspan 1 -pady 2 
	grid $base.l6 \
		-in .fda -column 0 -row 6 -columnspan 1 -rowspan 1 
	grid $base.e6 \
		-in .fda -column 1 -row 6 -columnspan 1 -rowspan 1 
	grid $base.bcmd \
		-in .fda -column 2 -row 6 -columnspan 1 -rowspan 1 
	grid $base.l7 \
		-in .fda -column 0 -row 7 -columnspan 1 -rowspan 1 
	grid $base.e7 \
		-in .fda -column 1 -row 7 -columnspan 1 -rowspan 1 
	grid $base.l8 \
		-in .fda -column 0 -row 8 -columnspan 1 -rowspan 1 
	grid $base.e8 \
		-in .fda -column 1 -row 8 -columnspan 1 -rowspan 1 -pady 2 
	grid $base.l0 \
		-in .fda -column 0 -row 0 -columnspan 2 -rowspan 1 
}

proc vTclWindow.fdcmd {base} {
global pref
	if {$base == ""} {
		set base .fdcmd
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 282x274+504+229
	wm maxsize $base 785 570
	wm minsize $base 1 19
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm title $base "Command"
	frame $base.f \
		-borderwidth 2 -height 75 -relief groove -width 125 
	scrollbar $base.f.sb \
		-borderwidth 1 -command {.fdcmd.f.txt yview} -orient vert -width 12 
	text $base.f.txt \
		-font $pref(font_fix) -height 1 \
		-width 115 -yscrollcommand {.fdcmd.f.sb set} 
	frame $base.fb \
		-height 75 -width 125 
	button $base.fb.b1 \
		-borderwidth 1 \
		-command {set fdvar(c_cmd) [.fdcmd.f.txt get 1.0 "end - 1 chars"]
Window hide .fdcmd
fd_set_command} \
		-text Ok -width 5 
	button $base.fb.b2 \
		-borderwidth 1 -command {Window hide .fdcmd} \
		-text Cancel 
	pack $base.f \
		-in .fdcmd -anchor center -expand 1 -fill both -side top 
	pack $base.f.sb \
		-in .fdcmd.f -anchor e -expand 1 -fill y -side right 
	pack $base.f.txt \
		-in .fdcmd.f -anchor center -expand 1 -fill both -side top 
	pack $base.fb \
		-in .fdcmd -anchor center -expand 0 -fill none -side top 
	pack $base.fb.b1 \
		-in .fdcmd.fb -anchor center -expand 0 -fill none -side left 
	pack $base.fb.b2 \
		-in .fdcmd.fb -anchor center -expand 0 -fill none -side top 
}

proc vTclWindow.fdmenu {base} {
	if {$base == ""} {
		set base .fdmenu
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 288x70+103+0
	wm maxsize $base 785 570
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base "Commands"
	button $base.but17 \
		-borderwidth 1 \
		-command {if {[tk_messageBox -title Warning -message "Delete all objects ?" -type yesno -default no]=="no"} return
fd_init} \
		-text {Delete all} 
	button $base.but18 \
		-borderwidth 1 -command {set fdvar(geometry) [wm geometry .fd] ; fd_test } \
		-text {Test form} 
	button $base.but19 \
		-borderwidth 1 -command {destroy .$fdvar(forminame)} \
		-text {Close test form} 
	button $base.bex \
		-borderwidth 1 \
		-command {if {[fd_save_form $fdvar(formname)]==1} {
catch {Window destroy .fd}
catch {Window destroy .fdtb}
catch {Window destroy .fdmenu}
catch {Window destroy .fda}
catch {Window destroy .fdcmd}
catch {Window destroy .$fdvar(forminame)}
}} \
		-text Close 
	button $base.bload \
		-borderwidth 1 -command {fd_load_form nimic design} \
		-text {Load from database} 
	button $base.button17 \
		-borderwidth 1 -command {fd_save_form nimic} \
		-text Save 
	label $base.l1 \
		-borderwidth 0 -text {Form name} 
	entry $base.e1 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(formname) 
	label $base.l2 \
		-borderwidth 0 \
		-text {Form's window internal name} 
	entry $base.e2 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-selectborderwidth 0 -textvariable fdvar(forminame) 
	place $base.but17 \
		-x 5 -y 80 -width 62 -height 24 -anchor nw -bordermode ignore 
	place $base.but18 \
		-x 5 -y 45 -width 62 -height 24 -anchor nw -bordermode ignore 
	place $base.but19 \
		-x 70 -y 45 -width 94 -height 24 -anchor nw -bordermode ignore 
	place $base.bex \
		-x 230 -y 45 -height 24 -anchor nw -bordermode ignore 
	place $base.bload \
		-x 75 -y 80 -width 114 -height 23 -anchor nw -bordermode ignore 
	place $base.button17 \
		-x 165 -y 45 -width 44 -height 24 -anchor nw -bordermode ignore 
	place $base.l1 \
		-x 5 -y 5 -anchor nw -bordermode ignore 
	place $base.e1 \
		-x 75 -y 5 -width 193 -height 17 -anchor nw -bordermode ignore 
	place $base.l2 \
		-x 5 -y 25 -anchor nw -bordermode ignore 
	place $base.e2 \
		-x 175 -y 25 -width 60 -height 17 -anchor nw -bordermode ignore 
}

proc vTclWindow.gpw {base} {
	if {$base == ""} {
		set base .gpw
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	set sw [winfo screenwidth .]
	set sh [winfo screenheight .]
	set x [expr ($sw - 297)/2]
	set y [expr ($sh - 98)/2]
	wm geometry $base 297x98+$x+$y
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 0 0
	wm deiconify $base
	wm title $base "Input parameter"
	label $base.l1 \
		-anchor nw -borderwidth 1 \
		-justify left -relief sunken -textvariable gpw(msg) -wraplength 200 
	entry $base.e1 \
		-background #fefefe -borderwidth 1 -highlightthickness 0 \
		-textvariable gpw(var) 
	bind $base.e1 <Key-KP_Enter> {
		set gpw(result) 1
destroy .gpw
	}
	bind $base.e1 <Key-Return> {
		set gpw(result) 1
destroy .gpw
	}
	button $base.bok \
		-borderwidth 1 -command {set gpw(result) 1
destroy .gpw} -text Ok 
	button $base.bcanc \
		-borderwidth 1 -command {set gpw(result) 0
destroy .gpw} -text Cancel 
	place $base.l1 \
		-x 10 -y 5 -width 201 -height 53 -anchor nw -bordermode ignore 
	place $base.e1 \
		-x 10 -y 65 -width 200 -height 24 -anchor nw -bordermode ignore 
	place $base.bok \
		-x 225 -y 5 -width 61 -height 26 -anchor nw -bordermode ignore 
	place $base.bcanc \
		-x 225 -y 35 -width 61 -height 26 -anchor nw -bordermode ignore 
}

proc vTclWindow.fdtb {base} {
	if {$base == ""} {
		set base .fdtb
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 90x172+0+0
	wm maxsize $base 785 570
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "Toolbar"
	radiobutton $base.rb1 \
		-anchor w -borderwidth 1 \
		-highlightthickness 0 -text Point -value point -variable fdvar(tool) \
		-width 9 
	radiobutton $base.rb2 \
		-anchor w -borderwidth 1 \
		-foreground #000000 -highlightthickness 0 \
		-text Label -value label -variable fdvar(tool) -width 9 
	radiobutton $base.rb3 \
		-anchor w -borderwidth 1 \
		-highlightthickness 0 -text Entry -value entry -variable fdvar(tool) \
		-width 9 
	radiobutton $base.rb4 \
		-anchor w -borderwidth 1 \
		-highlightthickness 0 -text Button -value button \
		-variable fdvar(tool) -width 9 
	radiobutton $base.rb5 \
		-anchor w -borderwidth 1 \
		-highlightthickness 0 -text {List box} -value listbox \
		-variable fdvar(tool) -width 9 
	radiobutton $base.rb6 \
		-anchor w -borderwidth 1 \
		-highlightthickness 0 -text {Check box} -value checkbox \
		-variable fdvar(tool) -width 9 
	radiobutton $base.rb7 \
		-anchor w -borderwidth 1 \
		-highlightthickness 0 -text {Radio btn} -value radio \
		-variable fdvar(tool) -width 9 
	radiobutton $base.rb9 \
		-anchor w -borderwidth 1 \
		-highlightthickness 0 -text {Text} -value text \
		-variable fdvar(tool) -width 9 
	radiobutton $base.rb8 \
		-anchor w -borderwidth 1 \
		-highlightthickness 0 -text Query -value query -variable fdvar(tool) \
		-width 9 
	grid $base.rb1 \
		-in .fdtb -column 0 -row 0 -columnspan 1 -rowspan 1 
	grid $base.rb2 \
		-in .fdtb -column 0 -row 1 -columnspan 1 -rowspan 1 
	grid $base.rb3 \
		-in .fdtb -column 0 -row 2 -columnspan 1 -rowspan 1 
	grid $base.rb4 \
		-in .fdtb -column 0 -row 3 -columnspan 1 -rowspan 1 
	grid $base.rb5 \
		-in .fdtb -column 0 -row 4 -columnspan 1 -rowspan 1 
	grid $base.rb6 \
		-in .fdtb -column 0 -row 5 -columnspan 1 -rowspan 1 
	grid $base.rb7 \
		-in .fdtb -column 0 -row 6 -columnspan 1 -rowspan 1 
	grid $base.rb9 \
		-in .fdtb -column 0 -row 7 -columnspan 1 -rowspan 1 
	grid $base.rb8 \
		-in .fdtb -column 0 -row 8 -columnspan 1 -rowspan 1 
}

proc vTclWindow.sqlw {base} {
	if {$base == ""} {
		set base .sqlw
	}
	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 551x408+192+169
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "SQL commands"
	frame $base.f \
		-borderwidth 1 -height 392 -relief raised -width 396 
	scrollbar $base.f.01 \
		-borderwidth 1 -command {.sqlw.f.t xview} -orient horiz \
		-width 10 
	scrollbar $base.f.02 \
		-borderwidth 1 -command {.sqlw.f.t yview} -orient vert -width 10 
	text $base.f.t \
		-borderwidth 1 \
		-height 200 -width 200 -wrap word \
		-xscrollcommand {.sqlw.f.01 set} \
		-yscrollcommand {.sqlw.f.02 set} 
	button $base.b1 \
		-borderwidth 1 -command {.sqlw.f.t delete 1.0 end} -text Clean 
	button $base.b2 \
		-borderwidth 1 -command {destroy .sqlw} -text Close 
	grid columnconf $base 0 -weight 1
	grid columnconf $base 1 -weight 1
	grid rowconf $base 0 -weight 1
	grid $base.f \
		-in .sqlw -column 0 -row 0 -columnspan 2 -rowspan 1 
	grid columnconf $base.f 0 -weight 1
	grid rowconf $base.f 0 -weight 1
	grid $base.f.01 \
		-in .sqlw.f -column 0 -row 1 -columnspan 1 -rowspan 1 -sticky ew 
	grid $base.f.02 \
		-in .sqlw.f -column 1 -row 0 -columnspan 1 -rowspan 1 -sticky ns 
	grid $base.f.t \
		-in .sqlw.f -column 0 -row 0 -columnspan 1 -rowspan 1 \
		-sticky nesw 
	grid $base.b1 \
		-in .sqlw -column 0 -row 1 -columnspan 1 -rowspan 1 
	grid $base.b2 \
		-in .sqlw -column 1 -row 1 -columnspan 1 -rowspan 1 
}

proc vTclWindow.rcw {base} {
    if {$base == ""} {
        set base .rcw
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    toplevel $base -class Toplevel
    wm focusmodel $base passive
    wm geometry $base 215x75+258+213
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 0 0
    wm deiconify $base
    wm title $base "Rename field"
    label $base.l1 \
        -borderwidth 0 -text {New name} 
    entry $base.e1 \
        -background #fefefe -borderwidth 1 -textvariable tiw(new_cn)
	bind $base.e1 <Key-KP_Enter> "rename_column"
	bind $base.e1 <Key-Return> "rename_column"
    frame $base.f \
        -height 75 -relief groove -width 147 
    button $base.f.b1 \
        -borderwidth 1 -command rename_column -text Rename 
    button $base.f.b2 \
        -borderwidth 1 -command {Window destroy .rcw} -text Cancel 
    label $base.l2 -borderwidth 0 
    grid $base.l1 \
        -in .rcw -column 0 -row 0 -columnspan 1 -rowspan 1 
    grid $base.e1 \
        -in .rcw -column 1 -row 0 -columnspan 1 -rowspan 1 
    grid $base.f \
        -in .rcw -column 0 -row 4 -columnspan 2 -rowspan 1 
    grid $base.f.b1 \
        -in .rcw.f -column 0 -row 0 -columnspan 1 -rowspan 1 
    grid $base.f.b2 \
        -in .rcw.f -column 1 -row 0 -columnspan 1 -rowspan 1 
    grid $base.l2 \
        -in .rcw -column 0 -row 3 -columnspan 1 -rowspan 1 
}

proc vTclWindow.anfw {base} {
    if {$base == ""} {
        set base .anfw
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    toplevel $base -class Toplevel
    wm focusmodel $base passive
    wm geometry $base 302x114+195+175
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 0 0
    wm deiconify $base
    wm title $base "Add new field"
    label $base.l1 \
        -borderwidth 0 \
        -text {Field name} 
    entry $base.e1 \
        -background #fefefe -borderwidth 1 -textvariable anfw(name) 
    bind $base.e1 <Key-KP_Enter> {
        focus .anfw.e2
    }
    bind $base.e1 <Key-Return> {
        focus .anfw.e2
    }
    label $base.l2 \
        -borderwidth 0 \
        -text {Field type} 
    entry $base.e2 \
        -background #fefefe -borderwidth 1 -textvariable anfw(type) 
    bind $base.e2 <Key-KP_Enter> {
        anfw:add
    }
    bind $base.e2 <Key-Return> {
        anfw:add
    }
    button $base.b1 \
        -borderwidth 1 -command anfw:add -text {Add field} 
    button $base.b2 \
        -borderwidth 1 -command {Window destroy .anfw} -text Cancel 
    place $base.l1 \
        -x 25 -y 10 -anchor nw -bordermode ignore 
    place $base.e1 \
        -x 98 -y 7 -width 178 -height 22 -anchor nw -bordermode ignore 
    place $base.l2 \
        -x 25 -y 40 -anchor nw -bordermode ignore 
    place $base.e2 \
        -x 98 -y 37 -width 178 -height 22 -anchor nw -bordermode ignore 
    place $base.b1 \
        -x 70 -y 75 -anchor nw -bordermode ignore 
    place $base.b2 \
        -x 160 -y 75 -anchor nw -bordermode ignore 
}

proc vTclWindow.uw {base} {
    if {$base == ""} {
        set base .uw
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    toplevel $base -class Toplevel
    wm focusmodel $base passive
    wm geometry $base 263x220+233+165
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 0 0
    wm deiconify $base
    wm title $base "Define new user"
    label $base.l1 \
        -borderwidth 0 -anchor w -text "User name"
    entry $base.e1 \
        -background #fefefe -borderwidth 1 -textvariable uw(username) 
	bind $base.e1 <Key-Return> "focus .uw.e2"
	bind $base.e1 <Key-KP_Enter> "focus .uw.e2"
    label $base.l2 \
        -borderwidth 0 -text Password 
    entry $base.e2 \
        -background #fefefe -borderwidth 1 -show * -textvariable uw(password) 
	bind $base.e2 <Key-Return> "focus .uw.e3"
	bind $base.e2 <Key-KP_Enter> "focus .uw.e3"
    label $base.l3 \
        -borderwidth 0 -text {verify password} 
    entry $base.e3 \
        -background #fefefe -borderwidth 1 -show * -textvariable uw(verify) 
	bind $base.e3 <Key-Return> "focus .uw.cb1"
	bind $base.e3 <Key-KP_Enter> "focus .uw.cb1"
    checkbutton $base.cb1 \
        -borderwidth 1 -offvalue NOCREATEDB -onvalue CREATEDB \
        -text {Alow user to create databases } -variable uw(createdb) 
    checkbutton $base.cb2 \
        -borderwidth 1 -offvalue NOCREATEUSER -onvalue CREATEUSER \
        -text {Allow users to create other users} -variable uw(createuser) 
    label $base.l4 \
        -borderwidth 0 -anchor w -text {Valid until (date)} 
    entry $base.e4 \
        -background #fefefe -borderwidth 1 -textvariable uw(valid)
	bind $base.e4 <Key-Return> "focus .uw.b1"
	bind $base.e4 <Key-KP_Enter> "focus .uw.b1"
    button $base.b1 \
        -borderwidth 1 -command uw:create_user -text Create 
    button $base.b2 \
        -borderwidth 1 -command {Window destroy .uw} -text Cancel 
    place $base.l1 \
        -x 5 -y 7 -width 62 -height 16 -anchor nw -bordermode ignore 
    place $base.e1 \
        -x 109 -y 5 -width 146 -height 20 -anchor nw -bordermode ignore 
    place $base.l2 \
        -x 5 -y 35 -anchor nw -bordermode ignore 
    place $base.e2 \
        -x 109 -y 32 -width 146 -height 20 -anchor nw -bordermode ignore 
    place $base.l3 \
        -x 5 -y 60 -anchor nw -bordermode ignore 
    place $base.e3 \
        -x 109 -y 58 -width 146 -height 20 -anchor nw -bordermode ignore 
    place $base.cb1 \
        -x 5 -y 90 -anchor nw -bordermode ignore 
    place $base.cb2 \
        -x 5 -y 115 -anchor nw -bordermode ignore 
    place $base.l4 \
        -x 5 -y 145 -width 100 -height 16 -anchor nw -bordermode ignore 
    place $base.e4 \
        -x 110 -y 143 -width 146 -height 20 -anchor nw -bordermode ignore 
    place $base.b1 \
        -x 45 -y 185 -anchor nw -width 70 -height 25 -bordermode ignore 
    place $base.b2 \
        -x 140 -y 185 -anchor nw -width 70 -height 25 -bordermode ignore 
}

Window show .
Window show .dw

main $argc $argv