aboutsummaryrefslogtreecommitdiff
path: root/pd/src/u_main.tk
blob: 26545f98436a0ea8bb2322257399016d633a7157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
#!/usr/bin/wish

# set pd_nt (bad name) 0 for unix, 1 for microsoft, and 2 for Mac OSX.
if { $tcl_platform(platform) == "windows" }  {
         set pd_nt 1
} elseif { $tcl_platform(os) == "Darwin" } {  
         set pd_nt 2
} else { 
         set pd_nt 0
}        

# Copyright (c) 1997-1999 Miller Puckette.
# For information on usage and redistribution, and for a DISCLAIMER OF ALL
# WARRANTIES, see the file, "LICENSE.txt," in this distribution.

# changed by Thomas Musil 09.2001
# between "pdtk_graph_dialog -- dialog window for graphs"
# and "pdtk_array_dialog -- dialog window for arrays"
# a new dialogbox was inserted, named:
# "pdtk_iemgui_dialog -- dialog window for iem guis"
#
# all this changes are labeled with #######iemlib##########

# Tearoff is set to true by default:
set pd_tearoff 1

# jsarlo
set pd_array_listview_pagesize 1000
set pd_array_listview_id(0) 0
set pd_array_listview_entry(0) 0
set pd_array_listview_page(0) 0
# end jsarlo

if {$pd_nt == 1} {
    global pd_guidir
    global pd_tearoff
    set pd_gui2 [string range $argv0 0 [expr [string last \\ $argv0 ] - 1]]
    regsub -all \\\\ $pd_gui2 / pd_gui3
    set pd_guidir $pd_gui3/..
    load $pd_guidir/bin/pdtcl.dll
    set pd_tearoff 1
}

if {$pd_nt == 2} {
# turn on James Tittle II's fast drawing (wait until I can test this...):
#   set tk::mac::useCGDrawing 1
    # set minimum line size for anti-aliasing.  If set to 1 or 0, then every
    # line will be anti-aliased.  While this makes connections and circles in
    # [bng] and such look really good, it makes boxes and messages look out of
    # focus.  Setting this to 2 makes it so the thick audio rate connections
    # are anti-aliased.  <hans@at.or.at> 2005-06-09
    set tk::mac::CGAntialiasLimit 2
    global pd_guidir
    global pd_tearoff
    set pd_gui2 [string range $argv0 0 [expr [string last / $argv0 ] - 1]]
    set pd_guidir $pd_gui2/..
    load $pd_guidir/bin/libPdTcl.dylib
    set pd_tearoff 0

	 # tk::mac::OpenDocument is called with the filenames put into the 
	 # var args whenever docs are either dropped on the Pd.app icon or 
	 # opened from the Finder.
	 # It uses menu_doc_open so it can handles numerous file types.
	 proc tk::mac::OpenDocument {args} {
		  foreach file $args {
				pd [concat pd open [pdtk_enquote [file tail $file]] \
						  [pdtk_enquote  [file dirname $file]] \;]
				menu_doc_open [file dirname $file] [file tail $file]
		  }
	 }
}

# hack so you can easily test-run this script in linux... define pd_guidir
# (which is normally defined at startup in pd under linux...)

if {$pd_nt == 0} {
    if {! [info exists pd_guidir]} {
    	global pd_guidir
    	puts stderr {setting pd_guidir to '.'}
	set pd_guidir .
    }
}

# it's unfortunate but we seem to have to turn off global bindings
# for Text objects to get control-s and control-t to do what we want for
# "text" dialogs below.  Also we have to get rid of tab's changing the focus.

bind all <Key-Tab> ""
bind all <<PrevWindow>> ""
bind Text <Control-t> {}
bind Text <Control-s> {}
# puts stderr [bind all]

################## set up main window #########################
# the menus are instantiated here for the main window
# for the patch windows, they are created by pdtk_canvas_new
menu .mbar
canvas .dummy -height 2p -width 6c

frame .controls
pack .controls .dummy -side top -fill x
menu .mbar.file -tearoff $pd_tearoff
.mbar add cascade -label "File" -menu .mbar.file
menu .mbar.find -tearoff $pd_tearoff
.mbar add cascade -label "Find" -menu .mbar.find
menu .mbar.windows -postcommand [concat pdtk_fixwindowmenu] -tearoff $pd_tearoff
menu .mbar.audio -tearoff $pd_tearoff
if {$pd_nt != 2} {
    .mbar add cascade -label "Windows" -menu .mbar.windows
    .mbar add cascade -label "Media" -menu .mbar.audio
    menu .mbar.help -tearoff $pd_tearoff
    .mbar add cascade -label "Help" -menu .mbar.help
} else {
    menu .mbar.apple -tearoff 0
    .mbar add cascade -label "Apple" -menu .mbar.apple 
# arrange menus according to Apple HIG
    .mbar add cascade -label "Media" -menu .mbar.audio
    .mbar add cascade -label "Window" -menu .mbar.windows
    menu .mbar.help -tearoff $pd_tearoff
    .mbar add cascade -label "Help" -menu .mbar.help
}

set ctrls_audio_on 0
set ctrls_meter_on 0
set ctrls_inlevel 0
set ctrls_outlevel 0

frame .controls.switches
checkbutton .controls.switches.audiobutton -text {compute audio} \
    -variable ctrls_audio_on \
    -anchor w \
    -command {pd [concat pd dsp $ctrls_audio_on \;]}

checkbutton .controls.switches.meterbutton -text {peak meters} \
    -variable ctrls_meter_on \
    -anchor w \
    -command {pd [concat pd meters $ctrls_meter_on \;]}

pack .controls.switches.audiobutton .controls.switches.meterbutton -side top

frame .controls.inout
frame .controls.inout.in
label .controls.inout.in.label -text IN
entry .controls.inout.in.level -textvariable ctrls_inlevel -width 3
button .controls.inout.in.clip -text {CLIP} -state disabled
pack .controls.inout.in.label .controls.inout.in.level \
      .controls.inout.in.clip -side top -pady 2

frame .controls.inout.out
label .controls.inout.out.label -text OUT
entry .controls.inout.out.level -textvariable ctrls_outlevel -width 3
button .controls.inout.out.clip -text {CLIP} -state disabled
pack .controls.inout.out.label .controls.inout.out.level \
      .controls.inout.out.clip -side top -pady 2

button .controls.dio -text "DIO\nerrors" \
    -command {pd [concat pd audiostatus \;]}

pack .controls.inout.in .controls.inout.out -side left -padx 6
pack .controls.inout -side left -padx 14
pack .controls.switches -side right
pack .controls.dio -side right -padx 20


frame .printout
text .printout.text -relief raised -bd 2 -font -*-courier-bold--normal--12-* \
    -yscrollcommand ".printout.scroll set" -width 80
# .printout.text insert end "\n\n\n\n\n\n\n\n\n\n"
scrollbar .printout.scroll -command ".printout.text yview"
pack .printout.scroll -side right -fill y
pack .printout.text -side left -fill both -expand 1
pack .printout -side bottom -fill both -expand 1

proc pdtk_post {stuff} {
    .printout.text insert end $stuff
    .printout.text yview end-2char
}

proc pdtk_standardkeybindings {id} {
    global pd_nt
    bind $id <Control-Key> {pdtk_pd_ctrlkey %W %K 0}
    bind $id <Control-Shift-Key> {pdtk_pd_ctrlkey %W %K 1}
    if {$pd_nt == 2} {
	bind $id <Mod1-Key> {pdtk_canvas_ctrlkey %W %K 0}
	bind $id <Mod1-Shift-Key> {pdtk_canvas_ctrlkey %W %K 1}
    }
}

pdtk_standardkeybindings .

wm title . "Pd"
. configure -menu .mbar -width 200 -height 150

############### set up global variables ################################

set untitled_number 1
set untitled_directory [pwd]
set saveas_client doggy
set pd_opendir $untitled_directory
set pd_undoaction no
set pd_redoaction no
set pd_undocanvas no

################ utility functions #########################

# enquote a string to send it to a tcl function
proc pdtk_enquote {x} {
    set foo [string map {"," "" ";" "" \" ""} $x]
    set foo2 [string map {" " "\\ "} $foo]
    concat $foo2
}

#enquote a string to send it to Pd.  Blow off semi and comma; alias spaces
proc pdtk_unspace {x} {
    string map {" " "_" ";" "" "," ""} $x
}

#enquote a string for preferences (command strings etc.)
proc pdtk_encodedialog {x} {
    concat +[string map {" " "+_" "$" "+d" ";" "+s" "," "+c" "+" "++"} $x]
}

proc pdtk_debug {x} {
    tk_messageBox -message $x -type ok
}

proc pdtk_watchdog {} {
    pd [concat pd watchdog \;]
    after 2000 {pdtk_watchdog}
}

proc pdtk_ping {} {
    pd [concat pd ping \;]
}

proc pdtk_check {x message} {
    set answer [tk_messageBox \-message $x \-type yesno \-icon question]
    switch $answer {
    	yes {pd $message} }
#    	no {tk_messageBox \-message "cancelled" \-type ok}
}

set menu_windowlist {} 

proc pdtk_fixwindowmenu {} {
    global menu_windowlist
    .mbar.windows delete 0 end
    foreach i $menu_windowlist {
	.mbar.windows add command -label [lindex $i 0] \
	    -command [concat menu_domenuwindow [lindex $i 1]]
        menu_fixwindowmenu [lindex $i 1]
    }
}

####### Odd little function to make better Mac accelerators #####

proc accel_munge {acc} {
    global pd_nt

    if {$pd_nt == 2} {
	if [string is upper [string index $acc end]] {
	    return [format "%s%s" "Shift+" \
			[string toupper [string map {Ctrl Meta} $acc] end]]
	} else {
	    return [string toupper [string map {Ctrl Meta} $acc] end]
	}
    } else {
	return $acc
    }
}



###############  the "New" menu command  ########################
proc menu_new {} {
    global untitled_number
    global untitled_directory
    pd [concat pd filename Untitled-$untitled_number $untitled_directory \;]
    pd {
    	#N canvas;
    	#X pop 1;
    }
    set untitled_number [expr $untitled_number + 1]
}

################## the "Open" menu command #########################

proc menu_open {} {
    global pd_opendir
    set filename [tk_getOpenFile -defaultextension .pd \
    	-filetypes { {{pd files} {.pd}} {{max files} {.pat}}} \
        -initialdir $pd_opendir]
    if {$filename != ""} {open_file $filename}
}

proc open_file {filename} {
    global pd_opendir
    set directory [string range $filename 0 [expr [string last / $filename] - 1]]
    set pd_opendir $directory
    set basename [string range $filename [expr [string last / $filename] + 1] end]
    if {[string last .pd $filename] >= 0} {
        pd "pd open [pdtk_enquote $basename] [pdtk_enquote $directory] ;"
    }
}

catch {
    package require tkdnd
    dnd bindtarget . text/uri-list <Drop> {
	foreach file %D {open_file $file}
    }
}

################## the "Message" menu command #########################
proc menu_send {} {
    toplevel .sendpanel
    entry .sendpanel.entry -textvariable send_textvariable
    pack .sendpanel.entry -side bottom -fill both -ipadx 100
    .sendpanel.entry select from 0
    .sendpanel.entry select adjust end
    bind .sendpanel.entry <KeyPress-Return> {
    	pd [concat $send_textvariable \;]
    }
    pdtk_standardkeybindings .sendpanel.entry
    focus .sendpanel.entry
}

################## the "Quit" menu command #########################
proc menu_really_quit {} {pd {pd quit;}}

proc menu_quit {} {pdtk_check {Really quit?} {pd quit;}}

######### the "Pd" menu command, which puts the Pd window on top ########
proc menu_pop_pd {} {raise .}

######### the "audio" menu command  ###############
proc menu_audio {flag} {pd [concat pd dsp $flag \;]}

######### the "documentation" menu command  ###############

set doc_number 1

# open text docs in a Pd window
proc menu_opentext {filename} {
    global doc_number
    global pd_guidir
    global pd_myversion
#    global pd_font3
    set name [format ".help%d" $doc_number]
    toplevel $name
    text $name.text -relief raised -bd 2 -font -*-times-regular--normal--14-* \
    	-yscrollcommand "$name.scroll set" -background white
    scrollbar $name.scroll -command "$name.text yview"
    pack $name.scroll -side right -fill y
    pack $name.text -side left -fill both -expand 1
    
    set f [open $filename]
    while {![eof $f]} {
    	set bigstring [read $f 1000]
	regsub -all PD_BASEDIR $bigstring $pd_guidir bigstring2
    	regsub -all PD_VERSION $bigstring2 $pd_myversion bigstring3
    	$name.text insert end $bigstring3
    }
    close $f
    set doc_number [expr $doc_number + 1] 
}

# open HTML docs from the menu using the OS-default HTML viewer
proc menu_openhtml {filename} {
    global pd_nt	 

    if {$pd_nt == 0} {
        exec sh -c \
            [format "firefox file:%s || mozilla file:%s &\n" \
                $filename $filename]
    } elseif {$pd_nt == 2} {
        puts stderr [format "open %s" $filename]
            exec sh -c [format "open %s" $filename]
    } else {
        exec rundll32 url.dll,FileProtocolHandler \
            [format "file:%s" $filename] &
    }
}

set help_directory $pd_guidir/doc
set help_top_directory $pd_guidir/doc

proc menu_documentation {} {
    global help_directory
    global pd_nt
    global pd_guidir

    if {$pd_nt == 2} {
    	exec rm -rf /tmp/pd-documentation
    	exec cp -pr $pd_guidir/doc /tmp/pd-documentation
	set filename [tk_getOpenFile -defaultextension .pd \
	-filetypes { {{documentation} {.pd .txt .htm}} } \
	-initialdir /tmp/pd-documentation]
    } else {
	set filename [tk_getOpenFile -defaultextension .pd \
	-filetypes { {{documentation} {.pd .txt .htm}} } \
	-initialdir $help_directory]
    }    
    if {$filename != ""} {
    	if {[string first .txt $filename] >= 0} {
    	    menu_opentext $filename
    	} elseif {[string first .htm $filename] >= 0} {
				menu_openhtml $filename
	} else {
	    set help_directory [string range $filename 0 \
    		[expr [string last / $filename ] - 1]]
    	    set basename [string range $filename \
    		[expr [string last / $filename ] + 1] end]
    	    pd [concat pd open [pdtk_enquote $basename] \
		 [pdtk_enquote $help_directory] \;]
    	}
    }
}

proc menu_doc_open {subdir basename} {
    global pd_guidir
 
    set dirname $pd_guidir/$subdir

    if {[regexp ".*\.(txt|c)$" $basename]} {
    	menu_opentext $dirname/$basename
    } elseif {[regexp ".*\.html?$" $basename]} {
		  menu_openhtml $dirname/$basename
    } else {
    	pd [concat pd open [pdtk_enquote $basename] \
		[pdtk_enquote $dirname] \;]
    }
}

proc doc_submenu {helpmenu subdir} {
	 global help_top_directory pd_tearoff

	 set menudir $help_top_directory/$subdir

    regsub -all "\\\." $subdir "" submenu

	 menu $helpmenu.$submenu -tearoff $pd_tearoff
    regsub -all "\\\." $subdir " " submenuname
	 $helpmenu add cascade -label $submenuname \
		  -menu $helpmenu.$submenu
    catch {
# use this glob pattern to exclude the supporting files
#	 foreach file [ lsort [ glob -dir $menudir {*[0-9][0-9]*} ] ] 
	 foreach file [ lsort [ glob -dir $menudir * ] ] {
		  set filename ""
		  regsub {.*/(.*\..+$)} $file {\1} filename
		  $helpmenu.$submenu add command -label $filename \
				-command "menu_doc_open doc/$subdir $filename"
	 }
    }
}

############# routine to add media, help, and apple menu items ###############

proc menu_addstd {mbar} {
    global pd_apilist pd_midiapilist pd_nt pd_tearoff
#          the "Audio" menu
    $mbar.audio add command -label {audio ON} -accelerator [accel_munge "Ctrl+/"] \
	-command {menu_audio 1} 
    $mbar.audio add command -label {audio OFF} -accelerator [accel_munge "Ctrl+."] \
	-command {menu_audio 0} 
    for {set x 0} {$x<[llength $pd_apilist]} {incr x} {
    	$mbar.audio add radiobutton -label [lindex [lindex $pd_apilist $x] 0] \
	    -command {menu_audio 0} -variable pd_whichapi \
	    	-value [lindex [lindex $pd_apilist $x] 1]\
		-command {pd [concat pd audio-setapi $pd_whichapi \;]}
    }
    for {set x 0} {$x<[llength $pd_midiapilist]} {incr x} {
    	$mbar.audio add radiobutton -label [lindex [lindex $pd_midiapilist $x] 0] \
	    -command {menu_midi 0} -variable pd_whichmidiapi \
	    	-value [lindex [lindex $pd_midiapilist $x] 1]\
		-command {pd [concat pd midi-setapi $pd_whichmidiapi \;]}
    }
	 if {$pd_nt != 2} {
    $mbar.audio add command -label {Audio settings...} \
	-command {pd pd audio-properties \;}
    $mbar.audio add command -label {MIDI settings...} \
	-command {pd pd midi-properties \;}
	 }
	 
    $mbar.audio add command -label {Test Audio and MIDI} \
	-command {menu_doc_open doc/7.stuff/tools testtone.pd} 
    $mbar.audio add command -label {Load Meter} \
	-command {menu_doc_open doc/7.stuff/tools load-meter.pd} 

#       the MacOS X app menu

# The menu on the main menubar named $whatever.apple while be treated
# as a special menu on MacOS X.  Tcl/Tk assigns the $whatever.apple menu
# to the app-specific menu in MacOS X that is named after the app,
# so in our case, the Pd menu.  <hans@at.or.at>
# See SPECIAL MENUS IN MENUBARS http://www.tcl.tk/man/tcl8.4/TkCmd/menu.htm
	 if {$pd_nt == 2} {
		  $mbar.apple add command -label "About Pd..." -command \
				{menu_doc_open doc/1.manual 1.introduction.txt} 
		  menu $mbar.apple.preferences -tearoff 0
		  $mbar.apple add cascade -label "Preferences" -menu $mbar.apple.preferences
		  $mbar.apple.preferences add command -label "Path..." \
				-command {pd pd start-path-dialog \;}
		  $mbar.apple.preferences add command -label "Startup..." \
				-command {pd pd start-startup-dialog \;}
		  $mbar.apple.preferences add command -label "Audio Settings..." \
				-command {pd pd audio-properties \;}
		  $mbar.apple.preferences add command -label "MIDI settings..." \
				-command {pd pd midi-properties \;}
	 }


    	# the "Help" menu
    if {$pd_nt != 2} {
    	$mbar.help add command -label {About Pd} \
	    -command {menu_doc_open doc/1.manual 1.introduction.txt} 
    }
    $mbar.help add command -label {Html ...} \
    	-command {menu_doc_open doc/1.manual index.htm} 
    $mbar.help add command -label {Browser ...} \
    	-command {menu_documentation} 
}

#################### the "File" menu for the Pd window ##############

.mbar.file add command -label New -command {menu_new} \
    -accelerator [accel_munge "Ctrl+n"]
.mbar.file add command -label Open -command {menu_open} \
    -accelerator [accel_munge "Ctrl+o"]
.mbar.file add  separator
.mbar.file add command -label Message -command {menu_send} \
    -accelerator [accel_munge "Ctrl+m"]
# On MacOS X, these are in the standard HIG locations
# i.e. the Preferences menu under "Pd"
if {$pd_nt != 2} {
.mbar.file add command -label Path... \
    -command {pd pd start-path-dialog \;}
.mbar.file add command -label Startup... \
    -command {pd pd start-startup-dialog \;}
}
.mbar.file add  separator
.mbar.file add command -label Quit -command {menu_quit} \
    -accelerator [accel_munge "Ctrl+q"]

#################### the "Find" menu for the Pd window ##############
.mbar.find add command -label {Find last error} -command {menu_finderror} 

###########  functions for menu functions on document windows ########

proc menu_save {name} {
    pdtk_canvas_checkgeometry $name
    pd [concat $name menusave \;]
}

proc menu_saveas {name} {
    pdtk_canvas_checkgeometry $name
    pd [concat $name menusaveas \;]
}

proc menu_print {name} {
    set filename [tk_getSaveFile -initialfile pd.ps \
       -defaultextension .ps \
       -filetypes { {{postscript} {.ps}} }]

    if {$filename != ""} {
    	$name.c postscript -file $filename
    }
}

proc menu_close {name} {
    pdtk_canvas_checkgeometry $name
    pd [concat $name menuclose \;]
}

proc menu_undo {name} {
    global pd_undoaction
    global pd_redoaction
    global pd_undocanvas
    if {$name == $pd_undocanvas && $pd_undoaction != "no"} {
    	pd [concat $name undo \;]
    }
}

proc menu_redo {name} {
    global pd_undoaction
    global pd_redoaction
    global pd_undocanvas
    if {$name == $pd_undocanvas && $pd_redoaction != "no"} {
    	pd [concat $name redo \;]
    }
}

proc menu_cut {name} {
    pd [concat $name cut \;]
}

proc menu_copy {name} {
    pd [concat $name copy \;]
}

proc menu_paste {name} {
    pd [concat $name paste \;]
}

proc menu_duplicate {name} {
    pd [concat $name duplicate \;]
}

proc menu_selectall {name} {
    pd [concat $name selectall \;]
}

proc menu_texteditor {name} {
    pd [concat $name texteditor \;]
}

proc menu_font {name} {
    pd [concat $name menufont \;]
}

proc menu_tidyup {name} {
    pd [concat $name tidy \;]
}

proc menu_editmode {name} {
    pd [concat $name editmode 0 \;]
}

proc menu_object {name accel} {
    pd [concat $name obj $accel \;]
}

proc menu_message {name accel} {
    pd [concat $name msg $accel \;]
}

proc menu_floatatom {name accel} {
    pd [concat $name floatatom $accel \;]
}

proc menu_symbolatom {name accel} {
    pd [concat $name symbolatom $accel \;]
}

proc menu_comment {name accel} {
    pd [concat $name text $accel \;]
}

proc menu_graph {name} {
    pd [concat $name graph \;]
}

proc menu_array {name} {
    pd [concat $name menuarray \;]
}

############iemlib##################
proc menu_bng {name accel} {
    pd [concat $name bng $accel \;]
}

proc menu_toggle {name accel} {
    pd [concat $name toggle $accel \;]
}

proc menu_numbox {name accel} {
    pd [concat $name numbox $accel \;]
}

proc menu_vslider {name accel} {
    pd [concat $name vslider $accel \;]
}

proc menu_hslider {name accel} {
    pd [concat $name hslider $accel \;]
}

proc menu_hradio {name accel} {
    pd [concat $name hradio $accel \;]
}

proc menu_vradio {name accel} {
    pd [concat $name vradio $accel \;]
}

proc menu_vumeter {name accel} {
    pd [concat $name vumeter $accel \;]
}

proc menu_mycnv {name accel} {
    pd [concat $name mycnv $accel \;]
}

############iemlib##################

# correct edit menu, enabling or disabling undo/redo
# LATER also cut/copy/paste
proc menu_fixeditmenu {name} {
    global pd_undoaction
    global pd_redoaction
    global pd_undocanvas
#    puts stderr [concat menu_fixeditmenu $name $pd_undocanvas $pd_undoaction]
    if {$name == $pd_undocanvas && $pd_undoaction != "no"} {
	$name.m.edit entryconfigure "Undo*" -state normal \
	    -label [concat "Undo " $pd_undoaction]
    } else {
	$name.m.edit entryconfigure "Undo*" -state disabled -label "Undo"
    }
    if {$name == $pd_undocanvas && $pd_redoaction != "no"} {
	$name.m.edit entryconfigure "Redo*" -state normal\
            -label [concat "Redo " $pd_redoaction]
    } else {
	$name.m.edit entryconfigure "Redo*" -state disabled
    }
}

# message from Pd to update the currently available undo/redo action
proc pdtk_undomenu {name undoaction redoaction} {
    global pd_undoaction
    global pd_redoaction
    global pd_undocanvas
#    puts stderr [concat pdtk_undomenu $name $undoaction $redoaction]
    set pd_undocanvas $name
    set pd_undoaction $undoaction
    set pd_redoaction $redoaction
    if {$name != "nobody"} {
#    unpleasant way of avoiding a more unpleasant bug situation --atl 2002.11.25
        menu_fixeditmenu $name
    }
}

proc menu_windowparent {name} {
    pd [concat $name findparent \;]
}

proc menu_findagain {name} {
    pd [concat $name findagain \;]
}

proc menu_finderror {} {
    pd [concat pd finderror \;]
}

proc menu_domenuwindow {i} {
    raise $i
}

proc menu_fixwindowmenu {name} {
    global menu_windowlist
    global pd_tearoff
    $name.m.windows add command
    if $pd_tearoff {
	$name.m.windows delete 4 end
    } else {
	$name.m.windows delete 3 end
    }
    foreach i $menu_windowlist {
	$name.m.windows add command -label [lindex $i 0] \
	    -command [concat menu_domenuwindow [lindex $i 1]]
    }
}

################## the "find" menu item ###################

set find_canvas nobody
set find_string ""
set find_count 1

proc find_apply {name} {
    global find_string
    global find_canvas
    regsub -all \; $find_string " _semi_ " find_string2
    regsub -all \, $find_string2 " _comma_ " find_string3
#    puts stderr [concat $find_canvas find $find_string3 \
#	\;]
    pd [concat $find_canvas find $find_string3 \
	\;]
    after 50 destroy $name
}

proc find_cancel {name} {
    after 50 destroy $name
}

proc menu_findobject {canvas} {
    global find_string
    global find_canvas
    global find_count
    
    set name [format ".find%d" $find_count]
    set find_count [expr $find_count + 1]

    set find_canvas $canvas
    
    toplevel $name

    label $name.label -text {find...}
    pack $name.label -side top

    entry $name.entry -textvariable find_string
    pack $name.entry -side top

    frame $name.buttonframe
    pack $name.buttonframe -side bottom -fill x -pady 2m
    button $name.buttonframe.cancel -text {Cancel}\
    	-command "find_cancel $name"
    button $name.buttonframe.ok -text {OK}\
    	-command "find_apply $name"
    pack $name.buttonframe.cancel -side left -expand 1
    pack $name.buttonframe.ok -side left -expand 1
    
    $name.entry select from 0
    $name.entry select adjust end
    bind $name.entry <KeyPress-Return> [ concat find_apply $name]
    pdtk_standardkeybindings $name.entry
    focus $name.entry
}


############# pdtk_canvas_new -- create a new canvas ###############
proc pdtk_canvas_new {name width height geometry editable} {
    global pd_opendir
    global pd_tearoff
    global pd_nt

    toplevel $name -menu $name.m

# slide offscreen windows into view
    set geometry [split $geometry +]
    set i 1
    foreach geo {width height} {
	set screen($geo) [winfo screen$geo .]
	if {[expr [lindex $geometry $i] + [set $geo]] > $screen($geo)} {
	    set pos($geo) [expr $screen($geo) - [set $geo]]
	    if {$pos($geo) < 0} {set pos($geo) 0}
	    lset geometry $i $pos($geo)
	}
	incr i
    }
    set geometry [join $geometry +] 

   wm geometry $name $geometry
    canvas $name.c -width $width -height $height -background white \
    	-yscrollcommand "$name.scrollvert set" \
    	-xscrollcommand "$name.scrollhort set" \
	-scrollregion [concat 0 0 $width $height] 

    scrollbar $name.scrollvert -command "$name.c yview"
    scrollbar $name.scrollhort -command "$name.c xview" \
    	-orient horizontal

    pack $name.scrollhort -side bottom -fill x
    pack $name.scrollvert -side right -fill y
    pack $name.c -side left -expand 1 -fill both
    wm minsize $name 1 1
    wm geometry $name $geometry
# the file menu

# The menus are instantiated here for the patch windows.
# For the main window, they are created on load, at the 
# top of this file.
    menu $name.m
    menu $name.m.file -tearoff $pd_tearoff
    $name.m add cascade -label File -menu $name.m.file

    $name.m.file add command -label New -command {menu_new} \
	-accelerator [accel_munge "Ctrl+n"]

    $name.m.file add command -label Open -command {menu_open} \
	-accelerator [accel_munge "Ctrl+o"]

    $name.m.file add  separator
    $name.m.file add command -label Message -command {menu_send} \
	-accelerator [accel_munge "Ctrl+m"]

	 # arrange menus according to Apple HIG
	 # these are now part of Preferences...
	 if {$pd_nt != 2 } {
    $name.m.file add command -label Path... \
    	-command {pd pd start-path-dialog \;} 

    $name.m.file add command -label Startup... \
    	-command {pd pd start-startup-dialog \;} 
	 }

    $name.m.file add  separator
    $name.m.file add command -label Close \
    	-command [concat menu_close $name] \
	-accelerator [accel_munge "Ctrl+w"]

    $name.m.file add command -label Save -command [concat menu_save $name] \
	-accelerator [accel_munge "Ctrl+s"]

    $name.m.file add command -label "Save as..." \
    	-command [concat menu_saveas $name] \
	-accelerator [accel_munge "Ctrl+S"]

    $name.m.file add command -label Print -command [concat menu_print $name] \
	-accelerator [accel_munge "Ctrl+p"]

    $name.m.file add separator

    $name.m.file add command -label Quit -command {menu_quit} \
	-accelerator [accel_munge "Ctrl+q"]

# the edit menu
    menu $name.m.edit -postcommand [concat menu_fixeditmenu $name] -tearoff $pd_tearoff
    $name.m add cascade -label Edit -menu $name.m.edit
    
    $name.m.edit add command -label Undo -command [concat menu_undo $name] \
	-accelerator [accel_munge "Ctrl+z"]

    $name.m.edit add command -label Redo -command [concat menu_redo $name] \
	-accelerator [accel_munge "Ctrl+Z"]

    $name.m.edit add separator

    $name.m.edit add command -label Cut -command [concat menu_cut $name] \
	-accelerator [accel_munge "Ctrl+x"]

    $name.m.edit add command -label Copy -command [concat menu_copy $name] \
	-accelerator [accel_munge "Ctrl+c"]

    $name.m.edit add command -label Paste \
    	-command [concat menu_paste $name] \
	-accelerator [accel_munge "Ctrl+v"]

    $name.m.edit add command -label Duplicate \
    	-command [concat menu_duplicate $name] \
	-accelerator [accel_munge "Ctrl+d"]

    $name.m.edit add command -label {Select all} \
    	-command [concat menu_selectall $name] \
	-accelerator [accel_munge "Ctrl+a"]

    $name.m.edit add separator

    $name.m.edit add command -label {Text Editor} \
    	-command [concat menu_texteditor $name] \
	-accelerator [accel_munge "Ctrl+t"]

    $name.m.edit add command -label Font \
    	-command [concat menu_font $name] 

    $name.m.edit add command -label {Tidy Up} \
    	-command [concat menu_tidyup $name]

    $name.m.edit add separator
    
# Apple, Microsoft, and others put find functions in the Edit menu.
    $name.m.edit add command -label {Find...} \
		  -accelerator [accel_munge "Ctrl+f"] \
		  -command [concat menu_findobject $name] 
    $name.m.edit add command -label {Find Again} \
		  -accelerator [accel_munge "Ctrl+g"] \
		  -command [concat menu_findagain $name] 
    $name.m.edit add command -label {Find last error} \
		  -command [concat menu_finderror] 

    $name.m.edit add separator

############iemlib##################
# instead of "red = #BC3C60" we take "grey85", so there is no difference,
# if widget is selected or not.

    $name.m.edit add checkbutton -label "Edit mode" \
    	-indicatoron true -selectcolor grey85 \
    	-command [concat menu_editmode $name] \
	-accelerator [accel_munge "Ctrl+e"]	

    if { $editable == 0 } {
            $name.m.edit entryconfigure "Edit mode" -indicatoron false }

	
############iemlib##################


# the put menu
    menu $name.m.put -tearoff $pd_tearoff
    $name.m add cascade -label Put -menu $name.m.put

    $name.m.put add command -label Object \
    	-command [concat menu_object $name 0] \
	-accelerator [accel_munge "Ctrl+1"]

    $name.m.put add command -label Message \
    	-command [concat menu_message $name 0] \
	-accelerator [accel_munge "Ctrl+2"]

    $name.m.put add command -label Number \
    	-command [concat menu_floatatom $name 0] \
	-accelerator [accel_munge "Ctrl+3"]

    $name.m.put add command -label Symbol \
    	-command [concat menu_symbolatom $name 0] \
	-accelerator [accel_munge "Ctrl+4"]

    $name.m.put add command -label Comment \
    	-command [concat menu_comment $name 0] \
	-accelerator [accel_munge "Ctrl+5"]

    $name.m.put add separator
	
############iemlib##################

    $name.m.put add command -label Bang \
    	-command [concat menu_bng $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+b"]
    
    $name.m.put add command -label Toggle \
    	-command [concat menu_toggle $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+t"]
    
    $name.m.put add command -label Number2 \
    	-command [concat menu_numbox $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+n"]
    
    $name.m.put add command -label Vslider \
    	-command [concat menu_vslider $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+v"]
    
    $name.m.put add command -label Hslider \
    	-command [concat menu_hslider $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+h"]
    
    $name.m.put add command -label Vradio \
    	-command [concat menu_vradio $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+d"]
    
    $name.m.put add command -label Hradio \
    	-command [concat menu_hradio $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+i"]
    
    $name.m.put add command -label VU \
    	-command [concat menu_vumeter $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+u"]
    
    $name.m.put add command -label Canvas \
    	-command [concat menu_mycnv $name 0] \
	-accelerator [accel_munge "Shift+Ctrl+c"]

############iemlib##################
    
    $name.m.put add separator
	
    $name.m.put add command -label Graph \
    	-command [concat menu_graph $name] 

    $name.m.put add command -label Array \
    	-command [concat menu_array $name] 

# the find menu
# Apple, Microsoft, and others put find functions in the Edit menu.
# But in order to move these items to the Edit menu, the Find menu
# handling needs to be dealt with, including this line in g_canvas.c:
#         sys_vgui(".mbar.find delete %d\n", i);
# <hans@at.or.at>
    menu $name.m.find -tearoff $pd_tearoff
    $name.m add cascade -label Find -menu $name.m.find

    $name.m.find add command -label {Find...} \
	-accelerator [accel_munge "Ctrl+f"] \
    	-command [concat menu_findobject $name] 
    $name.m.find add command -label {Find Again} \
	-accelerator [accel_munge "Ctrl+g"] \
    	-command [concat menu_findagain $name] 
    $name.m.find add command -label {Find last error} \
    	-command [concat menu_finderror] 
    
# the window menu
    menu $name.m.windows -postcommand [concat menu_fixwindowmenu $name] \
	-tearoff $pd_tearoff

    $name.m.windows add command -label {parent window}\
    	-command [concat menu_windowparent $name] 
    $name.m.windows add command -label {Pd window} -command menu_pop_pd
    $name.m.windows add separator

# the audio menu
    menu $name.m.audio -tearoff $pd_tearoff

    if {$pd_nt != 2} {
        $name.m add cascade -label Windows -menu $name.m.windows
        $name.m add cascade -label Media -menu $name.m.audio
    } else {
        $name.m add cascade -label Media -menu $name.m.audio
        $name.m add cascade -label Window -menu $name.m.windows
# the MacOS X app menu
		  menu $name.m.apple -tearoff $pd_tearoff
		  $name.m add cascade -label "Apple" -menu $name.m.apple 
    }

# the help menu

    menu $name.m.help -tearoff $pd_tearoff
    $name.m add cascade -label Help -menu $name.m.help

    menu_addstd $name.m

# the popup menu
    menu $name.popup -tearoff false
    $name.popup add command -label {Properties} \
	-command [concat popup_action $name 0] 
    $name.popup add command -label {Open} \
	-command [concat popup_action $name 1] 
    $name.popup add command -label {Help} \
	-command [concat popup_action $name 2] 

# WM protocol
    wm protocol $name WM_DELETE_WINDOW [concat menu_close $name]

# bindings.
# this is idiotic -- how do you just sense what mod keys are down and
# pass them on? I can't find it anywhere.
# Here we encode shift as 1, control 2, alt 4, in agreement
# with definitions in g_canvas.c.  The third button gets "8" but we don't
# bother with modifiers there.
# We don't handle multiple clicks yet.

    bind $name.c <Button> {pdtk_canvas_click %W %x %y %b 0}
    bind $name.c <Shift-Button> {pdtk_canvas_click %W %x %y %b 1}
    bind $name.c <Control-Shift-Button> {pdtk_canvas_click %W %x %y %b 3}
    # Alt key is called Option on the Mac
    if {$pd_nt == 2} {
    	bind $name.c <Option-Button> {pdtk_canvas_click %W %x %y %b 4}
    	bind $name.c <Option-Shift-Button> {pdtk_canvas_click %W %x %y %b 5}
    	bind $name.c <Option-Control-Button> {pdtk_canvas_click %W %x %y %b 6}
    	bind $name.c <Option-Control-Shift-Button> \
	    {pdtk_canvas_click %W %x %y %b 7}
    } else {
    	bind $name.c <Alt-Button> {pdtk_canvas_click %W %x %y %b 4}
    	bind $name.c <Alt-Shift-Button> {pdtk_canvas_click %W %x %y %b 5}
    	bind $name.c <Alt-Control-Button> {pdtk_canvas_click %W %x %y %b 6}
    	bind $name.c <Alt-Control-Shift-Button> \
	    {pdtk_canvas_click %W %x %y %b 7}
    }
    global pd_nt
# button 2 is the right button on Mac; on other platforms it's button 3.
    if {$pd_nt == 2} {
        bind $name.c <Button-2> {pdtk_canvas_click %W %x %y %b 8}
        bind $name.c <Control-Button> {pdtk_canvas_click %W %x %y %b 8}
    } else {
        bind $name.c <Button-3> {pdtk_canvas_click %W %x %y %b 8}
        bind $name.c <Control-Button> {pdtk_canvas_click %W %x %y %b 2}
    }
#on linux, button 2 "pastes" from the X windows clipboard
    if {$pd_nt == 0} {
    	bind $name.c <Button-2> {\
	    pdtk_canvas_click %W %x %y %b 0;\
	     pdtk_canvas_mouseup %W %x %y %b;\
	     pdtk_pastetext}
    }

    bind $name.c <ButtonRelease> {pdtk_canvas_mouseup %W %x %y %b}
    bind $name.c <Control-Key> {pdtk_canvas_ctrlkey %W %K 0}
    bind $name.c <Control-Shift-Key> {pdtk_canvas_ctrlkey %W %K 1}
#    bind $name.c <Mod1-Key> {puts stderr [concat mod1 %W %K %A]}
    if {$pd_nt == 2} {
	bind $name.c <Mod1-Key> {pdtk_canvas_ctrlkey %W %K 0}
	bind $name.c <Mod1-Shift-Key> {pdtk_canvas_ctrlkey %W %K 1}
    }
    bind $name.c <Key> {pdtk_canvas_key %W %K %A 0}
    bind $name.c <Shift-Key> {pdtk_canvas_key %W %K %A 1}
    bind $name.c <KeyRelease> {pdtk_canvas_keyup %W %K %A}
    bind $name.c <Motion> {pdtk_canvas_motion %W %x %y 0}
    bind $name.c <Control-Motion> {pdtk_canvas_motion %W %x %y 2}
    if {$pd_nt == 2} {
        bind $name.c <Option-Motion> {pdtk_canvas_motion %W %x %y 4}
    } else { 
        bind $name.c <Alt-Motion> {pdtk_canvas_motion %W %x %y 4}
    }   
    bind $name.c <Map> {pdtk_canvas_map %W}
    bind $name.c <Unmap> {pdtk_canvas_unmap %W}
    focus $name.c

    switch $pd_nt { 0 {
	bind $name.c <Button-4>  "pdtk_canvas_scroll $name.c y -1"
    	bind $name.c <Button-5>  "pdtk_canvas_scroll $name.c y +1"
	bind $name.c <Shift-Button-4>  "pdtk_canvas_scroll $name.c x -1"
    	bind $name.c <Shift-Button-5>  "pdtk_canvas_scroll $name.c x +1"
    } default {
	bind $name.c  <MouseWheel> \
	    "pdtk_canvas_scroll $name.c y \[expr -abs(%D)/%D\]"
	bind $name.c  <Shift-MouseWheel> \
	    "pdtk_canvas_scroll $name.c x \[expr -abs(%D)/%D\]"
    }}

    catch {
	dnd bindtarget $name.c text/uri-list <Drop> \
	    "pdtk_canvas_makeobjs $name %D %x %y"
    }

#    puts stderr "all done"
#   after 1 [concat raise $name]
    global pdtk_canvas_mouseup_name
    set pdtk_canvas_mouseup_name ""
}

#### jsarlo #####
proc pdtk_array_listview_setpage {arrayName page} {
    global pd_array_listview_page
    set pd_array_listview_page($arrayName) $page
}

proc pdtk_array_listview_changepage {arrayName np} {
    global pd_array_listview_page
    pdtk_array_listview_setpage \
      $arrayName [expr $pd_array_listview_page($arrayName) + $np]
    pdtk_array_listview_fillpage $arrayName
}

proc pdtk_array_listview_fillpage {arrayName} {
    global pd_array_listview_page
    global pd_array_listview_id
    set windowName [format ".%sArrayWindow" $arrayName]
    if {[winfo exists $windowName]} {
      set cmd "$pd_array_listview_id($arrayName) \
               arrayviewlistfillpage \
               $pd_array_listview_page($arrayName)"
      pd [concat $cmd \;]
    }
}

proc pdtk_array_listview_new {id arrayName page} {
    global pd_nt
    global pd_array_listview_page
    global pd_array_listview_id
    set pd_array_listview_page($arrayName) $page
    set pd_array_listview_id($arrayName) $id
    set windowName [format ".%sArrayWindow" $arrayName]
    if [winfo exists $windowName] then [destroy $windowName]
    toplevel $windowName
    wm protocol $windowName WM_DELETE_WINDOW \
      "pdtk_array_listview_close $id $arrayName"
    wm title $windowName [concat $arrayName "(list view)"]
    # FIXME
    set font 12
    set $windowName.lb [listbox $windowName.lb -height 20 -width 25\
                        -selectmode extended \
                        -relief solid -background white -borderwidth 1 \
                        -font [format -*-courier-bold--normal--%d-* \
                               $font] \
                        -yscrollcommand "$windowName.lb.sb set"]
    set $windowName.lb.sb [scrollbar $windowName.lb.sb \
                           -command "$windowName.lb yview" -orient vertical]
    place configure $windowName.lb.sb -relheight 1 -relx 0.9 -relwidth 0.1
    pack $windowName.lb
    bind $windowName.lb <Double-ButtonPress-1> \
         "pdtk_array_listview_edit $arrayName $page $font"
    # handle copy/paste
    if {$pd_nt == 0} {
      selection handle $windowName.lb \
            "pdtk_array_listview_lbselection $arrayName"
    } else {
      if {$pd_nt == 1} {
        bind $windowName.lb <ButtonPress-3> \
           "pdtk_array_listview_popup $arrayName"
      } 
    }
    set $windowName.prevBtn [button $windowName.prevBtn -text "<-" \
        -command "pdtk_array_listview_changepage $arrayName -1"]
    set $windowName.nextBtn [button $windowName.nextBtn -text "->" \
        -command "pdtk_array_listview_changepage $arrayName 1"]
    pack $windowName.prevBtn -side left -ipadx 20 -pady 10
    pack $windowName.nextBtn -side right -ipadx 20 -pady 10
    focus $windowName
}

proc pdtk_array_listview_lbselection {arrayName off size} {
    set windowName [format ".%sArrayWindow" $arrayName]
    set itemNums [$windowName.lb curselection]
    set cbString ""
    for {set i 0} {$i < [expr [llength $itemNums] - 1]} {incr i} {
      set listItem [$windowName.lb get [lindex $itemNums $i]]
      append cbString [string range $listItem \
                        [expr [string first ") " $listItem] + 2] \
                        end]
      append cbString "\n"
    }
    set listItem [$windowName.lb get [lindex $itemNums $i]]
    append cbString [string range $listItem \
                      [expr [string first ") " $listItem] + 2] \
                      end]
    set last $cbString
}

# Win32 uses a popup menu for copy/paste
proc pdtk_array_listview_popup {arrayName} {
    set windowName [format ".%sArrayWindow" $arrayName]
    if [winfo exists $windowName.popup] then [destroy $windowName.popup]
    menu $windowName.popup -tearoff false
    $windowName.popup add command -label {Copy} \
        -command "pdtk_array_listview_copy $arrayName; \
                  destroy $windowName.popup"
    $windowName.popup add command -label {Paste} \
        -command "pdtk_array_listview_paste $arrayName; \
                  destroy $windowName.popup"
    tk_popup $windowName.popup [winfo pointerx $windowName] \
             [winfo pointery $windowName] 0
}

proc pdtk_array_listview_copy {arrayName} {
    set windowName [format ".%sArrayWindow" $arrayName]
    set itemNums [$windowName.lb curselection]
    set cbString ""
    for {set i 0} {$i < [expr [llength $itemNums] - 1]} {incr i} {
      set listItem [$windowName.lb get [lindex $itemNums $i]]
      append cbString [string range $listItem \
                        [expr [string first ") " $listItem] + 2] \
                        end]
      append cbString "\n"
    }
    set listItem [$windowName.lb get [lindex $itemNums $i]]
    append cbString [string range $listItem \
                      [expr [string first ") " $listItem] + 2] \
                      end]
    clipboard clear
    clipboard append $cbString
}

proc pdtk_array_listview_paste {arrayName} {
    global pd_array_listview_page
    global pd_array_listview_pagesize
    set cbString [selection get -selection CLIPBOARD]
    set lbName [format ".%sArrayWindow.lb" $arrayName]
    set itemNum [lindex [$lbName curselection] 0]
    set splitChars ", \n"
    set itemString [split $cbString $splitChars]
    set flag 1
    for {set i 0; set counter 0} {$i < [llength $itemString]} {incr i} {
      if {[lindex $itemString $i] != {}} {
        pd [concat $arrayName [expr $itemNum + \
           [expr $counter + \
           [expr $pd_array_listview_pagesize \
                 * $pd_array_listview_page($arrayName)]]] \
           [lindex $itemString $i] \;]
        incr counter
        set flag 0
      }
    }
}

proc pdtk_array_listview_edit {arrayName page font} {
    global pd_array_listview_entry
    global pd_nt
    if {$pd_nt == 0} {
      set font [expr $font - 2]
    }
    set lbName [format ".%sArrayWindow.lb" $arrayName]
    if {[winfo exists $lbName.entry]} {
      pdtk_array_listview_update_entry \
        $arrayName $pd_array_listview_entry($arrayName)
      unset pd_array_listview_entry($arrayName)
    }
    set itemNum [$lbName index active]
    set pd_array_listview_entry($arrayName) $itemNum
    set bbox [$lbName bbox $itemNum]
    set y [expr [lindex $bbox 1] - 4]
    set $lbName.entry [entry $lbName.entry \
                       -font [format -*-courier-bold--normal--%d-* $font]]
    $lbName.entry insert 0 []
    place configure $lbName.entry -relx 0 -y $y -relwidth 1
    lower $lbName.entry
    focus $lbName.entry
    bind $lbName.entry <Return> \
         "pdtk_array_listview_update_entry $arrayName $itemNum;"
}

proc pdtk_array_listview_update_entry {arrayName itemNum} {
    global pd_array_listview_page
    global pd_array_listview_pagesize
    set lbName [format ".%sArrayWindow.lb" $arrayName]
    set splitChars ", \n"
    set itemString [split [$lbName.entry get] $splitChars]
    set flag 1
    for {set i 0; set counter 0} {$i < [llength $itemString]} {incr i} {
      if {[lindex $itemString $i] != {}} {
        pd [concat $arrayName [expr $itemNum + \
           [expr $counter + \
           [expr $pd_array_listview_pagesize \
                 * $pd_array_listview_page($arrayName)]]] \
           [lindex $itemString $i] \;]
        incr counter
        set flag 0
      }
    }
    pdtk_array_listview_fillpage $arrayName
    destroy $lbName.entry
}

proc pdtk_array_listview_closeWindow {arrayName} {
    set windowName [format ".%sArrayWindow" $arrayName]
    destroy $windowName
}

proc pdtk_array_listview_close {id arrayName} {
    pdtk_array_listview_closeWindow $arrayName
    set cmd [concat $id "arrayviewclose" \;]
    pd $cmd
}
##### end jsarlo #####

#################### event binding procedures ################

#get the name of the toplevel window for a canvas; this is also
#the name of the canvas object in Pd.

proc canvastosym {name} {
    string range $name 0 [expr [string length $name] - 3]
}

set pdtk_lastcanvasconfigured ""
set pdtk_lastcanvasconfiguration ""

proc pdtk_canvas_checkgeometry {topname} {
    set boo [winfo geometry $topname.c]
    set boo2 [wm geometry $topname]
    global pdtk_lastcanvasconfigured
    global pdtk_lastcanvasconfiguration
    if {$topname != $pdtk_lastcanvasconfigured || \
    	$boo != $pdtk_lastcanvasconfiguration} {
    	    set pdtk_lastcanvasconfigured $topname
    	    set pdtk_lastcanvasconfiguration $boo
    	    pd $topname relocate $boo $boo2 \;
    }
}

proc pdtk_canvas_click {name x y b f} {
    global pd_nt
    if {$pd_nt == 0} {focus $name}
    pd [canvastosym $name] mouse [$name canvasx $x] [$name canvasy $y] $b $f \;
}

proc pdtk_canvas_shiftclick {name x y b} {
    pd [canvastosym $name] mouse [$name canvasx $x] [$name canvasy $y] $b 1 \;
}

proc pdtk_canvas_ctrlclick {name x y b} {
    pd [canvastosym $name] mouse [$name canvasx $x] [$name canvasy $y] $b 2 \;
}

proc pdtk_canvas_altclick {name x y b} {
    pd [canvastosym $name] mouse [$name canvasx $x] [$name canvasy $y] $b 3 \;
}

proc pdtk_canvas_dblclick {name x y b} {
    pd [canvastosym $name] mouse [$name canvasx $x] [$name canvasy $y] $b 4 \;
}

set pdtk_canvas_mouseup_name 0
set pdtk_canvas_mouseup_xminval 0
set pdtk_canvas_mouseup_xmaxval 0
set pdtk_canvas_mouseup_yminval 0
set pdtk_canvas_mouseup_ymaxval 0

proc pdtk_canvas_mouseup {name x y b} {
    pd [concat [canvastosym $name] mouseup [$name canvasx $x] \
    	[$name canvasy $y] $b \;]
}

proc pdtk_canvas_getscroll {name} {
    global pdtk_canvas_mouseup_name
    global pdtk_canvas_mouseup_xminval
    global pdtk_canvas_mouseup_xmaxval
    global pdtk_canvas_mouseup_yminval
    global pdtk_canvas_mouseup_ymaxval

    set size [$name bbox all]
    if {$size != ""} {
    	set xminval 0
    	set yminval 0
    	set xmaxval 100
    	set ymaxval 100
    	set x1 [lindex $size 0]
    	set x2 [lindex $size 2]
    	set y1 [lindex $size 1]
    	set y2 [lindex $size 3]
    	
    	if {$x1 < 0} {set xminval $x1}
    	if {$y1 < 0} {set yminval $y1}

    	if {$x2 > 100} {set xmaxval $x2}
    	if {$y2 > 100} {set ymaxval $y2}
    	
    	if {$pdtk_canvas_mouseup_name != $name || \
    	    $pdtk_canvas_mouseup_xminval != $xminval || \
    	    $pdtk_canvas_mouseup_xmaxval != $xmaxval || \
    	    $pdtk_canvas_mouseup_yminval != $yminval || \
    	    $pdtk_canvas_mouseup_ymaxval != $ymaxval } {
    	    
	    	set newsize "$xminval $yminval $xmaxval $ymaxval"
	    	$name configure -scrollregion $newsize
	    	set pdtk_canvas_mouseup_name $name
	    	set pdtk_canvas_mouseup_xminval $xminval
	    	set pdtk_canvas_mouseup_xmaxval $xmaxval
	    	set pdtk_canvas_mouseup_yminval $yminval
	    	set pdtk_canvas_mouseup_ymaxval $ymaxval
    	}

    }
    pdtk_canvas_checkgeometry [canvastosym $name]
}

proc pdtk_canvas_key {name key iso shift} {
#    puts stderr [concat down key= $key iso= $iso]
#    .controls.switches.meterbutton configure -text $key
#  HACK for MAC OSX -- backspace seems different; I don't understand why.
#  invesigate this LATER...
    global pd_nt
    if {$pd_nt == 2} {
	if {$key == "BackSpace"} {
	    set key 8
	    set keynum 8
	}
	if {$key == "Delete"} {
	    set key 8
	    set keynum 8
	}
    }
    if {$key == "KP_Delete"} {
	set key 127
	set keynum 127
    }
    if {$iso != ""} {
    	scan $iso %c keynum 
    	pd [canvastosym $name] key 1 $keynum $shift\;
    } else {
    	pd [canvastosym $name] key 1 $key $shift\;
    }
}

proc pdtk_canvas_keyup {name key iso} {
#    puts stderr [concat up key= $key iso= $iso]
    if {$iso != ""} {
    	scan $iso %c keynum 
    	pd [canvastosym $name] key 0 $keynum 0 \;
    } else {
    	pd [canvastosym $name] key 0 $key 0 \;
    }
}

proc pdtk_canvas_ctrlkey {name key shift} {
# first get rid of ".c" suffix; we'll refer to the toplevel instead
    set topname [string trimright $name .c]
#   puts stderr [concat ctrl-key $key $topname]

    if {$key == "1"} {menu_object $topname 1}
    if {$key == "2"} {menu_message $topname 1}
    if {$key == "3"} {menu_floatatom $topname 1}
    if {$key == "4"} {menu_symbolatom $topname 1}
    if {$key == "5"} {menu_comment $topname 1}
    if {$key == "slash"} {menu_audio 1}
    if {$key == "period"} {menu_audio 0}
    if {$shift == 1} {
        if {$key == "q" || $key == "Q"} {menu_really_quit}
        if {$key == "s" || $key == "S"} {menu_saveas $topname}
        if {$key == "z" || $key == "Z"} {menu_redo $topname}
        if {$key == "b" || $key == "B"} {menu_bng $topname 1}
        if {$key == "t" || $key == "T"} {menu_toggle $topname 1}
        if {$key == "n" || $key == "N"} {menu_numbox $topname 1}
        if {$key == "v" || $key == "V"} {menu_vslider $topname 1}
        if {$key == "h" || $key == "H"} {menu_hslider $topname 1}
        if {$key == "i" || $key == "I"} {menu_hradio $topname 1}
        if {$key == "d" || $key == "D"} {menu_vradio $topname 1}
        if {$key == "u" || $key == "U"} {menu_vumeter $topname 1}
        if {$key == "c" || $key == "C"} {menu_mycnv $topname 1}
    } else {
        if {$key == "e" || $key == "E"} {menu_editmode $topname}
        if {$key == "q" || $key == "Q"} {menu_quit}
        if {$key == "s" || $key == "S"} {menu_save $topname}
        if {$key == "z" || $key == "Z"} {menu_undo $topname}
        if {$key == "n" || $key == "N"} {menu_new}
        if {$key == "o" || $key == "O"} {menu_open}
        if {$key == "m" || $key == "M"} {menu_send}
        if {$key == "w" || $key == "W"} {menu_close $topname}
        if {$key == "p" || $key == "P"} {menu_print $topname}
        if {$key == "x" || $key == "X"} {menu_cut $topname}
        if {$key == "c" || $key == "C"} {menu_copy $topname}
        if {$key == "v" || $key == "V"} {menu_paste $topname}
        if {$key == "d" || $key == "D"} {menu_duplicate $topname}
        if {$key == "a" || $key == "A"} {menu_selectall $topname}
        if {$key == "t" || $key == "T"} {menu_texteditor $topname}
        if {$key == "f" || $key == "F"} {menu_findobject $topname}
        if {$key == "g" || $key == "G"} {menu_findagain $topname}
    }
}

proc pdtk_canvas_scroll {canvas xy distance} {
    $canvas [list $xy]view scroll $distance units
}

proc pdtk_canvas_motion {name x y mods} {
#    puts stderr [concat [canvastosym $name] $name $x $y]
    pd [canvastosym $name] motion [$name canvasx $x] [$name canvasy $y] $mods \;
}

# "map" event tells us when the canvas becomes visible (arg is "0") or
# invisible (arg is "").  Invisibility means the Window Manager has minimized
# us.  We don't get a final "unmap" event when we destroy the window.
proc pdtk_canvas_map {name} {
#   puts stderr [concat map $name]
    pd [canvastosym $name] map 1 \;
}

proc pdtk_canvas_unmap {name} {
#   puts stderr [concat unmap $name]
    pd [canvastosym $name] map 0 \;
}

proc pdtk_canvas_makeobjs {name files x y} {
    set c 0
    for {set n 0} {$n < [llength $files]} {incr n} {
	if {[regexp {.*/(.+).pd$} [lindex $files $n] file obj] == 1} {
	    pd $name obj $x [expr $y + ($c * 30)] [pdtk_enquote $obj] \;
	    incr c
	}
    } 
}

set saveas_dir nowhere

############ pdtk_canvas_saveas -- run a saveas dialog ##############

proc pdtk_canvas_saveas {name initfile initdir} {
    global pd_nt
    set filename [tk_getSaveFile -initialfile $initfile \
       -initialdir $initdir  -defaultextension .pd \
        -filetypes { {{pd files} {.pd}} {{max files} {.pat}} }]

    if {$filename != ""} {
# yes, we need the extent even if we're on a mac.
	if {$pd_nt == 2} {
	  if {[string last .pd $filename] < 0 && \
            [string last .PD $filename] < 0 && \
            [string last .pat $filename] < 0 && \
	    [string last .PAT $filename] < 0} {
		set filename $filename.pd
		if {[file exists $filename]} {
    			set answer [tk_messageBox \
			\-message [concat overwrite $filename "?"] \
			 \-type yesno \-icon question]
    			if {$answer == no} {return}
		}
	  }
	}

    	set directory [string range $filename 0 \
    	    [expr [string last / $filename ] - 1]]
    	set basename [string range $filename \
    	    [expr [string last / $filename ] + 1] end]
    	pd [concat $name savetofile [pdtk_enquote $basename] \
	     [pdtk_enquote $directory] \;]
#    	pd [concat $name savetofile $basename $directory \;]
    }
}

############ pdtk_canvas_dofont -- run a font and resize dialog #########

set fontsize 0
set stretchval 0
set whichstretch 0

proc dofont_apply {name} {
    global fontsize
    global stretchval
    global whichstretch
    set cmd [concat $name font $fontsize $stretchval $whichstretch \;]
#    puts stderr $cmd
    pd $cmd
}

proc dofont_cancel {name} {
    set cmd [concat $name cancel \;]
#    puts stderr $cmd
    pd $cmd
}

proc pdtk_canvas_dofont {name initsize} {
    
    global fontsize
    set fontsize $initsize
    
    global stretchval
    set stretchval 100
    
    global whichstretch
    set whichstretch 1
    
    toplevel $name
    wm title $name  {FONT BOMB}
    wm protocol $name WM_DELETE_WINDOW [concat dofont_cancel $name]

    frame $name.buttonframe
    pack $name.buttonframe -side bottom -fill x -pady 2m
    button $name.buttonframe.cancel -text {Cancel}\
    	-command "dofont_cancel $name"
    button $name.buttonframe.ok -text {Do it}\
    	-command "dofont_apply $name"
    pack $name.buttonframe.cancel -side left -expand 1
    pack $name.buttonframe.ok -side left -expand 1
    
    frame $name.radiof
    pack $name.radiof -side left
    
    label $name.radiof.label -text {Font Size:}
    pack $name.radiof.label -side top

    radiobutton $name.radiof.radio8 -value 8 -variable fontsize -text "8"
    radiobutton $name.radiof.radio10 -value 10 -variable fontsize -text "10"
    radiobutton $name.radiof.radio12 -value 12 -variable fontsize -text "12"
    radiobutton $name.radiof.radio16 -value 16 -variable fontsize -text "16"
    radiobutton $name.radiof.radio24 -value 24 -variable fontsize -text "24"
    radiobutton $name.radiof.radio36 -value 36 -variable fontsize -text "36"
    pack $name.radiof.radio8 -side top -anchor w
    pack $name.radiof.radio10 -side top -anchor w
    pack $name.radiof.radio12 -side top -anchor w
    pack $name.radiof.radio16 -side top -anchor w
    pack $name.radiof.radio24 -side top -anchor w
    pack $name.radiof.radio36 -side top -anchor w

    frame $name.stretchf
    pack $name.stretchf -side left
    
    label $name.stretchf.label -text {Stretch:}
    pack $name.stretchf.label -side top
    
    entry $name.stretchf.entry -textvariable stretchval -width 5
    pack $name.stretchf.entry -side left

    radiobutton $name.stretchf.radio1 \
    	-value 1 -variable whichstretch -text "X and Y"
    radiobutton $name.stretchf.radio2 \
    	-value 2 -variable whichstretch -text "X only"
    radiobutton $name.stretchf.radio3 \
    	-value 3 -variable whichstretch -text "Y only"

    pack $name.stretchf.radio1 -side top -anchor w
    pack $name.stretchf.radio2 -side top -anchor w
    pack $name.stretchf.radio3 -side top -anchor w

}

############ pdtk_gatom_dialog -- run a gatom dialog #########

# dialogs like this one can come up in many copies; but in TK the easiest
# way to get data from an "entry", etc., is to set an associated variable
# name.  This is especially true for grouped "radio buttons".  So we have
# to synthesize variable names for each instance of the dialog.  The dialog
# gets a TK pathname $id, from which it strips the leading "." to make a
# variable suffix $vid.  Then you can get the actual value out by asking for
# [eval concat $$variablename].  There should be an easier way but I don't see
# it yet.

proc gatom_escape {sym} {
    if {[string length $sym] == 0} {
    	set ret "-"
#	puts stderr [concat escape1 $sym $ret]
    } else {
    	if {[string equal -length 1 $sym "-"]} {
    	set ret [string replace $sym 0 0 "--"]
#	puts stderr [concat escape $sym $ret]
	} else {
    	    if {[string equal -length 1 $sym "$"]} {
    		set ret [string replace $sym 0 0 "#"]
#	    	puts stderr [concat unescape $sym $ret]
    	    } else {
    		set ret $sym
#	    	puts stderr [concat escape $sym "no change"]
    	    }
	}
    }
    pdtk_unspace $ret
}

proc gatom_unescape {sym} {
    if {[string equal -length 1 $sym "-"]} {
    	set ret [string replace $sym 0 0 ""]
#	puts stderr [concat unescape $sym $ret]
    } else {
    	if {[string equal -length 1 $sym "#"]} {
    	    set ret [string replace $sym 0 0 "$"]
#	    puts stderr [concat unescape $sym $ret]
    	} else {
    	    set ret $sym
#	    puts stderr [concat unescape $sym "no change"]
    	}
    }
    concat $ret
}
    	
proc dogatom_apply {id} {
    set vid [string trimleft $id .]

    set var_gatomwidth [concat gatomwidth_$vid]
    global $var_gatomwidth
    set var_gatomlo [concat gatomlo_$vid]
    global $var_gatomlo
    set var_gatomhi [concat gatomhi_$vid]
    global $var_gatomhi
    set var_gatomwherelabel [concat gatomwherelabel_$vid]
    global $var_gatomwherelabel
    set var_gatomlabel [concat gatomlabel_$vid]
    global $var_gatomlabel
    set var_gatomsymfrom [concat gatomsymfrom_$vid]
    global $var_gatomsymfrom
    set var_gatomsymto [concat gatomsymto_$vid]
    global $var_gatomsymto

#    set cmd [concat $id param $gatomwidth $gatomlo $gatomhi \;]
    
    set cmd [concat $id param \
    	[eval concat $$var_gatomwidth] \
    	[eval concat $$var_gatomlo] \
    	[eval concat $$var_gatomhi] \
    	[eval gatom_escape $$var_gatomlabel] \
    	[eval concat $$var_gatomwherelabel] \
    	[eval gatom_escape $$var_gatomsymfrom] \
    	[eval gatom_escape $$var_gatomsymto] \
	\;]

#    puts stderr $cmd
    pd $cmd
}

proc dogatom_cancel {name} {
    set cmd [concat $name cancel \;]
#    puts stderr $cmd
    pd $cmd
}

proc dogatom_ok {name} {
    dogatom_apply $name
    dogatom_cancel $name
}

proc pdtk_gatom_dialog {id initwidth initlo inithi \
    wherelabel label symfrom symto} {

    set vid [string trimleft $id .]

    set var_gatomwidth [concat gatomwidth_$vid]
    global $var_gatomwidth
    set var_gatomlo [concat gatomlo_$vid]
    global $var_gatomlo
    set var_gatomhi [concat gatomhi_$vid]
    global $var_gatomhi
    set var_gatomwherelabel [concat gatomwherelabel_$vid]
    global $var_gatomwherelabel
    set var_gatomlabel [concat gatomlabel_$vid]
    global $var_gatomlabel
    set var_gatomsymfrom [concat gatomsymfrom_$vid]
    global $var_gatomsymfrom
    set var_gatomsymto [concat gatomsymto_$vid]
    global $var_gatomsymto

    set $var_gatomwidth $initwidth
    set $var_gatomlo $initlo
    set $var_gatomhi $inithi
    set $var_gatomwherelabel $wherelabel
    set $var_gatomlabel [gatom_unescape $label]
    set $var_gatomsymfrom [gatom_unescape $symfrom]
    set $var_gatomsymto [gatom_unescape $symto]

    toplevel $id
    wm title $id {Atom}
    wm protocol $id WM_DELETE_WINDOW [concat dogatom_cancel $id]

    frame $id.buttonframe
    pack $id.buttonframe -side bottom -fill x -pady 2m
    button $id.buttonframe.cancel -text {Cancel}\
    	-command "dogatom_cancel $id"
    button $id.buttonframe.apply -text {Apply}\
    	-command "dogatom_apply $id"
    button $id.buttonframe.ok -text {OK}\
    	-command "dogatom_ok $id"
    pack $id.buttonframe.cancel -side left -expand 1
    pack $id.buttonframe.apply -side left -expand 1
    pack $id.buttonframe.ok -side left -expand 1

    frame $id.paramsymto
    pack $id.paramsymto -side bottom
    label $id.paramsymto.entryname -text {send symbol}
    entry $id.paramsymto.entry -textvariable $var_gatomsymto -width 20
    pack $id.paramsymto.entryname $id.paramsymto.entry -side left

    frame $id.paramsymfrom
    pack $id.paramsymfrom -side bottom
    label $id.paramsymfrom.entryname -text {receive symbol}
    entry $id.paramsymfrom.entry -textvariable $var_gatomsymfrom -width 20
    pack $id.paramsymfrom.entryname $id.paramsymfrom.entry -side left
    
    frame $id.radio
    pack $id.radio -side bottom
    label $id.radio.label -text {show label on:}
    frame $id.radio.l
    frame $id.radio.r
    pack $id.radio.label -side top
    pack $id.radio.l $id.radio.r -side left
    radiobutton $id.radio.l.radio0 -value 0 \
	-variable $var_gatomwherelabel \
	-text "left"
    radiobutton $id.radio.l.radio1 -value 1 \
	-variable $var_gatomwherelabel \
	-text "right"
    radiobutton $id.radio.r.radio2 -value 2 \
	-variable $var_gatomwherelabel \
	-text "top"
    radiobutton $id.radio.r.radio3 -value 3 \
	-variable $var_gatomwherelabel \
	-text "bottom"
    pack $id.radio.l.radio0 $id.radio.l.radio1 -side top -anchor w
    pack $id.radio.r.radio2 $id.radio.r.radio3 -side top -anchor w
    	

    frame $id.paramlabel
    pack $id.paramlabel -side bottom
    label $id.paramlabel.entryname -text label
    entry $id.paramlabel.entry -textvariable $var_gatomlabel -width 20
    pack $id.paramlabel.entryname $id.paramlabel.entry -side left

    frame $id.paramhi
    pack $id.paramhi -side bottom
    label $id.paramhi.entryname -text "upper limit"
    entry $id.paramhi.entry -textvariable $var_gatomhi -width 8
    pack $id.paramhi.entryname $id.paramhi.entry -side left

    frame $id.paramlo
    pack $id.paramlo -side bottom
    label $id.paramlo.entryname -text "lower limit"
    entry $id.paramlo.entry -textvariable $var_gatomlo -width 8
    pack $id.paramlo.entryname $id.paramlo.entry -side left

    frame $id.params
    pack $id.params -side bottom
    label $id.params.entryname -text width
    entry $id.params.entry -textvariable $var_gatomwidth -width 4
    pack $id.params.entryname $id.params.entry -side left



    bind $id.paramhi.entry <KeyPress-Return> [concat dogatom_ok $id]
    bind $id.paramlo.entry <KeyPress-Return> [concat dogatom_ok $id]
    bind $id.params.entry <KeyPress-Return> [concat dogatom_ok $id]
    pdtk_standardkeybindings $id.paramhi.entry
    pdtk_standardkeybindings $id.paramlo.entry
    pdtk_standardkeybindings $id.params.entry
    $id.params.entry select from 0
    $id.params.entry select adjust end
    focus $id.params.entry
}

############ pdtk_canvas_popup -- popup menu for canvas #########

set popup_xpix 0
set popup_ypix 0

proc popup_action {name action} {
    global popup_xpix popup_ypix
    set cmd [concat $name done-popup $action $popup_xpix $popup_ypix \;]
#    puts stderr $cmd
    pd $cmd
}

proc pdtk_canvas_popup {name xpix ypix canprop canopen} {
    global popup_xpix popup_ypix
    set popup_xpix $xpix
    set popup_ypix $ypix
    if {$canprop == 0} {$name.popup entryconfigure 0 -state disabled}
    if {$canprop == 1} {$name.popup entryconfigure 0 -state active}
    if {$canopen == 0} {$name.popup entryconfigure 1 -state disabled}
    if {$canopen == 1} {$name.popup entryconfigure 1 -state active}
    tk_popup $name.popup [expr $xpix + [winfo rootx $name.c]] \
    	 [expr $ypix + [winfo rooty $name.c]] 0
}


# begin of change "iemlib"
############ pdtk_iemgui_dialog -- dialog window for iem guis #########

set iemgui_define_min_flashhold 50
set iemgui_define_min_flashbreak 10
set iemgui_define_min_fontsize 4

proc iemgui_clip_dim {id} {
    set vid [string trimleft $id .]

    set var_iemgui_wdt [concat iemgui_wdt_$vid]
    global $var_iemgui_wdt
    set var_iemgui_min_wdt [concat iemgui_min_wdt_$vid]
    global $var_iemgui_min_wdt
    set var_iemgui_hgt [concat iemgui_hgt_$vid]
    global $var_iemgui_hgt
    set var_iemgui_min_hgt [concat iemgui_min_hgt_$vid]
    global $var_iemgui_min_hgt
    
    if {[eval concat $$var_iemgui_wdt] < [eval concat $$var_iemgui_min_wdt]} {
        set $var_iemgui_wdt [eval concat $$var_iemgui_min_wdt]
        $id.dim.w_ent configure -textvariable $var_iemgui_wdt
    }
    if {[eval concat $$var_iemgui_hgt] < [eval concat $$var_iemgui_min_hgt]} {
        set $var_iemgui_hgt [eval concat $$var_iemgui_min_hgt]
        $id.dim.h_ent configure -textvariable $var_iemgui_hgt
    }
}

proc iemgui_clip_num {id} {
    set vid [string trimleft $id .]

    set var_iemgui_num [concat iemgui_num_$vid]
    global $var_iemgui_num
    
    if {[eval concat $$var_iemgui_num] > 2000} {
        set $var_iemgui_num 2000
	$id.para.num_ent configure -textvariable $var_iemgui_num
    }
    if {[eval concat $$var_iemgui_num] < 1} {
        set $var_iemgui_num 1
        $id.para.num_ent configure -textvariable $var_iemgui_num
    }
}

proc iemgui_sched_rng {id} {
    set vid [string trimleft $id .]

    set var_iemgui_min_rng [concat iemgui_min_rng_$vid]
    global $var_iemgui_min_rng
    set var_iemgui_max_rng [concat iemgui_max_rng_$vid]
    global $var_iemgui_max_rng
    set var_iemgui_rng_sch [concat iemgui_rng_sch_$vid]
    global $var_iemgui_rng_sch

    global iemgui_define_min_flashhold
    global iemgui_define_min_flashbreak
    
    if {[eval concat $$var_iemgui_rng_sch] == 2} {
        if {[eval concat $$var_iemgui_max_rng] < [eval concat $$var_iemgui_min_rng]} {
            set hhh [eval concat $$var_iemgui_min_rng]
	    set $var_iemgui_min_rng [eval concat $$var_iemgui_max_rng]
	    set $var_iemgui_max_rng $hhh
            $id.rng.max_ent configure -textvariable $var_iemgui_max_rng
	    $id.rng.min_ent configure -textvariable $var_iemgui_min_rng }
        if {[eval concat $$var_iemgui_max_rng] < $iemgui_define_min_flashhold} {
	    set $var_iemgui_max_rng $iemgui_define_min_flashhold
            $id.rng.max_ent configure -textvariable $var_iemgui_max_rng
        }
	if {[eval concat $$var_iemgui_min_rng] < $iemgui_define_min_flashbreak} {
	    set $var_iemgui_min_rng $iemgui_define_min_flashbreak
	    $id.rng.min_ent configure -textvariable $var_iemgui_min_rng
        }
    }
    if {[eval concat $$var_iemgui_rng_sch] == 1} {
	if {[eval concat $$var_iemgui_min_rng] == 0.0} {
	    set $var_iemgui_min_rng 1.0
	    $id.rng.min_ent configure -textvariable $var_iemgui_min_rng
        }
    }
}

proc iemgui_verify_rng {id} {
    set vid [string trimleft $id .]

    set var_iemgui_min_rng [concat iemgui_min_rng_$vid]
    global $var_iemgui_min_rng
    set var_iemgui_max_rng [concat iemgui_max_rng_$vid]
    global $var_iemgui_max_rng
    set var_iemgui_lin0_log1 [concat iemgui_lin0_log1_$vid]
    global $var_iemgui_lin0_log1
    
    if {[eval concat $$var_iemgui_lin0_log1] == 1} {
        if {[eval concat $$var_iemgui_max_rng] == 0.0 && [eval concat $$var_iemgui_min_rng] == 0.0} {
	    set $var_iemgui_max_rng 1.0
	    $id.rng.max_ent configure -textvariable $var_iemgui_max_rng
	    }
        if {[eval concat $$var_iemgui_max_rng] > 0} {
            if {[eval concat $$var_iemgui_min_rng] <= 0} {
	        set $var_iemgui_min_rng [expr [eval concat $$var_iemgui_max_rng] * 0.01]
		$id.rng.min_ent configure -textvariable $var_iemgui_min_rng
	    }
        } else {
            if {[eval concat $$var_iemgui_min_rng] > 0} {
	        set $var_iemgui_max_rng [expr [eval concat $$var_iemgui_min_rng] * 0.01]
		$id.rng.max_ent configure -textvariable $var_iemgui_max_rng
	    }
	}
    }
}

proc iemgui_clip_fontsize {id} {
    set vid [string trimleft $id .]

    set var_iemgui_gn_fs [concat iemgui_gn_fs_$vid]
    global $var_iemgui_gn_fs
    
    global iemgui_define_min_fontsize

    if {[eval concat $$var_iemgui_gn_fs] < $iemgui_define_min_fontsize} {
        set $var_iemgui_gn_fs $iemgui_define_min_fontsize
        $id.gnfs.fs_ent configure -textvariable $var_iemgui_gn_fs
    }
}

proc iemgui_set_col_example {id} {
    set vid [string trimleft $id .]

    set var_iemgui_bcol [concat iemgui_bcol_$vid]
    global $var_iemgui_bcol
    set var_iemgui_fcol [concat iemgui_fcol_$vid]
    global $var_iemgui_fcol
    set var_iemgui_lcol [concat iemgui_lcol_$vid]
    global $var_iemgui_lcol
    
    $id.col_example_choose.lb_bk configure \
       -background [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -activebackground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -foreground [format "#%6.6x" [eval concat $$var_iemgui_lcol]] \
       -activeforeground [format "#%6.6x" [eval concat $$var_iemgui_lcol]]
    
    if { [eval concat $$var_iemgui_fcol] >= 0 } {
       $id.col_example_choose.fr_bk configure \
       -background [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -activebackground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -foreground [format "#%6.6x" [eval concat $$var_iemgui_fcol]] \
       -activeforeground [format "#%6.6x" [eval concat $$var_iemgui_fcol]]
    } else {
       $id.col_example_choose.fr_bk configure \
       -background [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -activebackground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -foreground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -activeforeground [format "#%6.6x" [eval concat $$var_iemgui_bcol]]}
}

proc iemgui_preset_col {id presetcol} {
    set vid [string trimleft $id .]

    set var_iemgui_l2_f1_b0 [concat iemgui_l2_f1_b0_$vid]
    global $var_iemgui_l2_f1_b0
    set var_iemgui_bcol [concat iemgui_bcol_$vid]
    global $var_iemgui_bcol
    set var_iemgui_fcol [concat iemgui_fcol_$vid]
    global $var_iemgui_fcol
    set var_iemgui_lcol [concat iemgui_lcol_$vid]
    global $var_iemgui_lcol
    
    if { [eval concat $$var_iemgui_l2_f1_b0] == 0 } { set $var_iemgui_bcol $presetcol }
    if { [eval concat $$var_iemgui_l2_f1_b0] == 1 } { set $var_iemgui_fcol $presetcol }
    if { [eval concat $$var_iemgui_l2_f1_b0] == 2 } { set $var_iemgui_lcol $presetcol }
    iemgui_set_col_example $id
}

proc iemgui_choose_col_bkfrlb {id} {
    set vid [string trimleft $id .]

    set var_iemgui_l2_f1_b0 [concat iemgui_l2_f1_b0_$vid]
    global $var_iemgui_l2_f1_b0
    set var_iemgui_bcol [concat iemgui_bcol_$vid]
    global $var_iemgui_bcol
    set var_iemgui_fcol [concat iemgui_fcol_$vid]
    global $var_iemgui_fcol
    set var_iemgui_lcol [concat iemgui_lcol_$vid]
    global $var_iemgui_lcol
    
    if {[eval concat $$var_iemgui_l2_f1_b0] == 0} {
        set $var_iemgui_bcol [expr [eval concat $$var_iemgui_bcol] & 0xFCFCFC]
        set helpstring [tk_chooseColor -title "Background-Color" -initialcolor [format "#%6.6x" [eval concat $$var_iemgui_bcol]]]
        if { $helpstring != "" } {
              set $var_iemgui_bcol [string replace $helpstring 0 0 "0x"]
              set $var_iemgui_bcol [expr [eval concat $$var_iemgui_bcol] & 0xFCFCFC] }
    }
    if {[eval concat $$var_iemgui_l2_f1_b0] == 1} {
        set $var_iemgui_fcol [expr [eval concat $$var_iemgui_fcol] & 0xFCFCFC]
        set helpstring [tk_chooseColor -title "Front-Color" -initialcolor [format "#%6.6x" [eval concat $$var_iemgui_fcol]]]
        if { $helpstring != "" } {
              set $var_iemgui_fcol [string replace $helpstring 0 0 "0x"]
              set $var_iemgui_fcol [expr [eval concat $$var_iemgui_fcol] & 0xFCFCFC] }
    }
    if {[eval concat $$var_iemgui_l2_f1_b0] == 2} {
	set $var_iemgui_lcol [expr [eval concat $$var_iemgui_lcol] & 0xFCFCFC]
        set helpstring [tk_chooseColor -title "Label-Color" -initialcolor [format "#%6.6x" [eval concat $$var_iemgui_lcol]]]
        if { $helpstring != "" } {
              set $var_iemgui_lcol [string replace $helpstring 0 0 "0x"]
              set $var_iemgui_lcol [expr [eval concat $$var_iemgui_lcol] & 0xFCFCFC] }
    }
    iemgui_set_col_example $id
}

proc iemgui_lilo {id} {
    set vid [string trimleft $id .]

    set var_iemgui_lin0_log1 [concat iemgui_lin0_log1_$vid]
    global $var_iemgui_lin0_log1
    set var_iemgui_lilo0 [concat iemgui_lilo0_$vid]
    global $var_iemgui_lilo0
    set var_iemgui_lilo1 [concat iemgui_lilo1_$vid]
    global $var_iemgui_lilo1
   
    iemgui_sched_rng $id

    if {[eval concat $$var_iemgui_lin0_log1] == 0} {
        set $var_iemgui_lin0_log1 1
        $id.para.lilo configure -text [eval concat $$var_iemgui_lilo1]
	iemgui_verify_rng $id
	iemgui_sched_rng $id
    } else {
        set $var_iemgui_lin0_log1 0
        $id.para.lilo configure -text [eval concat $$var_iemgui_lilo0]
    }
}

proc iemgui_toggle_font {id} {
    set vid [string trimleft $id .]

    set var_iemgui_gn_f [concat iemgui_gn_f_$vid]
    global $var_iemgui_gn_f
    
    set $var_iemgui_gn_f [expr [eval concat $$var_iemgui_gn_f] + 1]
    if {[eval concat $$var_iemgui_gn_f] > 2} {set $var_iemgui_gn_f 0}
    if {[eval concat $$var_iemgui_gn_f] == 0} {$id.gnfs.fb configure -text "courier" -font {courier 10 bold}}
    if {[eval concat $$var_iemgui_gn_f] == 1} {$id.gnfs.fb configure -text "helvetica" -font {helvetica 10 bold}}
    if {[eval concat $$var_iemgui_gn_f] == 2} {$id.gnfs.fb configure -text "times" -font {times 10 bold}}
}

proc iemgui_lb {id} {
    set vid [string trimleft $id .]

    set var_iemgui_loadbang [concat iemgui_loadbang_$vid]
    global $var_iemgui_loadbang

    if {[eval concat $$var_iemgui_loadbang] == 0} {
        set $var_iemgui_loadbang 1
        $id.para.lb configure -text "init"
    } else {
        set $var_iemgui_loadbang 0
        $id.para.lb configure -text "no init"
    }
}

proc iemgui_stdy_jmp {id} {
    set vid [string trimleft $id .]

    set var_iemgui_steady [concat iemgui_steady_$vid]
    global $var_iemgui_steady
    
    if {[eval concat $$var_iemgui_steady]} {
        set $var_iemgui_steady 0
        $id.para.stdy_jmp configure -text "jump on click"
    } else {
        set $var_iemgui_steady 1
        $id.para.stdy_jmp configure -text "steady on click"
    }
}

proc iemgui_apply {id} {
    set vid [string trimleft $id .]

    set var_iemgui_wdt [concat iemgui_wdt_$vid]
    global $var_iemgui_wdt
    set var_iemgui_min_wdt [concat iemgui_min_wdt_$vid]
    global $var_iemgui_min_wdt
    set var_iemgui_hgt [concat iemgui_hgt_$vid]
    global $var_iemgui_hgt
    set var_iemgui_min_hgt [concat iemgui_min_hgt_$vid]
    global $var_iemgui_min_hgt
    set var_iemgui_min_rng [concat iemgui_min_rng_$vid]
    global $var_iemgui_min_rng
    set var_iemgui_max_rng [concat iemgui_max_rng_$vid]
    global $var_iemgui_max_rng
    set var_iemgui_lin0_log1 [concat iemgui_lin0_log1_$vid]
    global $var_iemgui_lin0_log1
    set var_iemgui_lilo0 [concat iemgui_lilo0_$vid]
    global $var_iemgui_lilo0
    set var_iemgui_lilo1 [concat iemgui_lilo1_$vid]
    global $var_iemgui_lilo1
    set var_iemgui_loadbang [concat iemgui_loadbang_$vid]
    global $var_iemgui_loadbang
    set var_iemgui_num [concat iemgui_num_$vid]
    global $var_iemgui_num
    set var_iemgui_steady [concat iemgui_steady_$vid]
    global $var_iemgui_steady
    set var_iemgui_snd [concat iemgui_snd_$vid]
    global $var_iemgui_snd
    set var_iemgui_rcv [concat iemgui_rcv_$vid]
    global $var_iemgui_rcv
    set var_iemgui_gui_nam [concat iemgui_gui_nam_$vid]
    global $var_iemgui_gui_nam
    set var_iemgui_gn_dx [concat iemgui_gn_dx_$vid]
    global $var_iemgui_gn_dx
    set var_iemgui_gn_dy [concat iemgui_gn_dy_$vid]
    global $var_iemgui_gn_dy
    set var_iemgui_gn_f [concat iemgui_gn_f_$vid]
    global $var_iemgui_gn_f
    set var_iemgui_gn_fs [concat iemgui_gn_fs_$vid]
    global $var_iemgui_gn_fs
    set var_iemgui_bcol [concat iemgui_bcol_$vid]
    global $var_iemgui_bcol
    set var_iemgui_fcol [concat iemgui_fcol_$vid]
    global $var_iemgui_fcol
    set var_iemgui_lcol [concat iemgui_lcol_$vid]
    global $var_iemgui_lcol
    
    iemgui_clip_dim $id
    iemgui_clip_num $id
    iemgui_sched_rng $id
    iemgui_verify_rng $id
    iemgui_sched_rng $id
    iemgui_clip_fontsize $id
    
    if {[eval concat $$var_iemgui_snd] == ""} {set hhhsnd "empty"} else {set hhhsnd [eval concat $$var_iemgui_snd]}
    if {[eval concat $$var_iemgui_rcv] == ""} {set hhhrcv "empty"} else {set hhhrcv [eval concat $$var_iemgui_rcv]}
    if {[eval concat $$var_iemgui_gui_nam] == ""} {set hhhgui_nam "empty"
        } else {
    set hhhgui_nam [eval concat $$var_iemgui_gui_nam]}

    if {[string index $hhhsnd 0] == "$"} {
       set hhhsnd [string replace $hhhsnd 0 0 #] }
    if {[string index $hhhrcv 0] == "$"} {
       set hhhrcv [string replace $hhhrcv 0 0 #] }
    if {[string index $hhhgui_nam 0] == "$"} {
       set hhhgui_nam [string replace $hhhgui_nam 0 0 #] }
    
    set hhhsnd [pdtk_unspace $hhhsnd]
    set hhhrcv [pdtk_unspace $hhhrcv]
    set hhhgui_nam [pdtk_unspace $hhhgui_nam]
    
    pd [concat $id dialog \
        [eval concat $$var_iemgui_wdt] \
        [eval concat $$var_iemgui_hgt] \
        [eval concat $$var_iemgui_min_rng] \
        [eval concat $$var_iemgui_max_rng] \
	[eval concat $$var_iemgui_lin0_log1] \
        [eval concat $$var_iemgui_loadbang] \
        [eval concat $$var_iemgui_num] \
	$hhhsnd \
        $hhhrcv \
	$hhhgui_nam \
        [eval concat $$var_iemgui_gn_dx] \
        [eval concat $$var_iemgui_gn_dy] \
	[eval concat $$var_iemgui_gn_f] \
        [eval concat $$var_iemgui_gn_fs] \
	[eval concat $$var_iemgui_bcol] \
        [eval concat $$var_iemgui_fcol] \
        [eval concat $$var_iemgui_lcol] \
        [eval concat $$var_iemgui_steady] \
	\;]
}

proc iemgui_cancel {id} {pd [concat $id cancel \;]}

proc iemgui_ok {id} {
    iemgui_apply $id
    iemgui_cancel $id
}

proc pdtk_iemgui_dialog {id mainheader \
        dim_header wdt min_wdt wdt_label hgt min_hgt hgt_label \
	rng_header min_rng min_rng_label max_rng max_rng_label rng_sched \
	lin0_log1 lilo0_label lilo1_label loadbang steady num_label num \
	snd rcv \
        gui_name \
	gn_dx gn_dy \
	gn_f gn_fs \
	bcol fcol lcol} {

    set vid [string trimleft $id .]

    set var_iemgui_wdt [concat iemgui_wdt_$vid]
    global $var_iemgui_wdt
    set var_iemgui_min_wdt [concat iemgui_min_wdt_$vid]
    global $var_iemgui_min_wdt
    set var_iemgui_hgt [concat iemgui_hgt_$vid]
    global $var_iemgui_hgt
    set var_iemgui_min_hgt [concat iemgui_min_hgt_$vid]
    global $var_iemgui_min_hgt
    set var_iemgui_min_rng [concat iemgui_min_rng_$vid]
    global $var_iemgui_min_rng
    set var_iemgui_max_rng [concat iemgui_max_rng_$vid]
    global $var_iemgui_max_rng
    set var_iemgui_rng_sch [concat iemgui_rng_sch_$vid]
    global $var_iemgui_rng_sch
    set var_iemgui_lin0_log1 [concat iemgui_lin0_log1_$vid]
    global $var_iemgui_lin0_log1
    set var_iemgui_lilo0 [concat iemgui_lilo0_$vid]
    global $var_iemgui_lilo0
    set var_iemgui_lilo1 [concat iemgui_lilo1_$vid]
    global $var_iemgui_lilo1
    set var_iemgui_loadbang [concat iemgui_loadbang_$vid]
    global $var_iemgui_loadbang
    set var_iemgui_num [concat iemgui_num_$vid]
    global $var_iemgui_num
    set var_iemgui_steady [concat iemgui_steady_$vid]
    global $var_iemgui_steady
    set var_iemgui_snd [concat iemgui_snd_$vid]
    global $var_iemgui_snd
    set var_iemgui_rcv [concat iemgui_rcv_$vid]
    global $var_iemgui_rcv
    set var_iemgui_gui_nam [concat iemgui_gui_nam_$vid]
    global $var_iemgui_gui_nam
    set var_iemgui_gn_dx [concat iemgui_gn_dx_$vid]
    global $var_iemgui_gn_dx
    set var_iemgui_gn_dy [concat iemgui_gn_dy_$vid]
    global $var_iemgui_gn_dy
    set var_iemgui_gn_f [concat iemgui_gn_f_$vid]
    global $var_iemgui_gn_f
    set var_iemgui_gn_fs [concat iemgui_gn_fs_$vid]
    global $var_iemgui_gn_fs
    set var_iemgui_l2_f1_b0 [concat iemgui_l2_f1_b0_$vid]
    global $var_iemgui_l2_f1_b0
    set var_iemgui_bcol [concat iemgui_bcol_$vid]
    global $var_iemgui_bcol
    set var_iemgui_fcol [concat iemgui_fcol_$vid]
    global $var_iemgui_fcol
    set var_iemgui_lcol [concat iemgui_lcol_$vid]
    global $var_iemgui_lcol

    set $var_iemgui_wdt $wdt
    set $var_iemgui_min_wdt $min_wdt
    set $var_iemgui_hgt $hgt
    set $var_iemgui_min_hgt $min_hgt
    set $var_iemgui_min_rng $min_rng
    set $var_iemgui_max_rng $max_rng
    set $var_iemgui_rng_sch $rng_sched
    set $var_iemgui_lin0_log1 $lin0_log1
    set $var_iemgui_lilo0 $lilo0_label
    set $var_iemgui_lilo1 $lilo1_label
    set $var_iemgui_loadbang $loadbang
    set $var_iemgui_num $num
    set $var_iemgui_steady $steady
    if {$snd == "empty"} {set $var_iemgui_snd [format ""]
        } else {set $var_iemgui_snd [format "%s" $snd]}
    if {$rcv == "empty"} {set $var_iemgui_rcv [format ""]
        } else {set $var_iemgui_rcv [format "%s" $rcv]}
    if {$gui_name == "empty"} {set $var_iemgui_gui_nam [format ""]
        } else {set $var_iemgui_gui_nam [format "%s" $gui_name]}
    
    if {[string index [eval concat $$var_iemgui_snd] 0] == "#"} {
       set $var_iemgui_snd [string replace [eval concat $$var_iemgui_snd] 0 0 $] }
    if {[string index [eval concat $$var_iemgui_rcv] 0] == "#"} {
       set $var_iemgui_rcv [string replace [eval concat $$var_iemgui_rcv] 0 0 $] }
    if {[string index [eval concat $$var_iemgui_gui_nam] 0] == "#"} {
       set $var_iemgui_gui_nam [string replace [eval concat $$var_iemgui_gui_nam] 0 0 $] }
    set $var_iemgui_gn_dx $gn_dx
    set $var_iemgui_gn_dy $gn_dy
    set $var_iemgui_gn_f $gn_f
    set $var_iemgui_gn_fs $gn_fs
    
    set $var_iemgui_bcol $bcol
    set $var_iemgui_fcol $fcol
    set $var_iemgui_lcol $lcol
    
    set $var_iemgui_l2_f1_b0 0

    toplevel $id
    wm title $id [format "%s-PROPERTIES" $mainheader]
    wm protocol $id WM_DELETE_WINDOW [concat iemgui_cancel $id]
    
    frame $id.dim
    pack $id.dim -side top
    label $id.dim.head -text $dim_header
    label $id.dim.w_lab -text $wdt_label -width 6
    entry $id.dim.w_ent -textvariable $var_iemgui_wdt -width 5
    label $id.dim.dummy1 -text " " -width 10
    label $id.dim.h_lab -text $hgt_label -width 6
    entry $id.dim.h_ent -textvariable $var_iemgui_hgt -width 5
    pack $id.dim.head -side top
    pack $id.dim.w_lab $id.dim.w_ent $id.dim.dummy1 -side left
    if { $hgt_label != "empty" } {
        pack $id.dim.h_lab $id.dim.h_ent -side left}

    frame $id.rng
    pack $id.rng -side top
    label $id.rng.head -text $rng_header
    label $id.rng.min_lab -text $min_rng_label -width 6
    entry $id.rng.min_ent -textvariable $var_iemgui_min_rng -width 9
    label $id.rng.dummy1 -text " " -width 1
    label $id.rng.max_lab -text $max_rng_label -width 8
    entry $id.rng.max_ent -textvariable $var_iemgui_max_rng -width 9
    if { $rng_header != "empty" } {
        pack $id.rng.head -side top
	if { $min_rng_label != "empty" } {
            pack $id.rng.min_lab $id.rng.min_ent -side left}
	if { $max_rng_label != "empty" } {
	    pack $id.rng.dummy1 \
            $id.rng.max_lab $id.rng.max_ent -side left} }
    
    if { [eval concat $$var_iemgui_lin0_log1] >= 0 || [eval concat $$var_iemgui_loadbang] >= 0 || [eval concat $$var_iemgui_num] > 0 || [eval concat $$var_iemgui_steady] >= 0 } {
        label $id.space1 -text "---------------------------------"
        pack $id.space1 -side top }

    frame $id.para
    pack $id.para -side top
    label $id.para.dummy2 -text "" -width 1
    label $id.para.dummy3 -text "" -width 1
    if {[eval concat $$var_iemgui_lin0_log1] == 0} {
        button $id.para.lilo -text [eval concat $$var_iemgui_lilo0] -width 5 -command "iemgui_lilo $id" }
    if {[eval concat $$var_iemgui_lin0_log1] == 1} {
        button $id.para.lilo -text [eval concat $$var_iemgui_lilo1] -width 5 -command "iemgui_lilo $id" }
    if {[eval concat $$var_iemgui_loadbang] == 0} {
        button $id.para.lb -text "no init" -width 5 -command "iemgui_lb $id" }
    if {[eval concat $$var_iemgui_loadbang] == 1} {
        button $id.para.lb -text "init" -width 5 -command "iemgui_lb $id" }
    label $id.para.num_lab -text $num_label -width 9
    entry $id.para.num_ent -textvariable $var_iemgui_num -width 4
    if {[eval concat $$var_iemgui_steady] == 0} {
        button $id.para.stdy_jmp -text "jump on click" -width 11 -command "iemgui_stdy_jmp $id" }
    if {[eval concat $$var_iemgui_steady] == 1} {
        button $id.para.stdy_jmp -text "steady on click" -width 11 -command "iemgui_stdy_jmp $id" }
    if {[eval concat $$var_iemgui_lin0_log1] >= 0} {
        pack $id.para.lilo -side left -expand 1}
    if {[eval concat $$var_iemgui_loadbang] >= 0} {
        pack $id.para.dummy2 $id.para.lb -side left -expand 1}
    if {[eval concat $$var_iemgui_num] > 0} {
        pack $id.para.dummy3 $id.para.num_lab $id.para.num_ent -side left -expand 1}
    if {[eval concat $$var_iemgui_steady] >= 0} {
        pack $id.para.dummy3 $id.para.stdy_jmp -side left -expand 1}
    if { $snd != "nosndno" || $rcv != "norcvno" } {
        label $id.space2 -text "---------------------------------"
        pack $id.space2 -side top }
    
    frame $id.snd
    pack $id.snd -side top
    label $id.snd.dummy1 -text "" -width 2
    label $id.snd.lab -text "send-symbol:" -width 12
    entry $id.snd.ent -textvariable $var_iemgui_snd -width 20
    if { $snd != "nosndno" } {
        pack $id.snd.dummy1 $id.snd.lab $id.snd.ent -side left}
    
    frame $id.rcv
    pack $id.rcv -side top
    label $id.rcv.lab -text "receive-symbol:" -width 15
    entry $id.rcv.ent -textvariable $var_iemgui_rcv -width 20
    if { $rcv != "norcvno" } {
        pack $id.rcv.lab $id.rcv.ent -side left}
    
    frame $id.gnam
    pack $id.gnam -side top
    label $id.gnam.head -text "--------------label:---------------"
    label $id.gnam.dummy1 -text "" -width 1
    label $id.gnam.lab -text "name:" -width 6
    entry $id.gnam.ent -textvariable $var_iemgui_gui_nam -width 29
    label $id.gnam.dummy2 -text "" -width 1
    pack $id.gnam.head -side top
    pack $id.gnam.dummy1 $id.gnam.lab $id.gnam.ent $id.gnam.dummy2 -side left
    
    frame $id.gnxy
    pack $id.gnxy -side top
    label $id.gnxy.x_lab -text "x_off:" -width 6
    entry $id.gnxy.x_ent -textvariable $var_iemgui_gn_dx -width 5
    label $id.gnxy.dummy1 -text " " -width 10
    label $id.gnxy.y_lab -text "y_off:" -width 6
    entry $id.gnxy.y_ent -textvariable $var_iemgui_gn_dy -width 5
    pack $id.gnxy.x_lab $id.gnxy.x_ent $id.gnxy.dummy1 \
    	 $id.gnxy.y_lab $id.gnxy.y_ent -side left
    
    frame $id.gnfs
    pack $id.gnfs -side top
    label $id.gnfs.f_lab -text "font:" -width 6
    if {[eval concat $$var_iemgui_gn_f] == 0} {
        button $id.gnfs.fb -text "courier" -font {courier 10 bold} -width 7 -command "iemgui_toggle_font $id" }
    if {[eval concat $$var_iemgui_gn_f] == 1} {
        button $id.gnfs.fb -text "helvetica" -font {helvetica 10 bold} -width 7 -command "iemgui_toggle_font $id" }
    if {[eval concat $$var_iemgui_gn_f] == 2} {
        button $id.gnfs.fb -text "times" -font {times 10 bold} -width 7 -command "iemgui_toggle_font $id" }
    label $id.gnfs.dummy1 -text "" -width 1
    label $id.gnfs.fs_lab -text "fontsize:" -width 8
    entry $id.gnfs.fs_ent -textvariable $var_iemgui_gn_fs -width 5
    pack $id.gnfs.f_lab $id.gnfs.fb $id.gnfs.dummy1 \
    	 $id.gnfs.fs_lab $id.gnfs.fs_ent -side left
    
    label $id.col_head -text "--------------colors:--------------"
    pack $id.col_head -side top
    
    frame $id.col_select
    pack $id.col_select -side top
    radiobutton $id.col_select.radio0 -value 0 -variable $var_iemgui_l2_f1_b0 \
       -text "backgd" -width 5
    radiobutton $id.col_select.radio1 -value 1 -variable $var_iemgui_l2_f1_b0 \
       -text "front" -width 5
    radiobutton $id.col_select.radio2 -value 2 -variable $var_iemgui_l2_f1_b0 \
       -text "label" -width 5
    if { [eval concat $$var_iemgui_fcol] >= 0 } {
         pack $id.col_select.radio0 $id.col_select.radio1 $id.col_select.radio2 -side left
       } else {pack $id.col_select.radio0 $id.col_select.radio2 -side left}
    
    frame $id.col_example_choose
    pack $id.col_example_choose -side top
    button $id.col_example_choose.but -text "compose color" -width 10 \
             -command "iemgui_choose_col_bkfrlb $id"
    label $id.col_example_choose.dummy1 -text "" -width 1
    if { [eval concat $$var_iemgui_fcol] >= 0 } {
      button $id.col_example_choose.fr_bk -text "o=||=o" -width 5 \
       -background [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -activebackground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -foreground [format "#%6.6x" [eval concat $$var_iemgui_fcol]] \
       -activeforeground [format "#%6.6x" [eval concat $$var_iemgui_fcol]] -pady 2
    } else {
      button $id.col_example_choose.fr_bk -text "o=||=o" -width 5 \
       -background [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -activebackground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -foreground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -activeforeground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] -pady 2}
    button $id.col_example_choose.lb_bk -text "testlabel" -width 7 \
       -background [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -activebackground [format "#%6.6x" [eval concat $$var_iemgui_bcol]] \
       -foreground [format "#%6.6x" [eval concat $$var_iemgui_lcol]] \
       -activeforeground [format "#%6.6x" [eval concat $$var_iemgui_lcol]] -pady 2
    
    pack $id.col_example_choose.but $id.col_example_choose.dummy1 \
       $id.col_example_choose.fr_bk $id.col_example_choose.lb_bk -side left
    
    label $id.space3 -text "------or click color preset:-------"
    pack $id.space3 -side top
    
    frame $id.bcol
    pack $id.bcol -side top
    foreach i { 0 1 2 3 4 5 6 7 8 9 } hexcol { 16579836 14737632 12369084 \
      16572640 16572608 16579784 14220504 14220540 14476540 16308476 } {
        button $id.bcol.c$i -background [format "#%6.6x" $hexcol] \
	    -activebackground [format "#%6.6x" $hexcol] \
            -font {courier 2 normal} -padx 7 -pady 6 \
            -command [format "iemgui_preset_col %s %d" $id $hexcol] }
    pack $id.bcol.c0 $id.bcol.c1 $id.bcol.c2 $id.bcol.c3 $id.bcol.c4 \
        $id.bcol.c5 $id.bcol.c6 $id.bcol.c7 $id.bcol.c8 $id.bcol.c9 -side left
    
    frame $id.fcol
    pack $id.fcol -side top
    foreach i { 0 1 2 3 4 5 6 7 8 9 } hexcol { 10526880 8158332 6316128 \
      16525352 16559172 15263784 1370132 2684148 3952892 16003312 } {
	button $id.fcol.c$i -background [format "#%6.6x" $hexcol] \
	    -activebackground [format "#%6.6x" $hexcol] \
            -font {courier 2 normal} -padx 7 -pady 6 \
            -command [format "iemgui_preset_col %s %d" $id $hexcol] }
    pack $id.fcol.c0 $id.fcol.c1 $id.fcol.c2 $id.fcol.c3 $id.fcol.c4 \
        $id.fcol.c5 $id.fcol.c6 $id.fcol.c7 $id.fcol.c8 $id.fcol.c9 -side left
    
    frame $id.lcol
    pack $id.lcol -side top
    foreach i { 0 1 2 3 4 5 6 7 8 9 } hexcol { 4210752 2105376 0 \
      9177096 5779456 7874580 2641940 17488 5256 5767248 } {
        button $id.lcol.c$i -background [format "#%6.6x" $hexcol] \
	    -activebackground [format "#%6.6x" $hexcol] \
            -font {courier 2 normal} -padx 7 -pady 6 \
            -command [format "iemgui_preset_col %s %d" $id $hexcol] }
    pack $id.lcol.c0 $id.lcol.c1 $id.lcol.c2 $id.lcol.c3 $id.lcol.c4 \
        $id.lcol.c5 $id.lcol.c6 $id.lcol.c7 $id.lcol.c8 $id.lcol.c9 -side left
    
    
    label $id.space4 -text "---------------------------------"
    pack $id.space4 -side top
    
    frame $id.cao
    pack $id.cao -side top
    button $id.cao.cancel -text {Cancel} -width 6 \
    	-command "iemgui_cancel $id"
    label $id.cao.dummy1 -text "" -width 3
    button $id.cao.apply -text {Apply} -width 6 \
    	-command "iemgui_apply $id"
    label $id.cao.dummy2 -text "" -width 3
    button $id.cao.ok -text {OK} -width 6 \
    	-command "iemgui_ok $id"
    pack $id.cao.cancel $id.cao.dummy1 \
        $id.cao.apply $id.cao.dummy2 \
	$id.cao.ok -side left
    
    label $id.space5 -text ""
    pack $id.space5 -side top

    if {[info tclversion] < 8.4} {
    	bind $id <Key-Tab> {tkTabToWindow [tk_focusNext %W]}
    	bind $id <<PrevWindow>> {tkTabToWindow [tk_focusPrev %W]}
    } else {
    	bind $id <Key-Tab> {tk::TabToWindow [tk_focusNext %W]}
    	bind $id <<PrevWindow>> {tk::TabToWindow [tk_focusPrev %W]}
    }
    
    bind $id.dim.w_ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.dim.h_ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.rng.min_ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.rng.max_ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.para.num_ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.snd.ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.rcv.ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.gnam.ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.gnxy.x_ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.gnxy.y_ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.gnfs.fs_ent <KeyPress-Return> [concat iemgui_ok $id]
    bind $id.cao.ok <KeyPress-Return> [concat iemgui_ok $id]
    pdtk_standardkeybindings $id.dim.w_ent
    pdtk_standardkeybindings $id.dim.h_ent
    pdtk_standardkeybindings $id.rng.min_ent
    pdtk_standardkeybindings $id.rng.max_ent
    pdtk_standardkeybindings $id.para.num_ent
    pdtk_standardkeybindings $id.snd.ent
    pdtk_standardkeybindings $id.rcv.ent
    pdtk_standardkeybindings $id.gnam.ent
    pdtk_standardkeybindings $id.gnxy.x_ent
    pdtk_standardkeybindings $id.gnxy.y_ent
    pdtk_standardkeybindings $id.gnfs.fs_ent
    pdtk_standardkeybindings $id.cao.ok
    
    $id.dim.w_ent select from 0
    $id.dim.w_ent select adjust end
    focus $id.dim.w_ent
}
# end of change "iemlib"

############ pdtk_array_dialog -- dialog window for arrays #########
# see comments above (pdtk_gatom_dialog) about variable name handling 

proc array_apply {id} {
# strip "." from the TK id to make a variable name suffix 
    set vid [string trimleft $id .]
# for each variable, make a local variable to hold its name...
    set var_array_name [concat array_name_$vid]
    global $var_array_name
    set var_array_n [concat array_n_$vid]
    global $var_array_n
    set var_array_saveit [concat array_saveit_$vid]
    global $var_array_saveit
    set var_array_drawasrects [concat array_drawasrects_$vid]
    global $var_array_drawasrects
    set var_array_otherflag [concat array_otherflag_$vid]
    global $var_array_otherflag
    set mofo [eval concat $$var_array_name]
    if {[string index $mofo 0] == "$"} {
       set mofo [string replace $mofo 0 0 #] }

    set saveit [eval concat $$var_array_saveit]
    set drawasrects [eval concat $$var_array_drawasrects]

    pd [concat $id arraydialog $mofo \
	[eval concat $$var_array_n] \
    	[expr $saveit + 2 * $drawasrects] \
	[eval concat $$var_array_otherflag] \
	\;]
}

# jsarlo
proc array_viewlist {id name page} {
    pd [concat $id arrayviewlistnew\;]
}
# end jsarlo

proc array_cancel {id} {
    set cmd [concat $id cancel \;]
    pd $cmd
}

proc array_ok {id} {
    array_apply $id
    array_cancel $id
}

proc pdtk_array_dialog {id name n flags newone} {
    set vid [string trimleft $id .]

    set var_array_name [concat array_name_$vid]
    global $var_array_name
    set var_array_n [concat array_n_$vid]
    global $var_array_n
    set var_array_saveit [concat array_saveit_$vid]
    global $var_array_saveit
    set var_array_drawasrects [concat array_drawasrects_$vid]
    global $var_array_drawasrects
    set var_array_otherflag [concat array_otherflag_$vid]
    global $var_array_otherflag

    set $var_array_name $name
    set $var_array_n $n
    set $var_array_saveit [expr ( $flags & 1 ) != 0]
    set $var_array_drawasrects [expr ( $flags & 2 ) != 0]
    set $var_array_otherflag 0

    toplevel $id
    wm title $id {array}
    wm protocol $id WM_DELETE_WINDOW [concat array_cancel $id]

    frame $id.name
    pack $id.name -side top
    label $id.name.label -text "name"
    entry $id.name.entry -textvariable $var_array_name
    pack $id.name.label $id.name.entry -side left

    frame $id.n
    pack $id.n -side top
    label $id.n.label -text "size"
    entry $id.n.entry -textvariable $var_array_n
    pack $id.n.label $id.n.entry -side left

    checkbutton $id.saveme -text {save contents} -variable $var_array_saveit \
    	-anchor w
    pack $id.saveme -side top

    frame $id.drawasrects
    pack $id.drawasrects -side top
    radiobutton $id.drawasrects.drawasrects0 -value 0 \
	-variable $var_array_drawasrects \
	-text "draw as points"
    radiobutton $id.drawasrects.drawasrects1 -value 1 \
	-variable $var_array_drawasrects \
	-text "polygon"
    radiobutton $id.drawasrects.drawasrects2 -value 2 \
	-variable $var_array_drawasrects \
	-text "bezier curve"
    pack $id.drawasrects.drawasrects0 -side top -anchor w
    pack $id.drawasrects.drawasrects1 -side top -anchor w
    pack $id.drawasrects.drawasrects2 -side top -anchor w

    if {$newone != 0} {
	frame $id.radio
    	pack $id.radio -side top
    	radiobutton $id.radio.radio0 -value 0 \
	    -variable $var_array_otherflag \
	    -text "in new graph"
    	radiobutton $id.radio.radio1 -value 1 \
	    -variable $var_array_otherflag \
	    -text "in last graph"
    	pack $id.radio.radio0 -side top -anchor w
    	pack $id.radio.radio1 -side top -anchor w
    } else {	
    	checkbutton $id.deleteme -text {delete me} \
	    -variable $var_array_otherflag -anchor w
	pack $id.deleteme -side top
    }
    # jsarlo
    if {$newone == 0} {
      button $id.listview -text {View list}\
        -command "array_viewlist $id $name 0"
      pack $id.listview -side left
    }
    # end jsarlo
    frame $id.buttonframe
    pack $id.buttonframe -side bottom -fill x -pady 2m
    button $id.buttonframe.cancel -text {Cancel}\
    	-command "array_cancel $id"
    if {$newone == 0} {button $id.buttonframe.apply -text {Apply}\
    	-command "array_apply $id"}
    button $id.buttonframe.ok -text {OK}\
    	-command "array_ok $id"
    pack $id.buttonframe.cancel -side left -expand 1
    if {$newone == 0} {pack $id.buttonframe.apply -side left -expand 1}
    pack $id.buttonframe.ok -side left -expand 1
    
    bind $id.name.entry <KeyPress-Return> [concat array_ok $id]
    bind $id.n.entry <KeyPress-Return> [concat array_ok $id]
    pdtk_standardkeybindings $id.name.entry
    pdtk_standardkeybindings $id.n.entry
    $id.name.entry select from 0
    $id.name.entry select adjust end
    focus $id.name.entry
}

############ pdtk_canvas_dialog -- dialog window for canvass #########
# see comments above (pdtk_gatom_dialog) about variable name handling 

proc canvas_apply {id} {
# strip "." from the TK id to make a variable name suffix 
    set vid [string trimleft $id .]
# for each variable, make a local variable to hold its name...

    set var_canvas_xscale [concat canvas_xscale_$vid]
    global $var_canvas_xscale
    set var_canvas_yscale [concat canvas_yscale_$vid]
    global $var_canvas_yscale
    set var_canvas_graphme [concat canvas_graphme_$vid]
    global $var_canvas_graphme
    set var_canvas_x1 [concat canvas_x1_$vid]
    global $var_canvas_x1
    set var_canvas_x2 [concat canvas_x2_$vid]
    global $var_canvas_x2
    set var_canvas_xpix [concat canvas_xpix_$vid]
    global $var_canvas_xpix
    set var_canvas_xmargin [concat canvas_xmargin_$vid]
    global $var_canvas_xmargin
    set var_canvas_y1 [concat canvas_y1_$vid]
    global $var_canvas_y1
    set var_canvas_y2 [concat canvas_y2_$vid]
    global $var_canvas_y2
    set var_canvas_ypix [concat canvas_ypix_$vid]
    global $var_canvas_ypix
    set var_canvas_ymargin [concat canvas_ymargin_$vid]
    global $var_canvas_ymargin

    pd [concat $id donecanvasdialog \
    	[eval concat $$var_canvas_xscale] \
	[eval concat $$var_canvas_yscale] \
    	[eval concat $$var_canvas_graphme] \
    	[eval concat $$var_canvas_x1] \
    	[eval concat $$var_canvas_y1] \
    	[eval concat $$var_canvas_x2] \
    	[eval concat $$var_canvas_y2] \
    	[eval concat $$var_canvas_xpix] \
    	[eval concat $$var_canvas_ypix] \
    	[eval concat $$var_canvas_xmargin] \
    	[eval concat $$var_canvas_ymargin] \
	\;]
}

proc canvas_cancel {id} {
    set cmd [concat $id cancel \;]
    pd $cmd
}

proc canvas_ok {id} {
    canvas_apply $id
    canvas_cancel $id
}

proc canvas_checkcommand {id} {
    set vid [string trimleft $id .]
#    puts stderr [concat canvas_checkcommand $id $vid]

    set var_canvas_xscale [concat canvas_xscale_$vid]
    global $var_canvas_xscale
    set var_canvas_yscale [concat canvas_yscale_$vid]
    global $var_canvas_yscale
    set var_canvas_graphme [concat canvas_graphme_$vid]
    global $var_canvas_graphme
    set var_canvas_x1 [concat canvas_x1_$vid]
    global $var_canvas_x1
    set var_canvas_x2 [concat canvas_x2_$vid]
    global $var_canvas_x2
    set var_canvas_xpix [concat canvas_xpix_$vid]
    global $var_canvas_xpix
    set var_canvas_xmargin [concat canvas_xmargin_$vid]
    global $var_canvas_xmargin
    set var_canvas_y1 [concat canvas_y1_$vid]
    global $var_canvas_y1
    set var_canvas_y2 [concat canvas_y2_$vid]
    global $var_canvas_y2
    set var_canvas_ypix [concat canvas_ypix_$vid]
    global $var_canvas_ypix
    set var_canvas_ymargin [concat canvas_ymargin_$vid]
    global $var_canvas_ymargin

    if { [eval concat $$var_canvas_graphme] != 0 } {
        $id.xrange.entry1 configure -state normal
        $id.xrange.entry2 configure -state normal
        $id.xrange.entry3 configure -state normal
        $id.xrange.entry4 configure -state normal
        $id.yrange.entry1 configure -state normal
        $id.yrange.entry2 configure -state normal
        $id.yrange.entry3 configure -state normal
        $id.yrange.entry4 configure -state normal
        $id.xscale.entry configure -state disabled
        $id.yscale.entry configure -state disabled
        set x1 [eval concat $$var_canvas_x1]
        set y1 [eval concat $$var_canvas_y1]
        set x2 [eval concat $$var_canvas_x2]
        set y2 [eval concat $$var_canvas_y2]
        if { [eval concat $$var_canvas_x1] == 0 && \
             [eval concat $$var_canvas_y1] == 0 && \
             [eval concat $$var_canvas_x2] == 0 && \
             [eval concat $$var_canvas_y2] == 0 } {
                set $var_canvas_x2 1
                set $var_canvas_y2 1
        }
        if { [eval concat $$var_canvas_xpix] == 0 } {
            set $var_canvas_xpix 85
            set $var_canvas_xmargin 100
        }
        if { [eval concat $$var_canvas_ypix] == 0 } {
            set $var_canvas_ypix 60
            set $var_canvas_ymargin 100
        }
    } else {
        $id.xrange.entry1 configure -state disabled
        $id.xrange.entry2 configure -state disabled
        $id.xrange.entry3 configure -state disabled
        $id.xrange.entry4 configure -state disabled
        $id.yrange.entry1 configure -state disabled
        $id.yrange.entry2 configure -state disabled
        $id.yrange.entry3 configure -state disabled
        $id.yrange.entry4 configure -state disabled
        $id.xscale.entry configure -state normal
        $id.yscale.entry configure -state normal
        if { [eval concat $$var_canvas_xscale] == 0 } {
            set $var_canvas_xscale 1
        }
        if { [eval concat $$var_canvas_yscale] == 0 } {
            set $var_canvas_yscale -1
        }
    }
}

proc pdtk_canvas_dialog {id xscale yscale graphme x1 y1 x2 y2 \
    xpix ypix xmargin ymargin} {
    set vid [string trimleft $id .]

    set var_canvas_xscale [concat canvas_xscale_$vid]
    global $var_canvas_xscale
    set var_canvas_yscale [concat canvas_yscale_$vid]
    global $var_canvas_yscale
    set var_canvas_graphme [concat canvas_graphme_$vid]
    global $var_canvas_graphme
    set var_canvas_x1 [concat canvas_x1_$vid]
    global $var_canvas_x1
    set var_canvas_x2 [concat canvas_x2_$vid]
    global $var_canvas_x2
    set var_canvas_xpix [concat canvas_xpix_$vid]
    global $var_canvas_xpix
    set var_canvas_xmargin [concat canvas_xmargin_$vid]
    global $var_canvas_xmargin
    set var_canvas_y1 [concat canvas_y1_$vid]
    global $var_canvas_y1
    set var_canvas_y2 [concat canvas_y2_$vid]
    global $var_canvas_y2
    set var_canvas_ypix [concat canvas_ypix_$vid]
    global $var_canvas_ypix
    set var_canvas_ymargin [concat canvas_ymargin_$vid]
    global $var_canvas_ymargin


    set $var_canvas_xscale $xscale
    set $var_canvas_yscale $yscale
    set $var_canvas_graphme $graphme
    set $var_canvas_x1 $x1
    set $var_canvas_y1 $y1
    set $var_canvas_x2 $x2
    set $var_canvas_y2 $y2
    set $var_canvas_xpix $xpix
    set $var_canvas_ypix $ypix
    set $var_canvas_xmargin $xmargin
    set $var_canvas_ymargin $ymargin

    toplevel $id
    wm title $id {canvas}
    wm protocol $id WM_DELETE_WINDOW [concat canvas_cancel $id]

    label $id.toplabel -text "Canvas Properties"
    pack $id.toplabel -side top
    
    frame $id.xscale
    pack $id.xscale -side top
    label $id.xscale.label -text "X units per pixel"
    entry $id.xscale.entry -textvariable $var_canvas_xscale -width 10
    pack $id.xscale.label $id.xscale.entry -side left

    frame $id.yscale
    pack $id.yscale -side top
    label $id.yscale.label -text "Y units per pixel"
    entry $id.yscale.entry -textvariable $var_canvas_yscale -width 10
    pack $id.yscale.label $id.yscale.entry -side left

    checkbutton $id.graphme -text {graph on parent} \
    	-variable $var_canvas_graphme -anchor w \
        -command [concat canvas_checkcommand $id]
    pack $id.graphme -side top

    frame $id.xrange
    pack $id.xrange -side top
    label $id.xrange.label1 -text "X range: from"
    entry $id.xrange.entry1 -textvariable $var_canvas_x1 -width 6
    label $id.xrange.label2 -text "to"
    entry $id.xrange.entry2 -textvariable $var_canvas_x2 -width 6
    label $id.xrange.label3 -text "size"
    entry $id.xrange.entry3 -textvariable $var_canvas_xpix -width 4
    label $id.xrange.label4 -text "margin"
    entry $id.xrange.entry4 -textvariable $var_canvas_xmargin -width 4
    pack $id.xrange.label1 $id.xrange.entry1 \
        $id.xrange.label2 $id.xrange.entry2 \
        $id.xrange.label3 $id.xrange.entry3 \
        $id.xrange.label4 $id.xrange.entry4 \
        -side left

    frame $id.yrange
    pack $id.yrange -side top
    label $id.yrange.label1 -text "Y range: from"
    entry $id.yrange.entry1 -textvariable $var_canvas_y1 -width 6
    label $id.yrange.label2 -text "to"
    entry $id.yrange.entry2 -textvariable $var_canvas_y2 -width 6
    label $id.yrange.label3 -text "size"
    entry $id.yrange.entry3 -textvariable $var_canvas_ypix -width 4
    label $id.yrange.label4 -text "margin"
    entry $id.yrange.entry4 -textvariable $var_canvas_ymargin -width 4
    pack $id.yrange.label1 $id.yrange.entry1 \
        $id.yrange.label2 $id.yrange.entry2 \
        $id.yrange.label3 $id.yrange.entry3 \
        $id.yrange.label4 $id.yrange.entry4 \
        -side left

    frame $id.buttonframe
    pack $id.buttonframe -side bottom -fill x -pady 2m
    button $id.buttonframe.cancel -text {Cancel}\
    	-command "canvas_cancel $id"
    button $id.buttonframe.apply -text {Apply}\
    	-command "canvas_apply $id"
    button $id.buttonframe.ok -text {OK}\
    	-command "canvas_ok $id"
    pack $id.buttonframe.cancel -side left -expand 1
    pack $id.buttonframe.apply -side left -expand 1
    pack $id.buttonframe.ok -side left -expand 1

    bind $id.xscale.entry <KeyPress-Return> [concat canvas_ok $id]
    bind $id.yscale.entry <KeyPress-Return> [concat canvas_ok $id]
    pdtk_standardkeybindings $id.xscale.entry
    pdtk_standardkeybindings $id.yscale.entry
    $id.xscale.entry select from 0
    $id.xscale.entry select adjust end
    focus $id.xscale.entry
    canvas_checkcommand $id
}

############ pdtk_data_dialog -- run a data dialog #########
proc dodata_send {name} {
#    puts stderr [$name.text get 0.0 end]

    for {set i 1} {[$name.text compare [concat $i.0 + 3 chars] < end]} \
    	    {incr i 1} {
#    	puts stderr [concat it's [$name.text get $i.0 [expr $i + 1].0]]
    	set cmd [concat $name data [$name.text get $i.0 [expr $i + 1].0] \;]
#	puts stderr $cmd
    	pd $cmd
    }
    set cmd [concat $name end \;]
#    puts stderr $cmd
    pd $cmd
}

proc dodata_cancel {name} {
    set cmd [concat $name cancel \;]
#    puts stderr $cmd
    pd $cmd
}

proc dodata_ok {name} {
    dodata_send $name
    dodata_cancel $name
}

proc pdtk_data_dialog {name stuff} {
    global pd_font3
    toplevel $name
    wm title $name {Atom}
    wm protocol $name WM_DELETE_WINDOW [concat dodata_cancel $name]

    frame $name.buttonframe
    pack $name.buttonframe -side bottom -fill x -pady 2m
    button $name.buttonframe.send -text {Send (Ctrl s)}\
    	-command [concat dodata_send $name]
    button $name.buttonframe.ok -text {OK (Ctrl t)}\
    	-command [concat dodata_ok $name]
    pack $name.buttonframe.send -side left -expand 1
    pack $name.buttonframe.ok -side left -expand 1

    text $name.text -relief raised -bd 2 -height 40 -width 60 \
    	-yscrollcommand "$name.scroll set" -font $pd_font3
    scrollbar $name.scroll -command "$name.text yview"
    pack $name.scroll -side right -fill y
    pack $name.text -side left -fill both -expand 1
    $name.text insert end $stuff
    focus $name.text
    bind $name.text <Control-t> [concat dodata_ok $name]
    bind $name.text <Control-s> [concat dodata_send $name]
}

############ check or uncheck the "edit" menu item ##############
#####################iemlib#######################
proc pdtk_canvas_editval {name value} {
    if { $value } {
	$name.m.edit entryconfigure "Edit mode" -indicatoron true
    } else {                          
        $name.m.edit entryconfigure "Edit mode" -indicatoron false
    }                                                 
}
#####################iemlib#######################

############ pdtk_text_new -- create a new text object #2###########
proc pdtk_text_new {canvasname myname x y text font color} {
#    if {$font < 13} {set fontname [format -*-courier-bold----%d-* $font]}
#    if {$font >= 13} {set fontname [format -*-courier-----%d-* $font]}

	global pd_font1 pd_font2 pd_font3 pd_font4 pd_font5 pd_font6 pd_font7 
	switch -- $font {
		8  { set typeface $pd_font1 }
		10 { set typeface $pd_font2 }
		12 { set typeface $pd_font3 }
		14 { set typeface $pd_font4 }
		16 { set typeface $pd_font5 }
		24 { set typeface $pd_font6 }
		36 { set typeface $pd_font7 }
	}

    $canvasname create text $x $y \
    	-font $typeface \
    	-tags $myname -text $text -fill $color  -anchor nw 
#    pd [concat $myname size [$canvasname bbox $myname] \;]
}

################ pdtk_text_set -- change the text ##################
proc pdtk_text_set {canvasname myname text} {
    $canvasname itemconfig $myname -text $text
#    pd [concat $myname size [$canvasname bbox $myname] \;]
}

############### event binding procedures for Pd window ################

proc pdtk_pd_ctrlkey {name key shift} {
#    puts stderr [concat key $key shift $shift]
#    .dummy itemconfig goo -text [concat ---> control-key event $key];
    if {$key == "n" || $key == "N"} {menu_new}
    if {$key == "o" || $key == "O"} {menu_open}
    if {$key == "m" || $key == "M"} {menu_send}
    if {$key == "q" || $key == "Q"} {
    	if {$shift == 1} {menu_really_quit} else    {menu_quit}
    }
    if {$key == "slash"} {menu_audio 1}
    if {$key == "period"} {menu_audio 0}
}

######### startup function.  ##############
# Tell pd the current directory; this is used in case the command line
# asked pd to open something.  Also, get character width and height for
# font sizes 8, 10, 12, 14, 16, and 24.

# tb: user defined typefaces
proc pdtk_pd_startup {version apilist midiapilist fontname} {
#    puts stderr [concat $version $apilist $fontname]
    global pd_myversion pd_apilist pd_midiapilist
    set pd_myversion $version
    set pd_apilist $apilist
    set pd_midiapilist $midiapilist
    global pd_font1 pd_font2 pd_font3 pd_font4 pd_font5 pd_font6 pd_font7

    set pd_font1 [format -*-%s-bold--normal--8-* $fontname]
    set pd_font2 [format -*-%s-bold--normal--10-* $fontname]
    set pd_font3 [format -*-%s-bold--normal--12-* $fontname]
    set pd_font4 [format -*-%s-bold--normal--14-* $fontname]
    set pd_font5 [format -*-%s-bold--normal--16-* $fontname]
    set pd_font6 [format -*-%s-bold--normal--24-* $fontname]
    set pd_font7 [format -*-%s-bold--normal--36-* $fontname]

    set width1 [font measure  $pd_font1 x]
    set height1 [lindex [font metrics $pd_font1] 5]
    set width2 [font measure  $pd_font2 x]
    set height2 [lindex [font metrics $pd_font2] 5]
    set width3 [font measure  $pd_font3 x]
    set height3 [lindex [font metrics $pd_font3] 5]
    set width4 [font measure  $pd_font4 x]
    set height4 [lindex [font metrics $pd_font4] 5]
    set width5 [font measure  $pd_font5 x]
    set height5 [lindex [font metrics $pd_font5] 5]
    set width6 [font measure  $pd_font6 x]
    set height6 [lindex [font metrics $pd_font6] 5]
    set width7 [font measure  $pd_font7 x]
    set height7 [lindex [font metrics $pd_font7] 5]

    set tclpatch [info patchlevel]
    if {$tclpatch == "8.3.0" || \
    	$tclpatch == "8.3.1" || \
	$tclpatch == "8.3.2" || \
	$tclpatch == "8.3.3" } {
    	set oldtclversion 1
    } else {
    	set oldtclversion 0
    }
    pd [concat pd init [pdtk_enquote [pwd]] \
    	8 $width1 $height1 \
    	10 $width2 $height2 \
    	12 $width3 $height3 \
    	14 $width4 $height4 \
    	16 $width5 $height5 \
    	24 $width6 $height6 \
    	36 $width7 $height7 \
    	$oldtclversion \;];

    # add the audio and help menus to the Pd window.  We delayed this
    # so that we'd know the value of "apilist".
    menu_addstd .mbar 

}

##################### DSP ON/OFF, METERS, DIO ERROR ###################
proc pdtk_pd_dsp {value} {
    global ctrls_audio_on
    if {$value == "ON"} {set ctrls_audio_on 1} else {set ctrls_audio_on 0}
#    puts stderr [concat its $ctrls_audio_on]
}

proc pdtk_pd_meters {indb outdb inclip outclip} {
#    puts stderr [concat meters $indb $outdb $inclip $outclip]
    global ctrls_inlevel ctrls_outlevel
    set ctrls_inlevel $indb
    if {$inclip == 1} {
	.controls.inout.in.clip configure -background red
    } else {
	.controls.inout.in.clip configure -background grey
    }
    set ctrls_outlevel $outdb
    if {$outclip == 1} {
	.controls.inout.out.clip configure -background red
    } else {
	.controls.inout.out.clip configure -background grey
    }
    
}

proc pdtk_pd_dio {red} {
#    puts stderr [concat dio $red]
    if {$red == 1} {
    	.controls.dio configure -background red -activebackground red
    } else {
    	.controls.dio configure -background grey -activebackground lightgrey
    }
    	
}

############# text editing from the "edit" menu ###################
set edit_number 1

proc texteditor_send {name} {
    set topname [string trimright $name .text]
    for {set i 0} \
    	{[$name compare [concat 0.0 + [expr $i + 1] chars] < end]} \
    	    {incr i 1} {
    	set cha [$name get [concat 0.0 + $i chars]]
    	scan $cha %c keynum
    	pd [concat pd key 1 $keynum 0 \;]
    }
}

proc texteditor_ok {name} {
    set topname [string trimright $name .text]
    texteditor_send $name
    destroy $topname
}


proc pdtk_pd_texteditor {stuff} {
    global edit_number pd_font3
    set name [format ".text%d" $edit_number]
    set edit_number [expr $edit_number + 1]

    toplevel $name
    wm title $name {TEXT}

    frame $name.buttons
    pack $name.buttons -side bottom -fill x -pady 2m
    button $name.buttons.send -text {Send (Ctrl s)}\
    	-command "texteditor_send $name.text"
    button $name.buttons.ok -text {OK (Ctrl t)}\
    	-command "texteditor_ok $name.text"
    pack $name.buttons.send -side left -expand 1
    pack $name.buttons.ok -side left -expand 1

    text $name.text -relief raised -bd 2 -height 12 -width 60 \
    	-yscrollcommand "$name.scroll set" -font $pd_font3
    scrollbar $name.scroll -command "$name.text yview"
    pack $name.scroll -side right -fill y
    pack $name.text -side left -fill both -expand 1
    $name.text insert end $stuff
    focus $name.text
    bind $name.text <Control-t> {texteditor_ok %W}
    bind $name.text <Control-s> {texteditor_send %W}
}

#  paste text into a text box
proc pdtk_pastetext {} {
    global pdtk_pastebuffer
    set pdtk_pastebuffer ""
    catch {global pdtk_pastebuffer; set pdtk_pastebuffer [selection get]}
#    puts stderr [concat paste $pdtk_pastebuffer]
    for {set i 0} {$i < [string length $pdtk_pastebuffer]} {incr i 1} {
    	set cha [string index $pdtk_pastebuffer $i]
    	scan $cha %c keynum
    	pd [concat pd key 1 $keynum 0 \;]
    }
}

############# open and save dialogs for objects in Pd ##########

proc pdtk_openpanel {target} {
    global pd_opendir
    set filename [tk_getOpenFile \
    	-initialdir $pd_opendir]
    if {$filename != ""} {
    	set directory [string range $filename 0 \
    	    [expr [string last / $filename ] - 1]]
    	set pd_opendir $directory

    	pd [concat $target symbol [pdtk_enquote $filename] \;]
    }
}

proc pdtk_savepanel {target} {
    set filename [tk_getSaveFile]
    if {$filename != ""} {
    	pd [concat $target symbol [pdtk_enquote $filename] \;]
    }
}

########################### comport hack ########################

set com1 0
set com2 0
set com3 0
set com4 0

proc com1_open {} {
    global com1
    set com1 [open com1 w]
    .dummy itemconfig goo -text $com1
    fconfigure $com1 -buffering none
    fconfigure $com1 -mode 19200,e,8,2
}

proc com1_send {str} {
    global com1
    puts -nonewline $com1 $str
}


############# start a polling process to watch the socket ##############
# this is needed for nt, and presumably for Mac as well.
# in UNIX this is handled by a tcl callback (set up in t_tkcmd.c)

if {$pd_nt == 1} {
    proc polleofloop {} {
    	pd_pollsocket
    	after 20 polleofloop
    }

    polleofloop
}

####################### audio dialog ##################3

proc audio_apply {id} {
    global audio_indev1 audio_indev2 audio_indev3 audio_indev4 
    global audio_inchan1 audio_inchan2 audio_inchan3 audio_inchan4
    global audio_inenable1 audio_inenable2 audio_inenable3 audio_inenable4
    global audio_outdev1 audio_outdev2 audio_outdev3 audio_outdev4 
    global audio_outchan1 audio_outchan2 audio_outchan3 audio_outchan4
    global audio_outenable1 audio_outenable2 audio_outenable3 audio_outenable4
    global audio_sr audio_advance

    pd [concat pd audio-dialog \
    	$audio_indev1 \
    	$audio_indev2 \
    	$audio_indev3 \
    	$audio_indev4 \
    	[expr $audio_inchan1 * ( $audio_inenable1 ? 1 : -1 ) ]\
    	[expr $audio_inchan2 * ( $audio_inenable2 ? 1 : -1 ) ]\
    	[expr $audio_inchan3 * ( $audio_inenable3 ? 1 : -1 ) ]\
    	[expr $audio_inchan4 * ( $audio_inenable4 ? 1 : -1 ) ]\
    	$audio_outdev1 \
    	$audio_outdev2 \
    	$audio_outdev3 \
    	$audio_outdev4 \
    	[expr $audio_outchan1 * ( $audio_outenable1 ? 1 : -1 ) ]\
    	[expr $audio_outchan2 * ( $audio_outenable2 ? 1 : -1 ) ]\
    	[expr $audio_outchan3 * ( $audio_outenable3 ? 1 : -1 ) ]\
    	[expr $audio_outchan4 * ( $audio_outenable4 ? 1 : -1 ) ]\
	$audio_sr \
	$audio_advance \
	\;]
}

proc audio_cancel {id} {
    pd [concat $id cancel \;]
}

proc audio_ok {id} {
    audio_apply $id
    audio_cancel $id
}

# callback from popup menu
proc audio_popup_action {buttonname varname devlist index} {
    global audio_indevlist audio_outdevlist $varname
    $buttonname configure -text [lindex $devlist $index]
#    puts stderr [concat popup_action $buttonname $varname $index]
    set $varname $index
}

# create a popup menu
proc audio_popup {name buttonname varname devlist} {
    if [winfo exists $name.popup] {destroy $name.popup}
    menu $name.popup -tearoff false
#    puts stderr [concat $devlist ]
    for {set x 0} {$x<[llength $devlist]} {incr x} {
	$name.popup add command -label [lindex $devlist $x] \
	    -command [list audio_popup_action \
	    	$buttonname $varname $devlist $x] 
    }
    tk_popup $name.popup [winfo pointerx $name] [winfo pointery $name] 0
}

# start a dialog window to select audio devices and settings.  "multi"
# is 0 if only one device is allowed; 1 if one apiece may be specified for
# input and output; and 2 if we can select multiple devices.  "longform"
# (which only makes sense if "multi" is 2) asks us to make controls for
# opening several devices; if not, we get an extra button to turn longform
# on and restart the dialog.

proc pdtk_audio_dialog {id indevlist indev1 indev2 indev3 indev4 \
    	inchan1 inchan2 inchan3 inchan4 \
    	outdevlist outdev1 outdev2 outdev3 outdev4 \
	outchan1 outchan2 outchan3 outchan4 sr advance multi longform} {
    global audio_indev1 audio_indev2 audio_indev3 audio_indev4 
    global audio_inchan1 audio_inchan2 audio_inchan3 audio_inchan4
    global audio_inenable1 audio_inenable2 audio_inenable3 audio_inenable4
    global audio_outdev1 audio_outdev2 audio_outdev3 audio_outdev4
    global audio_outchan1 audio_outchan2 audio_outchan3 audio_outchan4
    global audio_outenable1 audio_outenable2 audio_outenable3 audio_outenable4
    global audio_sr audio_advance
    global audio_indevlist audio_outdevlist

    set audio_indev1 $indev1
    set audio_indev2 $indev2
    set audio_indev3 $indev3
    set audio_indev4 $indev4

    set audio_inchan1 [expr ( $inchan1 > 0 ? $inchan1 : -$inchan1 ) ]
    set audio_inenable1 [expr $inchan1 > 0 ]
    set audio_inchan2 [expr ( $inchan2 > 0 ? $inchan2 : -$inchan2 ) ]
    set audio_inenable2 [expr $inchan2 > 0 ]
    set audio_inchan3 [expr ( $inchan3 > 0 ? $inchan3 : -$inchan3 ) ]
    set audio_inenable3 [expr $inchan3 > 0 ]
    set audio_inchan4 [expr ( $inchan4 > 0 ? $inchan4 : -$inchan4 ) ]
    set audio_inenable4 [expr $inchan4 > 0 ]

    set audio_outdev1 $outdev1
    set audio_outdev2 $outdev2
    set audio_outdev3 $outdev3
    set audio_outdev4 $outdev4

    set audio_outchan1 [expr ( $outchan1 > 0 ? $outchan1 : -$outchan1 ) ]
    set audio_outenable1 [expr $outchan1 > 0 ]
    set audio_outchan2 [expr ( $outchan2 > 0 ? $outchan2 : -$outchan2 ) ]
    set audio_outenable2 [expr $outchan2 > 0 ]
    set audio_outchan3 [expr ( $outchan3 > 0 ? $outchan3 : -$outchan3 ) ]
    set audio_outenable3 [expr $outchan3 > 0 ]
    set audio_outchan4 [expr ( $outchan4 > 0 ? $outchan4 : -$outchan4 ) ]
    set audio_outenable4 [expr $outchan4 > 0 ]

    set audio_sr $sr
    set audio_advance $advance
    set audio_indevlist $indevlist
    set audio_outdevlist $outdevlist

    toplevel $id
    wm title $id {audio}
    wm protocol $id WM_DELETE_WINDOW [concat audio_cancel $id]

    frame $id.buttonframe
    pack $id.buttonframe -side bottom -fill x -pady 2m
    button $id.buttonframe.cancel -text {Cancel}\
    	-command "audio_cancel $id"
    button $id.buttonframe.apply -text {Apply}\
    	-command "audio_apply $id"
    button $id.buttonframe.ok -text {OK}\
    	-command "audio_ok $id"
    pack $id.buttonframe.cancel -side left -expand 1
    pack $id.buttonframe.apply -side left -expand 1
    pack $id.buttonframe.ok -side left -expand 1
    
    	# sample rate and advance
    frame $id.srf
    pack $id.srf -side top
    
    label $id.srf.l1 -text "sample rate:"
    entry $id.srf.x1 -textvariable audio_sr -width 7
    label $id.srf.l2 -text "delay (msec):"
    entry $id.srf.x2 -textvariable audio_advance -width 4
    pack $id.srf.l1 $id.srf.x1 $id.srf.l2 $id.srf.x2 -side left
    
    	# input device 1
    frame $id.in1f
    pack $id.in1f -side top

    checkbutton $id.in1f.x0 -variable audio_inenable1 \
    	-text {input device 1} -anchor e
    button $id.in1f.x1 -text [lindex $indevlist $audio_indev1] \
    	-command [list audio_popup $id $id.in1f.x1 audio_indev1 $indevlist]
    label $id.in1f.l2 -text "channels:"
    entry $id.in1f.x2 -textvariable audio_inchan1 -width 3
    pack $id.in1f.x0 $id.in1f.x1 $id.in1f.l2 $id.in1f.x2 -side left

    	# input device 2
    if {$longform && $multi > 1 && [llength $indevlist] > 1} {
	frame $id.in2f
	pack $id.in2f -side top

    	checkbutton $id.in2f.x0 -variable audio_inenable2 \
    	    -text {input device 2} -anchor e
	button $id.in2f.x1 -text [lindex $indevlist $audio_indev2] \
    	    -command [list audio_popup $id $id.in2f.x1 audio_indev2 $indevlist]
	label $id.in2f.l2 -text "channels:"
	entry $id.in2f.x2 -textvariable audio_inchan2 -width 3
	pack $id.in2f.x0 $id.in2f.x1 $id.in2f.l2 $id.in2f.x2 -side left
    }

    	# input device 3
    if {$longform && $multi > 1 && [llength $indevlist] > 2} {
	frame $id.in3f
	pack $id.in3f -side top

    	checkbutton $id.in3f.x0 -variable audio_inenable3 \
    	    -text {input device 3} -anchor e
	button $id.in3f.x1 -text [lindex $indevlist $audio_indev3] \
    	    -command [list audio_popup $id $id.in3f.x1 audio_indev3 $indevlist]
	label $id.in3f.l2 -text "channels:"
	entry $id.in3f.x2 -textvariable audio_inchan3 -width 3
	pack $id.in3f.x0 $id.in3f.x1 $id.in3f.l2 $id.in3f.x2 -side left
    }

    	# input device 4
    if {$longform && $multi > 1 && [llength $indevlist] > 3} {
	frame $id.in4f
	pack $id.in4f -side top

    	checkbutton $id.in4f.x0 -variable audio_inenable4 \
    	    -text {input device 4} -anchor e
	button $id.in4f.x1 -text [lindex $indevlist $audio_indev4] \
    	    -command [list audio_popup $id $id.in4f.x1 audio_indev4 $indevlist]
	label $id.in4f.l2 -text "channels:"
	entry $id.in4f.x2 -textvariable audio_inchan4 -width 3
	pack $id.in4f.x0 $id.in4f.x1 $id.in4f.l2 $id.in4f.x2 -side left
    }

    	# output device 1
    frame $id.out1f
    pack $id.out1f -side top

    checkbutton $id.out1f.x0 -variable audio_outenable1 -text {output device 1} \
    	-anchor e
    if {$multi == 0} {
    	label $id.out1f.l1 \
	    -text "(same as input device) ..............      "
    } else {
    	button $id.out1f.x1 -text [lindex $outdevlist $audio_outdev1] \
    	    -command \
	    [list audio_popup $id $id.out1f.x1 audio_outdev1 $outdevlist]
    }
    label $id.out1f.l2 -text "channels:"
    entry $id.out1f.x2 -textvariable audio_outchan1 -width 3
    if {$multi == 0} {
    	pack $id.out1f.x0 $id.out1f.l1 $id.out1f.x2 -side left
    } else {
    	pack $id.out1f.x0 $id.out1f.x1 $id.out1f.l2 $id.out1f.x2 -side left
    }

    	# output device 2
    if {$longform && $multi > 1 && [llength $indevlist] > 1} {
	frame $id.out2f
	pack $id.out2f -side top

    	checkbutton $id.out2f.x0 -variable audio_outenable2 \
    	    -text {output device 2} -anchor e
    	button $id.out2f.x1 -text [lindex $outdevlist $audio_outdev2] \
    	    -command \
	    [list audio_popup $id $id.out2f.x1 audio_outdev2 $outdevlist]
	label $id.out2f.l2 -text "channels:"
	entry $id.out2f.x2 -textvariable audio_outchan2 -width 3
    	pack $id.out2f.x0 $id.out2f.x1 $id.out2f.l2 $id.out2f.x2 -side left
    }

    	# output device 3
    if {$longform && $multi > 1 && [llength $indevlist] > 2} {
	frame $id.out3f
	pack $id.out3f -side top

    	checkbutton $id.out3f.x0 -variable audio_outenable3 \
    	    -text {output device 3} -anchor e
    	button $id.out3f.x1 -text [lindex $outdevlist $audio_outdev3] \
    	    -command \
	    [list audio_popup $id $id.out3f.x1 audio_outdev3 $outdevlist]
	label $id.out3f.l2 -text "channels:"
	entry $id.out3f.x2 -textvariable audio_outchan3 -width 3
    	pack $id.out3f.x0 $id.out3f.x1 $id.out3f.l2 $id.out3f.x2 -side left
    }

    	# output device 4
    if {$longform && $multi > 1 && [llength $indevlist] > 3} {
	frame $id.out4f
	pack $id.out4f -side top

    	checkbutton $id.out4f.x0 -variable audio_outenable4 \
    	    -text {output device 4} -anchor e
    	button $id.out4f.x1 -text [lindex $outdevlist $audio_outdev4] \
    	    -command \
	    [list audio_popup $id $id.out4f.x1 audio_outdev4 $outdevlist]
	label $id.out4f.l2 -text "channels:"
	entry $id.out4f.x2 -textvariable audio_outchan4 -width 3
    	pack $id.out4f.x0 $id.out4f.x1 $id.out4f.l2 $id.out4f.x2 -side left
    }

    	# if not the "long form" but if "multi" is 2, make a button to
	# restart with longform set. 
    
    if {$longform == 0 && $multi > 1} {
	frame $id.longbutton
	pack $id.longbutton -side top
    	button $id.longbutton.b -text {use multiple devices} \
    	    -command  {pd pd audio-properties 1 \;}
    	pack $id.longbutton.b
    }
    bind $id.srf.x1 <KeyPress-Return> [concat audio_ok $id]
    bind $id.srf.x2 <KeyPress-Return> [concat audio_ok $id]
    bind $id.in1f.x2 <KeyPress-Return> [concat audio_ok $id]
    bind $id.out1f.x2 <KeyPress-Return> [concat audio_ok $id]
    $id.srf.x1 select from 0
    $id.srf.x1 select adjust end
    focus $id.srf.x1
    pdtk_standardkeybindings $id.srf.x1
    pdtk_standardkeybindings $id.srf.x2
    pdtk_standardkeybindings $id.in1f.x2
    pdtk_standardkeybindings $id.out1f.x2
}

####################### midi dialog ##################

proc midi_apply {id} {
    global midi_indev1 midi_indev2 midi_indev3 midi_indev4 
    global midi_outdev1 midi_outdev2 midi_outdev3 midi_outdev4 midi_alsain midi_alsaout

    pd [concat pd midi-dialog \
    	$midi_indev1 \
    	$midi_indev2 \
    	$midi_indev3 \
    	$midi_indev4 \
    	$midi_outdev1 \
    	$midi_outdev2 \
    	$midi_outdev3 \
    	$midi_outdev4 \
    	$midi_alsain \
    	$midi_alsaout \
	\;]
}

proc midi_cancel {id} {
    pd [concat $id cancel \;]
}

proc midi_ok {id} {
    midi_apply $id
    midi_cancel $id
}

# callback from popup menu
proc midi_popup_action {buttonname varname devlist index} {
    global midi_indevlist midi_outdevlist $varname
    $buttonname configure -text [lindex $devlist $index]
#    puts stderr [concat popup_action $buttonname $varname $index]
    set $varname $index
}

# create a popup menu
proc midi_popup {name buttonname varname devlist} {
    if [winfo exists $name.popup] {destroy $name.popup}
    menu $name.popup -tearoff false
#    puts stderr [concat $devlist ]
    for {set x 0} {$x<[llength $devlist]} {incr x} {
	$name.popup add command -label [lindex $devlist $x] \
	    -command [list midi_popup_action \
	    	$buttonname $varname $devlist $x] 
    }
    tk_popup $name.popup [winfo pointerx $name] [winfo pointery $name] 0
}

# start a dialog window to select midi devices.  "longform" asks us to make
# controls for opening several devices; if not, we get an extra button to
# turn longform on and restart the dialog.
proc pdtk_midi_dialog {id indevlist indev1 indev2 indev3 indev4 \
    	outdevlist outdev1 outdev2 outdev3 outdev4 longform} {
    global midi_indev1 midi_indev2 midi_indev3 midi_indev4 
    global midi_outdev1 midi_outdev2 midi_outdev3 midi_outdev4
    global midi_indevlist midi_outdevlist
    global midi_alsain midi_alsaout

    set midi_indev1 $indev1
    set midi_indev2 $indev2
    set midi_indev3 $indev3
    set midi_indev4 $indev4
    set midi_outdev1 $outdev1
    set midi_outdev2 $outdev2
    set midi_outdev3 $outdev3
    set midi_outdev4 $outdev4
    set midi_indevlist $indevlist
    set midi_outdevlist $outdevlist
    set midi_alsain [llength $indevlist]
    set midi_alsaout [llength $outdevlist]

    toplevel $id
    wm title $id {midi}
    wm protocol $id WM_DELETE_WINDOW [concat midi_cancel $id]

    frame $id.buttonframe
    pack $id.buttonframe -side bottom -fill x -pady 2m
    button $id.buttonframe.cancel -text {Cancel}\
    	-command "midi_cancel $id"
    button $id.buttonframe.apply -text {Apply}\
    	-command "midi_apply $id"
    button $id.buttonframe.ok -text {OK}\
    	-command "midi_ok $id"
    pack $id.buttonframe.cancel -side left -expand 1
    pack $id.buttonframe.apply -side left -expand 1
    pack $id.buttonframe.ok -side left -expand 1
    
    	# input device 1
    frame $id.in1f
    pack $id.in1f -side top

    label $id.in1f.l1 -text "input device 1:"
    button $id.in1f.x1 -text [lindex $indevlist $midi_indev1] \
    	-command [list midi_popup $id $id.in1f.x1 midi_indev1 $indevlist]
    pack $id.in1f.l1 $id.in1f.x1 -side left

    	# input device 2
    if {$longform && [llength $indevlist] > 2} {
	frame $id.in2f
	pack $id.in2f -side top

	label $id.in2f.l1 -text "input device 2:"
	button $id.in2f.x1 -text [lindex $indevlist $midi_indev2] \
    	    -command [list midi_popup $id $id.in2f.x1 midi_indev2 $indevlist]
	pack $id.in2f.l1 $id.in2f.x1 -side left
    }

    	# input device 3
    if {$longform && [llength $indevlist] > 3} {
	frame $id.in3f
	pack $id.in3f -side top

	label $id.in3f.l1 -text "input device 3:"
	button $id.in3f.x1 -text [lindex $indevlist $midi_indev3] \
    	    -command [list midi_popup $id $id.in3f.x1 midi_indev3 $indevlist]
	pack $id.in3f.l1 $id.in3f.x1 -side left
    }

    	# input device 4
    if {$longform && [llength $indevlist] > 4} {
	frame $id.in4f
	pack $id.in4f -side top

	label $id.in4f.l1 -text "input device 4:"
	button $id.in4f.x1 -text [lindex $indevlist $midi_indev4] \
    	    -command [list midi_popup $id $id.in4f.x1 midi_indev4 $indevlist]
	pack $id.in4f.l1 $id.in4f.x1 -side left
    }

    	# output device 1

    frame $id.out1f
    pack $id.out1f -side top
    label $id.out1f.l1 -text "output device 1:"
    button $id.out1f.x1 -text [lindex $outdevlist $midi_outdev1] \
    	-command [list midi_popup $id $id.out1f.x1 midi_outdev1 $outdevlist]
    pack $id.out1f.l1 $id.out1f.x1 -side left

    	# output device 2
    if {$longform && [llength $indevlist] > 2} {
	frame $id.out2f
	pack $id.out2f -side top
    	label $id.out2f.l1 -text "output device 2:"
    	button $id.out2f.x1 -text [lindex $outdevlist $midi_outdev2] \
    	    -command \
	    [list midi_popup $id $id.out2f.x1 midi_outdev2 $outdevlist]
    	pack $id.out2f.l1 $id.out2f.x1 -side left
    }

    	# output device 3
    if {$longform && [llength $indevlist] > 3} {
	frame $id.out3f
	pack $id.out3f -side top
    	label $id.out3f.l1 -text "output device 3:"
    	button $id.out3f.x1 -text [lindex $outdevlist $midi_outdev3] \
    	    -command \
	    [list midi_popup $id $id.out3f.x1 midi_outdev3 $outdevlist]
    	pack $id.out3f.l1 $id.out3f.x1 -side left
    }

    	# output device 4
    if {$longform && [llength $indevlist] > 4} {
	frame $id.out4f
	pack $id.out4f -side top
    	label $id.out4f.l1 -text "output device 4:"
    	button $id.out4f.x1 -text [lindex $outdevlist $midi_outdev4] \
    	    -command \
	    [list midi_popup $id $id.out4f.x1 midi_outdev4 $outdevlist]
    	pack $id.out4f.l1 $id.out4f.x1 -side left
    }

    	# if not the "long form" make a button to
	# restart with longform set. 
    
    if {$longform == 0} {
	frame $id.longbutton
	pack $id.longbutton -side top
    	button $id.longbutton.b -text {use multiple devices} \
    	    -command  {pd pd midi-properties 1 \;}
    	pack $id.longbutton.b
    }
}

proc pdtk_alsa_midi_dialog {id indevlist indev1 indev2 indev3 indev4 \
    	outdevlist outdev1 outdev2 outdev3 outdev4 longform alsa} {
    global midi_indev1 midi_indev2 midi_indev3 midi_indev4 
    global midi_outdev1 midi_outdev2 midi_outdev3 midi_outdev4
    global midi_indevlist midi_outdevlist
    global midi_alsain midi_alsaout

    set midi_indev1 $indev1
    set midi_indev2 $indev2
    set midi_indev3 $indev3
    set midi_indev4 $indev4
    set midi_outdev1 $outdev1
    set midi_outdev2 $outdev2
    set midi_outdev3 $outdev3
    set midi_outdev4 $outdev4
    set midi_indevlist $indevlist
    set midi_outdevlist $outdevlist
    set midi_alsain [llength $indevlist]
    set midi_alsaout [llength $outdevlist]
    
    toplevel $id
    wm title $id {midi}
    wm protocol $id WM_DELETE_WINDOW [concat midi_cancel $id]

    frame $id.buttonframe
    pack $id.buttonframe -side bottom -fill x -pady 2m
    button $id.buttonframe.cancel -text {Cancel}\
    	-command "midi_cancel $id"
    button $id.buttonframe.apply -text {Apply}\
    	-command "midi_apply $id"
    button $id.buttonframe.ok -text {OK}\
    	-command "midi_ok $id"
    pack $id.buttonframe.cancel -side left -expand 1
    pack $id.buttonframe.apply -side left -expand 1
    pack $id.buttonframe.ok -side left -expand 1

    frame $id.in1f
    pack $id.in1f -side top

  if {$alsa == 0} {
    	# input device 1
    label $id.in1f.l1 -text "input device 1:"
    button $id.in1f.x1 -text [lindex $indevlist $midi_indev1] \
    	-command [list midi_popup $id $id.in1f.x1 midi_indev1 $indevlist]
    pack $id.in1f.l1 $id.in1f.x1 -side left

    	# input device 2
    if {$longform && [llength $indevlist] > 2} {
	frame $id.in2f
	pack $id.in2f -side top

	label $id.in2f.l1 -text "input device 2:"
	button $id.in2f.x1 -text [lindex $indevlist $midi_indev2] \
    	    -command [list midi_popup $id $id.in2f.x1 midi_indev2 $indevlist]
	pack $id.in2f.l1 $id.in2f.x1 -side left
    }

    	# input device 3
    if {$longform && [llength $indevlist] > 3} {
	frame $id.in3f
	pack $id.in3f -side top

	label $id.in3f.l1 -text "input device 3:"
	button $id.in3f.x1 -text [lindex $indevlist $midi_indev3] \
    	    -command [list midi_popup $id $id.in3f.x1 midi_indev3 $indevlist]
	pack $id.in3f.l1 $id.in3f.x1 -side left
    }

    	# input device 4
    if {$longform && [llength $indevlist] > 4} {
	frame $id.in4f
	pack $id.in4f -side top

	label $id.in4f.l1 -text "input device 4:"
	button $id.in4f.x1 -text [lindex $indevlist $midi_indev4] \
    	    -command [list midi_popup $id $id.in4f.x1 midi_indev4 $indevlist]
	pack $id.in4f.l1 $id.in4f.x1 -side left
    }

    	# output device 1

    frame $id.out1f
    pack $id.out1f -side top
    label $id.out1f.l1 -text "output device 1:"
    button $id.out1f.x1 -text [lindex $outdevlist $midi_outdev1] \
    	-command [list midi_popup $id $id.out1f.x1 midi_outdev1 $outdevlist]
    pack $id.out1f.l1 $id.out1f.x1 -side left

    	# output device 2
    if {$longform && [llength $indevlist] > 2} {
	frame $id.out2f
	pack $id.out2f -side top
    	label $id.out2f.l1 -text "output device 2:"
    	button $id.out2f.x1 -text [lindex $outdevlist $midi_outdev2] \
    	    -command \
	    [list midi_popup $id $id.out2f.x1 midi_outdev2 $outdevlist]
    	pack $id.out2f.l1 $id.out2f.x1 -side left
    }

    	# output device 3
    if {$longform && [llength $indevlist] > 3} {
	frame $id.out3f
	pack $id.out3f -side top
    	label $id.out3f.l1 -text "output device 3:"
    	button $id.out3f.x1 -text [lindex $outdevlist $midi_outdev3] \
    	    -command \
	    [list midi_popup $id $id.out3f.x1 midi_outdev3 $outdevlist]
    	pack $id.out3f.l1 $id.out3f.x1 -side left
    }

    	# output device 4
    if {$longform && [llength $indevlist] > 4} {
	frame $id.out4f
	pack $id.out4f -side top
    	label $id.out4f.l1 -text "output device 4:"
    	button $id.out4f.x1 -text [lindex $outdevlist $midi_outdev4] \
    	    -command \
	    [list midi_popup $id $id.out4f.x1 midi_outdev4 $outdevlist]
    	pack $id.out4f.l1 $id.out4f.x1 -side left
    }

    	# if not the "long form" make a button to
	# restart with longform set. 
    
    if {$longform == 0} {
	frame $id.longbutton
	pack $id.longbutton -side top
    	button $id.longbutton.b -text {use multiple alsa devices} \
    	    -command  {pd pd midi-properties 1 \;}
    	pack $id.longbutton.b
    }
    }
    if {$alsa} {
    	label $id.in1f.l1 -text "In Ports:"
    	entry $id.in1f.x1 -textvariable midi_alsain -width 4
        pack $id.in1f.l1 $id.in1f.x1 -side left
	label $id.in1f.l2 -text "Out Ports:"
    	entry $id.in1f.x2 -textvariable midi_alsaout -width 4
        pack $id.in1f.l2 $id.in1f.x2 -side left
    }
}

############ pdtk_path_dialog -- dialog window for search path #########

proc path_apply {id} {
    global pd_extrapath pd_verbose
    global pd_path0 pd_path1 pd_path2 pd_path3 pd_path4 
    global pd_path5 pd_path6 pd_path7 pd_path8 pd_path9 

    pd [concat pd path-dialog $pd_extrapath $pd_verbose \
    [pdtk_encodedialog $pd_path0] [pdtk_encodedialog $pd_path1] \
    [pdtk_encodedialog $pd_path2] [pdtk_encodedialog $pd_path3] \
    [pdtk_encodedialog $pd_path4] [pdtk_encodedialog $pd_path5] \
    [pdtk_encodedialog $pd_path6] [pdtk_encodedialog $pd_path7] \
    [pdtk_encodedialog $pd_path8] [pdtk_encodedialog $pd_path9] \;]
}

proc path_cancel {id} {
    pd [concat $id cancel \;]
}

proc path_ok {id} {
    path_apply $id
    path_cancel $id
}

proc pdtk_path_dialog {id extrapath verbose} {
    global pd_extrapath pd_verbose
    global pd_path0 pd_path1 pd_path2 pd_path3 pd_path4 
    global pd_path5 pd_path6 pd_path7 pd_path8 pd_path9 

    set pd_extrapath $extrapath
    set pd_verbose $verbose
    toplevel $id
    wm title $id {PD search path for patches and other files}
    wm protocol $id WM_DELETE_WINDOW [concat path_cancel $id]

    frame $id.buttonframe
    pack $id.buttonframe -side bottom -fill x -pady 2m
    button $id.buttonframe.cancel -text {Cancel}\
    	-command "path_cancel $id"
    button $id.buttonframe.apply -text {Apply}\
    	-command "path_apply $id"
    button $id.buttonframe.ok -text {OK}\
    	-command "path_ok $id"
    pack $id.buttonframe.cancel -side left -expand 1
    pack $id.buttonframe.apply -side left -expand 1
    pack $id.buttonframe.ok -side left -expand 1
    
    frame $id.extraframe
    pack $id.extraframe -side bottom -fill x -pady 2m
    checkbutton $id.extraframe.extra -text {use standard extensions} \
	-variable pd_extrapath -anchor w 
    checkbutton $id.extraframe.verbose -text {verbose} \
	-variable pd_verbose -anchor w 
    button $id.extraframe.save -text {Save all settings}\
    	-command "path_apply $id \; pd pd save-preferences \\;"
    pack $id.extraframe.extra $id.extraframe.verbose $id.extraframe.save \
    	-side left -expand 1
    
    for {set x 0} {$x < 10} {incr x} {
	entry $id.f$x -textvariable pd_path$x -width 80
    	bind $id.f$x <KeyPress-Return> [concat path_ok $id]
    	pdtk_standardkeybindings $id.f$x
	pack $id.f$x -side top
    }

    focus $id.f0
}

proc pd_set {var value} {
	global $var
	set $var $value
}

########## pdtk_startup_dialog -- dialog window for startup options #########

proc startup_apply {id} {
    global pd_nort pd_flags
    global pd_startup0 pd_startup1 pd_startup2 pd_startup3 pd_startup4 
    global pd_startup5 pd_startup6 pd_startup7 pd_startup8 pd_startup9 

    pd [concat pd startup-dialog $pd_nort [pdtk_encodedialog $pd_flags] \
    [pdtk_encodedialog $pd_startup0] [pdtk_encodedialog $pd_startup1] \
    [pdtk_encodedialog $pd_startup2] [pdtk_encodedialog $pd_startup3] \
    [pdtk_encodedialog $pd_startup4] [pdtk_encodedialog $pd_startup5] \
    [pdtk_encodedialog $pd_startup6] [pdtk_encodedialog $pd_startup7] \
    [pdtk_encodedialog $pd_startup8] [pdtk_encodedialog $pd_startup9] \;]

}

proc startup_cancel {id} {
    pd [concat $id cancel \;]
}

proc startup_ok {id} {
    startup_apply $id
    startup_cancel $id
}

proc pdtk_startup_dialog {id nort flags} {
    global pd_nort pd_nt pd_flags
    global pd_startup0 pd_startup1 pd_startup2 pd_startup3 pd_startup4 
    global pd_startup5 pd_startup6 pd_startup7 pd_startup8 pd_startup9 

    set pd_nort $nort
    set pd_flags $flags
    toplevel $id
    wm title $id {Pd binaries to load (on next startup)}
    wm protocol $id WM_DELETE_WINDOW [concat startup_cancel $id]

    frame $id.buttonframe
    pack $id.buttonframe -side bottom -fill x -pady 2m
    button $id.buttonframe.cancel -text {Cancel}\
    	-command "startup_cancel $id"
    button $id.buttonframe.apply -text {Apply}\
    	-command "startup_apply $id"
    button $id.buttonframe.ok -text {OK}\
    	-command "startup_ok $id"
    pack $id.buttonframe.cancel -side left -expand 1
    pack $id.buttonframe.apply -side left -expand 1
    pack $id.buttonframe.ok -side left -expand 1
    
    frame $id.flags
    pack $id.flags -side bottom
    label $id.flags.entryname -text {startup flags}
    entry $id.flags.entry -textvariable pd_flags -width 80
    bind $id.flags.entry <KeyPress-Return> [concat startup_ok $id]
    pdtk_standardkeybindings $id.flags.entry
    pack $id.flags.entryname $id.flags.entry -side left

    frame $id.nortframe
    pack $id.nortframe -side bottom -fill x -pady 2m
    if {$pd_nt != 1} {
    	checkbutton $id.nortframe.nort -text {defeat real-time scheduling} \
	    -variable pd_nort -anchor w
    }
    button $id.nortframe.save -text {Save all settings}\
    	-command "startup_apply $id \; pd pd save-preferences \\;"
    if {$pd_nt != 1} {
    	pack $id.nortframe.nort $id.nortframe.save -side left -expand 1
    } else {
    	pack $id.nortframe.save -side left -expand 1
    }

    for {set x 0} {$x < 10} {incr x} {
	entry $id.f$x -textvariable pd_startup$x -width 80
    	bind $id.f$x <KeyPress-Return> [concat startup_ok $id]
    	pdtk_standardkeybindings $id.f$x
	pack $id.f$x -side top
    }

    focus $id.f0
}