File: Whiteboard.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (3388 lines) | stat: -rw-r--r-- 97,141 bytes parent folder | download | duplicates (4)
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
#  Whiteboard.tcl --
#  
#      This file is part of The Coccinella application. 
#      It implements the actual whiteboard.
#      
#  Copyright (c) 2002-2008  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: Whiteboard.tcl,v 1.98 2008-05-28 09:51:08 matben Exp $

package require anigif
package require moviecontroller
package require uriencode
package require CanvasDraw
package require CanvasText
package require CanvasUtils
package require CanvasCutCopyPaste
package require CanvasCmd
package require CanvasFile
package require FileCache
package require FilePrefs
package require GetFileIface
package require Import
package require ItemInspector
package require Plugins
package require PutFileIface
package require WBPrefs
package require WDialogs
package require UI

package provide Whiteboard 1.0

namespace eval ::WB {
        
    # Add all event hooks.
    # We use 'whiteboardPrefsInitHook' since we depend on Plugins.
    ::hooks::register firstLaunchHook     ::WB::FirstLaunchHook
    ::hooks::register initHook            ::WB::InitHook
    #::hooks::register prefsInitHook       ::WB::InitPrefsHook
    ::hooks::register whiteboardPrefsInitHook       ::WB::InitPrefsHook
    ::hooks::register quitAppHook         ::WB::QuitAppHook
    ::hooks::register quitAppHook         ::WB::SaveAnyState
    ::hooks::register whiteboardCloseHook ::WB::CloseWhiteboard
    ::hooks::register loginHook           ::WB::LoginHook
    ::hooks::register logoutHook          ::WB::LogoutHook

    # Tool button mappings.
    variable btNo2Name 
    variable btName2No
    variable BtName
    array set btNo2Name	{
	00 point  01 move  10 line   11 arrow 
	20 rect   21 oval  30 pen    31 brush
	40 text   41 del   50 paint  51 poly 
	60 arc    61 rot
    }
    array set btName2No {
	point 00  move 01  line  10  arrow 11 
	rect  20  oval 21  pen   30  brush 31
	text  40  del  41  paint 50  poly  51 
	arc   60  rot  61
    }
    set BtName [dict create]
    dict set BtName point [mc "Point tool, click to mark, double click to inspect, right click to popup"]
    dict set BtName move  [mc "Move tool"]
    dict set BtName line  [mc "Line tool"]
    dict set BtName arrow [mc "Arrow tool"]
    dict set BtName rect  [mc "Rectangle tool"]
    dict set BtName oval  [mc "Oval tool"]
    dict set BtName del   [mc "Click to delete"]
    dict set BtName pen   [mc "Pen tool"]
    dict set BtName brush [mc "Brush tool"]
    dict set BtName paint [mc "Paint tool, shift-click for transparency"]
    dict set BtName poly  [mc "Polygon tool, spacebar to cancel"]
    dict set BtName arc   [mc "Arc tool, click to set center, spacebar to cancel"]
    dict set BtName rot   [mc "Rotate tool, select item first"]
    dict set BtName text  [mc "Text tool"]

    # Use option database for customization.
    option add *TopWhiteboard*TRadiobutton.padding  {0}             50

    # Shortcut buttons.
    option add *TopWhiteboard.saveImage            document-save            widgetDefault
    option add *TopWhiteboard.saveDisImage         document-save-Dis         widgetDefault
    option add *TopWhiteboard.openImage            document-open            widgetDefault
    option add *TopWhiteboard.openDisImage         document-open-Dis         widgetDefault
    option add *TopWhiteboard.importImage          document-import          widgetDefault
    option add *TopWhiteboard.importDisImage       document-import-Dis       widgetDefault
    option add *TopWhiteboard.sendImage            mail-send            widgetDefault
    option add *TopWhiteboard.sendDisImage         mail-send-Dis         widgetDefault
    option add *TopWhiteboard.printImage           document-print           widgetDefault
    option add *TopWhiteboard.printDisImage        document-print-Dis        widgetDefault
    option add *TopWhiteboard.stopImage            dialog-error            widgetDefault
    option add *TopWhiteboard.stopDisImage         dialog-error-Dis    widgetDefault

    # Other icons.
    option add *TopWhiteboard.connect16Image       network-disconnect     widgetDefault
    option add *TopWhiteboard.connected16Image     network-connect       widgetDefault
    option add *TopWhiteboard.logoImage            logo            widgetDefault

    # Drawing tool buttons.
    # First define typical names, then redefine special names.
    foreach tname [array names btName2No] {
	option add *TopWhiteboard.tool${tname}Image draw-$tname    widgetDefault
    }
    option add *TopWhiteboard.toolmoveImage   transform-move    widgetDefault
    option add *TopWhiteboard.toolrotImage    transform-rotate  widgetDefault
    option add *TopWhiteboard.toolpaintImage  fill-color        widgetDefault

    # Color selector.
    option add *TopWhiteboard.bwrectImage          bwrect          widgetDefault
    option add *TopWhiteboard.colorSelectorImage   colorSelector   widgetDefault
    option add *TopWhiteboard.colorSelBWImage      colorSelBW      widgetDefault
    option add *TopWhiteboard.colorSelSwapImage    colorSelSwap    widgetDefault
    
    # Canvas selections.
    option add *TopWhiteboard.aSelect              2               widgetDefault
    option add *TopWhiteboard.fgSelectNormal       black           widgetDefault
    option add *TopWhiteboard.fgSelectLocked       red             widgetDefault

    # Special for X11 menus to look ok.
    if {[tk windowingsystem] eq "x11"} {
	option add *TopWhiteboard.Menu.borderWidth 0               50
    }

    # Keeps various geometry info.
    variable dims
    
    # @@@ BAD!!!!!!!!!!!!!!!!!!!!!!???????????????
    # Canvas size; these are also min sizes. Add new line of tools.
    set dims(wCanOri) 350
    set dims(hCanOri) [expr {328 + 28}]
    # Canvas size; with border.
    set dims(wMinCanvas) [expr {$dims(wCanOri) + 2}]
    set dims(hMinCanvas) [expr {$dims(hCanOri) + 2}]
    set dims(x) 30
    set dims(y) 30
    # Total size of the application (not including menu); only temporary values.
    set dims(wRoot) 1    
    set dims(hRoot) 1
    # As above but including the menu.
    set dims(wTot) 1    
    set dims(hTot) 1   
    
    # Total screen dimension.
    set dims(screenH) [winfo vrootheight .]
    set dims(screenW) [winfo vrootwidth .]

    # Unique id for main toplevels
    variable uidmain 0
        
    # Plugin stuff.
    variable menuSpecPublic
    set menuSpecPublic(wpaths) {}
    
    variable iconsInitted 0
    
    # Prefs:
    # Should text inserts be batched?
    set prefs(batchText) 1

    # Delay time in ms for batched text.
    set prefs(batchTextms) 2000

    # Want to fit all movies within canvas?
    set prefs(autoFitMovies) 1

    # Html sizes or point sizes when transferring text items?
    set prefs(useHtmlSizes) 1

    # Offset when duplicating canvas items and when opening images and movies.
    # Needed in ::CanvasUtils::NewImportAnchor
    set prefs(offsetCopy) 16

    # Grid spacing.
    set prefs(gridDist) 40                 
    
    # Mark bounding box (1) or each coords (0).
    set prefs(bboxOrCoords) 0
    
    # Scale factor used when scaling canvas items.
    set prefs(scaleFactor) 1.2
    set prefs(invScaleFac) [expr {1.0/$prefs(scaleFactor)}]

    # Use common CG when scaling more than one item?
    set prefs(scaleCommonCG) 0

    # Fraction of points to strip when straighten.
    set prefs(straightenFrac) 0.3
    
    # Are there a working canvas dash option?
    set prefs(haveDash) 0
    if {![string match "mac*" $this(platform)]} {
	set prefs(haveDash) 1
    }
    
    set ::config(wb,status-label) 0
}

# WB::InitPrefsHook --
# 
#       There is a global 'state' array which contains a generic state
#       that is inherited by instance specific 'state' array '::WB::${w}::state'

proc ::WB::InitPrefsHook {} {
    global  prefs this
    variable gstate
    
    ::Debug 2 "::WB::InitPrefsHook"
    
    # The tool buttons.
    set gstate(tool)      "point"
    set gstate(toolPrev)  "point"
    set gstate(toolCache) "point"
        
    # Bg color for canvas.
    set gstate(bgColCan) white
    
    # fg and bg colors set in color selector; bgCol always white.
    set gstate(fgCol) black
    set gstate(bgCol) white
    
    # Grid on or off.
    set gstate(canGridOn) 0                  
    
    # Line thickness.
    set gstate(penThick) 1	
    
    # Brush thickness.
    set gstate(brushThick) 8	
    
    # Fill color for circles, polygons etc.
    set gstate(fill) 0
    
    # If polygons should be smoothed.
    set gstate(smooth) 0
    
    # Arc styles.
    set gstate(arcstyle) "arc"
    
    # Dash style.
    set gstate(dash) { }
    
    # Font prefs set in menus. Sizes according to html.
    set gstate(fontSize) 2
    set gstate(font) Helvetica
    set gstate(fontWeight) normal
            
    # Constrain movements to 45 degrees, else 90 degree intervals.
    set prefs(45) 1

    #----   url's for streaming live movies ----------------------------------------
    set prefs(shortsMulticastQT) {{   \
      {user specified}   \
      {Bloomberg}          \
      {Hard Radio}       \
      {NPR}  \
      {BBC World TV} } {  \
      {}  \
      www.apple.com/quicktime/showcase/radio/bloomberg/bloombergradio.mov  \
      www.apple.com/quicktime/showcase/radio/hardradio/hardradio.mov  \
      www.apple.com/quicktime/showcase/radio/npr/npr.mov  \
      www.apple.com/quicktime/favorites/bbc_world1/bbc_world1.mov}}

    # States and prefs to be stored in prefs file.
    ::PrefUtils::Add [list  \
      [list prefs(45)              prefs_45              $prefs(45)]             \
      [list prefs(shortsMulticastQT) prefs_shortsMulticastQT $prefs(shortsMulticastQT) userDefault] \
      [list ::WB::gstate(tool)        wb_gstate_tool        $gstate(tool)]           \
      [list ::WB::gstate(bgColCan)    wb_gstate_bgColCan    $gstate(bgColCan)]       \
      [list ::WB::gstate(fgCol)       wb_gstate_fgCol       $gstate(fgCol)]          \
      [list ::WB::gstate(penThick)    wb_gstate_penThick    $gstate(penThick)]       \
      [list ::WB::gstate(brushThick)  wb_gstate_brushThick  $gstate(brushThick)]     \
      [list ::WB::gstate(fill)        wb_gstate_fill        $gstate(fill)]           \
      [list ::WB::gstate(arcstyle)    wb_gstate_arcstyle    $gstate(arcstyle)]       \
      [list ::WB::gstate(fontSize)    wb_gstate_fontSize    $gstate(fontSize)]       \
      [list ::WB::gstate(font)        wb_gstate_font        $gstate(font)]           \
      [list ::WB::gstate(fontWeight)  wb_gstate_fontWeight  $gstate(fontWeight)]     \
      [list ::WB::gstate(smooth)      wb_gstate_smooth      $gstate(smooth)]         \
      [list ::WB::gstate(dash)        wb_gstate_dash        $gstate(dash)]           \
      [list ::WB::gstate(canGridOn)   wb_gstate_canGridOn   $gstate(canGridOn)]  ]    
}

proc ::WB::FirstLaunchHook {} {
    #::WDialogs::WelcomeCanvas
}

proc ::WB::InitHook {} {
    Init
    InitMenuDefs   
}

# WB::Init --
# 
#       Various initializations for the UI stuff.

proc ::WB::Init {} {
    global  this prefs
    variable wbicons
    
    ::Debug 2 "::WB::Init"
        
    # Dashed options. Used both for the Preference menu and ItemInspector.
    # Need to be careful not to use empty string for menu value in -variable
    # because this gives the 'value' value.
    variable dashFull2Short
    variable dashShort2Full

    array set dashFull2Short {
	none " " dotted . dash-dotted -. dashed -
    }
    array set dashShort2Full {
	" " none . dotted -. dash-dotted - dashed
    }
    set dashShort2Full() none    

    # Init canvas utils.
    ::CanvasUtils::Init
    
    # Create the mapping between Html sizes and font point sizes dynamically.
    ::CanvasUtils::CreateFontSizeMapping
        
    # Drag and Drop support...
    set prefs(haveTkDnD) 0
    if {[tk windowingsystem] ne "aqua"} {
	if {![catch {package require tkdnd}]} {
	    set prefs(haveTkDnD) 1
	}    
    }

    variable animateWave
    
    # Defines canvas binding tags suitable for each tool.
    ::CanvasUtils::BindWhiteboardBindtags
    
    # Bindtags instead of binding to toplevel.
    bind TopWhiteboard <Destroy> {+::WB::Free %W}
}

# WB::InitIcons --
# 
#       Get all standard icons using the option database with the
#       preloaded icons as fallback.

proc ::WB::InitIcons {w} {
    variable icons
    variable iconsInitted 1
    variable btNo2Name
    variable btName2No
    variable wbicons
    	
    # Get icons.
    set icons(brokenImage) [::Theme::FindIconSize 32 image-missing]
    
    # Drawing tool buttons.
    foreach name [array names btName2No] {
	set rsrc [option get $w tool${name}Image {}]
	set wbicons($name) [::Theme::FindIconSize 22 $rsrc]
    }
    
    # Color selector.
    foreach name {colorSelector colorSelBW colorSelSwap} {
	set rsrc [option get $w ${name}Image {}]
	set wbicons($name) [::Theme::FindIcon elements/$rsrc]
    }
    set rsrc [option get $w bwrectImage {}]
    set wbicons(bwrect) [::Theme::FindIcon elements/$rsrc]
}

# WB::InitMenuDefs --
# 
#       The menu organization.

proc ::WB::InitMenuDefs {} {
    global  prefs this
    variable menuDefs
    
    ::Debug 2 "::WB::InitMenuDefs"

    if {[string match "mac*" $this(platform)] && $prefs(haveMenus)} {
	set haveAppleMenu 1
    } else {
	set haveAppleMenu 0
    }
    
    # Defines which menus to use; names and labels.
    variable menuBarDef
    set menuBarDef {
	file    mFile
	edit    mEdit
	prefs   mPreferences
	items   mLibrary
	info    mInfo
    }
	
    # All menu definitions for the main (whiteboard) windows as:
    #      {{type name cmd state accelerator opts} {{...} {...} ...}}
    
    # May be customized by jabber, p2p...

    set menuDefs(main,info,aboutwhiteboard)  \
      {command   mAboutCoccinella    {[mc "&About %s" $prefs(appName)]} {::Splash::SplashScreen}    {}}
    set menuDefs(main,info,aboutquicktimetcl)  \
      {command   mAboutQuickTimeTcl  {[mc "&About %s" QuickTimeTcl]} {::Dialogs::AboutQuickTimeTcl}  {}}

    # Only basic functionality.
    set menuDefs(main,file) {
	{command   mImportImage/Movie... {[mc "&Import Image/Movie"]...} {::WB::OnMenuImport}          I}
	{command   mOpenStream...        {[mc "Open St&ream"]...} {::WB::OnMenuOpenURL}         {}}
	{command   mOpenFile...          {[mc "&Open File"]...} {::WB::OnMenuOpenCanvas}      {}}
	{separator}
	{command   mCloseWindow          {[mc "&Close Window"]} {::UI::CloseWindowEvent}      W}
	{command   mSave                 {[mc "&Save"]...} {::WB::OnMenuSaveCanvas}      S}
	{command   mSaveAs               {[mc "Save &As"]...} {::WB::OnMenuSaveAs}          {}}
	{command   mSaveAsItem           {[mc "Sa&ve As Item"]...} {::WB::OnMenuSaveAsItem}      {}}
	{separator}
	{command   mPageSetup            {[mc "Page Set&up"]...} {::WB::OnMenuPageSetup}       {}}
	{command   mPrint...             {[mc "&Print"]...} {::WB::OnMenuPrintCanvas}     P}
	{separator}
	{command   mQuit                 {[mc "&Quit"]} {::UserActions::DoQuit}       Q}
    }
	    
    # If embedded the embedding app should close us down.
    if {$this(embedded)} {
	#lset menuDefs(main,file) end 3 disabled
    } else {
	package require Multicast
    }
    
    set menuDefs(main,edit) {    
	{command     mUndo             {[mc "&Undo"]} {::WB::OnMenuUndo}         Z}
	{command     mRedo             {[mc "Re&do"]} {::WB::OnMenuRedo}         {Shift-Z}}
	{separator}
	{command     mCut              {[mc "Cu&t"]} {::UI::CutEvent}           X}
	{command     mCopy             {[mc "&Copy"]} {::UI::CopyEvent}          C}
	{command     mPaste            {[mc "&Paste"]} {::UI::PasteEvent}         V}
	{command     mSelectAll        {[mc "Select &All"]} {::WB::OnMenuAll}          A}
	{command     mClear            {[mc "Clear"]} {::WB::OnMenuEraseAll}     {}}
	{separator}
	{command     mEditItem...      {[mc "&Edit Item"]...} {::WB::OnMenuItemInspector}  {}}
	{separator}
	{command     mRaise            {[mc "&Raise"]} {::WB::OnMenuRaise}        R}
	{command     mLower            {[mc "&Lower"]} {::WB::OnMenuLower}        L}
	{separator}
	{command     mLarger           {[mc "Larger"]} {::WB::OnMenuLarger}       >}
	{command     mSmaller          {[mc "Smaller"]} {::WB::OnMenuSmaller}      <}
	{cascade     mFlip             {[mc "Flip"]} {}                             {} {} {
	    {command   mHorizontal     {[mc "Horizontal"]} {::WB::OnMenuFlipHorizontal}     {} {}}
	    {command   mVertical       {[mc "Vertical"]} {::WB::OnMenuFlipVertical}       {} {}}}
	}
	{command     mImageLarger      {[mc "Image Larger"]} {::WB::OnMenuImageLarger}   {}}
	{command     mImageSmaller     {[mc "Image Smaller"]} {::WB::OnMenuImageSmaller}  {}}
    }
    
    # These are used not only in the drop-down menus.
    set menuDefs(main,prefs,separator) 	{separator}
    set menuDefs(main,prefs,background)  \
      {command     mBackgroundColor...   {[mc "Background Color"]...} {::CanvasCmd::SetCanvasBgColor $w}    {}}
    set menuDefs(main,prefs,grid)  \
      {checkbutton mGrid             {[mc "Grid"]} {::CanvasCmd::DoCanvasGrid $w}      {} \
      {-variable ::WB::${w}::state(canGridOn)}}
    set menuDefs(main,prefs,thickness)  \
      {cascade     mThickness        {[mc "Thickness"]} {}                                       {} {} {
	{radio   1                 {1} {}                                         {} \
	  {-variable ::WB::${w}::state(penThick)}}
	{radio   2                 {2} {}                                         {} \
	  {-variable ::WB::${w}::state(penThick)}}
	{radio   4                 {4} {}                                         {} \
	  {-variable ::WB::${w}::state(penThick)}}
	{radio   6                 {6} {}                                         {} \
	  {-variable ::WB::${w}::state(penThick)}}}
    }
    set menuDefs(main,prefs,brushthickness)  \
      {cascade     mBrushThickness   {[mc "Brush Thickness"]} {}                                       {} {} {
	{radio   8                 {8} {}                                         {} \
	  {-variable ::WB::${w}::state(brushThick)}}
	{radio   10                {10} {}                                         {} \
	  {-variable ::WB::${w}::state(brushThick)}}
	{radio   12                {12} {}                                         {} \
	  {-variable ::WB::${w}::state(brushThick)}}
	{radio   16                {16} {}                                         {} \
	  {-variable ::WB::${w}::state(brushThick)}}}
    }
    set menuDefs(main,prefs,fill)  \
      {checkbutton mFill             {[mc "Fill"]} {}                                       {} \
      {-variable ::WB::${w}::state(fill)}}
    set menuDefs(main,prefs,smoothness)  \
      {cascade     mLineSmoothness   {[mc "Line Smoothness"]} {}                                       {} {} {
	{radio   None              {[mc "None"]} {set ::WB::${w}::state(smooth) 0}           {} \
	  {-value 0 -variable ::WB::${w}::state(splinesteps)}}
	{radio   2                 {2} {set ::WB::${w}::state(smooth) 1}           {} \
	  {-value 2 -variable ::WB::${w}::state(splinesteps)}}
	{radio   4                 {4} {set ::WB::${w}::state(smooth) 1}           {} \
	  {-value 4 -variable ::WB::${w}::state(splinesteps)}}
	{radio   6                 {6} {set ::WB::${w}::state(smooth) 1}           {} \
	  {-value 6 -variable ::WB::${w}::state(splinesteps)}}
	{radio   10                {10} {set ::WB::${w}::state(smooth) 1}           {} \
	  {-value 10 -variable ::WB::${w}::state(splinesteps)}}}
    }
    set menuDefs(main,prefs,smooth)  \
      {checkbutton mLineSmoothness   {[mc "Line Smoothness"]} {}                                       {} \
      {-variable ::WB::${w}::state(smooth)}}
    set menuDefs(main,prefs,arcs)  \
      {cascade     mArcs             {[mc "Arcs"]} {}                                       {} {} {
	{radio   mPieslice         {[mc "Pieslice"]} {}                                         {} \
	  {-value pieslice -variable ::WB::${w}::state(arcstyle)}}
	{radio   mChord            {[mc "Chord"]} {}                                         {} \
	  {-value chord -variable ::WB::${w}::state(arcstyle)}}
	{radio   mArc              {[mc "Arc"]} {}                                         {} \
	  {-value arc -variable ::WB::${w}::state(arcstyle)}}}
    }

    # Dashes need a special build process. Be sure not to substitute $w.
    set dashList {}
    foreach dash [lsort -decreasing [array names ::WB::dashFull2Short]] {
	set dashval $::WB::dashFull2Short($dash)
	set lname [dict create]
	dict set lname none        [mc "None"]
	dict set lname dotted      [mc "Dotted"]
	dict set lname dashed      [mc "Dashed"]
	dict set lname dash-dotted [mc "Dash-dotted"]
	set str [dict get $lname $dash]
	if {[string equal " " $dashval]} {
	    set dopts {-value { } -variable ::WB::${w}::state(dash)}
	} else {
	    set dopts [format {-value %s -variable ::WB::${w}::state(dash)} $dashval]
	}
	lappend dashList [list radio $dash $str {} {} $dopts]
    }
    set menuDefs(main,prefs,dash)  \
      [list cascade   mDash          {[mc "Dash"]} {}                                       {} {} $dashList]
	
    set menuDefs(main,prefs,constrain)  \
      {cascade     mShiftConstrain   {[mc "Shift Constrain"]} {}                                       {} {} {
	{radio   mTo90degrees      {[mc "To 90 degrees"]} {}                                         {} \
	  {-variable prefs(45) -value 0}}
	{radio   mTo45degrees      {[mc "To 45 degrees"]} {}                                         {} \
	  {-variable prefs(45) -value 1}}}
    }
    set menuDefs(main,prefs,font)  \
      {cascade     mFont             {[mc "Font"]} {}                                       {} {} {}}
    set menuDefs(main,prefs,fontsize)  \
      {cascade     mSize             {[mc "Size"]} {}                                       {} {} {
	{radio   1                 {1} {::WB::FontChanged $w size}             {} \
	  {-variable ::WB::${w}::state(fontSize)}}
	{radio   2                 {2} {::WB::FontChanged $w size}             {} \
	  {-variable ::WB::${w}::state(fontSize)}}
	{radio   3                 {3} {::WB::FontChanged $w size}             {} \
	  {-variable ::WB::${w}::state(fontSize)}}
	{radio   4                 {4} {::WB::FontChanged $w size}             {} \
	  {-variable ::WB::${w}::state(fontSize)}}
	{radio   5                 {5} {::WB::FontChanged $w size}             {} \
	  {-variable ::WB::${w}::state(fontSize)}}
	{radio   6                 {6} {::WB::FontChanged $w size}             {} \
	  {-variable ::WB::${w}::state(fontSize)}}}
    }
    set menuDefs(main,prefs,fontweight)  \
      {cascade     mWeight           {[mc "Weight"]} {}                                       {} {} {
	{radio   mNormal           {[mc "Normal"]} {::WB::FontChanged $w weight}           {} \
	  {-value normal -variable ::WB::${w}::state(fontWeight)}}
	{radio   mBold             {[mc "Bold"]} {::WB::FontChanged $w weight}           {} \
	  {-value bold -variable ::WB::${w}::state(fontWeight)}}
	{radio   mItalic           {[mc "Italic"]} {::WB::FontChanged $w weight}           {} \
	  {-value italic -variable ::WB::${w}::state(fontWeight)}}}
    }
    set menuDefs(main,prefs,prefs)  \
      {command     mPreferences...   {[mc "&Preferences"]...} {::Preferences::Build}                   {}}
    
    # Build hierarchical list.
    set menuDefs(main,prefs) {}
    foreach key {background grid thickness brushthickness fill smooth  \
      arcs dash constrain separator font fontsize fontweight separator prefs} {
	lappend menuDefs(main,prefs) $menuDefs(main,prefs,$key)
    }

    set menuDefs(main,info) {    
	{command     mServer         {[mc "&Server"]}  {::WDialogs::ShowInfoServer}         {}}	
	{command     mPlugins        {[mc "Plu&gins"]} {::WDialogs::InfoOnPlugins}          {}}	
	{separator}
    }
        
    # Make platform specific things and special menus etc. Indices!!! BAD!
    if {!$prefs(haveDash)} {
	lset menuDefs(main,prefs) 7 3 disabled
    }
    if {!$haveAppleMenu} {
	lappend menuDefs(main,info) $menuDefs(main,info,aboutwhiteboard)
    }
    if {!$haveAppleMenu && ![catch {package require QuickTimeTcl 3.1}]} {
	lappend menuDefs(main,info) $menuDefs(main,info,aboutquicktimetcl)
    }
	
    # Menu definitions for a minimal setup. Used on mac only.
    set menuDefs(min,file) {
	{command   mNewWindow        {[mc "New &Window"]} {::WB::NewWhiteboard}         N}
	{command   mCloseWindow      {[mc "&Close Window"]} {::UI::CloseWindowEvent}      W}
	{separator}
	{command   mQuit             {[mc "&Quit"]} {::UserActions::DoQuit}       Q}
    }	    
    set menuDefs(min,edit) {    
	{command   mCut              {[mc "Cu&t"]} {::UI::CutEvent}            X}
	{command   mCopy             {[mc "&Copy"]} {::UI::CopyEvent}           C}
	{command   mPaste            {[mc "&Paste"]} {::UI::PasteEvent}          V}
    }
    
    # Used only on mac until the -postcommand bug fixed.
    if {[tk windowingsystem] eq "aqua"} {
	set menuDefs(main,items) [MakeItemMenuDef $this(itemPath)]
	set altItemsMenuDefs     [MakeItemMenuDef $this(altItemPath)]
	if {[llength $altItemsMenuDefs]} {
	    lappend menuDefs(main,items) {separator}
	    set menuDefs(main,items) [concat $menuDefs(main,items) $altItemsMenuDefs]
	}
    } else {
	set menuDefs(main,items) [list]
    }
    
    # When registering new menu entries they shall be added at:
    variable menuDefsInsertInd

    # Let components register their menus *after* the last separator.
    foreach name {file edit prefs items info} {
	set idx [lindex [lsearch -all $menuDefs(main,$name) separator] end]
	if {$idx < 0} {
	    set idx [llength $menuDefs(main,$name)]
	}
	set menuDefsInsertInd(main,$name) $idx
    }
}

proc ::WB::QuitAppHook {} {
    global  wDlgs
    
    ::UI::SaveWinPrefixGeom $wDlgs(wb) whiteboard
}

# WB::NewWhiteboard --
#
#       Makes a unique whiteboard.
#
# Arguments:
#       args    -file fileName
#               -state normal|disabled 
#               -title name
#               -usewingeom 0|1
#               ?-key value ...? custom arguments
#       
# Results:
#       toplevel widget path

proc ::WB::NewWhiteboard {args} { 
    global wDlgs
    variable uidmain
    
    set w [GetNewToplevelPath]
    eval {BuildWhiteboard $w} $args
    return $w
}

proc ::WB::GetNewToplevelPath {} {
    global wDlgs
    variable uidmain
    
    return $wDlgs(wb)[incr uidmain]
}

# WB::BuildWhiteboard --
#
#       Makes the main toplevel window.
#
# Arguments:
#       w           toplevel widget path
#       args        see above
#       
# Results:
#       new instance toplevel created.

proc ::WB::BuildWhiteboard {w args} {
    global  this prefs config
    
    variable gstate
    variable dims
    variable wbicons
    variable iconsInitted
    
    Debug 2 "::WB::BuildWhiteboard w=$w, args='$args'"
    
    ::Plugins::InitPrefsHook

    namespace eval ::WB::${w}:: {}
    
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::state state
    upvar ::WB::${w}::opts opts
    upvar ::WB::${w}::canvasImages canvasImages
    
    eval {::hooks::run whiteboardPreBuildHook $w} $args
    
    array set opts [list -state normal -title $prefs(theAppName) -usewingeom 0]
    array set opts $args
        
    set wfmain          $w.fmain
    set wfrleft         $wfmain.frleft
    set wfrcan          $wfmain.fc
    set wcomm           $w.fcomm
    set wtbar           $w.tb
    
    # Common widget paths.
    set wapp(toplevel)  $w
    set wapp(menu)      $w.menu
    set wapp(tbar)      $w.tb
    set wapp(tool)      $wfrleft.f.tool
    set wapp(buglabel)  $wfrleft.pad.f.bug
    set wapp(frcan)     $wfrcan
    set wapp(comm)      $wcomm
    set wapp(frstat)    $wcomm.st    
    set wapp(topchilds) [list $w.menu $w.tb $w.fmain $w.fcomm]
    
    # temporary...
    set wapp(can)       $wfrcan.can
    set wapp(xsc)       $wfrcan.xsc
    set wapp(ysc)       $wfrcan.ysc
    set wapp(can,)      $wapp(can)
    
    # Having the frame with canvas + scrollbars as a sibling makes it possible
    # to pack it in a different place.
    set wapp(ccon)      $w.fmain.cc
    
    # Notebook widget path to be packed into wapp(ccon).
    set wapp(nb)        $w.fmain.nb
    
    set canvasImages [list]
    
    # Init some of the state variables.
    # Inherit from the factory + preferences state.
    array set state [array get gstate]
    
    # This is the file we either opened from or last saved.
    set state(fileName) ""
    
    # Keep track if any unsaved edits.
    set state(unsaved) 0
    
    if {$opts(-state) eq "disabled"} {
	set state(tool) "point"
    }
    set state(msg) ""
    
    ::UI::Toplevel $w -class TopWhiteboard \
      -macclass {document {toolbarButton standardDocument}} \
      -closecommand ::WB::CloseHook

    wm withdraw $w
    wm title $w $opts(-title)
    
    bind $w <<ToolbarButton>> [list ::WB::OnToolbarButton $w]

    set iconResize [::Theme::FindIcon elements/sizegrip]
    set wbicons(resizehandle) $iconResize
    if {!$iconsInitted} {
	InitIcons $w
    }
    
    # Note that the order of calls can be critical as any 'update' may trigger
    # network events to attempt drawing etc. Beware!!!
     
    # Start with menus.
    BuildMenus $w
	
    # Special for X11 menus to look ok.
    if {[tk windowingsystem] eq "x11"} {
	ttk::separator $w.stop -orient horizontal
	pack $w.stop -side top -fill x
    }

    # Shortcut buttons at top? Do we want the toolbar to be visible.
    BuildToolbar $w
    pack  $wtbar  -side top -fill x
    
    # Make the connection frame.
    ttk::frame $wcomm
    pack  $wcomm  -side bottom -fill x
    
    # Status message part.
    if {$config(wb,status-label)} {
	ttk::label $wapp(frstat) -style Small.TLabel \
	  -textvariable ::WB::${w}::state(msg) -anchor w -padding {16 2}
	pack  $wapp(frstat)  -side top -fill x

	ttk::separator $wcomm.s -orient horizontal
	pack  $wcomm.s  -side top -fill x
    }
    
    # Build the header for the actual network setup. This is where we
    # may have mode specific parts, p2p, jabber...
    ::hooks::run whiteboardBuildEntryHook $w $wcomm
    
    ttk::separator $w.tsep -orient horizontal
    pack  $w.tsep  -side top -fill x
    set wapp(tsep) $w.tsep
    
    # Make frame for toolbar + canvas.
    frame $w.fmain
    frame $w.fmain.frleft
    frame $w.fmain.frleft.f -relief raised -borderwidth 1
    ttk::frame $w.fmain.frleft.f.tool
    frame $w.fmain.frleft.pad -relief raised -borderwidth 1
    ttk::frame $w.fmain.frleft.pad.f
    frame $w.fmain.cc -bd 1 -relief raised

    pack  $w.fmain            -side bottom -fill both -expand 1
    pack  $w.fmain.frleft     -side left -fill y
    pack  $w.fmain.frleft.f   -fill both
    pack  $w.fmain.frleft.f.tool -side top
    pack  $w.fmain.frleft.pad -fill both -expand 1
    pack  $w.fmain.frleft.pad.f  -fill both -expand 1
    pack  $w.fmain.cc         -fill both -expand 1 -side right
        
    # Make the tool buttons and invoke the one from the prefs file.
    BuildAllButtons $w
    
    # ...and the drawing canvas.
    NewCanvas $wapp(frcan) -background $state(bgColCan)
    set wapp(servCan) $wapp(can)
    pack $wapp(frcan) -in $wapp(ccon) -fill both -expand true -side right
    
    # Invoke tool button.
    SetToolButton $w $state(tool)

    # Add things that are defined in the prefs file and not updated else.
    ::CanvasCmd::DoCanvasGrid $w
    
    # Create the undo/redo object.
    # @@@ This shall be per canvas in the future!
    set state(undotoken) [undo::new]

    # Set up paste menu if something on the clipboard.
    GetFocus $w $w
    bind $w  <FocusIn>  +[list [namespace current]::GetFocus $w %W]

    if {$opts(-usewingeom)} {
	::UI::SetWindowGeometry $w
    } else {
	
	# Set window position only for the first whiteboard on screen.
	# Subsequent whiteboards are placed by the window manager.
	if {[llength [GetAllWhiteboards]] == 1} {	
	    ::UI::SetWindowGeometry $w whiteboard
	}
    }
    if {$prefs(haveTkDnD)} {
	update
	InitDnD $wapp(can)
    }
    catch {wm deiconify $w}
    #raise $w     This makes the window flashing when showed (linux)
    
    # A trick to let the window manager be finished before getting the geometry.
    # An 'update idletasks' needed anyway.
    after idle ::hooks::run whiteboardSetMinsizeHook $w

    if {[info exists opts(-file)]} {
	::CanvasFile::DrawCanvasItemFromFile $w $opts(-file)
    }
    
    ::hooks::run whiteboardPostBuildHook $w
}

proc ::WB::OnToolbarButton {w} {
    upvar ::WB::${w}::wapp wapp

    if {[llength [grab current]]} { return }
    if {[winfo ismapped $wapp(tbar)]} {
	HideToolbar $w
	set show 0
    } else {
	ShowToolbar $w
	set show 1
    }
    ::hooks::run uiWhiteboardToggleToolbar $show
}

proc ::WB::HideToolbar {w} {
    upvar ::WB::${w}::wapp wapp
    
    pack forget $wapp(tbar)
    pack forget $wapp(tsep)
}

proc ::WB::ShowToolbar {w} {
    upvar ::WB::${w}::wapp wapp
    
    pack $wapp(tbar) -side top -fill x
    pack $wapp(tsep) -side top -fill x
}

# WB::NewCanvas --
# 
#       Makes canvas with scrollbars in own frame.

proc ::WB::NewCanvas {w args} {
    global  prefs
    variable dims
    
    array set argsArr {
	-background white
    }
    array set argsArr $args
    
    frame $w -class WBCanvas
    set wcan $w.can
    set wxsc $w.xsc
    set wysc $w.ysc
    
    set bg $argsArr(-background)
    
    canvas $wcan -height $dims(hCanOri) -width $dims(wCanOri)  \
      -relief raised -bd 0 -background $bg -highlightbackground $bg \
      -scrollregion [list 0 0 $prefs(canScrollWidth) $prefs(canScrollHeight)]  \
      -xscrollcommand [list $wxsc set] -yscrollcommand [list $wysc set]	
    ttk::scrollbar $wxsc -command [list $wcan xview] -orient horizontal
    ttk::scrollbar $wysc -command [list $wcan yview] -orient vertical
    
    grid  $wcan  -row 0 -column 0 -sticky news -padx 0 -pady 0
    grid  $wysc  -row 0 -column 1 -sticky ns
    grid  $wxsc  -row 1 -column 0 -sticky ew
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure    $w 0 -weight 1

    ::CanvasText::Init $wcan

    return $wcan
}

# Testing Pages...

namespace eval ::WB {
    
    variable pageuid 0
}

proc ::WB::NewCanvasPage {w name} {
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::state state
    variable pageuid
    
    if {[string equal [winfo class [pack slaves $wapp(ccon)]] "WBCanvas"]} {
	
	# Repack the WBCanvas in notebook page.
	MoveCanvasToPage $w $name
    } else {
	set wnb $wapp(nb)
	set wpage $wnb.p[incr pageuid]
	ttk::frame $wpage
	$wnb add $wpage -text $name

	set frcan $wpage.fc
	NewCanvas $frcan -background $state(bgColCan)
	pack $frcan -fill both -expand true -side right
	set wapp(can,$name) $frcan.can
	set wapp(page,$name) $wpage
    }
}

proc ::WB::MoveCanvasToPage {w name} {
    upvar ::WB::${w}::wapp wapp
    variable pageuid
    
    # Repack the WBCanvas in notebook page.
    pack forget $wapp(frcan)
    
    set wnb $wapp(nb)
    ttk::notebook $wnb
    pack $wnb -in $wapp(ccon) -fill both -expand 1

    #bind $wnb <<NotebookTabChanged>> \
    #  [list [namespace current]::TabChanged $w]
    #ttk::notebook::enableTraversal $wnb

    set wpage $wnb.p[incr pageuid]
    ttk::frame $wpage
    $wnb add $wpage -text $name
    
    pack $wapp(frcan) -in $wpage -fill both -expand 1
    raise $wapp(frcan)
    set wapp(can,$name) $wapp(can,)
    set wapp(page,$name) $wpage
}

proc ::WB::DeleteCanvasPage {w name} {
    upvar ::WB::${w}::wapp wapp
    
    $wapp(nb) forget $wapp(page,$name)
}

proc ::WB::TabChanged {w} {
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::state state

    Debug 3 "::WB::TabChanged name=$name"
    
    set wapp(can) $wapp(can,$name)
    
    # Invoke tool button.
    SetToolButton $w $state(tool)
    ::hooks::run whiteboardSelectPage $w $name
}

proc ::WB::CloseHook {w} {
    
    if {[winfo exists $w] && [string equal [winfo class $w] "TopWhiteboard"]} {
	set wcan [GetCanvasFromWtop $w]
	::Plugins::DeregisterCanvasInstBinds $wcan
	::hooks::run whiteboardCloseHook $w
    }   
}

# WB::CloseWhiteboard --
#
#       Called when closing whiteboard window; cleanup etc.

proc ::WB::CloseWhiteboard {w} {
    
    Debug 3 "::WB::CloseWhiteboard w=$w"
    
    # Verify that window still exists.
    if {![winfo exists $w]} {
	return
    }
    upvar ::WB::${w}::wapp wapp
    
    # Reset and cancel all put/get file operations related to this window!
    # I think we let put operations go on.
    #::PutFileIface::CancelAllWtop $w
    ::GetFileIface::CancelAllWtop $w
    
    # These should be handled internally in Import ???
    ::Import::HttpResetAll $w
    ::Import::Free $w
    DestroyMain $w
}

# WB::DestroyMain --
# 
#       Destroys toplevel whiteboard. Free cleans up.

proc ::WB::DestroyMain {w} {
    global  prefs
    
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::opts opts
    
    Debug 3 "::WB::DestroyMain w=$w"
        
    # Save instance specific 'state' array into generic 'state'.
    if {$opts(-usewingeom)} {
	::UI::SaveWinGeom $w
    } else {
	::UI::SaveWinGeom whiteboard $w
    }
    SaveWhiteboardState $w

    catch {destroy $w}    
    #unset opts
    #unset wapp
    
    # We could do some cleanup here.
    #GarbageImages $w
    #::CanvasUtils::ItemFree $w
    #::UI::FreeMenu $w
}

# WB::CreateImageForWtop --
# 
#       Create an image that gets garbage collected when window closed.
#       
#       name     desired image name; can be empty
#       args     -file
#                -data

proc ::WB::CreateImageForWtop {wtop name args} {
    
    upvar ::WB::${wtop}::canvasImages canvasImages
    
    array set argsArr $args
    if {[info exists argsArr(-file)]} {
	if {[string tolower [file extension $argsArr(-file)]] eq ".gif"} {
	    set photo [::Utils::CreateGif $argsArr(-file) $name]
	} else {
	    if {$name eq ""} {
		set photo [image create photo -file $argsArr(-file)]
	    } else {
		set photo [image create photo $name -file $argsArr(-file)]
	    }
	}
    } else {
	if {$name eq ""} {
	    set photo [image create photo -data $argsArr(-data)]
	} else {
	    set photo [image create photo $name -data $argsArr(-data)]
	}
    }

    lappend canvasImages $photo
    return $photo
}

proc ::WB::AddImageToGarbageCollector {w name} {
    
    upvar ::WB::${w}::canvasImages canvasImages

    lappend canvasImages $name
}

proc ::WB::GarbageImages {w} {
    
    upvar ::WB::${w}::canvasImages canvasImages
    
    foreach name $canvasImages {
	if {[::anigif::isanigif $name]} {
	    ::anigif::delete $name
	} else {
	    if {![catch {image inuse $name}]} {
		catch {image delete $name}
	    }
	}
    }
}

# WB::SaveWhiteboardState
# 
# 

proc ::WB::SaveWhiteboardState {w} {
    upvar ::WB::${w}::wapp wapp
    variable gstate
    
    # Read back instance specific 'state' into generic 'state'.
    array set gstate [array get ::WB::${w}::state]
}

proc ::WB::SaveAnyState {} {
    
    set win ""
    set wbs [GetAllWhiteboards]
    if {[llength $wbs]} {
	set wfocus [focus]
	if {$wfocus ne ""} {
	    set win [winfo toplevel $wfocus]
	}
	set win [lsearch -inline $wbs $wfocus]
	if {$win eq ""} {
	    set win [lindex $wbs 0]
	}
	if {$win ne ""} {
	    ::WB::SaveWhiteboardState $win
	}	
    }
}

proc ::WB::GetStateArray {w} {
    upvar ::WB::${w}::state state

    return [array get state]
}

# WB::SaveWhiteboardDims --
# 
#       Stores the present whiteboard widget geom state in 'dims' array.

proc ::WB::SaveWhiteboardDims {w} {
    global  this

    upvar ::WB::dims dims
    upvar ::WB::${w}::wapp wapp
    
    set wcan $wapp(can)
        	    
    # Update actual size values. 'Root' no menu, 'Tot' with menu.
    #set dims(wStatMess) [winfo width $wapp(statmess)]
    if {[winfo exists $wapp(frstat)]} {
	set dims(wStatMess) [winfo width $wapp(frstat)]
    } else {
	set dims(wStatMess) 0
    }
    set dims(wRoot) [winfo width $w]
    set dims(hRoot) [winfo height $w]
    set dims(x) [winfo x $w]
    set dims(y) [winfo y $w]
    set dims(wTot) $dims(wRoot)
    
    # hMenu seems unreliable!!!
    if {![string match "mac*" $this(platform)]} {
	# MATS: seems to always give 1 Linux not...
	### EAS BEGIN
	set dims(hMenu) 1
	if {[winfo exists $w.#menu]} {
	    set dims(hMenu) [winfo height $w.#menu]
	}
	### EAS END
    } else {
	set dims(hMenu) 0
    }
    set dims(hTot) [expr {$dims(hRoot) + $dims(hMenu)}]
    set dims(wCanvas) [winfo width $wcan]
    set dims(hCanvas) [winfo height $wcan]

    Debug 3 "::WB::SaveWhiteboardDims dims(hRoot)=$dims(hRoot)"
}

# BADDDDDDDD!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# WB::SaveCleanWhiteboardDims --
# 
#       We want to save wRoot and hRoot as they would be without any connections 
#       in the communication frame. Non jabber only. Only needed when quitting
#       to get the correct dims when set from preferences when launched again.

proc ::WB::SaveCleanWhiteboardDims {w} {
    global prefs

    upvar ::WB::dims dims
    upvar ::WB::${w}::wapp wapp

    if {$w ne "."} {
	return
    }
    foreach {dims(wRoot) hRoot dims(x) dims(y)} [::UI::ParseWMGeometry [wm geometry .]] break
    set dims(hRoot) [expr {$dims(hCanvas) + $dims(hStatus) +  \
      $dims(hCommClean) + $dims(hTop) + $dims(hFakeMenu)}]
    incr dims(hRoot) [expr {[winfo height $wapp(xsc)] + 4}]
}

# WB::ConfigureMain --
#
#       Configure the options 'opts' state of a whiteboard.
#       Returns 'opts' if no arguments.

proc ::WB::ConfigureMain {w args} {
    
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::opts opts
    
    if {[llength $args] == 0} {
	return [array get opts]
    } else {

	foreach {name value} $args {
	    
	    switch -- $name {
		-title {
		    wm title $w $value    
		}
		-state {
		    set wmenu $wapp(menu)
		    MenubarSetState $wmenu $value
		    if {[string equal $value "normal"]} {

		    } else {
			DisableShortcutButtonPad $w
		    }
		}
	    }
	}
    }
    eval {::hooks::run whiteboardConfigureHook $w} $args
}

proc ::WB::SetButtonTrayDefs {buttonDefs} {
    variable btShortDefs
    
    set btShortDefs $buttonDefs
}

proc ::WB::SetMenuDefs {key menuDef} {
    variable menuDefs
    
    set menuDefs(main,$key) $menuDef
}

proc ::WB::GetMenuDefs {key} {
    variable menuDefs
    
    return $menuDefs(main,$key)
}

proc ::WB::LoginHook {} {
    
    foreach w [GetAllWhiteboards] {

	# Make menus consistent.
	::hooks::run whiteboardFixMenusWhenHook $w "connect"
    }
}

proc ::WB::LogoutHook {} {
    
    # Multiinstance whiteboard UI stuff.
    foreach w [GetAllWhiteboards] {

	# If no more connections left, make menus consistent.
	::hooks::run whiteboardFixMenusWhenHook $w "disconnect"
    }   
}

# WB::SetStatusMessage --

proc ::WB::SetStatusMessage {w msg} {
    
    # Make it failsafe.
    if {![winfo exists $w]} {
	return
    }
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::state state

    set state(msg) $msg
    #$wapp(statmess) itemconfigure stattxt -text $msg
}

proc ::WB::GetServerCanvasFromWtop {w} {    
    upvar ::WB::${w}::wapp wapp
    
    return $wapp(servCan)
}

proc ::WB::GetCanvasFromWtop {w} {    
    upvar ::WB::${w}::wapp wapp
    
    return $wapp(can)
}

# WB::GetButtonState --
#
#       This is a utility function mainly for plugins to get the tool buttons 
#       state.

proc ::WB::GetButtonState {w} {
    upvar ::WB::${w}::state state

    return $state(tool)
}

# This shall be per canvas in the future!
proc ::WB::GetUndoToken {wcan} { 
    set w [winfo toplevel $wcan]
    upvar ::WB::${w}::state state
    
    return $state(undotoken)
}

proc ::WB::GetButtonTray {w} {
    upvar ::WB::${w}::wapp wapp

    return $wapp(tbar)
}

proc ::WB::GetMenu {w} {

    return [::UI::GetMenuFromWindow $w]
}

# WB::GetAllWhiteboards --
# 
#       Return all whiteboard's toplevel widget paths as a list. 

proc ::WB::GetAllWhiteboards {} {    
    global  wDlgs

    return [lsort -dictionary \
      [lsearch -all -inline -glob [winfo children .] $wDlgs(wb)*]]
}

# WB::ToolCmd --
# 
#       Command for radiobutton tools.

proc ::WB::ToolCmd {w} {
    upvar ::WB::${w}::state state
        
    set state(toolPrev)  $state(toolCache)
    set state(toolCache) $state(tool)

    SetToolButton $w $state(tool)
}

# WB::SetToolButton --
#
#       Uhhh...  When a tool button is clicked.
#       
# Arguments:
#       w           toplevel widget path
#       btName 
#       
# Results:
#       tool buttons created and mapped

proc ::WB::SetToolButton {w btName} {
    global  prefs wapp this
    
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::state state
    upvar ::WB::${w}::opts opts

    Debug 3 "SetToolButton:: w=$w, btName=$btName"
    
    set wcan $wapp(can)
    
    # Deselect text items.
    if {$btName ne "text"} {
	$wcan select clear
    }
    if {$btName eq "del" || $btName eq "text"} {
	::CanvasCmd::DeselectAll $wcan
    }
    
    # Cancel any outstanding polygon drawings.
    ::CanvasDraw::FinalizePoly $wcan -10 -10
    
    $wcan config -cursor {}
    
    RemoveAllBindings $wcan
    SetBindtags $wcan $btName 
    SetItemBinds $wcan $btName 
    SetFrameItemBinds $wcan $btName
    SetToolUI $wcan $btName
    
    # Should this be tool specific?
    SetKeyboardBinds $wcan $btName

    # This is a hook for plugins to register their own bindings.
    # Calls any registered bindings for the plugin, and deregisters old ones.
    ::Plugins::SetCanvasBinds $wcan $state(toolPrev) $btName
}

# WB::SetBindtags --
# 
#       Bindings directly to the canvas widget are dealt with using bindtags.
#
#       The bindtags used:
#         Whiteboard
#         WhiteboardNonText
#         WhiteboardPoint
#         WhiteboardMove
#         WhiteboardLine
#         WhiteboardArrow
#         WhiteboardRect
#         WhiteboardOval
#         WhiteboardText
#         WhiteboardDel
#         WhiteboardPen
#         WhiteboardBrush
#         WhiteboardPaint
#         WhiteboardPoly
#         WhiteboardArc
#         WhiteboardRot
#         
#       Be sure to NEVER use any of these as -class or any other bindtags!
#       
# Arguments:
#       wcan        canvas widget
#       btName      tool button name
#       
# Results:
#       none

proc ::WB::SetBindtags {wcan btName} {
    
    set w [winfo toplevel $wcan]
    
    variable BtName

    switch -- $btName {
	point - move - line - arrow - rect - oval -
	del - pen - brush - paint - poly - arc - rot {
	    
	    # For all nontext items we have generically named bindtags.
	    set tag Whiteboard[string totitle $btName]
	    bindtags $wcan [list $wcan $tag WhiteboardNonText Whiteboard $w all]
	}
	text {
	    bindtags $wcan  \
	      [list $wcan WhiteboardText Whiteboard $w all]
	}
    }
}

# WB::SetKeyboardBinds --
# 
#       Sets edit menu key binds.

proc ::WB::SetKeyboardBinds {wcan btName} {
    global  this
    
    set mod $this(modkey)
        
    # Generic nontext binds.
    # 
    # @@@ Could perhaps made to bind to 'Whiteboard'.
    if {$btName ne "text"} {
	bind $wcan <BackSpace> [list ::CanvasDraw::DeleteSelected $wcan]
	bind $wcan <Delete>    [list ::CanvasDraw::DeleteSelected $wcan]
	bind $wcan <Control-d> [list ::CanvasDraw::DeleteSelected $wcan]
    }
}

# WB::SetItemBinds --
#
#       Mainly sets all button specific item binds.
#       
# Arguments:
#       wcan        canvas widget
#       btName      tool button name
#       
# Results:
#       none

proc ::WB::SetItemBinds {wcan btName} {
    global  this

    set w [winfo toplevel $wcan]

    # These ones are needed to cancel selection since we compete
    # with Button-1 binding to canvas.
    switch -- $this(platform) {
	macosx {
	    $wcan bind std <Control-ButtonRelease-1> {
		::CanvasDraw::CancelBox %W
		::CanvasDraw::CancelPoly %W
	    }
	    $wcan bind std <Control-B1-Motion> {
		::CanvasDraw::CancelBox %W
		::CanvasDraw::CancelPoly %W
	    }
	}
    }

    # Typical B3 bindings independent of tool selected.
    $wcan bind std&&!locked <<ButtonPopup>> {
	::CanvasUtils::DoItemPopup %W %X %Y 
    }
    $wcan bind std&&locked <<ButtonPopup>> {
	::CanvasUtils::DoLockedPopup %W %X %Y 
    }

    switch -- $btName {
	point {
	    $wcan bind std <Double-Button-1>  \
	      [list ::ItemInspector::ItemInspector $wcan current]

	    switch -- $this(platform) {
		macosx {
		    $wcan bind std&&!locked <Button-1> {
			::CanvasUtils::StartTimerToPopupEx %W %X %Y \
			  ::CanvasUtils::DoItemPopup
		    }
		    $wcan bind std&&locked <Button-1> {
			::CanvasUtils::StartTimerToPopupEx %W %X %Y \
			  ::CanvasUtils::DoLockedPopup
		    }
		    $wcan bind std <ButtonRelease-1> {
			::CanvasUtils::StopTimerToPopupEx
		    }
		}
	    }
	}
	move {
	    
	    # Bindings for moving items; movies need special class.
	    # The frame with the movie gets the mouse events, not the canvas.
	    # Binds directly to canvas widget since we want to move selected 
	    # items as well.
	    # With shift constrained move.

	    $wcan bind std&&!locked <Button-1> {
		::CanvasDraw::InitMoveCurrent %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind std&&!locked <B1-Motion> {
		::CanvasDraw::DragMoveCurrent %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind std&&!locked <ButtonRelease-1> {
		::CanvasDraw::FinalMoveCurrent %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind std&&!locked <Shift-B1-Motion> {
		::CanvasDraw::DragMoveCurrent %W [%W canvasx %x] [%W canvasy %y] shift
	    }
	    
	    $wcan bind tbbox&&(oval||rectangle) <Button-1> {
		::CanvasDraw::InitMoveRectPoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&(oval||rectangle) <B1-Motion> {
		::CanvasDraw::DragMoveRectPoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&(oval||rectangle) <ButtonRelease-1> {
		::CanvasDraw::FinalMoveRectPoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&(oval||rectangle) <Shift-B1-Motion> {
		::CanvasDraw::DragMoveRectPoint %W [%W canvasx %x] [%W canvasy %y] shift
	    }
	    
	    $wcan bind tbbox&&(line||polygon) <Button-1> {
		::CanvasDraw::InitMovePolyLinePoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&(line||polygon) <B1-Motion> {
		::CanvasDraw::DragMovePolyLinePoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&(line||polygon) <ButtonRelease-1> {
		::CanvasDraw::FinalMovePolyLinePoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&(line||polygon) <Shift-B1-Motion> {
		::CanvasDraw::DragMovePolyLinePoint %W [%W canvasx %x] [%W canvasy %y] shift
	    }
	    
	    $wcan bind tbbox&&arc <Button-1> {
		::CanvasDraw::InitMoveArcPoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&arc <B1-Motion> {
		::CanvasDraw::DragMoveArcPoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&arc <ButtonRelease-1> {
		::CanvasDraw::FinalMoveArcPoint %W [%W canvasx %x] [%W canvasy %y]
	    }
	    $wcan bind tbbox&&arc <Shift-B1-Motion> {
		::CanvasDraw::DragMoveArcPoint %W [%W canvasx %x] [%W canvasy %y] shift
	    }
	}
	text {
	    ::CanvasText::EditBind $wcan
	}
	del {
	    $wcan bind std&&!locked <Button-1> {
		::CanvasDraw::DeleteCurrent %W
	    }
	}
    }
}

proc ::WB::SetToolUI {wcan btName} {
    global  this

    variable BtName

    set w [winfo toplevel $wcan]
    SetStatusMessage $w [dict get $BtName $btName]

    switch -- $btName {
	point {
	    switch -- $this(platform) {
		macosx {
		    SetStatusMessage $w [mc "Point tool, click to mark, double click to inspect, press to popup"]
		}
		default {
		    SetStatusMessage $w [mc "Point tool, click to mark, double click to inspect, right click to popup"]		      
		}
	    }
	}
	move {
	    $wcan config -cursor fleur
	}
	text {
	    $wcan config -cursor xterm
	}
	pen {
	    $wcan config -cursor pencil
	}
	rot {
	    $wcan config -cursor exchange
	}
    }
}

# WB::SetFrameItemBinds --
#
#       Binding to canvas windows (QTFrame) must be made specifically for each
#       whiteboard instance.
#       Needed in two situations:
#       1) Clicking tool button
#       2) When whiteboard gets focus
#       
# Arguments:
#       wcan        canvas widget
#       btName      tool button name
#       
# Results:
#       none

proc ::WB::SetFrameItemBinds {wcan btName} {
    global  this

    set w [winfo toplevel $wcan]

    bind QTFrame <Button-1> {}
    bind QTFrame <B1-Motion> {}
    bind QTFrame <ButtonRelease-1> {}
    bind QTFrame <Shift-B1-Motion> {}
    bind SnackFrame <Button-1> {}
    bind SnackFrame <B1-Motion> {}
    bind SnackFrame <ButtonRelease-1> {}
    bind SnackFrame <Shift-B1-Motion> {}

    bind QTFrame <<ButtonPopup>> {
	::CanvasUtils::DoQuickTimePopup %W %X %Y 
    }
    bind SnackFrame <<ButtonPopup>> {
	::CanvasUtils::DoWindowPopup %W %X %Y 
    }
    
    switch -- $btName {
	point {

	    switch -- $this(platform) {
		macintosh - macosx {
		    bind QTFrame <Button-1> {
			::CanvasUtils::StartTimerToPopupEx %W %X %Y \
			  ::CanvasUtils::DoQuickTimePopup
		    }
		    bind QTFrame <ButtonRelease-1> {
			::CanvasUtils::StopTimerToPopupEx
		    }
		    bind SnackFrame <Button-1> {
			::CanvasUtils::StartTimerToWindowPopup %W %X %Y 
		    }
		    bind SnackFrame <ButtonRelease-1> {
			::CanvasUtils::StopTimerToWindowPopup
		    }
		}
	    }
	}
	move {
	    
	    # Need to substitute $wcan.
	    bind QTFrame <Button-1>  \
	      [subst {::CanvasDraw::InitMoveFrame $wcan %W %x %y}]
	    bind QTFrame <B1-Motion>  \
	      [subst {::CanvasDraw::DoMoveFrame $wcan %W %x %y}]
	    bind QTFrame <ButtonRelease-1>  \
	      [subst {::CanvasDraw::FinMoveFrame $wcan %W %x %y}]
	    bind QTFrame <Shift-B1-Motion>  \
	      [subst {::CanvasDraw::FinMoveFrame $wcan %W %x %y}]
	    
	    bind SnackFrame <Button-1>  \
	      [subst {::CanvasDraw::InitMoveFrame $wcan %W %x %y}]
	    bind SnackFrame <B1-Motion>  \
	      [subst {::CanvasDraw::DoMoveFrame $wcan %W %x %y}]
	    bind SnackFrame <ButtonRelease-1>  \
	      [subst {::CanvasDraw::FinMoveFrame $wcan %W %x %y}]
	    bind SnackFrame <Shift-B1-Motion>  \
	      [subst {::CanvasDraw::FinMoveFrame $wcan %W %x %y}]
	}
	del {
	    bind QTFrame <Button-1>  \
	      [subst {::CanvasDraw::DeleteFrame $wcan %W %x %y}]
	    bind SnackFrame <Button-1>  \
	      [subst {::CanvasDraw::DeleteFrame $wcan %W %x %y}]
	}
    }
}

# WB::RemoveAllBindings --
#
#       Clears all application defined bindings in the canvas.
#       
# Arguments:
#       wcan   the canvas widget.
#       
# Results:
#       none

proc ::WB::RemoveAllBindings {wcan} {
    
    Debug 3 "::WB::RemoveAllBindings wcan=$wcan"
    
    # List all tags that we may bind to.
    foreach btag {
	all  std  std&&locked  std&&!locked  
	text  tbbox&&arc  tbbox&&(oval||rectangle)
    } {
	foreach seq [$wcan bind $btag] {
	    $wcan bind $btag $seq {}
	}
    }
    
    # Seems necessary for the arc item... More?
    bind $wcan <Shift-B1-Motion> {}
	    
    # Remove any text insertion...
    $wcan focus {}
}

# RegisterCanvasClassBinds, RegisterCanvasInstBinds --
# 
#       Wrapper for plugins custom canvas bindings.
#       *Class* binds to all whiteboards, 
#       *Inst* binds only to the specified instance (w).

proc ::WB::RegisterCanvasClassBinds {name canvasBindList} {
    
    # Register the actual bindings.
    ::Plugins::RegisterCanvasClassBinds $name $canvasBindList
    
    # Must set the bindings for each instance.
    foreach w [::WB::GetAllWhiteboards] {
	upvar ::WB::${w}::state state
	upvar ::WB::${w}::wapp wapp
	
	::Plugins::SetCanvasBinds $wapp(can) $state(toolPrev) $state(tool)
    }
}

proc ::WB::RegisterCanvasInstBinds {wcan name canvasBindList} {

    set w [winfo toplevel $wcan]
    upvar ::WB::${w}::state state

    # Register the actual bindings.
    ::Plugins::RegisterCanvasInstBinds $wcan $name $canvasBindList
    
    # Must set the bindings for this instance.
    ::Plugins::SetCanvasBinds $wcan $state(toolPrev) $state(tool)
}

# WB::HaveCanvasFocus, ... --
#
#       These are functions used for menu and keyboard whiteboard commands.
#       Commands that need an explicit selection requires canvas focus but
#       other commands operate on the frontmost canvas.

proc ::WB::HaveCanvasFocus {} {
    if {[winfo exists [focus]]} {
	if {[winfo class [winfo parent [focus]]] eq "WBCanvas"} {
	    return 1
	}
    }
    return 0
}

proc ::WB::GetFrontmostCanvas {} {
    if {[winfo exists [focus]]} {
	return [GetCanvasFromWtop [winfo toplevel [focus]]]
    }
}

proc ::WB::HaveSelection {wcan} {
    return [expr {[llength [$wcan find withtag selected]] == 0 ? 0 : 1}]
}

proc ::WB::StateNormal {wcan} {
    set w [winfo toplevel $wcan]
    upvar ::WB::${w}::opts opts
    return [expr {($opts(-state) eq "normal") ? 1 : 0}]
}

# Menu events ------------------------------------------------------------------

# File menu.

proc ::WB::OnMenuImport {} {
    if {[llength [grab current]]} { return }
    OnImport [GetFrontmostCanvas]
}

proc ::WB::OnMenuOpenURL {} {
    if {[llength [grab current]]} { return }
    OnOpenURL [GetFrontmostCanvas]
}

proc ::WB::OnMenuOpenCanvas {} {
    if {[llength [grab current]]} { return }
    OnOpenCanvas [GetFrontmostCanvas]
}

proc ::WB::OnMenuSaveCanvas {} {
    if {[llength [grab current]]} { return }
    OnSaveCanvas [GetFrontmostCanvas]
}

proc ::WB::OnMenuSaveAs {} {
    if {[llength [grab current]]} { return }
    OnSaveAs [GetFrontmostCanvas]
}

proc ::WB::OnMenuSaveAsItem {} {
    if {[llength [grab current]]} { return }
    OnSaveAsItem [GetFrontmostCanvas]
}

proc ::WB::OnMenuPageSetup {} {
    if {[llength [grab current]]} { return }
    OnPageSetup [GetFrontmostCanvas]
}

proc ::WB::OnMenuPrintCanvas {} {
    if {[llength [grab current]]} { return }
    OnPrintCanvas [GetFrontmostCanvas]
}

# Edit menu.

proc ::WB::OnMenuUndo {} {
    if {[llength [grab current]]} { return }
    OnUndo [GetFrontmostCanvas]
}

proc ::WB::OnMenuRedo {} {
    if {[llength [grab current]]} { return }
    OnRedo [GetFrontmostCanvas]
}

proc ::WB::OnMenuAll {} {
    if {[llength [grab current]]} { return }
    OnAll [GetFrontmostCanvas]
}

proc ::WB::OnMenuEraseAll {} {
    if {[llength [grab current]]} { return }
    OnEraseAll [GetFrontmostCanvas]
}

proc ::WB::OnMenuRaise {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnRaise [focus]
    }
}

proc ::WB::OnMenuLower {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnLower [focus]
    }
}

proc ::WB::OnMenuLarger {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnLarger [focus]
    }
}

proc ::WB::OnMenuSmaller {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnSmaller [focus]
    }
}

proc ::WB::OnMenuItemInspector {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnItemInspector [focus]
    }
}

proc ::WB::OnMenuFlipHorizontal {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnFlipHorizontal [focus]
    }
}

proc ::WB::OnMenuFlipVertical {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnFlipVertical [focus]
    }
}

proc ::WB::OnMenuImageLarger {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnImageLarger [focus]
    }
}

proc ::WB::OnMenuImageSmaller {} {
    if {[llength [grab current]]} { return }
    if {[HaveCanvasFocus]} {
	OnImageSmaller [focus]
    }
}

# Keyboard events --------------------------------------------------------------

# File events.

proc ::WB::OnImport {wcan} {
    if {[StateNormal $wcan]} {
	::Import::ImportImageOrMovieDlg $wcan
    }
}

proc ::WB::OnOpenURL {wcan} {
    if {[StateNormal $wcan] && [::Plugins::HavePackage QuickTimeTcl]} {
	::Multicast::OpenMulticast $wcan
    }
}

proc ::WB::OnOpenCanvas {wcan} {
    if {[StateNormal $wcan]} {
	::CanvasFile::OpenCanvasFileDlg $wcan
    }
}

proc ::WB::OnSaveCanvas {wcan} {
    ::CanvasFile::Save $wcan
}

proc ::WB::OnSaveAs {wcan} {
    ::CanvasFile::SaveAsDlg $wcan
}

proc ::WB::OnSaveAsItem {wcan} {
    if {[StateNormal $wcan]} {
	::CanvasFile::DoSaveAsItem $wcan
    }
}

proc ::WB::OnPageSetup {wcan} {
    ::UserActions::PageSetup [winfo toplevel $wcan]
}

proc ::WB::OnPrintCanvas {wcan} {
    ::UserActions::DoPrintCanvas $wcan
}

# Edit events.

proc ::WB::OnUndo {wcan} {
    if {[StateNormal $wcan] && [undo::canundo [GetUndoToken $wcan]]} {
	::CanvasCmd::Undo $wcan
    }
}

proc ::WB::OnRedo {wcan} {
    if {[StateNormal $wcan] && [undo::canredo [GetUndoToken $wcan]]} {
	::CanvasCmd::Redo $wcan
    }
}

proc ::WB::OnAll {wcan} {
    ::CanvasCmd::SelectAll $wcan
}

proc ::WB::OnEraseAll {wcan} {
    if {[StateNormal $wcan]} {
	::CanvasCmd::DoEraseAll $wcan
    }
}

proc ::WB::OnLower {wcan} {
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::CanvasCmd::RaiseOrLowerItems $wcan lower
    }
}

proc ::WB::OnRaise {wcan} {
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::CanvasCmd::RaiseOrLowerItems $wcan raise
    }
}

proc ::WB::OnLarger {wcan} {
    global  prefs
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::CanvasCmd::ResizeItem $wcan $prefs(scaleFactor)
    }    
}

proc ::WB::OnSmaller {wcan} {
    global  prefs
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::CanvasCmd::ResizeItem $wcan $prefs(invScaleFac)
    }    
}

proc ::WB::OnItemInspector {wcan} {
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::ItemInspector::ItemInspector $wcan selected
    }
}

proc ::WB::OnFlipHorizontal {wcan} {
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::CanvasCmd::FlipItem $wcan horizontal
    }
}

proc ::WB::OnFlipVertical {wcan} {
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::CanvasCmd::FlipItem $wcan vertical
    }
}

proc ::WB::OnImageLarger {wcan} {
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::Import::ResizeImage $wcan 2 sel auto
    }
}

proc ::WB::OnImageSmaller {wcan} {
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::Import::ResizeImage $wcan -2 sel auto
    }
}

proc ::WB::OnKeyAnyArrow {wcan detail} {
    if {[HaveSelection $wcan] && [StateNormal $wcan]} {
	::CanvasCmd::MoveSelected $wcan $detail
    }
}

# WB::BuildMenus --
#
#       Makes all menus for a toplevel window.
#
# Arguments:
#       w           toplevel widget path
#       
# Results:
#       menu created

proc ::WB::BuildMenus {w} {
    global  this wDlgs prefs dashFull2Short
    
    variable menuDefs
    variable menuBarDef
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::state state
    upvar ::WB::${w}::opts opts
    
    ::Debug 2 "::WB::BuildMenus"
	
    set wcan   $wapp(can)
    set wmenu  $wapp(menu)
    
    if {$prefs(haveMenus)} {
	menu $wmenu -tearoff 0
    } else {
	frame $wmenu -bd 1 -relief raised
    }
    if {[tk windowingsystem] eq "aqua" && $prefs(haveMenus)} {
	set haveAppleMenu 1
    } else {
	set haveAppleMenu 0
    }
    if {$haveAppleMenu} {
	::UI::BuildAppleMenu $w $wmenu.apple $opts(-state)
    }
    foreach {name mLabel} $menuBarDef {
	BuildMenu $w $wmenu $name
    }    
    if {[tk windowingsystem] ne "aqua"} {
	$wmenu.items configure \
	  -postcommand [list ::WB::BuildItemsMenu $w $wmenu.items]
    }

    # Plugin menus if any.
    BuildRegisteredMenus $w $wmenu

    # The edit menu binds directly to canvas.
    ::UI::SetMenuAcceleratorBinds $w $wmenu.file
    ::UI::SetMenuAcceleratorBinds $w $wmenu.prefs
    ::UI::SetMenuAcceleratorBinds $w $wmenu.info

    # Handle '-state disabled' option. Keep Edit/Copy.
    if {$opts(-state) eq "disabled"} {
	MenubarSetState $wmenu disabled
    }
    
    # Use a function for this to dynamically build this menu if needed.
    BuildFontMenu $w $prefs(canvasFonts)    

    $wmenu.file configure -postcommand  \
      [list ::WB::FilePostCommand $w $wmenu.file]
    $wmenu.edit configure -postcommand  \
      [list ::WB::EditPostCommand $w $wmenu.edit]
    $wmenu.prefs configure -postcommand  \
      [list ::WB::PrefsPostCommand $w $wmenu.prefs]

    # End menus; place the menubar.
    if {$prefs(haveMenus)} {
	$w configure -menu $wmenu
    } else {
	pack $wmenu -side top -fill x
    }
}

proc ::WB::BuildMenu {w wmenu name} {
    variable menuDefs
    variable menuBarDef
    variable extraMenuDefs
    variable menuDefsInsertInd

    set lname [dict create]
    dict set lname file    [mc "&File"]
    dict set lname prefs   [mc "&Preferences"]
    dict set lname info    [mc "&Info"]
    dict set lname edit    [mc "&Edit"]
    dict set lname items   [mc "&Library"]
    set str [dict get $lname $name]

    array set mLabel $menuBarDef
    array set locname $menuBarDef
    set menuMerged $menuDefs(main,$name)
    if {[info exists extraMenuDefs(main,$name)]} {
	set menuMerged [eval {
	    linsert $menuMerged $menuDefsInsertInd(main,$name)
	} $extraMenuDefs(main,$name)]
    }
    ::UI::NewMenu $w $wmenu.$name  $mLabel($name)  $str  $menuMerged
}

# WB::RegisterNewMenu --
# 
#       Registers a new whiteboard menu.
#       
# Arguments:
#       mtail       widget tail name
#       name        menu label
#       menuSpec    {type name cmd accelerator opts} {{...} {...} ...}
#       
# Results:
#       menu entries added when whiteboard built.

proc ::WB::RegisterNewMenu {mtail name menuSpec} {    
    variable menuSpecPublic 
	
    # Make a new menu
    if {[lsearch $menuSpecPublic(wpaths) $mtail] < 0} {
	lappend menuSpecPublic(wpaths) $mtail
    }
    set menuSpecPublic($mtail,name) $name
    set menuSpecPublic($mtail,specs) [list $menuSpec]
}

proc ::WB::BuildRegisteredMenus {w wmenu} {
    variable menuSpecPublic
        
    foreach mtail $menuSpecPublic(wpaths) {
	set name  $menuSpecPublic($mtail,name)
	set specs $menuSpecPublic($mtail,specs)
	::UI::NewMenu $w $wmenu.$mtail $name $name $specs
    }
}

# WB::RegisterMenuEntry --
# 
#       Lets plugins/components register their own menu entry.

proc ::WB::RegisterMenuEntry {name menuSpec} {
    
    # Keeps track of all registered menu entries.
    variable extraMenuDefs
    
    # Add these entries in a section above the bottom section.
    # Add separator to section component entries.
    
    if {![info exists extraMenuDefs(main,$name)]} {

	# Add separator if this is the first addon entry.
	set extraMenuDefs(main,$name) {separator}
    }
    lappend extraMenuDefs(main,$name) $menuSpec
}

# WB::MenubarSetState --
#
#       Handle '-state disabled' option. Sets in a readonly state.

proc ::WB::MenubarSetState {wmenu mbstate} {
    variable menuSpecPublic
    
    $wmenu entryconfigure [mc "&Preferences"] -state $mbstate
    $wmenu entryconfigure [mc "&Library"] -state $mbstate
    $wmenu entryconfigure [mc "&Info"] -state $mbstate
	
    # Handle all 'plugins'.
    foreach wpath $menuSpecPublic(wpaths) {
	set name $menuSpecPublic($wpath,name)
	$wmenu entryconfigure $name -state $mbstate
    }
}

namespace eval ::WB:: {
    variable extButtonDefs {}
}

# WB::BuildToolbar --
#
#       Build the actual shortcut button pad.

proc ::WB::BuildToolbar {w} {
    global  wDlgs
    variable btShortDefs
    variable extButtonDefs
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::opts opts
    
    set wcan   $wapp(can)
    set wtbar  $wapp(tbar)

    ::ttoolbar::ttoolbar $wtbar

    # We need to substitute $wcan, $w etc specific for this wb instance.
    foreach {name cmd} $btShortDefs {
	set icon    [::Theme::Find32Icon $w ${name}Image]
	set iconDis [::Theme::Find32Icon $w ${name}DisImage]
	set cmd [subst -nocommands -nobackslashes $cmd]
	set txt [string totitle $name]
	$wtbar newbutton $name -text [mc $txt] \
	  -image $icon -disabledimage $iconDis -command $cmd
    }
    
    # Extra buttons from components if any.
    foreach btdef $extButtonDefs {
	foreach {name icon iconDis cmd} $btdef {
	    set cmd [subst -nocommands -nobackslashes $cmd]
	    set txt [string totitle $name]
	    $wtbar newbutton $name -text [mc $txt] \
	      -image $icon -disabledimage $iconDis -command $cmd
	}
    }

    # Anything special here.
    ::hooks::run whiteboardBuildButtonTrayHook $wtbar

    if {$opts(-state) eq "disabled"} {
	DisableShortcutButtonPad $w
    }
}

proc ::WB::RegisterShortcutButtons {btdefs} {
    variable extButtonDefs

    # Be sure to not have duplicates. Keep order!
    set names {}
    foreach spec $extButtonDefs {
	set name [lindex $spec 0]
	lappend names $name
	set tmpArr($name) $spec
    }
    foreach spec $btdefs {
	set name [lindex $spec 0]
	if {![info exists tmpArr($name)]} {
	    lappend names $name
	    set tmpArr($name) $spec
	}
    }
    set tmpDefs {}
    foreach name $names {
	lappend tmpDefs $tmpArr($name)
    }
    set extButtonDefs $tmpDefs
}

proc ::WB::DeregisterShortcutButton {name} {
    variable extButtonDefs

    set ind 0
    foreach btdef $extButtonDefs {
	if {[string equal [lindex $btdef 0] $name]} {
	    set extButtonDefs [lreplace $extButtonDefs $ind $ind]
	    break
	}
	incr ind
    }
}

# WB::DisableShortcutButtonPad --
#
#       Sets the state of the main to "read only".

proc ::WB::DisableShortcutButtonPad {w} {
    variable btShortDefs
    upvar ::WB::${w}::wapp wapp
    
    set wtbar $wapp(tbar)
    foreach {name cmd} $btShortDefs {

	switch -- $name {
	    save - print - stop {
		continue
	    }
	    default {
		$wtbar buttonconfigure $name -state disabled
	    }
	}
    }
}

# WB::BuildAllButtons --
#
#       Makes the toolbar button pad for the drawing tools.
#       
# Arguments:
#       w           toplevel widget path
#       
# Results:
#       tool buttons created and mapped

proc ::WB::BuildAllButtons {w} {
    global  prefs this
    
    variable btNo2Name 
    variable btName2No
    variable BtName
    variable wbicons
    upvar ::WB::${w}::state state
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::opts opts
    
    set wtool $wapp(tool)
    
    for {set icol 0} {$icol <= 1} {incr icol} {
	for {set irow 0} {$irow <= 6} {incr irow} {
	    set name $btNo2Name($irow$icol)
	    set wlabel $wtool.bt$irow$icol
	    
	    ttk::radiobutton $wlabel -style Toolbutton \
	      -variable ::WB::${w}::state(tool) -value $name \
	      -command [list [namespace current]::ToolCmd $w] \
	      -image $wbicons($name)
	    grid  $wlabel  -row $irow -column $icol -padx 0 -pady 0
	    
	    if {[string equal $opts(-state) "disabled"]} {
		$wlabel state {disabled}
	    } else {
		
		# Handle bindings to popup options.
		if {[string match "mac*" $this(platform)]} {
		    bind $wlabel <Button-1>        \
		      +[list [namespace current]::StartTimerToToolPopup $w %W $name]
		    bind $wlabel <ButtonRelease-1> \
		      [namespace current]::StopTimerToToolPopup
		}
		bind $wlabel <<ButtonPopup>> [list [namespace current]::DoToolPopup $w %W $name]
	    }
	    set str [dict get $BtName $name]
	    if {($name eq "point") && ($this(platform) eq "macosx")} {
		set str [mc "Point tool, click to mark, double click to inspect, press to popup"]
	    }
	    ::balloonhelp::balloonforwindow $wlabel $str
	}
    }
    
    # Make all popups.
    BuildToolPopups $w
    BuildToolPopupFontMenu $w $prefs(canvasFonts)
    
    # Color selector.
    BuildColorSelector $w 
}

proc ::WB::BuildColorSelector {w} {
    variable wbicons
    upvar ::WB::${w}::state state
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::opts opts
    
    set wtool $wapp(tool)
    set wcolsel $wtool.f.col
    set wfg     $wcolsel.fg
    set wbg1    $wcolsel.bg1
    set wbg2    $wcolsel.bg2
    set wbw     $wcolsel.bw
    set wswap   $wcolsel.swap

    set fg $state(fgCol)
    set bg $state(bgCol)

    # Need an extra frame else place gets misplaced.
    ttk::frame $wtool.f
    ttk::label $wcolsel -compound image -image $wbicons(colorSelector)
    label $wfg   -image $wbicons(bwrect) -bd 0 -width 34 -height 24 -bg $fg
    label $wbg1  -image $wbicons(bwrect) -bd 0 -width 14 -height 12 -bg $bg
    label $wbg2  -image $wbicons(bwrect) -bd 0 -width 34 -height 12 -bg $bg
    ttk::label $wbw   -image $wbicons(colorSelBW)
    ttk::label $wswap -image $wbicons(colorSelSwap)
    
    # The ttk::label seems to put an extra 2 pixel border. @@@ BAD
    set off 2
    grid  $wtool.f  -  -sticky ew
    pack  $wcolsel  -side top
    place $wfg   -x [expr {6+$off}]  -y [expr {5+$off}]
    place $wbg1  -x [expr {42+$off}] -y [expr {19+$off}]
    place $wbg2  -x [expr {22+$off}] -y [expr {31+$off}]
    place $wbw   -x  4 -y 33
    place $wswap -x 46 -y  3

    if {![string equal $opts(-state) "disabled"]} {
	bind $wfg   <Button-1> [list [namespace current]::OnColorSelector $w]
	bind $wbw   <Button-1> [list [namespace current]::OnColorSelectorReset $w]
	bind $wswap <Button-1> [list [namespace current]::OnColorSelectorSwitch $w]
    }
    set wapp(colSel)    $wfg
    set wapp(colSelBg1) $wbg1
    set wapp(colSelBg2) $wbg2
}

proc ::WB::BuildToolPopups {w} {
    global  prefs
    
    variable menuDefs
    upvar ::WB::${w}::wapp wapp
    
    set wtool $wapp(tool)
    
    # List of which entries where.
    array set menuArr {
	line       {thickness dash constrain}
	arrow      {thickness dash constrain}
	rect       {thickness fill dash}
	oval       {thickness fill dash}
	pen        {thickness smooth}
	brush      {brushthickness smooth}
	text       {font fontsize fontweight}
	poly       {thickness fill dash smooth}
	arc        {thickness fill dash arcs}
    }
    foreach name [array names menuArr] {
	set mDef($name) {}
	foreach key $menuArr($name) {
	    lappend mDef($name) $menuDefs(main,prefs,$key)
	}
	::UI::NewMenu $w $wtool.pop${name} {} TESTJE $mDef($name)
	if {!$prefs(haveDash) && ([lsearch $menuArr($name) dash] >= 0)} {
	    ::UI::MenuMethod $wtool.pop${name} entryconfigure mDash -state disabled -label [mc "Dash"]
	}
    }
}

# WB::StartTimerToToolPopup, StopTimerToToolPopup, DoToolPopup --
#
#       Some functions to handle the tool popup menu.

proc ::WB::StartTimerToToolPopup {w wbutton name} {
    
    variable toolPopupId
    
    if {[info exists toolPopupId]} {
	catch {after cancel $toolPopupId}
    }
    set toolPopupId [after 1000 \
      [list [namespace current]::DoToolPopup $w $wbutton $name]]
}

proc ::WB::StopTimerToToolPopup {} {
    
    variable toolPopupId

    if {[info exists toolPopupId]} {
	catch {after cancel $toolPopupId}
    }
}

proc ::WB::DoToolPopup {w wbutton name} {
    
    upvar ::WB::${w}::wapp wapp

    set wtool $wapp(tool)
    set wpop $wtool.pop${name}
    if {[winfo exists $wpop]} {
	set x [winfo rootx $w]
	set y [expr {[winfo rooty $wbutton] + [winfo height $wbutton]}]
	tk_popup $wpop $x $y
    }
}

proc ::WB::OnColorSelectorSwitch {w} {
    
    upvar ::WB::${w}::state state
    upvar ::WB::${w}::wapp wapp

    $wapp(colSel)    configure -bg $state(bgCol)
    $wapp(colSelBg1) configure -bg $state(fgCol)
    $wapp(colSelBg2) configure -bg $state(fgCol)
    set tmp $state(fgCol)
    set state(fgCol) $state(bgCol)
    set state(bgCol) $tmp
}

# WB::OnColorSelector --
#
#       Callback procedure for the color selector in the tools frame.
#       
# Arguments:
#       
# Results:
#       color dialog shown.

proc ::WB::OnColorSelector {w} {
    
    upvar ::WB::${w}::state state
    upvar ::WB::${w}::wapp wapp

    set col [tk_chooseColor -initialcolor $state(fgCol)]
    if {$col ne ""} {
	set state(fgCol) $col
	$wapp(colSel) configure -bg $state(fgCol)
    }
}

proc ::WB::OnColorSelectorReset {w} {

    upvar ::WB::${w}::state state
    upvar ::WB::${w}::wapp wapp

    $wapp(colSel)    configure -bg black
    $wapp(colSelBg1) configure -bg white
    $wapp(colSelBg2) configure -bg white
    set state(fgCol) black
    set state(bgCol) white
}
	
# Access functions to make it possible to isolate these variables.

proc ::WB::ToolBtNameToNum {name} {

    variable btName2No 
    return $btName2No($name)
}

proc ::WB::ToolBtNumToName {num} {

    variable btNo2Name     
    return $btNo2Name($num)
}

# ::WB::SetBasicMinsize --
# 
#       Makes a wm minsize call to set the minsize of the basic whiteboard.
#       Shall only be called after idle!

proc ::WB::SetBasicMinsize {w} {
    
    eval {wm minsize $w} [GetBasicWhiteboardMinsize $w]
}

# WB::GetBasicWhiteboardMinsize --
# 
#       Computes the minimum width and height of whiteboard including any menu
#       but excluding any custom made entry parts.

proc ::WB::GetBasicWhiteboardMinsize {w} {
    global  this prefs
    
    variable wbicons
    upvar ::WB::${w}::wapp wapp

    # Let the geometry manager finish before getting widget sizes.
    update idletasks
  
    # The min height.
    # If we have a custom made menubar using a frame with labels (embedded).
    if {$prefs(haveMenus)} {
	set hFakeMenu 0
	if {![string match "mac*" $this(platform)]} {
	     set hMenu 1
	     # In 8.4 it seems that .wb1.#wb1#menu is used.
	     set wmenu_ $w.#${w}#menu
	     if {[winfo exists $wmenu_]} {
		 set hMenu [winfo height $wmenu_]
	     }
	 } else {
	     set hMenu 0
	 }
    } else {
	set hMenu [winfo reqheight $wapp(menu)]
    }
    set hTop 0
    if {[winfo exists $wapp(tbar)]} {
	set hTop [winfo reqheight $wapp(tbar)]
    }
    set hTool     [winfo reqheight $wapp(tool)]
    #set hBugImage [image height $wapp(bugImage)]
    set hBugImage 0
    if {[winfo exists $wapp(frstat)]} {
	set hStatus   [winfo reqheight $wapp(frstat)]
    } else {
	set hStatus 0
    }
    
    # The min width.
    set wButtons  [$wapp(tbar) minwidth]
    
    set wMin [expr {$wButtons + 6}]
    set hMin [expr {$hMenu + $hTop + $hTool + $hBugImage + $hStatus}]
    
    return [list $wMin $hMin]
}

# ::WB::SetCanvasSize --
#
#       From the canvas size, 'cw' and 'ch', set the total application size.
#       
# Arguments:
#
# Results:
#       None.

proc ::WB::SetCanvasSize {w cw ch} {
    global  this
    upvar ::WB::${w}::wapp wapp

    # Compute new root size from the desired canvas size.
    set thick [expr {int([$wapp(can) cget -highlightthickness])}]
    set widthtot  [expr {$cw + [winfo reqwidth $wapp(tool)]}]
    set heighttot [expr {$ch + \
      [winfo reqheight $wapp(comm)] + \
      [winfo reqheight $wapp(tbar)]}]
    incr widthtot  [expr {[winfo reqwidth $wapp(ysc)] + 4 + $thick}]
    incr heighttot [expr {[winfo reqheight $wapp(xsc)] + 4 + $thick}]
    
    # Menu is a bit tricky. Not needed since wm geometry doesn't count it!
    if {0} {
	if {![string match "mac*" $this(platform)]} {
	    # ad hoc !
	    set wmenu "${w}.#[winfo name $w]#menu"
	    if {[winfo exists $wmenu]} {
		incr heighttot [winfo height $wmenu]
	    }
	}
    }
    
    # Make sure not bigger than the screen.
    set wscreen [winfo screenwidth $w]
    set hscreen [winfo screenheight $w]
    if {$widthtot > $wscreen} {
	set widthtot $wscreen
    }
    if {$heighttot > $hscreen} {
	set heighttot $hscreen
    }
    wm geometry $w ${widthtot}x${heighttot}

    Debug 4 "::WB::SetCanvasSize:: cw=$cw, ch=$ch, heighttot=$heighttot, \
      heighttot=$heighttot"
}

proc ::WB::GetCanvasSize {w} {
    upvar ::WB::${w}::wapp wapp

    return [list [winfo width $wapp(can)] [winfo height $wapp(can)]]
}

proc ::WB::SetScrollregion {w swidth sheight} {
    upvar ::WB::${w}::wapp wapp

    $wapp(can) configure -scrollregion [list 0 0 $swidth $sheight]
}

proc ::WB::FilePostCommand {w wmenu} {
    upvar ::WB::${w}::opts opts

    set editable 0
    if {$opts(-state) eq "normal"} {
	set editable 1
    }
    if {$editable} {
	::UI::MenuEnableAll $wmenu
	if {![::Plugins::HavePackage QuickTimeTcl]} {
	    ::UI::MenuMethod $wmenu entryconfigure mOpenStream... -state disabled -label [mc "Open St&ream"]...
	}
    } else {
	::UI::MenuDisableAllBut $wmenu {
	    mNewWindow mCloseWindow mSave mPageSetup mPrint...
	}
    }
    ::hooks::run menuPostCommand whiteboard-file $wmenu
    
    # Workaround for mac bug.
    update idletasks
}

# WB::EditPostCommand --
# 
#       Post command for edit menu.
#       
# Arguments:
#       w           toplevel widget path
#       wmenu       the edit menu
#
# Results:

proc ::WB::EditPostCommand {w wmenu} {
    
    set wfocus [focus]

    # Start by having all off.
    ::UI::MenuDisableAllBut $wmenu {}

    if {[winfo exists $wfocus]} {
	set wclass [winfo class $wfocus]
	if {$wclass eq "Canvas" && [winfo class [winfo parent $wfocus]] eq "WBCanvas"} {
	    set wclass WBCanvas
	}

	switch -- $wclass {
	    TEntry - Entry - Text {
		foreach {mkey mstate} [::UI::GenericCCPMenuStates] {
		    ::UI::MenuMethod $wmenu entryconfigure $mkey -state $mstate
		}	
	    }
	    WBCanvas {
		EditPostCommandWhiteboard $w $wmenu		
	    }
	}
    }

    ::hooks::run menuPostCommand whiteboard-edit $wmenu

    # Workaround for mac bug.
    update idletasks
}

# WB::EditPostCommandWhiteboard --
# 
#       Sets the specific whiteboard edit menu entry states.

proc ::WB::EditPostCommandWhiteboard {w wmenu} {
    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::opts opts

    set wcan $wapp(can)
    set normal 0
    if {$opts(-state) eq "normal"} {
	set normal 1
    }
    set selected [$wcan find withtag selected]
    set len [llength $selected]
    set haveSelection 0
    set haveTextSelection 0
    set haveFlip 0
    set haveImage 0
    set haveText 0
    set haveResize 1
    set flip 0
    set resize 0
    set resizeImage 0
    
    foreach id $selected {
	switch -- [$wcan type $id] {
	    line - polygon {
		set haveFlip 1
		set haveResize 1
	    }
	    rectangle - oval {
		set haveResize 1
	    }
	    image {
		set haveImage 1
	    }
	    text {
		set haveText 1
	    }
	}
    }
    if {$len == 1} {
	if {$haveImage} {
	    set resizeImage 1
	}
	if {$haveFlip} {
	    set flip 1
	}
    }
    if {$len} {
	set haveSelection 1
    } else {
	set t [$wcan select item]
	if {$t ne {}} {
	    set text [$wcan itemcget $t -text]
	    set start [$wcan index $t sel.first]
	    set end [$wcan index $t sel.last]
	    if {[string length [string range $text $start $end]]} {
		set haveTextSelection 1
	    }
	}
    }
    if {$haveResize && !$haveImage && !$haveText} {
	set resize 1
    }

    # Undo and redo.
    if {$normal && [undo::canundo [GetUndoToken $wcan]]} {
	::UI::MenuMethod $wmenu entryconfigure mUndo -state normal -label [mc "&Undo"]
    } else {
	::UI::MenuMethod $wmenu entryconfigure mUndo -state disabled -label [mc "&Undo"]
    }
    if {$normal && [undo::canredo [GetUndoToken $wcan]]} {
	::UI::MenuMethod $wmenu entryconfigure mRedo -state normal -label [mc "Re&do"]
    } else {
	::UI::MenuMethod $wmenu entryconfigure mRedo -state disabled -label [mc "Re&do"]
    }
    
    # Cut, copy and paste menu entries.
    if {$haveSelection} {
	if {$normal} {
	    ::UI::MenuMethod $wmenu entryconfigure mCut  -state normal -label [mc "Cu&t"]
	} else {
	    ::UI::MenuMethod $wmenu entryconfigure mCut  -state disabled -label [mc "Cu&t"]
	}
	::UI::MenuMethod $wmenu entryconfigure mCopy -state normal -label [mc "&Copy"]
    } elseif {$haveTextSelection} {
	::UI::MenuMethod $wmenu entryconfigure mCut  -state normal -label [mc "Cu&t"]
	::UI::MenuMethod $wmenu entryconfigure mCopy -state normal -label [mc "&Copy"]
    } else {
	::UI::MenuMethod $wmenu entryconfigure mCut  -state disabled -label [mc "Cu&t"]
	::UI::MenuMethod $wmenu entryconfigure mCopy -state disabled -label [mc "&Copy"]
    }
    if {[catch {selection get -sel CLIPBOARD} str]} {
	::UI::MenuMethod $wmenu entryconfigure mPaste -state disabled -label [mc "&Paste"]
    } elseif {$normal && ($str ne "")} {
	::UI::MenuMethod $wmenu entryconfigure mPaste -state normal -label [mc "&Paste"]
    } else {
	::UI::MenuMethod $wmenu entryconfigure mPaste -state disabled -label [mc "&Paste"]
    }

    # All and Erase All.
    ::UI::MenuMethod $wmenu entryconfigure mSelectAll -state normal -label [mc "Select &All"]
    if {$normal} {
	::UI::MenuMethod $wmenu entryconfigure mClear -state normal -label [mc "Clear"]
    } else {
	::UI::MenuMethod $wmenu entryconfigure mClear -state disabled -label [mc "Clear"]
    }
    if {!$len || !$normal} {
	
	# There is no selection in the canvas or whiteboard disabled.
	::UI::MenuMethod $wmenu entryconfigure mEditItem... -state disabled -label [mc "&Edit Item"]...
	::UI::MenuMethod $wmenu entryconfigure mRaise -state disabled -label [mc "&Raise"]
	::UI::MenuMethod $wmenu entryconfigure mLower -state disabled -label [mc "&Lower"]
	::UI::MenuMethod $wmenu entryconfigure mLarger -state disabled -label [mc "Larger"]
	::UI::MenuMethod $wmenu entryconfigure mSmaller -state disabled -label [mc "Smaller"]
	::UI::MenuMethod $wmenu entryconfigure mFlip -state disabled -label [mc "Flip"]
	::UI::MenuMethod $wmenu entryconfigure mImageLarger -state disabled -label [mc "Image Larger"]
	::UI::MenuMethod $wmenu entryconfigure mImageSmaller -state disabled -label [mc "Image Smaller"]  
    } else {	
	::UI::MenuMethod $wmenu entryconfigure mEditItem... -state normal -label [mc "&Edit Item"]...
	::UI::MenuMethod $wmenu entryconfigure mRaise -state normal -label [mc "&Raise"]
	::UI::MenuMethod $wmenu entryconfigure mLower -state normal -label [mc "&Lower"]
	if {$flip} {
	    ::UI::MenuMethod $wmenu entryconfigure mFlip -state normal -label [mc "Flip"]
	} else {
	    ::UI::MenuMethod $wmenu entryconfigure mFlip -state disabled -label [mc "Flip"]
	}
	if {$resizeImage} {
	    ::UI::MenuMethod $wmenu entryconfigure mImageLarger -state normal -label [mc "Image Larger"]
	    ::UI::MenuMethod $wmenu entryconfigure mImageSmaller -state normal -label [mc "Image Smaller"]  
	} else {
	    ::UI::MenuMethod $wmenu entryconfigure mImageLarger -state disabled -label [mc "Image Larger"]
	    ::UI::MenuMethod $wmenu entryconfigure mImageSmaller -state disabled -label [mc "Image Smaller"]  
	}	
	if {$resize} {
	    ::UI::MenuMethod $wmenu entryconfigure mLarger -state normal -label [mc "Larger"]
	    ::UI::MenuMethod $wmenu entryconfigure mSmaller -state normal -label [mc "Smaller"]
	} else {
	    ::UI::MenuMethod $wmenu entryconfigure mLarger -state disabled -label [mc "Larger"]
	    ::UI::MenuMethod $wmenu entryconfigure mSmaller -state disabled -label [mc "Smaller"]
	}
    }
}

proc ::WB::PrefsPostCommand {w wmenu} {
    
    # Complete menu disabled for disabled whiteboard.
    
    ::hooks::run menuPostCommand whiteboard-prefs $wmenu
    
    # Workaround for mac bug.
    update idletasks
}

# WB::GetFocus --
#
#       Check clipboard and activate corresponding menus. ???   
#       
# Results:
#       none.

proc ::WB::GetFocus {w wevent} {
    
    upvar ::WB::${w}::opts opts
    upvar ::WB::${w}::wapp wapp

    set wcan $wapp(can)

    # Bind to toplevel may fire multiple times.
    if {$w ne $wevent} {
	return
    }
    Debug 3 "GetFocus:: w=$w, wevent=$wevent"
    
    SetFrameItemBinds $wcan [GetButtonState $w]
}

# WB::MakeItemMenuDef --
# 
#       Makes a menuDefs list recursively for canvas files.
#       Only for mac until -postcommand bug fixed.

proc ::WB::MakeItemMenuDef {dir} {
    
    set mdef {}
    foreach f [glob -nocomplain -directory $dir -- *] {
	
	# Sort out directories we shouldn't search.
	switch -- [string tolower [file tail $f]] {
	    . - resource.frk - cvs {
		continue
	    }
	}
	if {[file isdirectory $f]} {
	    set submdef [MakeItemMenuDef $f]
	    set name [file tail $f]
	    if {[llength $submdef]} {
		lappend mdef [list cascade $name $name {} {} {} $submdef]
	    }
	} elseif {[string equal [file extension $f] ".can"]} {
	    set name [file rootname [file tail $f]]
	    set cmd {::CanvasFile::DrawCanvasItemFromFile $w}

	    # Important to protect any $ since we do 'subst'.
	    lappend cmd [string map {$ \\$} $f]
	    lappend mdef [list command $name $name $cmd {}]
	}
    }
    return $mdef
}

# WB::BuildItemsMenu, AddItemsMenu --
# 
#       Builds items (library) menu dynamically from -postcommand.

proc ::WB::BuildItemsMenu {w m} {
    global  this
    
    foreach msub [winfo children $m] {
	destroy $msub
    }
    $m delete 0 end
 
    AddItemsMenu $w $m $this(itemPath)
    if {[HaveAnyCanFiles $this(altItemPath)]} {
	$m add separator
	AddItemsMenu $w $m $this(altItemPath)
    }
    
    # Workaround for mac bug. Still doesn't work for submenus.
    update idletasks
}

proc ::WB::AddItemsMenu {w m dir} {
    
    set n 0
    foreach f [glob -nocomplain -directory $dir -- *] {
	
	# Sort out directories we shouldn't search.
	switch -- [string tolower [file tail $f]] {
	    . - resource.frk - cvs {
		continue
	    }
	}
	if {[file isdirectory $f] && [HaveAnyCanFiles $f]} {
	    set msub $m.$n
	    menu $msub -tearoff 0
	    set nsub [AddItemsMenu $w $msub $f]
	    set name [file tail $f]
	    $m add cascade -label $name -menu $msub
	} elseif {[string equal [file extension $f] ".can"]} {
	    set name [file rootname [file tail $f]]
	    set cmd [list ::CanvasFile::DrawCanvasItemFromFile $w $f]
	    $m add command -label $name -command $cmd
	}
	incr n
    }
    return $n
}

proc ::WB::HaveAnyCanFiles {dir} {
    
    foreach f [glob -nocomplain -directory $dir -- *] {
	if {[file isdirectory $f]} {
	    if {[HaveAnyCanFiles $f]} {
		return 1
	    }
	} elseif {[string equal [file extension $f] ".can"]} {
	    return 1
	}
    }
    return 0
}

# WB::BuildFontMenu ---
# 
#       Creates the font selection menu, and removes any old.
#    
# Arguments:
#       mt         The menu path.
#       allFonts   List of names of the fonts.
#       
# Results:
#       font submenu built.

proc ::WB::BuildFontMenu {w allFonts} {
    
    set mt $w.menu.prefs.mfont
    
    $mt delete 0 end
    foreach afont $allFonts {
	$mt add radio -label $afont -variable ::WB::${w}::state(font)  \
	  -command [list ::WB::FontChanged $w name]
    }
    
    # Be sure that the presently selected font family is still there,
    # else choose helvetica.
    set fontStateVar ::WB::${w}::state(font)
    if {[lsearch -exact $allFonts $fontStateVar] == -1} {
	set ::WB::${w}::state(font) {Helvetica}
    }
}

proc ::WB::BuildToolPopupFontMenu {w allFonts} {
    upvar ::WB::${w}::wapp wapp
    
    set wtool $wapp(tool)
    set mt $wtool.poptext.mfont
    
    $mt delete 0 end
    foreach afont $allFonts {
	$mt add radio -label $afont -variable ::WB::${w}::state(font)  \
	  -command [list ::WB::FontChanged $w name]
    }
}

proc ::WB::BuildAllFontMenus {allFonts} {

    # Must do this for all open whiteboards!
    foreach w [GetAllWhiteboards] {
	BuildFontMenu $w $allFonts
	BuildToolPopupFontMenu $w $allFonts
    }
}

# WB::FontChanged --
# 
#       Callback procedure for the font menu. When new font name, size or weight,
#       and we have focus on a text item, change the font spec of this item.
#
# Arguments:
#       w           toplevel widget path
#       what        name, size or weight
#       
# Results:
#       updates text item, sends to all clients.

proc ::WB::FontChanged {w what} {
    global  fontSize2Points fontPoints2Size

    upvar ::WB::${w}::wapp wapp
    upvar ::WB::${w}::state state
    
    set wcan $wapp(can)

    # If there is a focus on a text item, change the font for this item.
    set idfocus [$wcan focus]
    
    if {[string length $idfocus] > 0} {
	set theItno [::CanvasUtils::GetUtag $wcan focus]
	if {[string length $theItno] == 0} {
	    return
	}
	if {[$wcan type $theItno] ne "text"} {
	    return
	}
	set fontSpec [$wcan itemcget $theItno -font]
	if {[llength $fontSpec] > 0} {
	    array set whatToInd {name 0 size 1 weight 2}
	    array set whatToPref {name font size fontSize weight fontWeight}
	    set ind $whatToInd($what)

	    # Need to translate html size to point size.
	    if {$what eq "size"} {
		set newFontSpec [lreplace $fontSpec $ind $ind  \
		  $fontSize2Points($state($whatToPref($what)))]
	    } else {
		set newFontSpec [lreplace $fontSpec $ind $ind  \
		  $state($whatToPref($what))]
	    }
	    ::CanvasUtils::ItemConfigure $wcan $theItno -font $newFontSpec
	}
    }
}

proc ::WB::StartStopAnimatedWave {w start} {
    
}

# WB::CreateBrokenImage --
# 
#       Creates an actual image with the broken symbol that matches
#       up the width and height. The image is garbage collected.

proc ::WB::CreateBrokenImage {wcan width height} {
    variable icons
    
    set w [winfo toplevel $wcan]
    upvar ::WB::${w}::canvasImages canvasImages
    
    if {($width == 0) || ($height == 0)} {
	set name $icons(brokenImage)
    } else {
	set zoomx [expr {$width/[image width $icons(brokenImage)]}]
	set zoomy [expr {$height/[image height $icons(brokenImage)]}]
	if {($zoomx < 1) && ($zoomy < 1)} {
	    set name $icons(brokenImage)
	} else {
	    set zoomx [expr {$zoomx < 1 ? 1 : $zoomx}]
	    set zoomy [expr {$zoomy < 1 ? 1 : $zoomy}]
	    set name [image create photo -width $width -height $height]
	    $name blank
	    $name copy $icons(brokenImage) -to 0 0 $width $height  \
	      -zoom $zoomx $zoomy -compositingrule overlay
	    lappend canvasImages $name
	}
    }
    return $name
}

proc ::WB::InitDnD {wcan} {
    
    dnd bindtarget $wcan text/uri-list <Drop>      [list ::WB::DnDDrop %W %D %T %x %y]   
    dnd bindtarget $wcan text/uri-list <DragEnter> [list ::WB::DnDEnter %W %A %D %T]   
    dnd bindtarget $wcan text/uri-list <DragLeave> [list ::WB::DnDLeave %W %D %T]       
}

proc ::WB::DnDDrop {wcan data type x y} {
    global  prefs
    
    ::Debug 2 "::WB::DnDDrop data=$data, type=$type"

    set w [winfo toplevel $wcan]

    foreach f $data {
	
	# Strip off any file:// prefix.
	set f [string map {file:// ""} $f]
	set f [::uri::urn::unquote $f]
	
	# Allow also .can files to be dropped.
	if {[file extension $f] eq ".can"} {
	    ::CanvasFile::DrawCanvasItemFromFile $w $f
	} else {
	    set mime [::Types::GetMimeTypeForFileName $f]
	    set haveImporter [::Plugins::HaveImporterForMime $mime]
	    if {$haveImporter} {
		set opts [list -coords [list $x $y]]
		set errMsg [::Import::DoImport $wcan $opts -file $f]
		if {$errMsg ne ""} {
		    ::UI::MessageBox -title [mc "Error"] -icon error -type ok \
		      -message "Failed importing: $errMsg" -parent $w
		}
		incr x $prefs(offsetCopy)
		incr y $prefs(offsetCopy)
	    } else {
		::UI::MessageBox -title [mc "Error"] -icon error -type ok \
		  -message [mc "Cannot find importer for the MIME type %s" $mime] -parent $w
	    }
	}
    }
}

proc ::WB::DnDEnter {wcan action data type} {
    
    ::Debug 2 "::WB::DnDEnter action=$action, data=$data, type=$type"

    set act "none"
    foreach f $data {
	if {[file extension $f] eq ".can"} {
	    set haveImporter 1
	} else {
	    
	    # Require at least one file importable.
	    set haveImporter [::Plugins::HaveImporterForMime  \
	      [::Types::GetMimeTypeForFileName $f]]
	}
	if {$haveImporter} {
	    focus $wcan
	    set act $action
	    break
	}
    }
    return $act
}

proc ::WB::DnDLeave {wcan data type} {
    
    focus [winfo toplevel $wcan] 
}

#       Some stuff to handle sending messages using hooks.
#       The idea is to isolate us from jabber, p2p etc.
#       It shall only deal with remote clients, local drawing must be handled
#       separately.

# ::WB::SendMessageList --
# 
#       Invokes any registered send message hook. The 'cmdList' must
#       be without the "CANVAS:" prefix!

proc ::WB::SendMessageList {w cmdList args} {
    
    eval {::hooks::run whiteboardSendMessageHook $w $cmdList} $args
}

# ::WB::SendGenMessageList --
# 
#       Invokes any registered send message hook. 
#       The commands in the cmdList may include any prefix.
#       The prefix shall be included in commands of the cmdList.
#       @@@ THIS IS ACTUALLY A BAD SOLUTION AND SHALL BE REMOVED LATER!!!

proc ::WB::SendGenMessageList {w cmdList args} {
    
    eval {::hooks::run whiteboardSendGenMessageHook $w $cmdList} $args
}

# ::WB::PutFile --
# 
#       Invokes any registered hook for putting a file. This is only called
#       when we want to do p2p file transports (put/get).

proc ::WB::PutFile {w fileName opts args} {
    
    eval {::hooks::run whiteboardPutFileHook $w $fileName $opts} $args
}

# ::WB::RegisterHandler --
# 
#       Register handlers for additional command in the protocol.

proc ::WB::RegisterHandler {prefix cmd} {
    variable handler

    set handler($prefix) $cmd
    ::hooks::run whiteboardRegisterHandlerHook $prefix $cmd
}

# ::WB::GetRegisteredHandlers --
# 
#       Code that wants to get registered handlers must call this to get
#       the present handlers, and to add the 'whiteboardRegisterHandlerHook'
#       to get subsequent handlers.

proc ::WB::GetRegisteredHandlers {} {
    variable handler

    return [array get handler]
}

# ::WB::Free --
# 
#       Destroy handler only for cleanup.

proc ::WB::Free {w} {
        
    Debug 2 "::WB::Free w=$w"
	    
    # We could do some cleanup here.
    GarbageImages $w
    ::CanvasUtils::ItemFree $w
    ::UI::FreeMenu $w
    
    namespace delete ::WB::${w}
}

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