summaryrefslogtreecommitdiff
path: root/lib/tcl/wrapper.tcl
blob: 20486e03ef5e05cedba1dd6be905d58b39acdb3d (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
#-------------------------------------------------------------------------
# Start of xcircuit GUI configuration file.
# This file is sourced by "xcircuit.tcl" (".wishrc")
#-------------------------------------------------------------------------

# This script sets up all the xcircuit windows and callback functions.
# The callback routines are in the shared object file xcwrap.so
#

#-------------------------------
# Main xcircuit drawing window
#-------------------------------

proc xcircuit::new_window { name } {

  global XCIRCUIT_VERSION XCIRCUIT_REVISION XCOps XCWinOps tcl_platform

  toplevel $name
  wm title $name XCircuit
  wm group $name .
  wm protocol $name WM_DELETE_WINDOW \
	"xcircuit::closewindow ${name}.mainframe.mainarea.drawing"

  # All the internal frames

  frame ${name}.menubar
  frame ${name}.infobar -borderwidth 1 -bg #a07650
  frame ${name}.mainframe -borderwidth 1 -bg #a07650

  grid propagate ${name} false
  grid ${name}.menubar -sticky news -row 0 -column 0
  grid ${name}.mainframe -sticky news -row 1 -columnspan 2
  grid ${name}.infobar -sticky news -row 2 -columnspan 2

  grid rowconfigure ${name} 0 -weight 0
  grid rowconfigure ${name} 1 -weight 1
  grid rowconfigure ${name} 2 -weight 0

  grid columnconfigure ${name} 0 -weight 0
  grid columnconfigure ${name} 1 -weight 1

  frame ${name}.mainframe.mainarea
  frame ${name}.mainframe.toolbar -bg #a07650 -borderwidth 1

  pack ${name}.mainframe.toolbar -side right -fill y
  pack ${name}.mainframe.mainarea -expand true -fill both

  set drawing ${name}.mainframe.mainarea.drawing
  simple $drawing -bg white -commandproc "focus $drawing ; set XCOps(focus) $name"

  simple ${name}.mainframe.mainarea.sbleft -width 13 -bg #a07650
  simple ${name}.mainframe.mainarea.sbbottom -height 13 -bg #a07650
  simple ${name}.mainframe.mainarea.corner -width 13 -height 13 -bg brown4

  # The drawing area and its scrollbars

  grid ${name}.mainframe.mainarea.sbleft -row 0 -column 0 -sticky ns
  grid ${name}.mainframe.mainarea.sbbottom -row 1 -column 1 -sticky ew
  grid $drawing -row 0 -column 1 -sticky news
  grid ${name}.mainframe.mainarea.corner -row 1 -column 0 -sticky news

  grid rowconfigure ${name}.mainframe.mainarea 0 -weight 1
  grid columnconfigure ${name}.mainframe.mainarea 1 -weight 1

  # The top menu and message bar

  menubutton ${name}.menubar.filebutton -text File \
	-menu ${name}.menubar.filebutton.filemenu
  menubutton ${name}.menubar.editbutton -text Edit \
	-menu ${name}.menubar.editbutton.editmenu
  menubutton ${name}.menubar.textbutton -text Text \
	-menu ${name}.menubar.textbutton.textmenu
  menubutton ${name}.menubar.optionsbutton -text Options \
	-menu ${name}.menubar.optionsbutton.optionsmenu
  menubutton ${name}.menubar.windowbutton -text Window \
	-menu ${name}.menubar.windowbutton.windowmenu
  menubutton ${name}.menubar.netlistbutton -text Netlist \
	-menu ${name}.menubar.netlistbutton.netlistmenu 

  grid ${name}.menubar.filebutton ${name}.menubar.editbutton \
	${name}.menubar.textbutton ${name}.menubar.optionsbutton \
	${name}.menubar.windowbutton ${name}.menubar.netlistbutton \
	-ipadx 10 -sticky news

  # The top message bar

  label ${name}.message -text \
	"Welcome to Xcircuit v${XCIRCUIT_VERSION} rev ${XCIRCUIT_REVISION}" \
	-background beige -justify left -anchor w

  grid ${name}.message -row 0 -column 1 -sticky news -ipadx 10

  button ${name}.infobar.symb -text "Symbol" -bg gray30 -fg white
  button ${name}.infobar.schem -text "Schematic" -bg red -fg white
  button ${name}.infobar.mode -text "Wire Mode" -bg skyblue2 -fg gray20
  label ${name}.infobar.message1 -text "Editing: Page 1" \
	-background beige
  label ${name}.infobar.message2 -text "Grid 1/6 in : Snap 1/12 in" \
	-background beige -justify left -anchor w
  pack ${name}.infobar.symb ${name}.infobar.schem ${name}.infobar.message1 \
	${name}.infobar.mode -side left -ipadx 6 -fill y
  pack ${name}.infobar.message2 -ipadx 6 -expand true -fill both

  #-------------------------------------------------
  # Mouse hint window (if mousehints are enabled)
  #-------------------------------------------------

  if {$XCOps(mousehints) == 0} {
     bind ${name}.infobar.mode <Button-1> {xcircuit::enable_mousehints}
  } elseif {$XCOps(mousehints) == 1} {
     xcircuit::mousehint_create ${name}
  }

  #-------------------------------------------------
  # Create the menus, toolbar and associated tools
  #-------------------------------------------------

  xcircuit::makemenus $name
  xcircuit::createtoolbar $name
  xcircuit::arrangetoolbar $name
  xcircuit::allcolorbuttons $name
  xcircuit::allfontbuttons $name

  #-----------------------------------------------------------------
  # Add key and button bindings for XCircuit commands (standard actions)
  # These can be overridden by binding to specific keys and/or buttons.
  #-----------------------------------------------------------------

  bind $drawing <ButtonPress> {standardaction %b down %s}
  bind $drawing <ButtonRelease> {standardaction %b up %s}
  bind $drawing <KeyPress> {standardaction %k down %s}
  bind $drawing <KeyRelease> {standardaction %k up %s}

  # Here are some extra key functions that come with the TCL wrapper

  bind $drawing <Key-Next> {catch {page [expr {[page] + 1}]}}
  bind $drawing <Key-Prior> {catch {page [expr {[page] - 1}]}}
  bind $drawing <Control-Key-p> {xcircuit::prompteditparams}

  xcircuit::keybind <Key-bracketleft> {if {[select get] == {}} \
		{select here; element lower; deselect} else {element lower}; \
		refresh} $drawing
  xcircuit::keybind <Key-bracketright> {if {[select get] == {}} \
		{select here; element raise; deselect} else {element raise}; \
		refresh} $drawing

  # Bind numbers 1-9 and 0 so that they call the Tcl "page" command,
  # and so can take advantage of the tag callback to "pageupdate".

  xcircuit::keybind <Key-1> {page 1} $drawing
  xcircuit::keybind <Key-2> {page 2} $drawing
  xcircuit::keybind <Key-3> {page 3} $drawing
  xcircuit::keybind <Key-4> {page 4} $drawing
  xcircuit::keybind <Key-5> {page 5} $drawing
  xcircuit::keybind <Key-6> {page 6} $drawing
  xcircuit::keybind <Key-7> {page 7} $drawing
  xcircuit::keybind <Key-8> {page 8} $drawing
  xcircuit::keybind <Key-9> {page 9} $drawing
  xcircuit::keybind <Key-0> {page 10} $drawing

  # These are supposed to disable the scroll wheel on the scrollbars. . .

  bind ${name}.mainframe.mainarea.sbleft <Button-4> {}
  bind ${name}.mainframe.mainarea.sbleft <Button-5> {}
  bind ${name}.mainframe.mainarea.sbbottom <Button-4> {}
  bind ${name}.mainframe.mainarea.sbbottom <Button-5> {}

  if {$tcl_platform(platform) == "windows"} {
     bind $name <FocusIn> \
	"catch {config focus ${drawing} ; focus ${drawing}; \
	set XCOps(focus) ${name} ; xcircuit::updatedialog}"
  } else {
     bind $drawing <Enter> {focus %W}
     bind $name <FocusIn> "catch {config focus $drawing ; \
	set XCOps(focus) ${name} ; xcircuit::updatedialog}"
  }

  # Window-specific variable defaults (variables associated with toggle
  # and radio buttons, etc.).  Note that we really should set these
  # defaults for the first window only, and subsequent windows should
  # inherit values from the first window.

  set XCWinOps(${name},button1) None
  set XCWinOps(${name},colorval) inherit
  set XCWinOps(${name},jhoriz) left
  set XCWinOps(${name},jvert) bottom
  set XCWinOps(${name},linestyle) solid
  set XCWinOps(${name},fillamount) 0
  set XCWinOps(${name},opaque) false
  set XCWinOps(${name},polyedittype) manhattan
  set XCWinOps(${name},pathedittype) tangents
  set XCWinOps(${name},showgrid) true
  set XCWinOps(${name},showsnap) true
  set XCWinOps(${name},showaxes) true
  set XCWinOps(${name},showbbox) false
  set XCWinOps(${name},fontfamily) Helvetica
  set XCWinOps(${name},fontstyle) normal
  set XCWinOps(${name},fontencoding) Standard
  set XCWinOps(${name},fontlining) normal
  set XCWinOps(${name},fontscript) normal
  set XCWinOps(${name},gridstyle) "internal units"
  set XCWinOps(${name},flipinvariant) true
  set XCWinOps(${name},pinvisible) false
  set XCWinOps(${name},showclipmasks) show
  set XCWinOps(${name},latexmode) false
  set XCWinOps(${name},colorscheme) normal
  set XCWinOps(${name},editinplace) true
  set XCWinOps(${name},pinpositions) invisible
  set XCWinOps(${name},pinattach) false
  set XCWinOps(${name},namespaces) false
  set XCWinOps(${name},centerobject) true
  set XCWinOps(${name},manhattandraw) false
  set XCWinOps(${name},polyclosed) closed
  set XCWinOps(${name},scaleinvariant) invariant
  set XCWinOps(${name},endcaps) round
  set XCWinOps(${name},bboxtype) false
  set XCWinOps(${name},clipmask) false
  set XCWinOps(${name},substringparam) false
  set XCWinOps(${name},numericparam) false
  set XCWinOps(${name},expressparam) false
  set XCWinOps(${name},xposparam) false
  set XCWinOps(${name},yposparam) false
  set XCWinOps(${name},styleparam) false
  set XCWinOps(${name},justparam) false
  set XCWinOps(${name},startparam) false
  set XCWinOps(${name},endparam) false
  set XCWinOps(${name},radiusparam) false
  set XCWinOps(${name},minorparam) false
  set XCWinOps(${name},rotationparam) false
  set XCWinOps(${name},scaleparam) false
  set XCWinOps(${name},linewidthparam) false
  set XCWinOps(${name},colorparam) false
  set XCWinOps(${name},sel_lab) true
  set XCWinOps(${name},sel_inst) true
  set XCWinOps(${name},sel_poly) true
  set XCWinOps(${name},sel_arc) true
  set XCWinOps(${name},sel_spline) true
  set XCWinOps(${name},sel_graphic) true
  set XCWinOps(${name},sel_path) true
  set XCWinOps(${name},labeltype) Text
  set XCWinOps(${name},labelimage) img_t
  set XCWinOps(${name},rotateamount) 15

  #-----------------------------------------------------------------
  # The "catch" statement here allows "i" and "I" to have other bindings for
  # normal mode (e.g., "I" for "make info label") when nothing is selected.
  #-----------------------------------------------------------------

  xcircuit::keybind i {if {[catch {xcircuit::autoincr}]} \
	{standardaction %k down %s}} $drawing
  xcircuit::keybind I {if {[catch {xcircuit::autoincr -1}]} \
	{standardaction %k down %s}} $drawing

  #-----------------------------------------------------------------
  # Function bindings for the mouse scroll wheel.
  #-----------------------------------------------------------------

  xcircuit::keybind <Button-4> { pan up 0.05 ; refresh} $drawing
  xcircuit::keybind <Button-5> { pan down 0.05 ; refresh} $drawing
  xcircuit::keybind <Shift-Button-4> { pan left 0.05 ; refresh} $drawing
  xcircuit::keybind <Shift-Button-5> { pan right 0.05 ; refresh} $drawing

  #-----------------------------------------------------------------
  # Evaluate registered callback procedures
  #-----------------------------------------------------------------

  catch {eval $XCOps(callback)}
  catch {eval $XCWinOps(${name}, callback)}
}

#----------------------------------------------------------------------
# Delete a window.
#----------------------------------------------------------------------

proc xcircuit::closewindow {name} {
   set winlist [config windownames]
   if {[llength $winlist] > 1} {
      if {[lsearch $winlist $name] != -1} {
         config delete $name
	 set newwin [lindex [config windownames] 0]
	 destroy [winfo top $name]
	 config focus $newwin
      }
   } else {
      quit
   }
}

#----------------------------------------------------------------------
# Create a new window, and set it to the same page as the current one.
#----------------------------------------------------------------------

proc xcircuit::forkwindow {} {
   set suffix [llength [config windownames]]
   set newname .xcircuit${suffix}
   xcircuit::new_window $newname
   config init $newname
}

#----------------------------------------------------------------------
# Find the geometry position that centers a window on the cursor
# position
#----------------------------------------------------------------------

proc xcircuit::centerwin {wname} {
   set xmax [expr {[winfo screenwidth $wname] - 10}]
   set ymax [expr {[winfo screenheight $wname] - 35}] ;# allow for titlebar height
   set x [winfo pointerx $wname]
   set y [winfo pointery $wname]
   tkwait visibility $wname
   set w [winfo width $wname]
   set h [winfo height $wname]
   set x [expr $x - $w / 2]
   set y [expr $y - $h / 2]
   if {$x < 10} {set x 10}
   if {$y < 10} {set y 10}
   if {[expr {$x + $w}] > $xmax} {set x [expr {$xmax - $w}]}
   if {[expr {$y + $h}] > $ymax} {set y [expr {$ymax - $h}]}
   if {$x > 0} {set x "+$x"}
   if {$y > 0} {set y "+$y"}
   wm geometry $wname $x$y
}

#----------------------------------------------------------------------
# Message handling.  Assumes that windows are named
#  1) "pagename"	where the page name is printed
#  2) "coordinates"	where the coordinates are printed
#  3) "status"		the general-purpose status and message line.
#----------------------------------------------------------------------

proc xcircuit::print {wname string} {
   global XCOps
   set window $XCOps(focus)

   switch -glob ${wname} {
      page* {
	 ${window}.infobar.message1 configure -text ${string}
      }
      coord* {
	 ${window}.message configure -text ${string}
      }
      mes* -
      stat* {
	 ${window}.infobar.message2 configure -text ${string}
      }
   }
}

proc xcircuit::getinitstate {wname} {
   if [winfo exists $wname] {
      set wstate [wm state $wname]
   } else {
      set wstate "none"
   }
   return $wstate
}

#----------------------------------------------------------------------
# Support procedures for tag callbacks
#----------------------------------------------------------------------

proc xcircuit::popupdialog {} {
   set wstate [xcircuit::getinitstate .dialog]
   xcircuit::removelists .dialog
   wm deiconify .dialog
   if {"$wstate" != "normal"} {centerwin .dialog}
   raise .dialog
   focus .dialog.textent.txt
}

proc xcircuit::popupfilelist {} {
   set wstate [xcircuit::getinitstate .filelist]
   xcircuit::removelists .filelist
   wm deiconify .filelist
   centerwin .filelist
   raise .filelist
   focus .filelist.textent.txt
}

#----------------------------------------------------------------------
# A refined "page size" that keeps a lid on the numerical precision
# out of Tcl. . .
#----------------------------------------------------------------------

proc xcircuit::getpagesize {} {
   set slist [xcircuit::page size]
   set coordstyle [lindex $slist 3]
   if {$coordstyle == "cm"} {
      # Round centimeter coords to the nearest 0.1
      set xcm [lindex $slist 0]
      set xcm [expr {round($xcm * 10.0) / 10.0}]
      set slist [lreplace $slist 0 0 $xcm]
      set ycm [lindex $slist 2]
      set ycm [expr {round($ycm * 10.0) / 10.0}]
      set slist [lreplace $slist 2 2 $ycm]
   } elseif {$coordstyle == "in"} {
      # Round inch coords to the nearest 1/8
      set xin [lindex $slist 0]
      set xin [expr {round($xin * 8.0) / 8.0}]
      set slist [lreplace $slist 0 0 $xin]
      set yin [lindex $slist 2]
      set yin [expr {round($yin * 8.0) / 8.0}]
      set slist [lreplace $slist 2 2 $yin]
   }
   return $slist
}

#----------------------------------------------------------------------
# This procedure configures the sheet size according to the page
# dimensions (if they match, within reason)
#----------------------------------------------------------------------

proc xcircuit::setsheetsize {} {
   global XCOps

   set slist [xcircuit::getpagesize]
   set coordstyle [lindex $slist 3]
   if {$coordstyle == "cm"} {
      set xcm [lindex $slist 0]
      set ycm [lindex $slist 2]

      if {$xcm == 21.0 && $ycm == 29.7} {
         set XCOps(sheetsize) a4
         .output.textent.txtf.sizb configure -text "A4"
      } elseif {$xcm == 29.7 && $ycm == 42.0} {
         set XCOps(sheetsize) a3
         .output.textent.txtf.sizb configure -text "A3"
      } elseif {$xcm == 14.8 && $ycm == 18.4} {
         set XCOps(sheetsize) a5
         .output.textent.txtf.sizb configure -text "A5"
      } elseif {$xcm == 25.7 && $ycm == 36.4} {
         set XCOps(sheetsize) b4
         .output.textent.txtf.sizb configure -text "B4"
      } elseif {$xcm == 18.2 && $ycm == 25.7} {
         set XCOps(sheetsize) b5
         .output.textent.txtf.sizb configure -text "B5"
      } else {
         set XCOps(sheetsize) special
         .output.textent.txtf.sizb configure -text "Special"
      }
   } elseif {$coordstyle == "in"} {
      set xin [lindex $slist 0]
      set yin [lindex $slist 2]

      if {$xin == 8.5 && $yin == 11.0} {
         set XCOps(sheetsize) letter
         .output.textent.txtf.sizb configure -text Letter
      } elseif {$xin == 8.5 && $yin == 14.0} {
         set XCOps(sheetsize) legal
         .output.textent.txtf.sizb configure -text Legal
      } elseif {$xin == 5.5 && $yin == 8.5} {
         set XCOps(sheetsize) statement
         .output.textent.txtf.sizb configure -text Statement
      } elseif {$xin == 11.0 && $yin == 17.0} {
         set XCOps(sheetsize) tabloid
         .output.textent.txtf.sizb configure -text Tabloid
      } elseif {$xin == 17.0 && $yin == 11.0} {
         set XCOps(sheetsize) ledger
         .output.textent.txtf.sizb configure -text Ledger
      } elseif {$xin == 8.5 && $yin == 13.0} {
         set XCOps(sheetsize) folio
         .output.textent.txtf.sizb configure -text Folio
      } elseif {$xin == 10.0 && $yin == 14.0} {
         set XCOps(sheetsize) tenfourteen
         .output.textent.txtf.sizb configure -text 10x14
      } elseif {$xin == 7.5 && $yin == 10.0} {
         set XCOps(sheetsize) executive
         .output.textent.txtf.sizb configure -text Executive
      } elseif {$xin == 17.0 && $yin == 22.0} {
         set XCOps(sheetsize) ansic
         .output.textent.txtf.sizb configure -text "ANSI C"
      } elseif {$xin == 22.0 && $yin == 34.0} {
         set XCOps(sheetsize) ansid
         .output.textent.txtf.sizb configure -text "ANSI D"
      } elseif {$xin == 34.0 && $yin == 44.0} {
         set XCOps(sheetsize) ansie
         .output.textent.txtf.sizb configure -text "ANSI E"
      } else {
         set XCOps(sheetsize) special
         .output.textent.txtf.sizb configure -text "Special"
      }
   }
}

#----------------------------------------------------------------------
# This procedure configures the output properties window according to
# the page mode (full or encapsulated)
#----------------------------------------------------------------------

proc xcircuit::setpstype {mode} {
   global XCOps
   switch -- $mode {
      {eps} { .output.textent.butp configure -text "Embedded (EPS)"
	 grid remove .output.textent.but7
	 grid remove .output.textent.butf
	 grid remove .output.textent.txtf
      }
      {full} {.output.textent.butp configure -text "Full Page"
	 grid .output.textent.but7 -row 6 -column 3 -pady 5 -ipadx 10
	 grid .output.textent.butf -row 5 -column 2 -padx 10
	 grid .output.textent.txtf -row 6 -column 2 -sticky ew -padx 10
      }
   }
   set XCOps(pstype) $mode
   xcircuit::page encapsulation $mode
}

#----------------------------------------------------------------------

proc xcircuit::dolinks {} {
   global XCOps
   set ilinks [xcircuit::page links independent]
   if {$ilinks > 1} {
      set XCOps(imultiple) 1
   } else {
      set XCOps(imultiple) 0
   }
   if {$ilinks == 1} { set plural ""} else { set plural "s"}
   .output.title.imulti configure -text "$ilinks schematic$plural"

   if {$XCOps(dmultiple) == 1} {
      set dlinks [xcircuit::page links dependent]
   } else {
      set dlinks 0
   }
   if {$dlinks == 1} { set plural ""} else { set plural "s"}
   .output.title.dmulti configure -text "$dlinks subcircuit$plural"
}

#----------------------------------------------------------------------

proc xcircuit::setlinksmenu {} {
   set m .output.textent.butl.linksmenu
   $m delete 0 end
   $m add radio -label "None" -variable XCOps(links) -command \
	{.output.textent.butl configure -text None ; \
	xcircuit::page filename {}}
   if {![catch {set plist [xcircuit::page list]}]} {
      set fnames {}
      foreach p $plist {
         set pfile [xcircuit::page $p filename]
	 if {"$pfile" != ""} {
	    lappend fnames $pfile
	 }
      }
      foreach f [lsort -uniq $fnames] {
         $m add radio -label $f -variable XCOps(links) \
		-command ".output.textent.butl configure -text $f ; \
		xcircuit::page filename $f"
      }
   }
   .output.textent.butl configure -text "(change)"
}

#----------------------------------------------------------------------

proc xcircuit::pageupdate { {subcommand "none"} } {
   global XCOps
   if {[info level] <= 1} {
     switch -- $subcommand {
	save {
	   .output.bbar.okay configure -text "Done"
	   .output.bbar.okay configure -command {wm withdraw .output}
	}
	make {
	   xcircuit::newpagebutton [xcircuit::page label]
	}
	default {
	  .output.title.field configure -text \
		"PostScript output properties (Page [xcircuit::page])"
	  set fname [xcircuit::page filename]
	  .output.textent.but1 configure -text Apply
	  .output.textent.but2 configure -text Apply
	  .output.textent.but3 configure -text Apply
	  .output.textent.but4 configure -text Apply
	  .output.textent.but5 configure -text Apply
	  .output.textent.but7 configure -text Apply
	  .output.textent.txt1 delete 0 end
	  .output.textent.txt1 insert 0 $fname
	  .output.textent.txt2 delete 0 end
	  .output.textent.txt2 insert 0 [xcircuit::page label]
	  .output.textent.txt3 delete 0 end
	  set stext [format "%g" [xcircuit::page scale]]
	  .output.textent.txt3 insert 0 $stext
	  .output.textent.txt4 delete 0 end
	  set wtext [format "%g" [xcircuit::page width]]
	  .output.textent.txt4 insert 0 $wtext
	  .output.textent.txt4 insert end " "
	  .output.textent.txt4 insert end [xcircuit::coordstyle get]
	  .output.textent.txt5 delete 0 end
	  set htext [format "%g" [xcircuit::page height]]
	  .output.textent.txt5 insert 0 $htext
	  .output.textent.txt5 insert end " "
	  .output.textent.txt5 insert end [xcircuit::coordstyle get]
	  .output.textent.txtf.txtp delete 0 end
	  .output.textent.txtf.txtp insert 0 [xcircuit::getpagesize]
	  xcircuit::setpstype [xcircuit::page encapsulation]
	  set XCOps(orient) [xcircuit::page orientation]
	  if {$XCOps(orient) == 0} {
	     .output.textent.buto configure -text Portrait
	  } else {
	     .output.textent.buto configure -text Landscape
	  }
	  xcircuit::dolinks
	  xcircuit::setlinksmenu
	  xcircuit::setsheetsize

	  set XCOps(autofit) [xcircuit::page fit]
	  if {[string match *.* $fname] == 0} {append fname .ps}
	  if {[glob -nocomplain ${fname}] == {}} {
	    .output.bbar.okay configure -text "Write File"
	  } else {
	    .output.bbar.okay configure -text "Overwrite File"
	  }
	  .output.bbar.okay configure -command \
		{.output.textent.but1 invoke; \
		 .output.textent.but2 invoke; \
		 if {$XCOps(autofit)} {xcircuit::page fit true}; \
		 if {$XCOps(dmultiple) == 1} {xcircuit::page save} else { \
		 xcircuit::page saveonly }; wm withdraw .output}
        }
     }
  }
}

#----------------------------------------------------------------------
# Update the GUI based on the schematic class of the current page
# This is called internally from the xcircuit code and the function
# must be defined, even if it is a null proc.
#----------------------------------------------------------------------

proc xcircuit::setsymschem {} {
   global XCOps
   set window $XCOps(focus)

   if {[info level] <= 1} {
      set schemtype [xcircuit::schematic type]
      set symschem [xcircuit::schematic get]
      set m ${window}.menubar.netlistbutton.netlistmenu
      switch -- $schemtype {
         primary -
	 secondary -
	 schematic {
	    ${window}.infobar.schem configure -background red -foreground white
	    if {$symschem == {}} {
		${window}.infobar.symb configure -background gray70 \
			-foreground gray40
		$m entryconfigure 5 -label "Make Matching Symbol" \
			-command {xcircuit::promptmakesymbol [page label]}
		$m entryconfigure 6 -label "Associate With Symbol" \
			-command {xcircuit::symbol associate}
	    } else {
		${window}.infobar.symb configure -background white -foreground black
		$m entryconfigure 5 -label "Go To Symbol" \
			-command {xcircuit::symbol go}
		$m entryconfigure 6 -label "Disassociate Symbol" \
			-command {xcircuit::symbol disassociate}
	    }
	 }
	 symbol -
	 fundamental -
	 trivial {
	    ${window}.infobar.symb configure -foreground white
	    if {$symschem == {}} {
		${window}.infobar.schem configure -background gray70 -foreground \
			gray40
		$m entryconfigure 5 -label "Make Matching Schematic" \
			-command {xcircuit::schematic make}
		$m entryconfigure 6 -label "Associate With Schematic" \
			-command {xcircuit::schematic associate}
	    } else {
		${window}.infobar.schem configure -background white -foreground black
		$m entryconfigure 5 -label "Go To Schematic" \
			-command {xcircuit::schematic go}
		$m entryconfigure 6 -label "Disassociate Schematic" \
			-command {xcircuit::schematic disassociate}
	    }
	 }
      }
      switch -- $schemtype {
	 trivial {
	    ${window}.infobar.symb configure -background red
	 }
	 fundamental {
	    ${window}.infobar.symb configure -background green4 ;# bboxcolor
	 }
	 symbol {
	    ${window}.infobar.symb configure -background blue2 
	 }
      }
   }
}
   
#----------------------------------------------------------------------
# Set the coordinate style to inches from cm and vice versa.
# This routine avoids switching from fractional to decimal inches
# and vice versa if we are already in one of the two inches modes.
#
# with no argument, or argument "get", returns the "short" name
# ("cm" or "in") of the style.
#----------------------------------------------------------------------

proc xcircuit::coordstyle { { mode get } } {
   global XCOps XCWinOps
   set curstyle [xcircuit::config coordstyle]
   switch -- $mode {
      inches {
	 switch -- $curstyle {
	    centimeters {
	       xcircuit::config coordstyle "decimal inches"
	       xcircuit::pageupdate
	    }
	 }
      }
      centimeters -
      cm {
	 switch -- $curstyle {
	    centimeters {
	    }
	    default {
	       xcircuit::config coordstyle "centimeters"
	       xcircuit::pageupdate
	    }
	 }
      }
      get {
	 switch -- $curstyle {
	    centimeters {
	       return "cm"
	    }
	    default {
	       return "in"
	    }
	 }
      }
   }
}

#----------------------------------------------------------------------

proc xcircuit::raiseconsole {} {
   global XCOps
   set window $XCOps(focus)

   xcircuit::consoleup
   xcircuit::consoleontop
   set cidx [${window}.menubar.filebutton.filemenu index *Console]
   ${window}.menubar.filebutton.filemenu entryconfigure \
	 $cidx -label "No Console" -command {xcircuit::lowerconsole}
}

#----------------------------------------------------------------------

proc xcircuit::lowerconsole {} {
   global XCOps
   set window $XCOps(focus)

   xcircuit::consoledown
   set cidx [${window}.menubar.filebutton.filemenu index *Console]
   ${window}.menubar.filebutton.filemenu entryconfigure \
	 $cidx -label "Tcl Console" -command {xcircuit::raiseconsole}
}

#----------------------------------------------------------------------
# Command tags---these let the command-line entry functions update the
# Tk windows, so that the Tk window structure doesn't need to be hard-
# coded into the source.
#----------------------------------------------------------------------

xcircuit::tag page {xcircuit::pageupdate %1 ; xcircuit::updateparams}
xcircuit::tag promptsavepage {xcircuit::pageupdate ;
        set wstate [xcircuit::getinitstate .output] ; wm deiconify .output ;
	if {"$wstate" != "normal"} {xcircuit::centerwin .output} ; raise .output}
xcircuit::tag loadfont {xcircuit::newfontbutton %r}
xcircuit::tag color { if {"%1" == "set"} {
	set XCWinOps($XCOps(focus),colorval) %2; set iname img_co;
	if {"%2" != "inherit"} {append iname l%2} ;
	$XCOps(focus).mainframe.toolbar.bco configure -image $iname} }
xcircuit::tag border {if {%# == 2} {
	switch -- %1 {
	   dashed { set XCWinOps($XCOps(focus),linestyle) dashed}
	   dotted { set XCWinOps($XCOps(focus),linestyle) dotted}
	   unbordered { set XCWinOps($XCOps(focus),linestyle) unbordered}
	   solid { set XCWinOps($XCOps(focus),linestyle) solid}
	   square { set XCWinOps($XCOps(focus),endcaps) square}
	   round { set XCWinOps($XCOps(focus),endcaps) round}
	   closed { set XCWinOps($XCOps(focus),polyclosed) closed}
	   unclosed { set XCWinOps($XCOps(focus),polyclosed) unclosed}
	}} elseif {%# == 3} {
	switch -- %1 {
	   bbox { set XCWinOps($XCOps(focus),bboxtype) %2}
	   clipmask { set XCWinOps($XCOps(focus),clipmask) %2}
	}}}
xcircuit::tag fill { foreach i %N { switch -- "$i" {
	   opaque { set XCWinOps($XCOps(focus),opaque) true }
	   transparent { set XCWinOps($XCOps(focus),opaque) false } 0 - unfilled
		{set XCWinOps($XCOps(focus),fillamount) 0;
	      $XCOps(focus).mainframe.toolbar.bfi configure -image img_fi}
	   solid {set XCWinOps($XCOps(focus),fillamount) 100;
	      $XCOps(focus).mainframe.toolbar.bfi configure -image img_stip100}
	   default {set XCWinOps($XCOps(focus),fillamount) $i;
	      $XCOps(focus).mainframe.toolbar.bfi configure -image img_stip$i} } } }

xcircuit::tag select {if {%N > 1} {xcircuit::updateparams; xcircuit::updatedialog}}
xcircuit::tag unselect {xcircuit::updateparams; xcircuit::updatedialog}
xcircuit::tag schematic {xcircuit::setsymschem}
xcircuit::tag symbol {xcircuit::setsymschem}

xcircuit::tag parameter { if {"%1" == "make"} {set cond true} else {set cond false} 
    switch %1 {
	set -
	forget -
	delete {xcircuit::updateparams}
	make -
	replace {switch -- "%2" {
		"x position" {set XCWinOps($XCOps(focus),xposparam) $cond}
		"y position" {set XCWinOps($XCOps(focus),yposparam) $cond}
		style {set XCWinOps($XCOps(focus),styleparam) $cond}
		"start angle" {set XCWinOps($XCOps(focus),startparam) $cond}
		"end angle" {set XCWinOps($XCOps(focus),endparam) $cond}
		justification {set XCWinOps($XCOps(focus),justparam) $cond}
		radius {set XCWinOps($XCOps(focus),radiusparam) $cond}
		"minor axis" {set XCWinOps($XCOps(focus),minorparam) $cond}
		rotation {set XCWinOps($XCOps(focus),rotationparam) $cond}
		scale {set XCWinOps($XCOps(focus),scaleparam) $cond}
		linewidth {set XCWinOps($XCOps(focus),linewidthparam) $cond}
		color {set XCWinOps($XCOps(focus),colorparam) $cond}
		default {xcircuit::updateparams}
	}}
	default {if {%# == 4} {xcircuit::updateparams}}
    }}

xcircuit::tag config {if {%# == 3} {
	switch -- %1 {
	   colorscheme {set XCWinOps($XCOps(focus),colorscheme) [config colorscheme];
		refresh}
	   bbox {set XCWinOps($XCOps(focus),showbbox) [config bbox]}
	   editinplace {set XCWinOps($XCOps(focus),editinplace) [config editinplace]}
	   pinpositions {set XCWinOps($XCOps(focus),pinpositions) [config pinpositions]}
	   pinattach {set XCWinOps($XCOps(focus),pinattach) [config pinattach]}
	   technologies {set XCWinOps($XCOps(focus),namespaces) [config technologies]}
	   hold {set XCOps(hold) [config hold]}
	   grid {catch {set XCWinOps($XCOps(focus),showgrid) [config grid]}}
	   snap {catch {set XCWinOps($XCOps(focus),showsnap) [config snap]}}
	   axes {set XCWinOps($XCOps(focus),showaxes) [config axes]}
	   centering {set XCWinOps($XCOps(focus),centerobject) [config centering]}
	   manhattan {set XCWinOps($XCOps(focus),manhattandraw) [config manhattan]}
	   coordstyle {set XCWinOps($XCOps(focus),gridstyle) [config coordstyle]}
	   boxedit {set XCWinOps($XCOps(focus),polyedittype) [config boxedit]}
	   pathedit {set XCWinOps($XCOps(focus),pathedittype) [config pathedit]}
	   technologies {set XCWinOps($XCOps(focus),showtech) [config technologies]}
	}} elseif {(%# == 4) && ("%1" == "filter")} {
	   set XCWinOps($XCOps(focus),sel_%2) [config filter %2]
	}}

xcircuit::tag label {if {%# == 3} {
	switch -- %1 {
	   encoding {
	      set XCWinOps($XCOps(focus),fontencoding) %2
	      xcircuit::newencodingbutton %2
	   }
	   family {if {"%2" != "-all"} {set XCWinOps($XCOps(focus),fontfamily) %2}}
	   style {set XCWinOps($XCOps(focus),fontstyle) %2}
	   justify {
	      switch -- %2 {
		 top -
		 bottom -
		 middle {set XCWinOps($XCOps(focus),jvert) %2}
		 default {set XCWinOps($XCOps(focus),jhoriz) %2}
	      }
	   }
	   flipinvariant {set XCWinOps($XCOps(focus),flipinvariant) %2}
	   visible {set XCWinOps($XCOps(focus),pinvisible) %2}
	   latex {set XCWinOps($XCOps(focus),latexmode) %2}
        }} elseif {(%# == 4) && ("%1" == "justify")} {
	   switch -- %2 {
	      top -
	      bottom -
	      middle {set XCWinOps($XCOps(focus),jvert) %2 ;
	      	      set XCWinOps($XCOps(focus),jhoriz) %3}
	      default {set XCWinOps($XCOps(focus),jhoriz) %2 ;
		      set XCWinOps($XCOps(focus),jvert) %3}
	   }
	}}

#------------------------------
# Create the file-list window
#------------------------------

# First, set the variables associated with toggle and radio buttons
set XCOps(filter) 1

toplevel .filelist -bg beige
wm title .filelist "File List Window"
wm group .filelist .
wm protocol .filelist WM_DELETE_WINDOW {wm withdraw .filelist}
wm withdraw .filelist

frame .filelist.listwin
frame .filelist.textent -bg beige
frame .filelist.bbar	-bg beige

pack .filelist.listwin -side top -padx 20 -pady 7 -expand true -fill both
pack .filelist.textent -side top -padx 20 -pady 7 -fill x
pack .filelist.bbar -side bottom -padx 20 -pady 7 -fill x

simple .filelist.listwin.win -bg white
simple .filelist.listwin.sb -width 13 -bg beige

grid .filelist.listwin.win -row 0 -column 0 -sticky news -padx 1 -pady 1
grid .filelist.listwin.sb -row 0 -column 1 -sticky ns -padx 1 -pady 1

grid columnconfigure .filelist.listwin 0 -weight 1 -minsize 100
grid rowconfigure .filelist.listwin 0 -weight 1 -minsize 100

frame .filelist.textent.title -bg beige
pack .filelist.textent.title -side top -fill x

label .filelist.textent.title.field -text "Select file to load:" -bg beige
label .filelist.textent.title.chklabel -text "Filter" -bg beige
checkbutton .filelist.textent.title.filter -bg beige -variable XCOps(filter) \
   -command {event generate .filelist.listwin.win <ButtonPress> -button 3 ; \
	event generate .filelist.listwin.win <ButtonRelease> -button 3}

entry .filelist.textent.txt -bg white -relief sunken -width 50

pack .filelist.textent.title.filter -side right
pack .filelist.textent.title.chklabel -side right
pack .filelist.textent.title.field -side left
pack .filelist.textent.txt -side bottom -fill x -expand true

button .filelist.bbar.okay -text Okay -bg beige
button .filelist.bbar.cancel -text Cancel -bg beige -command {wm withdraw .filelist}

pack .filelist.bbar.okay -side left -ipadx 10
pack .filelist.bbar.cancel -side right -ipadx 10

# Allow <return> to update or accept entry
bind .filelist.textent.txt <Return> \
	{event generate .filelist.listwin.win <ButtonPress> -button 2 ; \
	event generate .filelist.listwin.win <ButtonRelease> -button 2}

#--------------------------------------
# Create the output generating window
#--------------------------------------

# First, set the variables associated with toggle and radio buttons
set XCOps(autofit) 0
set XCOps(imultiple) 0
set XCOps(dmultiple) 0	;# don't save subcircuits with different filenames
if {[catch {set XCOps(technology)}]} {set XCOps(technology) "(user)"}
if {[catch {set XCOps(library)}]} {set XCOps(library) "User Library"}

toplevel .output -bg beige
wm title .output "PostScript Output Properties"
wm group .output .
wm protocol .output WM_DELETE_WINDOW {wm withdraw .output}
wm withdraw .output

frame .output.title -bg beige
frame .output.textent -bg beige
frame .output.bbar -bg beige

pack .output.title -side top -padx 20 -pady 7 -fill x
pack .output.textent -side top -padx 20 -pady 7 -fill x
pack .output.bbar -side bottom -padx 20 -pady 7 -fill x

label .output.title.field -text "PostScript output properties (Page 1):" -bg tan
checkbutton .output.title.imulti -text "1 schematic" -bg beige \
	-variable XCOps(imultiple) \
	-command {xcircuit::dolinks ; \
	if {$XCOps(imultiple) == 1} {.output.textent.txt1 \
	delete 0 end; .output.textent.but1 configure -text Apply; xcircuit::page \
	filename {}; focus .output.textent.txt1 ; xcircuit::dolinks }}
checkbutton .output.title.dmulti -text "0 subcircuits" -bg beige \
	-variable XCOps(dmultiple) \
	-command {xcircuit::dolinks ; .output.textent.but1 configure -text Apply; \
	if {$XCOps(dmultiple) == 1} {xcircuit::page filename {}; \
	.output.textent.txt1 delete 0 end; focus .output.textent.txt1 }; \
	xcircuit::dolinks }

pack .output.title.dmulti -side right -padx 5
pack .output.title.imulti -side right -padx 5

pack .output.title.field -side left

label .output.textent.lab1 -text "Filename:" -bg beige
label .output.textent.lab2 -text "Page label:" -bg beige
label .output.textent.lab3 -text "Scale:" -bg beige
label .output.textent.lab4 -text "Width:" -bg beige
label .output.textent.lab5 -text "Height:" -bg beige
label .output.textent.lab6 -text "Orientation:" -bg beige
label .output.textent.lab7 -text "Mode:" -bg beige
label .output.textent.lab8 -text "Link to:" -bg beige

entry .output.textent.txt1 -bg white -relief sunken -width 20
entry .output.textent.txt2 -bg white -relief sunken -width 20
entry .output.textent.txt3 -bg white -relief sunken -width 20
entry .output.textent.txt4 -bg white -relief sunken -width 20
entry .output.textent.txt5 -bg white -relief sunken -width 20

menubutton .output.textent.buto -text Portrait -bg beige \
	-menu .output.textent.buto.orientmenu
menubutton .output.textent.butp -text "Embedded (EPS)" -bg beige \
	-menu .output.textent.butp.psmenu
menubutton .output.textent.butl -text "(change)" -bg beige \
	-menu .output.textent.butl.linksmenu

checkbutton .output.textent.butf -text "Auto-fit" -bg beige \
	-variable XCOps(autofit) -onvalue true -offvalue false \
	-command {xcircuit::page fit $XCOps(autofit)}
frame .output.textent.txtf -bg beige
menubutton .output.textent.txtf.sizb -text "Sizes" -bg beige \
	-menu .output.textent.txtf.sizb.sizemenu
entry .output.textent.txtf.txtp -bg white -relief sunken -width 14

pack .output.textent.txtf.txtp -side left -fill y
pack .output.textent.txtf.sizb -side left

button .output.textent.but1 -text Apply -bg beige \
	-command {xcircuit::page filename [.output.textent.txt1 get]
	if {[llength [xcircuit::page label]] > 1} {
	   xcircuit::page label [file root [.output.textent.txt1 get]]};\
	.output.textent.but1 configure -text Okay}
button .output.textent.but2 -text Apply -bg beige \
	-command {xcircuit::page label [.output.textent.txt2 get];\
	.output.textent.but2 configure -text Okay}
button .output.textent.but3 -text Apply -bg beige \
	-command {xcircuit::page scale [.output.textent.txt3 get];\
	.output.textent.but3 configure -text Okay}
button .output.textent.but4 -text Apply -bg beige \
	-command {xcircuit::page width [.output.textent.txt4 get];\
	.output.textent.but4 configure -text Okay}
button .output.textent.but5 -text Apply -bg beige \
	-command {xcircuit::page height [.output.textent.txt5 get];\
	.output.textent.but5 configure -text Okay}
button .output.textent.but7 -text Apply -bg beige \
	-command {xcircuit::page size [.output.textent.txtf.txtp get];\
	.output.textent.but7 configure -text Okay}

bind .output.textent.txt1 <Return> {.output.textent.but1 invoke}
bind .output.textent.txt2 <Return> {.output.textent.but2 invoke}
bind .output.textent.txt3 <Return> {.output.textent.but3 invoke}
bind .output.textent.txt4 <Return> {.output.textent.but4 invoke}
bind .output.textent.txt5 <Return> {.output.textent.but5 invoke}

grid .output.textent.lab1 -row 0 -column 0 -sticky w
grid .output.textent.lab2 -row 1 -column 0 -sticky w
grid .output.textent.lab3 -row 2 -column 0 -sticky w
grid .output.textent.lab4 -row 3 -column 0 -sticky w
grid .output.textent.lab5 -row 4 -column 0 -sticky w
grid .output.textent.lab6 -row 5 -column 0 -sticky w
grid .output.textent.lab7 -row 6 -column 0 -sticky w
grid .output.textent.lab8 -row 7 -column 0 -sticky w

grid .output.textent.txt1 -row 0 -column 1 -columnspan 2 -sticky ew -padx 10
grid .output.textent.txt2 -row 1 -column 1 -columnspan 2 -sticky ew -padx 10
grid .output.textent.txt3 -row 2 -column 1 -columnspan 2 -sticky ew -padx 10
grid .output.textent.txt4 -row 3 -column 1 -columnspan 2 -sticky ew -padx 10
grid .output.textent.txt5 -row 4 -column 1 -columnspan 2 -sticky ew -padx 10
grid .output.textent.buto -row 5 -column 1 -sticky w -padx 10
grid .output.textent.butp -row 6 -column 1 -sticky w -padx 10
grid .output.textent.butl -row 7 -column 1 -sticky w -padx 10

grid .output.textent.but1 -row 0 -column 3 -pady 5 -ipadx 10
grid .output.textent.but2 -row 1 -column 3 -pady 5 -ipadx 10
grid .output.textent.but3 -row 2 -column 3 -pady 5 -ipadx 10
grid .output.textent.but4 -row 3 -column 3 -pady 5 -ipadx 10
grid .output.textent.but5 -row 4 -column 3 -pady 5 -ipadx 10

grid columnconfigure .output.textent 2 -weight 1

button .output.bbar.okay -text Okay -bg beige -command {xcircuit::page save; \
  wm withdraw .output}
button .output.bbar.cancel -text Cancel -bg beige -command {wm withdraw .output}

# Setup simple choice menus for page type and orientation
# First, set the variables associated with the radio buttons. . . 
set XCOps(orient) 0
set XCOps(pstype) eps

set m [menu .output.textent.buto.orientmenu -tearoff 0]
$m add radio -label "Portrait" -variable XCOps(orient) -value 0 -command \
	{.output.textent.buto configure -text Portrait ; \
	xcircuit::page orientation 0}
$m add radio -label "Landscape" -variable XCOps(orient) -value 90 -command \
	{.output.textent.buto configure -text Landscape ; \
	xcircuit::page orientation 90}

set m [menu .output.textent.butp.psmenu -tearoff 0]
$m add radio -label "Embedded (EPS)" -variable XCOps(pstype) -value eps -command \
	{xcircuit::setpstype eps}
$m add radio -label "Full Page" -variable XCOps(pstype) -value full -command \
	{xcircuit::setpstype full}

menu .output.textent.butl.linksmenu -tearoff 0
xcircuit::setlinksmenu

pack .output.bbar.okay -side left -ipadx 10
pack .output.bbar.cancel -side right -ipadx 10

set m [menu .output.textent.txtf.sizb.sizemenu -tearoff 0]
$m add radio -label "Letter (ANSI A)" -variable XCOps(sheetsize) \
	-value letter -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "8.5 x 11.0 in"}
$m add radio -label "Legal" -variable XCOps(sheetsize) -value legal -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "8.5 x 14.0 in"}
$m add radio -label "Statement" -variable XCOps(sheetsize) -value statement \
	-command \
	{ xcircuit::coordstyle inches; xcircuit::page size "5.5 x 8.5 in"}
$m add radio -label "Tabloid (ANSI B)" -variable XCOps(sheetsize) \
	 -value tabloid -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "11.0 x 17.0 in"}
$m add radio -label "Ledger" -variable XCOps(sheetsize) -value ledger -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "17.0 x 11.0 in"}
$m add radio -label "Folio" -variable XCOps(sheetsize) -value folio -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "8.5 x 13.0 in"}
$m add radio -label "Quarto" -variable XCOps(sheetsize) -value quarto -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "8.472 x 10.833 in"}
$m add radio -label "10x14" -variable XCOps(sheetsize) -value tenfourteen -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "10.0 x 14.0 in"}
$m add radio -label "Executive" -variable XCOps(sheetsize) -value executive -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "7.5 x 10.0 in"}
$m add radio -label "ANSI C" -variable XCOps(sheetsize) -value ansic -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "17.0 x 22.0 in"}
$m add radio -label "ANSI D" -variable XCOps(sheetsize) -value ansid -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "22.0 x 34.0 in"}
$m add radio -label "ANSI E" -variable XCOps(sheetsize) -value ansie -command \
	{ xcircuit::coordstyle inches; xcircuit::page size "34.0 x 44.0 in"}
$m add radio -label "A3" -variable XCOps(sheetsize) -value a3 -command \
	{ xcircuit::coordstyle centimeters; xcircuit::page size "29.7 x 42.0 cm"}
$m add radio -label "A4" -variable XCOps(sheetsize) -value a4 -command \
	{ xcircuit::coordstyle centimeters; xcircuit::page size "21.0 x 29.7 cm"}
$m add radio -label "A5" -variable XCOps(sheetsize) -value a5 -command \
	{ xcircuit::coordstyle centimeters; xcircuit::page size "14.82 x 18.43 cm"}
$m add radio -label "B4" -variable XCOps(sheetsize) -value b4 -command \
	{ xcircuit::coordstyle centimeters; xcircuit::page size "25.7 x 36.4 cm"}
$m add radio -label "B5" -variable XCOps(sheetsize) -value b5 -command \
	{ xcircuit::coordstyle centimeters; xcircuit::page size "18.2 x 25.7 cm"}
$m add radio -label "Special" -variable XCOps(sheetsize) -value special}

#-----------------------------------------------------------------
# Clear the selection listbox.  Create it if it does not exist.
#-----------------------------------------------------------------

proc xcircuit::make_parameter_listbox {} {
   if {[catch {wm state .parameter}]} {
      toplevel .parameter -bg beige
      wm group .parameter .
      wm withdraw .parameter

      label .parameter.title -text "Parameters" -bg beige
      label .parameter.keytitle -text "Key" -bg beige
      label .parameter.valtitle -text "Value" -bg beige

      listbox .parameter.keylist -bg white
      listbox .parameter.vallist -bg white
      listbox .parameter.parvals -bg white

      button .parameter.dismiss -text "Dismiss" -bg beige \
		-command {wm withdraw .parameter}

      menubutton .parameter.delete -text "Delete..." -bg beige \
		-menu .parameter.delete.deleteparam
      menu .parameter.delete.deleteparam -tearoff 0

      menubutton .parameter.create -text "New..." -bg beige \
		-menu .parameter.create.newparam
      menu .parameter.create.newparam -tearoff 0
      .parameter.create.newparam add command -label "Substring" -command \
		"xcircuit::promptmakeparam substring"
      .parameter.create.newparam add command -label "Numeric" -command \
		"xcircuit::promptmakeparam numeric"
      .parameter.create.newparam add command -label "Expression" -command \
		"xcircuit::promptmakeparam expression"

      labelframe .parameter.valedit -text "Edit value" -bg beige
      entry .parameter.valedit.entry -textvariable new_paramval -bg white
      button .parameter.valedit.apply -text "Apply" -bg beige

      pack .parameter.valedit.entry -side left -fill x -expand true -padx 2
      pack .parameter.valedit.apply -side top

      grid .parameter.title -row 0 -column 0 -columnspan 2 -sticky news
      grid .parameter.keytitle -row 1 -column 0 -sticky news
      grid .parameter.keylist -row 2 -column 0 -sticky news
      grid .parameter.valtitle -row 1 -column 1 -sticky news
      grid .parameter.vallist -row 2 -column 1 -sticky news
      grid .parameter.parvals -row 2 -column 1 -sticky news

      grid .parameter.valedit -row 3 -column 0 -columnspan 2 -padx 2 \
		-pady 2 -sticky ew

      grid .parameter.create -row 4 -column 1 -sticky ns
      grid .parameter.delete -row 4 -column 0 -sticky ns
      grid .parameter.dismiss -row 5 -column 0 -columnspan 2 -sticky ns

      grid rowconfigure .parameter 2 -weight 1
      grid columnconfigure .parameter 0 -weight 1
      grid columnconfigure .parameter 1 -weight 2

      raise .parameter.vallist

      bind .parameter <Escape> {wm withdraw .parameter}
      bind .parameter.valedit.entry <Return> {.parameter.valedit.apply invoke}
   }
}

# Update the dialog box, if it has been left visible
# (Corrected 2/4/12:  Don't delete contents except in these specific cases!)

proc xcircuit::updatedialog {} {
   global XCOps
   if {[xcircuit::getinitstate .dialog] == "normal"} {
      switch -- $XCOps(dialog) {
 	 linewidth {
	    set btext [format "%g" [lindex [xcircuit::border get] 0]]
            .dialog.textent.txt delete 0 end
	    .dialog.textent.txt insert 0 $btext
	 }
	 textscale {
	    set cscale [xcircuit::label scale]
            .dialog.textent.txt delete 0 end
	    .dialog.textent.txt insert 0 $cscale
	 }
	 elementscale {
            set selects [xcircuit::select]
	    if {$selects > 0} {
	       set cscale [xcircuit::element scale]
               .dialog.textent.txt delete 0 end
	       .dialog.textent.txt insert 0 $cscale
	    }
	 }
      }
   }
}

proc xcircuit::makedialogline {dframe textline} {
   if {[catch {frame .dialog.${dframe} -bg beige}]} {
      .dialog.${dframe}.title.field configure -text ${textline}
   } else {
      pack .dialog.${dframe} -side top -padx 20 -pady 7 -fill x

      frame .dialog.${dframe}.title -bg beige
      entry .dialog.${dframe}.txt -bg white -relief sunken -width 50

      pack .dialog.${dframe}.title -side top -fill x
      pack .dialog.${dframe}.txt -side bottom -fill x -expand true

      label .dialog.${dframe}.title.field -text ${textline} -bg beige
      pack .dialog.${dframe}.title.field -side left
   }
}

proc xcircuit::removedialogline {dframe} {
   global XCOps
   pack forget .dialog.${dframe}
   destroy .dialog.${dframe}
   set XCOps(dialog) 0
}

#--------------------------------------
# Create the simple popup prompt window
#--------------------------------------

toplevel .dialog -bg beige
wm title .dialog "Dialog Box"
wm group .dialog .
wm protocol .dialog WM_DELETE_WINDOW {wm withdraw .dialog}
wm withdraw .dialog
set XCOps(dialog) 0

xcircuit::makedialogline textent "Select file to load:"

frame .dialog.bbar	-bg beige
pack .dialog.bbar -side bottom -padx 20 -pady 7 -fill x

button .dialog.bbar.okay -text Okay -bg beige -command {.dialog.bbar.apply invoke ;\
	wm withdraw .dialog}
button .dialog.bbar.apply -text Apply -bg beige
button .dialog.bbar.cancel -text Cancel -bg beige -command {wm withdraw .dialog}

bind .dialog.textent.txt <Return> {.dialog.bbar.apply invoke}

pack .dialog.bbar.okay -side left -ipadx 10
pack .dialog.bbar.apply -side left -ipadx 10
pack .dialog.bbar.cancel -side right -ipadx 10

#--------------------------------------
# Create the query prompt window
#--------------------------------------

toplevel .query -bg beige
wm title .query "Query Dialog Box"
wm group .query .
wm protocol .query WM_DELETE_WINDOW {wm withdraw .query}
wm withdraw .query

frame .query.title -bg beige
frame .query.bbar	-bg beige

pack .query.title -side top -padx 20 -pady 7 -fill x
pack .query.bbar -side bottom -padx 20 -pady 7 -fill x

label .query.title.field -text "Select:" -bg beige
pack .query.title.field -side left

button .query.bbar.okay -text Okay -bg beige
button .query.bbar.cancel -text Cancel -bg beige -command {wm withdraw .query}

pack .query.bbar.okay -side left -ipadx 10
pack .query.bbar.cancel -side right -ipadx 10

#--------------------------------------------------------
# Generate all of the menu cascades
# Most commands reference XCircuit internal routines
#--------------------------------------------------------

# Supporting procedures

proc xcircuit::printstring {stringlist} {
   set p ""
   foreach i $stringlist {
      switch -- [lindex $i 0] {
	 Text {append p [lindex $i 1]}
	 Half -
	 Quarter {append p " "} 
      }
   }
   return $p
}

proc xcircuit::printjust {justif} {
   switch [expr {$justif & 3}] {
      0 {set p "left"}
      1 {set p "center"}
      3 {set p "right"}
   }
   switch [expr {$justif & 12}] {
      0 {append p " bottom"}
      4 {append p " middle"}
      12 {append p " top"}
   }
   return $p
}

proc xcircuit::labelmakeparam {} {
   global XCOps
   if {[xcircuit::select] > 0} {	;# this should be true. . .
      set XCOps(dialog) paramname
      xcircuit::removedialogline textent2	;# default is the selected text
      .dialog.bbar.apply configure -command \
	 [subst {xcircuit::parameter make substring \[.dialog.textent.txt get\];\
	 xcircuit::updateparams substring}]
      .dialog.textent.title.field configure -text "Parameter name:"
   }
   .dialog.textent.txt delete 0 end
   xcircuit::popupdialog
}

proc xcircuit::promptmakeparam {{mode substring}} {
   global XCOps

   set XCOps(dialog) paramdefault
   if {$mode == "label"} {set mode substring}
   xcircuit::makedialogline textent2 "Default value:"
   .dialog.bbar.apply configure -command \
	 [subst {xcircuit::parameter make $mode \
	 \[.dialog.textent.txt get\] \[.dialog.textent2.txt get\] -forward; \
	 xcircuit::removedialogline textent2; \
	 xcircuit::updateparams $mode}]
   .dialog.textent.title.field configure -text \
		"Parameter name:"
   .dialog.textent.txt delete 0 end
   xcircuit::popupdialog
}

#----------------------------------------------------------------------
# This procedure generates a new index number for list selection
# inside a parameter, using "regsub" and "subst" to replace the
# old index with the new one.  This procedure depends on the
# existance of the listbox widget ".paramlist.plist".
#----------------------------------------------------------------------

proc xcircuit::renewparam {key y args} {
   set newidx [.parameter.parvals nearest $y]
   set current [join [xcircuit::parameter get $key ${args} -verbatim]]
   regsub {(.*lindex +{.*} +)([0-9]+)(.*)} $current {\1$newidx\3} tmpkey
   set newkey [subst -nocommands -nobackslashes "$tmpkey"]
   xcircuit::parameter set $key $newkey $args
}

#----------------------------------------------------------------------
# Prompt for a new value of a parameter.  Do some sophisticated checking
# for parameters that declare a list of possible options, and handle
# that situation separately.
#----------------------------------------------------------------------

proc xcircuit::changeparamvalue {key current args} {

  .parameter.valedit.entry delete 0 end
  if {[xcircuit::parameter type $key -forward] == "expression"} {

     # Use regexp processing to find if there is some part of the expression
     # that chooses a single fixed value from a list.  If so, generate a
     # listbox to present the choices in the list.

     set loccurnt [join [xcircuit::parameter get $key ${args} -verbatim]]
     if {[regexp {.*lindex +{(.*)} +[0-9]+.*} $loccurnt qall sellist] > 0} {
        .parameter.parvals delete 0 end
        raise .parameter.parvals

	foreach item $sellist {
	   .parameter.parvals insert end $item
	}

	#Abort the parameter value selection
	bind .parameter.parvals <ButtonRelease-3> {raise .parameter.vallist}

	bind .parameter.parvals <ButtonRelease-1> [subst {xcircuit::renewparam \
	  $key %y $args ;\
	  .parameter.keylist configure -state normal ;\
	  xcircuit::updateparams ;\
	  raise .parameter.vallist}]

	.parameter.valedit.entry delete 0 end
	.parameter.valedit.entry insert 0 $loccurnt

     } else {
	# If the parameter is an expression but not a choice-list type, then
	# we had better print the verbatim entry, or people will just get
	# confused when the value becomes "invalid result:..."
        .parameter.valedit.entry insert 0 $loccurnt
     }
  } else {
     .parameter.valedit.entry insert 0 $current
  }
  focus .parameter.valedit.entry
  .parameter.valedit.apply configure \
	-command [subst {xcircuit::parameter set \
	$key \[.parameter.valedit.entry get\] \
	${args}; xcircuit::updateparams}]
}

#----------------------------------------------------------------------

proc xcircuit::updateparams { {mode {substring numeric expression}} } {

   if {[catch {wm state .parameter}]} {return}

   # Avoid infinite recursion if a parameter invokes updateparams
   # (e.g., any "page" command in a parameter will do this!)

   if {$mode != "force"} {
      if {[info level] > 1} {return}
   } else {
      set mode {substring numeric expression}
   }

   while {[.parameter.keylist size] > 0} {.parameter.keylist delete 0}
   while {[.parameter.vallist size] > 0} {.parameter.vallist delete 0}

   .parameter.delete.deleteparam delete 0 last

   if {$mode == "none"} {
      set dlist [xcircuit::parameter get -forward]
   } else {
      set dlist {}
      foreach i $mode {
         set dlist [concat $dlist [parameter get $i -forward]]
      }
   }

   # Ensure that the parvals list is not present
   if {[select] == 0} { lower .parameter.parvals }

   # All selections will be lost, so make sure that the value field is clear
   .parameter.valedit.entry delete 0 end
   .parameter.valedit.apply configure -command {}

   bind .parameter.vallist <ButtonRelease-1> {
		set kidx [.parameter.keylist nearest %y]; \
		xcircuit::changeparamvalue \
		[.parameter.keylist get $kidx] [.parameter.vallist get $kidx] \
		-forward}

   ;#The insertion of parameters should only be applicable in "text" mode
   bind .parameter.keylist <ButtonRelease-1> {
		set kidx [.parameter.keylist nearest %y]; \
		if {[string last "text" [xcircuit::eventmode]] >= 0} {
		   label insert parameter [.parameter.keylist get $kidx]
		}
	    }

   if {[catch {set oname [xcircuit::object name]}]} {
      .parameter.title configure -text "Parameters"
   } else {
      .parameter.title configure -text "Parameters of $oname"
   }
   foreach i $dlist {
      set p_name [lindex $i 0]
      set p_val [lindex $i 1]
      .parameter.delete.deleteparam add command -label $p_name -command \
	 "xcircuit::parameter delete $p_name -forward"
      .parameter.keylist insert end $p_name
      switch -- [xcircuit::parameter type $p_name -forward] {
	 "substring" {
	    .parameter.vallist insert end [xcircuit::printstring $p_val]
	 }
	 "justification" {
	    .parameter.vallist insert end [xcircuit::printjust $p_val]
	 }
	 default {
	    .parameter.vallist insert end $p_val
         }
      }
   }
}

#----------------------------------------------------------------------

proc xcircuit::prompteditparams {} {
   set wstate [xcircuit::getinitstate .parameter]
   xcircuit::make_parameter_listbox
   xcircuit::updateparams force
   if {"$wstate" != "normal"} {
      wm deiconify .parameter
      xcircuit::centerwin .parameter
   }
   raise .parameter
}

#----------------------------------------------------------------------

proc xcircuit::promptmakesymbol {{name ""}} {
  global XCOps
  
  set XCOps(dialog) makeobject
  .dialog.bbar.apply configure -command \
	  {if {[string first "Page " [page label]] >= 0} { \
	  page label [.dialog.textent.txt get]}; \
	  xcircuit::symbol make [.dialog.textent.txt get] $XCOps(library)}
  xcircuit::removedialogline textent2
  .dialog.textent.title.field configure -text "Name for new object:"
  .dialog.textent.txt delete 0 end
  .dialog.textent.txt insert 0 $name
  xcircuit::popupdialog
  xcircuit::addliblist .dialog "Place in: "
}

#----------------------------------------------------------------------

proc xcircuit::prompttargettech {{name ""}} {
  global XCOps
  
  set XCOps(dialog) targettech
  .dialog.bbar.apply configure -command { \
	  set selects [xcircuit::select]; \
	  if {$selects > 0} { \
	    if {[catch {set techname [.dialog.textent2.txt get]}]} {\
		set techname $XCOps(technology)}; \
	    technology objects $techname [.dialog.textent.txt get]}\
	  } 
  xcircuit::removedialogline textent2
  .dialog.textent.title.field configure -text "Objects to move:"
  .dialog.textent.txt delete 0 end
  .dialog.textent.txt insert 0 $name
  xcircuit::popupdialog
  xcircuit::addtechlist .dialog "Target technology: "

  # Add an additional selection to the tech menu for adding a new
  # technology namespace.  This is relevant only to "prompttargettech".

  .dialog.techself.techselect.menu add \
	command -label "Add New Tech" -command \
	"xcircuit::makedialogline textent2 {New tech name:}"
}

#----------------------------------------------------------------------

proc xcircuit::promptelementsize {} {
   global XCOps
   if {![catch {set cscale [xcircuit::element scale]}]} {
      set XCOps(dialog) elementscale
      .dialog.bbar.apply configure -command \
	  {xcircuit::element scale [.dialog.textent.txt get]}
      .dialog.textent.title.field configure -text "Element scale:"
      .dialog.textent.txt delete 0 end
      .dialog.textent.txt insert 0 $cscale
      xcircuit::popupdialog
   }
}

#----------------------------------------------------------------------

proc xcircuit::promptborderwidth {} {
   global XCOps
   .dialog.textent.txt delete 0 end
   set XCOps(dialog) linewidth
   set elist [xcircuit::select get]
   if {[llength $elist] == 0} {
      .dialog.bbar.apply configure -command \
	   [subst {config focus [config focus] ;\
	    xcircuit::border set \[.dialog.textent.txt get\]}]
      .dialog.textent.title.field configure -text "Default linewidth scale:"
      set btext [format "%g" [xcircuit::border get]]
      .dialog.textent.txt insert 0 $btext
   } else {
      .dialog.bbar.apply configure -command \
	   [subst {config focus [config focus] ;\
	   xcircuit::border set \[.dialog.textent.txt get \]}]
      .dialog.textent.title.field configure -text "Element linewidth:"
      set btext [format "%g" [lindex [xcircuit::border get] 0]]
      .dialog.textent.txt insert 0 $btext
   }
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

proc xcircuit::promptlinewidth {} {
   global XCOps
   set XCOps(dialog) linescale
   .dialog.bbar.apply configure -command \
	[subst {config focus [config focus] ;\
	xcircuit::config linewidth \[.dialog.textent.txt get \]}]
   .dialog.textent.title.field configure -text "Page linewidth scaling:"
   .dialog.textent.txt delete 0 end
   set ltext [format "%g" [xcircuit::config linewidth]]
   .dialog.textent.txt insert 0 $ltext
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

proc xcircuit::promptdrawingscale {} {
   global XCOps
   set XCOps(dialog) drawingscale
   .dialog.bbar.apply configure -command \
	{xcircuit::config drawingscale [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Drawing scale:"
   .dialog.textent.txt delete 0 end
   .dialog.textent.txt insert 0 [xcircuit::config drawingscale]
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

proc xcircuit::promptgridspace {} {
   .dialog.bbar.apply configure -command \
	{xcircuit::config grid spacing [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Grid spacing:"
   .dialog.textent.txt delete 0 end
   .dialog.textent.txt insert 0 [xcircuit::config grid space]
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

proc xcircuit::promptsnapspace {} {
   global XCOps
   set XCOps(dialog) snapspace
   .dialog.bbar.apply configure -command \
	{xcircuit::config snap spacing [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Snap spacing:"
   .dialog.textent.txt delete 0 end
   .dialog.textent.txt insert 0 [xcircuit::config snap space]
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

proc xcircuit::promptmakeobject {} {
   global XCOps
   if {[xcircuit::select] > 0} {
      set XCOps(dialog) makeobject
      .dialog.bbar.apply configure -command \
	   {if {[select get] != {}} {\
	   if {[.dialog.textent.txt get] == ""} {\
	   .dialog.textent.title.field configure -text "Please enter a name" }\
	   elseif {[catch {xcircuit::object handle [.dialog.textent.txt get]}]} {\
	   xcircuit::object make [.dialog.textent.txt get] $XCOps(library)}\
	   else {.dialog.textent.title.field configure -text \
	   "Name already used.  Choose another name" ; .dialog.textent.txt \
	   delete 0 end}}}
      .dialog.textent.title.field configure -text "Name for new object"
      .dialog.textent.txt delete 0 end
      if {$XCOps(technology) != "(user)"} {
         .dialog.textent.txt insert 0 "${XCOps(technology)}::"
      }
      xcircuit::popupdialog
      xcircuit::addtechlist .dialog "Technology: " {(user)} prefix
      xcircuit::addliblist .dialog "Place in: "

   }
}

#----------------------------------------------------------------------

proc xcircuit::promptreplaceobject {} {
   global XCOps
   if {[xcircuit::select] > 0} {
      set XCOps(dialog) replaceobject
      .dialog.bbar.apply configure -command \
	  {xcircuit::element selected object [.dialog.textent.txt get]}
      .dialog.textent.title.field configure -text "Name of replacement object"
      .dialog.textent.txt delete 0 end
      xcircuit::popupdialog
   }
}

#----------------------------------------------------------------------

proc xcircuit::promptloadlibrary {} {
   global XCOps

   .filelist.bbar.okay configure -command \
	{xcircuit::library [.filelist.libself.libselect cget -text] load \
	[.filelist.textent.txt get]; wm withdraw .filelist}
   .filelist.listwin.win configure -data "lps"
   .filelist.textent.title.field configure -text "Select technology file to load:"
   .filelist.textent.txt delete 0 end
   xcircuit::popupfilelist
   xcircuit::addliblist .filelist "Load to which library page: "
}

#----------------------------------------------------------------------

proc xcircuit::promptsavetech {} {
   global XCOps

   set XCOps(dialog) techname
   .dialog.bbar.apply configure -command \
	{xcircuit::technology save [.dialog.techself.techselect cget -text] \
	 [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Filename to save technology as:"
   .dialog.textent.txt delete 0 end
   xcircuit::popupdialog
   xcircuit::addtechlist .dialog "Save which technology: " {(user)} true
   set fname ""
   catch {set fname [technology filename $XCOps(technology)]}
   if {$fname == "(no associated file)"} {
      set fname $XCOps(technology).lps
   }
   .dialog.textent.txt insert 0 $fname
}

#----------------------------------------------------------------------

proc xcircuit::promptaddlibrary {} {
   global XCOps
   set XCOps(dialog) libname
   .dialog.bbar.apply configure -command \
	{xcircuit::library make [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Name of new library page:"
   .dialog.textent.txt delete 0 end
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

proc xcircuit::promptloadfile {} {
   .filelist.bbar.okay configure -command \
	{xcircuit::page load [.filelist.textent.txt get] -replace \
	[.filelist.techself.techselect cget -text] \
	-target $XCOps(library); wm withdraw .filelist}
   .filelist.listwin.win configure -data "ps eps"
   .filelist.textent.title.field configure -text "Select file to load:"
   .filelist.textent.txt delete 0 end
   xcircuit::popupfilelist
   xcircuit::addtechlist .filelist "Replace from: " {(user) all none}
   xcircuit::addliblist .filelist "Target library: "
}

#----------------------------------------------------------------------

proc xcircuit::promptimportspice {} {
   .filelist.bbar.okay configure -command \
	{xcircuit::page import spice \
	[.filelist.textent.txt get]; wm withdraw .filelist}
   .filelist.listwin.win configure -data "spice spc ckt sp"
   .filelist.textent.title.field configure -text "Select SPICE file to import:"
   .filelist.textent.txt delete 0 end
   xcircuit::popupfilelist
}

#----------------------------------------------------------------------

proc xcircuit::promptimportfile {} {
   .filelist.bbar.okay configure -command \
	{xcircuit::page import xcircuit \
	[.filelist.textent.txt get]; wm withdraw .filelist}
   .filelist.listwin.win configure -data "ps eps"
   .filelist.textent.title.field configure -text "Select file to import:"
   xcircuit::popupfilelist
}

#----------------------------------------------------------------------

proc xcircuit::promptimportbackground {} {
   .filelist.bbar.okay configure -command \
	{xcircuit::page import background \
	[.filelist.textent.txt get]; wm withdraw .filelist}
   .filelist.listwin.win configure -data "ps eps"
   .filelist.textent.title.field configure -text "Select file to use as background:"
   .filelist.textent.txt delete 0 end
   xcircuit::popupfilelist
}

#----------------------------------------------------------------------
# Convert a graphic image using ImageMagick "convert" (if available)
#----------------------------------------------------------------------

proc xcircuit::convertgraphic {filename} {
   set fileext [file extension $filename]
   set fileroot [file rootname $filename]
   set temp true
   switch -- $fileext {
      .gif -
      .jpg -
      .png -
      .pnm {
	 exec convert $filename ${fileroot}.ppm
      }
      .ppm {
	 set temp false
      }
      .ps -
      .pdf {
	 exec convert -density 300x300 $filename ${fileroot}.ppm
      }
   }
   xcircuit::graphic make ${fileroot}.ppm {0 0} 1;
   if {$temp == true} {
      file delete ${fileroot}.ppm
   }
}

#----------------------------------------------------------------------

proc xcircuit::promptimportgraphic {} {
   if {![catch {exec convert -version}]} {
      .filelist.bbar.okay configure -command \
		{xcircuit::convertgraphic [.filelist.textent.txt get];
	 	refresh; wm withdraw .filelist}
      .filelist.listwin.win configure -data "pnm ppm gif jpg png"
   } else {
      .filelist.bbar.okay configure -command \
		{xcircuit::graphic make [.filelist.textent.txt get] {0 0} 1;
	 	refresh; wm withdraw .filelist}
      .filelist.listwin.win configure -data "pnm ppm"
   }
   .filelist.textent.title.field configure -text "Select graphic image file:"
   .filelist.textent.txt delete 0 end
   xcircuit::popupfilelist
}

#----------------------------------------------------------------------

proc xcircuit::promptexecscript {} {
   .filelist.bbar.okay configure -command \
	{source [.filelist.textent.txt get]; wm withdraw .filelist}
   .filelist.listwin.win configure -data "tcl xcircuitrc"
   .filelist.textent.title.field configure -text "Select script to execute:"
   xcircuit::popupfilelist
}

#----------------------------------------------------------------------

proc xcircuit::prompttextsize {} {
   global XCOps
   set XCOps(dialog) textscale
   .dialog.bbar.apply configure -command \
	{xcircuit::label scale [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Text scale:"
   .dialog.textent.txt delete 0 end
   set stext [format "%g" [xcircuit::label scale]]
   .dialog.textent.txt insert 0 $stext
   xcircuit::popupdialog
}

#----------------------------------------------------------------------
# add a library list widget to a dialog box
#----------------------------------------------------------------------

proc xcircuit::addliblist {w {prompt "Target: "}} {
   global XCOps

   frame ${w}.libself
   label ${w}.libself.title -text $prompt -bg beige

   set liblist [library directory list]

   menubutton ${w}.libself.libselect -menu ${w}.libself.libselect.menu -relief groove
   menu ${w}.libself.libselect.menu -tearoff 0
   foreach j $liblist {
      ${w}.libself.libselect.menu add \
	radio -label "$j" -variable XCOps(library) -value \
	"$j" -command "${w}.libself.libselect configure -text {$j}"
   }
   ${w}.libself.libselect configure -text $XCOps(library)

   pack ${w}.libself.title -side left
   pack ${w}.libself.libselect -side left
   pack ${w}.libself -side top -anchor w -padx 20
}

#----------------------------------------------------------------------
# Add a technology list widget to a dialog box
#
# If "update" is "true", then a selection of a technology in the list
# will update the text entry window contents with the corresponding
# filename.  If "update" is "prefix", then a selection of a technology
# in the list will update the text entry window contents with the
# technology namespace prefix corresponding to the selected technology.
# If "update" is "false", then selecting the technology will set the
# global variable XCOps(technology) but will not alter the window
# contents.
#----------------------------------------------------------------------

proc xcircuit::addtechlist {w {prompt "Technology: "} {endlist {}} {update {false}}} {
   global XCOps

   frame ${w}.techself
   label ${w}.techself.title -text $prompt -bg beige

   set techlist [technology list]
   foreach j $endlist { lappend techlist $j }

   menubutton ${w}.techself.techselect -menu ${w}.techself.techselect.menu -relief groove
   menu ${w}.techself.techselect.menu -tearoff 0
   foreach j $techlist {
      if {$update == true} {
	 set fname ""
	 catch {set fname [technology filename "$j"]}
	 
         ${w}.techself.techselect.menu add \
	    radio -label "$j" -variable XCOps(technology) -value \
	    "$j" -command "${w}.techself.techselect configure -text {$j} ; \
		${w}.textent.txt delete 0 end ; ${w}.textent.txt insert 0 {$fname}"
      } elseif {$update == "prefix"} {
	 if {$j != {} && $j != "(user)"} {
	    set tpfix "${j}::"
	 } else {
	    set tpfix {}
	 }
         ${w}.techself.techselect.menu add \
	    radio -label "$j" -variable XCOps(technology) -value \
	    "$j" -command [subst {${w}.techself.techselect configure -text {$j} ; \
		set pfixend \[string last :: \[${w}.textent.txt get\]\] ; \
		if {\$pfixend < 0} {set pfixend 0} else {incr pfixend 2} ; \
		${w}.textent.txt delete 0 \$pfixend ; \
		${w}.textent.txt insert 0 {$tpfix}}]
      } else {
         ${w}.techself.techselect.menu add \
	    radio -label "$j" -variable XCOps(technology) -value \
	    "$j" -command "${w}.techself.techselect configure -text {$j}"
      }
   }
   ${w}.techself.techselect configure -text $XCOps(technology)

   pack ${w}.techself.title -side left
   pack ${w}.techself.techselect -side left
   pack ${w}.techself -side right -anchor w -padx 20
}

#----------------------------------------------------------------------

proc xcircuit::removelists {w} {
   global XCOps

   catch {
      pack forget ${w}.libself
      destroy ${w}.libself
   }
   catch {
      pack forget ${w}.techself
      destroy ${w}.techself
   }
}

#----------------------------------------------------------------------
# newcolorbutton is called internally to xcircuit---don't mess with it!
#----------------------------------------------------------------------

proc xcircuit::newcolorbutton {r g b idx} {
   global XCWinOps XCIRCUIT_LIB_DIR

   set colorrgb [format "#%04X%04X%04X" $r $g $b]
   image create bitmap img_col$idx -foreground $colorrgb -file \
		${XCIRCUIT_LIB_DIR}/pixmaps/solid.xbm

   foreach window [config windownames] {
      set frame [winfo top $window]
      ${frame}.menubar.optionsbutton.optionsmenu.elementsmenu.colormenu \
		add radio -image img_col$idx -activebackground $colorrgb \
		-variable XCWinOps(${frame},colorval) -value $idx -command \
		"xcircuit::color set $idx"
   }
}

#----------------------------------------------------------------------
# Regenerate the list of color buttons for a new window
#----------------------------------------------------------------------

proc xcircuit::allcolorbuttons {window} {
   global XCWinOps

   set colorlist [color get -all]
   set frame [winfo top $window]
   set idx 0
   foreach colorrgb $colorlist {
      ${frame}.menubar.optionsbutton.optionsmenu.elementsmenu.colormenu \
		add radio -image img_col$idx -activebackground $colorrgb \
		-variable XCWinOps(${frame},colorval) -value $idx -command \
		"xcircuit::color set $idx"
      incr idx
   }
}

#----------------------------------------------------------------------

proc xcircuit::picknewcolor {} {
   if {[catch {set colorrgb [tk_chooseColor]}]} {
      set colorrgb [tkColorDialog]
   }
   xcircuit::color add $colorrgb
}

#----------------------------------------------------------------------

proc xcircuit::newencodingbutton {encodingname} {
   global XCWinOps

   foreach window [config windownames] {
      set frame [winfo top $window]
      if {[catch {${frame}.menubar.textbutton.textmenu.encodingmenu \
		index $encodingname} result]} {
         ${frame}.menubar.textbutton.textmenu.encodingmenu add radio -label \
	  	$encodingname -command "xcircuit::label encoding $encodingname" \
		-variable XCWinOps(${frame},fontencoding) -value $encodingname
      }
   }
}

#----------------------------------------------------------------------

proc xcircuit::newfontbutton {familyname} {
   global XCWinOps

   foreach window [config windownames] {
      set frame [winfo top $window]
      if {[catch {${frame}.menubar.textbutton.textmenu.fontmenu \
		index $familyname} result]} {
         ${frame}.menubar.textbutton.textmenu.fontmenu add radio -label \
	  	$familyname -command "xcircuit::label family $familyname" \
		-variable XCWinOps(${frame},fontfamily) -value $familyname
      }
   }
}

#----------------------------------------------------------------------
# Regenerate the list of known font families for a new window
#----------------------------------------------------------------------

proc xcircuit::allfontbuttons {window} {
   global XCWinOps

   set familylist [label family -all]
   set frame [winfo top $window]
   while {$familylist != {}} {
      set familyname [lindex $familylist 0]
      ${frame}.menubar.textbutton.textmenu.fontmenu add radio -label \
	  	$familyname -command "xcircuit::label family $familyname" \
		-variable XCWinOps(${frame},fontfamily) -value $familyname
      # Remove all such entries (this works like "lsort -unique" but doesn't
      # scramble the list entries).
      set familylist [lsearch -all -inline -not $familylist $familyname]
   }
}

#----------------------------------------------------------------------

proc xcircuit::newlibrarybutton {libname} {
   if {[catch {.librarymenu index $libname} result]} {
      set libidx [.librarymenu index end]
      if {$libidx <= 1} {set libidx [expr $libidx + 1]}
      .librarymenu insert $libidx command -label $libname -command \
		"xcircuit::library \"$libname\" goto"
   }
}

#----------------------------------------------------------------------

proc xcircuit::newpagebutton {pagename {pageno 0}} {
   if {[catch {.pagemenu index $pagename} result]} {
      set target $pagename
      if {$pageno > 0} { set target $pageno }
      .pagemenu add command -label $pagename -command \
		"xcircuit::page \"$target\" goto"
   }
}

#----------------------------------------------------------------------

proc xcircuit::renamelib {libno libname} {
   set target [expr $libno + 1]
   .librarymenu entryconfigure $target -label $libname -command \
		"xcircuit::library \"$libname\" goto"
}

#----------------------------------------------------------------------

proc xcircuit::renamepage {pageno pagename} {
   set target [expr $pageno + 1]
   .pagemenu entryconfigure $target -label $pagename -command \
		"xcircuit::page \"$pagename\" goto"
}

#----------------------------------------------------------------------

proc xcircuit::promptnewfont {} {
   global XCOps
   set XCOps(dialog) fontname
   .dialog.bbar.apply configure -command \
	{xcircuit::loadfont [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Font name:"
   .dialog.textent.txt delete 0 end
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

proc xcircuit::promptkern {} {
   global XCOps
   set XCOps(dialog) kernamount
   .dialog.bbar.apply configure -command \
	{xcircuit::label insert kern [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Kern amount:"
   .dialog.textent.txt delete 0 end
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

proc xcircuit::promptmargin {} {
   global XCOps
   set XCOps(dialog) marginamount
   .dialog.bbar.apply configure -command \
	{xcircuit::label insert margin [.dialog.textent.txt get]}
   .dialog.textent.title.field configure -text "Margin amount:"
   .dialog.textent.txt delete 0 end
   xcircuit::popupdialog
}

#----------------------------------------------------------------------

set XCOps(tools) [list pn w b a s t mv cp e d2 cw ccw fx fy r pu2 po2 mk pz \
	uj co bd fi pm pa li yp pl z4 z5 i]

for {set i 0} {$i < [llength $XCOps(tools)]} {incr i 1} {
   set bname [lindex $XCOps(tools) $i]
   image create photo img_${bname} -file ${XCIRCUIT_LIB_DIR}/pixmaps/${bname}.gif
}

#----------------------------------------------------------------------

proc xcircuit::modebutton {widget} {
   global XCOps XCWinOps
   set window $XCOps(focus)

   for {set i 0} {$i < [llength $XCOps(tools)]} {incr i 1} {
      set bname [lindex $XCOps(tools) $i]
      ${window}.mainframe.toolbar.b${bname} configure -relief raised \
		-highlightbackground gray90 -background gray90
   }
   $widget configure -relief solid -highlightbackground green3 -background green3
   if {$XCWinOps(${window},button1) == "Rotate"} {
      ${window}.infobar.mode configure -text "$XCWinOps(${window},button1) \
		$XCWinOps(${window},rotateamount) Mode"
   } else {
      ${window}.infobar.mode configure -text "$XCWinOps(${window},button1) Mode"
   }
}

#----------------------------------------------------------------------

proc xcircuit::button1action {button cursor action {value {}}} {
   global XCOps XCWinOps

   set window $XCOps(focus).mainframe.mainarea.drawing
   catch {bindkey $window Button1 $XCWinOps($XCOps(focus),button1) forget}

   if {$value == {}} {
      bindkey $window Button1 $action
   } else {
      bindkey $window Button1 $action $value
   }
   set XCWinOps($XCOps(focus),button1) $action
   xcircuit::modebutton $button
   catch {cursor $cursor}
   catch {xcircuit::automousehint $window}
}

#----------------------------------------------------------------------

proc xcircuit::createtoolbar {window} {
   global XCOps XCWinOps XCIRCUIT_LIB_DIR

   set tooltips [list "pan window" "draw wire" "draw box" \
	"draw arc" "draw spline" "enter text" \
	"move element" "copy element" "edit element" "delete element" \
	"rotate 15 degrees clockwise" "rotate 15 degrees counterclockwise" \
	"flip horizontal" "flip vertical" "rescale" "push (edit object)" \
	"pop (return from object edit)" "make an object from selection" \
	"join elements into polygon or path" "separate path into elements" \
	"set color" "set border and line properties" "set fill properties" \
	"parameterize properties" "parameter selection" \
	"go to next library" "go to library directory" \
	"go to page directory" "zoom in" "zoom out" "pop up help window"]	
   set toolactions [list \
	{xcircuit::button1action %W hand Pan 6} \
	{xcircuit::button1action %W cross Wire} \
	{xcircuit::button1action %W cross Box} \
	{xcircuit::button1action %W circle Arc} \
	{if {[select]} {spline make} else { \
		xcircuit::button1action %W arrow Spline}} \
	{xcircuit::button1action %W text $XCWinOps($XCOps(focus),labeltype) ; \
		$XCOps(focus).mainframe.toolbar.bt configure -image \
		$XCWinOps($XCOps(focus),labelimage)} \
	{if {[select]} {move selected} else { \
		xcircuit::button1action %W arrow Move}} \
	{if {[select]} {copy selected} else { \
		xcircuit::button1action %W copy Copy}} \
	{if {[select] == 1} {edit selected} else { \
		xcircuit::button1action %W edit Edit}} \
	{if {[select]} {delete selected} else { \
		xcircuit::button1action %W scissors Delete}} \
	{if {[select]} {rotate $XCWinOps($XCOps(focus),rotateamount)} else {\
		xcircuit::button1action %W rotate Rotate \
		$XCWinOps($XCOps(focus),rotateamount)}} \
	{if {[select]} {rotate -$XCWinOps($XCOps(focus),rotateamount)} else {\
		xcircuit::button1action %W rotate Rotate \
		-$XCWinOps($XCOps(focus),rotateamount)}} \
	{if {[select]} {flip horizontal} else { \
		xcircuit::button1action %W rotate "Flip X"}} \
	{if {[select]} {flip vertical} else { \
		xcircuit::button1action %W rotate "Flip Y"}} \
	{if {[select]} {xcircuit::promptelementsize} else { \
		xcircuit::button1action %W arrow Rescale}} \
	{if {[select] == 1} {push selected} else { \
		xcircuit::button1action %W question Push}} \
	{pop} \
	{if {[select]} {xcircuit::promptmakeobject} else { \
		xcircuit::button1action %W question "Select Save"}} \
	{if {[select]} {path join selected} else { \
		xcircuit::button1action %W arrow Join}} \
	{if {[select] == 1} {path unjoin selected} else { \
		xcircuit::button1action %W arrow Unjoin}} \
	{tk_popup $XCOps(focus).colormenu [expr {[winfo rootx \
		$XCOps(focus).mainframe.toolbar.bco] \
		- [winfo width $XCOps(focus).colormenu]}] \
		[expr {[winfo rooty $XCOps(focus).mainframe.toolbar.bco] \
		- [winfo height $XCOps(focus).colormenu] / 2}] } \
	{tk_popup $XCOps(focus).bordermenu [expr {[winfo rootx \
		$XCOps(focus).mainframe.toolbar.bbd] \
		- [winfo width $XCOps(focus).bordermenu]}] \
		[expr {[winfo rooty $XCOps(focus).mainframe.toolbar.bbd] \
		- [winfo height $XCOps(focus).bordermenu] / 2}] } \
	{tk_popup $XCOps(focus).fillmenu [expr {[winfo rootx \
		$XCOps(focus).mainframe.toolbar.bfi] \
		- [winfo width $XCOps(focus).fillmenu]}] \
		[expr {[winfo rooty $XCOps(focus).mainframe.toolbar.bfi] \
		- [winfo height $XCOps(focus).fillmenu] / 2}] } \
	{tk_popup $XCOps(focus).parammenu [expr {[winfo rootx \
		$XCOps(focus).mainframe.toolbar.bpm] \
		- [winfo width $XCOps(focus).parammenu]}] \
		[expr {[winfo rooty $XCOps(focus).mainframe.toolbar.bpm] \
		- [winfo height $XCOps(focus).parammenu] / 2}] } \
	{xcircuit::prompteditparams} \
	{library next} \
	{library directory} {page directory} \
	{zoom 1.5; refresh} {zoom [expr {1 / 1.5}]; refresh} \
	{xcircuit::helpwindow} ]

   for {set i 0} {$i < [llength $XCOps(tools)]} {incr i 1} {
      set bname [lindex $XCOps(tools) $i]
      set btip [lindex $tooltips $i]
      regsub -all -- %W [lindex $toolactions $i] \
		${window}.mainframe.toolbar.b${bname} bcmd
      button ${window}.mainframe.toolbar.b${bname} -image img_${bname} -command \
		"$bcmd"
      bind ${window}.mainframe.toolbar.b${bname} <Enter> \
		[subst {${window}.infobar.message2 configure -text "$btip"}]
      bind ${window}.mainframe.toolbar.b${bname} <Leave> \
		[subst {${window}.infobar.message2 configure -text ""}]
   }

   # pack the first button so we can query its height for arrangement.
   # this assumes that the height of each button is the same!
   set bname [lindex $XCOps(tools) 0]
   place ${window}.mainframe.toolbar.b${bname} -x 0 -y 0
   update idletasks
}

#----------------------------------------------------------------------

proc xcircuit::arrangetoolbar {window} {
   global XCOps

   set numtools [llength $XCOps(tools)]
   for {set i 0} {$i < $numtools} {incr i 1} {
      set bname [lindex $XCOps(tools) $i]
      place forget ${window}.mainframe.toolbar.b${bname}
   }
   set bname [lindex $XCOps(tools) 0]
   set bheight [winfo height ${window}.mainframe.toolbar.b${bname}]
   set bwidth [winfo width ${window}.mainframe.toolbar.b${bname}]
   set wheight [winfo height ${window}.mainframe]
   set nrows [expr {$wheight / $bheight}]
   ${window}.mainframe.toolbar configure -width [expr {$bwidth}]
   set j 0
   set k 0
   for {set i 0} {$i < [llength $XCOps(tools)]} {incr i; incr j} {
      if {$j == $nrows} {
	 set j 0
	 incr k
	 ${window}.mainframe.toolbar configure -width [expr {($k + 1) * $bwidth}]
      }
      set bname [lindex $XCOps(tools) $i]
      place ${window}.mainframe.toolbar.b${bname} \
		-x [expr {$k * $bwidth}] \
		-y [expr {$j * $bheight}]
   }
}

#----------------------------------------------------------------------

proc xcircuit::toolbar {value} {
   global XCOps
   set window $XCOps(focus)

   switch -- $value {
      true -
      enable {
	 pack forget ${window}.mainframe.mainarea
	 pack ${window}.mainframe.toolbar -side right -fill y -padx 2
	 pack ${window}.mainframe.mainarea -expand true -fill both
	 set midx [${window}.menubar.optionsbutton.optionsmenu \
		index "Enable Toolbar"]
	 ${window}.menubar.optionsbutton.optionsmenu entryconfigure $midx \
		-command {xcircuit::toolbar disable} -label \
		"Disable Toolbar"
      }
      false -
      disable {
	 pack forget ${window}.mainframe.toolbar
	 set midx [${window}.menubar.optionsbutton.optionsmenu \
		index "Disable Toolbar"]
	 ${window}.menubar.optionsbutton.optionsmenu entryconfigure $midx \
		-command {xcircuit::toolbar enable} -label \
		"Enable Toolbar"
      }
   }
}

#----------------------------------------------------------------------
# These variables are associated with toggle and radio buttons
# but must be the same for all windows.
#----------------------------------------------------------------------

set XCOps(sheetsize) letter
set XCOps(spiceend)  true
set XCOps(forcenets) true
set XCOps(hold)      true

set XCOps(focus)     .xcircuit

#----------------------------------------------------------------------
# Create stipple images
#----------------------------------------------------------------------

image create bitmap img_stip0 -foreground white -background black -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/solid.xbm
image create bitmap img_stip12 -foreground black -background white -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/stip12.xbm
image create bitmap img_stip25 -foreground black -background white -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/stip25.xbm
image create bitmap img_stip38 -foreground black -background white -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/stip38.xbm
image create bitmap img_stip50 -foreground black -background white -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/stip50.xbm
image create bitmap img_stip62 -foreground black -background white -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/stip62.xbm
image create bitmap img_stip75 -foreground black -background white -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/stip75.xbm
image create bitmap img_stip88 -foreground black -background white -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/stip88.xbm
image create bitmap img_stip100 -foreground black -background white -file \
	${XCIRCUIT_LIB_DIR}/pixmaps/solid.xbm

#----------------------------------------------------------------------
# alternate label type images (info, global, and pin labels)
#----------------------------------------------------------------------

image create photo img_ti -file ${XCIRCUIT_LIB_DIR}/pixmaps/ti.gif
image create photo img_tg -file ${XCIRCUIT_LIB_DIR}/pixmaps/tg.gif
image create photo img_tp -file ${XCIRCUIT_LIB_DIR}/pixmaps/tp.gif
 
#----------------------------------------------------------------------

proc xcircuit::makemenus {window} {
   global XCOps

   set m [menu ${window}.menubar.filebutton.filemenu -tearoff 0]
   $m add command -label "New Window" -command {xcircuit::forkwindow}
   $m add command -label "Close Window" -command \
	"xcircuit::closewindow ${window}.mainframe.mainarea.drawing"
   $m add separator
   $m add command -label "Read XCircuit File" -command {xcircuit::promptloadfile}
   if {![catch {set XCOps(module,files)}]} {
	$m add command -label "Write All..." -command {xcircuit::promptwriteall}
   }
   $m add command -label "Format Page Output" -command {xcircuit::promptsavepage}
   $m add separator
   $m add command -label "Load Dependencies" -command \
	{while {[page links load -replace $XCOps(technology) \
	-target $XCOps(library)]} {}}
   $m add cascade -label "Import" -menu $m.importmenu
   $m add cascade -label "Export" -menu $m.exportmenu

   $m add command -label "Execute Script" -command {xcircuit::promptexecscript}
   if {[file tail [info nameofexecutable]] != "xcircexec"} {
      $m add command -label "Tcl Console" -command {xcircuit::raiseconsole}
   }
   $m add separator
   if {![catch {set XCOps(module,library)}]} {
      $m add command -label "Library Manager" -command {xcircuit::raisemanager}
   }
   $m add command -label "New Library Page" -command {xcircuit::promptaddlibrary}
   $m add command -label "Load Technology (.lps)" -command {xcircuit::promptloadlibrary}
   $m add command -label "Save Technology (.lps)" -command {xcircuit::promptsavetech}

   $m add separator
   $m add command -label "Clear Page" -command {xcircuit::page reset}
   $m add separator
   $m add command -label "Quit" -command {quit}

   # Sub-menu for Import functions

   set m2 [menu $m.importmenu -tearoff 0]
   $m2 add command -label "Import XCircuit File" -command {xcircuit::promptimportfile}
   $m2 add command -label "Import background PS" -command \
	{xcircuit::promptimportbackground}
   $m2 add command -label "Import graphic image" -command \
	{xcircuit::promptimportgraphic}
   if {![catch {set XCOps(module,edif)}]} {
      $m2 add command -label "Read EDIF file" -command {xcircuit::promptreadedif}
   }
   if {![catch {set XCOps(module,matgen)}]} {
      $m2 add command -label "Import Matlab PS" -command {xcircuit::promptimportmatlab}
   }
   if {![catch {set XCIRCUIT_ASG}]} {
      $m2 add command -label "Import SPICE Deck" -command \
	{xcircuit::promptimportspice}
   }
   if {![catch {set XCOps(module,text)}]} {
      $m2 add command -label "Import text" -command {xcircuit::promptimporttext}
   }

   # Sub-menu for Export functions

   set m2 [menu $m.exportmenu -tearoff 0]
   $m2 add command -label "Export SVG" -command {svg -fullscale}

   set m [menu ${window}.menubar.editbutton.editmenu -tearoff 0]
   $m add command -label "Undo" -command {undo}
   $m add command -label "Redo" -command {redo}
   $m add separator
   $m add command -label "Delete" -command \
	"${window}.mainframe.toolbar.bd2 invoke"
   $m add command -label "Copy" -command \
	"${window}.mainframe.toolbar.bcp invoke"
   $m add command -label "Move" -command \
	"${window}.mainframe.toolbar.bmv invoke"
   $m add command -label "Edit" -command \
	"${window}.mainframe.toolbar.be invoke"
   $m add cascade -label "Rotate/Flip" -menu $m.rotmenu
   $m add command -label "Deselect" -command {deselect selected}
   $m add cascade -label "Select Filter" -menu $m.selmenu
   $m add command -label "Push Selected" -command \
	"${window}.mainframe.toolbar.pu2 invoke"
   $m add command -label "Pop Hierarchy" -command {pop}
   $m add separator
   $m add command -label "Change Technology" -command \
	{xcircuit::prompttargettech [element selected object]}
   $m add separator
   $m add command -label "Make User Object" -command \
	"${window}.mainframe.toolbar.bmk invoke"
   $m add command -label "Make Arc" -command \
	"${window}.mainframe.toolbar.ba invoke"
   $m add command -label "Make Box" -command \
	"${window}.mainframe.toolbar.bb invoke"
   $m add command -label "Make Spline" -command \
	"${window}.mainframe.toolbar.bs invoke"
   $m add command -label "Make Wire" -command \
	"${window}.mainframe.toolbar.bw invoke"
   $m add command -label "Replace" -command {xcircuit::promptreplaceobject}
   $m add command -label "Join" -command \
	"${window}.mainframe.toolbar.bpz invoke"
   $m add command -label "Unjoin" -command \
	"${window}.mainframe.toolbar.buj invoke"
   $m add command -label "Raise/Lower" -command {element exchange}

   set m2 [menu $m.rotmenu -tearoff 0]
   $m2 add command -label "Flip Horizontal" -command \
	"${window}.mainframe.toolbar.bfx invoke"
   $m2 add command -label "Flip Vertical" -command \
	"${window}.mainframe.toolbar.bfy invoke"
   $m2 add command -label "Rescale" -command \
	"${window}.mainframe.toolbar.r invoke"
   $m2 add separator
   $m2 add command -label "Rotate CW 90" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 90; \
	$XCOps(focus).mainframe.toolbar.bcw invoke}
   $m2 add command -label "Rotate CW 45" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 45; \
	$XCOps(focus).mainframe.toolbar.bcw invoke}
   $m2 add command -label "Rotate CW 30" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 30; \
	$XCOps(focus).mainframe.toolbar.bcw invoke}
   $m2 add command -label "Rotate CW 15" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 15; \
	$XCOps(focus).mainframe.toolbar.bcw invoke}
   $m2 add command -label "Rotate CW 5" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 5; \
	$XCOps(focus).mainframe.toolbar.bcw invoke}
   $m2 add command -label "Rotate CW 1" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 1; \
	$XCOps(focus).mainframe.toolbar.bcw invoke}
   $m2 add separator
   $m2 add command -label "Rotate CCW 90" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 90; \
	$XCOps(focus).mainframe.toolbar.bccw invoke}
   $m2 add command -label "Rotate CCW 45" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 45; \
	$XCOps(focus).mainframe.toolbar.bccw invoke}
   $m2 add command -label "Rotate CCW 30" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 30; \
	$XCOps(focus).mainframe.toolbar.bccw invoke}
   $m2 add command -label "Rotate CCW 15" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 15; \
	$XCOps(focus).mainframe.toolbar.bccw invoke}
   $m2 add command -label "Rotate CCW 5" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 5; \
	$XCOps(focus).mainframe.toolbar.bccw invoke}
   $m2 add command -label "Rotate CCW 1" -command \
	{set XCWinOps($XCOps(focus),rotateamount) 1; \
	$XCOps(focus).mainframe.toolbar.bccw invoke}

   set m2 [menu $m.selmenu -tearoff 0]
   $m2 add command -label "Disable selection" -command {element select hide}
   $m2 add command -label "Remove all disabled" -command {element select allow}
   $m2 add separator
   $m2 add check -label "Labels" -variable XCWinOps(${window},sel_lab) \
	-onvalue true -offvalue false -command \
	{xcircuit::config filter label $XCWinOps($XCOps(focus),sel_lab)}
   $m2 add check -label "Objects" -variable XCWinOps(${window},sel_inst) \
	-onvalue true -offvalue false -command \
	{xcircuit::config filter instance $XCWinOps($XCOps(focus),sel_inst)}
   $m2 add check -label "Polygons" -variable XCWinOps(${window},sel_poly) \
	-onvalue true -offvalue false -command \
	{xcircuit::config filter polygon $XCWinOps($XCOps(focus),sel_poly)}
   $m2 add check -label "Arcs" -variable XCWinOps(${window},sel_arc) \
	-onvalue true -offvalue false -command \
	{xcircuit::config filter arc $XCWinOps($XCOps(focus),sel_arc)}
   $m2 add check -label "Splines" -variable XCWinOps(${window},sel_spline) \
	-onvalue true -offvalue false -command \
	{xcircuit::config filter spline $XCWinOps($XCOps(focus),sel_spline)}
   $m2 add check -label "Paths" -variable XCWinOps(${window},sel_path) \
	-onvalue true -offvalue false -command \
	{xcircuit::config filter path $XCWinOps($XCOps(focus),sel_path)}
   $m2 add check -label "Graphic Images" -variable XCWinOps(${window},sel_graphic) \
	-onvalue true -offvalue false -command \
	{xcircuit::config filter graphic $XCWinOps($XCOps(focus),sel_graphic)}

   set m [menu ${window}.menubar.textbutton.textmenu -tearoff 0]
   $m add command -label "Text Size" -command {xcircuit::prompttextsize}
   $m add cascade -label "Font" -menu $m.fontmenu
   $m add cascade -label "Style" -menu $m.stylemenu
   $m add cascade -label "Encoding" -menu $m.encodingmenu
   $m add cascade -label "Insert" -menu $m.insertmenu
   $m add cascade -label "Justification" -menu $m.justifymenu
   $m add command -label "Parameterize" \
	-command {xcircuit::labelmakeparam}
   $m add command -label "Unparameterize" \
	-command {xcircuit::parameter replace substring}
   $m add separator
   $m add check -label "LaTeX mode" -variable XCWinOps(${window},latexmode) \
	-onvalue true -offvalue false -command {xcircuit::label latex \
	$XCWinOps($XCOps(focus),latexmode)}
   if {![catch {set XCOps(module,text)}]} {
      $m add separator
      $m add command -label "Modify Text..." -command {xcircuit::textmod}
   }
   $m add separator
   $m add command -label "Make Text" -command \
	{set XCWinOps($XCOps(focus),labeltype) "Text"; set \
	XCWinOps($XCOps(focus),labelimage) img_t; \
	$XCOps(focus).mainframe.toolbar.bt invoke}

   set m2 [menu $m.fontmenu -tearoff 0]
   $m2 add command -label "Add New Font" -command {xcircuit::promptnewfont}
   $m2 add separator

   set m2 [menu $m.stylemenu -tearoff 0]
   $m2 add radio -label "Normal" -variable XCWinOps(${window},fontstyle) \
	-value normal -command "xcircuit::label style normal"
   $m2 add radio -label "Bold" -variable XCWinOps(${window},fontstyle) \
	-value bold -command "xcircuit::label style bold"
   $m2 add radio -label "Italic" -variable XCWinOps(${window},fontstyle) \
	-value italic -command "xcircuit::label style italic"
   $m2 add radio -label "BoldItalic" -variable XCWinOps(${window},fontstyle) \
	-value bolditalic -command "xcircuit::label style bolditalic"
   $m2 add separator
   $m2 add radio -label "Subscript" -variable XCWinOps(${window},fontscript) \
	-value subscript -command "xcircuit::label insert subscript"
   $m2 add radio -label "Superscript" -variable XCWinOps(${window},fontscript) \
	-value superscript -command "xcircuit::label insert superscript"
   $m2 add radio -label "Normalscript" -variable XCWinOps(${window},fontscript) \
	-value normal -command "xcircuit::label insert normalscript"
   $m2 add separator
   $m2 add radio -label "Underline" -variable XCWinOps(${window},fontlining) \
	-value underline -command "xcircuit::label insert underline"
   $m2 add radio -label "Overline" -variable XCWinOps(${window},fontlining) \
	-value overline -command "xcircuit::label insert overline"
   $m2 add radio -label "No Line" -variable XCWinOps(${window},fontlining) \
	-value normal -command "xcircuit::label insert noline"

   set m2 [menu $m.encodingmenu -tearoff 0]
   $m2 add radio -label "Standard" -variable XCWinOps(${window},fontencoding) \
	-value Standard -command "xcircuit::label encoding Standard"
   $m2 add radio -label "ISOLatin1" -variable XCWinOps(${window},fontencoding) \
	-value ISOLatin1 -command "xcircuit::label encoding ISOLatin1"

   set m2 [menu $m.insertmenu -tearoff 0]
   $m2 add command -label "Tab stop" -command "xcircuit::label insert stop"
   $m2 add command -label "Tab forward" -command "xcircuit::label insert forward"
   $m2 add command -label "Tab backward" -command "xcircuit::label insert backward"
   $m2 add command -label "Margin stop" -command {if {[lindex [xcircuit::label \
		substring] 1] <= 1} {xcircuit::promptmargin} else \
		{xcircuit::label insert margin}}
   $m2 add command -label "Carriage Return" -command "xcircuit::label insert return"
   $m2 add command -label "1/2 space" -command "xcircuit::label insert halfspace"
   $m2 add command -label "1/4 space" -command "xcircuit::label insert quarterspace"
   $m2 add command -label "Kern" -command "xcircuit::promptkern"
   $m2 add command -label "Character" -command "xcircuit::label insert special"
   $m2 add command -label "Parameter" -command "xcircuit::prompteditparams"

   set m2 [menu $m.justifymenu -tearoff 0]
   $m2 add radio -label "Left Justified" -variable XCWinOps(${window},jhoriz) \
	-value left -command "xcircuit::label justify left"
   $m2 add radio -label "Center Justified" -variable XCWinOps(${window},jhoriz) \
	-value center -command "xcircuit::label justify center"
   $m2 add radio -label "Right Justified" -variable XCWinOps(${window},jhoriz) \
	-value right -command "xcircuit::label justify right"
   $m2 add separator
   $m2 add radio -label "Top Justified" -variable XCWinOps(${window},jvert) \
	-value top -command "xcircuit::label justify top"
   $m2 add radio -label "Middle Justified" -variable XCWinOps(${window},jvert) \
	-value middle -command "xcircuit::label justify middle"
   $m2 add radio -label "Bottom Justified" -variable XCWinOps(${window},jvert) \
	-value bottom -command "xcircuit::label justify bottom"
   $m2 add separator
   $m2 add check -label "Flip Invariant" \
	-variable XCWinOps(${window},flipinvariant) \
	-onvalue true -offvalue false -command {xcircuit::label flipinvariant \
	$XCWinOps($XCOps(focus),flipinvariant)}

   set m [menu ${window}.menubar.optionsbutton.optionsmenu -tearoff 0]
   $m add check -label "Alt Colors" -variable XCWinOps(${window},colorscheme) \
	-onvalue inverse -offvalue normal -command {xcircuit::config \
	colorscheme $XCWinOps($XCOps(focus),colorscheme)}
   $m add check -label "Show Bounding Box" -variable XCWinOps(${window},showbbox) \
	-onvalue visible -offvalue invisible -command \
	{xcircuit::config bbox $XCWinOps($XCOps(focus),showbbox)}
   $m add check -label "Edit In Place" -variable XCWinOps(${window},editinplace) \
	-onvalue true -offvalue false -command {xcircuit::config editinplace \
	$XCWinOps($XCOps(focus),editinplace)}
   $m add check -label "Show Pin Positions" \
	-variable XCWinOps(${window},pinpositions) \
	-onvalue visible -offvalue invisible -command \
	{xcircuit::config pinpositions $XCWinOps($XCOps(focus),pinpositions)}
   $m add check -label "Wires Stay Attached to Pins" \
	-variable XCWinOps(${window},pinattach) \
	-onvalue true -offvalue false -command \
	{xcircuit::config pinattach $XCWinOps($XCOps(focus),pinattach)}
   $m add check -label "Show Clipmask Outlines" \
	-variable XCWinOps(${window},showclipmasks) \
	-onvalue show -offvalue hide -command \
	{xcircuit::config clipmasks $XCWinOps($XCOps(focus),showclipmasks)}
   $m add check -label "Show Technology Namespaces" \
	-variable XCWinOps(${window},namespaces) \
	-onvalue true -offvalue false -command \
	{xcircuit::config technologies $XCWinOps($XCOps(focus),namespaces)}

   $m add command -label "Disable Toolbar" -command {xcircuit::toolbar disable}
   $m add check -label "Allow HOLD Mode" -variable XCOps(hold) -onvalue true \
	-offvalue false -command {xcircuit::config hold $XCOps(hold)}
   $m add cascade -label "Grid" -menu $m.gridmenu
   $m add cascade -label "Snap-to" -menu $m.snapmenu
   $m add cascade -label "Linewidth" -menu $m.linemenu
   $m add cascade -label "Elements" -menu $m.elementsmenu
   $m add separator
   $m add command -label "Help!" -command {xcircuit::helpwindow}

   set m2 [menu $m.gridmenu -tearoff 0]
   $m2 add check -label "Grid" -variable XCWinOps(${window},showgrid) \
	-onvalue true -offvalue false \
	-command {xcircuit::config grid $XCWinOps($XCOps(focus),showgrid); refresh}
   $m2 add check -label "Axes" -variable XCWinOps(${window},showaxes) \
	-onvalue true -offvalue false \
	-command {xcircuit::config axes $XCWinOps($XCOps(focus),showaxes); refresh}
   $m2 add command -label "Grid Spacing" -command {xcircuit::promptgridspace}
   $m2 add cascade -label "Grid type/display" -menu $m2.gridsubmenu

   set m3 [menu $m2.gridsubmenu -tearoff 0]
   $m3 add radio -label "Decimal Inches" -variable XCWinOps(${window},gridstyle) \
	-value "decimal inches" \
	-command {xcircuit::config coordstyle "decimal inches"}
   $m3 add radio -label "Fractional Inches" -variable XCWinOps(${window},gridstyle) \
	-value "fractional inches" \
	-command {xcircuit::config coordstyle "fractional inches"}
   $m3 add radio -label "Centimeters" -variable XCWinOps(${window},gridstyle) \
	-value "centimeters" -command {xcircuit::config coordstyle "centimeters"}
   $m3 add radio -label "Internal Units" -variable XCWinOps(${window},gridstyle) \
	-value "internal units" -command \
	{xcircuit::config coordstyle "internal units"}
   $m3 add separator
   $m3 add command -label "Drawing Scale" -command {xcircuit::promptdrawingscale}

   set m2 [menu $m.snapmenu -tearoff 0]
   $m2 add check -label "Snap-to" -variable XCWinOps(${window},showsnap) \
	-onvalue true \
	-offvalue false -command {xcircuit::config snap \
	$XCWinOps($XCOps(focus),showsnap); refresh}
   $m2 add command -label "Snap Spacing" -command {xcircuit::promptsnapspace}

   set m2 [menu $m.linemenu -tearoff 0]
   $m2 add command -label "Wire Linewidth" -command {xcircuit::promptborderwidth}
   $m2 add command -label "Global Linewidth" -command {xcircuit::promptlinewidth}

   set m2 [menu $m.elementsmenu -tearoff 0]
   $m2 add cascade -label "Border" -menu $m2.bordermenu
   $m2 add cascade -label "Fill" -menu $m2.fillmenu
   $m2 add cascade -label "Color" -menu $m2.colormenu
   $m2 add separator
   $m2 add cascade -label "Parameters" -menu $m2.parammenu
   $m2 add command -label "Scale" -command {xcircuit::promptelementsize}
   $m2 add check -label "Center Object" -variable XCWinOps(${window},centerobject) \
	-onvalue true -offvalue false -command {xcircuit::config centering \
	$XCWinOps($XCOps(focus),centerobject)}
   $m2 add check -label "Instance Scale-invariant Linewidth" \
	-variable XCWinOps(${window},scaleinvariant) \
	-onvalue invariant -offvalue variant -command {xcircuit::instance linewidth \
	$XCWinOps($XCOps(focus),scaleinvariant)}
   $m2 add check -label "Manhattan Draw" \
	-variable XCWinOps(${window},manhattandraw) \
	-onvalue true -offvalue false -command {xcircuit::config manhattan \
	$XCWinOps($XCOps(focus),manhattandraw)}
   $m2 add check -label "Link Curve Tangents" \
	-variable XCWinOps(${window},pathedittype) \
	-onvalue tangents -offvalue normal -command {xcircuit::config pathedit \
	$XCWinOps($XCOps(focus),pathedittype)}
   $m2 add cascade -label "Polygon Edit" -menu $m2.polyeditmenu

   set m3 [menu $m2.bordermenu -tearoff 0]
   $m3 add command -label "Linewidth" -command {xcircuit::promptborderwidth}
   $m3 add separator
   $m3 add radio -label "Solid" -variable XCWinOps(${window},linestyle) \
	-value solid -command {xcircuit::border solid}
   $m3 add radio -label "Dashed" -variable XCWinOps(${window},linestyle) \
	-value dashed -command {xcircuit::border dashed}
   $m3 add radio -label "Dotted" -variable XCWinOps(${window},linestyle) \
	-value dotted -command {xcircuit::border dotted}
   $m3 add radio -label "Unbordered" -variable XCWinOps(${window},linestyle) \
	-value unbordered -command {xcircuit::border unbordered}
   $m3 add separator
   $m3 add check -label "Closed" -variable XCWinOps(${window},polyclosed) \
	-onvalue closed -offvalue unclosed -command \
	{xcircuit::border $XCWinOps($XCOps(focus),polyclosed)}
   $m3 add check -label "Square Endcaps" -variable XCWinOps(${window},endcaps) \
	-onvalue square -offvalue round -command {xcircuit::border \
	$XCWinOps($XCOps(focus),endcaps)}
   $m3 add check -label "Bounding Box" -variable XCWinOps(${window},bboxtype) \
	-onvalue true -offvalue false -command {xcircuit::border bbox \
	$XCWinOps($XCOps(focus),bboxtype)}
   $m3 add check -label "Clipmask" -variable XCWinOps(${window},clipmask) \
	-onvalue true -offvalue false -command {xcircuit::border clipmask \
	$XCWinOps($XCOps(focus),clipmask)}
   $m3 add check -label "Manhattan Draw" -variable XCWinOps(${window},manhattandraw) \
	-onvalue true -offvalue false -command {xcircuit::config manhattan \
	$XCWinOps($XCOps(focus),manhattandraw)}
   $m3 add check -label "Manhattan Edit" \
	-variable XCWinOps(${window},polyedittype) \
	-onvalue manhattan -offvalue normal \
	-command {xcircuit::config boxedit \
	$XCWinOps($XCOps(focus),polyedittype)}

   set m3 [menu $m2.fillmenu -tearoff 0]
   $m3 add radio -image img_stip100 -variable XCWinOps(${window},fillamount) \
	-value 100 -command {xcircuit::fill 100 opaque}
   $m3 add radio -image img_stip88 -variable XCWinOps(${window},fillamount) \
	-value 88 -command {xcircuit::fill 88 opaque}
   $m3 add radio -image img_stip75 -variable XCWinOps(${window},fillamount) \
	-value 75 -command {xcircuit::fill 75 opaque}
   $m3 add radio -image img_stip62 -variable XCWinOps(${window},fillamount) \
	-value 62 -command {xcircuit::fill 62 opaque}
   $m3 add radio -image img_stip50 -variable XCWinOps(${window},fillamount) \
	-value 50 -command {xcircuit::fill 50 opaque}
   $m3 add radio -image img_stip38 -variable XCWinOps(${window},fillamount) \
	-value 38 -command {xcircuit::fill 38 opaque}
   $m3 add radio -image img_stip25 -variable XCWinOps(${window},fillamount) \
	-value 25 -command {xcircuit::fill 25 opaque}
   $m3 add radio -image img_stip12 -variable XCWinOps(${window},fillamount) \
	-value 12 -command {xcircuit::fill 12 opaque}
   $m3 add radio -image img_stip0 -variable XCWinOps(${window},fillamount) \
	-value 0 -command {xcircuit::fill 0 transparent}
   $m3 add separator
   $m3 add radio -label "Opaque" -variable XCWinOps(${window},opaque) \
	-value true -command {xcircuit::fill opaque}
   $m3 add radio -label "Transparent" -variable XCWinOps(${window},opaque) \
	-value false -command {xcircuit::fill transparent}

   set m3 [menu $m2.colormenu -tearoff 0]
   $m3 add command -label "Add New Color" -command {xcircuit::picknewcolor}
   $m3 add separator
   $m3 add radio -label "Inherit Color" -variable XCWinOps(${window},colorval) \
	-value inherit -command {color set inherit}

   set m3 [menu $m2.parammenu -tearoff 0]
   $m3 add command -label "Manage Parameters" -command {xcircuit::prompteditparams}
   $m3 add separator
   $m3 add check -label "X Position" -variable XCWinOps(${window},xposparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),xposparam)} \
		{xcircuit::parameter make "x position"} \
		{xcircuit::parameter replace "x position"}}
   $m3 add check -label "Y Position" -variable XCWinOps(${window},yposparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),yposparam)} \
		{xcircuit::parameter make "y position"} \
		{xcircuit::parameter replace "y position"}}
   $m3 add check -label "Justification" -variable XCWinOps(${window},justparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),justparam)} \
		{xcircuit::parameter make justification} \
		{xcircuit::parameter replace justification}}
   $m3 add check -label "Rotation" -variable XCWinOps(${window},rotationparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),rotationparam)} \
		{xcircuit::parameter make rotation} \
		{xcircuit::parameter replace rotation}}
   $m3 add check -label "Style" -variable XCWinOps(${window},styleparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),styleparam)} \
		{xcircuit::parameter make style} \
		{xcircuit::parameter replace style}}
   $m3 add check -label "Scale" -variable XCWinOps(${window},scaleparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),scaleparam)} \
		{xcircuit::parameter make scale} \
		{xcircuit::parameter replace scale}}
   $m3 add check -label "Linewidth" -variable XCWinOps(${window},linewidthparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),linewidthparam)} \
		{xcircuit::parameter make linewidth} \
		{xcircuit::parameter replace linewidth}}
   $m3 add check -label "Color" -variable XCWinOps(${window},colorparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),colorparam)} \
		{xcircuit::parameter make color} \
		{xcircuit::parameter replace color}}
   $m3 add check -label "Start Angle" -variable XCWinOps(${window},startparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),startparam)} \
		{xcircuit::parameter make "start angle"} \
		{xcircuit::parameter replace "start angle"}}
   $m3 add check -label "End Angle" -variable XCWinOps(${window},endparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),endparam)} \
		{xcircuit::parameter make "end angle"} \
		{xcircuit::parameter replace "end angle"}}
   $m3 add check -label "Radius" -variable XCWinOps(${window},radiusparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),radiusparam)} \
		{xcircuit::parameter make radius} \
		{xcircuit::parameter replace radius}}
   $m3 add check -label "Minor Axis" -variable XCWinOps(${window},minorparam) \
	-onvalue true -offvalue false -command \
	{if {$XCWinOps($XCOps(focus),minorparam)} \
		{xcircuit::parameter make "minor axis"} \
		{xcircuit::parameter replace "minor axis"}}

   set m3 [menu $m2.polyeditmenu -tearoff 0]
   $m3 add radio -label "Manhattan Box Edit" \
	-variable XCWinOps(${window},polyedittype) \
	-value manhattan -command {xcircuit::config boxedit manhattan}
   $m3 add radio -label "Rhomboid X" -variable XCWinOps(${window},polyedittype) \
	-value rhomboidx -command {xcircuit::config boxedit rhomboidx}
   $m3 add radio -label "Rhomboid Y" -variable XCWinOps(${window},polyedittype) \
	-value rhomboidy -command {xcircuit::config boxedit rhomboidy}
   $m3 add radio -label "Rhomboid A" -variable XCWinOps(${window},polyedittype) \
	-value rhomboida -command {xcircuit::config boxedit rhomboida}
   $m3 add radio -label "Normal" -variable XCWinOps(${window},polyedittype) \
	-value normal -command {xcircuit::config boxedit normal}

   set m [menu ${window}.menubar.windowbutton.windowmenu -tearoff 0]
   $m add command -label "Zoom In" -command {zoom 1.5; refresh}
   $m add command -label "Zoom Out" -command {zoom [expr {1 / 1.5}]; refresh}
   $m add command -label "Pan" -command {$XCOps(focus).mainframe.toolbar.bp invoke}
   $m add command -label "Full View" -command {zoom view; refresh}
   $m add command -label "Refresh" -command {refresh}
   $m add separator
   $m add command -label "Library Directory" -command {xcircuit::library directory}
   $m add cascade -label "Goto Library" -menu $m.librarymenu
   $m add separator
   $m add command -label "Page Directory" -command {xcircuit::page directory}
   $m add cascade -label "Goto Page" -menu $m.pagemenu

   set m [menu ${window}.menubar.netlistbutton.netlistmenu -tearoff 0]
   $m add command -label "Make Pin" -command \
	{set XCWinOps($XCOps(focus),labeltype) "Pin Label"; set \
	XCWinOps($XCOps(focus),labelimage) img_tp; \
	$XCOps(focus).mainframe.toolbar.bt invoke}
   $m add command -label "Make Info Pin" -command \
	{set XCWinOps($XCOps(focus),labeltype) "Info Label"; set \
	XCWinOps($XCOps(focus),labelimage) img_ti; \
	$XCOps(focus).mainframe.toolbar.bt invoke}
   $m add command -label "Make Global Pin" -command \
	{set XCWinOps($XCOps(focus),labeltype) "Pin Global"; set \
	XCWinOps($XCOps(focus),labelimage) img_tg; \
	$XCOps(focus).mainframe.toolbar.bt invoke}
   $m add cascade -label "Convert Label To..." -menu $m.pinmenu
   $m add check -label "Pin Visibility" -variable XCWinOps(${window},pinvisible) \
	-onvalue true -offvalue false -command {xcircuit::label visible \
	$XCWinOps($XCOps(focus),pinvisible)}
   $m add command -label "Make Matching Symbol" -command \
	{xcircuit::promptmakesymbol [page label]}
   $m add command -label "Associate With Symbol" -command \
	{xcircuit::symbol associate}
   $m add command -label "Highlight Connectivity" -command \
	{xcircuit::netlist highlight}
   $m add command -label "Auto-number Components" -command \
	{xcircuit::netlist autonumber}
   $m add command -label "Un-number Components" -command \
	{xcircuit::netlist autonumber -forget}
   $m add separator
   $m add check -label "SPICE .end statement" -variable XCOps(spiceend) \
	-onvalue true -offvalue false
   $m add check -label "Always regenerate netlists" -variable XCOps(forcenets) \
	-onvalue true -offvalue false
   $m add separator
   $m add command -label "Write SPICE netlist" -command \
	{if {$XCOps(forcenets)} {xcircuit::netlist update}; \
	 xcircuit::netlist write spice spc $XCOps(spiceend)}
   $m add command -label "Write flattened SPICE" -command \
	{if {$XCOps(forcenets)} {xcircuit::netlist update}; \
	xcircuit::netlist write flatspice fspc}
   $m add command -label "Write sim" -command \
	{if {$XCOps(forcenets)} {xcircuit::netlist update}; \
	xcircuit::netlist write flatsim sim}
   $m add command -label "Write pcb" -command \
	{if {$XCOps(forcenets)} {xcircuit::netlist update}; \
	xcircuit::netlist write pcb pcbnet}

   set m2 [menu $m.pinmenu -tearoff 0]
   $m2 add command -label "Normal label" -command {xcircuit::label type normal}
   $m2 add command -label "Local Pin" -command {xcircuit::label type pin}
   $m2 add command -label "Global Pin" -command {xcircuit::label type global}
   $m2 add command -label "Info label" -command {xcircuit::label type info}

   #---------------------------------------------------------------------------
   # Create the cloned menu links used by the toolbar
   #---------------------------------------------------------------------------

   ${window}.menubar.optionsbutton.optionsmenu.elementsmenu.parammenu \
		clone ${window}.parammenu
   ${window}.menubar.optionsbutton.optionsmenu.elementsmenu.colormenu \
		clone ${window}.colormenu
   ${window}.menubar.optionsbutton.optionsmenu.elementsmenu.bordermenu \
		clone ${window}.bordermenu
   ${window}.menubar.optionsbutton.optionsmenu.elementsmenu.fillmenu \
		clone ${window}.fillmenu

   .librarymenu clone ${window}.menubar.windowbutton.windowmenu.librarymenu
   .pagemenu clone ${window}.menubar.windowbutton.windowmenu.pagemenu
}

#-----------------------------------------------------------------
# Wrapper procedure to (re)bind a key to a Tcl procedure (Ed Casas 9/4/03)
# With no arguments, prints a list of bindings to stdout.  Key
# bindings should use "keyaction" to check for text mode, so that
# rebinding of keys does not prevent text entry.  Button bindings
# do not need this restriction.
#-----------------------------------------------------------------

proc xcircuit::keybind { {key {}} {proc {}} {window {}} } {
   global XCOps

   if { $window == {} } { set window $XCOps(focus).mainframe.mainarea.drawing }

   switch -glob -- $key {
      {} {
	puts stdout "XCircuit standard key bindings:"
	puts stdout "Key		Binding"
	puts stdout "-------------------------------------"
	set kpairs [xcircuit::bindkey]
	foreach i $kpairs {
	   set pkey [lindex $i 0]
	   set pval [lindex $i 1]
	puts stdout "$pkey		$pval"
	}
	puts stdout ""
      }
      <[Bb]utton-?> {
	bind ${window} $key $proc
      }
      default {
	bind ${window} $key "if \{!\[xcircuit::keyaction %k %s\]\} \{ $proc \}"
      }
   }
}

#-----------------------------------------------------------------
# Enable mouse hints in the window
#-----------------------------------------------------------------

# James Vernon's mouse button hints

if {[catch {source $XCIRCUIT_SRC_DIR/mousehint.tcl}]} {
   set XCOps(mousehints) -1
} else {
   set XCOps(mousehints) 0
}

proc xcircuit::enable_mousehints {} {
   global XCOps

   if {$XCOps(mousehints) == 0} {
      set XCOps(mousehints) 1
      foreach window [config windownames] {
         set frame [winfo top $window]
         xcircuit::mousehint_create $frame
      }
   }
}

#-----------------------------------------------------------------
# Final setup stuff before exiting back to interpreter
#-----------------------------------------------------------------

# This gets rid of the original "wish", in favor of our own window

if {[string range [wm title .] 0 3] == "wish"} {
   wm withdraw .
}

#----------------------------------------------------------------------
# Library and Page menus (common to all windows)
#----------------------------------------------------------------------

menu .librarymenu -tearoff 0
.librarymenu add command -label "New Library Page" -command \
	{xcircuit::promptaddlibrary}
.librarymenu add separator

menu .pagemenu -tearoff 0
.pagemenu add command -label "Add New Page" -command {xcircuit::page make}
.pagemenu add separator

#----------------------------------------------------------------------
# Source other Tcl scripts, if they exist in the $XCIRCUIT_SRC_DIR path
# and add the capabilities to the GUI.
#----------------------------------------------------------------------

# "Write All" feature

catch {source $XCIRCUIT_SRC_DIR/files.tcl}

# Library manager widget

catch {source $XCIRCUIT_SRC_DIR/library.tcl}

# Fancy "Make Matching Symbol" feature

catch {source $XCIRCUIT_SRC_DIR/symbol.tcl}

# Help window

catch {source $XCIRCUIT_SRC_DIR/xchelp.tcl}

# EDIF file parser

catch {source $XCIRCUIT_SRC_DIR/edif.tcl}

# System Clipboard paste into labels

catch {source $XCIRCUIT_SRC_DIR/selection.tcl}

# Regexp label substitutions and auto-increment feature

catch {source $XCIRCUIT_SRC_DIR/text.tcl}

# Wim Vereecken's Matlab PostScript import function

catch {source $XCIRCUIT_SRC_DIR/matgen.tcl}

#----------------------------------------------------------------------
# Create the initial window.
#----------------------------------------------------------------------

xcircuit::new_window $XCOps(focus)

#----------------------------------------------------------------------
# Add buttons for the pre-allocated pages
#----------------------------------------------------------------------

xcircuit::newpagebutton "Page 1" 1
xcircuit::newpagebutton "Page 2" 2
xcircuit::newpagebutton "Page 3" 3
xcircuit::newpagebutton "Page 4" 4
xcircuit::newpagebutton "Page 5" 5
xcircuit::newpagebutton "Page 6" 6
xcircuit::newpagebutton "Page 7" 7
xcircuit::newpagebutton "Page 8" 8
xcircuit::newpagebutton "Page 9" 9
xcircuit::newpagebutton "Page 10" 10

#----------------------------------------------------------------------
# Add buttons for the pre-allocated libraries
#----------------------------------------------------------------------

xcircuit::newlibrarybutton "User Library"

#-----------------------------------------------------------------
# New key bindings should pass through this function so that key
# strokes are captured correctly for label editing.
#----------------------------------------------------------------------

proc xcircuit::keyaction {keycode {keystate 0}} {
   switch -- [eventmode] {
      text -
      etext -
      cattext {
	 standardaction $keycode down $keystate
	 return true
      }
   }
   return false
}

#-----------------------------------------------------------------

proc scrollboth { lists args } {
   foreach l $lists {
      eval {$l yview} $args
   }
}

#-----------------------------------------------------------------
# Procedure to generate the help window
#-----------------------------------------------------------------

proc xcircuit::makehelpwindow {} {
   toplevel .help -bg beige
   wm group .help .
   wm withdraw .help

   frame .help.title -bg beige
   frame .help.listwin

   pack .help.title -side top -fill x
   pack .help.listwin -side top -fill both -expand true

   label .help.title.field -text "XCircuit Help" -bg beige
   button .help.title.dbut -text "Dismiss" -bg beige -command {wm withdraw .help}
   pack .help.title.field -side left -padx 10
   pack .help.title.dbut -side right -ipadx 10

   listbox .help.listwin.func -yscrollcommand ".help.listwin.sb set" \
	-setgrid 1 -height 20
   listbox .help.listwin.keys -yscrollcommand ".help.listwin.sb set" \
	-setgrid 1 -height 20
   scrollbar .help.listwin.sb -orient vertical -command \
	[list scrollboth [list .help.listwin.func .help.listwin.keys]]
   message .help.listwin.win -width 200 -justify left -anchor n \
	-relief groove -text "Click on a function for help text"

   grid .help.listwin.func -row 0 -column 0 -sticky news -padx 1 -pady 1
   grid .help.listwin.keys -row 0 -column 1 -sticky news -padx 1 -pady 1
   grid .help.listwin.sb -row 0 -column 2 -sticky ns -padx 1 -pady 1
   grid .help.listwin.win -row 0 -column 3 -sticky news -padx 1 -pady 1

   grid columnconfigure .help.listwin 1 -weight 1 -minsize 100
   grid rowconfigure .help.listwin 0 -weight 1 -minsize 100

   bind .help.listwin.func <ButtonRelease-1> "xcircuit::printhelp"
}

#-----------------------------------------------------------------
# Procedure to update and display the help window
#-----------------------------------------------------------------

proc xcircuit::helpwindow {} {

   # Create the help window if it doesn't exist
   if {[catch {wm state .help}]} {
      xcircuit::makehelpwindow
   }
   set wstate [xcircuit::getinitstate .help]

   .help.listwin.func delete 0 end
   .help.listwin.keys delete 0 end

   set k [lsort -dictionary [xcircuit::bindkey]]

   .help.listwin.func insert end "Function"
   .help.listwin.keys insert end "Keys"
   .help.listwin.func insert end ""
   .help.listwin.keys insert end ""

   foreach i $k {
      set pkeys [xcircuit::bindkey -func $i]
      .help.listwin.func insert end "$i"
      .help.listwin.keys insert end "$pkeys"
   }

   if {"$wstate" != "normal"} { 
      wm deiconify .help
      xcircuit::centerwin .help
   }
   raise .help
}

#-----------------------------------------------------------------
# Prevent "Tab" from removing focus from the window during text edits,
# but allow it to take its normal meaning at other times.
#-----------------------------------------------------------------

bind all <Tab> { 
   switch -- [eventmode] {                        
      text -
      etext -
      cattext {} 
      default {tk::TabToWindow [tk_focusNext %W]}
   }  
}  

bind all <<PrevWindow>> {
   switch -- [eventmode] {
      text -
      etext -
      cattext {}
      default {tk::TabToWindow [tk_focusPrev %W]}
   }
}

#-----------------------------------------------------------------
# Wait for the drawing area to become visible, and set the focus on it.
# Invoke the "wire" button so we have a default button1 action.
#-----------------------------------------------------------------

tkwait visibility $XCOps(focus).mainframe.mainarea.drawing
focus -force $XCOps(focus).mainframe.mainarea.drawing

#-----------------------------------------------------------------
# This pops down the interpreter window, if the "console.tcl" script was run.
#-----------------------------------------------------------------

catch xcircuit::consoledown

#-----------------------------------------------------------------
# End of GUI configuration file.  Xcircuit continues to load the xcircuit
# startup configuration files.
#-----------------------------------------------------------------