1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" Pyface 'DockSizer' support.
This package provides the sizer associated with a Pyface DockWindow
component. The sizer manages the layout of the DockWindow child controls
and the notebook tabs and dragbars associated with the DockWindow.
"""
import sys
import wx
from traits.api import (
HasPrivateTraits,
Instance,
Str,
Int,
List,
Enum,
Tuple,
Any,
Range,
Property,
Callable,
Constant,
Event,
Undefined,
Bool,
cached_property,
observe,
)
from traitsui.dock_window_theme import dock_window_theme
from traitsui.wx.helper import BufferDC
from pyface.api import SystemMetrics
from pyface.image_resource import ImageResource
from pyface.ui_traits import Image
from pyface.wx.drag_and_drop import PythonDropSource
from pyface.timer.api import do_later, do_after
from .idockable import IDockable
from .ifeature_tool import IFeatureTool
# Define version dependent values:
is_mac = sys.platform == "darwin"
# -------------------------------------------------------------------------------
# Constants:
# -------------------------------------------------------------------------------
# Standard font text height:
text_dy = 13
# Maximum allowed length of a tab label:
MaxTabLength = 30
# Size of a drag bar (in pixels):
DragBarSize = 14
# Images sizes (in pixels):
CloseTabSize = 10
CloseDragSize = 7
# Tab drawing states:
TabInactive = 0
TabActive = 1
TabHover = 2
NormalStates = (TabInactive, TabActive)
NotActiveStates = (TabInactive, TabHover)
# Feature overlay colors:
FeatureBrushColor = (255, 255, 255)
FeaturePenColor = (92, 92, 92)
# Color used to update the screen while dragging a splitter bar:
DragColor = (96, 96, 96)
# Color used to update the screen while showing a docking operation in progress:
DockColorBrush = (255, 0, 0, 96)
# Drop Info kinds:
DOCK_TOP = 0
DOCK_BOTTOM = 1
DOCK_LEFT = 2
DOCK_RIGHT = 3
DOCK_TAB = 4
DOCK_TABADD = 5
DOCK_BAR = 6
DOCK_NONE = 7
DOCK_SPLITTER = 8
DOCK_EXPORT = 9
# Splitter states:
SPLIT_VLEFT = 0
SPLIT_VMIDDLE = 1
SPLIT_VRIGHT = 2
SPLIT_HTOP = 3
SPLIT_HMIDDLE = 4
SPLIT_HBOTTOM = 5
# Empty clipping area:
no_clip = (0, 0, 0, 0)
# Valid sequence types:
SequenceType = (list, tuple)
# Tab scrolling directions:
SCROLL_LEFT = 1
SCROLL_RIGHT = 2
SCROLL_TO = 3
# Feature modes:
FEATURE_NONE = -1 # Has no features
FEATURE_NORMAL = 0 # Has normal features
FEATURE_CHANGED = 1 # Has changed or new features
FEATURE_DROP = 2 # Has drag data compatible drop features
FEATURE_DISABLED = 3 # Has feature icon, but is currently disabled
FEATURE_VISIBLE = 4 # Has visible features (mouseover mode)
FEATURE_DROP_VISIBLE = 5 # Has visible drop features (mouseover mode)
FEATURE_PRE_NORMAL = 6 # Has normal features (but has not been drawn yet)
FEATURE_EXTERNAL_DRAG = 256 # A drag started in another DockWindow is active
# Feature sets:
NO_FEATURE_ICON = (
FEATURE_NONE,
FEATURE_DISABLED,
FEATURE_VISIBLE,
FEATURE_DROP_VISIBLE,
)
FEATURES_VISIBLE = (FEATURE_VISIBLE, FEATURE_DROP_VISIBLE)
FEATURE_END_DROP = (FEATURE_DROP, FEATURE_VISIBLE, FEATURE_DROP_VISIBLE)
NORMAL_FEATURES = (FEATURE_NORMAL, FEATURE_DISABLED)
# -------------------------------------------------------------------------------
# Global data:
# -------------------------------------------------------------------------------
# Standard font used by the DockWindow:
standard_font = None
# The list of available DockWindowFeatures:
features = []
# -------------------------------------------------------------------------------
# Trait definitions:
# -------------------------------------------------------------------------------
# Bounds (i.e. x, y, dx, dy):
Bounds = Tuple(Int, Int, Int, Int)
# Docking drag bar style:
DockStyle = Enum("horizontal", "vertical", "tab", "fixed")
# -------------------------------------------------------------------------------
# Adds a new DockWindowFeature class to the list of available features:
# -------------------------------------------------------------------------------
def add_feature(feature_class):
""" Adds a new DockWindowFeature class to the list of available features.
"""
global features
result = feature_class not in features
if result:
features.append(feature_class)
# Mark the feature class as having been installed:
if feature_class.state == 0:
feature_class.state = 1
return result
# -------------------------------------------------------------------------------
# Sets the standard font to use for a specified device context:
# -------------------------------------------------------------------------------
def set_standard_font(dc):
""" Sets the standard font to use for a specified device context.
"""
global standard_font
if standard_font is None:
standard_font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
dc.SetFont(standard_font)
return dc
# -------------------------------------------------------------------------------
# Clears a window to the standard background color:
# -------------------------------------------------------------------------------
def clear_window(window):
""" Clears a window to the standard background color.
"""
bg_color = SystemMetrics().dialog_background_color
bg_color = wx.Colour(
int(bg_color[0] * 255), int(bg_color[1] * 255), int(bg_color[2] * 255)
)
dx, dy = window.GetSize().Get()
dc = wx.PaintDC(window)
dc.SetBrush(wx.Brush(bg_color, wx.SOLID))
dc.SetPen(wx.TRANSPARENT_PEN)
dc.DrawRectangle(0, 0, dx, dy)
# -------------------------------------------------------------------------------
# Gets a temporary device context for a specified window to draw in:
# -------------------------------------------------------------------------------
def get_dc(window):
""" Gets a temporary device context for a specified window to draw in.
"""
if is_mac:
dc = wx.ClientDC(window)
x, y = window.GetPosition().Get()
dx, dy = window.GetSize().Get()
while True:
window = window.GetParent()
if window is None:
break
xw, yw = window.GetPosition().Get()
dxw, dyw = window.GetSize().Get()
dx, dy = min(dx, dxw - x), min(dy, dyw - y)
x += xw
y += yw
dc.SetClippingRegion(0, 0, dx, dy)
return (dc, 0, 0)
x, y = window.ClientToScreen(0, 0)
return (wx.ScreenDC(), x, y)
# -------------------------------------------------------------------------------
# 'DockImages' class:
# -------------------------------------------------------------------------------
class DockImages(HasPrivateTraits):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# Image for closing a tab:
close_tab = Image(ImageResource("close_tab"))
# Image for closing a drag bar:
close_drag = Image(ImageResource("close_drag"))
# ---------------------------------------------------------------------------
# Initalizes the object:
# ---------------------------------------------------------------------------
def __init__(self, **traits):
""" Initializes the object.
"""
super().__init__(**traits)
self._lazy_init_done = False
def init(self):
""" Initializes the parts of the object that depend on the toolkit
selection.
"""
# See if it has already been done.
if self._lazy_init_done:
return
self._lazy_init_done = True
self._close_tab = self.close_tab.create_image().ConvertToBitmap()
self._close_drag = self.close_drag.create_image().ConvertToBitmap()
self._splitter_images = [
ImageResource(name).create_image().ConvertToBitmap()
for name in [
"sv_left",
"sv_middle",
"sv_right",
"sh_top",
"sh_middle",
"sh_bottom",
]
]
self._tab_scroller_images = [
ImageResource(name).create_image().ConvertToBitmap()
for name in ["tab_scroll_l", "tab_scroll_r", "tab_scroll_lr"]
]
self._tab_scroller_dx = self._tab_scroller_images[0].GetWidth()
self._tab_scroller_dy = self._tab_scroller_images[0].GetHeight()
self._feature_images = [
ImageResource(name).create_image().ConvertToBitmap()
for name in [
"tab_feature_normal",
"tab_feature_changed",
"tab_feature_drop",
"tab_feature_disabled",
"bar_feature_normal",
"bar_feature_changed",
"bar_feature_drop",
"bar_feature_disabled",
]
]
self._tab_feature_width = self._feature_images[0].GetWidth()
self._tab_feature_height = self._feature_images[0].GetHeight()
self._bar_feature_width = self._feature_images[3].GetWidth()
self._bar_feature_height = self._feature_images[3].GetHeight()
# ---------------------------------------------------------------------------
# Returns the splitter image to use for a specified splitter state:
# ---------------------------------------------------------------------------
def get_splitter_image(self, state):
""" Returns the splitter image to use for a specified splitter state.
"""
return self._splitter_images[state]
# ---------------------------------------------------------------------------
# Returns the feature image to use for a specified feature state:
# ---------------------------------------------------------------------------
def get_feature_image(self, state, is_tab=True):
""" Returns the feature image to use for a specified feature state.
"""
if is_tab:
return self._feature_images[state]
return self._feature_images[state + 3]
# Creates a singleton instance of the class:
DockImages = DockImages()
# -------------------------------------------------------------------------------
# 'DockItem' class:
# -------------------------------------------------------------------------------
class DockItem(HasPrivateTraits):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# The parent of this item:
parent = Any()
# The control associated with this item (used in subclasses):
control = Instance(wx.Control)
# The DockWindow that owns this item:
owner = Property(observe="parent")
# Bounds of the item:
bounds = Bounds
# The name of this item (used in subclasses):
name = Str()
# Current width of the item:
width = Int(-1)
# Current height of the item:
height = Int(-1)
# Bounds of the item's drag bar or tab:
drag_bounds = Bounds
# The current tab state:
tab_state = Any()
# The tab displayable version of the control's UI name:
tab_name = Property(observe="name")
# Width of the item's tab:
tab_width = Property(observe="control, tab_state, tab_name")
# The DockWindowTheme for this item's DockWindow:
theme = Property
# The theme for the current tab state:
tab_theme = Property
# The current feature mode:
feature_mode = Enum(
FEATURE_NONE,
FEATURE_NORMAL,
FEATURE_CHANGED,
FEATURE_DROP,
FEATURE_VISIBLE,
FEATURE_DROP_VISIBLE,
FEATURE_DISABLED,
FEATURE_PRE_NORMAL,
)
# The position where the feature popup should appear:
feature_popup_position = Property
# The list of features for this item:
features = List()
# The list of drag data compatible drop features for this item:
drop_features = List()
# Current active set of features:
active_features = Property
# ---------------------------------------------------------------------------
# Implementation of the 'owner' property:
# ---------------------------------------------------------------------------
@cached_property
def _get_owner(self):
if self.parent is None:
return None
return self.parent.owner
# ---------------------------------------------------------------------------
# Implementation of the 'tab_name' property:
# ---------------------------------------------------------------------------
@cached_property
def _get_tab_name(self):
name = self.name
if len(name) > MaxTabLength:
name = "%s...%s" % (name[: MaxTabLength - 23], name[-20:])
return name
# ---------------------------------------------------------------------------
# Implementation of the 'tab_width' property:
# ---------------------------------------------------------------------------
@cached_property
def _get_tab_width(self):
if self.control is None:
return 0
self._is_tab = True
# Calculate the size needed by the theme and margins:
theme = self.tab_theme
tw = (
theme.image_slice.xleft
+ theme.image_slice.xright
+ theme.content.left
+ theme.content.right
)
# Add feature marker width:
if self.feature_mode != FEATURE_NONE:
tw += DockImages._tab_feature_width + 3
# Add text width:
dc = set_standard_font(wx.ClientDC(self.control))
tw += dc.GetTextExtent(self.tab_name)[0]
# Add custom image width:
image = self.get_image()
if image is not None:
tw += image.GetWidth() + 3
# Add close button width:
if self.closeable:
tw += CloseTabSize + 6
# Return the computed width:
return tw
# ---------------------------------------------------------------------------
# Implementation of the 'theme' property:
# ---------------------------------------------------------------------------
def _get_theme(self):
if self.control is None:
return dock_window_theme()
return self.control.GetParent().owner.theme
# ---------------------------------------------------------------------------
# Implementation of the 'tab_theme' property:
# ---------------------------------------------------------------------------
def _get_tab_theme(self):
if self.tab_state == TabInactive:
return self.theme.tab_inactive
if self.tab_state == TabActive:
return self.theme.tab_active
return self.theme.tab_hover
# ---------------------------------------------------------------------------
# Implementation of the 'active_features' property:
# ---------------------------------------------------------------------------
def _get_active_features(self):
if len(self.drop_features) > 0:
return self.drop_features
return self.features
# ---------------------------------------------------------------------------
# Implementation of the 'feature_popup_position' property:
# ---------------------------------------------------------------------------
def _get_feature_popup_position(self):
x, y, dx, dy = self.drag_bounds
return wx.Point(x + 5, y + 3)
# ---------------------------------------------------------------------------
# Returns whether or not the item is at a specified window position:
# ---------------------------------------------------------------------------
def is_at(self, x, y, bounds=None):
""" Returns whether or not the item is at a specified window position.
"""
if bounds is None:
bounds = self.bounds
bx, by, bdx, bdy = bounds
return (bx <= x < (bx + bdx)) and (by <= y < (by + bdy))
# ---------------------------------------------------------------------------
# Returns whether or not an event is within a specified bounds:
# ---------------------------------------------------------------------------
def is_in(self, event, x, y, dx, dy):
""" Returns whether or not an event is within a specified bounds.
"""
return (x <= event.GetX() < (x + dx)) and (
y <= event.GetY() < (y + dy)
)
# ---------------------------------------------------------------------------
# Sets the control's drag bounds:
# ---------------------------------------------------------------------------
def set_drag_bounds(self, x, y, dx, dy):
""" Sets the control's drag bounds.
"""
bx, by, bdx, bdy = self.bounds
if (bx + bdx - x) > 0:
self.drag_bounds = (x, y, min(x + dx, bx + bdx) - x, dy)
else:
self.drag_bounds = (x, y, dx, dy)
# ---------------------------------------------------------------------------
# Gets the cursor to use when the mouse is over the item:
# ---------------------------------------------------------------------------
def get_cursor(self, event):
""" Gets the cursor to use when the mouse is over the item.
"""
if self._is_tab and (not self._is_in_close(event)):
return wx.CURSOR_ARROW
return wx.CURSOR_HAND
# ---------------------------------------------------------------------------
# Gets the DockInfo object for a specified window position:
# ---------------------------------------------------------------------------
def dock_info_at(self, x, y, tdx, is_control):
""" Gets the DockInfo object for a specified window position.
"""
if self.is_at(x, y, self.drag_bounds):
x, y, dx, dy = self.drag_bounds
control = self
if self._is_tab:
if is_control:
kind = DOCK_TABADD
tab_bounds = (x, y, dx, dy)
else:
kind = DOCK_TAB
tab_bounds = (x - (tdx // 2), y, tdx, dy)
else:
if is_control:
kind = DOCK_TABADD
tab_bounds = (x, y, self.tab_width, dy)
else:
kind = DOCK_TAB
control = None
tab_bounds = (x + self.tab_width, y, tdx, dy)
return DockInfo(
kind=kind,
tab_bounds=tab_bounds,
region=self.parent,
control=control,
)
return None
# ---------------------------------------------------------------------------
# Prepares for drawing into a device context:
# ---------------------------------------------------------------------------
def begin_draw(self, dc, ox=0, oy=0):
""" Prepares for drawing into a device context.
"""
self._save_clip = dc.GetClippingRect()
x, y, dx, dy = self.bounds
dc.SetClippingRegion(x + ox, y + oy, dx, dy)
# ---------------------------------------------------------------------------
# Terminates drawing into a device context:
# ---------------------------------------------------------------------------
def end_draw(self, dc):
""" Terminates drawing into a device context.
"""
dc.DestroyClippingRegion()
if self._save_clip != no_clip:
dc.SetClippingRegion(*self._save_clip)
self._save_clip = None
# ---------------------------------------------------------------------------
# Handles the left mouse button being pressed:
# ---------------------------------------------------------------------------
def mouse_down(self, event):
""" Handles the left mouse button being pressed.
"""
self._xy = (event.GetX(), event.GetY())
self._closing = self._is_in_close(event)
self._dragging = False
# ---------------------------------------------------------------------------
# Handles the left mouse button being released:
# ---------------------------------------------------------------------------
def mouse_up(self, event):
""" Handles the left mouse button being released.
"""
# Handle the user closing a control:
if self._closing:
if self._is_in_close(event):
self.close()
# Handle the completion of a dragging operation:
elif self._dragging:
window = event.GetEventObject()
dock_info, self._dock_info = self._dock_info, None
self.mark_bounds(False)
control = self
# Check to see if the user is attempting to drag an entire notebook
# region:
if event.AltDown():
control = self.parent
# If the parent is not a notebook, then use the parent's parent:
if isinstance(control, DockRegion) and (
not control.is_notebook
):
control = control.parent
# Make sure the target is not contained within the notebook
# group we are trying to move:
region = dock_info.region
while region is not None:
if region is control:
# If it is, the operation is invalid, abort:
return
region = region.parent
# Check to see if the user is attempting to copy the control:
elif event.ControlDown():
owner = window.owner
control = owner.handler.dock_control_for(
*(owner.handler_args + (window, control))
)
# Complete the docking maneuver:
dock_info.dock(control, window)
# Handle the user clicking on a notebook tab to select it:
elif self._is_tab and self.is_at(
event.GetX(), event.GetY(), self.drag_bounds
):
self.parent.tab_clicked(self)
# ---------------------------------------------------------------------------
# Handles the mouse moving while the left mouse button is pressed:
# ---------------------------------------------------------------------------
def mouse_move(self, event):
""" Handles the mouse moving while the left mouse button is pressed.
"""
# Exit if control is 'fixed' or a 'close' is pending:
if self._closing or self.locked or (self.style == "fixed"):
return
window = event.GetEventObject()
# Check to see if we are in 'drag mode' yet:
if not self._dragging:
x, y = self._xy
if (abs(x - event.GetX()) + abs(y - event.GetY())) < 3:
return
self._dragging = True
self._dock_info = no_dock_info
self._dock_size = self.tab_width
self.mark_bounds(True)
# Get the window and DockInfo object associated with the event:
cur_dock_info = self._dock_info
self._dock_info = dock_info = window.GetSizer().DockInfoAt(
event.GetX(), event.GetY(), self._dock_size, event.ShiftDown()
)
# If the DockInfo has not changed, then no update is needed:
if (
(cur_dock_info.kind == dock_info.kind)
and (cur_dock_info.region is dock_info.region)
and (cur_dock_info.bounds == dock_info.bounds)
and (cur_dock_info.tab_bounds == dock_info.tab_bounds)
):
return
# Make sure the new DockInfo is legal:
region = self.parent
if (
(not event.ControlDown())
and (dock_info.region is region)
and (
(len(region.contents) <= 1)
or (DOCK_TAB <= dock_info.kind <= DOCK_BAR)
and (dock_info.control is self)
)
):
self._dock_info = no_dock_info
window.owner.set_cursor(wx.CURSOR_SIZING)
return
# Draw the new region:
dock_info.draw(window, self._drag_bitmap)
# If this is the start of an export (i.e. drag and drop) request:
if (
(dock_info.kind == DOCK_EXPORT)
and (self.export != "")
and (self.dockable is not None)
):
# Begin the drag and drop operation:
self.mark_bounds(False)
window.owner.set_cursor(wx.CURSOR_ARROW)
window.owner.release_mouse()
try:
window._dragging = True
if PythonDropSource(window, self).result in (
wx.DragNone,
wx.DragCancel,
):
window.owner.handler.open_view_for(self)
finally:
window._dragging = False
else:
# Update the mouse pointer as required:
cursor = wx.CURSOR_SIZING
if dock_info.kind == DOCK_BAR:
cursor = wx.CURSOR_HAND
window.owner.set_cursor(cursor)
# ---------------------------------------------------------------------------
# Handles the mouse hovering over the item:
# ---------------------------------------------------------------------------
def hover_enter(self, event):
""" Handles the mouse hovering over the item.
"""
if self._is_tab and (self.tab_state != TabActive):
self._redraw_tab(TabHover)
# ---------------------------------------------------------------------------
# Handles the mouse exiting from hovering over the item:
# ---------------------------------------------------------------------------
def hover_exit(self, event):
""" Handles the mouse exiting from hovering over the item.
"""
if self._is_tab and (self.tab_state != TabActive):
self._redraw_tab(TabInactive)
# ---------------------------------------------------------------------------
# Marks/Unmarks the bounds of the bounding DockWindow:
# ---------------------------------------------------------------------------
def mark_bounds(self, begin):
""" Marks/Unmarks the bounds of the bounding DockWindow.
"""
window = self.control.GetParent()
if begin:
dc, x, y = get_dc(window)
dx, dy = window.GetSize().Get()
dc2 = wx.MemoryDC()
self._drag_bitmap = wx.Bitmap(dx, dy)
dc2.SelectObject(self._drag_bitmap)
dc2.Blit(0, 0, dx, dy, dc, x, y)
try:
dc3 = wx.GCDC(dc2)
dc3.SetBrush(wx.Brush(wx.Colour(158, 166, 255, 64)))
dc3.SetPen(wx.TRANSPARENT_PEN)
dc3.DrawRectangle(0, 0, dx, dy)
except AttributeError:
pass
dc.Blit(x, y, dx, dy, dc2, 0, 0)
else:
self._drag_bitmap = None
if is_mac:
top_level_window_for(window).Refresh()
else:
window.Refresh()
def get_bg_color(self):
""" Gets the background color
"""
color = SystemMetrics().dialog_background_color
return wx.Colour(
int(color[0] * 255), int(color[1] * 255), int(color[2] * 255)
)
# ---------------------------------------------------------------------------
# Fills a specified region with the control's background color:
# ---------------------------------------------------------------------------
def fill_bg_color(self, dc, x, y, dx, dy):
""" Fills a specified region with the control's background color.
"""
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(wx.Brush(self.get_bg_color()))
dc.DrawRectangle(x, y, dx, dy)
# ---------------------------------------------------------------------------
# Draws a notebook tab:
# ---------------------------------------------------------------------------
def draw_tab(self, dc, state):
global text_dy
""" Draws a notebook tab.
"""
x0, y0, dx, dy = self.drag_bounds
tab_color = self.get_bg_color()
if state == TabActive:
pass
elif state == TabInactive:
r, g, b = tab_color.Get()[0:3]
tab_color.Set(max(0, r - 20), max(0, g - 20), max(0, b - 20))
else:
r, g, b = tab_color.Get()[0:3]
tab_color.Set(min(255, r + 20), min(255, g + 20), min(255, b + 20))
self._is_tab = True
self.tab_state = state
theme = self.tab_theme
slice = theme.image_slice
bdc = BufferDC(dc, dx, dy)
self.fill_bg_color(bdc, 0, 0, dx, dy)
if state == TabActive:
# fill the tab bg with the desired color
brush = wx.Brush(tab_color)
bdc.SetBrush(brush)
bdc.SetPen(wx.TRANSPARENT_PEN)
bdc.DrawRectangle(0, 0, dx, dy)
# Draw the left, top, and right side of a rectange around the tab
pen = wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNSHADOW))
bdc.SetPen(pen)
bdc.DrawLine(0, dy, 0, 0) # up
bdc.DrawLine(0, 0, dx, 0) # right
bdc.DrawLine(dx - 1, 0, dx - 1, dy) # down
pen = wx.Pen(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT)
)
bdc.SetPen(pen)
bdc.DrawLine(1, dy, 1, 1)
bdc.DrawLine(1, 1, dx - 2, 1)
bdc.DrawLine(dx - 2, 1, dx - 2, dy)
else:
# fill the tab bg with the desired color
brush = wx.Brush(tab_color)
bdc.SetBrush(brush)
bdc.SetPen(wx.TRANSPARENT_PEN)
bdc.DrawRectangle(0, 3, dx, dy)
# Draw the left, top, and right side of a rectange around the tab
pen = wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNSHADOW))
bdc.SetPen(pen)
bdc.DrawLine(0, dy, 0, 3)
bdc.DrawLine(0, 3, dx - 1, 3)
bdc.DrawLine(dx - 1, 3, dx - 1, dy)
# Compute the initial drawing position:
name = self.tab_name
tdx, text_dy = dc.GetTextExtent(name)
tc = theme.content
ox, oy = theme.label.left, theme.label.top
y = oy + (
(dy + slice.xtop + tc.top - slice.xbottom - tc.bottom - text_dy)
// 2
)
x = ox + slice.xleft + tc.left
mode = self.feature_mode
if mode == FEATURE_PRE_NORMAL:
mode = self.set_feature_mode(False)
# Draw the feature 'trigger' icon (if necessary):
if mode != FEATURE_NONE:
if mode not in FEATURES_VISIBLE:
bdc.DrawBitmap(DockImages.get_feature_image(mode), x, y, True)
x += DockImages._tab_feature_width + 3
# Draw the image (if necessary):
image = self.get_image()
if image is not None:
bdc.DrawBitmap(image, x, y, True)
x += image.GetWidth() + 3
# Draw the text label:
bdc.DrawText(name, x, y + 1)
# Draw the close button (if necessary):
if self.closeable:
bdc.DrawBitmap(DockImages._close_tab, x + tdx + 5, y + 2, True)
# Copy the buffer to the display:
bdc.copy(x0, y0)
# ---------------------------------------------------------------------------
# Draws a fixed drag bar:
# ---------------------------------------------------------------------------
def draw_fixed(self, dc):
""" Draws a fixed drag bar.
"""
pass
# ---------------------------------------------------------------------------
# Draws a horizontal drag bar:
# ---------------------------------------------------------------------------
def draw_horizontal(self, dc):
""" Draws a horizontal drag bar.
"""
self._is_tab = False
x, y, dx, dy = self.drag_bounds
self.fill_bg_color(dc, x, y, dx, dy)
pen = wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNHILIGHT))
dc.SetPen(pen)
dc.DrawLine(x, y, x + dx, y)
dc.DrawLine(x, y + 2, x + dx, y + 2)
# ---------------------------------------------------------------------------
# Draws a vertical drag bar:
# ---------------------------------------------------------------------------
def draw_vertical(self, dc):
""" Draws a vertical drag bar.
"""
self._is_tab = False
x, y, dx, dy = self.drag_bounds
self.fill_bg_color(dc, x, y, dx, dy)
pen = wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNHILIGHT))
dc.SetPen(pen)
dc.DrawLine(x, y, x, y + dy)
dc.DrawLine(x + 2, y, x + 2, y + dy)
# ---------------------------------------------------------------------------
# Redraws the control's tab:
# ---------------------------------------------------------------------------
def _redraw_tab(self, state=None):
if state is None:
state = self.tab_state
region = self.parent
if region is not None:
dc = set_standard_font(wx.ClientDC(self.control.GetParent()))
if region.is_notebook:
dc.SetClippingRegion(*region._tab_clip_bounds)
self.draw_tab(dc, state)
dc.DestroyClippingRegion()
else:
self.draw_tab(dc, state)
# ---------------------------------------------------------------------------
# Redraws the control's drag bar:
# ---------------------------------------------------------------------------
def _redraw_bar(self):
dc = wx.ClientDC(self.control)
getattr(self, "draw_" + self.style)(dc)
# ---------------------------------------------------------------------------
# Redraws the control's tab or bar:
# ---------------------------------------------------------------------------
def _redraw_control(self):
if self._is_tab:
self._redraw_tab()
else:
self._redraw_bar()
# ---------------------------------------------------------------------------
# Returns the bounds of the close button (if any):
# ---------------------------------------------------------------------------
def _close_bounds(self):
global text_dy
if self.closeable and self._is_tab:
x, y, dx, dy = self.drag_bounds
theme = self.tab_theme
slice = theme.image_slice
tc = theme.content
ox, oy = theme.label.left, theme.label.top
# fixme: x calculation seems to be off by -1...
return (
x + dx + ox - slice.xright - tc.right - CloseTabSize,
y
+ oy
+ (
(
dy
+ slice.xtop
+ tc.top
- slice.xbottom
- tc.bottom
- text_dy
)
// 2
)
+ 3,
CloseTabSize,
CloseTabSize,
)
return (0, 0, 0, 0)
# ---------------------------------------------------------------------------
# Returns whether a specified window position is over the close button:
# ---------------------------------------------------------------------------
def _is_in_close(self, event):
return self.is_in(event, *self._close_bounds())
# ---------------------------------------------------------------------------
# Sets/Returns the 'normal' feature mode for the control based on the
# number of currently active features:
# ---------------------------------------------------------------------------
def set_feature_mode(self, changed=True):
if (not changed) or (self.feature_mode != FEATURE_PRE_NORMAL):
mode = FEATURE_DROP
features = self.drop_features
if len(features) == 0:
mode = FEATURE_NORMAL
features = self.features
for feature in features:
if feature.bitmap is not None:
if changed:
self.feature_mode = FEATURE_CHANGED
else:
self.feature_mode = mode
break
else:
self.feature_mode = FEATURE_DISABLED
return self.feature_mode
# ---------------------------------------------------------------------------
# Returns whether or not a specified window position is over the feature
# 'trigger' icon, and if so, triggers display of the feature icons:
# ---------------------------------------------------------------------------
def feature_activate(self, event, drag_object=Undefined):
global text_dy
if (self.feature_mode in NO_FEATURE_ICON) or (not self._is_tab):
return False
# In 'drag' mode, we may get the same coordinate over and over again.
# We don't want to restart the timer, so exit now:
exy = (event.GetX(), event.GetY())
if self._feature_popup_xy == exy:
return True
x, y, dx, dy = self.drag_bounds
idx = DockImages._tab_feature_width
idy = DockImages._tab_feature_height
theme = self.tab_theme
slice = theme.image_slice
tc = theme.content
ox, oy = theme.label.left, theme.label.top
y += oy + (
(dy + slice.xtop + tc.top - slice.xbottom - tc.bottom - text_dy)
// 2
)
x += ox + slice.xleft + tc.left
result = self.is_in(event, x, y, idx, idy)
# If the pointer is over the feature 'trigger' icon, save the event for
# the popup processing:
if result:
# If this is part of a drag operation, prepare for drag mode:
if drag_object is not Undefined:
self.pre_drag(drag_object, FEATURE_EXTERNAL_DRAG)
# Schedule the popup for later:
self._feature_popup_xy = exy
do_after(100, self._feature_popup)
return result
# ---------------------------------------------------------------------------
# Resets any pending feature popup:
# ---------------------------------------------------------------------------
def reset_feature_popup(self):
self._feature_popup_xy = None
# ---------------------------------------------------------------------------
# Pops up the current features if a feature popup is still pending:
# ---------------------------------------------------------------------------
def _feature_popup(self):
if self._feature_popup_xy is not None:
# Set the new feature mode:
if self.feature_mode == FEATURE_DROP:
self.feature_mode = FEATURE_DROP_VISIBLE
else:
self.feature_mode = FEATURE_VISIBLE
self.owner.feature_bar_popup(self)
self._feature_popup_xy = None
else:
self.post_drag(FEATURE_EXTERNAL_DRAG)
# ---------------------------------------------------------------------------
# Finishes the processing of a feature popup:
# ---------------------------------------------------------------------------
def feature_bar_closed(self):
if self.feature_mode == FEATURE_DROP_VISIBLE:
self.feature_mode = FEATURE_DROP
else:
self.feature_mode = FEATURE_NORMAL
do_later(self._redraw_control)
# ---------------------------------------------------------------------------
# Handles all pre-processing before a feature is dragged:
# ---------------------------------------------------------------------------
def pre_drag_all(self, object):
""" Prepare all DockControls in the associated DockWindow for being
dragged over.
"""
for control in self.dock_controls:
control.pre_drag(object)
self.pre_drag(object)
def pre_drag(self, object, tag=0):
""" Prepare this DockControl for being dragged over.
"""
if (
self.visible
and (self.feature_mode != FEATURE_NONE)
and (self._feature_mode is None)
):
if isinstance(object, IFeatureTool):
if object.feature_can_drop_on(
self.object
) or object.feature_can_drop_on_dock_control(self):
from .feature_tool import FeatureTool
self.drop_features = [FeatureTool(dock_control=self)]
else:
self.drop_features = [
f
for f in self.features
if f.can_drop(object) and (f.bitmap is not None)
]
self._feature_mode = self.feature_mode + tag
if len(self.drop_features) > 0:
self.feature_mode = FEATURE_DROP
else:
self.feature_mode = FEATURE_DISABLED
self._redraw_control()
# ---------------------------------------------------------------------------
# Handles all post-processing after a feature has been dragged:
# ---------------------------------------------------------------------------
def post_drag_all(self):
""" Restore all DockControls in the associated DockWindow after a drag
operation is completed.
"""
for control in self.dock_controls:
control.post_drag()
self.post_drag()
def post_drag(self, tag=0):
""" Restore this DockControl after a drag operation is completed.
"""
if (
(self._feature_mode is None)
or (tag == 0)
or ((self._feature_mode & tag) != 0)
):
self.drop_features = []
if self.feature_mode != FEATURE_NONE:
if self._feature_mode is not None:
self.feature_mode = self._feature_mode & (~tag)
self._feature_mode = None
else:
self.set_feature_mode(False)
self._redraw_control()
# -------------------------------------------------------------------------------
# 'DockSplitter' class:
# -------------------------------------------------------------------------------
class DockSplitter(DockItem):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# Style of the splitter bar:
style = Enum("horizontal", "vertical")
# Index of the splitter within its parent:
index = Int()
# Current state of the splitter (i.e. its position relative to the things
# it splits):
state = Property
# ---------------------------------------------------------------------------
# Override the definition of the inherited 'theme' property:
# ---------------------------------------------------------------------------
def _get_theme(self):
return self.parent.control.GetParent().owner.theme
# ---------------------------------------------------------------------------
# Draws the contents of the splitter:
# ---------------------------------------------------------------------------
def draw(self, dc):
""" Draws the contents of the splitter.
"""
if (self._live_drag is False) and (self._first_bounds is not None):
x, y, dx, dy = self._first_bounds
else:
x, y, dx, dy = self.bounds
image = DockImages.get_splitter_image(self.state)
idx, idy = image.GetWidth(), image.GetHeight()
self.fill_bg_color(dc, x, y, dx, dy)
if self.style == "horizontal":
# Draw a line the same color as the system button shadow, which
# should be a darkish color in the users color scheme
pen = wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNSHADOW))
dc.SetPen(pen)
dc.DrawLine(x + idx + 1, y + dy // 2, x + dx - 2, y + dy // 2)
iy = y + 2
ix = x
# sets the hittable area for changing the cursor to be the height of
# the image
dx = idx
else:
# Draw a line the same color as the system button shadow, which
# should be a darkish color in the users color scheme
pen = wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNSHADOW))
dc.SetPen(pen)
dc.DrawLine(x + dx // 2, y + idy + 1, x + dx // 2, y + dy - 2)
iy = y
ix = x + 2
# sets the hittable area for changing the cursor to be the width of
# the image
dy = idy
dc.DrawBitmap(image, ix, iy, True)
self._hot_spot = (x, y, dx, dy)
# ---------------------------------------------------------------------------
# Gets the cursor to use when the mouse is over the splitter bar:
# ---------------------------------------------------------------------------
def get_cursor(self, event):
""" Gets the cursor to use when the mouse is over the splitter bar.
"""
if (self._hot_spot is None) or self.is_in(event, *self._hot_spot):
return wx.CURSOR_ARROW
if self.style == "horizontal":
return wx.CURSOR_SIZENS
return wx.CURSOR_SIZEWE
# ---------------------------------------------------------------------------
# Returns a copy of the splitter 'structure', minus the actual content:
# ---------------------------------------------------------------------------
def get_structure(self):
""" Returns a copy of the splitter 'structure', minus the actual
content.
"""
return self.clone_traits(["_last_bounds"])
# ---------------------------------------------------------------------------
# Handles the left mouse button being pressed:
# ---------------------------------------------------------------------------
def mouse_down(self, event):
""" Handles the left mouse button being pressed.
"""
self._live_drag = event.ControlDown()
self._click_pending = (self._hot_spot is not None) and self.is_in(
event, *self._hot_spot
)
if not self._click_pending:
self._xy = (event.GetX(), event.GetY())
self._max_bounds = self.parent.get_splitter_bounds(self)
self._first_bounds = self.bounds
if not self._live_drag:
self._draw_bounds(event, self.bounds)
# ---------------------------------------------------------------------------
# Handles the left mouse button being released:
# ---------------------------------------------------------------------------
def mouse_up(self, event):
""" Handles the left mouse button being released.
"""
if self._click_pending:
hx, hy, hdx, hdy = self._hot_spot
if not self.is_in(event, hx, hy, hdx, hdy):
return
if self.style == "horizontal":
if event.GetX() < (hx + (hdx / 2)):
self.collapse(True)
else:
self.collapse(False)
else:
if event.GetY() < (hy + (hdy / 2)):
self.collapse(True)
else:
self.collapse(False)
else:
self._last_bounds, self._first_bounds = self._first_bounds, None
if not self._live_drag:
self._draw_bounds(event)
self.parent.update_splitter(self, event.GetEventObject())
# ---------------------------------------------------------------------------
# Handles the mouse moving while the left mouse button is pressed:
# ---------------------------------------------------------------------------
def mouse_move(self, event):
""" Handles the mouse moving while the left mouse button is pressed.
"""
if not self._click_pending:
if self._first_bounds is not None:
x, y, dx, dy = self._first_bounds
mx, my, mdx, mdy = self._max_bounds
if self.style == "horizontal":
y = y + event.GetY() - self._xy[1]
y = min(max(y, my), my + mdy - dy)
else:
x = x + event.GetX() - self._xy[0]
x = min(max(x, mx), mx + mdx - dx)
bounds = (x, y, dx, dy)
if bounds != self.bounds:
self.bounds = bounds
if self._live_drag:
self.parent.update_splitter(self, event.GetEventObject())
else:
self._draw_bounds(event, bounds)
# ---------------------------------------------------------------------------
# Collapse/expands a splitter
# ---------------------------------------------------------------------------
def collapse(self, forward):
""" Move the splitter has far as possible in one direction. 'forward'
is a boolean: True=right/down, False=left/up.
If the splitter is already collapsed, restores it to its previous
position.
"""
is_horizontal = self.style == "horizontal"
x, y, dx, dy = self.bounds
if self._last_bounds is not None:
if is_horizontal:
y = self._last_bounds[1]
else:
x = self._last_bounds[0]
state = self.state
contents = self.parent.visible_contents
ix1, iy1, idx1, idy1 = contents[self.index].bounds
ix2, iy2, idx2, idy2 = contents[self.index + 1].bounds
if is_horizontal:
if state != SPLIT_HMIDDLE:
if (
(y == self.bounds[1])
or (y < iy1)
or ((y + dy) > (iy2 + idy2))
):
y = (iy1 + iy2 + idy2 - dy) // 2
else:
self._last_bounds = self.bounds
if forward:
y = iy1
else:
y = iy2 + idy2 - dy
elif state != SPLIT_VMIDDLE:
if (x == self.bounds[0]) or (x < ix1) or ((x + dx) > (ix2 + idx2)):
x = (ix1 + ix2 + idx2 - dx) // 2
else:
self._last_bounds = self.bounds
if forward:
x = ix2 + idx2 - dx
else:
x = ix1
self.bounds = (x, y, dx, dy)
# ---------------------------------------------------------------------------
# Handles the mouse hovering over the item:
# ---------------------------------------------------------------------------
def hover_enter(self, event):
""" Handles the mouse hovering over the item.
"""
pass
# ---------------------------------------------------------------------------
# Handles the mouse exiting from hovering over the item:
# ---------------------------------------------------------------------------
def hover_exit(self, event):
""" Handles the mouse exiting from hovering over the item.
"""
pass
# ---------------------------------------------------------------------------
# Draws the splitter bar in a new position while it is being dragged:
# ---------------------------------------------------------------------------
def _draw_bounds(self, event, bounds=None):
""" Draws the splitter bar in a new position while it is being dragged.
"""
# Set up the drawing environment:
window = event.GetEventObject()
dc, x0, y0 = get_dc(window)
dc.SetLogicalFunction(wx.XOR)
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(wx.Brush(wx.Colour(*DragColor), wx.SOLID))
is_horizontal = self.style == "horizontal"
nx = ox = None
# Draw the new bounds (if any):
if bounds is not None:
ax = ay = adx = ady = 0
nx, ny, ndx, ndy = bounds
if is_horizontal:
ady = ndy - 6
ay = ady // 2
else:
adx = ndx - 6
ax = adx // 2
nx += ax
ny += ay
ndx -= adx
ndy -= ady
if self._bounds is not None:
ax = ay = adx = ady = 0
ox, oy, odx, ody = self._bounds
if is_horizontal:
ady = ody - 6
ay = ady // 2
else:
adx = odx - 6
ax = adx // 2
ox += ax
oy += ay
odx -= adx
ody -= ady
if nx is not None:
tx, ty, tdx, tdy = nx, ny, ndx, ndy
if ox is not None:
if is_horizontal:
yoy = oy - ty
if 0 <= yoy < tdy:
tdy = yoy
elif -ody < yoy <= 0:
ty = oy + ody
tdy = tdy - ody - yoy
else:
xox = ox - tx
if 0 <= xox < tdx:
tdx = xox
elif -odx < xox <= 0:
tx = ox + odx
tdx = tdx - odx - xox
dc.DrawRectangle(tx + x0, ty + y0, tdx, tdy)
# Erase the old bounds (if any):
if ox is not None:
if nx is not None:
if is_horizontal:
yoy = ny - oy
if 0 <= yoy < ody:
ody = yoy
elif -ndy < yoy <= 0:
oy = ny + ndy
ody = ody - ndy - yoy
else:
xox = nx - ox
if 0 <= xox < odx:
odx = xox
elif -ndx < xox <= 0:
ox = nx + ndx
odx = odx - ndx - xox
dc.DrawRectangle(ox + x0, oy + y0, odx, ody)
if is_mac:
window.Refresh(rect=wx.Rect(ox + x0, oy + y0, odx, ody))
# Save the new bounds for the next call:
self._bounds = bounds
# ---------------------------------------------------------------------------
# Implementation of the 'state' property:
# ---------------------------------------------------------------------------
def _get_state(self):
contents = self.parent.contents
x, y, dx, dy = self.bounds
ix1, iy1, idx1, idy1 = contents[self.index].bounds
ix2, iy2, idx2, idy2 = contents[self.index + 1].bounds
if self.style == "horizontal":
if y == iy1:
return SPLIT_HTOP
if (y + dy) == (iy2 + idy2):
return SPLIT_HBOTTOM
return SPLIT_HMIDDLE
else:
if x == ix1:
return SPLIT_VLEFT
if (x + dx) == (ix2 + idx2):
return SPLIT_VRIGHT
return SPLIT_VMIDDLE
# -------------------------------------------------------------------------------
# 'DockControl' class:
# -------------------------------------------------------------------------------
class DockControl(DockItem):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# The control this object describes:
control = Instance(wx.Window, allow_none=True)
# The number of global DockWindowFeature's that were available the last
# the time the feature set was checked:
num_features = Int()
# A feature associated with the DockControl has been changed:
feature_changed = Event()
# The image to display for this control:
image = Image()
# The UI name of this control:
name = Str()
# Has the user set the name of the control?
user_name = Bool(False)
# The object (if any) associated with this control:
object = Property
# The id of this control:
id = Str()
# Style of drag bar/tab:
style = DockStyle
# Has the user set the style for this control:
user_style = Bool(False)
# Category of control when it is dragged out of the DockWindow:
export = Str()
# Is the control visible?
visible = Bool(True)
# Is the control's drag bar locked?
locked = Bool(False)
# Can the control be resized?
resizable = Bool(True)
# Can the control be closed?
closeable = Bool(False)
# Function to call when a DockControl is requesting to be closed:
on_close = Callable
# (Optional) object that allows the control to be docked with a different
# DockWindow:
dockable = Instance(IDockable, allow_none=True)
# List of all other DockControl's in the same DockWindow:
dock_controls = Property
# Event fired when the control's notebook tab is activated by the user:
activated = Event()
# ---------------------------------------------------------------------------
# Calculates the minimum size of the control:
# ---------------------------------------------------------------------------
def calc_min(self, use_size=False):
""" Calculates the minimum size of the control.
"""
self.check_features()
dx, dy = self.width, self.height
if self.control is not None:
size = self.control.GetEffectiveMinSize()
dx = size.GetWidth()
dy = size.GetHeight()
if self.width < 0:
self.width, self.height = dx, dy
if use_size and (self.width >= 0):
return (self.width, self.height)
return (dx, dy)
# ---------------------------------------------------------------------------
# Layout the contents of the control based on the specified bounds:
# ---------------------------------------------------------------------------
def recalc_sizes(self, x, y, dx, dy):
""" Layout the contents of the region based on the specified bounds.
"""
self.width = dx = max(0, dx)
self.height = dy = max(0, dy)
self.bounds = (x, y, dx, dy)
# Note: All we really want to do is the 'SetSize' call, but the
# other code is needed for Linux/GTK which will not correctly process
# the SetSize call if the min size is larger than the specified
# size. So we temporarily set its min size to (0,0), do the
# SetSize, then restore the original min size. The restore is
# necessary so that DockWindow itself will correctly draw the 'drag'
# box when performing a docking maneuver...
control = self.control
min_size = control.GetMinSize()
control.SetMinSize(wx.Size(0, 0))
control.SetSize(x, y, dx, dy)
control.SetMinSize(min_size)
# ---------------------------------------------------------------------------
# Checks to make sure that all applicable DockWindowFeatures have been
# applied:
# ---------------------------------------------------------------------------
def check_features(self):
""" Checks to make sure that all applicable DockWindowFeatures have been
applied.
"""
global features
mode = self.feature_mode
n = len(features)
if (
(self.num_features < n)
and (self.control is not None)
and isinstance(self.control.GetParent().GetSizer(), DockSizer)
):
for i in range(self.num_features, n):
feature_class = features[i]
feature = feature_class.new_feature_for(self)
if feature is not None:
if not isinstance(feature, SequenceType):
feature = [feature]
self.features.extend(list(feature))
if mode == FEATURE_NONE:
self.feature_mode = FEATURE_PRE_NORMAL
if feature_class.state != 1:
for item in feature:
item.disable()
else:
self._tab_width = None
if mode in NORMAL_FEATURES:
self.set_feature_mode()
self.num_features = n
# ---------------------------------------------------------------------------
# Sets the visibility of the control:
# ---------------------------------------------------------------------------
def set_visibility(self, visible):
""" Sets the visibility of the control.
"""
if self.control is not None:
self.control.Show(visible)
# ---------------------------------------------------------------------------
# Returns all DockControl objects contained in the control:
# ---------------------------------------------------------------------------
def get_controls(self, visible_only=True):
""" Returns all DockControl objects contained in the control.
"""
if visible_only and (not self.visible):
return []
return [self]
# ---------------------------------------------------------------------------
# Gets the image (if any) associated with the control:
# ---------------------------------------------------------------------------
def get_image(self):
""" Gets the image (if any) associated with the control.
"""
if self._image is None:
if self.image is not None:
self._image = self.image.create_image().ConvertToBitmap()
return self._image
# ---------------------------------------------------------------------------
# Hides or shows the control:
# ---------------------------------------------------------------------------
def show(self, visible=True, layout=True):
""" Hides or shows the control.
"""
if visible != self.visible:
self.visible = visible
self._layout(layout)
# ---------------------------------------------------------------------------
# Activates a control (i.e. makes it the active page within its containing
# notebook):
# ---------------------------------------------------------------------------
def activate(self, layout=True):
""" Activates a control (i.e. makes it the active page within its
containing notebook).
"""
if self.parent is not None:
self.parent.activate(self, layout)
# ---------------------------------------------------------------------------
# Closes the control:
# ---------------------------------------------------------------------------
def close(self, layout=True, force=False):
""" Closes the control.
"""
control = self.control
if control is not None:
window = control.GetParent()
if self.on_close is not None:
# Ask the handler if it is OK to close the control:
if self.on_close(self, force) is False:
# If not OK to close it, we're done:
return
elif self.dockable is not None:
# Ask the IDockable handler if it is OK to close the control:
if self.dockable.dockable_close(self, force) is False:
# If not OK to close it, we're done:
return
else:
# No close handler, just destroy the widget ourselves:
control.Destroy()
# Reset all features:
self.reset_features()
# Remove the DockControl from the sizer:
self.parent.remove(self)
# Mark the DockControl as closed (i.e. has no associated widget or
# parent):
self.control = self.parent = None
# If a screen update is requested, lay everything out again now:
if layout:
window.Layout()
window.Refresh()
# ---------------------------------------------------------------------------
# Returns the object at a specified window position:
# ---------------------------------------------------------------------------
def object_at(self, x, y):
""" Returns the object at a specified window position.
"""
return None
# ---------------------------------------------------------------------------
# Returns a copy of the control 'structure', minus the actual content:
# ---------------------------------------------------------------------------
def get_structure(self):
""" Returns a copy of the control 'structure', minus the actual content.
"""
return self.clone_traits(
[
"id",
"name",
"user_name",
"style",
"user_style",
"visible",
"locked",
"closeable",
"resizable",
"width",
"height",
]
)
# ---------------------------------------------------------------------------
# Toggles the 'lock' status of the control:
# ---------------------------------------------------------------------------
def toggle_lock(self):
""" Toggles the 'lock' status of the control.
"""
self.locked = not self.locked
# ---------------------------------------------------------------------------
# Prints the contents of the control:
# ---------------------------------------------------------------------------
def dump(self, indent):
""" Prints the contents of the control.
"""
print(
(
"%sControl( %08X, name = %s, id = %s,\n%s"
"style = %s, locked = %s,\n%s"
"closeable = %s, resizable = %s, visible = %s\n%s"
"width = %d, height = %d )"
% (
" " * indent,
id(self),
self.name,
self.id,
" " * (indent + 9),
self.style,
self.locked,
" " * (indent + 9),
self.closeable,
self.resizable,
self.visible,
" " * (indent + 9),
self.width,
self.height,
)
)
)
# ---------------------------------------------------------------------------
# Draws the contents of the control:
# ---------------------------------------------------------------------------
def draw(self, dc):
""" Draws the contents of the control.
"""
pass
# ---------------------------------------------------------------------------
# Sets a new name for the control:
# ---------------------------------------------------------------------------
def set_name(self, name, layout=True):
""" Sets a new name for the control.
"""
if name != self.name:
self.name = name
self._layout(layout)
# ---------------------------------------------------------------------------
# Resets the state of the tab:
# ---------------------------------------------------------------------------
def reset_tab(self):
""" Resets the state of the tab.
"""
self.reset_features()
self._layout()
# ---------------------------------------------------------------------------
# Resets all currently defined features:
# ---------------------------------------------------------------------------
def reset_features(self):
""" Resets all currently defined features.
"""
for feature in self.features:
feature.dispose()
self.features = []
self.num_features = 0
# ---------------------------------------------------------------------------
# Forces the containing DockWindow to be laid out:
# ---------------------------------------------------------------------------
def _layout(self, layout=True):
""" Forces the containing DockWindow to be laid out.
"""
if layout and (self.control is not None):
do_later(self.control.GetParent().owner.update_layout)
# ---------------------------------------------------------------------------
# Handles the 'activated' event being fired:
# ---------------------------------------------------------------------------
@observe('activated')
def _activate_dockable_tab(self, event):
""" Notifies the active dockable that the control's tab is being
activated.
"""
if self.dockable is not None:
self.dockable.dockable_tab_activated(self, True)
# ---------------------------------------------------------------------------
# Handles the 'feature_changed' trait being changed:
# ---------------------------------------------------------------------------
@observe("feature_changed")
def _feature_changed_updated(self, event):
""" Handles the 'feature_changed' trait being changed
"""
self.set_feature_mode()
# ---------------------------------------------------------------------------
# Handles the 'control' trait being changed:
# ---------------------------------------------------------------------------
@observe("control")
def _control_updated(self, event):
""" Handles the 'control' trait being changed.
"""
old, new = event.old, event.new
self._tab_width = None
if old is not None:
old._dock_control = None
if new is not None:
new._dock_control = self
self.reset_tab()
# ---------------------------------------------------------------------------
# Handles the 'name' trait being changed:
# ---------------------------------------------------------------------------
@observe("name")
def _name_updated(self, event):
""" Handles the 'name' trait being changed.
"""
self._tab_width = self._tab_name = None
# ---------------------------------------------------------------------------
# Handles the 'style' trait being changed:
# ---------------------------------------------------------------------------
@observe("style")
def _style_updated(self, event):
""" Handles the 'style' trait being changed.
"""
if self.parent is not None:
self.parent._is_notebook = None
# ---------------------------------------------------------------------------
# Handles the 'image' trait being changed:
# ---------------------------------------------------------------------------
@observe("image")
def _image_updated(self, event):
""" Handles the 'image' trait being changed.
"""
self._image = None
# ---------------------------------------------------------------------------
# Handles the 'visible' trait being changed:
# ---------------------------------------------------------------------------
@observe("visible")
def _visible_updated(self, event):
""" Handles the 'visible' trait being changed.
"""
if self.parent is not None:
self.parent.show_hide(self)
# ---------------------------------------------------------------------------
# Handles the 'dockable' trait being changed:
# ---------------------------------------------------------------------------
@observe("dockable")
def _dockable_updated(self, event):
""" Handles the 'dockable' trait being changed.
"""
dockable = event.new
if dockable is not None:
dockable.dockable_bind(self)
# ---------------------------------------------------------------------------
# Implementation of the 'object' property:
# ---------------------------------------------------------------------------
def _get_object(self):
return getattr(self.control, "_object", None)
# ---------------------------------------------------------------------------
# Implementation of the DockControl's property:
# ---------------------------------------------------------------------------
def _get_dock_controls(self):
# Get all of the DockControls in the parent DockSizer:
controls = (
self.control.GetParent()
.GetSizer()
.GetContents()
.get_controls(False)
)
# Remove ourself from the list:
try:
controls.remove(self)
except:
pass
return controls
# -------------------------------------------------------------------------------
# 'DockGroup' class:
# -------------------------------------------------------------------------------
class DockGroup(DockItem):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# The contents of the group:
contents = List()
# The UI name of this group:
name = Property
# Style of drag bar/tab:
style = Property
# Are the contents of the group resizable?
resizable = Property
# Category of control when it is dragged out of the DockWindow:
export = Constant("")
# Is the group visible?
visible = Property
# Content items which are visible:
visible_contents = Property
# Can the control be closed?
closeable = Property
# The control associated with this group:
control = Property
# Is the group locked?
locked = Property
# Has the initial layout been performed?
initialized = Bool(False)
# ---------------------------------------------------------------------------
# Implementation of the 'name' property:
# ---------------------------------------------------------------------------
def _get_name(self):
controls = self.get_controls()
n = len(controls)
if n == 0:
return ""
if n == 1:
return controls[0].name
return "%s [%d]" % (controls[0].name, n)
# ---------------------------------------------------------------------------
# Implementation of the 'visible' property:
# ---------------------------------------------------------------------------
def _get_visible(self):
for item in self.contents:
if item.visible:
return True
return False
# ---------------------------------------------------------------------------
# Implementation of the 'visible_contents' property:
# ---------------------------------------------------------------------------
def _get_visible_contents(self):
return [item for item in self.contents if item.visible]
# ---------------------------------------------------------------------------
# Implementation of the 'closeable' property:
# ---------------------------------------------------------------------------
def _get_closeable(self):
for item in self.contents:
if not item.closeable:
return False
return True
# ---------------------------------------------------------------------------
# Implementation of the 'style' property:
# ---------------------------------------------------------------------------
def _get_style(self):
# Make sure there is at least one item in the group:
if len(self.contents) > 0:
# Return the first item's style:
return self.contents[0].style
# Otherwise, return a default style for an empty group:
return "horizontal"
# ---------------------------------------------------------------------------
# Implementation of the 'resizable' property:
# ---------------------------------------------------------------------------
def _get_resizable(self):
if self._resizable is None:
self._resizable = False
for control in self.get_controls():
if control.resizable:
self._resizable = True
break
return self._resizable
# ---------------------------------------------------------------------------
# Implementation of the 'control' property:
# ---------------------------------------------------------------------------
def _get_control(self):
if len(self.contents) == 0:
return None
return self.contents[0].control
# ---------------------------------------------------------------------------
# Implementation of the 'locked' property:
# ---------------------------------------------------------------------------
def _get_locked(self):
return self.contents[0].locked
# ---------------------------------------------------------------------------
# Handles 'initialized' being changed:
# ---------------------------------------------------------------------------
@observe("initialized")
def _initialized_updated(self, event):
""" Handles 'initialized' being changed.
"""
for item in self.contents:
if isinstance(item, DockGroup):
item.initialized = self.initialized
# ---------------------------------------------------------------------------
# Hides or shows the contents of the group:
# ---------------------------------------------------------------------------
def show(self, visible=True, layout=True):
""" Hides or shows the contents of the group.
"""
for item in self.contents:
item.show(visible, False)
if layout:
window = self.control.GetParent()
window.Layout()
window.Refresh()
# ---------------------------------------------------------------------------
# Replaces a specified DockControl by another:
# ---------------------------------------------------------------------------
def replace_control(self, old, new):
""" Replaces a specified DockControl by another.
"""
for i, item in enumerate(self.contents):
if isinstance(item, DockControl):
if item is old:
self.contents[i] = new
new.parent = self
return True
elif item.replace_control(old, new):
return True
return False
# ---------------------------------------------------------------------------
# Returns all DockControl objects contained in the group:
# ---------------------------------------------------------------------------
def get_controls(self, visible_only=True):
""" Returns all DockControl objects contained in the group.
"""
if visible_only:
contents = self.visible_contents
else:
contents = self.contents
result = []
for item in contents:
result.extend(item.get_controls(visible_only))
return result
# ---------------------------------------------------------------------------
# Gets the image (if any) associated with the group:
# ---------------------------------------------------------------------------
def get_image(self):
""" Gets the image (if any) associated with the group.
"""
if len(self.contents) == 0:
return None
return self.contents[0].get_image()
# ---------------------------------------------------------------------------
# Gets the cursor to use when the mouse is over the item:
# ---------------------------------------------------------------------------
def get_cursor(self, event):
""" Gets the cursor to use when the mouse is over the item.
"""
return wx.CURSOR_ARROW
# ---------------------------------------------------------------------------
# Toggles the 'lock' status of every control in the group:
# ---------------------------------------------------------------------------
def toggle_lock(self):
""" Toggles the 'lock' status of every control in the group.
"""
for item in self.contents:
item.toggle_lock()
# ---------------------------------------------------------------------------
# Closes the group:
# ---------------------------------------------------------------------------
def close(self, layout=True, force=False):
""" Closes the control.
"""
window = self.control.control.GetParent()
for item in self.contents[:]:
item.close(False, force=force)
if layout:
window.Layout()
window.Refresh()
# -------------------------------------------------------------------------------
# 'DockRegion' class:
# -------------------------------------------------------------------------------
class DockRegion(DockGroup):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# Index of the currently active 'contents' DockControl:
active = Int()
# Is the region drawn as a notebook or not:
is_notebook = Property
# Index of the tab scroll image to use (-1 = No tab scroll):
tab_scroll_index = Int(-1)
# The index of the current leftmost visible tab:
left_tab = Int()
# The current maximum value for 'left_tab':
max_tab = Int()
# Contents have been modified property:
modified = Property
# ---------------------------------------------------------------------------
# Calculates the minimum size of the region:
# ---------------------------------------------------------------------------
def calc_min(self, use_size=False):
""" Calculates the minimum size of the region.
"""
tab_dx = tdx = tdy = 0
contents = self.visible_contents
theme = self.theme
if self.is_notebook:
for item in contents:
dx, dy = item.calc_min(use_size)
tdx = max(tdx, dx)
tdy = max(tdy, dy)
tab_dx += item.tab_width
tis = theme.tab.image_slice
tc = theme.tab.content
tdx = max(tdx, tab_dx) + (
tis.xleft + tis.xright + tc.left + tc.right
)
tdy += (
theme.tab_active.image_slice.dy
+ tis.xtop
+ tis.xbottom
+ tc.top
+ tc.bottom
)
elif len(contents) > 0:
item = contents[0]
tdx, tdy = item.calc_min(use_size)
if not item.locked:
if item.style == "horizontal":
tdy += theme.horizontal_drag.image_slice.dy
elif item.style == "vertical":
tdx += theme.vertical_drag.image_slice.dx
if self.width < 0:
self.width = tdx
self.height = tdy
return (tdx, tdy)
# ---------------------------------------------------------------------------
# Layout the contents of the region based on the specified bounds:
# ---------------------------------------------------------------------------
def recalc_sizes(self, x, y, dx, dy):
""" Layout the contents of the region based on the specified bounds.
"""
self.width = dx = max(0, dx)
self.height = dy = max(0, dy)
self.bounds = (x, y, dx, dy)
theme = self.theme
contents = self.visible_contents
if self.is_notebook:
tis = theme.tab.image_slice
tc = theme.tab.content
th = theme.tab_active.image_slice.dy
# Layout the region out as a notebook:
x += tis.xleft + tc.left
tx0 = tx = x + theme.tab.label.left
dx -= tis.xleft + tis.xright + tc.left + tc.right
ady = dy - th
dy = ady - tis.xtop - tis.xbottom - tc.top - tc.bottom
iy = y + tis.xtop + tc.top
if theme.tabs_at_top:
iy += th
else:
y += ady
for item in contents:
item.recalc_sizes(x, iy, dx, dy)
tdx = item.tab_width
item.set_drag_bounds(tx, y, tdx, th)
tx += tdx
# Calculate the default tab clipping bounds:
cdx = dx + tc.left + tc.right
self._tab_clip_bounds = (tx0, y, cdx, th)
# Do we need to enable tab scrolling?
xr = tx0 + cdx
if tx > xr:
# Scrolling needed, calculate maximum tab index for scrolling:
self.max_tab = 1
n = len(contents) - 1
xr -= DockImages._tab_scroller_dx
for i in range(n, -1, -1):
xr -= contents[i].tab_width
if xr < tx0:
self.max_tab = min(i + 1, n)
break
# Set the new leftmost tab index:
self.left_tab = min(self.left_tab, self.max_tab)
# Determine which tab scroll image to use:
self.tab_scroll_index = (
(self.left_tab < self.max_tab) + (2 * (self.left_tab > 0))
) - 1
# Now adjust each tab's bounds accordingly:
if self.left_tab > 0:
adx = contents[self.left_tab].drag_bounds[0] - tx0
for item in contents:
dbx, dby, dbdx, dbdy = item.drag_bounds
item.set_drag_bounds(
dbx - adx, dby, item.tab_width, dbdy
)
# Exclude the scroll buttons from the tab clipping region:
self._tab_clip_bounds = (
tx0,
y,
cdx - DockImages._tab_scroller_dx,
th,
)
else:
self.tab_scroll_index = -1
self.left_tab = 0
else:
# Lay the region out as a drag bar:
item = contents[0]
drag_bounds = (0, 0, 0, 0)
if not item.locked:
if item.style == "horizontal":
db_dy = theme.horizontal_drag.image_slice.dy
drag_bounds = (x, y, dx, db_dy)
y += db_dy
dy -= db_dy
elif item.style == "vertical":
db_dx = theme.vertical_drag.image_slice.dx
drag_bounds = (x, y, db_dx, dy)
x += db_dx
dx -= db_dx
item.recalc_sizes(x, y, dx, dy)
item.set_drag_bounds(*drag_bounds)
# Make sure all of the contained controls have the right visiblity:
self._set_visibility()
# ---------------------------------------------------------------------------
# Adds a new control before or after a specified control:
# ---------------------------------------------------------------------------
def add(self, control, before=None, after=None, activate=True):
""" Adds a new control before a specified control.
"""
contents = self.contents
if control.parent is self:
contents.remove(control)
if before is None:
if after is None:
i = len(contents)
else:
i = contents.index(after) + 1
else:
i = contents.index(before)
contents.insert(i, control)
if activate:
self.active = i
# ---------------------------------------------------------------------------
# Removes a specified item:
# ---------------------------------------------------------------------------
def remove(self, item):
""" Removes a specified item.
"""
contents = self.contents
i = contents.index(item)
if isinstance(item, DockGroup) and (len(item.contents) == 1):
item = item.contents[0]
if isinstance(item, DockRegion):
contents[i:i + 1] = item.contents[:]
else:
contents[i] = item
else:
del contents[i]
# Change the active selection only if 'item' is in closing mode,
# or was dragged to a new location.
# If this entire dock region is being closed, then all contained
# dock items will be removed and we do not want to change 'active'
# selection.
if item._closing or item._dragging:
if (self.active > i) or (self.active >= len(contents)):
self.active -= 1
# If the active item was removed, then 'active' stays
# unchanged, but it reflects the index of the next page in
# the dock region. Since _active_changed won't be fired now,
# we fire the 'activated' event on the next page.
elif i == self.active:
control = self.contents[i]
if isinstance(control, DockControl):
control.activated = True
if self.parent is not None:
if len(contents) == 0:
self.parent.remove(self)
elif (len(contents) == 1) and isinstance(self.parent, DockRegion):
self.parent.remove(self)
# ---------------------------------------------------------------------------
# Returns a copy of the region 'structure', minus the actual content:
# ---------------------------------------------------------------------------
def get_structure(self):
""" Returns a copy of the region 'structure', minus the actual content.
"""
return self.clone_traits(["active", "width", "height"]).trait_set(
contents=[item.get_structure() for item in self.contents]
)
# ---------------------------------------------------------------------------
# Toggles the 'lock' status of every control in the group:
# ---------------------------------------------------------------------------
def toggle_lock(self):
""" Toggles the 'lock' status of every control in the group.
"""
super().toggle_lock()
self._is_notebook = None
# ---------------------------------------------------------------------------
# Draws the contents of the region:
# ---------------------------------------------------------------------------
def draw(self, dc):
""" Draws the contents of the region.
"""
if self._visible is not False:
self.begin_draw(dc)
if self.is_notebook:
# fixme: There seems to be a case where 'draw' is called before
# 'recalc_sizes' (which defines '_tab_clip_bounds'), so we need
# to check to make sure it is defined. If not, it seems safe to
# exit immediately, since in all known cases, the bounds are
# ( 0, 0, 0, 0 ), so there is nothing to draw anyways. The
# question is why 'recalc_sizes' is not being called first.
if self._tab_clip_bounds is None:
self.end_draw(dc)
return
self.fill_bg_color(dc, *self.bounds)
if self.active >= len(self.contents):
# on some platforms, if the active tab was destroyed
# the new active tab may not have been set yet
self.active = len(self.contents) - 1
self._draw_notebook(dc)
active = self.active
# Draw the scroll buttons (if necessary):
x, y, dx, dy = self._tab_clip_bounds
index = self.tab_scroll_index
if index >= 0:
dc.DrawBitmap(
DockImages._tab_scroller_images[index],
x + dx,
y + 2,
True,
)
# Draw all the inactive tabs first:
dc.SetClippingRegion(x, y, dx, dy)
last_inactive = -1
for i, item in enumerate(self.contents):
if (i != active) and item.visible:
last_inactive = i
state = item.tab_state
if state not in NotActiveStates:
state = TabInactive
item.draw_tab(dc, state)
# Draw the active tab last:
self.contents[active].draw_tab(dc, TabActive)
# If the last inactive tab drawn is also the rightmost tab and
# the theme has a 'tab right edge' image, draw the image just
# to the right of the last tab:
if last_inactive > active:
if item.tab_state == TabInactive:
bitmap = self.theme.tab_inactive_edge_bitmap
else:
bitmap = self.theme.tab_hover_edge_bitmap
if bitmap is not None:
x, y, dx, dy = item.drag_bounds
dc.DrawBitmap(bitmap, x + dx, y, True)
else:
item = self.visible_contents[0]
if not item.locked:
getattr(item, "draw_" + item.style)(dc)
self.end_draw(dc)
# Draw each of the items contained in the region:
for item in self.contents:
if item.visible:
item.draw(dc)
# ---------------------------------------------------------------------------
# Returns the object at a specified window position:
# ---------------------------------------------------------------------------
def object_at(self, x, y):
""" Returns the object at a specified window position.
"""
if (self._visible is not False) and self.is_at(x, y):
if self.is_notebook and (self.tab_scroll_index >= 0):
cx, cy, cdx, cdy = self._tab_clip_bounds
if self.is_at(
x,
y,
(
cx + cdx,
cy + 2,
DockImages._tab_scroller_dx,
DockImages._tab_scroller_dy,
),
):
return self
for item in self.visible_contents:
if item.is_at(x, y, item.drag_bounds):
return item
object = item.object_at(x, y)
if object is not None:
return object
return None
# ---------------------------------------------------------------------------
# Gets the DockInfo object for a specified window position:
# ---------------------------------------------------------------------------
def dock_info_at(self, x, y, tdx, is_control):
""" Gets the DockInfo object for a specified window position.
"""
# Check to see if the point is in our drag bar:
info = super().dock_info_at(x, y, tdx, is_control)
if info is not None:
return info
# If we are not visible, or the point is not contained in us, give up:
if (self._visible is False) or (not self.is_at(x, y)):
return None
# Check to see if the point is in the drag bars of any controls:
contents = self.visible_contents
for item in contents:
object = item.dock_info_at(x, y, tdx, is_control)
if object is not None:
return object
# If we are in 'notebook mode' check to see if the point is in the
# empty region outside of any tabs:
lx, ty, dx, dy = self.bounds
if self.is_notebook:
item = contents[-1]
ix, iy, idx, idy = item.drag_bounds
if (x > (ix + idx)) and (iy <= y < (iy + idy)):
return DockInfo(
kind=DOCK_TAB,
tab_bounds=(ix + idx, iy, tdx, idy),
region=self,
)
# Otherwise, figure out which edge the point is closest to, and
# return a DockInfo object describing that edge:
left = x - lx
right = lx + dx - 1 - x
top = y - ty
bottom = ty + dy - 1 - y
choice = min(left, right, top, bottom)
mdx = dx // 3
mdy = dy // 3
if choice == left:
return DockInfo(
kind=DOCK_LEFT, bounds=(lx, ty, mdx, dy), region=self
)
if choice == right:
return DockInfo(
kind=DOCK_RIGHT,
bounds=(lx + dx - mdx, ty, mdx, dy),
region=self,
)
if choice == top:
return DockInfo(
kind=DOCK_TOP, bounds=(lx, ty, dx, mdy), region=self
)
return DockInfo(
kind=DOCK_BOTTOM, bounds=(lx, ty + dy - mdy, dx, mdy), region=self
)
# ---------------------------------------------------------------------------
# Handles a contained notebook tab being clicked:
# ---------------------------------------------------------------------------
def tab_clicked(self, control):
""" Handles a contained notebook tab being clicked.
"""
# Find the page that was clicked and mark it as active:
i = self.contents.index(control)
if i != self.active:
self.active = i
# Recalculate the tab layout:
self.recalc_sizes(*self.bounds)
# Force the notebook to be redrawn:
control.control.GetParent().RefreshRect(wx.Rect(*self.bounds))
# Fire the 'activated' event on the control:
if isinstance(control, DockControl):
control.activated = True
# ---------------------------------------------------------------------------
# Handles the user clicking an active scroll button:
# ---------------------------------------------------------------------------
def scroll(self, type, left_tab=0):
""" Handles the user clicking an active scroll button.
"""
if type == SCROLL_LEFT:
left_tab = min(self.left_tab + 1, self.max_tab)
elif type == SCROLL_RIGHT:
left_tab = max(self.left_tab - 1, 0)
if left_tab != self.left_tab:
# Calculate the amount we need to adjust each tab by:
contents = self.visible_contents
adx = (
contents[left_tab].drag_bounds[0]
- contents[self.left_tab].drag_bounds[0]
)
# Set the new leftmost tab index:
self.left_tab = left_tab
# Determine which tab scroll image to use:
self.tab_scroll_index = (
(left_tab < self.max_tab) + (2 * (left_tab > 0))
) - 1
# Now adjust each tab's bounds accordingly:
for item in contents:
dbx, dby, dbdx, dbdy = item.drag_bounds
item.set_drag_bounds(dbx - adx, dby, item.tab_width, dbdy)
# Finally, force a redraw of the affected part of the window:
x, y, dx, dy = self._tab_clip_bounds
item.control.GetParent().RefreshRect(
wx.Rect(x, y, dx + DockImages._tab_scroller_dx, dy)
)
# ---------------------------------------------------------------------------
# Handles the left mouse button being pressed:
# ---------------------------------------------------------------------------
def mouse_down(self, event):
""" Handles the left mouse button being pressed.
"""
self._scroll = self._get_scroll_button(event)
# ---------------------------------------------------------------------------
# Handles the left mouse button being released:
# ---------------------------------------------------------------------------
def mouse_up(self, event):
""" Handles the left mouse button being released.
"""
if (self._scroll is not None) and (
self._scroll == self._get_scroll_button(event)
):
self.scroll(self._scroll)
else:
super().mouse_up(event)
# ---------------------------------------------------------------------------
# Handles the mouse moving while the left mouse button is pressed:
# ---------------------------------------------------------------------------
def mouse_move(self, event):
""" Handles the mouse moving while the left mouse button is pressed.
"""
pass
# ---------------------------------------------------------------------------
# Sets the visibility of the region:
# ---------------------------------------------------------------------------
def set_visibility(self, visible):
""" Sets the visibility of the region.
"""
self._visible = visible
active = self.active
for i, item in enumerate(self.contents):
item.set_visibility(visible and (i == active))
# ---------------------------------------------------------------------------
# Activates a specified control (i.e. makes it the current notebook tab):
# ---------------------------------------------------------------------------
def activate(self, control, layout=True):
""" Activates a specified control (i.e. makes it the current notebook
tab).
"""
if control.visible and self.is_notebook:
active = self.contents.index(control)
if active != self.active:
self.active = active
self.make_active_tab_visible()
window = control.control.GetParent()
if layout:
do_later(window.owner.update_layout)
else:
window.RefreshRect(wx.Rect(*self.bounds))
else:
# Fire the activated event for the control.
if isinstance(control, DockControl):
control.activated = True
# ---------------------------------------------------------------------------
# Makes sure the active control's tab is completely visible (if possible):
# ---------------------------------------------------------------------------
def make_active_tab_visible(self):
""" Makes sure the active control's tab is completely visible (if
possible).
"""
active = self.active
if active < self.left_tab:
self.scroll(SCROLL_TO, active)
else:
x, y, dx, dy = self.contents[active].drag_bounds
if not self.is_at(x + dx - 1, y + dy - 1, self._tab_clip_bounds):
self.scroll(SCROLL_TO, min(active, self.max_tab))
# ---------------------------------------------------------------------------
# Handles a contained DockControl item being hidden or shown:
# ---------------------------------------------------------------------------
def show_hide(self, control):
""" Handles a contained DockControl item being hidden or shown.
"""
i = self.contents.index(control)
if i == self.active:
self._update_active()
elif (self.active < 0) and control.visible:
self.active = i
self._is_notebook = None
# ---------------------------------------------------------------------------
# Prints the contents of the region:
# ---------------------------------------------------------------------------
def dump(self, indent):
""" Prints the contents of the region.
"""
print(
"%sRegion( %08X, active = %s, width = %d, height = %d )"
% (" " * indent, id(self), self.active, self.width, self.height)
)
for item in self.contents:
item.dump(indent + 3)
# ---------------------------------------------------------------------------
# Returns which scroll button (if any) the pointer is currently over:
# ---------------------------------------------------------------------------
def _get_scroll_button(self, event):
""" Returns which scroll button (if any) the pointer is currently over.
"""
x, y, dx, dy = self._tab_clip_bounds
if self.is_in(
event,
x + dx,
y + 2,
DockImages._tab_scroller_dx,
DockImages._tab_scroller_dy,
):
if (event.GetX() - (x + dx)) < (DockImages._tab_scroller_dx // 2):
return SCROLL_LEFT
return SCROLL_RIGHT
return None
# ---------------------------------------------------------------------------
# Updates the currently active page after a change:
# ---------------------------------------------------------------------------
def _update_active(self, active=None):
""" Updates the currently active page after a change.
"""
if active is None:
active = self.active
contents = self.contents
for i in list(range(active, len(contents))) + list(
range(active - 1, -1, -1)
):
if contents[i].visible:
self.active = i
return
self.active = -1
# ---------------------------------------------------------------------------
# Handles the 'active' trait being changed:
# ---------------------------------------------------------------------------
@observe("active")
def _active_updated(self, event):
old, new = event.old, event.new
self._set_visibility()
# Set the correct tab state for each tab:
for i, item in enumerate(self.contents):
item.tab_state = NormalStates[i == new]
n = len(self.contents)
if 0 <= old < n:
# Notify the previously active dockable that the control's tab is
# being deactivated:
control = self.contents[old]
if isinstance(control, DockControl) and (
control.dockable is not None
):
control.dockable.dockable_tab_activated(control, False)
if 0 <= new < n:
# Notify the new dockable that the control's tab is being
# activated:
control = self.contents[new]
if isinstance(control, DockControl):
control.activated = True
# ---------------------------------------------------------------------------
# Handles the 'contents' trait being changed:
# ---------------------------------------------------------------------------
@observe("contents")
def _contents_updated(self, event):
""" Handles the 'contents' trait being changed.
"""
self._is_notebook = None
for item in self.contents:
item.parent = self
self.calc_min(True)
self.modified = True
@observe("contents:items")
def _contents_items_updated(self, event):
""" Handles the 'contents' trait being changed.
"""
self._is_notebook = None
for item in event.added:
item.parent = self
self.calc_min(True)
self.modified = True
# ---------------------------------------------------------------------------
# Set the proper visiblity for all contained controls:
# ---------------------------------------------------------------------------
def _set_visibility(self):
""" Set the proper visiblity for all contained controls.
"""
active = self.active
for i, item in enumerate(self.contents):
item.set_visibility(i == active)
# ---------------------------------------------------------------------------
# Implementation of the 'modified' property:
# ---------------------------------------------------------------------------
def _set_modified(self, value):
if self.parent is not None:
self.parent.modified = True
# ---------------------------------------------------------------------------
# Implementation of the 'is_notebook' property:
# ---------------------------------------------------------------------------
def _get_is_notebook(self):
if self._is_notebook is None:
contents = self.visible_contents
n = len(contents)
self._is_notebook = n > 1
if n == 1:
self._is_notebook = contents[0].style == "tab"
return self._is_notebook
# ---------------------------------------------------------------------------
# Draws the notebook body:
# ---------------------------------------------------------------------------
def _draw_notebook(self, dc):
""" Draws the notebook body.
"""
theme = self.theme
tab_height = theme.tab_active.image_slice.dy
x, y, dx, dy = self.bounds
self.fill_bg_color(dc, x, y, dx, dy)
# Draws a box around the frame containing the tab contents, starting
# below the tab
pen = wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNSHADOW))
dc.SetPen(pen)
dc.DrawRectangle(x, y + tab_height, dx, dy - tab_height)
# draw highlight
pen = wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT))
dc.SetPen(pen)
dc.DrawLine(x + 1, y + tab_height + 1, x + dx - 1, y + tab_height + 1)
# Erases the line under the active tab
x0 = x + self.tab_theme.label.left
x1 = x0
for i in range(self.active + 1):
x0 = x1 + 1
x1 += self.contents[i].tab_width
dc.SetPen(wx.Pen(self.get_bg_color()))
dc.DrawLine(x0, y + tab_height, x1, y + tab_height)
dc.DrawLine(x0, y + tab_height + 1, x1, y + tab_height + 1)
# -------------------------------------------------------------------------------
# 'DockSection' class:
# -------------------------------------------------------------------------------
class DockSection(DockGroup):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# Is this a row (or a column)?
is_row = Bool(True)
# Bounds of any splitter bars associated with the region:
splitters = List(DockSplitter)
# The DockWindow that owns this section (set on top level section only):
dock_window = Instance("pyface.dock.dock_window.DockWindow")
# Contents of the section have been modified property:
modified = Property
# ---------------------------------------------------------------------------
# Re-implementation of the 'owner' property:
# ---------------------------------------------------------------------------
@cached_property
def _get_owner(self):
if self.dock_window is not None:
return self.dock_window
if self.parent is None:
return None
return self.parent.owner
# ---------------------------------------------------------------------------
# Calculates the minimum size of the section:
# ---------------------------------------------------------------------------
def calc_min(self, use_size=False):
""" Calculates the minimum size of the section.
"""
tdx = tdy = 0
contents = self.visible_contents
n = len(contents)
if self.is_row:
# allow 10 pixels for the splitter
sdx = 10
for item in contents:
dx, dy = item.calc_min(use_size)
tdx += dx
tdy = max(tdy, dy)
if self.resizable:
tdx += (n - 1) * sdx
else:
tdx += (n + 1) * 3
tdy += 6
else:
# allow 10 pixels for the splitter
sdy = 10
for item in contents:
dx, dy = item.calc_min(use_size)
tdx = max(tdx, dx)
tdy += dy
if self.resizable:
tdy += (n - 1) * sdy
else:
tdx += 6
tdy += (n + 1) * 3
if self.width < 0:
self.width = tdx
self.height = tdy
return (tdx, tdy)
# ---------------------------------------------------------------------------
# Perform initial layout of the section based on the specified bounds:
# ---------------------------------------------------------------------------
def initial_recalc_sizes(self, x, y, dx, dy):
""" Layout the contents of the section based on the specified bounds.
"""
self.width = dx = max(0, dx)
self.height = dy = max(0, dy)
self.bounds = (x, y, dx, dy)
# If none of the contents are resizable, use the fixed layout method
if not self.resizable:
self.recalc_sizes_fixed(x, y, dx, dy)
return
contents = self.visible_contents
n = len(contents) - 1
splitters = []
# Find out how much space is available.
splitter_size = 10
sizes = []
if self.is_row:
total = dx - (n * splitter_size)
else:
total = dy - (n * splitter_size)
# Get requested sizes from the items.
for item in contents:
size = -1.0
for dock_control in item.get_controls():
dockable = dock_control.dockable
if dockable is not None and dockable.element is not None:
if self.is_row:
size = max(size, dockable.element.width)
else:
size = max(size, dockable.element.height)
sizes.append(size)
# Allocate requested space.
avail = total
remain = 0
for i, sz in enumerate(sizes):
if avail <= 0:
break
if sz >= 0:
if sz >= 1:
sz = min(sz, avail)
else:
sz *= total
sz = int(sz)
sizes[i] = sz
avail -= sz
else:
remain += 1
# Allocate the remainder to those parts that didn't request a width.
if remain > 0:
remain = int(avail / remain)
for i, sz in enumerate(sizes):
if sz < 0:
sizes[i] = remain
# If all requested a width, allocate the remainder to the last item.
else:
sizes[-1] += avail
# Resize contents and add splitters
if self.is_row:
for i, item in enumerate(contents):
idx = int(sizes[i])
item.recalc_sizes(x, y, idx, dy)
x += idx
if i < n:
splitters.append(
DockSplitter(
bounds=(x, y, splitter_size, dy),
style="vertical",
parent=self,
index=i,
)
)
x += splitter_size
else:
for i, item in enumerate(contents):
idy = int(sizes[i])
item.recalc_sizes(x, y, dx, idy)
y += idy
if i < n:
splitters.append(
DockSplitter(
bounds=(x, y, dx, splitter_size),
style="horizontal",
parent=self,
index=i,
)
)
y += splitter_size
# Preserve the current internal '_last_bounds' for all splitters if
# possible:
cur_splitters = self.splitters
for i in range(min(len(splitters), len(cur_splitters))):
splitters[i]._last_bounds = cur_splitters[i]._last_bounds
# Save the new set of splitter bars:
self.splitters = splitters
# Set the visibility for all contained items:
self._set_visibility()
# ---------------------------------------------------------------------------
# Layout the contents of the section based on the specified bounds:
# ---------------------------------------------------------------------------
def recalc_sizes(self, x, y, dx, dy):
""" Layout the contents of the section based on the specified bounds.
"""
# Check if we need to perform initial layout
if not self.initialized:
self.initial_recalc_sizes(x, y, dx, dy)
self.initialized = True
return
self.width = dx = max(0, dx)
self.height = dy = max(0, dy)
self.bounds = (x, y, dx, dy)
# If none of the contents are resizable, use the fixed layout method:
if not self.resizable:
self.recalc_sizes_fixed(x, y, dx, dy)
return
contents = self.visible_contents
n = len(contents) - 1
splitters = []
# Perform a horizontal layout:
if self.is_row:
# allow 10 pixels for the splitter
sdx = 10
dx -= n * sdx
cdx = 0
# Calculate the current and minimum width:
for item in contents:
cdx += item.width
cdx = max(1, cdx)
# Calculate the delta between the current and new width:
delta = remaining = dx - cdx
# Allocate the change (plus or minus) proportionally based on each
# item's current size:
for i, item in enumerate(contents):
if i < n:
idx = int(round(float(item.width * delta) / cdx))
else:
idx = remaining
remaining -= idx
idx += item.width
item.recalc_sizes(x, y, idx, dy)
x += idx
# Define the splitter bar between adjacent items:
if i < n:
splitters.append(
DockSplitter(
bounds=(x, y, sdx, dy),
style="vertical",
parent=self,
index=i,
)
)
x += sdx
# Perform a vertical layout:
else:
# allow 10 pixels for the splitter
sdy = 10
dy -= n * sdy
cdy = 0
# Calculate the current and minimum height:
for item in contents:
cdy += item.height
cdy = max(1, cdy)
# Calculate the delta between the current and new height:
delta = remaining = dy - cdy
# Allocate the change (plus or minus) proportionally based on each
# item's current size:
for i, item in enumerate(contents):
if i < n:
idy = int(round(float(item.height * delta) / cdy))
else:
idy = remaining
remaining -= idy
idy += item.height
item.recalc_sizes(x, y, dx, idy)
y += idy
# Define the splitter bar between adjacent items:
if i < n:
splitters.append(
DockSplitter(
bounds=(x, y, dx, sdy),
style="horizontal",
parent=self,
index=i,
)
)
y += sdy
# Preserve the current internal '_last_bounds' for all splitters if
# possible:
cur_splitters = self.splitters
for i in range(min(len(splitters), len(cur_splitters))):
splitters[i]._last_bounds = cur_splitters[i]._last_bounds
# Save the new set of splitter bars:
self.splitters = splitters
# Set the visibility for all contained items:
self._set_visibility()
# ---------------------------------------------------------------------------
# Layout the contents of the section based on the specified bounds using
# the minimum requested size for each item:
# ---------------------------------------------------------------------------
def recalc_sizes_fixed(self, x, y, dx, dy):
""" Layout the contents of the section based on the specified bounds
using the minimum requested size for each item.
"""
self.splitters = []
x += 3
y += 3
dx = max(0, dx - 3)
dy = max(0, dy - 3)
# Perform a horizontal layout:
if self.is_row:
# Allocate the space for each item based on its minimum size until
# the space runs out:
for item in self.visible_contents:
idx, idy = item.calc_min()
idx = min(dx, idx)
idy = min(dy, idy)
dx = max(0, dx - idx - 3)
item.recalc_sizes(x, y, idx, idy)
x += idx + 3
# Perform a vertical layout:
else:
# Allocate the space for each item based on its minimum size until
# the space runs out:
for item in self.visible_contents:
idx, idy = item.calc_min()
idx = min(dx, idx)
idy = min(dy, idy)
dy = max(0, dy - idy - 3)
item.recalc_sizes(x, y, idx, idy)
y += idy + 3
# Set the visibility for all contained items:
self._set_visibility()
# ---------------------------------------------------------------------------
# Draws the contents of the section:
# ---------------------------------------------------------------------------
def draw(self, dc):
""" Draws the contents of the section.
"""
if self._visible is not False:
contents = self.visible_contents
x, y, dx, dy = self.bounds
self.fill_bg_color(dc, x, y, dx, dy)
for item in contents:
item.draw(dc)
self.begin_draw(dc)
for item in self.splitters:
item.draw(dc)
self.end_draw(dc)
# ---------------------------------------------------------------------------
# Returns the object at a specified window position:
# ---------------------------------------------------------------------------
def object_at(self, x, y, force=False):
""" Returns the object at a specified window position.
"""
if self._visible is not False:
for item in self.splitters:
if item.is_at(x, y):
return item
for item in self.visible_contents:
object = item.object_at(x, y)
if object is not None:
return object
if force and self.is_at(x, y):
return self
return None
# ---------------------------------------------------------------------------
# Gets the DockInfo object for a specified window position:
# ---------------------------------------------------------------------------
def dock_info_at(self, x, y, tdx, is_control, force=False):
""" Gets the DockInfo object for a specified window position.
"""
# Check to see if the point is in our drag bar:
info = super().dock_info_at(x, y, tdx, is_control)
if info is not None:
return info
if self._visible is False:
return None
for item in self.splitters:
if item.is_at(x, y):
return DockInfo(kind=DOCK_SPLITTER)
for item in self.visible_contents:
object = item.dock_info_at(x, y, tdx, is_control)
if object is not None:
return object
# Check to see if we must return a DockInfo object:
if not force:
return None
# Otherwise, figure out which edge the point is closest to, and
# return a DockInfo object describing that edge:
lx, ty, dx, dy = self.bounds
left = lx - x
right = x - lx - dx + 1
top = ty - y
bottom = y - ty - dy + 1
# If the point is way outside of the section, mark it is a drag and
# drop candidate:
if max(left, right, top, bottom) > 20:
return DockInfo(kind=DOCK_EXPORT)
left = abs(left)
right = abs(right)
top = abs(top)
bottom = abs(bottom)
choice = min(left, right, top, bottom)
mdx = dx // 3
mdy = dy // 3
if choice == left:
return DockInfo(kind=DOCK_LEFT, bounds=(lx, ty, mdx, dy))
if choice == right:
return DockInfo(
kind=DOCK_RIGHT, bounds=(lx + dx - mdx, ty, mdx, dy)
)
if choice == top:
return DockInfo(kind=DOCK_TOP, bounds=(lx, ty, dx, mdy))
return DockInfo(kind=DOCK_BOTTOM, bounds=(lx, ty + dy - mdy, dx, mdy))
# ---------------------------------------------------------------------------
# Adds a control to the section at the edge of the region specified:
# ---------------------------------------------------------------------------
def add(self, control, region, kind):
""" Adds a control to the section at the edge of the region specified.
"""
contents = self.contents
new_region = control
if not isinstance(control, DockRegion):
new_region = DockRegion(contents=[control])
i = contents.index(region)
if self.is_row:
if (kind == DOCK_TOP) or (kind == DOCK_BOTTOM):
if kind == DOCK_TOP:
new_contents = [new_region, region]
else:
new_contents = [region, new_region]
contents[i] = DockSection(is_row=False).trait_set(
contents=new_contents
)
else:
if new_region.parent is self:
contents.remove(new_region)
i = contents.index(region)
if kind == DOCK_RIGHT:
i += 1
contents.insert(i, new_region)
else:
if (kind == DOCK_LEFT) or (kind == DOCK_RIGHT):
if kind == DOCK_LEFT:
new_contents = [new_region, region]
else:
new_contents = [region, new_region]
contents[i] = DockSection(is_row=True).trait_set(
contents=new_contents
)
else:
if new_region.parent is self:
contents.remove(new_region)
i = contents.index(region)
if kind == DOCK_BOTTOM:
i += 1
contents.insert(i, new_region)
# ---------------------------------------------------------------------------
# Removes a specified region or section from the section:
# ---------------------------------------------------------------------------
def remove(self, item):
""" Removes a specified region or section from the section.
"""
contents = self.contents
if isinstance(item, DockGroup) and (len(item.contents) == 1):
contents[contents.index(item)] = item.contents[0]
else:
contents.remove(item)
if self.parent is not None:
if len(contents) <= 1:
self.parent.remove(self)
elif (len(contents) == 0) and (self.dock_window is not None):
self.dock_window.dock_window_empty()
# ---------------------------------------------------------------------------
# Sets the visibility of the group:
# ---------------------------------------------------------------------------
def set_visibility(self, visible):
""" Sets the visibility of the group.
"""
self._visible = visible
for item in self.contents:
item.set_visibility(visible)
# ---------------------------------------------------------------------------
# Returns a copy of the section 'structure', minus the actual content:
# ---------------------------------------------------------------------------
def get_structure(self):
""" Returns a copy of the section 'structure', minus the actual content.
"""
return self.clone_traits(["is_row", "width", "height"]).trait_set(
contents=[item.get_structure() for item in self.contents],
splitters=[item.get_structure() for item in self.splitters],
)
# ---------------------------------------------------------------------------
# Gets the maximum bounds that a splitter bar is allowed to be dragged:
# ---------------------------------------------------------------------------
def get_splitter_bounds(self, splitter):
""" Gets the maximum bounds that a splitter bar is allowed to be dragged.
"""
x, y, dx, dy = splitter.bounds
i = self.splitters.index(splitter)
contents = self.visible_contents
item1 = contents[i]
item2 = contents[i + 1]
bx, by, bdx, bdy = item2.bounds
if self.is_row:
x = item1.bounds[0]
dx = bx + bdx - x
else:
y = item1.bounds[1]
dy = by + bdy - y
return (x, y, dx, dy)
# ---------------------------------------------------------------------------
# Updates the affected regions when a splitter bar is released:
# ---------------------------------------------------------------------------
def update_splitter(self, splitter, window):
""" Updates the affected regions when a splitter bar is released.
"""
x, y, dx, dy = splitter.bounds
i = self.splitters.index(splitter)
contents = self.visible_contents
item1 = contents[i]
item2 = contents[i + 1]
ix1, iy1, idx1, idy1 = item1.bounds
ix2, iy2, idx2, idy2 = item2.bounds
window.Freeze()
if self.is_row:
item1.recalc_sizes(ix1, iy1, x - ix1, idy1)
item2.recalc_sizes(x + dx, iy2, ix2 + idx2 - x - dx, idy2)
else:
item1.recalc_sizes(ix1, iy1, idx1, y - iy1)
item2.recalc_sizes(ix2, y + dy, idx2, iy2 + idy2 - y - dy)
window.Thaw()
if splitter.style == "horizontal":
dx = 0
else:
dy = 0
window.RefreshRect(
wx.Rect(
ix1 - dx,
iy1 - dy,
ix2 + idx2 - ix1 + 2 * dx,
iy2 + idy2 - iy1 + 2 * dy,
)
)
# ---------------------------------------------------------------------------
# Prints the contents of the section:
# ---------------------------------------------------------------------------
def dump(self, indent=0):
""" Prints the contents of the section.
"""
print(
"%sSection( %08X, is_row = %s, width = %d, height = %d )"
% (" " * indent, id(self), self.is_row, self.width, self.height)
)
for item in self.contents:
item.dump(indent + 3)
# ---------------------------------------------------------------------------
# Sets the correct visiblity for all contained items:
# ---------------------------------------------------------------------------
def _set_visibility(self):
""" Sets the correct visiblity for all contained items.
"""
for item in self.contents:
item.set_visibility(item.visible)
# ---------------------------------------------------------------------------
# Handles the 'contents' trait being changed:
# ---------------------------------------------------------------------------
@observe("contents")
def _contents_updated(self, event):
""" Handles the 'contents' trait being changed.
"""
for item in self.contents:
item.parent = self
self.calc_min(True)
self.modified = True
@observe("contents:items")
def _contents_items_updated(self, event):
""" Handles the 'contents' trait being changed.
"""
for item in event.added:
item.parent = self
self.calc_min(True)
self.modified = True
# ---------------------------------------------------------------------------
# Handles the 'splitters' trait being changed:
# ---------------------------------------------------------------------------
@observe("splitters")
def _splitters_updated(self, event):
""" Handles the 'splitters' trait being changed.
"""
for item in self.splitters:
item.parent = self
@observe("splitters:items")
def _splitters_items_updated(self, event):
""" Handles the 'splitters' trait being changed.
"""
for item in event.added:
item.parent = self
# ---------------------------------------------------------------------------
# Implementation of the 'modified' property:
# ---------------------------------------------------------------------------
def _set_modified(self, value):
self._resizable = None
if self.parent is not None:
self.parent.modified = True
# -------------------------------------------------------------------------------
# 'DockInfo' class:
# -------------------------------------------------------------------------------
class DockInfo(HasPrivateTraits):
# ---------------------------------------------------------------------------
# Trait definitions:
# ---------------------------------------------------------------------------
# Dock kind:
kind = Range(DOCK_TOP, DOCK_EXPORT)
# Dock bounds:
bounds = Bounds
# Tab bounds (if needed):
tab_bounds = Bounds
# Dock Region:
region = Instance(DockRegion)
# Dock Control:
control = Instance(DockItem)
# ---------------------------------------------------------------------------
# Draws the DockInfo on the display:
# ---------------------------------------------------------------------------
def draw(self, window, bitmap=None):
""" Draws the DockInfo on the display.
"""
if DOCK_TOP <= self.kind <= DOCK_TABADD:
if bitmap is None:
bitmap = self._bitmap
if bitmap is None:
return
else:
self._bitmap = bitmap
sdc, bx, by = get_dc(window)
bdc = wx.MemoryDC()
bdc2 = wx.MemoryDC()
bdx, bdy = bitmap.GetWidth(), bitmap.GetHeight()
bitmap2 = wx.Bitmap(bdx, bdy)
bdc.SelectObject(bitmap)
bdc2.SelectObject(bitmap2)
bdc2.Blit(0, 0, bdx, bdy, bdc, 0, 0)
try:
bdc3 = wx.GCDC(bdc2)
bdc3.SetPen(wx.TRANSPARENT_PEN)
bdc3.SetBrush(wx.Brush(wx.Colour(*DockColorBrush)))
x, y, dx, dy = self.bounds
if DOCK_TAB <= self.kind <= DOCK_TABADD:
tx, ty, tdx, tdy = self.tab_bounds
bdc3.DrawRoundedRectangle(tx, ty, tdx, tdy, 4)
else:
bdc3.DrawRoundedRectangle(x, y, dx, dy, 8)
except Exception:
pass
sdc.Blit(bx, by, bdx, bdy, bdc2, 0, 0)
# ---------------------------------------------------------------------------
# Docks the specified control:
# ---------------------------------------------------------------------------
def dock(self, control, window):
""" Docks the specified control.
"""
the_control = control
kind = self.kind
if kind < DOCK_NONE:
the_parent = control.parent
region = self.region
if (kind == DOCK_TAB) or (kind == DOCK_BAR):
region.add(control, self.control)
elif kind == DOCK_TABADD:
item = self.control
if isinstance(item, DockControl):
if isinstance(control, DockControl):
control = DockRegion(contents=[control])
i = region.contents.index(item)
region.contents[i] = item = DockSection(
contents=[DockRegion(contents=[item]), control],
is_row=True,
)
elif isinstance(item, DockSection):
if isinstance(control, DockSection) and (
item.is_row == control.is_row
):
item.contents.extend(control.contents)
else:
if isinstance(control, DockControl):
control = DockRegion(contents=[control])
item.contents.append(control)
else:
item.contents.append(control)
region.active = region.contents.index(item)
elif region is not None:
region.parent.add(control, region, kind)
else:
sizer = window.GetSizer()
section = sizer._contents
if (
section.is_row
and ((kind == DOCK_TOP) or (kind == DOCK_BOTTOM))
) or (
(not section.is_row)
and ((kind == DOCK_LEFT) or (kind == DOCK_RIGHT))
):
if len(section.contents) > 0:
sizer._contents = section = DockSection(
is_row=not section.is_row
).trait_set(contents=[section])
if len(section.contents) > 0:
i = 0
if (kind == DOCK_RIGHT) or (kind == DOCK_BOTTOM):
i = -1
section.add(control, section.contents[i], kind)
else:
section.is_row = not section.is_row
section.contents = [DockRegion(contents=[control])]
section = None
if (the_parent is not None) and (
the_parent is not the_control.parent
):
the_parent.remove(the_control)
# Force the main window to be laid out and redrawn:
window.Layout()
window.Refresh()
# Create a reusable DockInfo indicating no information available:
no_dock_info = DockInfo(kind=DOCK_NONE)
# -------------------------------------------------------------------------------
# 'SetStructureHandler' class
# -------------------------------------------------------------------------------
class SetStructureHandler(object):
# ---------------------------------------------------------------------------
# Resolves an unresolved DockControl id:
# ---------------------------------------------------------------------------
def resolve_id(self, id):
""" Resolves an unresolved DockControl id.
"""
return None
# ---------------------------------------------------------------------------
# Resolves extra, unused DockControls not referenced by the structure:
# ---------------------------------------------------------------------------
def resolve_extras(self, structure, extras):
""" Resolves extra, unused DockControls not referenced by the structure.
"""
for dock_control in extras:
if dock_control.control is not None:
dock_control.control.Show(False)
# -------------------------------------------------------------------------------
# 'DockSizer' class:
# -------------------------------------------------------------------------------
class DockSizer(wx.Sizer):
# ---------------------------------------------------------------------------
# Initializes the object:
# ---------------------------------------------------------------------------
def __init__(self, contents=None):
super().__init__()
# Make sure the DockImages singleton has been initialized:
DockImages.init()
# Finish initializing the sizer itself:
self._contents = self._structure = self._max_structure = None
if contents is not None:
self.SetContents(contents)
# ---------------------------------------------------------------------------
# Calculates the minimum size needed by the sizer:
# ---------------------------------------------------------------------------
def CalcMin(self):
if self._contents is None:
return wx.Size(20, 20)
dx, dy = self._contents.calc_min()
return wx.Size(dx, dy)
# ---------------------------------------------------------------------------
# Layout the contents of the sizer based on the sizer's current size and
# position:
# ---------------------------------------------------------------------------
def RecalcSizes(self):
""" Layout the contents of the sizer based on the sizer's current size
and position.
"""
if self._contents is None:
return
x, y = self.GetPosition().Get()
dx, dy = self.GetSize().Get()
self._contents.recalc_sizes(x, y, dx, dy)
# ---------------------------------------------------------------------------
# Returns the current sizer contents:
# ---------------------------------------------------------------------------
def GetContents(self):
""" Returns the current sizer contents.
"""
return self._contents
# ---------------------------------------------------------------------------
# Initializes the layout of a DockWindow from a content list:
# ---------------------------------------------------------------------------
def SetContents(self, contents):
""" Initializes the layout of a DockWindow from a content list.
"""
if isinstance(contents, DockGroup):
self._contents = contents
elif isinstance(contents, tuple):
self._contents = self._set_region(contents)
elif isinstance(contents, list):
self._contents = self._set_section(contents, True)
elif isinstance(contents, DockControl):
self._contents = self._set_section([contents], True)
else:
raise TypeError()
# Set the owner DockWindow for the top-level group (if possible)
# so that it can notify the owner when the DockWindow becomes empty:
control = self._contents.control
if control is not None:
self._contents.dock_window = control.GetParent().owner
# If no saved structure exists yet, save the current one:
if self._structure is None:
self._structure = self.GetStructure()
def _set_region(self, contents):
items = []
for item in contents:
if isinstance(item, tuple):
items.append(self._set_region(item))
elif isinstance(item, list):
items.append(self._set_section(item, True))
elif isinstance(item, DockItem):
items.append(item)
else:
raise TypeError()
return DockRegion(contents=items)
def _set_section(self, contents, is_row):
items = []
for item in contents:
if isinstance(item, tuple):
items.append(self._set_region(item))
elif isinstance(item, list):
items.append(self._set_section(item, not is_row))
elif isinstance(item, DockControl):
items.append(DockRegion(contents=[item]))
else:
raise TypeError()
return DockSection(is_row=is_row).trait_set(contents=items)
# ---------------------------------------------------------------------------
# Returns a copy of the layout 'structure', minus the actual content
# (i.e. controls, splitters, bounds). This method is intended for use in
# persisting the current user layout, so that it can be restored in a
# future session:
# ---------------------------------------------------------------------------
def GetStructure(self):
""" Returns a copy of the layout 'structure', minus the actual content
(i.e. controls, splitters, bounds). This method is intended for use
in persisting the current user layout, so that it can be restored in
a future session.
"""
if self._contents is not None:
return self._contents.get_structure()
return DockSection()
# ---------------------------------------------------------------------------
# Takes a previously saved 'GetStructure' result and applies it to the
# contents of the sizer in order to restore a previous layout using a
# new set of controls:
# ---------------------------------------------------------------------------
def SetStructure(self, window, structure, handler=None):
""" Takes a previously saved 'GetStructure' result and applies it to the
contents of the sizer in order to restore a previous layout using a
new set of controls.
"""
section = self._contents
if (section is None) or (not isinstance(structure, DockGroup)):
return
# Make sure that DockSections, which have a separate layout algorithm
# for the first layout, are set as initialized.
structure.initialized = True
# Save the current structure in case a 'ResetStructure' call is made
# later:
self._structure = self.GetStructure()
extras = []
# Create a mapping for all the DockControls in the new structure:
map = {}
for control in structure.get_controls(False):
if control.id in map:
control.parent.remove(control)
else:
map[control.id] = control
# Try to map each current item into an equivalent item in the saved
# preferences:
for control in section.get_controls(False):
mapped_control = map.get(control.id)
if mapped_control is not None:
control.trait_set(
**mapped_control.get(
"visible",
"locked",
"closeable",
"resizable",
"width",
"height",
)
)
if mapped_control.user_name:
control.name = mapped_control.name
if mapped_control.user_style:
control.style = mapped_control.style
structure.replace_control(mapped_control, control)
del map[control.id]
else:
extras.append(control)
# Try to resolve all unused saved items:
for id, item in map.items():
# If there is a handler, see if it can resolve it:
if handler is not None:
control = handler.resolve_id(id)
if control is not None:
item.control = control
continue
# If nobody knows what it is, just remove it:
item.parent.remove(item)
# Check if there are any new items that we have never seen before:
if len(extras) > 0:
if handler is not None:
# Allow the handler to decide their fate:
handler.resolve_extras(structure, extras)
else:
# Otherwise, add them to the top level as a new region (let the
# user re-arrange them):
structure.contents.append(DockRegion(contents=extras))
# Finally, replace the original structure with the updated structure:
self.SetContents(structure)
# ---------------------------------------------------------------------------
# Restores the previously saved structure (if any):
# ---------------------------------------------------------------------------
def ResetStructure(self, window):
""" Restores the previously saved structure (if any).
"""
if self._structure is not None:
self.SetStructure(window, self._structure)
# ---------------------------------------------------------------------------
# Toggles the current 'lock' setting of the contents:
# ---------------------------------------------------------------------------
def ToggleLock(self):
""" Toggles the current 'lock' setting of the contents.
"""
if self._contents is not None:
self._contents.toggle_lock()
# ---------------------------------------------------------------------------
# Draws the contents of the sizer:
# ---------------------------------------------------------------------------
def Draw(self, window):
""" Draws the contents of the sizer.
"""
if self._contents is not None:
self._contents.draw(set_standard_font(wx.PaintDC(window)))
else:
clear_window(window)
# ---------------------------------------------------------------------------
# Returns the object at a specified x, y position:
# ---------------------------------------------------------------------------
def ObjectAt(self, x, y, force=False):
""" Returns the object at a specified window position.
"""
if self._contents is not None:
return self._contents.object_at(x, y, force)
return None
# ---------------------------------------------------------------------------
# Gets a DockInfo object at a specified x, y position:
# ---------------------------------------------------------------------------
def DockInfoAt(self, x, y, size, is_control):
""" Gets a DockInfo object at a specified x, y position.
"""
if self._contents is not None:
return self._contents.dock_info_at(x, y, size, is_control, True)
return no_dock_info
# ---------------------------------------------------------------------------
# Minimizes/Maximizes a specified DockControl:
# ---------------------------------------------------------------------------
def MinMax(self, window, dock_control):
""" Minimizes/Maximizes a specified DockControl.
"""
if self._max_structure is None:
self._max_structure = self.GetStructure()
for control in self.GetContents().get_controls():
control.visible = control is dock_control
else:
self.Reset(window)
# ---------------------------------------------------------------------------
# Resets the DockSizer to a known state:
# ---------------------------------------------------------------------------
def Reset(self, window):
""" Resets the DockSizer to a known state.
"""
if self._max_structure is not None:
self.SetStructure(window, self._max_structure)
self._max_structure = None
# ---------------------------------------------------------------------------
# Returns whether the sizer can be maximized now:
# ---------------------------------------------------------------------------
def IsMaximizable(self):
""" Returns whether the sizer can be maximized now.
"""
return self._max_structure is None
def top_level_window_for(control):
""" Returns the top-level window for a specified control.
"""
parent = control.GetParent()
while parent is not None:
control = parent
parent = control.GetParent()
return control
|