Source

wxPython / samples / pySketch / pySketch.py

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

    A simple object-oriented drawing program.

    This is completely free software; please feel free to adapt or use this in
    any way you like.

    Original Author: Erik Westra (ewestra@wave.co.nz)

    Other contributors: Bill Baxter (wbaxter@gmail.com)

    #########################################################################

    NOTE

    pySketch requires wxPython version 2.3.  If you are running an earlier
    version, you need to patch your copy of wxPython to fix a bug which will
    cause the "Edit Text Object" dialog box to crash.

    To patch an earlier version of wxPython, edit the wxPython/windows.py file,
    find the wxPyValidator.__init__ method and change the line which reads:

        self._setSelf(self, wxPyValidator, 0)

    to:

        self._setSelf(self, wxPyValidator, 1)

    This fixes a known bug in wxPython 2.2.5 (and possibly earlier) which has
    now been fixed in wxPython 2.3.

    #########################################################################

    TODO:

      * Add ARGV checking to see if a document was double-clicked on.

    Known Bugs:

      * Scrolling the window causes the drawing panel to be mucked up until you
        refresh it.  I've got no idea why.

      * I suspect that the reference counting for some wxPoint objects is
        getting mucked up; when the user quits, we get errors about being
        unable to call del on a 'None' object.

      * Saving files via pickling is not a robust cross-platform solution.
"""
import sys
import cPickle, os.path
import copy
import wx
from wx.lib.buttons import GenBitmapButton,GenBitmapToggleButton


import traceback, types

#----------------------------------------------------------------------------
#                            System Constants
#----------------------------------------------------------------------------

# Our menu item IDs:

menu_DUPLICATE     = wx.NewId() # Edit menu items.
menu_EDIT_PROPS    = wx.NewId()

menu_SELECT        = wx.NewId() # Tools menu items.
menu_LINE          = wx.NewId()
menu_POLYGON       = wx.NewId()
menu_RECT          = wx.NewId()
menu_ELLIPSE       = wx.NewId()
menu_TEXT          = wx.NewId()

menu_DC            = wx.NewId() # View menu items.
menu_GCDC          = wx.NewId()

menu_MOVE_FORWARD  = wx.NewId() # Object menu items.
menu_MOVE_TO_FRONT = wx.NewId()
menu_MOVE_BACKWARD = wx.NewId()
menu_MOVE_TO_BACK  = wx.NewId()

menu_ABOUT         = wx.NewId() # Help menu items.

# Our tool IDs:

id_SELECT   = wx.NewId()
id_LINE     = wx.NewId()
id_POLYGON  = wx.NewId()
id_SCRIBBLE = wx.NewId()
id_RECT     = wx.NewId()
id_ELLIPSE  = wx.NewId()
id_TEXT     = wx.NewId()

# Our tool option IDs:

id_FILL_OPT   = wx.NewId()
id_PEN_OPT    = wx.NewId()
id_LINE_OPT   = wx.NewId()

id_LINESIZE_0 = wx.NewId()
id_LINESIZE_1 = wx.NewId()
id_LINESIZE_2 = wx.NewId()
id_LINESIZE_3 = wx.NewId()
id_LINESIZE_4 = wx.NewId()
id_LINESIZE_5 = wx.NewId()

# Size of the drawing page, in pixels.

PAGE_WIDTH  = 1000
PAGE_HEIGHT = 1000

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

class DrawingFrame(wx.Frame):
    """ A frame showing the contents of a single document. """

    # ==========================================
    # == Initialisation and Window Management ==
    # ==========================================

    def __init__(self, parent, id, title, fileName=None):
        """ Standard constructor.

            'parent', 'id' and 'title' are all passed to the standard wx.Frame
            constructor.  'fileName' is the name and path of a saved file to
            load into this frame, if any.
        """
        wx.Frame.__init__(self, parent, id, title,
                         style = wx.DEFAULT_FRAME_STYLE | wx.WANTS_CHARS |
                                 wx.NO_FULL_REPAINT_ON_RESIZE)

        # Setup our menu bar.
        menuBar = wx.MenuBar()

        self.fileMenu = wx.Menu()
        self.fileMenu.Append(wx.ID_NEW,    "New\tCtrl-N", "Create a new document")
        self.fileMenu.Append(wx.ID_OPEN,   "Open...\tCtrl-O", "Open an existing document")
        self.fileMenu.Append(wx.ID_CLOSE,  "Close\tCtrl-W")
        self.fileMenu.AppendSeparator()
        self.fileMenu.Append(wx.ID_SAVE,   "Save\tCtrl-S")
        self.fileMenu.Append(wx.ID_SAVEAS, "Save As...")
        self.fileMenu.Append(wx.ID_REVERT, "Revert...")
        self.fileMenu.AppendSeparator()
        self.fileMenu.Append(wx.ID_EXIT,   "Quit\tCtrl-Q")

        menuBar.Append(self.fileMenu, "File")

        self.editMenu = wx.Menu()
        self.editMenu.Append(wx.ID_UNDO,      "Undo\tCtrl-Z")
        self.editMenu.Append(wx.ID_REDO,      "Redo\tCtrl-Y")
        self.editMenu.AppendSeparator()
        self.editMenu.Append(wx.ID_SELECTALL, "Select All\tCtrl-A")
        self.editMenu.AppendSeparator()
        self.editMenu.Append(menu_DUPLICATE,  "Duplicate\tCtrl-D")
        self.editMenu.Append(menu_EDIT_PROPS,"Edit...\tCtrl-E", "Edit object properties")
        self.editMenu.Append(wx.ID_CLEAR,     "Delete\tDel")

        menuBar.Append(self.editMenu, "Edit")

        self.viewMenu = wx.Menu()
        self.viewMenu.Append(menu_DC,  "Normal quality", 
                             "Normal rendering using wx.DC",
                             kind=wx.ITEM_RADIO)
        self.viewMenu.Append(menu_GCDC,"High quality", 
                             "Anti-aliased rendering using wx.GCDC", 
                             kind=wx.ITEM_RADIO)

        menuBar.Append(self.viewMenu, "View")

        self.toolsMenu = wx.Menu()
        self.toolsMenu.Append(id_SELECT,  "Selection", kind=wx.ITEM_RADIO)
        self.toolsMenu.Append(id_LINE,    "Line",      kind=wx.ITEM_RADIO)
        self.toolsMenu.Append(id_POLYGON, "Polygon",   kind=wx.ITEM_RADIO)
        self.toolsMenu.Append(id_SCRIBBLE,"Scribble",  kind=wx.ITEM_RADIO)
        self.toolsMenu.Append(id_RECT,    "Rectangle", kind=wx.ITEM_RADIO)
        self.toolsMenu.Append(id_ELLIPSE, "Ellipse",   kind=wx.ITEM_RADIO)
        self.toolsMenu.Append(id_TEXT,    "Text",      kind=wx.ITEM_RADIO)

        menuBar.Append(self.toolsMenu, "Tools")

        self.objectMenu = wx.Menu()
        self.objectMenu.Append(menu_MOVE_FORWARD,  "Move Forward")
        self.objectMenu.Append(menu_MOVE_TO_FRONT, "Move to Front\tCtrl-F")
        self.objectMenu.Append(menu_MOVE_BACKWARD, "Move Backward")
        self.objectMenu.Append(menu_MOVE_TO_BACK,  "Move to Back\tCtrl-B")

        menuBar.Append(self.objectMenu, "Object")

        self.helpMenu = wx.Menu()
        self.helpMenu.Append(menu_ABOUT, "About pySketch...")

        menuBar.Append(self.helpMenu, "Help")

        self.SetMenuBar(menuBar)

        # Create our statusbar

        self.CreateStatusBar()

        # Create our toolbar.

        tsize = (15,15)
        self.toolbar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT)

        artBmp = wx.ArtProvider.GetBitmap
        self.toolbar.AddSimpleTool(
            wx.ID_NEW, artBmp(wx.ART_NEW, wx.ART_TOOLBAR, tsize), "New")
        self.toolbar.AddSimpleTool(
            wx.ID_OPEN, artBmp(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, tsize), "Open")
        self.toolbar.AddSimpleTool(
            wx.ID_SAVE, artBmp(wx.ART_FILE_SAVE, wx.ART_TOOLBAR, tsize), "Save")
        self.toolbar.AddSimpleTool(
            wx.ID_SAVEAS, artBmp(wx.ART_FILE_SAVE_AS, wx.ART_TOOLBAR, tsize),
            "Save As...")
        #-------
        self.toolbar.AddSeparator()
        self.toolbar.AddSimpleTool(
            wx.ID_UNDO, artBmp(wx.ART_UNDO, wx.ART_TOOLBAR, tsize), "Undo")
        self.toolbar.AddSimpleTool(
            wx.ID_REDO, artBmp(wx.ART_REDO, wx.ART_TOOLBAR, tsize), "Redo")
        self.toolbar.AddSeparator()
        self.toolbar.AddSimpleTool(
            menu_DUPLICATE, wx.Bitmap("images/duplicate.bmp", wx.BITMAP_TYPE_BMP),
            "Duplicate")
        #-------
        self.toolbar.AddSeparator()
        self.toolbar.AddSimpleTool(
            menu_MOVE_FORWARD, wx.Bitmap("images/moveForward.bmp", wx.BITMAP_TYPE_BMP),
            "Move Forward")
        self.toolbar.AddSimpleTool(
            menu_MOVE_BACKWARD, wx.Bitmap("images/moveBack.bmp", wx.BITMAP_TYPE_BMP),
            "Move Backward")

        self.toolbar.Realize()

        # Associate menu/toolbar items with their handlers.
        menuHandlers = [
        (wx.ID_NEW,    self.doNew),
        (wx.ID_OPEN,   self.doOpen),
        (wx.ID_CLOSE,  self.doClose),
        (wx.ID_SAVE,   self.doSave),
        (wx.ID_SAVEAS, self.doSaveAs),
        (wx.ID_REVERT, self.doRevert),
        (wx.ID_EXIT,   self.doExit),

        (wx.ID_UNDO,         self.doUndo),
        (wx.ID_REDO,         self.doRedo),
        (wx.ID_SELECTALL,    self.doSelectAll),
        (menu_DUPLICATE,     self.doDuplicate),
        (menu_EDIT_PROPS,    self.doEditObject),
        (wx.ID_CLEAR,        self.doDelete),

        (id_SELECT,  self.onChooseTool, self.updChooseTool),
        (id_LINE,    self.onChooseTool, self.updChooseTool),
        (id_POLYGON, self.onChooseTool, self.updChooseTool),
        (id_SCRIBBLE,self.onChooseTool, self.updChooseTool),
        (id_RECT,    self.onChooseTool, self.updChooseTool),
        (id_ELLIPSE, self.onChooseTool, self.updChooseTool),
        (id_TEXT,    self.onChooseTool, self.updChooseTool),

        (menu_DC,      self.doChooseQuality),
        (menu_GCDC,    self.doChooseQuality),

        (menu_MOVE_FORWARD,  self.doMoveForward),
        (menu_MOVE_TO_FRONT, self.doMoveToFront),
        (menu_MOVE_BACKWARD, self.doMoveBackward),
        (menu_MOVE_TO_BACK,  self.doMoveToBack),

        (menu_ABOUT, self.doShowAbout)]
        for combo in menuHandlers:
            id, handler = combo[:2]
            self.Bind(wx.EVT_MENU, handler, id = id)
            if len(combo)>2:
                self.Bind(wx.EVT_UPDATE_UI, combo[2], id = id)
                
        # Install our own method to handle closing the window.  This allows us
        # to ask the user if he/she wants to save before closing the window, as
        # well as keeping track of which windows are currently open.

        self.Bind(wx.EVT_CLOSE, self.doClose)

        # Install our own method for handling keystrokes.  We use this to let
        # the user move the selected object(s) around using the arrow keys.

        self.Bind(wx.EVT_CHAR_HOOK, self.onKeyEvent)

        # Setup our top-most panel.  This holds the entire contents of the
        # window, excluding the menu bar.

        self.topPanel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)

        # Setup our tool palette, with all our drawing tools and option icons.

        self.toolPalette = wx.BoxSizer(wx.VERTICAL)

        self.selectIcon  = ToolPaletteToggle(self.topPanel, id_SELECT,
                                           "select", "Selection Tool", mode=wx.ITEM_RADIO)
        self.lineIcon    = ToolPaletteToggle(self.topPanel, id_LINE,
                                           "line", "Line Tool", mode=wx.ITEM_RADIO)
        self.polygonIcon = ToolPaletteToggle(self.topPanel, id_POLYGON,
                                             "polygon", "Polygon Tool", mode=wx.ITEM_RADIO)
        self.scribbleIcon = ToolPaletteToggle(self.topPanel, id_SCRIBBLE,
                                             "scribble", "Scribble Tool", mode=wx.ITEM_RADIO)
        self.rectIcon    = ToolPaletteToggle(self.topPanel, id_RECT,
                                           "rect", "Rectangle Tool", mode=wx.ITEM_RADIO)
        self.ellipseIcon = ToolPaletteToggle(self.topPanel, id_ELLIPSE,
                                           "ellipse", "Ellipse Tool", mode=wx.ITEM_RADIO)
        self.textIcon    = ToolPaletteToggle(self.topPanel, id_TEXT,
                                             "text", "Text Tool", mode=wx.ITEM_RADIO)

        # Create the tools
        self.tools = {
            'select'  : (self.selectIcon,   SelectDrawingTool()),
            'line'    : (self.lineIcon,     LineDrawingTool()),
            'polygon' : (self.polygonIcon,  PolygonDrawingTool()),
            'scribble': (self.scribbleIcon, ScribbleDrawingTool()),
            'rect'    : (self.rectIcon,     RectDrawingTool()),
            'ellipse' : (self.ellipseIcon,  EllipseDrawingTool()),
            'text'    : (self.textIcon,     TextDrawingTool())
        }


        toolSizer = wx.GridSizer(0, 2, 5, 5)
        toolSizer.Add(self.selectIcon)
        toolSizer.Add(self.lineIcon)
        toolSizer.Add(self.rectIcon)
        toolSizer.Add(self.ellipseIcon)
        toolSizer.Add(self.polygonIcon)
        toolSizer.Add(self.scribbleIcon)
        toolSizer.Add(self.textIcon)

        self.optionIndicator = ToolOptionIndicator(self.topPanel)
        self.optionIndicator.SetToolTip(
                wx.ToolTip("Shows Current Pen/Fill/Line Size Settings"))

        optionSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.penOptIcon  = ToolPaletteButton(self.topPanel, id_PEN_OPT,
                                             "penOpt", "Set Pen Colour",)
        self.fillOptIcon = ToolPaletteButton(self.topPanel, id_FILL_OPT,
                                             "fillOpt", "Set Fill Colour")
        self.lineOptIcon = ToolPaletteButton(self.topPanel, id_LINE_OPT,
                                             "lineOpt", "Set Line Size")

        margin = wx.LEFT | wx.RIGHT
        optionSizer.Add(self.penOptIcon,  0, margin, 1)
        optionSizer.Add(self.fillOptIcon, 0, margin, 1)
        optionSizer.Add(self.lineOptIcon, 0, margin, 1)

        margin = wx.TOP | wx.LEFT | wx.RIGHT | wx.ALIGN_CENTRE
        self.toolPalette.Add(toolSizer,            0, margin, 5)
        self.toolPalette.Add((0, 0),               0, margin, 5) # Spacer.
        self.toolPalette.Add(self.optionIndicator, 0, margin, 5)
        self.toolPalette.Add(optionSizer,          0, margin, 5)

        # Make the tool palette icons respond when the user clicks on them.

        for tool in self.tools.itervalues():
            tool[0].Bind(wx.EVT_BUTTON,    self.onChooseTool)

        self.selectIcon.Bind(wx.EVT_BUTTON, self.onChooseTool)
        self.lineIcon.Bind(wx.EVT_BUTTON, self.onChooseTool)


        self.penOptIcon.Bind(wx.EVT_BUTTON, self.onPenOptionIconClick)
        self.fillOptIcon.Bind(wx.EVT_BUTTON, self.onFillOptionIconClick)
        self.lineOptIcon.Bind(wx.EVT_BUTTON, self.onLineOptionIconClick)

        # Setup the main drawing area.

        self.drawPanel = wx.ScrolledWindow(self.topPanel, -1,
                                          style=wx.SUNKEN_BORDER|wx.NO_FULL_REPAINT_ON_RESIZE)
        self.drawPanel.SetBackgroundColour(wx.WHITE)

        self.drawPanel.EnableScrolling(True, True)
        self.drawPanel.SetScrollbars(20, 20, PAGE_WIDTH / 20, PAGE_HEIGHT / 20)

        self.drawPanel.Bind(wx.EVT_MOUSE_EVENTS, self.onMouseEvent)

        self.drawPanel.Bind(wx.EVT_IDLE, self.onIdle)
        self.drawPanel.Bind(wx.EVT_SIZE, self.onSize)
        self.drawPanel.Bind(wx.EVT_PAINT, self.onPaint)
        self.drawPanel.Bind(wx.EVT_ERASE_BACKGROUND, self.onEraseBackground)
        self.drawPanel.Bind(wx.EVT_SCROLLWIN, self.onPanelScroll)

        self.Bind(wx.EVT_TIMER, self.onIdle)


        # Position everything in the window.

        topSizer = wx.BoxSizer(wx.HORIZONTAL)
        topSizer.Add(self.toolPalette, 0)
        topSizer.Add(self.drawPanel, 1, wx.EXPAND)

        self.topPanel.SetAutoLayout(True)
        self.topPanel.SetSizer(topSizer)

        self.SetSizeHints(250, 200)
        self.SetSize(wx.Size(600, 400))

        # Select an initial tool.

        self.curToolName = None
        self.curToolIcon = None
        self.curTool = None
        self.setCurrentTool("select")

        # Set initial dc mode to fast
        self.wrapDC = lambda dc: dc

        # Setup our frame to hold the contents of a sketch document.

        self.dirty     = False
        self.fileName  = fileName
        self.contents  = []     # front-to-back ordered list of DrawingObjects.
        self.selection = []     # List of selected DrawingObjects.
        self.undoStack = []     # Stack of saved contents for undo.
        self.redoStack = []     # Stack of saved contents for redo.

        if self.fileName != None:
            self.loadContents()

        self._initBuffer()

        self._adjustMenus()

        # Finally, set our initial pen, fill and line options.

        self._setPenColour(wx.BLACK)
        self._setFillColour(wx.Colour(215,253,254))
        self._setLineSize(2)
        
        self.backgroundFillBrush = None # create on demand

        # Start the background redraw timer
        # This is optional, but it gives the double-buffered contents a 
        # chance to redraw even when idle events are disabled (like during 
        # resize and scrolling)
        self.redrawTimer = wx.Timer(self)
        self.redrawTimer.Start(700)


    # ============================
    # == Event Handling Methods ==
    # ============================


    def onPenOptionIconClick(self, event):
        """ Respond to the user clicking on the "Pen Options" icon.
        """
        data = wx.ColourData()
        if len(self.selection) == 1:
            data.SetColour(self.selection[0].getPenColour())
        else:
            data.SetColour(self.penColour)

        dialog = wx.ColourDialog(self, data)
        dialog.SetTitle('Choose line colour')
        if dialog.ShowModal() == wx.ID_OK:
            c = dialog.GetColourData().GetColour()
            self._setPenColour(wx.Colour(c.Red(), c.Green(), c.Blue()))
        dialog.Destroy()


    def onFillOptionIconClick(self, event):
        """ Respond to the user clicking on the "Fill Options" icon.
        """
        data = wx.ColourData()
        if len(self.selection) == 1:
            data.SetColour(self.selection[0].getFillColour())
        else:
            data.SetColour(self.fillColour)

        dialog = wx.ColourDialog(self, data)
        dialog.SetTitle('Choose fill colour')
        if dialog.ShowModal() == wx.ID_OK:
            c = dialog.GetColourData().GetColour()
            self._setFillColour(wx.Colour(c.Red(), c.Green(), c.Blue()))
        dialog.Destroy()

    def onLineOptionIconClick(self, event):
        """ Respond to the user clicking on the "Line Options" icon.
        """
        if len(self.selection) == 1:
            menu = self._buildLineSizePopup(self.selection[0].getLineSize())
        else:
            menu = self._buildLineSizePopup(self.lineSize)

        pos = self.lineOptIcon.GetPosition()
        pos.y = pos.y + self.lineOptIcon.GetSize().height
        self.PopupMenu(menu, pos)
        menu.Destroy()


    def onKeyEvent(self, event):
        """ Respond to a keypress event.

            We make the arrow keys move the selected object(s) by one pixel in
            the given direction.
        """
        step = 1
        if event.ShiftDown():
            step = 20

        if event.GetKeyCode() == wx.WXK_UP:
            self._moveObject(0, -step)
        elif event.GetKeyCode() == wx.WXK_DOWN:
            self._moveObject(0, step)
        elif event.GetKeyCode() == wx.WXK_LEFT:
            self._moveObject(-step, 0)
        elif event.GetKeyCode() == wx.WXK_RIGHT:
            self._moveObject(step, 0)
        else:
            event.Skip()


    def onMouseEvent(self, event):
        """ Respond to mouse events in the main drawing panel

            How we respond depends on the currently selected tool.
        """
        if self.curTool is None: return

        # Translate event into canvas coordinates and pass to current tool
        origx,origy = event.X, event.Y
        pt = self._getEventCoordinates(event)
        event.m_x = pt.x
        event.m_y = pt.y
        handled = self.curTool.onMouseEvent(self,event)
        event.m_x = origx
        event.m_y = origy

        if handled: return

        # otherwise handle it ourselves
        if event.RightDown():
            self.doPopupContextMenu(event)
        

    def doPopupContextMenu(self, event):
        """ Respond to the user right-clicking within our drawing panel.

            We select the clicked-on item, if necessary, and display a pop-up
            menu of available options which can be applied to the selected
            item(s).
        """
        mousePt = self._getEventCoordinates(event)
        obj = self.getObjectAt(mousePt)

        if obj == None: return # Nothing selected.

        # Select the clicked-on object.

        self.select(obj)

        # Build our pop-up menu.

        menu = wx.Menu()
        menu.Append(menu_DUPLICATE, "Duplicate")
        menu.Append(menu_EDIT_PROPS,"Edit...")
        menu.Append(wx.ID_CLEAR,    "Delete")
        menu.AppendSeparator()
        menu.Append(menu_MOVE_FORWARD,   "Move Forward")
        menu.Append(menu_MOVE_TO_FRONT,  "Move to Front")
        menu.Append(menu_MOVE_BACKWARD,  "Move Backward")
        menu.Append(menu_MOVE_TO_BACK,   "Move to Back")

        menu.Enable(menu_EDIT_PROPS,    obj.hasPropertyEditor())
        menu.Enable(menu_MOVE_FORWARD,  obj != self.contents[0])
        menu.Enable(menu_MOVE_TO_FRONT, obj != self.contents[0])
        menu.Enable(menu_MOVE_BACKWARD, obj != self.contents[-1])
        menu.Enable(menu_MOVE_TO_BACK,  obj != self.contents[-1])

        self.Bind(wx.EVT_MENU, self.doDuplicate,   id=menu_DUPLICATE)
        self.Bind(wx.EVT_MENU, self.doEditObject,  id=menu_EDIT_PROPS)
        self.Bind(wx.EVT_MENU, self.doDelete,      id=wx.ID_CLEAR)
        self.Bind(wx.EVT_MENU, self.doMoveForward, id=menu_MOVE_FORWARD)
        self.Bind(wx.EVT_MENU, self.doMoveToFront, id=menu_MOVE_TO_FRONT)
        self.Bind(wx.EVT_MENU, self.doMoveBackward,id=menu_MOVE_BACKWARD)
        self.Bind(wx.EVT_MENU, self.doMoveToBack,  id=menu_MOVE_TO_BACK)  
                            
        # Show the pop-up menu.

        clickPt = wx.Point(mousePt.x + self.drawPanel.GetPosition().x,
                          mousePt.y + self.drawPanel.GetPosition().y)
        self.drawPanel.PopupMenu(menu, mousePt)
        menu.Destroy()


    def onSize(self, event):
        """
        Called when the window is resized.  We set a flag so the idle
        handler will resize the buffer.
        """
        self.requestRedraw()


    def onIdle(self, event):
        """
        If the size was changed then resize the bitmap used for double
        buffering to match the window size.  We do it in Idle time so
        there is only one refresh after resizing is done, not lots while
        it is happening.
        """
        if self._reInitBuffer and self.IsShown():
            self._initBuffer()
            self.drawPanel.Refresh(False)

    def requestRedraw(self):
        """Requests a redraw of the drawing panel contents.

        The actual redrawing doesn't happen until the next idle time.
        """
        self._reInitBuffer = True

    def onPaint(self, event):
        """
        Called when the window is exposed.
        """
        # Create a buffered paint DC.  It will create the real
        # wx.PaintDC and then blit the bitmap to it when dc is
        # deleted.
        dc = wx.BufferedPaintDC(self.drawPanel, self.buffer)


        # On Windows, if that's all we do things look a little rough
        # So in order to make scrolling more polished-looking
        # we iterate over the exposed regions and fill in unknown
        # areas with a fall-back pattern.

        if wx.Platform != '__WXMSW__':
            return

        # First get the update rects and subtract off the part that 
        # self.buffer has correct already
        region = self.drawPanel.GetUpdateRegion()
        panelRect = self.drawPanel.GetClientRect()
        offset = list(self.drawPanel.CalcUnscrolledPosition(0,0))
        offset[0] -= self.saved_offset[0]
        offset[1] -= self.saved_offset[1]
        region.Subtract(-offset[0],- offset[1],panelRect.Width, panelRect.Height)

        # Now iterate over the remaining region rects and fill in with a pattern
        rgn_iter = wx.RegionIterator(region)
        if rgn_iter.HaveRects():
            self.setBackgroundMissingFillStyle(dc)
            offset = self.drawPanel.CalcUnscrolledPosition(0,0)
        while rgn_iter:
            r = rgn_iter.GetRect()
            if r.Size != self.drawPanel.ClientSize:
                dc.DrawRectangleRect(r)
            rgn_iter.Next()
            

    def setBackgroundMissingFillStyle(self, dc):
        if self.backgroundFillBrush is None:
            # Win95 can only handle a 8x8 stipple bitmaps max
            #stippleBitmap = wx.BitmapFromBits("\xf0"*4 + "\x0f"*4,8,8)
            # ...but who uses Win95?
            stippleBitmap = wx.BitmapFromBits("\x06",2,2)
            stippleBitmap.SetMask(wx.Mask(stippleBitmap))
            bgbrush = wx.Brush(wx.WHITE, wx.STIPPLE_MASK_OPAQUE)
            bgbrush.SetStipple(stippleBitmap)
            self.backgroundFillBrush = bgbrush

        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBrush(self.backgroundFillBrush)
        dc.SetTextForeground(wx.LIGHT_GREY)
        dc.SetTextBackground(wx.WHITE)
            

    def onEraseBackground(self, event):
        """
        Overridden to do nothing to prevent flicker
        """
        pass


    def onPanelScroll(self, event):
        """
        Called when the user changes scrolls the drawPanel
        """
        # make a note to ourselves to redraw when we get a chance
        self.requestRedraw()
        event.Skip()
        pass

    def drawContents(self, dc):
        """
        Does the actual drawing of all drawing contents with the specified dc
        """
        # PrepareDC sets the device origin according to current scrolling
        self.drawPanel.PrepareDC(dc)

        gdc = self.wrapDC(dc)

        # First pass draws objects
        ordered_selection = []
        for obj in self.contents[::-1]:
            if obj in self.selection:
                obj.draw(gdc, True)
                ordered_selection.append(obj)
            else:
                obj.draw(gdc, False)

        # First pass draws objects
        if self.curTool is not None:
            self.curTool.draw(gdc)

        # Second pass draws selection handles so they're always on top
        for obj in ordered_selection:
            obj.drawHandles(gdc)



    # ==========================
    # == Menu Command Methods ==
    # ==========================

    def doNew(self, event):
        """ Respond to the "New" menu command.
        """
        global _docList
        newFrame = DrawingFrame(None, -1, "Untitled")
        newFrame.Show(True)
        _docList.append(newFrame)


    def doOpen(self, event):
        """ Respond to the "Open" menu command.
        """
        global _docList

        curDir = os.getcwd()
        fileName = wx.FileSelector("Open File", default_extension="psk",
                                  flags = wx.OPEN | wx.FILE_MUST_EXIST)
        if fileName == "": return
        fileName = os.path.join(os.getcwd(), fileName)
        os.chdir(curDir)

        title = os.path.basename(fileName)

        if (self.fileName == None) and (len(self.contents) == 0):
            # Load contents into current (empty) document.
            self.fileName = fileName
            self.SetTitle(os.path.basename(fileName))
            self.loadContents()
        else:
            # Open a new frame for this document.
            newFrame = DrawingFrame(None, -1, os.path.basename(fileName),
                                    fileName=fileName)
            newFrame.Show(True)
            _docList.append(newFrame)


    def doClose(self, event):
        """ Respond to the "Close" menu command.
        """
        global _docList

        if self.dirty:
            if not self.askIfUserWantsToSave("closing"): return

        _docList.remove(self)
        self.Destroy()


    def doSave(self, event):
        """ Respond to the "Save" menu command.
        """
        if self.fileName != None:
            self.saveContents()


    def doSaveAs(self, event):
        """ Respond to the "Save As" menu command.
        """
        if self.fileName == None:
            default = ""
        else:
            default = self.fileName

        curDir = os.getcwd()
        fileName = wx.FileSelector("Save File As", "Saving",
                                  default_filename=default,
                                  default_extension="psk",
                                  wildcard="*.psk",
                                  flags = wx.SAVE | wx.OVERWRITE_PROMPT)
        if fileName == "": return # User cancelled.
        fileName = os.path.join(os.getcwd(), fileName)
        os.chdir(curDir)

        title = os.path.basename(fileName)
        self.SetTitle(title)

        self.fileName = fileName
        self.saveContents()


    def doRevert(self, event):
        """ Respond to the "Revert" menu command.
        """
        if not self.dirty: return

        if wx.MessageBox("Discard changes made to this document?", "Confirm",
                        style = wx.OK | wx.CANCEL | wx.ICON_QUESTION,
                        parent=self) == wx.CANCEL: return
        self.loadContents()


    def doExit(self, event):
        """ Respond to the "Quit" menu command.
        """
        global _docList, _app
        for doc in _docList:
            if not doc.dirty: continue
            doc.Raise()
            if not doc.askIfUserWantsToSave("quitting"): return
            _docList.remove(doc)
            doc.Destroy()

        _app.ExitMainLoop()


    def doUndo(self, event):
        """ Respond to the "Undo" menu command.
        """
        if not self.undoStack: return 

        state = self._buildStoredState()
        self.redoStack.append(state)
        state = self.undoStack.pop()
        self._restoreStoredState(state)

    def doRedo(self, event):
        """ Respond to the "Redo" menu.
        """
        if not self.redoStack: return

        state = self._buildStoredState()
        self.undoStack.append(state)
        state = self.redoStack.pop()
        self._restoreStoredState(state)

    def doSelectAll(self, event):
        """ Respond to the "Select All" menu command.
        """
        self.selectAll()


    def doDuplicate(self, event):
        """ Respond to the "Duplicate" menu command.
        """
        self.saveUndoInfo()

        objs = []
        for obj in self.contents:
            if obj in self.selection:
                newObj = copy.deepcopy(obj)
                pos = obj.getPosition()
                newObj.setPosition(wx.Point(pos.x + 10, pos.y + 10))
                objs.append(newObj)

        self.contents = objs + self.contents

        self.selectMany(objs)


    def doEditObject(self, event):
        """ Respond to the "Edit..." menu command.
        """
        if len(self.selection) != 1: return

        obj = self.selection[0]
        if not obj.hasPropertyEditor(): 
            assert False, "doEditObject called on non-editable"

        ret = obj.doPropertyEdit(self)
        if ret:
            self.dirty = True
            self.requestRedraw()
            self._adjustMenus()


    def doDelete(self, event):
        """ Respond to the "Delete" menu command.
        """
        self.saveUndoInfo()

        for obj in self.selection:
            self.contents.remove(obj)
            del obj
        self.deselectAll()


    def onChooseTool(self, event):
        """ Respond to tool selection menu and tool palette selections
        """
        obj = event.GetEventObject()
        id2name = { id_SELECT: "select",
                    id_LINE: "line",
                    id_POLYGON: "polygon",
                    id_SCRIBBLE: "scribble",
                    id_RECT: "rect",
                    id_ELLIPSE: "ellipse",
                    id_TEXT: "text" }
        toolID = event.GetId()
        name = id2name.get( toolID )
        
        if name:
            self.setCurrentTool(name)

    def updChooseTool(self, event):
        """UI update event that keeps tool menu in sync with the PaletteIcons"""
        obj = event.GetEventObject()
        id2name = { id_SELECT: "select",
                    id_LINE: "line",
                    id_POLYGON: "polygon",
                    id_SCRIBBLE: "scribble",
                    id_RECT: "rect",
                    id_ELLIPSE: "ellipse",
                    id_TEXT: "text" }
        toolID = event.GetId()
        event.Check( toolID == self.curToolIcon.GetId() )


    def doChooseQuality(self, event):
        """Respond to the render quality menu commands
        """
        if event.GetId() == menu_DC:
            self.wrapDC = lambda dc: dc
        else:
            self.wrapDC = lambda dc: wx.GCDC(dc)
        self._adjustMenus()
        self.requestRedraw()

    def doMoveForward(self, event):
        """ Respond to the "Move Forward" menu command.
        """
        if len(self.selection) != 1: return

        self.saveUndoInfo()

        obj = self.selection[0]
        index = self.contents.index(obj)
        if index == 0: return

        del self.contents[index]
        self.contents.insert(index-1, obj)

        self.requestRedraw()
        self._adjustMenus()


    def doMoveToFront(self, event):
        """ Respond to the "Move to Front" menu command.
        """
        if len(self.selection) != 1: return

        self.saveUndoInfo()

        obj = self.selection[0]
        self.contents.remove(obj)
        self.contents.insert(0, obj)

        self.requestRedraw()
        self._adjustMenus()


    def doMoveBackward(self, event):
        """ Respond to the "Move Backward" menu command.
        """
        if len(self.selection) != 1: return

        self.saveUndoInfo()

        obj = self.selection[0]
        index = self.contents.index(obj)
        if index == len(self.contents) - 1: return

        del self.contents[index]
        self.contents.insert(index+1, obj)

        self.requestRedraw()
        self._adjustMenus()


    def doMoveToBack(self, event):
        """ Respond to the "Move to Back" menu command.
        """
        if len(self.selection) != 1: return

        self.saveUndoInfo()

        obj = self.selection[0]
        self.contents.remove(obj)
        self.contents.append(obj)

        self.requestRedraw()
        self._adjustMenus()


    def doShowAbout(self, event):
        """ Respond to the "About pySketch" menu command.
        """
        dialog = wx.Dialog(self, -1, "About pySketch") # ,
                          #style=wx.DIALOG_MODAL | wx.STAY_ON_TOP)
        dialog.SetBackgroundColour(wx.WHITE)

        panel = wx.Panel(dialog, -1)
        panel.SetBackgroundColour(wx.WHITE)

        panelSizer = wx.BoxSizer(wx.VERTICAL)

        boldFont = wx.Font(panel.GetFont().GetPointSize(),
                          panel.GetFont().GetFamily(),
                          wx.NORMAL, wx.BOLD)

        logo = wx.StaticBitmap(panel, -1, wx.Bitmap("images/logo.bmp",
                                                  wx.BITMAP_TYPE_BMP))

        lab1 = wx.StaticText(panel, -1, "pySketch")
        lab1.SetFont(wx.Font(36, boldFont.GetFamily(), wx.ITALIC, wx.BOLD))
        lab1.SetSize(lab1.GetBestSize())

        imageSizer = wx.BoxSizer(wx.HORIZONTAL)
        imageSizer.Add(logo, 0, wx.ALL | wx.ALIGN_CENTRE_VERTICAL, 5)
        imageSizer.Add(lab1, 0, wx.ALL | wx.ALIGN_CENTRE_VERTICAL, 5)

        lab2 = wx.StaticText(panel, -1, "A simple object-oriented drawing " + \
                                       "program.")
        lab2.SetFont(boldFont)
        lab2.SetSize(lab2.GetBestSize())

        lab3 = wx.StaticText(panel, -1, "pySketch is completely free " + \
                                       "software; please")
        lab3.SetFont(boldFont)
        lab3.SetSize(lab3.GetBestSize())

        lab4 = wx.StaticText(panel, -1, "feel free to adapt or use this " + \
                                       "in any way you like.")
        lab4.SetFont(boldFont)
        lab4.SetSize(lab4.GetBestSize())

        lab5 = wx.StaticText(panel, -1,
                             "Author: Erik Westra " + \
                             "(ewestra@wave.co.nz)\n" + \
                             "Contributors: Bill Baxter " +\
                             "(wbaxter@gmail.com)  ")

        lab5.SetFont(boldFont)
        lab5.SetSize(lab5.GetBestSize())

        btnOK = wx.Button(panel, wx.ID_OK, "OK")

        panelSizer.Add(imageSizer, 0, wx.ALIGN_CENTRE)
        panelSizer.Add((10, 10)) # Spacer.
        panelSizer.Add(lab2, 0, wx.ALIGN_CENTRE)
        panelSizer.Add((10, 10)) # Spacer.
        panelSizer.Add(lab3, 0, wx.ALIGN_CENTRE)
        panelSizer.Add(lab4, 0, wx.ALIGN_CENTRE)
        panelSizer.Add((10, 10)) # Spacer.
        panelSizer.Add(lab5, 0, wx.ALIGN_CENTRE)
        panelSizer.Add((10, 10)) # Spacer.
        panelSizer.Add(btnOK, 0, wx.ALL | wx.ALIGN_CENTRE, 5)

        panel.SetAutoLayout(True)
        panel.SetSizer(panelSizer)
        panelSizer.Fit(panel)

        topSizer = wx.BoxSizer(wx.HORIZONTAL)
        topSizer.Add(panel, 0, wx.ALL, 10)

        dialog.SetAutoLayout(True)
        dialog.SetSizer(topSizer)
        topSizer.Fit(dialog)

        dialog.Centre()

        btn = dialog.ShowModal()
        dialog.Destroy()

    def getTextEditor(self):
        if not hasattr(self,'textEditor') or not self.textEditor:
            self.textEditor = EditTextObjectDialog(self, "Edit Text Object")
        return self.textEditor

    # =============================
    # == Object Creation Methods ==
    # =============================

    def addObject(self, obj, select=True):
        """Add a new drawing object to the canvas.
        
        If select is True then also select the object
        """
        self.saveUndoInfo()
        self.contents.insert(0, obj)
        self.dirty = True
        if select:
            self.select(obj)
        #self.setCurrentTool('select')

    def saveUndoInfo(self):
        """ Remember the current state of the document, to allow for undo.

            We make a copy of the document's contents, so that we can return to
            the previous contents if the user does something and then wants to
            undo the operation.
            
            This should be called only for a new modification to the document
            since it erases the redo history.
        """
        state = self._buildStoredState()

        self.undoStack.append(state)
        self.redoStack = []
        self.dirty = True
        self._adjustMenus()

    # =======================
    # == Selection Methods ==
    # =======================

    def setCurrentTool(self, toolName):
        """ Set the currently selected tool.
        """
        
        toolIcon, tool = self.tools[toolName]
        if self.curToolIcon is not None:
            self.curToolIcon.SetValue(False)

        toolIcon.SetValue(True)
        self.curToolName = toolName
        self.curToolIcon = toolIcon
        self.curTool = tool
        self.drawPanel.SetCursor(tool.getDefaultCursor())


    def selectAll(self):
        """ Select every DrawingObject in our document.
        """
        self.selection = []
        for obj in self.contents:
            self.selection.append(obj)
        self.requestRedraw()
        self._adjustMenus()


    def deselectAll(self):
        """ Deselect every DrawingObject in our document.
        """
        self.selection = []
        self.requestRedraw()
        self._adjustMenus()


    def select(self, obj, add=False):
        """ Select the given DrawingObject within our document.

        If 'add' is True obj is added onto the current selection
        """
        if not add:
            self.selection = []
        if obj not in self.selection:
            self.selection += [obj]
            self.requestRedraw()
            self._adjustMenus()

    def selectMany(self, objs):
        """ Select the given list of DrawingObjects.
        """
        self.selection = objs
        self.requestRedraw()
        self._adjustMenus()


    def selectByRectangle(self, x, y, width, height):
        """ Select every DrawingObject in the given rectangular region.
        """
        self.selection = []
        for obj in self.contents:
            if obj.objectWithinRect(x, y, width, height):
                self.selection.append(obj)
        self.requestRedraw()
        self._adjustMenus()

    def getObjectAndSelectionHandleAt(self, pt):
        """ Return the object and selection handle at the given point.

            We draw selection handles (small rectangles) around the currently
            selected object(s).  If the given point is within one of the
            selection handle rectangles, we return the associated object and a
            code indicating which selection handle the point is in.  If the
            point isn't within any selection handle at all, we return the tuple
            (None, None).
        """
        for obj in self.selection:
            handle = obj.getSelectionHandleContainingPoint(pt.x, pt.y)
            if handle is not None:
                return obj, handle

        return None, None


    def getObjectAt(self, pt):
        """ Return the first object found which is at the given point.
        """
        for obj in self.contents:
            if obj.objectContainsPoint(pt.x, pt.y):
                return obj
        return None


    # ======================
    # == File I/O Methods ==
    # ======================

    def loadContents(self):
        """ Load the contents of our document into memory.
        """

        try:
            f = open(self.fileName, "rb")
            objData = cPickle.load(f)
            f.close()

            for klass, data in objData:
                obj = klass()
                obj.setData(data)
                self.contents.append(obj)

            self.dirty = False
            self.selection = []
            self.undoStack  = []
            self.redoStack  = []

            self.requestRedraw()
            self._adjustMenus()
        except:
            response = wx.MessageBox("Unable to load " + self.fileName + ".",
                                     "Error", wx.OK|wx.ICON_ERROR, self)



    def saveContents(self):
        """ Save the contents of our document to disk.
        """
        # SWIG-wrapped native wx contents cannot be pickled, so 
        # we have to convert our data to something pickle-friendly.
        
        try:
            objData = []
            for obj in self.contents:
                objData.append([obj.__class__, obj.getData()])

            f = open(self.fileName, "wb")
            cPickle.dump(objData, f)
            f.close()

            self.dirty = False
            self._adjustMenus()
        except:
            response = wx.MessageBox("Unable to load " + self.fileName + ".",
                                     "Error", wx.OK|wx.ICON_ERROR, self)


    def askIfUserWantsToSave(self, action):
        """ Give the user the opportunity to save the current document.

            'action' is a string describing the action about to be taken.  If
            the user wants to save the document, it is saved immediately.  If
            the user cancels, we return False.
        """
        if not self.dirty: return True # Nothing to do.

        response = wx.MessageBox("Save changes before " + action + "?",
                                "Confirm", wx.YES_NO | wx.CANCEL, self)

        if response == wx.YES:
            if self.fileName == None:
                fileName = wx.FileSelector("Save File As", "Saving",
                                          default_extension="psk",
                                          wildcard="*.psk",
                                          flags = wx.SAVE | wx.OVERWRITE_PROMPT)
                if fileName == "": return False # User cancelled.
                self.fileName = fileName

            self.saveContents()
            return True
        elif response == wx.NO:
            return True # User doesn't want changes saved.
        elif response == wx.CANCEL:
            return False # User cancelled.

    # =====================
    # == Private Methods ==
    # =====================

    def _initBuffer(self):
        """Initialize the bitmap used for buffering the display."""
        size = self.drawPanel.GetSize()
        self.buffer = wx.EmptyBitmap(max(1,size.width),max(1,size.height))
        dc = wx.BufferedDC(None, self.buffer)
        dc.SetBackground(wx.Brush(self.drawPanel.GetBackgroundColour()))
        dc.Clear()
        self.drawContents(dc)
        del dc  # commits all drawing to the buffer

        self.saved_offset = self.drawPanel.CalcUnscrolledPosition(0,0)

        self._reInitBuffer = False



    def _adjustMenus(self):
        """ Adjust our menus and toolbar to reflect the current state of the
            world.

            Doing this manually rather than using an EVT_UPDATE_UI is a bit
            more efficient (since it's only done when it's really needed), 
            but it means we have to remember to call _adjustMenus any time
            menus may need adjusting.
        """
        canSave   = (self.fileName != None) and self.dirty
        canRevert = (self.fileName != None) and self.dirty
        canUndo   = self.undoStack!=[]
        canRedo   = self.redoStack!=[]
        selection = len(self.selection) > 0
        onlyOne   = len(self.selection) == 1
        hasEditor = onlyOne and self.selection[0].hasPropertyEditor()
        front     = onlyOne and (self.selection[0] == self.contents[0])
        back      = onlyOne and (self.selection[0] == self.contents[-1])

        # Enable/disable our menu items.

        self.fileMenu.Enable(wx.ID_SAVE,   canSave)
        self.fileMenu.Enable(wx.ID_REVERT, canRevert)

        self.editMenu.Enable(wx.ID_UNDO,      canUndo)
        self.editMenu.Enable(wx.ID_REDO,      canRedo)
        self.editMenu.Enable(menu_DUPLICATE, selection)
        self.editMenu.Enable(menu_EDIT_PROPS,hasEditor)
        self.editMenu.Enable(wx.ID_CLEAR,    selection)

        self.objectMenu.Enable(menu_MOVE_FORWARD,  onlyOne and not front)
        self.objectMenu.Enable(menu_MOVE_TO_FRONT, onlyOne and not front)
        self.objectMenu.Enable(menu_MOVE_BACKWARD, onlyOne and not back)
        self.objectMenu.Enable(menu_MOVE_TO_BACK,  onlyOne and not back)

        # Enable/disable our toolbar icons.

        self.toolbar.EnableTool(wx.ID_NEW,          True)
        self.toolbar.EnableTool(wx.ID_OPEN,         True)
        self.toolbar.EnableTool(wx.ID_SAVE,         canSave)
        self.toolbar.EnableTool(wx.ID_UNDO,         canUndo)
        self.toolbar.EnableTool(wx.ID_REDO,         canRedo)
        self.toolbar.EnableTool(menu_DUPLICATE,     selection)
        self.toolbar.EnableTool(menu_MOVE_FORWARD,  onlyOne and not front)
        self.toolbar.EnableTool(menu_MOVE_BACKWARD, onlyOne and not back)


    def _setPenColour(self, colour):
        """ Set the default or selected object's pen colour.
        """
        if len(self.selection) > 0:
            self.saveUndoInfo()
            for obj in self.selection:
                obj.setPenColour(colour)
            self.requestRedraw()

        self.penColour = colour
        self.optionIndicator.setPenColour(colour)


    def _setFillColour(self, colour):
        """ Set the default or selected object's fill colour.
        """
        if len(self.selection) > 0:
            self.saveUndoInfo()
            for obj in self.selection:
                obj.setFillColour(colour)
            self.requestRedraw()

        self.fillColour = colour
        self.optionIndicator.setFillColour(colour)


    def _setLineSize(self, size):
        """ Set the default or selected object's line size.
        """
        if len(self.selection) > 0:
            self.saveUndoInfo()
            for obj in self.selection:
                obj.setLineSize(size)
            self.requestRedraw()

        self.lineSize = size
        self.optionIndicator.setLineSize(size)


    def _buildStoredState(self):
        """ Remember the current state of the document, to allow for undo.

            We make a copy of the document's contents, so that we can return to
            the previous contents if the user does something and then wants to
            undo the operation.  

            Returns an object representing the current document state.
        """
        savedContents = []
        for obj in self.contents:
            savedContents.append([obj.__class__, obj.getData()])

        savedSelection = []
        for i in range(len(self.contents)):
            if self.contents[i] in self.selection:
                savedSelection.append(i)

        info = {"contents"  : savedContents,
                "selection" : savedSelection}

        return info
        
    def _restoreStoredState(self, savedState):
        """Restore the state of the document to a previous point for undo/redo.

        Takes a stored state object and recreates the document from it.
        Used by undo/redo implementation.
        """
        self.contents = []

        for draw_class, data in savedState["contents"]:
            obj = draw_class()
            obj.setData(data)
            self.contents.append(obj)

        self.selection = []
        for i in savedState["selection"]:
            self.selection.append(self.contents[i])

        self.dirty = True
        self._adjustMenus()
        self.requestRedraw()

    def _resizeObject(self, obj, anchorPt, oldPt, newPt):
        """ Resize the given object.

            'anchorPt' is the unchanging corner of the object, while the
            opposite corner has been resized.  'oldPt' are the current
            coordinates for this corner, while 'newPt' are the new coordinates.
            The object should fit within the given dimensions, though if the
            new point is less than the anchor point the object will need to be
            moved as well as resized, to avoid giving it a negative size.
        """
        if isinstance(obj, TextDrawingObject):
            # Not allowed to resize text objects -- they're sized to fit text.
            wx.Bell()
            return

        self.saveUndoInfo()

        topLeft  = wx.Point(min(anchorPt.x, newPt.x),
                           min(anchorPt.y, newPt.y))
        botRight = wx.Point(max(anchorPt.x, newPt.x),
                           max(anchorPt.y, newPt.y))

        newWidth  = botRight.x - topLeft.x
        newHeight = botRight.y - topLeft.y

        if isinstance(obj, LineDrawingObject):
            # Adjust the line so that its start and end points match the new
            # overall object size.

            startPt = obj.getStartPt()
            endPt   = obj.getEndPt()

            slopesDown = ((startPt.x < endPt.x) and (startPt.y < endPt.y)) or \
                         ((startPt.x > endPt.x) and (startPt.y > endPt.y))

            # Handle the user flipping the line.

            hFlip = ((anchorPt.x < oldPt.x) and (anchorPt.x > newPt.x)) or \
                    ((anchorPt.x > oldPt.x) and (anchorPt.x < newPt.x))
            vFlip = ((anchorPt.y < oldPt.y) and (anchorPt.y > newPt.y)) or \
                    ((anchorPt.y > oldPt.y) and (anchorPt.y < newPt.y))

            if (hFlip and not vFlip) or (vFlip and not hFlip):
                slopesDown = not slopesDown # Line flipped.

            if slopesDown:
                obj.setStartPt(wx.Point(0, 0))
                obj.setEndPt(wx.Point(newWidth, newHeight))
            else:
                obj.setStartPt(wx.Point(0, newHeight))
                obj.setEndPt(wx.Point(newWidth, 0))

        # Finally, adjust the bounds of the object to match the new dimensions.

        obj.setPosition(topLeft)
        obj.setSize(wx.Size(botRight.x - topLeft.x, botRight.y - topLeft.y))

        self.requestRedraw()


    def _moveObject(self, offsetX, offsetY):
        """ Move the currently selected object(s) by the given offset.
        """
        self.saveUndoInfo()

        for obj in self.selection:
            pos = obj.getPosition()
            pos.x = pos.x + offsetX
            pos.y = pos.y + offsetY
            obj.setPosition(pos)

        self.requestRedraw()


    def _buildLineSizePopup(self, lineSize):
        """ Build the pop-up menu used to set the line size.

            'lineSize' is the current line size value.  The corresponding item
            is checked in the pop-up menu.
        """
        menu = wx.Menu()
        menu.Append(id_LINESIZE_0, "no line",      kind=wx.ITEM_CHECK)
        menu.Append(id_LINESIZE_1, "1-pixel line", kind=wx.ITEM_CHECK)
        menu.Append(id_LINESIZE_2, "2-pixel line", kind=wx.ITEM_CHECK)
        menu.Append(id_LINESIZE_3, "3-pixel line", kind=wx.ITEM_CHECK)
        menu.Append(id_LINESIZE_4, "4-pixel line", kind=wx.ITEM_CHECK)
        menu.Append(id_LINESIZE_5, "5-pixel line", kind=wx.ITEM_CHECK)

        if   lineSize == 0: menu.Check(id_LINESIZE_0, True)
        elif lineSize == 1: menu.Check(id_LINESIZE_1, True)
        elif lineSize == 2: menu.Check(id_LINESIZE_2, True)
        elif lineSize == 3: menu.Check(id_LINESIZE_3, True)
        elif lineSize == 4: menu.Check(id_LINESIZE_4, True)
        elif lineSize == 5: menu.Check(id_LINESIZE_5, True)

        self.Bind(wx.EVT_MENU, self._lineSizePopupSelected, id=id_LINESIZE_0, id2=id_LINESIZE_5)

        return menu


    def _lineSizePopupSelected(self, event):
        """ Respond to the user selecting an item from the line size popup menu
        """
        id = event.GetId()
        if   id == id_LINESIZE_0: self._setLineSize(0)
        elif id == id_LINESIZE_1: self._setLineSize(1)
        elif id == id_LINESIZE_2: self._setLineSize(2)
        elif id == id_LINESIZE_3: self._setLineSize(3)
        elif id == id_LINESIZE_4: self._setLineSize(4)
        elif id == id_LINESIZE_5: self._setLineSize(5)
        else:
            wx.Bell()
            return

        self.optionIndicator.setLineSize(self.lineSize)


    def _getEventCoordinates(self, event):
        """ Return the coordinates associated with the given mouse event.

            The coordinates have to be adjusted to allow for the current scroll
            position.
        """
        originX, originY = self.drawPanel.GetViewStart()
        unitX, unitY = self.drawPanel.GetScrollPixelsPerUnit()
        return wx.Point(event.GetX() + (originX * unitX),
                       event.GetY() + (originY * unitY))


    def _drawObjectOutline(self, offsetX, offsetY):
        """ Draw an outline of the currently selected object.

            The selected object's outline is drawn at the object's position
            plus the given offset.

            Note that the outline is drawn by *inverting* the window's
            contents, so calling _drawObjectOutline twice in succession will
            restore the window's contents back to what they were previously.
        """
        if len(self.selection) != 1: return

        position = self.selection[0].getPosition()
        size     = self.selection[0].getSize()

        dc = wx.ClientDC(self.drawPanel)
        self.drawPanel.PrepareDC(dc)
        dc.BeginDrawing()
        dc.SetPen(wx.BLACK_DASHED_PEN)
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        dc.SetLogicalFunction(wx.INVERT)

        dc.DrawRectangle(position.x + offsetX, position.y + offsetY,
                         size.width, size.height)

        dc.EndDrawing()


#============================================================================
class DrawingTool(object):
    """Base class for drawing tools"""

    def __init__(self):
        pass

    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.STANDARD_CURSOR

    def draw(self,dc):
        pass


    def onMouseEvent(self,parent, event):
        """Mouse events passed in from the parent.

        Returns True if the event is handled by the tool,
        False if the canvas can try to use it.
        """
        event.Skip()
        return False

#----------------------------------------------------------------------------
class SelectDrawingTool(DrawingTool):
    """Represents the tool for selecting things"""

    def __init__(self):
        self.curHandle = None
        self.curObject = None
        self.objModified = False
        self.startPt = None
        self.curPt = None

    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.STANDARD_CURSOR

    def draw(self, dc):
        if self._doingRectSelection():
            dc.SetPen(wx.BLACK_DASHED_PEN)
            dc.SetBrush(wx.TRANSPARENT_BRUSH)
            x = [self.startPt.x, self.curPt.x]; x.sort()
            y = [self.startPt.y, self.curPt.y]; y.sort()
            dc.DrawRectangle(x[0],y[0], x[1]-x[0],y[1]-y[0])


    def onMouseEvent(self,parent, event):
        handlers = { wx.EVT_LEFT_DOWN.evtType[0]:   self.onMouseLeftDown,
                     wx.EVT_MOTION.evtType[0]:      self.onMouseMotion,
                     wx.EVT_LEFT_UP.evtType[0]:     self.onMouseLeftUp,
                     wx.EVT_LEFT_DCLICK.evtType[0]: self.onMouseLeftDClick }
        handler = handlers.get(event.GetEventType())
        if handler is not None:
            return handler(parent,event)
        else:
            event.Skip()
            return False

    def onMouseLeftDown(self,parent,event):
        mousePt = wx.Point(event.X,event.Y)
        obj, handle = parent.getObjectAndSelectionHandleAt(mousePt)
        self.startPt = mousePt
        self.curPt = mousePt
        if obj is not None and handle is not None:
            self.curObject = obj
            self.curHandle = handle
        else:
            self.curObject = None
            self.curHandle = None
        
        obj = parent.getObjectAt(mousePt)
        if self.curObject is None and obj is not None:
            self.curObject = obj
            self.dragDelta = obj.position-mousePt
            self.curHandle = None
            parent.select(obj, event.ShiftDown())
            
        return True

    def onMouseMotion(self,parent,event):
        if not event.LeftIsDown(): return

        self.curPt = wx.Point(event.X,event.Y)

        obj,handle = self.curObject,self.curHandle
        if self._doingDragHandle():
            self._prepareToModify(parent)
            obj.moveHandle(handle,event.X,event.Y)
            parent.requestRedraw()

        elif self._doingDragObject():
            self._prepareToModify(parent)
            obj.position = self.curPt + self.dragDelta
            parent.requestRedraw()

        elif self._doingRectSelection():
            parent.requestRedraw()

        return True

    def onMouseLeftUp(self,parent,event):

        obj,handle = self.curObject,self.curHandle
        if self._doingDragHandle():
            obj.moveHandle(handle,event.X,event.Y)
            obj.finalizeHandle(handle,event.X,event.Y)

        elif self._doingDragObject():
            curPt = wx.Point(event.X,event.Y)
            obj.position = curPt + self.dragDelta

        elif self._doingRectSelection():
            x = [event.X, self.startPt.x]
            y = [event.Y, self.startPt.y]
            x.sort()
            y.sort()
            parent.selectByRectangle(x[0],y[0],x[1]-x[0],y[1]-y[0])
            

        self.curObject = None
        self.curHandle = None
        self.curPt = None
        self.startPt = None
        self.objModified = False
        parent.requestRedraw()

        return True

    def onMouseLeftDClick(self,parent,event):
        event.Skip()
        mousePt = wx.Point(event.X,event.Y)
        obj = parent.getObjectAt(mousePt)
        if obj and obj.hasPropertyEditor():
            if obj.doPropertyEdit(parent):
                parent.requestRedraw()
                return True

        return False

    
    def _prepareToModify(self,parent):
        if not self.objModified:
            parent.saveUndoInfo()
            self.objModified = True
        
    def _doingRectSelection(self):
        return self.curObject is None \
               and self.startPt is not None \
               and self.curPt is not None

    def _doingDragObject(self):
        return self.curObject is not None and self.curHandle is None

    def _doingDragHandle(self):
        return self.curObject is not None and self.curHandle is not None



#----------------------------------------------------------------------------
class LineDrawingTool(DrawingTool):
    """Represents the tool for drawing lines"""

    def __init__(self):
        self.newObject = None
        self.startPt = None


    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.StockCursor(wx.CURSOR_PENCIL)

    def draw(self, dc):
        if self.newObject is None: return
        self.newObject.draw(dc,True)

    def onMouseEvent(self,parent, event):
        handlers = { wx.EVT_LEFT_DOWN.evtType[0]: self.onMouseLeftDown,
                     wx.EVT_MOTION.evtType[0]:    self.onMouseMotion,
                     wx.EVT_LEFT_UP.evtType[0]:   self.onMouseLeftUp }
        handler = handlers.get(event.GetEventType())
        if handler is not None:
            return handler(parent,event)
        else:
            event.Skip()
            return False

    def onMouseLeftDown(self,parent, event):
        self.startPt = wx.Point(event.GetX(), event.GetY())
        self.newObject = None
        event.Skip()
        return True

    def onMouseMotion(self,parent, event):
        if not event.Dragging(): return

        if self.newObject is None:
            obj = LineDrawingObject(startPt=wx.Point(0,0),
                                    penColour=parent.penColour,
                                    fillColour=parent.fillColour,
                                    lineSize=parent.lineSize,
                                    position=wx.Point(event.X,event.Y))
            self.newObject = obj

        self._updateObjFromEvent(self.newObject, event)

        parent.requestRedraw()
        event.Skip()
        return True

    def onMouseLeftUp(self,parent, event):

        if self.newObject is None:
            return

        self._updateObjFromEvent(self.newObject,event)

        parent.addObject(self.newObject)

        self.newObject = None
        self.startPt = None

        event.Skip()
        return True


    def _updateObjFromEvent(self,obj,event):
        obj.setEndPt(wx.Point(event.X,event.Y))


#----------------------------------------------------------------------------
class RectDrawingTool(DrawingTool):
    """Represents the tool for drawing rectangles"""

    def __init__(self):
        self.newObject = None

    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.CROSS_CURSOR

    def draw(self, dc):
        if self.newObject is None: return
        self.newObject.draw(dc,True)


    def onMouseEvent(self,parent, event):
        handlers = { wx.EVT_LEFT_DOWN.evtType[0]: self.onMouseLeftDown,
                     wx.EVT_MOTION.evtType[0]:    self.onMouseMotion,
                     wx.EVT_LEFT_UP.evtType[0]:   self.onMouseLeftUp }
        handler = handlers.get(event.GetEventType())
        if handler is not None:
            return handler(parent,event)
        else:
            event.Skip()
            return False

    def onMouseLeftDown(self,parent, event):
        self.startPt = wx.Point(event.GetX(), event.GetY())
        self.newObject = None
        event.Skip()
        return True

    def onMouseMotion(self,parent, event):
        if not event.Dragging(): return

        if self.newObject is None:
            obj = RectDrawingObject(penColour=parent.penColour,
                                    fillColour=parent.fillColour,
                                    lineSize=parent.lineSize)
            self.newObject = obj

        self._updateObjFromEvent(self.newObject, event)

        parent.requestRedraw()
        event.Skip()
        return True

    def onMouseLeftUp(self,parent, event):

        if self.newObject is None:
            return

        self._updateObjFromEvent(self.newObject,event)

        parent.addObject(self.newObject)

        self.newObject = None

        event.Skip()
        return True


    def _updateObjFromEvent(self,obj,event):
        x = [event.X, self.startPt.x]
        y = [event.Y, self.startPt.y]
        x.sort()
        y.sort()
        width = x[1]-x[0]
        height = y[1]-y[0]

        obj.setPosition(wx.Point(x[0],y[0]))
        obj.setSize(wx.Size(width,height))

        


#----------------------------------------------------------------------------
class EllipseDrawingTool(DrawingTool):
    """Represents the tool for drawing ellipses"""

    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.CROSS_CURSOR


    def __init__(self):
        self.newObject = None

    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.CROSS_CURSOR

    def draw(self, dc):
        if self.newObject is None: return
        self.newObject.draw(dc,True)


    def onMouseEvent(self,parent, event):
        handlers = { wx.EVT_LEFT_DOWN.evtType[0]: self.onMouseLeftDown,
                     wx.EVT_MOTION.evtType[0]:    self.onMouseMotion,
                     wx.EVT_LEFT_UP.evtType[0]:   self.onMouseLeftUp }
        handler = handlers.get(event.GetEventType())
        if handler is not None:
            return handler(parent,event)
        else:
            event.Skip()
            return False
            
    def onMouseLeftDown(self,parent, event):
        self.startPt = wx.Point(event.GetX(), event.GetY())
        self.newObject = None
        event.Skip()
        return True

    def onMouseMotion(self,parent, event):
        if not event.Dragging(): return

        if self.newObject is None:
            obj = EllipseDrawingObject(penColour=parent.penColour,
                                       fillColour=parent.fillColour,
                                       lineSize=parent.lineSize)
            self.newObject = obj

        self._updateObjFromEvent(self.newObject, event)

        parent.requestRedraw()
        event.Skip()
        return True 

    def onMouseLeftUp(self,parent, event):

        if self.newObject is None:
            return

        self._updateObjFromEvent(self.newObject,event)

        parent.addObject(self.newObject)

        self.newObject = None

        event.Skip()
        return True


    def _updateObjFromEvent(self,obj,event):
        x = [event.X, self.startPt.x]
        y = [event.Y, self.startPt.y]
        x.sort()
        y.sort()
        width = x[1]-x[0]
        height = y[1]-y[0]

        obj.setPosition(wx.Point(x[0],y[0]))
        obj.setSize(wx.Size(width,height))


#----------------------------------------------------------------------------
class PolygonDrawingTool(DrawingTool):
    """Represents the tool for drawing polygons"""

    def __init__(self):
        self.newObject = None

    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.CROSS_CURSOR


    def draw(self, dc):
        if self.newObject is None: return
        self.newObject.draw(dc,True)


    def onMouseEvent(self,parent, event):
        handlers = { wx.EVT_LEFT_DOWN.evtType[0]:  self.onMouseLeftDown,
                     wx.EVT_MOTION.evtType[0]:     self.onMouseMotion,
                     wx.EVT_LEFT_UP.evtType[0]:    self.onMouseLeftUp, 
                     wx.EVT_LEFT_DCLICK.evtType[0]:self.onMouseLeftDClick }
        handler = handlers.get(event.GetEventType())
        if handler is not None:
            return handler(parent,event)
        else:
            event.Skip()
            return False
            
    def onMouseLeftDown(self,parent, event):
        event.Skip()
        self.startPt = (event.GetX(), event.GetY())
        if self.newObject is None:
            obj = PolygonDrawingObject(points=[(0,0)],penColour=parent.penColour,
                                       fillColour=parent.fillColour,
                                       lineSize=parent.lineSize,
                                       position=wx.Point(event.X, event.Y))
            obj.addPoint(event.X,event.Y)
            self.newObject = obj
        else:
            CLOSE_THRESH=3
            pt0 = self.newObject.getPoint(0)
            if abs(pt0[0]-event.X)<CLOSE_THRESH and abs(pt0[1]-event.Y)<CLOSE_THRESH:
                self.newObject.popPoint()
                parent.addObject(self.newObject)
                self.newObject = None
            else:
                self.newObject.addPoint(event.X,event.Y)

        return True

    def onMouseMotion(self,parent, event):

        event.Skip()
        if self.newObject:
            self.newObject.movePoint(-1, event.X, event.Y)
            parent.requestRedraw()
            return True 

        return False

    def onMouseLeftDClick(self,parent,event):
        event.Skip()
        if self.newObject:
            CLOSE_THRESH=3
            pt0 = self.newObject.getPoint(0)
            if abs(pt0[0]-event.X)<CLOSE_THRESH and abs(pt0[1]-event.Y)<CLOSE_THRESH:
                self.newObject.popPoint()
            self.newObject.popPoint()
            parent.addObject(self.newObject)
            self.newObject = None

        return True

    def onMouseLeftUp(self,parent, event):
        event.Skip()
        return True

        


#----------------------------------------------------------------------------
class ScribbleDrawingTool(DrawingTool):
    """Represents the tool for drawing scribble drawing objects"""

    def __init__(self):
        self.newObject = None

    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.StockCursor(wx.CURSOR_PENCIL)

    def draw(self, dc):
        if self.newObject is None: return
        self.newObject.draw(dc,True)


    def onMouseEvent(self,parent, event):
        handlers = { wx.EVT_LEFT_DOWN.evtType[0]:  self.onMouseLeftDown,
                     wx.EVT_MOTION.evtType[0]:     self.onMouseMotion,
                     wx.EVT_LEFT_UP.evtType[0]:    self.onMouseLeftUp 
                    }
        handler = handlers.get(event.GetEventType())
        if handler is not None:
            return handler(parent,event)
        else:
            event.Skip()
            return False
            
    def onMouseLeftDown(self,parent, event):
        event.Skip()
        obj = ScribbleDrawingObject(points=[(0,0)],penColour=parent.penColour,
                                    fillColour=parent.fillColour,
                                    lineSize=parent.lineSize,
                                    position=wx.Point(event.X, event.Y))
        self.newObject = obj
        return True

    def onMouseMotion(self,parent, event):
        event.Skip()
        if self.newObject:
            self.newObject.addPoint(event.X,event.Y)
            parent.requestRedraw()
            return True 

        return False

    def onMouseLeftUp(self,parent, event):
        event.Skip()
        if self.newObject:
            parent.addObject(self.newObject)
            self.newObject = None
        return True

        



#----------------------------------------------------------------------------
class TextDrawingTool(DrawingTool):
    """Represents the tool for drawing text"""

    def getDefaultCursor(self):
        """Return the cursor to use by default which this drawing tool is selected"""
        return wx.StockCursor(wx.CURSOR_IBEAM)

    def onMouseEvent(self,parent, event):
        handlers = { #wx.EVT_LEFT_DOWN.evtType[0]: self.onMouseLeftDown,
                     #wx.EVT_MOTION.evtType[0]:    self.onMouseMotion,
                     wx.EVT_LEFT_UP.evtType[0]:   self.onMouseLeftUp 
                     }
        handler = handlers.get(event.GetEventType())
        if handler is not None:
            return handler(parent,event)
        else:
            event.Skip()
            return False

    def onMouseLeftUp(self,parent, event):

        editor = parent.getTextEditor()
        editor.SetTitle("Create Text Object")
        if editor.ShowModal() == wx.ID_CANCEL:
            editor.Hide()
            return True

        obj = TextDrawingObject(position=wx.Point(event.X, event.Y))
        editor.dialogToObject(obj)
        editor.Hide()

        parent.addObject(obj)

        event.Skip()
        return True



#============================================================================
class DrawingObject(object):
    """ Base class for objects within the drawing panel.

        A pySketch document consists of a front-to-back ordered list of
        DrawingObjects.  Each DrawingObject has the following properties:

            'position'      The position of the object within the document.
            'size'          The size of the object within the document.
            'penColour'     The colour to use for drawing the object's outline.
            'fillColour'    Colour to use for drawing object's interior.
            'lineSize'      Line width (in pixels) to use for object's outline.
            """

    # ==================
    # == Constructors ==
    # ==================

    def __init__(self, position=wx.Point(0, 0), size=wx.Size(0, 0),
                 penColour=wx.BLACK, fillColour=wx.WHITE, lineSize=1,
                 ):
        """ Standard constructor.

            The remaining parameters let you set various options for the newly
            created DrawingObject.
        """
        # One must take great care with constructed default arguments
        # like wx.Point(0,0) above.  *EVERY* caller that uses the
        # default will get the same instance.  Thus, below we make a
        # deep copy of those arguments with object defaults.

        self.position          = wx.Point(position.x,position.y)
        self.size              = wx.Size(size.x,size.y)
        self.penColour         = penColour
        self.fillColour        = fillColour
        self.lineSize          = lineSize

    # =============================
    # == Object Property Methods ==
    # =============================

    def getData(self):
        """ Return a copy of the object's internal data.

            This is used to save this DrawingObject to disk.
        """
        return [self.position.x, self.position.y,
                self.size.width, self.size.height,
                self.penColour.Red(),
                self.penColour.Green(),
                self.penColour.Blue(),
                self.fillColour.Red(),
                self.fillColour.Green(),
                self.fillColour.Blue(),
                self.lineSize]


    def setData(self, data):
        """ Set the object's internal data.

            'data' is a copy of the object's saved data, as returned by
            getData() above.  This is used to restore a previously saved
            DrawingObject.

            Returns an iterator to any remaining data not consumed by 
            this base class method.
        """
        #data = copy.deepcopy(data) # Needed?

        d = iter(data)
        try:
            self.position          = wx.Point(d.next(), d.next())
            self.size              = wx.Size(d.next(), d.next())
            self.penColour         = wx.Colour(red=d.next(),
                                              green=d.next(),
                                              blue=d.next())
            self.fillColour        = wx.Colour(red=d.next(),
                                              green=d.next(),
                                              blue=d.next())
            self.lineSize          = d.next()
        except StopIteration:
            raise ValueError('Not enough data in setData call')

        return d


    def hasPropertyEditor(self):
        return False

    def doPropertyEdit(self, parent):
        assert False, "Must be overridden if hasPropertyEditor returns True"

    def setPosition(self, position):
        """ Set the origin (top-left corner) for this DrawingObject.
        """
        self.position = position


    def getPosition(self):
        """ Return this DrawingObject's position.
        """
        return self.position


    def setSize(self, size):
        """ Set the size for this DrawingObject.
        """
        self.size = size


    def getSize(self):
        """ Return this DrawingObject's size.
        """
        return self.size


    def setPenColour(self, colour):
        """ Set the pen colour used for this DrawingObject.
        """
        self.penColour = colour


    def getPenColour(self):
        """ Return this DrawingObject's pen colour.
        """
        return self.penColour


    def setFillColour(self, colour):
        """ Set the fill colour used for this DrawingObject.
        """
        self.fillColour = colour


    def getFillColour(self):
        """ Return this DrawingObject's fill colour.
        """
        return self.fillColour


    def setLineSize(self, lineSize):
        """ Set the linesize used for this DrawingObject.
        """
        self.lineSize = lineSize


    def getLineSize(self):
        """ Return this DrawingObject's line size.
        """
        return self.lineSize


    # ============================
    # == Object Drawing Methods ==
    # ============================

    def draw(self, dc, selected):
        """ Draw this DrawingObject into our window.

            'dc' is the device context to use for drawing.  

            If 'selected' is True, the object is currently selected.
            Drawing objects can use this to change the way selected objects
            are drawn, however the actual drawing of selection handles
            should be done in the 'drawHandles' method
        """
        if self.lineSize == 0:
            dc.SetPen(wx.Pen(self.penColour, self.lineSize, wx.TRANSPARENT))
        else:
            dc.SetPen(wx.Pen(self.penColour, self.lineSize, wx.SOLID))
        dc.SetBrush(wx.Brush(self.fillColour, wx.SOLID))

        self._privateDraw(dc, self.position, selected)


    def drawHandles(self, dc):
        """Draw selection handles for this DrawingObject"""

        # Default is to draw selection handles at all four corners.
        dc.SetPen(wx.BLACK_PEN)
        dc.SetBrush(wx.BLACK_BRUSH)
        
        x,y = self.position
        self._drawSelHandle(dc, x, y)
        self._drawSelHandle(dc, x + self.size.width, y)
        self._drawSelHandle(dc, x, y + self.size.height)
        self._drawSelHandle(dc, x + self.size.width, y + self.size.height)


    # =======================
    # == Selection Methods ==
    # =======================

    def objectContainsPoint(self, x, y):
        """ Returns True iff this object contains the given point.

            This is used to determine if the user clicked on the object.
        """
        # Firstly, ignore any points outside of the object's bounds.

        if x < self.position.x: return False
        if x > self.position.x + self.size.x: return False
        if y < self.position.y: return False
        if y > self.position.y + self.size.y: return False

        # Now things get tricky.  There's no straightforward way of
        # knowing whether the point is within an arbitrary object's
        # bounds...to get around this, we draw the object into a
        # memory-based bitmap and see if the given point was drawn.
        # This could no doubt be done more efficiently by some tricky
        # maths, but this approach works and is simple enough.

        # Subclasses can implement smarter faster versions of this.

        bitmap = wx.EmptyBitmap(self.size.x + 10, self.size.y + 10)
        dc = wx.MemoryDC()
        dc.SelectObject(bitmap)
        dc.BeginDrawing()
        dc.SetBackground(wx.WHITE_BRUSH)
        dc.Clear()
        dc.SetPen(wx.Pen(wx.BLACK, self.lineSize + 5, wx.SOLID))
        dc.SetBrush(wx.BLACK_BRUSH)
        self._privateDraw(dc, wx.Point(5, 5), True)
        dc.EndDrawing()
        pixel = dc.GetPixel(x - self.position.x + 5, y - self.position.y + 5)
        if (pixel.Red() == 0) and (pixel.Green() == 0) and (pixel.Blue() == 0):
            return True
        else:
            return False

    handle_TOP    = 0
    handle_BOTTOM = 1
    handle_LEFT   = 0
    handle_RIGHT  = 1

    def getSelectionHandleContainingPoint(self, x, y):
        """ Return the selection handle containing the given point, if any.

            We return one of the predefined selection handle ID codes.
        """
        # Default implementation assumes selection handles at all four bbox corners.
        # Return a list so we can modify the contents later in moveHandle()
        if self._pointInSelRect(x, y, self.position.x, self.position.y):
            return [self.handle_TOP, self.handle_LEFT]
        elif self._pointInSelRect(x, y, self.position.x + self.size.width,
                                        self.position.y):
            return [self.handle_TOP, self.handle_RIGHT]
        elif self._pointInSelRect(x, y, self.position.x,
                                        self.position.y + self.size.height):
            return [self.handle_BOTTOM, self.handle_LEFT]
        elif self._pointInSelRect(x, y, self.position.x + self.size.width,
                                        self.position.y + self.size.height):
            return [self.handle_BOTTOM, self.handle_RIGHT]
        else:
            return None

    def moveHandle(self, handle, x, y):
        """ Move the specified selection handle to given canvas location.
        """
        assert handle is not None

        # Default implementation assumes selection handles at all four bbox corners.
        pt = wx.Point(x,y)
        x,y = self.position
        w,h = self.size
        if handle[0] == self.handle_TOP:
            if handle[1] == self.handle_LEFT:
                dpos = pt - self.position
                self.position = pt
                self.size.width -= dpos.x
                self.size.height -= dpos.y
            else:
                dx = pt.x - ( x + w )
                dy = pt.y - ( y )
                self.position.y = pt.y
                self.size.width += dx
                self.size.height -= dy
        else: # BOTTOM
            if handle[1] == self.handle_LEFT:
                dx = pt.x - ( x )
                dy = pt.y - ( y + h )
                self.position.x = pt.x
                self.size.width -= dx
                self.size.height += dy
            else: 
                dpos = pt - self.position
                dpos.x -= w
                dpos.y -= h
                self.size.width += dpos.x
                self.size.height += dpos.y


        # Finally, normalize so no negative widths or heights.
        # And update the handle variable accordingly.
        if self.size.height<0:
            self.position.y += self.size.height
            self.size.height = -self.size.height
            handle[0] = 1-handle[0]

        if self.size.width<0:
            self.position.x += self.size.width
            self.size.width = -self.size.width
            handle[1] = 1-handle[1]
            


    def finalizeHandle(self, handle, x, y):
        pass


    def objectWithinRect(self, x, y, width, height):
        """ Return True iff this object falls completely within the given rect.
        """
        if x          > self.position.x:                    return False
        if x + width  < self.position.x + self.size.width:  return False
        if y          > self.position.y:                    return False
        if y + height < self.position.y + self.size.height: return False
        return True

    # =====================
    # == Private Methods ==
    # =====================

    def _privateDraw(self, dc, position, selected):
        """ Private routine to draw this DrawingObject.

            'dc' is the device context to use for drawing, while 'position' is
            the position in which to draw the object.
        """
        pass

    def _drawSelHandle(self, dc, x, y):
        """ Draw a selection handle around this DrawingObject.

            'dc' is the device context to draw the selection handle within,
            while 'x' and 'y' are the coordinates to use for the centre of the
            selection handle.
        """
        dc.DrawRectangle(x - 3, y - 3, 6, 6)


    def _pointInSelRect(self, x, y, rX, rY):
        """ Return True iff (x, y) is within the selection handle at (rX, ry).
        """
        if   x < rX - 3: return False
        elif x > rX + 3: return False
        elif y < rY - 3: return False
        elif y > rY + 3: return False
        else:            return True


#----------------------------------------------------------------------------
class LineDrawingObject(DrawingObject):
    """ DrawingObject subclass that represents one line segment.

        Adds the following members to the base DrawingObject:
            'startPt'       The point, relative to the object's position, where
                            the line starts.
            'endPt'         The point, relative to the object's position, where
                            the line ends.
            """

    def __init__(self, startPt=wx.Point(0,0), endPt=wx.Point(0,0), *varg, **kwarg):
        DrawingObject.__init__(self, *varg, **kwarg)

        self.startPt           = wx.Point(startPt.x,startPt.y)
        self.endPt             = wx.Point(endPt.x,endPt.y)

    # ============================
    # == Object Drawing Methods ==
    # ============================

    def drawHandles(self, dc):
        """Draw selection handles for this DrawingObject"""

        dc.SetPen(wx.BLACK_PEN)
        dc.SetBrush(wx.BLACK_BRUSH)

        x,y = self.position
        # Draw selection handles at the start and end points.
        self._drawSelHandle(dc, x + self.startPt.x, y + self.startPt.y)
        self._drawSelHandle(dc, x + self.endPt.x,   y + self.endPt.y)



    # =======================
    # == Selection Methods ==
    # =======================


    handle_START_POINT = 1
    handle_END_POINT = 2

    def getSelectionHandleContainingPoint(self, x, y):
        """ Return the selection handle containing the given point, if any.

            We return one of the predefined selection handle ID codes.
        """
        # We have selection handles at the start and end points.
        if self._pointInSelRect(x, y, self.position.x + self.startPt.x,
                                      self.position.y + self.startPt.y):
            return self.handle_START_POINT
        elif self._pointInSelRect(x, y, self.position.x + self.endPt.x,
                                        self.position.y + self.endPt.y):
            return self.handle_END_POINT
        else:
            return None
        
    def moveHandle(self, handle, x, y):
        """Move the handle to specified handle to the specified canvas coordinates
        """
        ptTrans = wx.Point(x-self.position.x, y-self.position.y)
        if handle == self.handle_START_POINT:
            self.startPt = ptTrans
        elif handle == self.handle_END_POINT:
            self.endPt = ptTrans
        else:
            raise ValueError("Bad handle type for a line")

        self._updateBoundingBox()

    # =============================
    # == Object Property Methods ==
    # =============================

    def getData(self):
        """ Return a copy of the object's internal data.

            This is used to save this DrawingObject to disk.
        """
        # get the basics
        data = DrawingObject.getData(self)
        # add our specifics
        data += [self.startPt.x, self.startPt.y,
                 self.endPt.x,   self.endPt.y]
        return data

    def setData(self, data):
        """ Set the object's internal data.

            'data' is a copy of the object's saved data, as returned by
            getData() above.  This is used to restore a previously saved
            DrawingObject.
        """
        #data = copy.deepcopy(data) # Needed?

        d = DrawingObject.setData(self, data)

        try:
            self.startPt           = wx.Point(d.next(), d.next())
            self.endPt             = wx.Point(d.next(), d.next())
        except StopIteration:
            raise ValueError('Not enough data in setData call')

        return d


    def setStartPt(self, startPt):
        """ Set the starting point for this line DrawingObject.
        """
        self.startPt = startPt - self.position
        self._updateBoundingBox()


    def getStartPt(self):
        """ Return the starting point for this line DrawingObject.
        """
        return self.startPt + self.position


    def setEndPt(self, endPt):
        """ Set the ending point for this line DrawingObject.
        """
        self.endPt = endPt - self.position
        self._updateBoundingBox()


    def getEndPt(self):
        """ Return the ending point for this line DrawingObject.
        """
        return self.endPt + self.position


    # =====================
    # == Private Methods ==
    # =====================


    def _privateDraw(self, dc, position, selected):
        """ Private routine to draw this DrawingObject.

            'dc' is the device context to use for drawing, while 'position' is
            the position in which to draw the object.  If 'selected' is True,
            the object is drawn with selection handles.  This private drawing
            routine assumes that the pen and brush have already been set by the
            caller.
        """
        dc.DrawLine(position.x + self.startPt.x,
                    position.y + self.startPt.y,
                    position.x + self.endPt.x,
                    position.y + self.endPt.y)
            
    def _updateBoundingBox(self):
        x = [self.startPt.x, self.endPt.x]; x.sort()
        y = [self.startPt.y, self.endPt.y]; y.sort()

        dp = wx.Point(-x[0],-y[0])
        self.position.x += x[0]
        self.position.y += y[0]
        self.size.width = x[1]-x[0]
        self.size.height = y[1]-y[0]

        self.startPt += dp
        self.endPt += dp

#----------------------------------------------------------------------------
class PolygonDrawingObject(DrawingObject):
    """ DrawingObject subclass that represents a poly-line or polygon
    """
    def __init__(self, points=[], *varg, **kwarg):
        DrawingObject.__init__(self, *varg, **kwarg)
        self.points = list(points)

    # =======================
    # == Selection Methods ==
    # =======================

    def getSelectionHandleContainingPoint(self, x, y):
        """ Return the selection handle containing the given point, if any.

            We return one of the predefined selection handle ID codes.
        """
        # We have selection handles at the start and end points.
        for i,p in enumerate(self.points):
            if self._pointInSelRect(x, y,
                                    self.position.x + p[0],
                                    self.position.y + p[1]):
                return i+1

        return None
        

    def addPoint(self, x,y):
        self.points.append((x-self.position.x,y-self.position.y))
        self._updateBoundingBox()

    def getPoint(self, idx):
        x,y = self.points[idx]
        return (x+self.position.x,y+self.position.y)

    def movePoint(self, idx, x,y):
        self.points[idx] = (x-self.position.x,y-self.position.y)
        self._updateBoundingBox()

    def popPoint(self, idx=-1):
        self.points.pop(idx)
        self._updateBoundingBox()

    # =====================
    # == Drawing Methods ==
    # =====================

    def drawHandles(self, dc):
        """Draw selection handles for this DrawingObject"""

        dc.SetPen(wx.BLACK_PEN)
        dc.SetBrush(wx.BLACK_BRUSH)

        x,y = self.position
        # Draw selection handles at the start and end points.
        for p in self.points:
            self._drawSelHandle(dc, x + p[0], y + p[1])

    def moveHandle(self, handle, x, y):
        """Move the specified handle"""
        self.movePoint(handle-1,x,y)


    # =============================
    # == Object Property Methods ==
    # =============================

    def getData(self):
        """ Return a copy of the object's internal data.

            This is used to save this DrawingObject to disk.
        """
        # get the basics
        data = DrawingObject.getData(self)
        # add our specifics
        data += [list(self.points)]

        return data


    def setData(self, data):
        """ Set the object's internal data.

            'data' is a copy of the object's saved data, as returned by
            getData() above.  This is used to restore a previously saved
            DrawingObject.
        """
        #data = copy.deepcopy(data) # Needed?
        d = DrawingObject.setData(self, data)

        try:
            self.points            = d.next()
        except StopIteration:
            raise ValueError('Not enough data in setData call')

        return d
        

    # =====================
    # == Private Methods ==
    # =====================
    def _privateDraw(self, dc, position, selected):
        """ Private routine to draw this DrawingObject.

            'dc' is the device context to use for drawing, while 'position' is
            the position in which to draw the object.  If 'selected' is True,
            the object is drawn with selection handles.  This private drawing
            routine assumes that the pen and brush have already been set by the
            caller.
        """
        dc.DrawPolygon(self.points, position.x, position.y)

    def _updateBoundingBox(self):
        x = min([p[0] for p in self.points])
        y = min([p[1] for p in self.points])
        x2 = max([p[0] for p in self.points])
        y2 = max([p[1] for p in self.points])
        dx = -x
        dy = -y
        self.position.x += x
        self.position.y += y
        self.size.width = x2-x
        self.size.height = y2-y
        # update coords also because they're relative to self.position
        for i,p in enumerate(self.points):
            self.points[i] = (p[0]+dx,p[1]+dy)
            

#----------------------------------------------------------------------------
class ScribbleDrawingObject(DrawingObject):
    """ DrawingObject subclass that represents a poly-line or polygon
    """
    def __init__(self, points=[], *varg, **kwarg):
        DrawingObject.__init__(self, *varg, **kwarg)
        self.points = list(points)

    # =======================
    # == Selection Methods ==
    # =======================

    def addPoint(self, x,y):
        self.points.append((x-self.position.x,y-self.position.y))
        self._updateBoundingBox()

    def getPoint(self, idx):
        x,y = self.points[idx]
        return (x+self.position.x,y+self.position.y)

    def movePoint(self, idx, x,y):
        self.points[idx] = (x-self.position.x,y-self.position.y)
        self._updateBoundingBox()

    def popPoint(self, idx=-1):
        self.points.pop(idx)
        self._updateBoundingBox()


    # =============================
    # == Object Property Methods ==
    # =============================

    def getData(self):
        """ Return a copy of the object's internal data.

            This is used to save this DrawingObject to disk.
        """
        # get the basics
        data = DrawingObject.getData(self)
        # add our specifics
        data += [list(self.points)]

        return data


    def setData(self, data):
        """ Set the object's internal data.

            'data' is a copy of the object's saved data, as returned by
            getData() above.  This is used to restore a previously saved
            DrawingObject.
        """
        #data = copy.deepcopy(data) # Needed?
        d = DrawingObject.setData(self, data)

        try:
            self.points            = d.next()
        except StopIteration:
            raise ValueError('Not enough data in setData call')

        return d
        

    # =====================
    # == Private Methods ==
    # =====================
    def _privateDraw(self, dc, position, selected):
        """ Private routine to draw this DrawingObject.

            'dc' is the device context to use for drawing, while 'position' is
            the position in which to draw the object.  If 'selected' is True,
            the object is drawn with selection handles.  This private drawing
            routine assumes that the pen and brush have already been set by the
            caller.
        """
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        dc.DrawLines(self.points, position.x, position.y)

    def _updateBoundingBox(self):
        x = min([p[0] for p in self.points])
        y = min([p[1] for p in self.points])
        x2 = max([p[0] for p in self.points])
        y2 = max([p[1] for p in self.points])
        dx = -x
        dy = -y
        self.position = wx.Point(self.position.x + x,self.position.y + y)
        self.size = wx.Size(x2-x, y2-y)
        #self.position.x += x
        #self.position.y += y
        #self.size.width = x2-x
        #self.size.height = y2-y
        # update coords also because they're relative to self.position
        for i,p in enumerate(self.points):
            self.points[i] = (p[0]+dx,p[1]+dy)

#----------------------------------------------------------------------------
class RectDrawingObject(DrawingObject):
    """ DrawingObject subclass that represents an axis-aligned rectangle.
    """
    def __init__(self, *varg, **kwarg):
        DrawingObject.__init__(self, *varg, **kwarg)

    def objectContainsPoint(self, x, y):
        """ Returns True iff this object contains the given point.

            This is used to determine if the user clicked on the object.
        """
        # Firstly, ignore any points outside of the object's bounds.

        if x < self.position.x: return False
        if x > self.position.x + self.size.x: return False
        if y < self.position.y: return False
        if y > self.position.y + self.size.y: return False

        # Rectangles are easy -- they're always selected if the
        # point is within their bounds.
        return True

    # =====================
    # == Private Methods ==
    # =====================

    def _privateDraw(self, dc, position, selected):
        """ Private routine to draw this DrawingObject.

            'dc' is the device context to use for drawing, while 'position' is
            the position in which to draw the object.  If 'selected' is True,
            the object is drawn with selection handles.  This private drawing
            routine assumes that the pen and brush have already been set by the
            caller.
        """
        dc.DrawRectangle(position.x, position.y,
                         self.size.width, self.size.height)


#----------------------------------------------------------------------------
class EllipseDrawingObject(DrawingObject):
    """ DrawingObject subclass that represents an axis-aligned ellipse.
    """
    def __init__(self, *varg, **kwarg):
        DrawingObject.__init__(self, *varg, **kwarg)

    # =====================
    # == Private Methods ==
    # =====================
    def _privateDraw(self, dc, position, selected):
        """ Private routine to draw this DrawingObject.

            'dc' is the device context to use for drawing, while 'position' is
            the position in which to draw the object.  If 'selected' is True,
            the object is drawn with selection handles.  This private drawing
            routine assumes that the pen and brush have already been set by the
            caller.
        """
        dc.DrawEllipse(position.x, position.y,
                       self.size.width, self.size.height)




#----------------------------------------------------------------------------
class TextDrawingObject(DrawingObject):
    """ DrawingObject subclass that holds text.

        Adds the following members to the base DrawingObject:
            'text'          The object's text (obj_TEXT objects only).
            'textFont'      The text object's font name.
            """

    def __init__(self, text=None, *varg, **kwarg):
        DrawingObject.__init__(self, *varg, **kwarg)

        self.text              = text
        self.textFont          = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)


    # =============================
    # == Object Property Methods ==
    # =============================

    def getData(self):
        """ Return a copy of the object's internal data.

            This is used to save this DrawingObject to disk.
        """
        # get the basics
        data = DrawingObject.getData(self)
        # add our specifics
        data += [self.text, self.textFont.GetNativeFontInfoDesc()]

        return data


    def setData(self, data):
        """ Set the object's internal data.

            'data' is a copy of the object's saved data, as returned by
            getData() above.  This is used to restore a previously saved
            DrawingObject.
        """
        d = DrawingObject.setData(self, data)

        try:
            self.text              = d.next()
            desc                   = d.next()
            self.textFont = wx.FontFromNativeInfoString(desc)
        except StopIteration:
            raise ValueError('Not enough data in setData call')

        return d
        

    def hasPropertyEditor(self):
        return True

    def doPropertyEdit(self, parent):
        editor = parent.getTextEditor()
        editor.SetTitle("Edit Text Object")
        editor.objectToDialog(self)
        if editor.ShowModal() == wx.ID_CANCEL:
            editor.Hide()
            return False

        parent.saveUndoInfo()

        editor.dialogToObject(self)
        editor.Hide()

        return True


    def setText(self, text):
        """ Set the text for this DrawingObject.
        """
        self.text = text


    def getText(self):
        """ Return this DrawingObject's text.
        """
        return self.text


    def setFont(self, font):
        """ Set the font for this text DrawingObject.
        """
        self.textFont = font


    def getFont(self):
        """ Return this text DrawingObject's font.
        """
        return self.textFont



    # ============================
    # == Object Drawing Methods ==
    # ============================

    def draw(self, dc, selected):
        """ Draw this DrawingObject into our window.

            'dc' is the device context to use for drawing.  If 'selected' is
            True, the object is currently selected and should be drawn as such.
        """
        dc.SetTextForeground(self.penColour)
        dc.SetTextBackground(self.fillColour)

        self._privateDraw(dc, self.position, selected)

    def objectContainsPoint(self, x, y):
        """ Returns True iff this object contains the given point.

            This is used to determine if the user clicked on the object.
        """
        # Firstly, ignore any points outside of the object's bounds.

        if x < self.position.x: return False
        if x > self.position.x + self.size.x: return False
        if y < self.position.y: return False
        if y > self.position.y + self.size.y: return False

        # Text is easy -- it's always selected if the
        # point is within its bounds.
        return True


    def fitToText(self):
        """ Resize a text DrawingObject so that it fits it's text exactly.
        """

        dummyWindow = wx.Frame(None, -1, "")
        dummyWindow.SetFont(self.textFont)
        width, height = dummyWindow.GetTextExtent(self.text)
        dummyWindow.Destroy()

        self.size = wx.Size(width, height)

    # =====================
    # == Private Methods ==
    # =====================

    def _privateDraw(self, dc, position, selected):
        """ Private routine to draw this DrawingObject.

            'dc' is the device context to use for drawing, while 'position' is
            the position in which to draw the object.  If 'selected' is True,
            the object is drawn with selection handles.  This private drawing
            routine assumes that the pen and brush have already been set by the
            caller.
        """
        dc.SetFont(self.textFont)
        dc.DrawText(self.text, position.x, position.y)



#----------------------------------------------------------------------------
class ToolPaletteToggleX(wx.ToggleButton):
    """ An icon appearing in the tool palette area of our sketching window.

        Note that this is actually implemented as a wx.Bitmap rather
        than as a wx.Icon.  wx.Icon has a very specific meaning, and isn't
        appropriate for this more general use.
    """

    def __init__(self, parent, iconID, iconName, toolTip, mode = wx.ITEM_NORMAL):
        """ Standard constructor.

            'parent'   is the parent window this icon will be part of.
            'iconID'   is the internal ID used for this icon.
            'iconName' is the name used for this icon.
            'toolTip'  is the tool tip text to show for this icon.
            'mode'     is one of wx.ITEM_NORMAL, wx.ITEM_CHECK, wx.ITEM_RADIO

            The icon name is used to get the appropriate bitmap for this icon.
        """
        bmp = wx.Bitmap("images/" + iconName + "Icon.bmp", wx.BITMAP_TYPE_BMP)
        bmpsel = wx.Bitmap("images/" + iconName + "IconSel.bmp", wx.BITMAP_TYPE_BMP)

        wx.ToggleButton.__init__(self, parent, iconID,
                                 size=(bmp.GetWidth()+1, bmp.GetHeight()+1)
                                 )
        self.SetLabel( iconName )
        self.SetToolTip(wx.ToolTip(toolTip))
        #self.SetBitmapLabel(bmp)
        #self.SetBitmapSelected(bmpsel)

        self.iconID     = iconID
        self.iconName   = iconName

class ToolPaletteToggle(GenBitmapToggleButton):
    """ An icon appearing in the tool palette area of our sketching window.

        Note that this is actually implemented as a wx.Bitmap rather
        than as a wx.Icon.  wx.Icon has a very specific meaning, and isn't
        appropriate for this more general use.
    """

    def __init__(self, parent, iconID, iconName, toolTip, mode = wx.ITEM_NORMAL):
        """ Standard constructor.

            'parent'   is the parent window this icon will be part of.
            'iconID'   is the internal ID used for this icon.
            'iconName' is the name used for this icon.
            'toolTip'  is the tool tip text to show for this icon.
            'mode'     is one of wx.ITEM_NORMAL, wx.ITEM_CHECK, wx.ITEM_RADIO

            The icon name is used to get the appropriate bitmap for this icon.
        """
        bmp = wx.Bitmap("images/" + iconName + "Icon.bmp", wx.BITMAP_TYPE_BMP)
        bmpsel = wx.Bitmap("images/" + iconName + "IconSel.bmp", wx.BITMAP_TYPE_BMP)

        GenBitmapToggleButton.__init__(self, parent, iconID, bitmap=bmp, 
                                       size=(bmp.GetWidth()+1, bmp.GetHeight()+1),
                                       style=wx.BORDER_NONE)

        self.SetToolTip(wx.ToolTip(toolTip))
        self.SetBitmapLabel(bmp)
        self.SetBitmapSelected(bmpsel)

        self.iconID     = iconID
        self.iconName   = iconName


class ToolPaletteButton(GenBitmapButton):
    """ An icon appearing in the tool palette area of our sketching window.

        Note that this is actually implemented as a wx.Bitmap rather
        than as a wx.Icon.  wx.Icon has a very specific meaning, and isn't
        appropriate for this more general use.
    """

    def __init__(self, parent, iconID, iconName, toolTip):
        """ Standard constructor.

            'parent'   is the parent window this icon will be part of.
            'iconID'   is the internal ID used for this icon.
            'iconName' is the name used for this icon.
            'toolTip'  is the tool tip text to show for this icon.

            The icon name is used to get the appropriate bitmap for this icon.
        """
        bmp = wx.Bitmap("images/" + iconName + "Icon.bmp", wx.BITMAP_TYPE_BMP)
        GenBitmapButton.__init__(self, parent, iconID, bitmap=bmp, 
                                 size=(bmp.GetWidth()+1, bmp.GetHeight()+1),
                                 style=wx.BORDER_NONE)
        self.SetToolTip(wx.ToolTip(toolTip))
        self.SetBitmapLabel(bmp)

        self.iconID     = iconID
        self.iconName   = iconName



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

class ToolOptionIndicator(wx.Window):
    """ A visual indicator which shows the current tool options.
    """
    def __init__(self, parent):
        """ Standard constructor.
        """
        wx.Window.__init__(self, parent, -1, wx.DefaultPosition, wx.Size(52, 32))

        self.penColour  = wx.BLACK
        self.fillColour = wx.WHITE
        self.lineSize   = 1

        # Win95 can only handle a 8x8 stipple bitmaps max
        #self.stippleBitmap = wx.BitmapFromBits("\xf0"*4 + "\x0f"*4,8,8)
        # ...but who uses Win95?
        self.stippleBitmap = wx.BitmapFromBits("\xff\x00"*8+"\x00\xff"*8,16,16)
        self.stippleBitmap.SetMask(wx.Mask(self.stippleBitmap))

        self.Bind(wx.EVT_PAINT, self.onPaint)


    def setPenColour(self, penColour):
        """ Set the indicator's current pen colour.
        """
        self.penColour = penColour
        self.Refresh()


    def setFillColour(self, fillColour):
        """ Set the indicator's current fill colour.
        """
        self.fillColour = fillColour
        self.Refresh()


    def setLineSize(self, lineSize):
        """ Set the indicator's current pen colour.
        """
        self.lineSize = lineSize
        self.Refresh()


    def onPaint(self, event):
        """ Paint our tool option indicator.
        """
        dc = wx.PaintDC(self)
        dc.BeginDrawing()

        dc.SetPen(wx.BLACK_PEN)
        bgbrush = wx.Brush(wx.WHITE, wx.STIPPLE_MASK_OPAQUE)
        bgbrush.SetStipple(self.stippleBitmap)
        dc.SetTextForeground(wx.LIGHT_GREY)
        dc.SetTextBackground(wx.WHITE)
        dc.SetBrush(bgbrush)
        dc.DrawRectangle(0, 0, self.GetSize().width,self.GetSize().height)

        if self.lineSize == 0:
            dc.SetPen(wx.Pen(self.penColour, self.lineSize, wx.TRANSPARENT))
        else:
            dc.SetPen(wx.Pen(self.penColour, self.lineSize, wx.SOLID))
        dc.SetBrush(wx.Brush(self.fillColour, wx.SOLID))

        size = self.GetSize()
        ctrx = size.x/2
        ctry = size.y/2
        radius = min(size)//2 - 5
        dc.DrawCircle(ctrx, ctry, radius)

        dc.EndDrawing()

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

class EditTextObjectDialog(wx.Dialog):
    """ Dialog box used to edit the properties of a text object.

        The user can edit the object's text, font, size, and text style.
    """

    def __init__(self, parent, title):
        """ Standard constructor.
        """
        wx.Dialog.__init__(self, parent, -1, title,
                           style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)

        self.textCtrl = wx.TextCtrl(
            self, 1001, "Enter text here", style=wx.TE_PROCESS_ENTER|wx.TE_RICH,
            validator=TextObjectValidator()
            )
        extent = self.textCtrl.GetFullTextExtent("Hy")
        lineHeight = extent[1] + extent[3]
        self.textCtrl.SetSize(wx.Size(-1, lineHeight * 4))
        self.curFont = self.textCtrl.GetFont()
        self.curClr = wx.BLACK

        self.Bind(wx.EVT_TEXT_ENTER, self._doEnter, id=1001)

        fontBtn = wx.Button(self, -1, "Select Font...")
        self.Bind(wx.EVT_BUTTON, self.OnSelectFont, fontBtn)

        gap = wx.LEFT | wx.TOP | wx.RIGHT

        self.okButton     = wx.Button(self, wx.ID_OK,     "&OK")
        self.okButton.SetDefault()
        self.cancelButton = wx.Button(self, wx.ID_CANCEL, "&Cancel")

        btnSizer = wx.StdDialogButtonSizer()

        btnSizer.Add(self.okButton,     0, gap, 5)
        btnSizer.Add(self.cancelButton, 0, gap, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.textCtrl, 1, gap | wx.EXPAND,       5)
        sizer.Add(fontBtn,    0, gap | wx.ALIGN_RIGHT, 5)
        sizer.Add((10, 10)) # Spacer.
        btnSizer.Realize()
        sizer.Add(btnSizer,      0, gap | wx.ALIGN_CENTRE, 5)

        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        sizer.Fit(self)

        self.textCtrl.SetFocus()


    def OnSelectFont(self, evt):
        """Shows the font dialog and sets the font of the sample text"""
        data = wx.FontData()
        data.EnableEffects(True)
        data.SetColour(self.curClr)         # set colour
        data.SetInitialFont(self.curFont)

        dlg = wx.FontDialog(self, data)
        
        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetFontData()
            font = data.GetChosenFont()
            colour = data.GetColour()

            self.curFont = font
            self.curClr = colour

            self.textCtrl.SetFont(font)
            # Update dialog for the new height of the text
            self.GetSizer().Fit(self)

        dlg.Destroy()


    def objectToDialog(self, obj):
        """ Copy the properties of the given text object into the dialog box.
        """
        self.textCtrl.SetValue(obj.getText())
        self.textCtrl.SetSelection(0, len(obj.getText()))

        self.curFont = obj.getFont()
        self.textCtrl.SetFont(self.curFont)



    def dialogToObject(self, obj):
        """ Copy the properties from the dialog box into the given text object.
        """
        obj.setText(self.textCtrl.GetValue())
        obj.setFont(self.curFont)
        obj.fitToText()

    # ======================
    # == Private Routines ==
    # ======================

    def _doEnter(self, event):
        """ Respond to the user hitting the ENTER key.

            We simulate clicking on the "OK" button.
        """
        if self.Validate(): self.Show(False)

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

class TextObjectValidator(wx.PyValidator):
    """ This validator is used to ensure that the user has entered something
        into the text object editor dialog's text field.
    """
    def __init__(self):
        """ Standard constructor.
        """
        wx.PyValidator.__init__(self)


    def Clone(self):
        """ Standard cloner.

            Note that every validator must implement the Clone() method.
        """
        return TextObjectValidator()


    def Validate(self, win):
        """ Validate the contents of the given text control.
        """
        textCtrl = self.GetWindow()
        text = textCtrl.GetValue()

        if len(text) == 0:
            wx.MessageBox("A text object must contain some text!", "Error")
            return False
        else:
            return True


    def TransferToWindow(self):
        """ Transfer data from validator to window.

            The default implementation returns False, indicating that an error
            occurred.  We simply return True, as we don't do any data transfer.
        """
        return True # Prevent wx.Dialog from complaining.


    def TransferFromWindow(self):
        """ Transfer data from window to validator.

            The default implementation returns False, indicating that an error
            occurred.  We simply return True, as we don't do any data transfer.
        """
        return True # Prevent wx.Dialog from complaining.

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

class ExceptionHandler:
    """ A simple error-handling class to write exceptions to a text file.

        Under MS Windows, the standard DOS console window doesn't scroll and
        closes as soon as the application exits, making it hard to find and
        view Python exceptions.  This utility class allows you to handle Python
        exceptions in a more friendly manner.
    """

    def __init__(self):
        """ Standard constructor.
        """
        self._buff = ""
        if os.path.exists("errors.txt"):
            os.remove("errors.txt") # Delete previous error log, if any.


    def write(self, s):
        """ Write the given error message to a text file.

            Note that if the error message doesn't end in a carriage return, we
            have to buffer up the inputs until a carriage return is received.
        """
        if (s[-1] != "\n") and (s[-1] != "\r"):
            self._buff = self._buff + s
            return

        try:
            s = self._buff + s
            self._buff = ""

            f = open("errors.txt", "a")
            f.write(s)
            f.close()

            if s[:9] == "Traceback":
                # Tell the user than an exception occurred.
                wx.MessageBox("An internal error has occurred.\nPlease " + \
                             "refer to the 'errors.txt' file for details.",
                             "Error", wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION)


        except:
            pass # Don't recursively crash on errors.

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

class SketchApp(wx.App):
    """ The main pySketch application object.
    """
    def OnInit(self):
        """ Initialise the application.
        """
        global _docList
        _docList = []

        if len(sys.argv) == 1:
            # No file name was specified on the command line -> start with a
            # blank document.
            frame = DrawingFrame(None, -1, "Untitled")
            frame.Centre()
            frame.Show(True)
            _docList.append(frame)
        else:
            # Load the file(s) specified on the command line.
            for arg in sys.argv[1:]:
                fileName = os.path.join(os.getcwd(), arg)
                if os.path.isfile(fileName):
                    frame = DrawingFrame(None, -1,
                                         os.path.basename(fileName),
                                         fileName=fileName)
                    frame.Show(True)
                    _docList.append(frame)

        return True

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

def main():
    """ Start up the pySketch application.
    """
    global _app

    # Redirect python exceptions to a log file.

    sys.stderr = ExceptionHandler()

    # Create and start the pySketch application.

    _app = SketchApp(0)
    _app.MainLoop()


if __name__ == "__main__":
    main()