labelbook.py
110 KB
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
# --------------------------------------------------------------------------- #
# LABELBOOK And FLATIMAGEBOOK Widgets wxPython IMPLEMENTATION
#
# Original C++ Code From Eran, embedded in the FlatMenu source code
#
#
# License: wxWidgets license
#
#
# Python Code By:
#
# Andrea Gavana, @ 03 Nov 2006
# Latest Revision: 22 Jan 2013, 21.00 GMT
#
#
# For All Kind Of Problems, Requests Of Enhancements And Bug Reports, Please
# Write To Me At:
#
# andrea.gavana@gmail.com
# andrea.gavana@maerskoil.com
#
# Or, Obviously, To The wxPython Mailing List!!!
#
# TODO:
# LabelBook - Support IMB_SHOW_ONLY_IMAGES
# LabelBook - An option to only draw the border
# between the controls and the pages so the background
# colour can flow into the window background
#
#
#
# End Of Comments
# --------------------------------------------------------------------------- #
"""
:class:`LabelBook` and :class:`FlatImageBook` are a quasi-full generic and owner-drawn
implementations of :class:`Notebook`.
Description
===========
:class:`LabelBook` and :class:`FlatImageBook` are quasi-full implementations of the :class:`Notebook`,
and designed to be a drop-in replacement for :class:`Notebook`. The API functions are
similar so one can expect the function to behave in the same way.
:class:`LabelBook` anf :class:`FlatImageBook` share their appearance with :class:`Toolbook` and
:class:`Listbook`, while having more options for custom drawings, label positioning,
mouse pointing and so on. Moreover, they retain also some visual characteristics
of the Outlook address book.
Some features:
- They are generic controls;
- Supports for left, right, top (:class:`FlatImageBook` only), bottom (:class:`FlatImageBook`
only) book styles;
- Possibility to draw images only, text only or both (:class:`FlatImageBook` only);
- Support for a "pin-button", that allows the user to shrink/expand the book
tab area;
- Shadows behind tabs (:class:`LabelBook` only);
- Gradient shading of the tab area (:class:`LabelBook` only);
- Web-like mouse pointing on tabs style (:class:`LabelBook` only);
- Many customizable colours (tab area, active tab text, tab borders, active
tab, highlight) - :class:`LabelBook` only.
And much more. See the demo for a quasi-complete review of all the functionalities
of :class:`LabelBook` and :class:`FlatImageBook`.
Usage
=====
Usage example::
import wx
import wx.lib.agw.labelbook as LB
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "LabelBook Demo")
# Possible values for Tab placement are INB_TOP, INB_BOTTOM, INB_RIGHT, INB_LEFT
notebook = LB.LabelBook(self, -1, agwStyle=LB.INB_FIT_LABELTEXT|LB.INB_LEFT|LB.INB_DRAW_SHADOW|LB.INB_GRADIENT_BACKGROUND)
pane1 = wx.Panel(notebook)
pane2 = wx.Panel(notebook)
imagelist = wx.ImageList(32, 32)
imagelist.Add(wx.Bitmap("my_bitmap.png", wx.BITMAP_TYPE_PNG))
notebook.AssignImageList(imagelist)
notebook.AddPage(pane_1, "Tab1", 1, 0)
notebook.AddPage(pane_2, "Tab2", 0, 0)
# our normal wxApp-derived class, as usual
app = wx.App(0)
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
Supported Platforms
===================
:class:`LabelBook` and :class:`FlatImageBook` have been tested on the following platforms:
* Windows (Windows XP);
* Linux Ubuntu (Dapper 6.06)
Window Styles
=============
This class supports the following window styles:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
Events Processing
=================
This class processes the following events:
=================================== ==================================================
Event Name Description
=================================== ==================================================
``EVT_IMAGENOTEBOOK_PAGE_CHANGED`` Notify client objects when the active page in :class:`FlatImageBook` or :class:`LabelBook` has changed.
``EVT_IMAGENOTEBOOK_PAGE_CHANGING`` Notify client objects when the active page in :class:`FlatImageBook` or :class:`LabelBook` is about to change.
``EVT_IMAGENOTEBOOK_PAGE_CLOSED`` Notify client objects when a page in :class:`FlatImageBook` or :class:`LabelBook` has been closed.
``EVT_IMAGENOTEBOOK_PAGE_CLOSING`` Notify client objects when a page in :class:`FlatImageBook` or :class:`LabelBook` is closing.
=================================== ==================================================
TODOs
=====
- :class:`LabelBook`: support ``IMB_SHOW_ONLY_IMAGES``;
- :class:`LabelBook`: an option to only draw the border between the controls and the pages so the background
colour can flow into the window background.
License And Version
===================
:class:`LabelBook` and :class:`FlatImageBook` are distributed under the wxPython license.
Latest Revision: Andrea Gavana @ 22 Jan 2013, 21.00 GMT
Version 0.6.
"""
__docformat__ = "epytext"
__version__ = "0.6"
#----------------------------------------------------------------------
# Beginning Of IMAGENOTEBOOK wxPython Code
#----------------------------------------------------------------------
import wx
from artmanager import ArtManager, DCSaver
from fmresources import *
# Check for the new method in 2.7 (not present in 2.6.3.3)
if wx.VERSION_STRING < "2.7":
wx.Rect.Contains = lambda self, point: wx.Rect.Inside(self, point)
# FlatImageBook and LabelBook styles
INB_BOTTOM = 1
""" Place labels below the page area. Available only for :class:`FlatImageBook`."""
INB_LEFT = 2
""" Place labels on the left side. Available only for :class:`FlatImageBook`."""
INB_RIGHT = 4
""" Place labels on the right side. """
INB_TOP = 8
""" Place labels above the page area. """
INB_BORDER = 16
""" Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. """
INB_SHOW_ONLY_TEXT = 32
""" Shows only text labels and no images. Available only for :class:`LabelBook`."""
INB_SHOW_ONLY_IMAGES = 64
""" Shows only tab images and no label texts. Available only for :class:`LabelBook`."""
INB_FIT_BUTTON = 128
""" Displays a pin button to show/hide the book control. """
INB_DRAW_SHADOW = 256
""" Draw shadows below the book tabs. Available only for :class:`LabelBook`."""
INB_USE_PIN_BUTTON = 512
""" Displays a pin button to show/hide the book control. """
INB_GRADIENT_BACKGROUND = 1024
""" Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`."""
INB_WEB_HILITE = 2048
""" On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`."""
INB_NO_RESIZE = 4096
""" Don't allow resizing of the tab area. """
INB_FIT_LABELTEXT = 8192
""" Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. """
INB_BOLD_TAB_SELECTION = 16384
""" Show the selected tab text using a bold font. """
wxEVT_IMAGENOTEBOOK_PAGE_CHANGED = wx.wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
wxEVT_IMAGENOTEBOOK_PAGE_CHANGING = wx.wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
wxEVT_IMAGENOTEBOOK_PAGE_CLOSING = wx.NewEventType()
wxEVT_IMAGENOTEBOOK_PAGE_CLOSED = wx.NewEventType()
#-----------------------------------#
# ImageNotebookEvent
#-----------------------------------#
EVT_IMAGENOTEBOOK_PAGE_CHANGED = wx.EVT_NOTEBOOK_PAGE_CHANGED
""" Notify client objects when the active page in :class:`FlatImageBook` or :class:`LabelBook` has changed. """
EVT_IMAGENOTEBOOK_PAGE_CHANGING = wx.EVT_NOTEBOOK_PAGE_CHANGING
""" Notify client objects when the active page in :class:`FlatImageBook` or :class:`LabelBook` is about to change. """
EVT_IMAGENOTEBOOK_PAGE_CLOSING = wx.PyEventBinder(wxEVT_IMAGENOTEBOOK_PAGE_CLOSING, 1)
""" Notify client objects when a page in :class:`FlatImageBook` or :class:`LabelBook` is closing. """
EVT_IMAGENOTEBOOK_PAGE_CLOSED = wx.PyEventBinder(wxEVT_IMAGENOTEBOOK_PAGE_CLOSED, 1)
""" Notify client objects when a page in :class:`FlatImageBook` or :class:`LabelBook` has been closed. """
# ---------------------------------------------------------------------------- #
# Class ImageNotebookEvent
# ---------------------------------------------------------------------------- #
class ImageNotebookEvent(wx.PyCommandEvent):
"""
This events will be sent when a ``EVT_IMAGENOTEBOOK_PAGE_CHANGED``,
``EVT_IMAGENOTEBOOK_PAGE_CHANGING``, ``EVT_IMAGENOTEBOOK_PAGE_CLOSING``,
``EVT_IMAGENOTEBOOK_PAGE_CLOSED`` is mapped in the parent.
"""
def __init__(self, eventType, eventId=1, sel=-1, oldsel=-1):
"""
Default class constructor.
:param `eventType`: the event type;
:param `eventId`: the event identifier;
:param `sel`: the current selection;
:param `oldsel`: the old selection.
"""
wx.PyCommandEvent.__init__(self, eventType, eventId)
self._eventType = eventType
self._sel = sel
self._oldsel = oldsel
self._allowed = True
def SetSelection(self, s):
"""
Sets the event selection.
:param `s`: an integer specifying the new selection.
"""
self._sel = s
def SetOldSelection(self, s):
"""
Sets the event old selection.
:param `s`: an integer specifying the old selection.
"""
self._oldsel = s
def GetSelection(self):
""" Returns the event selection. """
return self._sel
def GetOldSelection(self):
""" Returns the old event selection. """
return self._oldsel
def Veto(self):
"""
Prevents the change announced by this event from happening.
:note: It is in general a good idea to notify the user about the reasons
for vetoing the change because otherwise the applications behaviour (which
just refuses to do what the user wants) might be quite surprising.
"""
self._allowed = False
def Allow(self):
"""
This is the opposite of :meth:`~ImageNotebookEvent.Veto`: it explicitly allows the event to be processed.
For most events it is not necessary to call this method as the events are
allowed anyhow but some are forbidden by default (this will be mentioned
in the corresponding event description).
"""
self._allowed = True
def IsAllowed(self):
"""
Returns ``True`` if the change is allowed (:meth:`~ImageNotebookEvent.Veto` hasn't been called) or
``False`` otherwise (if it was).
"""
return self._allowed
# ---------------------------------------------------------------------------- #
# Class ImageInfo
# ---------------------------------------------------------------------------- #
class ImageInfo(object):
"""
This class holds all the information (caption, image, etc...) belonging to a
single tab in :class:`LabelBook`.
"""
def __init__(self, strCaption="", imageIndex=-1, enabled=True):
"""
Default class constructor.
:param `strCaption`: the tab caption;
:param `imageIndex`: the tab image index based on the assigned (set)
:class:`ImageList` (if any);
:param `enabled`: sets the tab as enabled or disabled.
"""
self._pos = wx.Point()
self._size = wx.Size()
self._strCaption = strCaption
self._ImageIndex = imageIndex
self._captionRect = wx.Rect()
self._bEnabled = enabled
def SetCaption(self, value):
"""
Sets the tab caption.
:param `value`: the new tab caption.
"""
self._strCaption = value
def GetCaption(self):
""" Returns the tab caption. """
return self._strCaption
def SetPosition(self, value):
"""
Sets the tab position.
:param `value`: the new tab position, an instance of :class:`Point`.
"""
self._pos = value
def GetPosition(self):
""" Returns the tab position. """
return self._pos
def SetSize(self, value):
"""
Sets the tab size.
:param `value`: the new tab size, an instance of :class:`Size`.
"""
self._size = value
def GetSize(self):
""" Returns the tab size. """
return self._size
def SetImageIndex(self, value):
"""
Sets the tab image index.
:param `value`: an index into the image list..
"""
self._ImageIndex = value
def GetImageIndex(self):
""" Returns the tab image index. """
return self._ImageIndex
def SetTextRect(self, rect):
"""
Sets the client rectangle available for the tab text.
:param `rect`: the tab text client rectangle, an instance of :class:`Rect`.
"""
self._captionRect = rect
def GetTextRect(self):
""" Returns the client rectangle available for the tab text. """
return self._captionRect
def GetEnabled(self):
""" Returns whether the tab is enabled or not. """
return self._bEnabled
def EnableTab(self, enabled):
"""
Sets the tab enabled or disabled.
:param `enabled`: ``True`` to enable a tab, ``False`` to disable it.
"""
self._bEnabled = enabled
# ---------------------------------------------------------------------------- #
# Class ImageContainerBase
# ---------------------------------------------------------------------------- #
class ImageContainerBase(wx.Panel):
"""
Base class for :class:`FlatImageBook` image container.
"""
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, agwStyle=0, name="ImageContainerBase"):
"""
Default class constructor.
:param `parent`: parent window. Must not be ``None``;
:param `id`: window identifier. A value of -1 indicates a default value;
:param `pos`: the control position. A value of (-1, -1) indicates a default position,
chosen by either the windowing system or wxPython, depending on platform;
:param `size`: the control size. A value of (-1, -1) indicates a default size,
chosen by either the windowing system or wxPython, depending on platform;
:param `style`: the underlying :class:`Panel` window style;
:param `agwStyle`: the AGW-specific window style. This can be a combination of the
following bits:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
:param `name`: the window name.
"""
self._nIndex = -1
self._nImgSize = 16
self._ImageList = None
self._nHoveredImgIdx = -1
self._bCollapsed = False
self._tabAreaSize = (-1, -1)
self._nPinButtonStatus = INB_PIN_NONE
self._pagesInfoVec = []
self._pinBtnRect = wx.Rect()
wx.Panel.__init__(self, parent, id, pos, size, style | wx.NO_BORDER | wx.NO_FULL_REPAINT_ON_RESIZE, name)
def HasAGWFlag(self, flag):
"""
Tests for existance of flag in the style.
:param `flag`: a window style. This can be a combination of the following bits:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
"""
style = self.GetParent().GetAGWWindowStyleFlag()
res = (style & flag and [True] or [False])[0]
return res
def ClearFlag(self, flag):
"""
Removes flag from the style.
:param `flag`: a window style flag.
:see: :meth:`~ImageContainerBase.HasAGWFlag` for a list of possible window style flags.
"""
parent = self.GetParent()
agwStyle = parent.GetAGWWindowStyleFlag()
agwStyle &= ~(flag)
parent.SetAGWWindowStyleFlag(agwStyle)
def AssignImageList(self, imglist):
"""
Assigns an image list to the :class:`ImageContainerBase`.
:param `imglist`: an instance of :class:`ImageList`.
"""
if imglist and imglist.GetImageCount() != 0:
self._nImgSize = imglist.GetBitmap(0).GetHeight()
self._ImageList = imglist
parent = self.GetParent()
agwStyle = parent.GetAGWWindowStyleFlag()
parent.SetAGWWindowStyleFlag(agwStyle)
def GetImageList(self):
""" Return the image list for :class:`ImageContainerBase`. """
return self._ImageList
def GetImageSize(self):
""" Returns the image size inside the :class:`ImageContainerBase` image list. """
return self._nImgSize
def FixTextSize(self, dc, text, maxWidth):
"""
Fixes the text, to fit `maxWidth` value. If the text length exceeds
`maxWidth` value this function truncates it and appends two dots at
the end. ("Long Long Long Text" might become "Long Long...").
:param `dc`: an instance of :class:`DC`;
:param `text`: the text to fix/truncate;
:param `maxWidth`: the maximum allowed width for the text, in pixels.
"""
return ArtManager.Get().TruncateText(dc, text, maxWidth)
def CanDoBottomStyle(self):
"""
Allows the parent to examine the children type. Some implementation
(such as :class:`LabelBook`), does not support top/bottom images, only left/right.
"""
return False
def AddPage(self, caption, selected=False, imgIdx=-1):
"""
Adds a page to the container.
:param `caption`: specifies the text for the new tab;
:param `selected`: specifies whether the page should be selected;
:param `imgIdx`: specifies the optional image index for the new tab.
"""
self._pagesInfoVec.append(ImageInfo(caption, imgIdx))
if selected or len(self._pagesInfoVec) == 1:
self._nIndex = len(self._pagesInfoVec)-1
self.Refresh()
def InsertPage(self, page_idx, caption, selected=False, imgIdx=-1):
"""
Inserts a page into the container at the specified position.
:param `page_idx`: specifies the position for the new tab;
:param `caption`: specifies the text for the new tab;
:param `selected`: specifies whether the page should be selected;
:param `imgIdx`: specifies the optional image index for the new tab.
"""
self._pagesInfoVec.insert(page_idx, ImageInfo(caption, imgIdx))
if selected or len(self._pagesInfoVec) == 1:
self._nIndex = len(self._pagesInfoVec)-1
self.Refresh()
def SetPageImage(self, page, imgIdx):
"""
Sets the image for the given page.
:param `page`: the index of the tab;
:param `imgIdx`: specifies the optional image index for the tab.
"""
imgInfo = self._pagesInfoVec[page]
imgInfo.SetImageIndex(imgIdx)
def SetPageText(self, page, text):
"""
Sets the tab caption for the given page.
:param `page`: the index of the tab;
:param `text`: the new tab caption.
"""
imgInfo = self._pagesInfoVec[page]
imgInfo.SetCaption(text)
def GetPageImage(self, page):
"""
Returns the image index for the given page.
:param `page`: the index of the tab.
"""
imgInfo = self._pagesInfoVec[page]
return imgInfo.GetImageIndex()
def GetPageText(self, page):
"""
Returns the tab caption for the given page.
:param `page`: the index of the tab.
"""
imgInfo = self._pagesInfoVec[page]
return imgInfo.GetCaption()
def GetEnabled(self, page):
"""
Returns whether a tab is enabled or not.
:param `page`: an integer specifying the page index.
"""
if page >= len(self._pagesInfoVec):
return True # Adding a page - enabled by default
imgInfo = self._pagesInfoVec[page]
return imgInfo.GetEnabled()
def EnableTab(self, page, enabled=True):
"""
Enables or disables a tab.
:param `page`: an integer specifying the page index;
:param `enabled`: ``True`` to enable a tab, ``False`` to disable it.
"""
if page >= len(self._pagesInfoVec):
return
imgInfo = self._pagesInfoVec[page]
imgInfo.EnableTab(enabled)
def ClearAll(self):
""" Deletes all the pages in the container. """
self._pagesInfoVec = []
self._nIndex = wx.NOT_FOUND
def DoDeletePage(self, page):
"""
Does the actual page deletion.
:param `page`: the index of the tab.
"""
# Remove the page from the vector
book = self.GetParent()
self._pagesInfoVec.pop(page)
if self._nIndex >= page:
self._nIndex = self._nIndex - 1
# The delete page was the last first on the array,
# but the book still has more pages, so we set the
# active page to be the first one (0)
if self._nIndex < 0 and len(self._pagesInfoVec) > 0:
self._nIndex = 0
# Refresh the tabs
if self._nIndex >= 0:
book._bForceSelection = True
book.SetSelection(self._nIndex)
book._bForceSelection = False
if not self._pagesInfoVec:
# Erase the page container drawings
dc = wx.ClientDC(self)
dc.Clear()
def OnSize(self, event):
"""
Handles the ``wx.EVT_SIZE`` event for :class:`ImageContainerBase`.
:param `event`: a :class:`SizeEvent` event to be processed.
"""
self.Refresh() # Call on paint
event.Skip()
def OnEraseBackground(self, event):
"""
Handles the ``wx.EVT_ERASE_BACKGROUND`` event for :class:`ImageContainerBase`.
:param `event`: a :class:`EraseEvent` event to be processed.
:note: This method is intentionally empty to reduce flicker.
"""
pass
def HitTest(self, pt):
"""
Returns the index of the tab at the specified position or ``wx.NOT_FOUND``
if ``None``, plus the flag style of :meth:`~ImageContainerBase.HitTest`.
:param `pt`: an instance of :class:`Point`, to test for hits.
:return: The index of the tab at the specified position plus the hit test
flag, which can be one of the following bits:
====================== ======= ================================
HitTest Flags Value Description
====================== ======= ================================
``IMG_OVER_IMG`` 0 The mouse is over the tab icon
``IMG_OVER_PIN`` 1 The mouse is over the pin button
``IMG_OVER_EW_BORDER`` 2 The mouse is over the east-west book border
``IMG_NONE`` 3 Nowhere
====================== ======= ================================
"""
style = self.GetParent().GetAGWWindowStyleFlag()
if style & INB_USE_PIN_BUTTON:
if self._pinBtnRect.Contains(pt):
return -1, IMG_OVER_PIN
for i in xrange(len(self._pagesInfoVec)):
if self._pagesInfoVec[i].GetPosition() == wx.Point(-1, -1):
break
# For Web Hover style, we test the TextRect
if not self.HasAGWFlag(INB_WEB_HILITE):
buttonRect = wx.RectPS(self._pagesInfoVec[i].GetPosition(), self._pagesInfoVec[i].GetSize())
else:
buttonRect = self._pagesInfoVec[i].GetTextRect()
if buttonRect.Contains(pt):
return i, IMG_OVER_IMG
if self.PointOnSash(pt):
return -1, IMG_OVER_EW_BORDER
else:
return -1, IMG_NONE
def PointOnSash(self, pt):
"""
Tests whether pt is located on the sash.
:param `pt`: an instance of :class:`Point`, to test for hits.
"""
# Check if we are on a the sash border
cltRect = self.GetClientRect()
if self.HasAGWFlag(INB_LEFT) or self.HasAGWFlag(INB_TOP):
if pt.x > cltRect.x + cltRect.width - 4:
return True
else:
if pt.x < 4:
return True
return False
def OnMouseLeftDown(self, event):
"""
Handles the ``wx.EVT_LEFT_DOWN`` event for :class:`ImageContainerBase`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
newSelection = -1
event.Skip()
# Support for collapse/expand
style = self.GetParent().GetAGWWindowStyleFlag()
if style & INB_USE_PIN_BUTTON:
if self._pinBtnRect.Contains(event.GetPosition()):
self._nPinButtonStatus = INB_PIN_PRESSED
dc = wx.ClientDC(self)
self.DrawPin(dc, self._pinBtnRect, not self._bCollapsed)
return
# Incase panel is collapsed, there is nothing
# to check
if self._bCollapsed:
return
tabIdx, where = self.HitTest(event.GetPosition())
if where == IMG_OVER_IMG:
self._nHoveredImgIdx = -1
if tabIdx == -1:
return
self.GetParent().SetSelection(tabIdx)
def OnMouseLeaveWindow(self, event):
"""
Handles the ``wx.EVT_LEAVE_WINDOW`` event for :class:`ImageContainerBase`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
bRepaint = self._nHoveredImgIdx != -1
self._nHoveredImgIdx = -1
# Make sure the pin button status is NONE
# incase we were in pin button style
style = self.GetParent().GetAGWWindowStyleFlag()
if style & INB_USE_PIN_BUTTON:
self._nPinButtonStatus = INB_PIN_NONE
dc = wx.ClientDC(self)
self.DrawPin(dc, self._pinBtnRect, not self._bCollapsed)
# Restore cursor
wx.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
if bRepaint:
self.Refresh()
def OnMouseLeftUp(self, event):
"""
Handles the ``wx.EVT_LEFT_UP`` event for :class:`ImageContainerBase`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
style = self.GetParent().GetAGWWindowStyleFlag()
if style & INB_USE_PIN_BUTTON:
bIsLabelContainer = not self.CanDoBottomStyle()
if self._pinBtnRect.Contains(event.GetPosition()):
self._nPinButtonStatus = INB_PIN_NONE
self._bCollapsed = not self._bCollapsed
if self._bCollapsed:
# Save the current tab area width
self._tabAreaSize = self.GetSize()
if bIsLabelContainer:
self.SetSizeHints(20, self._tabAreaSize.y)
else:
if style & INB_BOTTOM or style & INB_TOP:
self.SetSizeHints(self._tabAreaSize.x, 20)
else:
self.SetSizeHints(20, self._tabAreaSize.y)
else:
if bIsLabelContainer:
self.SetSizeHints(self._tabAreaSize.x, -1)
else:
# Restore the tab area size
if style & INB_BOTTOM or style & INB_TOP:
self.SetSizeHints(-1, self._tabAreaSize.y)
else:
self.SetSizeHints(self._tabAreaSize.x, -1)
self.GetParent().GetSizer().Layout()
self.Refresh()
return
def OnMouseMove(self, event):
"""
Handles the ``wx.EVT_MOTION`` event for :class:`ImageContainerBase`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
style = self.GetParent().GetAGWWindowStyleFlag()
if style & INB_USE_PIN_BUTTON:
# Check to see if we are in the pin button rect
if not self._pinBtnRect.Contains(event.GetPosition()) and self._nPinButtonStatus == INB_PIN_PRESSED:
self._nPinButtonStatus = INB_PIN_NONE
dc = wx.ClientDC(self)
self.DrawPin(dc, self._pinBtnRect, not self._bCollapsed)
imgIdx, where = self.HitTest(event.GetPosition())
# Allow hovering unless over current tab or tab is disabled
self._nHoveredImgIdx = -1
if imgIdx < len(self._pagesInfoVec) and self.GetEnabled(imgIdx) and imgIdx != self._nIndex:
self._nHoveredImgIdx = imgIdx
if not self._bCollapsed:
if self._nHoveredImgIdx >= 0 and self.HasAGWFlag(INB_WEB_HILITE):
# Change the cursor to be Hand if we have the Web hover style set
wx.SetCursor(wx.StockCursor(wx.CURSOR_HAND))
elif not self.PointOnSash(event.GetPosition()):
# Restore the cursor if we are not currently hovering the sash
wx.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
self.Refresh()
def DrawPin(self, dc, rect, downPin):
"""
Draw a pin button, that allows collapsing of the image panel.
:param `dc`: an instance of :class:`DC`;
:param `rect`: the pin button client rectangle;
:param `downPin`: ``True`` if the pin button is facing downwards, ``False``
if it is facing leftwards.
"""
# Set the bitmap according to the button status
if downPin:
pinBmp = wx.BitmapFromXPMData(pin_down_xpm)
else:
pinBmp = wx.BitmapFromXPMData(pin_left_xpm)
xx = rect.x + 2
if self._nPinButtonStatus in [INB_PIN_HOVER, INB_PIN_NONE]:
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetPen(wx.BLACK_PEN)
dc.DrawRectangle(xx, rect.y, 16, 16)
# Draw upper and left border with grey colour
dc.SetPen(wx.WHITE_PEN)
dc.DrawLine(xx, rect.y, xx + 16, rect.y)
dc.DrawLine(xx, rect.y, xx, rect.y + 16)
elif self._nPinButtonStatus == INB_PIN_PRESSED:
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetPen(wx.Pen(wx.NamedColour("LIGHT GREY")))
dc.DrawRectangle(xx, rect.y, 16, 16)
# Draw upper and left border with grey colour
dc.SetPen(wx.BLACK_PEN)
dc.DrawLine(xx, rect.y, xx + 16, rect.y)
dc.DrawLine(xx, rect.y, xx, rect.y + 16)
# Set the masking
pinBmp.SetMask(wx.Mask(pinBmp, wx.WHITE))
# Draw the new bitmap
dc.DrawBitmap(pinBmp, xx, rect.y, True)
# Save the pin rect
self._pinBtnRect = rect
# ---------------------------------------------------------------------------- #
# Class ImageContainer
# ---------------------------------------------------------------------------- #
class ImageContainer(ImageContainerBase):
"""
Base class for :class:`FlatImageBook` image container.
"""
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, agwStyle=0, name="ImageContainer"):
"""
Default class constructor.
:param `parent`: parent window. Must not be ``None``;
:param `id`: window identifier. A value of -1 indicates a default value;
:param `pos`: the control position. A value of (-1, -1) indicates a default position,
chosen by either the windowing system or wxPython, depending on platform;
:param `size`: the control size. A value of (-1, -1) indicates a default size,
chosen by either the windowing system or wxPython, depending on platform;
:param `style`: the underlying :class:`Panel` window style;
:param `agwStyle`: the AGW-specific window style. This can be a combination of the
following bits:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
:param `name`: the window name.
"""
ImageContainerBase.__init__(self, parent, id, pos, size, style, agwStyle, name)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_MOTION, self.OnMouseMove)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeaveWindow)
def OnSize(self, event):
"""
Handles the ``wx.EVT_SIZE`` event for :class:`ImageContainer`.
:param `event`: a :class:`SizeEvent` event to be processed.
"""
ImageContainerBase.OnSize(self, event)
event.Skip()
def OnMouseLeftDown(self, event):
"""
Handles the ``wx.EVT_LEFT_DOWN`` event for :class:`ImageContainer`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
ImageContainerBase.OnMouseLeftDown(self, event)
event.Skip()
def OnMouseLeftUp(self, event):
"""
Handles the ``wx.EVT_LEFT_UP`` event for :class:`ImageContainer`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
ImageContainerBase.OnMouseLeftUp(self, event)
event.Skip()
def OnEraseBackground(self, event):
"""
Handles the ``wx.EVT_ERASE_BACKGROUND`` event for :class:`ImageContainer`.
:param `event`: a :class:`EraseEvent` event to be processed.
"""
ImageContainerBase.OnEraseBackground(self, event)
def OnMouseMove(self, event):
"""
Handles the ``wx.EVT_MOTION`` event for :class:`ImageContainer`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
ImageContainerBase.OnMouseMove(self, event)
event.Skip()
def OnMouseLeaveWindow(self, event):
"""
Handles the ``wx.EVT_LEAVE_WINDOW`` event for :class:`ImageContainer`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
ImageContainerBase.OnMouseLeaveWindow(self, event)
event.Skip()
def CanDoBottomStyle(self):
"""
Allows the parent to examine the children type. Some implementation
(such as :class:`LabelBook`), does not support top/bottom images, only left/right.
"""
return True
def OnPaint(self, event):
"""
Handles the ``wx.EVT_PAINT`` event for :class:`ImageContainer`.
:param `event`: a :class:`PaintEvent` event to be processed.
"""
dc = wx.BufferedPaintDC(self)
style = self.GetParent().GetAGWWindowStyleFlag()
backBrush = wx.WHITE_BRUSH
if style & INB_BORDER:
borderPen = wx.Pen(wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DSHADOW))
else:
borderPen = wx.TRANSPARENT_PEN
size = self.GetSize()
# Background
dc.SetBrush(backBrush)
borderPen.SetWidth(1)
dc.SetPen(borderPen)
dc.DrawRectangle(0, 0, size.x, size.y)
bUsePin = (style & INB_USE_PIN_BUTTON and [True] or [False])[0]
if bUsePin:
# Draw the pin button
clientRect = self.GetClientRect()
pinRect = wx.Rect(clientRect.GetX() + clientRect.GetWidth() - 20, 2, 20, 20)
self.DrawPin(dc, pinRect, not self._bCollapsed)
if self._bCollapsed:
return
borderPen = wx.BLACK_PEN
borderPen.SetWidth(1)
dc.SetPen(borderPen)
dc.DrawLine(0, size.y, size.x, size.y)
dc.DrawPoint(0, size.y)
clientSize = 0
bUseYcoord = (style & INB_RIGHT or style & INB_LEFT)
if bUseYcoord:
clientSize = size.GetHeight()
else:
clientSize = size.GetWidth()
# We reserver 20 pixels for the 'pin' button
# The drawing of the images start position. This is
# depenedent of the style, especially when Pin button
# style is requested
if bUsePin:
if style & INB_TOP or style & INB_BOTTOM:
pos = (style & INB_BORDER and [0] or [1])[0]
else:
pos = (style & INB_BORDER and [20] or [21])[0]
else:
pos = (style & INB_BORDER and [0] or [1])[0]
nPadding = 4 # Pad text with 2 pixels on the left and right
nTextPaddingLeft = 2
count = 0
normalFont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
boldFont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
boldFont.SetWeight(wx.BOLD)
for i in xrange(len(self._pagesInfoVec)):
count = count + 1
# incase the 'fit button' style is applied, we set the rectangle width to the
# text width plus padding
# Incase the style IS applied, but the style is either LEFT or RIGHT
# we ignore it
dc.SetFont(normalFont)
if style & INB_BOLD_TAB_SELECTION and self._nIndex == i:
dc.SetFont(boldFont)
textWidth, textHeight = dc.GetTextExtent(self._pagesInfoVec[i].GetCaption())
# Default values for the surronounding rectangle
# around a button
rectWidth = self._nImgSize * 2 # To avoid the recangle to 'touch' the borders
rectHeight = self._nImgSize * 2
# Incase the style requires non-fixed button (fit to text)
# recalc the rectangle width
if style & INB_FIT_BUTTON and \
not ((style & INB_LEFT) or (style & INB_RIGHT)) and \
not self._pagesInfoVec[i].GetCaption() == "" and \
not (style & INB_SHOW_ONLY_IMAGES):
rectWidth = ((textWidth + nPadding * 2) > rectWidth and [nPadding * 2 + textWidth] or [rectWidth])[0]
# Make the width an even number
if rectWidth % 2 != 0:
rectWidth += 1
# Check that we have enough space to draw the button
# If Pin button is used, consider its space as well (applicable for top/botton style)
# since in the left/right, its size is already considered in 'pos'
pinBtnSize = (bUsePin and [20] or [0])[0]
if pos + rectWidth + pinBtnSize > clientSize:
break
# Calculate the button rectangle
modRectWidth = ((style & INB_LEFT or style & INB_RIGHT) and [rectWidth - 2] or [rectWidth])[0]
modRectHeight = ((style & INB_LEFT or style & INB_RIGHT) and [rectHeight] or [rectHeight - 2])[0]
if bUseYcoord:
buttonRect = wx.Rect(1, pos, modRectWidth, modRectHeight)
else:
buttonRect = wx.Rect(pos , 1, modRectWidth, modRectHeight)
# Check if we need to draw a rectangle around the button
if self._nIndex == i:
# Set the colours
penColour = wx.SystemSettings_GetColour(wx.SYS_COLOUR_ACTIVECAPTION)
brushColour = ArtManager.Get().LightColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_ACTIVECAPTION), 75)
dc.SetPen(wx.Pen(penColour))
dc.SetBrush(wx.Brush(brushColour))
# Fix the surrounding of the rect if border is set
if style & INB_BORDER:
if style & INB_TOP or style & INB_BOTTOM:
buttonRect = wx.Rect(buttonRect.x + 1, buttonRect.y, buttonRect.width - 1, buttonRect.height)
else:
buttonRect = wx.Rect(buttonRect.x, buttonRect.y + 1, buttonRect.width, buttonRect.height - 1)
dc.DrawRectangleRect(buttonRect)
if self._nHoveredImgIdx == i:
# Set the colours
penColour = wx.SystemSettings_GetColour(wx.SYS_COLOUR_ACTIVECAPTION)
brushColour = ArtManager.Get().LightColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_ACTIVECAPTION), 90)
dc.SetPen(wx.Pen(penColour))
dc.SetBrush(wx.Brush(brushColour))
# Fix the surrounding of the rect if border is set
if style & INB_BORDER:
if style & INB_TOP or style & INB_BOTTOM:
buttonRect = wx.Rect(buttonRect.x + 1, buttonRect.y, buttonRect.width - 1, buttonRect.height)
else:
buttonRect = wx.Rect(buttonRect.x, buttonRect.y + 1, buttonRect.width, buttonRect.height - 1)
dc.DrawRectangleRect(buttonRect)
if bUseYcoord:
rect = wx.Rect(0, pos, rectWidth, rectWidth)
else:
rect = wx.Rect(pos, 0, rectWidth, rectWidth)
# Incase user set both flags:
# INB_SHOW_ONLY_TEXT and INB_SHOW_ONLY_IMAGES
# We override them to display both
if style & INB_SHOW_ONLY_TEXT and style & INB_SHOW_ONLY_IMAGES:
style ^= INB_SHOW_ONLY_TEXT
style ^= INB_SHOW_ONLY_IMAGES
self.GetParent().SetAGWWindowStyleFlag(style)
# Draw the caption and text
imgTopPadding = 10
if not style & INB_SHOW_ONLY_TEXT and self._pagesInfoVec[i].GetImageIndex() != -1:
if bUseYcoord:
imgXcoord = self._nImgSize / 2
imgYcoord = (style & INB_SHOW_ONLY_IMAGES and [pos + self._nImgSize / 2] or [pos + imgTopPadding])[0]
else:
imgXcoord = pos + (rectWidth / 2) - (self._nImgSize / 2)
imgYcoord = (style & INB_SHOW_ONLY_IMAGES and [self._nImgSize / 2] or [imgTopPadding])[0]
self._ImageList.Draw(self._pagesInfoVec[i].GetImageIndex(), dc,
imgXcoord, imgYcoord,
wx.IMAGELIST_DRAW_TRANSPARENT, True)
# Draw the text
if not style & INB_SHOW_ONLY_IMAGES and not self._pagesInfoVec[i].GetCaption() == "":
if style & INB_BOLD_TAB_SELECTION and self._nIndex == i:
dc.SetFont(boldFont)
else:
dc.SetFont(normalFont)
# Check if the text can fit the size of the rectangle,
# if not truncate it
fixedText = self._pagesInfoVec[i].GetCaption()
if not style & INB_FIT_BUTTON or (style & INB_LEFT or (style & INB_RIGHT)):
fixedText = self.FixTextSize(dc, self._pagesInfoVec[i].GetCaption(), self._nImgSize *2 - 4)
# Update the length of the text
textWidth, textHeight = dc.GetTextExtent(fixedText)
if bUseYcoord:
textOffsetX = ((rectWidth - textWidth) / 2 )
textOffsetY = (not style & INB_SHOW_ONLY_TEXT and [pos + self._nImgSize + imgTopPadding + 3] or \
[pos + ((self._nImgSize * 2 - textHeight) / 2 )])[0]
else:
textOffsetX = (rectWidth - textWidth) / 2 + pos + nTextPaddingLeft
textOffsetY = (not style & INB_SHOW_ONLY_TEXT and [self._nImgSize + imgTopPadding + 3] or \
[((self._nImgSize * 2 - textHeight) / 2 )])[0]
dc.SetTextForeground(wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT))
dc.DrawText(fixedText, textOffsetX, textOffsetY)
# Update the page info
self._pagesInfoVec[i].SetPosition(buttonRect.GetPosition())
self._pagesInfoVec[i].SetSize(buttonRect.GetSize())
pos += rectWidth
# Update all buttons that can not fit into the screen as non-visible
for ii in xrange(count, len(self._pagesInfoVec)):
self._pagesInfoVec[ii].SetPosition(wx.Point(-1, -1))
# Draw the pin button
if bUsePin:
clientRect = self.GetClientRect()
pinRect = wx.Rect(clientRect.GetX() + clientRect.GetWidth() - 20, 2, 20, 20)
self.DrawPin(dc, pinRect, not self._bCollapsed)
# ---------------------------------------------------------------------------- #
# Class LabelContainer
# ---------------------------------------------------------------------------- #
class LabelContainer(ImageContainerBase):
""" Base class for :class:`LabelBook`. """
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, agwStyle=0, name="LabelContainer"):
"""
Default class constructor.
:param `parent`: parent window. Must not be ``None``;
:param `id`: window identifier. A value of -1 indicates a default value;
:param `pos`: the control position. A value of (-1, -1) indicates a default position,
chosen by either the windowing system or wxPython, depending on platform;
:param `size`: the control size. A value of (-1, -1) indicates a default size,
chosen by either the windowing system or wxPython, depending on platform;
:param `style`: the underlying :class:`Panel` window style;
:param `agwStyle`: the AGW-specific window style. This can be a combination of the
following bits:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
:param `name`: the window name.
"""
ImageContainerBase.__init__(self, parent, id, pos, size, style, agwStyle, name)
self._nTabAreaWidth = 100
self._oldCursor = wx.NullCursor
self._coloursMap = {}
self._skin = wx.NullBitmap
self._sashRect = wx.Rect()
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMouseMove)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeaveWindow)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnSize(self, event):
"""
Handles the ``wx.EVT_SIZE`` event for :class:`LabelContainer`.
:param `event`: a :class:`SizeEvent` event to be processed.
"""
ImageContainerBase.OnSize(self, event)
event.Skip()
def OnEraseBackground(self, event):
"""
Handles the ``wx.EVT_ERASE_BACKGROUND`` event for :class:`LabelContainer`.
:param `event`: a :class:`EraseEvent` event to be processed.
"""
ImageContainerBase.OnEraseBackground(self, event)
def GetTabAreaWidth(self):
""" Returns the width of the tab area. """
return self._nTabAreaWidth
def SetTabAreaWidth(self, width):
"""
Sets the width of the tab area.
:param `width`: the width of the tab area, in pixels.
"""
self._nTabAreaWidth = width
def CanDoBottomStyle(self):
"""
Allows the parent to examine the children type. Some implementation
(such as :class:`LabelBook`), does not support top/bottom images, only left/right.
"""
return False
def SetBackgroundBitmap(self, bmp):
"""
Sets the background bitmap for the control.
:param `bmp`: a valid :class:`Bitmap` object.
"""
self._skin = bmp
def OnPaint(self, event):
"""
Handles the ``wx.EVT_PAINT`` event for :class:`LabelContainer`.
:param `event`: a :class:`PaintEvent` event to be processed.
"""
style = self.GetParent().GetAGWWindowStyleFlag()
dc = wx.BufferedPaintDC(self)
backBrush = wx.Brush(self._coloursMap[INB_TAB_AREA_BACKGROUND_COLOUR])
if self.HasAGWFlag(INB_BORDER):
borderPen = wx.Pen(self._coloursMap[INB_TABS_BORDER_COLOUR])
else:
borderPen = wx.TRANSPARENT_PEN
size = self.GetSize()
# Set the pen & brush
dc.SetBrush(backBrush)
dc.SetPen(borderPen)
# Incase user set both flags, we override them to display both
# INB_SHOW_ONLY_TEXT and INB_SHOW_ONLY_IMAGES
if style & INB_SHOW_ONLY_TEXT and style & INB_SHOW_ONLY_IMAGES:
style ^= INB_SHOW_ONLY_TEXT
style ^= INB_SHOW_ONLY_IMAGES
self.GetParent().SetAGWWindowStyleFlag(style)
if self.HasAGWFlag(INB_GRADIENT_BACKGROUND) and not self._skin.Ok():
# Draw graident in the background area
startColour = self._coloursMap[INB_TAB_AREA_BACKGROUND_COLOUR]
endColour = ArtManager.Get().LightColour(self._coloursMap[INB_TAB_AREA_BACKGROUND_COLOUR], 50)
ArtManager.Get().PaintStraightGradientBox(dc, wx.Rect(0, 0, size.x / 2, size.y), startColour, endColour, False)
ArtManager.Get().PaintStraightGradientBox(dc, wx.Rect(size.x / 2, 0, size.x / 2, size.y), endColour, startColour, False)
else:
# Draw the border and background
if self._skin.Ok():
dc.SetBrush(wx.TRANSPARENT_BRUSH)
self.DrawBackgroundBitmap(dc)
dc.DrawRectangleRect(wx.Rect(0, 0, size.x, size.y))
# Draw border
if self.HasAGWFlag(INB_BORDER) and self.HasAGWFlag(INB_GRADIENT_BACKGROUND):
# Just draw the border with transparent brush
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.DrawRectangleRect(wx.Rect(0, 0, size.x, size.y))
bUsePin = (self.HasAGWFlag(INB_USE_PIN_BUTTON) and [True] or [False])[0]
if bUsePin:
# Draw the pin button
clientRect = self.GetClientRect()
pinRect = wx.Rect(clientRect.GetX() + clientRect.GetWidth() - 20, 2, 20, 20)
self.DrawPin(dc, pinRect, not self._bCollapsed)
if self._bCollapsed:
return
dc.SetPen(wx.BLACK_PEN)
self.SetSizeHints(self._nTabAreaWidth, -1)
# We reserve 20 pixels for the pin button
posy = 20
count = 0
for i in xrange(len(self._pagesInfoVec)):
count = count+1
# Default values for the surronounding rectangle
# around a button
rectWidth = self._nTabAreaWidth
if self.HasAGWFlag(INB_SHOW_ONLY_TEXT):
font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
font.SetPointSize(font.GetPointSize() * self.GetParent().GetFontSizeMultiple())
if self.GetParent().GetFontBold():
font.SetWeight(wx.FONTWEIGHT_BOLD)
elif self.HasAGWFlag(INB_BOLD_TAB_SELECTION) and self._nIndex == i:
font.SetWeight(wx.FONTWEIGHT_BOLD)
dc.SetFont(font)
w, h = dc.GetTextExtent(self._pagesInfoVec[i].GetCaption())
rectHeight = h * 2
else:
rectHeight = self._nImgSize * 2
# Check that we have enough space to draw the button
if posy + rectHeight > size.GetHeight():
break
# Calculate the button rectangle
posx = 0
buttonRect = wx.Rect(posx, posy, rectWidth, rectHeight)
indx = self._pagesInfoVec[i].GetImageIndex()
if indx == -1:
bmp = wx.NullBitmap
else:
bmp = self._ImageList.GetBitmap(indx)
self.DrawLabel(dc, buttonRect, self._pagesInfoVec[i].GetCaption(), bmp,
self._pagesInfoVec[i], self.HasAGWFlag(INB_LEFT) or self.HasAGWFlag(INB_TOP),
i, self._nIndex == i, self._nHoveredImgIdx == i)
posy += rectHeight
# Update all buttons that can not fit into the screen as non-visible
for ii in xrange(count, len(self._pagesInfoVec)):
self._pagesInfoVec[i].SetPosition(wx.Point(-1, -1))
if bUsePin:
clientRect = self.GetClientRect()
pinRect = wx.Rect(clientRect.GetX() + clientRect.GetWidth() - 20, 2, 20, 20)
self.DrawPin(dc, pinRect, not self._bCollapsed)
def DrawBackgroundBitmap(self, dc):
"""
Draws a bitmap as the background of the control.
:param `dc`: an instance of :class:`DC`.
"""
clientRect = self.GetClientRect()
width = clientRect.GetWidth()
height = clientRect.GetHeight()
coveredY = coveredX = 0
xstep = self._skin.GetWidth()
ystep = self._skin.GetHeight()
bmpRect = wx.Rect(0, 0, xstep, ystep)
if bmpRect != clientRect:
mem_dc = wx.MemoryDC()
bmp = wx.EmptyBitmap(width, height)
mem_dc.SelectObject(bmp)
while coveredY < height:
while coveredX < width:
mem_dc.DrawBitmap(self._skin, coveredX, coveredY, True)
coveredX += xstep
coveredX = 0
coveredY += ystep
mem_dc.SelectObject(wx.NullBitmap)
#self._skin = bmp
dc.DrawBitmap(bmp, 0, 0)
else:
dc.DrawBitmap(self._skin, 0, 0)
def OnMouseLeftUp(self, event):
"""
Handles the ``wx.EVT_LEFT_UP`` event for :class:`LabelContainer`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
if self.HasAGWFlag(INB_NO_RESIZE):
ImageContainerBase.OnMouseLeftUp(self, event)
return
if self.HasCapture():
self.ReleaseMouse()
# Sash was being dragged?
if not self._sashRect.IsEmpty():
# Remove sash
ArtManager.Get().DrawDragSash(self._sashRect)
self.Resize(event)
self._sashRect = wx.Rect()
return
self._sashRect = wx.Rect()
# Restore cursor
if self._oldCursor.Ok():
wx.SetCursor(self._oldCursor)
self._oldCursor = wx.NullCursor
ImageContainerBase.OnMouseLeftUp(self, event)
def Resize(self, event):
"""
Actually resizes the tab area.
:param `event`: an instance of :class:`SizeEvent`.
"""
# Resize our size
self._tabAreaSize = self.GetSize()
newWidth = self._tabAreaSize.x
x = event.GetX()
if self.HasAGWFlag(INB_BOTTOM) or self.HasAGWFlag(INB_RIGHT):
newWidth -= event.GetX()
else:
newWidth = x
if newWidth < 100: # Dont allow width to be lower than that
newWidth = 100
self.SetSizeHints(newWidth, self._tabAreaSize.y)
# Update the tab new area width
self._nTabAreaWidth = newWidth
self.GetParent().Freeze()
self.GetParent().GetSizer().Layout()
self.GetParent().Thaw()
def OnMouseMove(self, event):
"""
Handles the ``wx.EVT_MOTION`` event for :class:`LabelContainer`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
if self.HasAGWFlag(INB_NO_RESIZE):
ImageContainerBase.OnMouseMove(self, event)
return
# Remove old sash
if not self._sashRect.IsEmpty():
ArtManager.Get().DrawDragSash(self._sashRect)
if event.LeftIsDown():
if not self._sashRect.IsEmpty():
# Progress sash, and redraw it
clientRect = self.GetClientRect()
pt = self.ClientToScreen(wx.Point(event.GetX(), 0))
self._sashRect = wx.RectPS(pt, wx.Size(4, clientRect.height))
ArtManager.Get().DrawDragSash(self._sashRect)
else:
# Sash is not being dragged
if self._oldCursor.Ok():
wx.SetCursor(self._oldCursor)
self._oldCursor = wx.NullCursor
else:
if self.HasCapture():
self.ReleaseMouse()
if self.PointOnSash(event.GetPosition()):
# Change cursor to EW cursor
self._oldCursor = self.GetCursor()
wx.SetCursor(wx.StockCursor(wx.CURSOR_SIZEWE))
elif self._oldCursor.Ok():
wx.SetCursor(self._oldCursor)
self._oldCursor = wx.NullCursor
self._sashRect = wx.Rect()
ImageContainerBase.OnMouseMove(self, event)
def OnMouseLeftDown(self, event):
"""
Handles the ``wx.EVT_LEFT_DOWN`` event for :class:`LabelContainer`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
if self.HasAGWFlag(INB_NO_RESIZE):
ImageContainerBase.OnMouseLeftDown(self, event)
return
imgIdx, where = self.HitTest(event.GetPosition())
if IMG_OVER_EW_BORDER == where and not self._bCollapsed:
# We are over the sash
if not self._sashRect.IsEmpty():
ArtManager.Get().DrawDragSash(self._sashRect)
else:
# first time, begin drawing sash
self.CaptureMouse()
# Change mouse cursor
self._oldCursor = self.GetCursor()
wx.SetCursor(wx.StockCursor(wx.CURSOR_SIZEWE))
clientRect = self.GetClientRect()
pt = self.ClientToScreen(wx.Point(event.GetX(), 0))
self._sashRect = wx.RectPS(pt, wx.Size(4, clientRect.height))
ArtManager.Get().DrawDragSash(self._sashRect)
else:
ImageContainerBase.OnMouseLeftDown(self, event)
def OnMouseLeaveWindow(self, event):
"""
Handles the ``wx.EVT_LEAVE_WINDOW`` event for :class:`LabelContainer`.
:param `event`: a :class:`MouseEvent` event to be processed.
"""
if self.HasAGWFlag(INB_NO_RESIZE):
ImageContainerBase.OnMouseLeaveWindow(self, event)
return
# If Sash is being dragged, ignore this event
if not self.HasCapture():
ImageContainerBase.OnMouseLeaveWindow(self, event)
def DrawRegularHover(self, dc, rect):
"""
Draws a rounded rectangle around the current tab.
:param `dc`: an instance of :class:`DC`;
:param `rect`: the current tab client rectangle.
"""
# The hovered tab with default border
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetPen(wx.Pen(wx.WHITE))
# We draw CCW
if self.HasAGWFlag(INB_RIGHT) or self.HasAGWFlag(INB_TOP):
# Right images
# Upper line
dc.DrawLine(rect.x + 1, rect.y, rect.x + rect.width, rect.y)
# Right line (white)
dc.DrawLine(rect.x + rect.width, rect.y, rect.x + rect.width, rect.y + rect.height)
# Bottom diagnol - we change pen
dc.SetPen(wx.Pen(self._coloursMap[INB_TABS_BORDER_COLOUR]))
# Bottom line
dc.DrawLine(rect.x + rect.width, rect.y + rect.height, rect.x, rect.y + rect.height)
else:
# Left images
# Upper line white
dc.DrawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y)
# Left line
dc.DrawLine(rect.x, rect.y, rect.x, rect.y + rect.height)
# Bottom diagnol, we change the pen
dc.SetPen(wx.Pen(self._coloursMap[INB_TABS_BORDER_COLOUR]))
# Bottom line
dc.DrawLine(rect.x, rect.y + rect.height, rect.x + rect.width, rect.y + rect.height)
def DrawWebHover(self, dc, caption, xCoord, yCoord, selected):
"""
Draws a web style hover effect (cursor set to hand & text is underlined).
:param `dc`: an instance of :class:`DC`;
:param `caption`: the tab caption text;
:param `xCoord`: the x position of the tab caption;
:param `yCoord`: the y position of the tab caption;
:param `selected`: ``True`` if the tab is selected, ``False`` otherwise.
"""
# Redraw the text with underlined font
underLinedFont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
underLinedFont.SetPointSize(underLinedFont.GetPointSize() * self.GetParent().GetFontSizeMultiple())
if self.GetParent().GetFontBold():
underLinedFont.SetWeight(wx.FONTWEIGHT_BOLD)
elif self.HasAGWFlag(INB_BOLD_TAB_SELECTION) and selected:
underLinedFont.SetWeight(wx.FONTWEIGHT_BOLD)
underLinedFont.SetUnderlined(True)
dc.SetFont(underLinedFont)
dc.DrawText(caption, xCoord, yCoord)
def SetColour(self, which, colour):
"""
Sets a colour for a parameter.
:param `which`: can be one of the following parameters:
================================== ======= ==================================
Colour Key Value Description
================================== ======= ==================================
``INB_TAB_AREA_BACKGROUND_COLOUR`` 100 The tab area background colour
``INB_ACTIVE_TAB_COLOUR`` 101 The active tab background colour
``INB_TABS_BORDER_COLOUR`` 102 The tabs border colour
``INB_TEXT_COLOUR`` 103 The tab caption text colour
``INB_ACTIVE_TEXT_COLOUR`` 104 The active tab caption text colour
``INB_HILITE_TAB_COLOUR`` 105 The tab caption highlight text colour
================================== ======= ==================================
:param `colour`: a valid :class:`Colour` object.
"""
self._coloursMap[which] = colour
def GetColour(self, which):
"""
Returns a colour for a parameter.
:param `which`: the colour key.
:see: :meth:`~LabelContainer.SetColour` for a list of valid colour keys.
"""
if not self._coloursMap.has_key(which):
return wx.Colour()
return self._coloursMap[which]
def InitializeColours(self):
""" Initializes the colours map to be used for this control. """
# Initialize map colours
self._coloursMap.update({INB_TAB_AREA_BACKGROUND_COLOUR: ArtManager.Get().LightColour(ArtManager.Get().FrameColour(), 50)})
self._coloursMap.update({INB_ACTIVE_TAB_COLOUR: ArtManager.Get().GetMenuFaceColour()})
self._coloursMap.update({INB_TABS_BORDER_COLOUR: wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DSHADOW)})
self._coloursMap.update({INB_HILITE_TAB_COLOUR: wx.NamedColour("LIGHT BLUE")})
self._coloursMap.update({INB_TEXT_COLOUR: wx.WHITE})
self._coloursMap.update({INB_ACTIVE_TEXT_COLOUR: wx.BLACK})
# dont allow bright colour one on the other
if not ArtManager.Get().IsDark(self._coloursMap[INB_TAB_AREA_BACKGROUND_COLOUR]) and \
not ArtManager.Get().IsDark(self._coloursMap[INB_TEXT_COLOUR]):
self._coloursMap[INB_TEXT_COLOUR] = ArtManager.Get().DarkColour(self._coloursMap[INB_TEXT_COLOUR], 100)
def DrawLabel(self, dc, rect, text, bmp, imgInfo, orientationLeft, imgIdx, selected, hover):
"""
Draws a label using the specified dc.
:param `dc`: an instance of :class:`DC`;
:param `rect`: the text client rectangle;
:param `text`: the actual text string;
:param `bmp`: a bitmap to be drawn next to the text;
:param `imgInfo`: an instance of :class:`ImageInfo`;
:param `orientationLeft`: ``True`` if the book has the ``INB_RIGHT`` or ``INB_LEFT``
style set;
:param `imgIdx`: the tab image index;
:param `selected`: ``True`` if the tab is selected, ``False`` otherwise;
:param `hover`: ``True`` if the tab is being hovered with the mouse, ``False`` otherwise.
"""
dcsaver = DCSaver(dc)
nPadding = 6
if orientationLeft:
rect.x += nPadding
rect.width -= nPadding
else:
rect.width -= nPadding
textRect = wx.Rect(*rect)
imgRect = wx.Rect(*rect)
font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
font.SetPointSize(font.GetPointSize() * self.GetParent().GetFontSizeMultiple())
if self.GetParent().GetFontBold():
font.SetWeight(wx.FONTWEIGHT_BOLD)
elif self.HasAGWFlag(INB_BOLD_TAB_SELECTION) and selected:
font.SetWeight(wx.FONTWEIGHT_BOLD)
dc.SetFont(font)
# First we define the rectangle for the text
w, h = dc.GetTextExtent(text)
#-------------------------------------------------------------------------
# Label layout:
# [ nPadding | Image | nPadding | Text | nPadding ]
#-------------------------------------------------------------------------
# Text bounding rectangle
textRect.x += nPadding
textRect.y = rect.y + (rect.height - h)/2
textRect.width = rect.width - 2 * nPadding
if bmp.Ok() and not self.HasAGWFlag(INB_SHOW_ONLY_TEXT):
textRect.x += (bmp.GetWidth() + nPadding)
textRect.width -= (bmp.GetWidth() + nPadding)
textRect.height = h
# Truncate text if needed
caption = ArtManager.Get().TruncateText(dc, text, textRect.width)
# Image bounding rectangle
if bmp.Ok() and not self.HasAGWFlag(INB_SHOW_ONLY_TEXT):
imgRect.x += nPadding
imgRect.width = bmp.GetWidth()
imgRect.y = rect.y + (rect.height - bmp.GetHeight())/2
imgRect.height = bmp.GetHeight()
# Draw bounding rectangle
if selected:
# First we colour the tab
dc.SetBrush(wx.Brush(self._coloursMap[INB_ACTIVE_TAB_COLOUR]))
if self.HasAGWFlag(INB_BORDER):
dc.SetPen(wx.Pen(self._coloursMap[INB_TABS_BORDER_COLOUR]))
else:
dc.SetPen(wx.Pen(self._coloursMap[INB_ACTIVE_TAB_COLOUR]))
labelRect = wx.Rect(*rect)
if orientationLeft:
labelRect.width += 3
else:
labelRect.width += 3
labelRect.x -= 3
dc.DrawRoundedRectangleRect(labelRect, 3)
if not orientationLeft and self.HasAGWFlag(INB_DRAW_SHADOW):
dc.SetPen(wx.BLACK_PEN)
dc.DrawPoint(labelRect.x + labelRect.width - 1, labelRect.y + labelRect.height - 1)
# Draw the text & bitmap
if caption != "":
if selected:
dc.SetTextForeground(self._coloursMap[INB_ACTIVE_TEXT_COLOUR])
else:
dc.SetTextForeground(self._coloursMap[INB_TEXT_COLOUR])
dc.DrawText(caption, textRect.x, textRect.y)
imgInfo.SetTextRect(textRect)
else:
imgInfo.SetTextRect(wx.Rect())
if bmp.Ok() and not self.HasAGWFlag(INB_SHOW_ONLY_TEXT):
dc.DrawBitmap(bmp, imgRect.x, imgRect.y, True)
# Drop shadow
if self.HasAGWFlag(INB_DRAW_SHADOW) and selected:
sstyle = 0
if orientationLeft:
sstyle = BottomShadow
else:
sstyle = BottomShadowFull | RightShadow
if self.HasAGWFlag(INB_WEB_HILITE):
# Always drop shadow for this style
ArtManager.Get().DrawBitmapShadow(dc, rect, sstyle)
else:
if imgIdx+1 != self._nHoveredImgIdx:
ArtManager.Get().DrawBitmapShadow(dc, rect, sstyle)
# Draw hover effect
if hover:
if self.HasAGWFlag(INB_WEB_HILITE) and caption != "":
self.DrawWebHover(dc, caption, textRect.x, textRect.y, selected)
else:
self.DrawRegularHover(dc, rect)
# Update the page information bout position and size
imgInfo.SetPosition(rect.GetPosition())
imgInfo.SetSize(rect.GetSize())
# ---------------------------------------------------------------------------- #
# Class FlatBookBase
# ---------------------------------------------------------------------------- #
class FlatBookBase(wx.Panel):
""" Base class for the containing window for :class:`LabelBook` and :class:`FlatImageBook`. """
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, agwStyle=0, name="FlatBookBase"):
"""
Default class constructor.
:param `parent`: parent window. Must not be ``None``;
:param `id`: window identifier. A value of -1 indicates a default value;
:param `pos`: the control position. A value of (-1, -1) indicates a default position,
chosen by either the windowing system or wxPython, depending on platform;
:param `size`: the control size. A value of (-1, -1) indicates a default size,
chosen by either the windowing system or wxPython, depending on platform;
:param `style`: the underlying :class:`Panel` window style;
:param `agwStyle`: the AGW-specific window style. This can be a combination of the
following bits:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
:param `name`: the window name.
"""
self._pages = None
self._bInitializing = True
self._pages = None
self._bForceSelection = False
self._windows = []
self._fontSizeMultiple = 1.0
self._fontBold = False
style |= wx.TAB_TRAVERSAL
self._agwStyle = agwStyle
wx.Panel.__init__(self, parent, id, pos, size, style, name)
self._bInitializing = False
self.Bind(wx.EVT_NAVIGATION_KEY, self.OnNavigationKey)
self.Bind(wx.EVT_MOUSE_CAPTURE_LOST, lambda evt: True)
def SetAGWWindowStyleFlag(self, agwStyle):
"""
Sets the window style.
:param `agwStyle`: can be a combination of the following bits:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
"""
self._agwStyle = agwStyle
# Check that we are not in initialization process
if self._bInitializing:
return
if not self._pages:
return
# Detach the windows attached to the sizer
if self.GetSelection() >= 0:
self._mainSizer.Detach(self._windows[self.GetSelection()])
self._mainSizer.Detach(self._pages)
if isinstance(self, LabelBook):
self._mainSizer = wx.BoxSizer(wx.HORIZONTAL)
else:
if agwStyle & INB_LEFT or agwStyle & INB_RIGHT:
self._mainSizer = wx.BoxSizer(wx.HORIZONTAL)
else:
self._mainSizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self._mainSizer)
# Add the tab container and the separator
self._mainSizer.Add(self._pages, 0, wx.EXPAND)
if isinstance(self, FlatImageBook):
if agwStyle & INB_LEFT or agwStyle & INB_RIGHT:
self._pages.SetSizeHints(self._pages._nImgSize * 2, -1)
else:
self._pages.SetSizeHints(-1, self._pages._nImgSize * 2)
# Attach the windows back to the sizer to the sizer
if self.GetSelection() >= 0:
self.DoSetSelection(self._windows[self.GetSelection()])
if agwStyle & INB_FIT_LABELTEXT:
self.ResizeTabArea()
self._mainSizer.Layout()
dummy = wx.SizeEvent()
wx.PostEvent(self, dummy)
self._pages.Refresh()
def GetAGWWindowStyleFlag(self):
"""
Returns the :class:`FlatBookBase` window style.
:see: :meth:`~FlatBookBase.SetAGWWindowStyleFlag` for a list of possible window style flags.
"""
return self._agwStyle
def HasAGWFlag(self, flag):
"""
Returns whether a flag is present in the :class:`FlatBookBase` style.
:param `flag`: one of the possible :class:`FlatBookBase` window styles.
:see: :meth:`~FlatBookBase.SetAGWWindowStyleFlag` for a list of possible window style flags.
"""
agwStyle = self.GetAGWWindowStyleFlag()
res = (agwStyle & flag and [True] or [False])[0]
return res
def AddPage(self, page, text, select=False, imageId=-1):
"""
Adds a page to the book.
:param `page`: specifies the new page;
:param `text`: specifies the text for the new page;
:param `select`: specifies whether the page should be selected;
:param `imageId`: specifies the optional image index for the new page.
:note: The call to this function generates the page changing events.
"""
if not page:
return
page.Reparent(self)
self._windows.append(page)
if select or len(self._windows) == 1:
self.SetSelection(len(self._windows)-1)
else:
page.Hide()
self._pages.AddPage(text, select, imageId)
self.ResizeTabArea()
self.Refresh()
def InsertPage(self, page_idx, page, text, select=False, imageId=-1):
"""
Inserts a page into the book at the specified position.
:param `page_idx`: specifies the position for the new page;
:param `page`: specifies the new page;
:param `text`: specifies the text for the new page;
:param `select`: specifies whether the page should be selected;
:param `imageId`: specifies the optional image index for the new page.
:note: The call to this function generates the page changing events.
"""
if not page:
return
page.Reparent(self)
self._windows.insert(page_idx, page)
if select or len(self._windows) == 1:
self.SetSelection(page_idx)
else:
page.Hide()
self._pages.InsertPage(page_idx, text, select, imageId)
self.ResizeTabArea()
self.Refresh()
def DeletePage(self, page):
"""
Deletes the specified page, and the associated window.
:param `page`: an integer specifying the page to be deleted.
:note: The call to this function generates the page changing events.
"""
if page >= len(self._windows) or page < 0:
return
# Fire a closing event
event = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CLOSING, self.GetId())
event.SetSelection(page)
event.SetEventObject(self)
self.GetEventHandler().ProcessEvent(event)
# The event handler allows it?
if not event.IsAllowed():
return False
self.Freeze()
# Delete the requested page
pageRemoved = self._windows[page]
# If the page is the current window, remove it from the sizer
# as well
if page == self.GetSelection():
self._mainSizer.Detach(pageRemoved)
# Remove it from the array as well
self._windows.pop(page)
# Now we can destroy it in wxWidgets use Destroy instead of delete
pageRemoved.Destroy()
self._mainSizer.Layout()
self._pages.DoDeletePage(page)
self.ResizeTabArea()
self.Thaw()
# Fire a closed event
closedEvent = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CLOSED, self.GetId())
closedEvent.SetSelection(page)
closedEvent.SetEventObject(self)
self.GetEventHandler().ProcessEvent(closedEvent)
def RemovePage(self, page):
"""
Deletes the specified page, without deleting the associated window.
:param `page`: an integer specifying the page to be removed.
:note: The call to this function generates the page changing events.
"""
if page >= len(self._windows):
return False
# Fire a closing event
event = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CLOSING, self.GetId())
event.SetSelection(page)
event.SetEventObject(self)
self.GetEventHandler().ProcessEvent(event)
# The event handler allows it?
if not event.IsAllowed():
return False
self.Freeze()
# Remove the requested page
pageRemoved = self._windows[page]
# If the page is the current window, remove it from the sizer
# as well
if page == self.GetSelection():
self._mainSizer.Detach(pageRemoved)
# Remove it from the array as well
self._windows.pop(page)
self._mainSizer.Layout()
self.ResizeTabArea()
self.Thaw()
self._pages.DoDeletePage(page)
# Fire a closed event
closedEvent = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CLOSED, self.GetId())
closedEvent.SetSelection(page)
closedEvent.SetEventObject(self)
self.GetEventHandler().ProcessEvent(closedEvent)
return True
def ResizeTabArea(self):
""" Resizes the tab area if the control has the ``INB_FIT_LABELTEXT`` style set. """
agwStyle = self.GetAGWWindowStyleFlag()
if agwStyle & INB_FIT_LABELTEXT == 0:
return
if agwStyle & INB_LEFT or agwStyle & INB_RIGHT:
dc = wx.MemoryDC()
dc.SelectObject(wx.EmptyBitmap(1, 1))
font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
font.SetPointSize(font.GetPointSize()*self._fontSizeMultiple)
if self.GetFontBold() or agwStyle & INB_BOLD_TAB_SELECTION:
font.SetWeight(wx.FONTWEIGHT_BOLD)
dc.SetFont(font)
maxW = 0
for page in xrange(self.GetPageCount()):
caption = self._pages.GetPageText(page)
w, h = dc.GetTextExtent(caption)
maxW = max(maxW, w)
maxW += 24 #TODO this is 6*4 6 is nPadding from drawlabel
if not agwStyle & INB_SHOW_ONLY_TEXT:
maxW += self._pages._nImgSize * 2
maxW = max(maxW, 100)
self._pages.SetSizeHints(maxW, -1)
self._pages._nTabAreaWidth = maxW
def DeleteAllPages(self):
""" Deletes all the pages in the book. """
if not self._windows:
return
self.Freeze()
for win in self._windows:
win.Destroy()
self._windows = []
self.Thaw()
# remove old selection
self._pages.ClearAll()
self._pages.Refresh()
def SetSelection(self, page):
"""
Changes the selection from currently visible/selected page to the page
given by page.
:param `page`: an integer specifying the page to be selected.
:note: The call to this function generates the page changing events.
"""
if page >= len(self._windows):
return
if not self.GetEnabled(page):
return
if page == self.GetSelection() and not self._bForceSelection:
return
oldSelection = self.GetSelection()
# Generate an event that indicates that an image is about to be selected
event = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CHANGING, self.GetId())
event.SetSelection(page)
event.SetOldSelection(oldSelection)
event.SetEventObject(self)
self.GetEventHandler().ProcessEvent(event)
# The event handler allows it?
if not event.IsAllowed() and not self._bForceSelection:
return
self.DoSetSelection(self._windows[page])
# Now we can update the new selection
self._pages._nIndex = page
# Refresh calls the OnPaint of this class
self._pages.Refresh()
# Generate an event that indicates that an image was selected
eventChanged = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CHANGED, self.GetId())
eventChanged.SetEventObject(self)
eventChanged.SetOldSelection(oldSelection)
eventChanged.SetSelection(page)
self.GetEventHandler().ProcessEvent(eventChanged)
def AssignImageList(self, imglist):
"""
Assigns an image list to the control.
:param `imglist`: an instance of :class:`ImageList`.
"""
self._pages.AssignImageList(imglist)
# Force change
self.SetAGWWindowStyleFlag(self.GetAGWWindowStyleFlag())
def GetSelection(self):
""" Returns the current selection. """
if self._pages:
return self._pages._nIndex
else:
return -1
def DoSetSelection(self, window):
"""
Select the window by the provided pointer.
:param `window`: an instance of :class:`Window`.
"""
curSel = self.GetSelection()
agwStyle = self.GetAGWWindowStyleFlag()
# Replace the window in the sizer
self.Freeze()
# Check if a new selection was made
bInsertFirst = (agwStyle & INB_BOTTOM or agwStyle & INB_RIGHT)
if curSel >= 0:
# Remove the window from the main sizer
self._mainSizer.Detach(self._windows[curSel])
self._windows[curSel].Hide()
if bInsertFirst:
self._mainSizer.Insert(0, window, 1, wx.EXPAND)
else:
self._mainSizer.Add(window, 1, wx.EXPAND)
window.Show()
self._mainSizer.Layout()
self.Thaw()
def GetImageList(self):
""" Returns the associated image list. """
return self._pages.GetImageList()
def GetPageCount(self):
""" Returns the number of pages in the book. """
return len(self._windows)
def GetFontBold(self):
""" Gets the font bold status. """
return self._fontBold
def SetFontBold(self, bold):
"""
Sets whether the page captions are bold or not.
:param `bold`: ``True`` or ``False``.
"""
self._fontBold = bold
def GetFontSizeMultiple(self):
""" Gets the font size multiple for the page captions. """
return self._fontSizeMultiple
def SetFontSizeMultiple(self, multiple):
"""
Sets the font size multiple for the page captions.
:param `multiple`: The multiple to be applied to the system font to get the our font size.
"""
self._fontSizeMultiple = multiple
def SetPageImage(self, page, imageId):
"""
Sets the image index for the given page.
:param `page`: an integer specifying the page index;
:param `image`: an index into the image list.
"""
self._pages.SetPageImage(page, imageId)
self._pages.Refresh()
def SetPageText(self, page, text):
"""
Sets the text for the given page.
:param `page`: an integer specifying the page index;
:param `text`: the new tab label.
"""
self._pages.SetPageText(page, text)
self._pages.Refresh()
def GetPageText(self, page):
"""
Returns the text for the given page.
:param `page`: an integer specifying the page index.
"""
return self._pages.GetPageText(page)
def GetPageImage(self, page):
"""
Returns the image index for the given page.
:param `page`: an integer specifying the page index.
"""
return self._pages.GetPageImage(page)
def GetEnabled(self, page):
"""
Returns whether a tab is enabled or not.
:param `page`: an integer specifying the page index.
"""
return self._pages.GetEnabled(page)
def EnableTab(self, page, enabled=True):
"""
Enables or disables a tab.
:param `page`: an integer specifying the page index;
:param `enabled`: ``True`` to enable a tab, ``False`` to disable it.
"""
if page >= len(self._windows):
return
self._windows[page].Enable(enabled)
self._pages.EnableTab(page, enabled)
def GetPage(self, page):
"""
Returns the window at the given page position.
:param `page`: an integer specifying the page to be returned.
"""
if page >= len(self._windows):
return
return self._windows[page]
def GetCurrentPage(self):
""" Returns the currently selected notebook page or ``None``. """
if self.GetSelection() < 0:
return
return self.GetPage(self.GetSelection())
def OnNavigationKey(self, event):
"""
Handles the ``wx.EVT_NAVIGATION_KEY`` event for :class:`FlatBookBase`.
:param `event`: a :class:`NavigationKeyEvent` event to be processed.
"""
if event.IsWindowChange():
if self.GetPageCount() == 0:
return
# change pages
self.AdvanceSelection(event.GetDirection())
else:
event.Skip()
def AdvanceSelection(self, forward=True):
"""
Cycles through the tabs.
:param `forward`: if ``True``, the selection is advanced in ascending order
(to the right), otherwise the selection is advanced in descending order.
:note: The call to this function generates the page changing events.
"""
nSel = self.GetSelection()
if nSel < 0:
return
nMax = self.GetPageCount() - 1
if forward:
newSelection = (nSel == nMax and [0] or [nSel + 1])[0]
else:
newSelection = (nSel == 0 and [nMax] or [nSel - 1])[0]
self.SetSelection(newSelection)
def ChangeSelection(self, page):
"""
Changes the selection for the given page, returning the previous selection.
:param `page`: an integer specifying the page to be selected.
:note: The call to this function does not generate the page changing events.
"""
if page < 0 or page >= self.GetPageCount():
return
oldPage = self.GetSelection()
self.DoSetSelection(page)
return oldPage
CurrentPage = property(GetCurrentPage, doc="See `GetCurrentPage`")
Page = property(GetPage, doc="See `GetPage`")
PageCount = property(GetPageCount, doc="See `GetPageCount`")
PageImage = property(GetPageImage, SetPageImage, doc="See `GetPageImage, SetPageImage`")
PageText = property(GetPageText, SetPageText, doc="See `GetPageText, SetPageText`")
Selection = property(GetSelection, SetSelection, doc="See `GetSelection, SetSelection`")
# ---------------------------------------------------------------------------- #
# Class FlatImageBook
# ---------------------------------------------------------------------------- #
class FlatImageBook(FlatBookBase):
"""
Default implementation of the image book, it is like a :class:`Notebook`, except that
images are used to control the different pages. This container is usually used
for configuration dialogs etc.
:note: Currently, this control works properly for images of size 32x32 and bigger.
"""
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, agwStyle=0, name="FlatImageBook"):
"""
Default class constructor.
:param `parent`: parent window. Must not be ``None``;
:param `id`: window identifier. A value of -1 indicates a default value;
:param `pos`: the control position. A value of (-1, -1) indicates a default position,
chosen by either the windowing system or wxPython, depending on platform;
:param `size`: the control size. A value of (-1, -1) indicates a default size,
chosen by either the windowing system or wxPython, depending on platform;
:param `style`: the underlying :class:`Panel` window style;
:param `agwStyle`: the AGW-specific window style. This can be a combination of the
following bits:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
:param `name`: the window name.
"""
FlatBookBase.__init__(self, parent, id, pos, size, style, agwStyle, name)
self._pages = self.CreateImageContainer()
if agwStyle & INB_LEFT or agwStyle & INB_RIGHT:
self._mainSizer = wx.BoxSizer(wx.HORIZONTAL)
else:
self._mainSizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self._mainSizer)
# Add the tab container to the sizer
self._mainSizer.Add(self._pages, 0, wx.EXPAND)
if agwStyle & INB_LEFT or agwStyle & INB_RIGHT:
self._pages.SetSizeHints(self._pages.GetImageSize() * 2, -1)
else:
self._pages.SetSizeHints(-1, self._pages.GetImageSize() * 2)
self._mainSizer.Layout()
def CreateImageContainer(self):
""" Creates the image container class for :class:`FlatImageBook`. """
return ImageContainer(self, wx.ID_ANY, agwStyle=self.GetAGWWindowStyleFlag())
# ---------------------------------------------------------------------------- #
# Class LabelBook
# ---------------------------------------------------------------------------- #
class LabelBook(FlatBookBase):
"""
An implementation of a notebook control - except that instead of having
tabs to show labels, it labels to the right or left (arranged horizontally).
"""
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, agwStyle=0, name="LabelBook"):
"""
Default class constructor.
:param `parent`: parent window. Must not be ``None``;
:param `id`: window identifier. A value of -1 indicates a default value;
:param `pos`: the control position. A value of (-1, -1) indicates a default position,
chosen by either the windowing system or wxPython, depending on platform;
:param `size`: the control size. A value of (-1, -1) indicates a default size,
chosen by either the windowing system or wxPython, depending on platform;
:param `style`: the underlying :class:`Panel` window style;
:param `agwStyle`: the AGW-specific window style. This can be a combination of the
following bits:
=========================== =========== ==================================================
Window Styles Hex Value Description
=========================== =========== ==================================================
``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`.
``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`.
``INB_RIGHT`` 0x4 Place labels on the right side.
``INB_TOP`` 0x8 Place labels above the page area.
``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`.
``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`.
``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`.
``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control.
``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`.
``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control.
``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`.
``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`.
``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area.
``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs.
``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font.
=========================== =========== ==================================================
:param `name`: the window name.
"""
FlatBookBase.__init__(self, parent, id, pos, size, style, agwStyle, name)
self._pages = self.CreateImageContainer()
# Label book specific initialization
self._mainSizer = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(self._mainSizer)
# Add the tab container to the sizer
self._mainSizer.Add(self._pages, 0, wx.EXPAND)
self._pages.SetSizeHints(self._pages.GetTabAreaWidth(), -1)
# Initialize the colours maps
self._pages.InitializeColours()
self.Bind(wx.EVT_SIZE, self.OnSize)
def CreateImageContainer(self):
""" Creates the image container (LabelContainer) class for :class:`FlatImageBook`. """
return LabelContainer(self, wx.ID_ANY, agwStyle=self.GetAGWWindowStyleFlag())
def SetColour(self, which, colour):
"""
Sets the colour for the specified parameter.
:param `which`: the colour key;
:param `colour`: a valid :class:`Colour` instance.
:see: :meth:`LabelContainer.SetColour() <LabelContainer.SetColour>` for a list of valid colour keys.
"""
self._pages.SetColour(which, colour)
def GetColour(self, which):
"""
Returns the colour for the specified parameter.
:param `which`: the colour key.
:see: :meth:`LabelContainer.SetColour() <LabelContainer.SetColour>` for a list of valid colour keys.
"""
return self._pages.GetColour(which)
def OnSize(self, event):
"""
Handles the ``wx.EVT_SIZE`` event for :class:`LabelBook`.
:param `event`: a :class:`SizeEvent` event to be processed.
"""
self._pages.Refresh()
event.Skip()