art_msw.py 132 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
"""
`art_msw` is responsible for drawing all the components of the ribbon
interface using a Windows appearance.


Description
===========

This allows a ribbon bar to have a pluggable look-and-feel, while retaining the same
underlying behaviour. As a single art provider is used for all ribbon components, a
ribbon bar usually has a consistent (though unique) appearance.

By default, a :class:`~lib.agw.ribbon.bar.RibbonBar` uses an instance of a class called
:class:`~lib.agw.ribbon.art_default.RibbonDefaultArtProvider`,
which resolves to :class:`~lib.agw.ribbon.art_aui.RibbonAUIArtProvider`,
:class:`~lib.agw.ribbon.art_msw.RibbonMSWArtProvider`, or
:class:`~lib.agw.ribbon.art_osx.RibbonOSXArtProvider` - whichever is most appropriate
to the current platform. These art providers are all
slightly configurable with regard to colours and fonts, but for larger modifications,
you can derive from one of these classes, or write a completely new art provider class.

Call :meth:`RibbonBar.SetArtProvider() <lib.agw.ribbon.bar.RibbonBar.SetArtProvider>` to change the art provider being used.


See Also
========

:class:`~lib.agw.ribbon.bar.RibbonBar`
"""

import wx
import types

from math import cos
from math import pi as M_PI

import panel as PANEL
import page as PAGE

from art_internal import RibbonLoadPixmap, RibbonInterpolateColour, RibbonDrawParallelGradientLines
from art_internal import RibbonCanLabelBreakAtPosition
from art_internal import RibbonHSLColour

from art import *


gallery_up_xpm = ["5 5 2 1", "  c None", "x c #FF00FF", "     ", "  x  ", " xxx ", "xxxxx", "     "]
gallery_down_xpm = ["5 5 2 1", "  c None", "x c #FF00FF", "     ", "xxxxx", " xxx ", "  x  ", "     "]
gallery_left_xpm = ["5 5 2 1", "  c None", "x c #FF00FF", "   x ", "  xx ", " xxx ", "  xx ", "   x "]
gallery_right_xpm = ["5 5 2 1", "  c None", "x c #FF00FF", " x   ", " xx  ", " xxx ", " xx  ", " x   "]
gallery_extension_xpm = ["5 5 2 1", "  c None", "x c #FF00FF", "xxxxx", "     ", "xxxxx", " xxx ", "  x  "]
panel_extension_xpm = ["7 7 2 1", "  c None", "x c #FF00FF", "xxxxxx ", "x      ", "x      ",
                       "x  x  x", "x   xxx", "x   xxx", "   xxxx"]


def LikePrimary(primary_hsl, is_gray, h, s, l):

    return primary_hsl.ShiftHue(h).Saturated((is_gray and [0] or [s])[0]).Lighter(l).ToRGB()


def LikeSecondary(secondary_hsl, is_gray, h, s, l):
    
    return secondary_hsl.ShiftHue(h).Saturated((is_gray and [0] or [s])[0]).Lighter(l).ToRGB()


def SingleLine(dc, rect, start, finish):

    dc.DrawLine(start.x + rect.x, start.y + rect.y, finish.x + rect.x, finish.y + rect.y)

                               
class RibbonMSWArtProvider(object):

    def __init__(self, set_colour_scheme=True):

        self._flags = 0
        self._tab_label_font = wx.NORMAL_FONT
        self._button_bar_label_font = wx.NORMAL_FONT
        self._panel_label_font = wx.NORMAL_FONT

        self._gallery_up_bitmap = [wx.NullBitmap for i in xrange(4)]
        self._gallery_down_bitmap = [wx.NullBitmap for i in xrange(4)]
        self._gallery_extension_bitmap = [wx.NullBitmap for i in xrange(4)]
        self._panel_extension_bitmap = [wx.NullBitmap for i in xrange(2)]

        if set_colour_scheme:
            self.SetColourScheme(wx.Colour(194, 216, 241), wx.Colour(255, 223, 114), wx.Colour(0, 0, 0))
        
        self._cached_tab_separator_visibility = -10.0 # valid visibilities are in range [0, 1]
        self._tab_separation_size = 3
        self._page_border_left = 2
        self._page_border_top = 1
        self._page_border_right = 2
        self._page_border_bottom = 3
        self._panel_x_separation_size = 1
        self._panel_y_separation_size = 1
        self._tool_group_separation_size = 3
        self._gallery_bitmap_padding_left_size = 4
        self._gallery_bitmap_padding_right_size = 4
        self._gallery_bitmap_padding_top_size = 4
        self._gallery_bitmap_padding_bottom__size = 4
        self._cached_tab_separator = wx.NullBitmap

            
    def GetColourScheme(self, primary, secondary, tertiary):
        """
        Get the current colour scheme.

        Returns three colours such that if :meth:`~RibbonMSWArtProvider.SetColourScheme` were called with them, the
        colour scheme would be restored to what it was when :meth:`~RibbonMSWArtProvider.SetColourScheme` was last
        called. In practice, this usually means that the returned values are the three
        colours given in the last call to :meth:`~RibbonMSWArtProvider.SetColourScheme`, however if
        :meth:`~RibbonMSWArtProvider.SetColourScheme` performs an idempotent operation upon the colours it is given
        (like clamping a component of the colour), then the returned values may not be
        the three colours given in the last call to :meth:`~RibbonMSWArtProvider.SetColourScheme`.

        If :meth:`~RibbonMSWArtProvider.SetColourScheme` has not been called, then the returned values should result
        in a colour scheme similar to, if not identical to, the default colours of the
        art provider. Note that if :meth:`~RibbonMSWArtProvider.SetColour` is called, then :meth:`~RibbonMSWArtProvider.GetColourScheme` does
        not try and return a colour scheme similar to colours being used - it's return
        values are dependant upon the last values given to :meth:`~RibbonMSWArtProvider.SetColourScheme`, as
        described above.

        :param `primary`: Pointer to a location to store the primary colour, or ``None``;
        :param `secondary`: Pointer to a location to store the secondary colour, or ``None``;
        :param `tertiary`: Pointer to a location to store the tertiary colour, or ``None``.

        """

        if primary != None:
            primary = self._primary_scheme_colour
        if secondary != None:
            secondary = self._secondary_scheme_colour
        if tertiary != None:
            tertiary = self._tertiary_scheme_colour

        return primary, secondary, tertiary
    

    def SetColourScheme(self, primary, secondary, tertiary):
        """
        Set all applicable colour settings from a few base colours.

        Uses any or all of the three given colours to create a colour scheme, and then
        sets all colour settings which are relevant to the art provider using that
        scheme. Note that some art providers may not use the tertiary colour for
        anything, and some may not use the secondary colour either.

        :param `primary`: MISSING DESCRIPTION;
        :param `secondary`: MISSING DESCRIPTION;
        :param `tertiary`: MISSING DESCRIPTION.

        :see: :meth:`~RibbonMSWArtProvider.SetColour`, :meth:`~RibbonMSWArtProvider.GetColourScheme`
        """

        self._primary_scheme_colour = primary
        self._secondary_scheme_colour = secondary
        self._tertiary_scheme_colour = tertiary

        primary_hsl = RibbonHSLColour(primary)
        secondary_hsl = RibbonHSLColour(secondary)
        # tertiary not used for anything

        # Map primary saturation from [0, 1] to [.25, .75]
        primary_is_gray = False
        gray_saturation_threshold = 0.01

        if primary_hsl.saturation <= gray_saturation_threshold:
            primary_is_gray = True
        else:    
            primary_hsl.saturation = cos(primary_hsl.saturation * M_PI) * -0.25 + 0.5

        # Map primary luminance from [0, 1] to [.23, .83]
        primary_hsl.luminance = cos(primary_hsl.luminance * M_PI) * -0.3 + 0.53

        # Map secondary saturation from [0, 1] to [0.16, 0.84]
        secondary_is_gray = False
        
        if secondary_hsl.saturation <= gray_saturation_threshold:
            secondary_is_gray = True
        else:
            secondary_hsl.saturation = cos(secondary_hsl.saturation * M_PI) * -0.34 + 0.5

        # Map secondary luminance from [0, 1] to [0.1, 0.9]
        secondary_hsl.luminance = cos(secondary_hsl.luminance * M_PI) * -0.4 + 0.5

        self._page_border_pen = wx.Pen(LikePrimary(primary_hsl, primary_is_gray, 1.4, 0.00, -0.08))
        self._page_background_top_colour = LikePrimary(primary_hsl, primary_is_gray, -0.1, -0.03, 0.12)
        self._page_hover_background_top_colour = LikePrimary(primary_hsl, primary_is_gray, -2.8, 0.27, 0.17)
        self._page_background_top_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 0.1, -0.10, 0.08)
        self._page_hover_background_top_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 3.2, 0.16, 0.13)
        self._page_background_colour = LikePrimary(primary_hsl, primary_is_gray, 0.4, -0.09, 0.05)
        self._page_hover_background_colour = LikePrimary(primary_hsl, primary_is_gray, 0.1, 0.19, 0.10)
        self._page_background_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, -3.2, 0.27, 0.10)
        self._page_hover_background_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 1.8, 0.01, 0.15)

        self._tab_active_background_colour = LikePrimary(primary_hsl, primary_is_gray, -0.1, -0.31, 0.16)
        self._tab_active_background_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, -0.1, -0.03, 0.12)
        self._tab_separator_colour = LikePrimary(primary_hsl, primary_is_gray, 0.9, 0.24, 0.05)
        self._tab_ctrl_background_brush = wx.Brush(LikePrimary(primary_hsl, primary_is_gray, 1.0, 0.39, 0.07))
        self._tab_hover_background_colour = LikePrimary(primary_hsl, primary_is_gray, 1.3, 0.15, 0.10)
        self._tab_hover_background_top_colour = LikePrimary(primary_hsl, primary_is_gray, 1.4, 0.36, 0.08)
        self._tab_border_pen = wx.Pen(LikePrimary(primary_hsl, primary_is_gray, 1.4, 0.03, -0.05)  )
        self._tab_separator_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 1.7, -0.15, -0.18)
        self._tab_hover_background_top_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 1.8, 0.34, 0.13)   
        self._tab_label_colour = LikePrimary(primary_hsl, primary_is_gray, 4.3, 0.13, -0.49)
        self._tab_hover_background_gradient_colour = LikeSecondary(primary_hsl, secondary_is_gray, -1.5, -0.34, 0.01)

        self._panel_minimised_border_gradient_pen = wx.Pen(LikePrimary(primary_hsl, primary_is_gray, -6.9, -0.17, -0.09))
        self._panel_minimised_border_pen = wx.Pen(LikePrimary(primary_hsl, primary_is_gray, -5.3, -0.24, -0.06))
        self._panel_border_gradient_pen = wx.Pen(LikePrimary(primary_hsl, primary_is_gray, -5.2, -0.15, -0.06))
        self._panel_border_pen = wx.Pen(LikePrimary(primary_hsl, primary_is_gray, -2.8, -0.32, 0.02))
        self._panel_label_background_brush = wx.Brush(LikePrimary(primary_hsl, primary_is_gray, -1.5, 0.03, 0.05))
        self._panel_active_background_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 0.5, 0.34, 0.05)
        self._panel_hover_label_background_brush = wx.Brush(LikePrimary(primary_hsl, primary_is_gray, 1.0, 0.30, 0.09))
        self._panel_active_background_top_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 1.4, -0.17, -0.13)
        self._panel_active_background_colour = LikePrimary(primary_hsl, primary_is_gray, 1.6, -0.18, -0.18)
        self._panel_active_background_top_colour = LikePrimary(primary_hsl, primary_is_gray, 1.7, -0.20, -0.03)
        self._panel_label_colour = LikePrimary(primary_hsl, primary_is_gray, 2.8, -0.14, -0.35)
        self._panel_hover_label_colour = self._panel_label_colour
        self._panel_minimised_label_colour = self._tab_label_colour

        self._panel_hover_button_background_brush = wx.Brush(LikeSecondary(secondary_hsl, secondary_is_gray, -0.9, 0.16, -0.07))
        self._panel_hover_button_border_pen = wx.Pen(LikeSecondary(secondary_hsl, secondary_is_gray, -3.9, -0.16, -0.14))
        self.SetColour(RIBBON_ART_PANEL_BUTTON_FACE_COLOUR, LikePrimary(primary_hsl, primary_is_gray, 1.4, -0.21, -0.23))
        self.SetColour(RIBBON_ART_PANEL_BUTTON_HOVER_FACE_COLOUR, LikePrimary(primary_hsl, primary_is_gray, 1.5, -0.24, -0.29))
    
        self._gallery_button_disabled_background_colour = LikePrimary(primary_hsl, primary_is_gray, -2.8, -0.46, 0.09)
        self._gallery_button_disabled_background_top_brush = wx.Brush(LikePrimary(primary_hsl, primary_is_gray, -2.8, -0.36, 0.15))
        self._gallery_hover_background_brush = wx.Brush(LikePrimary(primary_hsl, primary_is_gray, -0.8, 0.05, 0.15))
        self._gallery_border_pen = wx.Pen(LikePrimary(primary_hsl, primary_is_gray, 0.7, -0.02, 0.03))
        self._gallery_button_background_top_brush = wx.Brush(LikePrimary(primary_hsl, primary_is_gray, 0.8, 0.34, 0.13))
        self._gallery_button_background_colour = LikePrimary(primary_hsl, primary_is_gray, 1.3, 0.10, 0.08)

        # SetColour used so that the relevant bitmaps are generated
        self.SetColour(RIBBON_ART_GALLERY_BUTTON_FACE_COLOUR, LikePrimary(primary_hsl, primary_is_gray, 1.4, -0.21, -0.23))
        self.SetColour(RIBBON_ART_GALLERY_BUTTON_HOVER_FACE_COLOUR, LikePrimary(primary_hsl, primary_is_gray, 1.5, -0.24, -0.29))
        self.SetColour(RIBBON_ART_GALLERY_BUTTON_ACTIVE_FACE_COLOUR, LikePrimary(primary_hsl, primary_is_gray, 1.5, -0.24, -0.29))
        self.SetColour(RIBBON_ART_GALLERY_BUTTON_DISABLED_FACE_COLOUR, LikePrimary(primary_hsl, primary_is_gray, 0.0, -1.0, 0.0))
        self._gallery_button_disabled_background_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 1.5, -0.43, 0.12)
        self._gallery_button_background_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 1.7, 0.11, 0.09)
        self._gallery_item_border_pen = wx.Pen(LikeSecondary(secondary_hsl, secondary_is_gray, -3.9, -0.16, -0.14))
        self._gallery_button_hover_background_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -0.9, 0.16, -0.07)
        self._gallery_button_hover_background_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, 0.1, 0.12, 0.03)
        self._gallery_button_hover_background_top_brush = wx.Brush(LikeSecondary(secondary_hsl, secondary_is_gray, 4.3, 0.16, 0.17))

        self._gallery_button_active_background_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -9.9, 0.03, -0.22)
        self._gallery_button_active_background_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -9.5, 0.14, -0.11)
        self._gallery_button_active_background_top_brush = wx.Brush(LikeSecondary(secondary_hsl, secondary_is_gray, -9.0, 0.15, -0.08))
        
        self._button_bar_label_colour = self._tab_label_colour
        self._button_bar_hover_border_pen = wx.Pen(LikeSecondary(secondary_hsl, secondary_is_gray, -6.2, -0.47, -0.14))
        self._button_bar_hover_background_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -0.6, 0.16, 0.04)
        self._button_bar_hover_background_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -0.2, 0.16, -0.10)
        self._button_bar_hover_background_top_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, 0.2, 0.16, 0.03)
        self._button_bar_hover_background_top_colour = LikeSecondary(secondary_hsl, secondary_is_gray, 8.8, 0.16, 0.17)
        self._button_bar_active_border_pen = wx.Pen(LikeSecondary(secondary_hsl, secondary_is_gray, -6.2, -0.47, -0.25))
        self._button_bar_active_background_top_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -8.4, 0.08, 0.06)
        self._button_bar_active_background_top_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -9.7, 0.13, -0.07)
        self._button_bar_active_background_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -9.9, 0.14, -0.14)
        self._button_bar_active_background_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -8.7, 0.17, -0.03)

        self._toolbar_border_pen = wx.Pen(LikePrimary(primary_hsl, primary_is_gray, 1.4, -0.21, -0.16))
        self.SetColour(RIBBON_ART_TOOLBAR_FACE_COLOUR, LikePrimary(primary_hsl, primary_is_gray, 1.4, -0.17, -0.22))
        self._tool_background_top_colour = LikePrimary(primary_hsl, primary_is_gray, -1.9, -0.07, 0.06)
        self._tool_background_top_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 1.4, 0.12, 0.08)
        self._tool_background_colour = LikePrimary(primary_hsl, primary_is_gray, 1.4, -0.09, 0.03)
        self._tool_background_gradient_colour = LikePrimary(primary_hsl, primary_is_gray, 1.9, 0.11, 0.09)
        self._tool_hover_background_top_colour = LikeSecondary(secondary_hsl, secondary_is_gray, 3.4, 0.11, 0.16)
        self._tool_hover_background_top_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -1.4, 0.04, 0.08)
        self._tool_hover_background_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -1.8, 0.16, -0.12)
        self._tool_hover_background_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -2.6, 0.16, 0.05)
        self._tool_active_background_top_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -9.9, -0.12, -0.09)
        self._tool_active_background_top_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -8.5, 0.16, -0.12)
        self._tool_active_background_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -7.9, 0.16, -0.20)
        self._tool_active_background_gradient_colour = LikeSecondary(secondary_hsl, secondary_is_gray, -6.6, 0.16, -0.10)

        # Invalidate cached tab separator
        self._cached_tab_separator_visibility = -1.0


    def Clone(self):
        """
        Create a new art provider which is a clone of this one.
        """

        copy = RibbonMSWArtProvider()
        self.CloneTo(copy)
        return copy


    def CloneTo(self, copy):

        for i in xrange(4):    
            copy._gallery_up_bitmap[i] = self._gallery_up_bitmap[i]
            copy._gallery_down_bitmap[i] = self._gallery_down_bitmap[i]
            copy._gallery_extension_bitmap[i] = self._gallery_extension_bitmap[i]

        for i in xrange(2):
            copy._panel_extension_bitmap[i] = self._panel_extension_bitmap[i]
    
        copy._toolbar_drop_bitmap = self._toolbar_drop_bitmap

        copy._primary_scheme_colour = self._primary_scheme_colour
        copy._secondary_scheme_colour = self._secondary_scheme_colour
        copy._tertiary_scheme_colour = self._tertiary_scheme_colour

        copy._button_bar_label_colour = self._button_bar_label_colour
        copy._tab_label_colour = self._tab_label_colour
        copy._tab_separator_colour = self._tab_separator_colour
        copy._tab_separator_gradient_colour = self._tab_separator_gradient_colour
        copy._tab_active_background_colour = self._tab_hover_background_colour
        copy._tab_active_background_gradient_colour = self._tab_hover_background_gradient_colour
        copy._tab_hover_background_colour = self._tab_hover_background_colour
        copy._tab_hover_background_gradient_colour = self._tab_hover_background_gradient_colour
        copy._tab_hover_background_top_colour = self._tab_hover_background_top_colour
        copy._tab_hover_background_top_gradient_colour = self._tab_hover_background_top_gradient_colour
        copy._panel_label_colour = self._panel_label_colour
        copy._panel_hover_label_colour = self._panel_hover_label_colour
        copy._panel_minimised_label_colour = self._panel_minimised_label_colour
        copy._panel_button_face_colour = self._panel_button_face_colour
        copy._panel_button_hover_face_colour = self._panel_button_hover_face_colour
        copy._panel_active_background_colour = self._panel_active_background_colour
        copy._panel_active_background_gradient_colour = self._panel_active_background_gradient_colour
        copy._panel_active_background_top_colour = self._panel_active_background_top_colour
        copy._panel_active_background_top_gradient_colour = self._panel_active_background_top_gradient_colour
        copy._page_background_colour = self._page_background_colour
        copy._page_background_gradient_colour = self._page_background_gradient_colour
        copy._page_background_top_colour = self._page_background_top_colour
        copy._page_background_top_gradient_colour = self._page_background_top_gradient_colour
        copy._page_hover_background_colour = self._page_hover_background_colour
        copy._page_hover_background_gradient_colour = self._page_hover_background_gradient_colour
        copy._page_hover_background_top_colour = self._page_hover_background_top_colour
        copy._page_hover_background_top_gradient_colour = self._page_hover_background_top_gradient_colour
        copy._button_bar_hover_background_colour = self._button_bar_hover_background_colour
        copy._button_bar_hover_background_gradient_colour = self._button_bar_hover_background_gradient_colour
        copy._button_bar_hover_background_top_colour = self._button_bar_hover_background_top_colour
        copy._button_bar_hover_background_top_gradient_colour = self._button_bar_hover_background_top_gradient_colour
        copy._button_bar_active_background_colour = self._button_bar_active_background_colour
        copy._button_bar_active_background_gradient_colour = self._button_bar_active_background_gradient_colour
        copy._button_bar_active_background_top_colour = self._button_bar_active_background_top_colour
        copy._button_bar_active_background_top_gradient_colour = self._button_bar_active_background_top_gradient_colour
        copy._gallery_button_background_colour = self._gallery_button_background_colour
        copy._gallery_button_background_gradient_colour = self._gallery_button_background_gradient_colour    
        copy._gallery_button_hover_background_colour = self._gallery_button_hover_background_colour
        copy._gallery_button_hover_background_gradient_colour = self._gallery_button_hover_background_gradient_colour
        copy._gallery_button_active_background_colour = self._gallery_button_active_background_colour
        copy._gallery_button_active_background_gradient_colour = self._gallery_button_active_background_gradient_colour
        copy._gallery_button_disabled_background_colour = self._gallery_button_disabled_background_colour
        copy._gallery_button_disabled_background_gradient_colour = self._gallery_button_disabled_background_gradient_colour
        copy._gallery_button_face_colour = self._gallery_button_face_colour
        copy._gallery_button_hover_face_colour = self._gallery_button_hover_face_colour
        copy._gallery_button_active_face_colour = self._gallery_button_active_face_colour
        copy._gallery_button_disabled_face_colour = self._gallery_button_disabled_face_colour

        copy._tab_ctrl_background_brush = self._tab_ctrl_background_brush
        copy._panel_label_background_brush = self._panel_label_background_brush
        copy._panel_hover_label_background_brush = self._panel_hover_label_background_brush
        copy._panel_hover_button_background_brush = self._panel_hover_button_background_brush
        copy._gallery_hover_background_brush = self._gallery_hover_background_brush
        copy._gallery_button_background_top_brush = self._gallery_button_background_top_brush
        copy._gallery_button_hover_background_top_brush = self._gallery_button_hover_background_top_brush
        copy._gallery_button_active_background_top_brush = self._gallery_button_active_background_top_brush
        copy._gallery_button_disabled_background_top_brush = self._gallery_button_disabled_background_top_brush

        copy._tab_label_font = self._tab_label_font
        copy._button_bar_label_font = self._button_bar_label_font
        copy._panel_label_font = self._panel_label_font

        copy._page_border_pen = self._page_border_pen
        copy._panel_border_pen = self._panel_border_pen
        copy._panel_border_gradient_pen = self._panel_border_gradient_pen
        copy._panel_hover_button_border_pen = self._panel_hover_button_border_pen
        copy._panel_minimised_border_pen = self._panel_minimised_border_pen
        copy._panel_minimised_border_gradient_pen = self._panel_minimised_border_gradient_pen
        copy._tab_border_pen = self._tab_border_pen
        copy._gallery_border_pen = self._gallery_border_pen
        copy._button_bar_hover_border_pen = self._button_bar_hover_border_pen
        copy._button_bar_active_border_pen = self._button_bar_active_border_pen
        copy._gallery_item_border_pen = self._gallery_item_border_pen
        copy._toolbar_border_pen = self._toolbar_border_pen

        copy._flags = self._flags
        copy._tab_separation_size = self._tab_separation_size
        copy._page_border_left = self._page_border_left
        copy._page_border_top = self._page_border_top
        copy._page_border_right = self._page_border_right
        copy._page_border_bottom = self._page_border_bottom
        copy._panel_x_separation_size = self._panel_x_separation_size
        copy._panel_y_separation_size = self._panel_y_separation_size
        copy._gallery_bitmap_padding_left_size = self._gallery_bitmap_padding_left_size
        copy._gallery_bitmap_padding_right_size = self._gallery_bitmap_padding_right_size
        copy._gallery_bitmap_padding_top_size = self._gallery_bitmap_padding_top_size
        copy._gallery_bitmap_padding_bottom__size = self._gallery_bitmap_padding_bottom__size


    def GetFlags(self):
        """
        Get the previously set style flags.
        """

        return self._flags


    def SetFlags(self, flags):
        """
        Set the style flags.

        Normally called automatically by :meth:`RibbonBar.SetArtProvider() <RibbonBar.SetArtProvider>` with the ribbon
        bar's style flags, so that the art provider has the same flags as the bar which
        it is serving.

        :param `flags`: MISSING DESCRIPTION.

        """

        if (flags ^ self._flags) & RIBBON_BAR_FLOW_VERTICAL:        
            if flags & RIBBON_BAR_FLOW_VERTICAL:            
                self._page_border_left += 1
                self._page_border_right += 1
                self._page_border_top -= 1
                self._page_border_bottom -= 1
            else:            
                self._page_border_left -= 1
                self._page_border_right -= 1
                self._page_border_top += 1
                self._page_border_bottom += 1
            
        self._flags = flags

        # Need to reload some bitmaps when flags change
        self.Reload(RIBBON_ART_GALLERY_BUTTON_FACE_COLOUR)
        self.Reload(RIBBON_ART_GALLERY_BUTTON_HOVER_FACE_COLOUR)
        self.Reload(RIBBON_ART_GALLERY_BUTTON_ACTIVE_FACE_COLOUR)
        self.Reload(RIBBON_ART_GALLERY_BUTTON_DISABLED_FACE_COLOUR)
        self.Reload(RIBBON_ART_PANEL_BUTTON_FACE_COLOUR)
        self.Reload(RIBBON_ART_PANEL_BUTTON_HOVER_FACE_COLOUR)
        

    def Reload(self, setting):

        self.SetColour(setting, self.GetColour(setting))


    def GetMetric(self, id):
        """
        Get the value of a certain integer setting.

        can be one of the size values of `RibbonArtSetting`.

        :param `id`: a metric id.

        """

        if id == RIBBON_ART_TAB_SEPARATION_SIZE:
            return self._tab_separation_size
        elif id == RIBBON_ART_PAGE_BORDER_LEFT_SIZE:
            return self._page_border_left
        elif id == RIBBON_ART_PAGE_BORDER_TOP_SIZE:
            return self._page_border_top
        elif id == RIBBON_ART_PAGE_BORDER_RIGHT_SIZE:
            return self._page_border_right
        elif id == RIBBON_ART_PAGE_BORDER_BOTTOM_SIZE:
            return self._page_border_bottom
        elif id == RIBBON_ART_PANEL_X_SEPARATION_SIZE:
            return self._panel_x_separation_size
        elif id == RIBBON_ART_PANEL_Y_SEPARATION_SIZE:
            return self._panel_y_separation_size
        elif id == RIBBON_ART_TOOL_GROUP_SEPARATION_SIZE:
            return self._tool_group_separation_size
        elif id == RIBBON_ART_GALLERY_BITMAP_PADDING_LEFT_SIZE:
            return self._gallery_bitmap_padding_left_size
        elif id == RIBBON_ART_GALLERY_BITMAP_PADDING_RIGHT_SIZE:
            return self._gallery_bitmap_padding_right_size
        elif id == RIBBON_ART_GALLERY_BITMAP_PADDING_TOP_SIZE:
            return self._gallery_bitmap_padding_top_size
        elif id == RIBBON_ART_GALLERY_BITMAP_PADDING_BOTTOM_SIZE:
            return self._gallery_bitmap_padding_bottom__size
        else:
            raise Exception("Invalid Metric Ordinal")


    def SetMetric(self, id, new_val):
        """
        Set the value of a certain integer setting to the value.

        can be one of the size values of `RibbonArtSetting`.

        :param `id`: a metric id;
        :param `new_val`: the new value of the metric setting.

        """

        if id == RIBBON_ART_TAB_SEPARATION_SIZE:
            self._tab_separation_size = new_val
        elif id == RIBBON_ART_PAGE_BORDER_LEFT_SIZE:
            self._page_border_left = new_val
        elif id == RIBBON_ART_PAGE_BORDER_TOP_SIZE:
            self._page_border_top = new_val
        elif id == RIBBON_ART_PAGE_BORDER_RIGHT_SIZE:
            self._page_border_right = new_val
        elif id == RIBBON_ART_PAGE_BORDER_BOTTOM_SIZE:
            self._page_border_bottom = new_val
        elif id == RIBBON_ART_PANEL_X_SEPARATION_SIZE:
            self._panel_x_separation_size = new_val
        elif id == RIBBON_ART_PANEL_Y_SEPARATION_SIZE:
            self._panel_y_separation_size = new_val
        elif id == RIBBON_ART_TOOL_GROUP_SEPARATION_SIZE:
            self._tool_group_separation_size = new_val
        elif id == RIBBON_ART_GALLERY_BITMAP_PADDING_LEFT_SIZE:
            self._gallery_bitmap_padding_left_size = new_val
        elif id == RIBBON_ART_GALLERY_BITMAP_PADDING_RIGHT_SIZE:
            self._gallery_bitmap_padding_right_size = new_val
        elif id == RIBBON_ART_GALLERY_BITMAP_PADDING_TOP_SIZE:
            self._gallery_bitmap_padding_top_size = new_val
        elif id == RIBBON_ART_GALLERY_BITMAP_PADDING_BOTTOM_SIZE:
            self._gallery_bitmap_padding_bottom__size = new_val
        else:
            raise Exception("Invalid Metric Ordinal")
    

    def SetFont(self, id, font):
        """
        Set the value of a certain font setting to the value.

        can be one of the font values of `RibbonArtSetting`.

        :param `id`: a font id;
        :param `font`: the new font.

        """
        
        if id == RIBBON_ART_TAB_LABEL_FONT:
            self._tab_label_font = font
        elif id == RIBBON_ART_BUTTON_BAR_LABEL_FONT:
            self._button_bar_label_font = font
        elif id == RIBBON_ART_PANEL_LABEL_FONT:
            self._panel_label_font = font
        else:
            raise Exception("Invalid Font Ordinal")


    def GetFont(self, id):
        """
        Get the value of a certain font setting.

        can be one of the font values of `RibbonArtSetting`.

        :param `id`: the font id.

        """

        if id == RIBBON_ART_TAB_LABEL_FONT:
            return self._tab_label_font
        elif id == RIBBON_ART_BUTTON_BAR_LABEL_FONT:
            return self._button_bar_label_font
        elif id == RIBBON_ART_PANEL_LABEL_FONT:
            return self._panel_label_font
        else:
            raise Exception("Invalid Font Ordinal")


    def GetColour(self, id):
        """
        Get the value of a certain colour setting.

        can be one of the colour values of `RibbonArtSetting`.

        :param `id`: the colour id.

        """

        if id == RIBBON_ART_BUTTON_BAR_LABEL_COLOUR:
            return self._button_bar_label_colour
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BORDER_COLOUR:
            return self._button_bar_hover_border_pen.GetColour()
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_TOP_COLOUR:
            return self._button_bar_hover_background_top_colour
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_TOP_GRADIENT_COLOUR:
            return self._button_bar_hover_background_top_gradient_colour
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_COLOUR:
            return self._button_bar_hover_background_colour
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_GRADIENT_COLOUR:
            return self._button_bar_hover_background_gradient_colour
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BORDER_COLOUR:
            return self._button_bar_active_border_pen.GetColour()
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BACKGROUND_TOP_COLOUR:
            return self._button_bar_active_background_top_colour
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BACKGROUND_TOP_GRADIENT_COLOUR:
            return self._button_bar_active_background_top_gradient_colour
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BACKGROUND_COLOUR:
            return self._button_bar_active_background_colour
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BACKGROUND_GRADIENT_COLOUR:
            return self._button_bar_active_background_gradient_colour
        elif id == RIBBON_ART_GALLERY_BORDER_COLOUR:
            return self._gallery_border_pen.GetColour()
        elif id == RIBBON_ART_GALLERY_HOVER_BACKGROUND_COLOUR:
            return self._gallery_hover_background_brush.GetColour()
        elif id == RIBBON_ART_GALLERY_BUTTON_BACKGROUND_COLOUR:
            return self._gallery_button_background_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_BACKGROUND_GRADIENT_COLOUR:
            return self._gallery_button_background_gradient_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_BACKGROUND_TOP_COLOUR:
            return self._gallery_button_background_top_brush.GetColour()
        elif id == RIBBON_ART_GALLERY_BUTTON_FACE_COLOUR:
            return self._gallery_button_face_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_COLOUR:
            return self._gallery_button_hover_background_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_GRADIENT_COLOUR:
            return self._gallery_button_hover_background_gradient_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_TOP_COLOUR:
            return self._gallery_button_hover_background_top_brush.GetColour()
        elif id == RIBBON_ART_GALLERY_BUTTON_HOVER_FACE_COLOUR:
            return self._gallery_button_face_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_COLOUR:
            return self._gallery_button_active_background_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_GRADIENT_COLOUR:
            return self._gallery_button_active_background_gradient_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_TOP_COLOUR:
            return self._gallery_button_background_top_brush.GetColour()
        elif id == RIBBON_ART_GALLERY_BUTTON_ACTIVE_FACE_COLOUR:
            return self._gallery_button_active_face_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_COLOUR:
            return self._gallery_button_disabled_background_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_GRADIENT_COLOUR:
            return self._gallery_button_disabled_background_gradient_colour
        elif id == RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_TOP_COLOUR:
            return self._gallery_button_disabled_background_top_brush.GetColour()
        elif id == RIBBON_ART_GALLERY_BUTTON_DISABLED_FACE_COLOUR:
            return self._gallery_button_disabled_face_colour
        elif id == RIBBON_ART_GALLERY_ITEM_BORDER_COLOUR:
            return self._gallery_item_border_pen.GetColour()
        elif id in [RIBBON_ART_TAB_CTRL_BACKGROUND_COLOUR, RIBBON_ART_TAB_CTRL_BACKGROUND_GRADIENT_COLOUR]:
            return self._tab_ctrl_background_brush.GetColour()
        elif id == RIBBON_ART_TAB_LABEL_COLOUR:
            return self._tab_label_colour
        elif id == RIBBON_ART_TAB_SEPARATOR_COLOUR:
            return self._tab_separator_colour
        elif id == RIBBON_ART_TAB_SEPARATOR_GRADIENT_COLOUR:
            return self._tab_separator_gradient_colour
        elif id in [RIBBON_ART_TAB_ACTIVE_BACKGROUND_TOP_COLOUR, RIBBON_ART_TAB_ACTIVE_BACKGROUND_TOP_GRADIENT_COLOUR]:
            return wx.Colour(0, 0, 0)
        elif id == RIBBON_ART_TAB_ACTIVE_BACKGROUND_COLOUR:
            return self._tab_active_background_colour
        elif id == RIBBON_ART_TAB_ACTIVE_BACKGROUND_GRADIENT_COLOUR:
            return self._tab_active_background_gradient_colour
        elif id == RIBBON_ART_TAB_HOVER_BACKGROUND_TOP_COLOUR:
            return self._tab_hover_background_top_colour
        elif id == RIBBON_ART_TAB_HOVER_BACKGROUND_TOP_GRADIENT_COLOUR:
            return self._tab_hover_background_top_gradient_colour
        elif id == RIBBON_ART_TAB_HOVER_BACKGROUND_COLOUR:
            return self._tab_hover_background_colour
        elif id == RIBBON_ART_TAB_HOVER_BACKGROUND_GRADIENT_COLOUR:
            return self._tab_hover_background_gradient_colour
        elif id == RIBBON_ART_TAB_BORDER_COLOUR:
            return self._tab_border_pen.GetColour()
        elif id == RIBBON_ART_PANEL_BORDER_COLOUR:
            return self._panel_border_pen.GetColour()
        elif id == RIBBON_ART_PANEL_BORDER_GRADIENT_COLOUR:
            return self._panel_border_gradient_pen.GetColour()
        elif id == RIBBON_ART_PANEL_MINIMISED_BORDER_COLOUR:
            return self._panel_minimised_border_pen.GetColour()
        elif id == RIBBON_ART_PANEL_MINIMISED_BORDER_GRADIENT_COLOUR:
            return self._panel_minimised_border_gradient_pen.GetColour()
        elif id in [RIBBON_ART_PANEL_LABEL_BACKGROUND_COLOUR, RIBBON_ART_PANEL_LABEL_BACKGROUND_GRADIENT_COLOUR]:
            return self._panel_label_background_brush.GetColour()
        elif id == RIBBON_ART_PANEL_LABEL_COLOUR:
            return self._panel_label_colour
        elif id == RIBBON_ART_PANEL_MINIMISED_LABEL_COLOUR:
            return self._panel_minimised_label_colour
        elif id in [RIBBON_ART_PANEL_HOVER_LABEL_BACKGROUND_COLOUR, RIBBON_ART_PANEL_HOVER_LABEL_BACKGROUND_GRADIENT_COLOUR]:
            return self._panel_hover_label_background_brush.GetColour()
        elif id == RIBBON_ART_PANEL_HOVER_LABEL_COLOUR:
            return self._panel_hover_label_colour
        elif id == RIBBON_ART_PANEL_ACTIVE_BACKGROUND_TOP_COLOUR:
            return self._panel_active_background_top_colour
        elif id == RIBBON_ART_PANEL_ACTIVE_BACKGROUND_TOP_GRADIENT_COLOUR:
            return self._panel_active_background_top_gradient_colour
        elif id == RIBBON_ART_PANEL_ACTIVE_BACKGROUND_COLOUR:
            return self._panel_active_background_colour
        elif id == RIBBON_ART_PANEL_ACTIVE_BACKGROUND_GRADIENT_COLOUR:
            return self._panel_active_background_gradient_colour
        elif id == RIBBON_ART_PANEL_BUTTON_FACE_COLOUR:
            return self._panel_button_face_colour
        elif id == RIBBON_ART_PANEL_BUTTON_HOVER_FACE_COLOUR:
            return self._panel_button_hover_face_colour
        elif id == RIBBON_ART_PAGE_BORDER_COLOUR:
            return self._page_border_pen.GetColour()
        elif id == RIBBON_ART_PAGE_BACKGROUND_TOP_COLOUR:
            return self._page_background_top_colour
        elif id == RIBBON_ART_PAGE_BACKGROUND_TOP_GRADIENT_COLOUR:
            return self._page_background_top_gradient_colour
        elif id == RIBBON_ART_PAGE_BACKGROUND_COLOUR:
            return self._page_background_colour
        elif id == RIBBON_ART_PAGE_BACKGROUND_GRADIENT_COLOUR:
            return self._page_background_gradient_colour
        elif id == RIBBON_ART_PAGE_HOVER_BACKGROUND_TOP_COLOUR:
            return self._page_hover_background_top_colour
        elif id == RIBBON_ART_PAGE_HOVER_BACKGROUND_TOP_GRADIENT_COLOUR:
            return self._page_hover_background_top_gradient_colour
        elif id == RIBBON_ART_PAGE_HOVER_BACKGROUND_COLOUR:
            return self._page_hover_background_colour
        elif id == RIBBON_ART_PAGE_HOVER_BACKGROUND_GRADIENT_COLOUR:
            return self._page_hover_background_gradient_colour
        elif id in [RIBBON_ART_TOOLBAR_BORDER_COLOUR, RIBBON_ART_TOOLBAR_HOVER_BORDER_COLOUR]:
            return self._toolbar_border_pen.GetColour()
        elif id == RIBBON_ART_TOOLBAR_FACE_COLOUR:
            return self._tool_face_colour
        else:
            raise Exception("Invalid Colour Ordinal")


    def SetColour(self, id, colour):
        """
        Set the value of a certain colour setting to the value.

        can be one of the colour values of `RibbonArtSetting`, though not all colour
        settings will have an affect on every art provider.

        :param `id`: the colour id;
        :param `colour`: the colour.

        :see: :meth:`~RibbonMSWArtProvider.SetColourScheme`
        """
    
        if id == RIBBON_ART_BUTTON_BAR_LABEL_COLOUR:
            self._button_bar_label_colour = colour
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BORDER_COLOUR:
            self._button_bar_hover_border_pen.SetColour(colour)
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_TOP_COLOUR:
            self._button_bar_hover_background_top_colour = colour
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_TOP_GRADIENT_COLOUR:
            self._button_bar_hover_background_top_gradient_colour = colour
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_COLOUR:
            self._button_bar_hover_background_colour = colour
        elif id == RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_GRADIENT_COLOUR:
            self._button_bar_hover_background_gradient_colour = colour
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BORDER_COLOUR:
            self._button_bar_active_border_pen.SetColour(colour)
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BACKGROUND_TOP_COLOUR:
            self._button_bar_active_background_top_colour = colour
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BACKGROUND_TOP_GRADIENT_COLOUR:
            self._button_bar_active_background_top_gradient_colour = colour
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BACKGROUND_COLOUR:
            self._button_bar_active_background_colour = colour
        elif id == RIBBON_ART_BUTTON_BAR_ACTIVE_BACKGROUND_GRADIENT_COLOUR:
            self._button_bar_active_background_gradient_colour = colour
        elif id == RIBBON_ART_GALLERY_BORDER_COLOUR:
            self._gallery_border_pen.SetColour(colour)
        elif id == RIBBON_ART_GALLERY_HOVER_BACKGROUND_COLOUR:
            self._gallery_hover_background_brush.SetColour(colour)
        elif id == RIBBON_ART_GALLERY_BUTTON_BACKGROUND_COLOUR:
            self._gallery_button_background_colour = colour
        elif id == RIBBON_ART_GALLERY_BUTTON_BACKGROUND_GRADIENT_COLOUR:
            self._gallery_button_background_gradient_colour = colour
        elif id == RIBBON_ART_GALLERY_BUTTON_BACKGROUND_TOP_COLOUR:
            self._gallery_button_background_top_brush.SetColour(colour)
        elif id == RIBBON_ART_GALLERY_BUTTON_FACE_COLOUR:
            self._gallery_button_face_colour = colour
            
            if self._flags & RIBBON_BAR_FLOW_VERTICAL:            
                self._gallery_up_bitmap[0] = RibbonLoadPixmap(gallery_left_xpm, colour)
                self._gallery_down_bitmap[0] = RibbonLoadPixmap(gallery_right_xpm, colour)
            else:            
                self._gallery_up_bitmap[0] = RibbonLoadPixmap(gallery_up_xpm, colour)
                self._gallery_down_bitmap[0] = RibbonLoadPixmap(gallery_down_xpm, colour)
            
            self._gallery_extension_bitmap[0] = RibbonLoadPixmap(gallery_extension_xpm, colour)
            
        elif id == RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_COLOUR:
            self._gallery_button_hover_background_colour = colour
        elif id == RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_GRADIENT_COLOUR:
            self._gallery_button_hover_background_gradient_colour = colour
        elif id == RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_TOP_COLOUR:
            self._gallery_button_hover_background_top_brush.SetColour(colour)
        elif id == RIBBON_ART_GALLERY_BUTTON_HOVER_FACE_COLOUR:
            self._gallery_button_hover_face_colour = colour
            
            if self._flags & RIBBON_BAR_FLOW_VERTICAL:
                self._gallery_up_bitmap[1] = RibbonLoadPixmap(gallery_left_xpm, colour)
                self._gallery_down_bitmap[1] = RibbonLoadPixmap(gallery_right_xpm, colour)
            else:            
                self._gallery_up_bitmap[1] = RibbonLoadPixmap(gallery_up_xpm, colour)
                self._gallery_down_bitmap[1] = RibbonLoadPixmap(gallery_down_xpm, colour)
            
            self._gallery_extension_bitmap[1] = RibbonLoadPixmap(gallery_extension_xpm, colour)
            
        elif id == RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_COLOUR:
            self._gallery_button_active_background_colour = colour
        elif id == RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_GRADIENT_COLOUR:
            self._gallery_button_active_background_gradient_colour = colour
        elif id == RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_TOP_COLOUR:
            self._gallery_button_background_top_brush.SetColour(colour)
        elif id == RIBBON_ART_GALLERY_BUTTON_ACTIVE_FACE_COLOUR:
            self._gallery_button_active_face_colour = colour

            if self._flags & RIBBON_BAR_FLOW_VERTICAL:
                self._gallery_up_bitmap[2] = RibbonLoadPixmap(gallery_left_xpm, colour)
                self._gallery_down_bitmap[2] = RibbonLoadPixmap(gallery_right_xpm, colour)
            else:            
                self._gallery_up_bitmap[2] = RibbonLoadPixmap(gallery_up_xpm, colour)
                self._gallery_down_bitmap[2] = RibbonLoadPixmap(gallery_down_xpm, colour)
            
            self._gallery_extension_bitmap[2] = RibbonLoadPixmap(gallery_extension_xpm, colour)

        elif id == RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_COLOUR:
            self._gallery_button_disabled_background_colour = colour
        elif id == RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_GRADIENT_COLOUR:
            self._gallery_button_disabled_background_gradient_colour = colour
        elif id == RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_TOP_COLOUR:
            self._gallery_button_disabled_background_top_brush.SetColour(colour)
        elif id == RIBBON_ART_GALLERY_BUTTON_DISABLED_FACE_COLOUR:
            self._gallery_button_disabled_face_colour = colour
            
            if self._flags & RIBBON_BAR_FLOW_VERTICAL:
                self._gallery_up_bitmap[3] = RibbonLoadPixmap(gallery_left_xpm, colour)
                self._gallery_down_bitmap[3] = RibbonLoadPixmap(gallery_right_xpm, colour)            
            else:            
                self._gallery_up_bitmap[3] = RibbonLoadPixmap(gallery_up_xpm, colour)
                self._gallery_down_bitmap[3] = RibbonLoadPixmap(gallery_down_xpm, colour)
            
            self._gallery_extension_bitmap[3] = RibbonLoadPixmap(gallery_extension_xpm, colour)

        elif id == RIBBON_ART_GALLERY_ITEM_BORDER_COLOUR:
            self._gallery_item_border_pen.SetColour(colour)

        elif id in [RIBBON_ART_TAB_CTRL_BACKGROUND_COLOUR, RIBBON_ART_TAB_CTRL_BACKGROUND_GRADIENT_COLOUR]:
            self._tab_ctrl_background_brush.SetColour(colour)
            self._cached_tab_separator_visibility = -1.0
        elif id == RIBBON_ART_TAB_LABEL_COLOUR:
            self._tab_label_colour = colour
        elif id == RIBBON_ART_TAB_SEPARATOR_COLOUR:
            self._tab_separator_colour = colour
            self._cached_tab_separator_visibility = -1.0
        elif id == RIBBON_ART_TAB_SEPARATOR_GRADIENT_COLOUR:
            self._tab_separator_gradient_colour = colour
            self._cached_tab_separator_visibility = -1.0
        elif id in [RIBBON_ART_TAB_ACTIVE_BACKGROUND_TOP_COLOUR, RIBBON_ART_TAB_ACTIVE_BACKGROUND_TOP_GRADIENT_COLOUR]:
            pass
        elif id == RIBBON_ART_TAB_ACTIVE_BACKGROUND_COLOUR:
            self._tab_active_background_colour = colour
        elif id == RIBBON_ART_TAB_ACTIVE_BACKGROUND_GRADIENT_COLOUR:
            self._tab_active_background_gradient_colour = colour
        elif id == RIBBON_ART_TAB_HOVER_BACKGROUND_TOP_COLOUR:
            self._tab_hover_background_top_colour = colour
        elif id == RIBBON_ART_TAB_HOVER_BACKGROUND_TOP_GRADIENT_COLOUR:
            self._tab_hover_background_top_gradient_colour = colour
        elif id == RIBBON_ART_TAB_HOVER_BACKGROUND_COLOUR:
            self._tab_hover_background_colour = colour
        elif id == RIBBON_ART_TAB_HOVER_BACKGROUND_GRADIENT_COLOUR:
            self._tab_hover_background_gradient_colour = colour
        elif id == RIBBON_ART_TAB_BORDER_COLOUR:
            self._tab_border_pen.SetColour(colour)
        elif id == RIBBON_ART_PANEL_BORDER_COLOUR:
            self._panel_border_pen.SetColour(colour)
        elif id == RIBBON_ART_PANEL_BORDER_GRADIENT_COLOUR:
            self._panel_border_gradient_pen.SetColour(colour)
        elif id == RIBBON_ART_PANEL_MINIMISED_BORDER_COLOUR:
            self._panel_minimised_border_pen.SetColour(colour)
        elif id == RIBBON_ART_PANEL_MINIMISED_BORDER_GRADIENT_COLOUR:
            self._panel_minimised_border_gradient_pen.SetColour(colour)
        elif id in [RIBBON_ART_PANEL_LABEL_BACKGROUND_COLOUR, RIBBON_ART_PANEL_LABEL_BACKGROUND_GRADIENT_COLOUR]:
            self._panel_label_background_brush.SetColour(colour)
        elif id == RIBBON_ART_PANEL_LABEL_COLOUR:
            self._panel_label_colour = colour
        elif id in [RIBBON_ART_PANEL_HOVER_LABEL_BACKGROUND_COLOUR, RIBBON_ART_PANEL_HOVER_LABEL_BACKGROUND_GRADIENT_COLOUR]:
            self._panel_hover_label_background_brush.SetColour(colour)
        elif id == RIBBON_ART_PANEL_HOVER_LABEL_COLOUR:
            self._panel_hover_label_colour = colour
        elif id == RIBBON_ART_PANEL_MINIMISED_LABEL_COLOUR:
            self._panel_minimised_label_colour = colour
        elif id == RIBBON_ART_PANEL_ACTIVE_BACKGROUND_TOP_COLOUR:
            self._panel_active_background_top_colour = colour
        elif id == RIBBON_ART_PANEL_ACTIVE_BACKGROUND_TOP_GRADIENT_COLOUR:
            self._panel_active_background_top_gradient_colour = colour
        elif id == RIBBON_ART_PANEL_ACTIVE_BACKGROUND_COLOUR:
            self._panel_active_background_colour = colour
        elif id == RIBBON_ART_PANEL_ACTIVE_BACKGROUND_GRADIENT_COLOUR:
            self._panel_active_background_gradient_colour = colour
        elif id == RIBBON_ART_PANEL_BUTTON_FACE_COLOUR:
            self._panel_button_face_colour = colour
            self._panel_extension_bitmap[0] = RibbonLoadPixmap(panel_extension_xpm, colour)
        elif id == RIBBON_ART_PANEL_BUTTON_HOVER_FACE_COLOUR:
            self._panel_button_hover_face_colour = colour
            self._panel_extension_bitmap[1] = RibbonLoadPixmap(panel_extension_xpm, colour)
        elif id == RIBBON_ART_PAGE_BORDER_COLOUR:
            self._page_border_pen.SetColour(colour)
        elif id == RIBBON_ART_PAGE_BACKGROUND_TOP_COLOUR:
            self._page_background_top_colour = colour
        elif id == RIBBON_ART_PAGE_BACKGROUND_TOP_GRADIENT_COLOUR:
            self._page_background_top_gradient_colour = colour
        elif id == RIBBON_ART_PAGE_BACKGROUND_COLOUR:
            self._page_background_colour = colour
        elif id == RIBBON_ART_PAGE_BACKGROUND_GRADIENT_COLOUR:
            self._page_background_gradient_colour = colour
        elif id == RIBBON_ART_PAGE_HOVER_BACKGROUND_TOP_COLOUR:
            self._page_hover_background_top_colour = colour
        elif id == RIBBON_ART_PAGE_HOVER_BACKGROUND_TOP_GRADIENT_COLOUR:
            self._page_hover_background_top_gradient_colour = colour
        elif id == RIBBON_ART_PAGE_HOVER_BACKGROUND_COLOUR:
            self._page_hover_background_colour = colour
        elif id == RIBBON_ART_PAGE_HOVER_BACKGROUND_GRADIENT_COLOUR:
            self._page_hover_background_gradient_colour = colour
        elif id in [RIBBON_ART_TOOLBAR_BORDER_COLOUR, RIBBON_ART_TOOLBAR_HOVER_BORDER_COLOUR]:
            self._toolbar_border_pen.SetColour(colour)
        elif id == RIBBON_ART_TOOLBAR_FACE_COLOUR:
            self._tool_face_colour = colour
            self._toolbar_drop_bitmap = RibbonLoadPixmap(gallery_down_xpm, colour)
        else:
            raise Exception("Invalid Colour Ordinal")
    

    def DrawTabCtrlBackground(self, dc, wnd, rect):
        """
        Draw the background of the tab region of a ribbon bar.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto;
        :param `rect`: The rectangle within which to draw.

        """

        dc.SetPen(wx.TRANSPARENT_PEN)

        dc.SetBrush(self._tab_ctrl_background_brush)
        dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)

        dc.SetPen(self._page_border_pen)
        
        if rect.width > 6:        
            dc.DrawLine(rect.x + 3, rect.y + rect.height - 1, rect.x + rect.width - 3, rect.y + rect.height - 1)        
        else:        
            dc.DrawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width, rect.y + rect.height - 1)
    

    def DrawTab(self, dc, wnd, tab):
        """
        Draw a single tab in the tab region of a ribbon bar.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto (not the :class:`~lib.agw.ribbon.page.RibbonPage` associated
         with the tab being drawn);
        :param `tab`: The rectangle within which to draw, and also the tab label, icon, and
         state (active and/or hovered). The drawing rectangle will be entirely within a
         rectangle on the same device context previously painted with :meth:`~RibbonMSWArtProvider.DrawTabCtrlBackground`.
         The rectangle's width will be at least the minimum value returned by :meth:`~RibbonMSWArtProvider.GetBarTabWidth`,
         and height will be the value returned by :meth:`~RibbonMSWArtProvider.GetTabCtrlHeight`.

        """

        if tab.rect.height <= 2:
            return

        if tab.active or tab.hovered:        
            if tab.active:            
                background = wx.Rect(*tab.rect)
                background.x += 2
                background.y += 2
                background.width -= 4
                background.height -= 2

                dc.GradientFillLinear(background, self._tab_active_background_colour,
                                      self._tab_active_background_gradient_colour, wx.SOUTH)

                # TODO: active and hovered
            
            elif tab.hovered:            
                background = wx.Rect(*tab.rect)
                background.x += 2
                background.y += 2
                background.width -= 4
                background.height -= 3
                h = background.height
                background.height /= 2
                dc.GradientFillLinear(background, self._tab_hover_background_top_colour,
                                      self._tab_hover_background_top_gradient_colour, wx.SOUTH)

                background.y += background.height
                background.height = h - background.height
                dc.GradientFillLinear(background, self._tab_hover_background_colour,
                                      self._tab_hover_background_gradient_colour, wx.SOUTH)
           
            border_points = [wx.Point() for i in xrange(6)]
            border_points[0] = wx.Point(1, tab.rect.height - 2)
            border_points[1] = wx.Point(1, 3)
            border_points[2] = wx.Point(3, 1)
            border_points[3] = wx.Point(tab.rect.width - 4, 1)
            border_points[4] = wx.Point(tab.rect.width - 2, 3)
            border_points[5] = wx.Point(tab.rect.width - 2, tab.rect.height - 1)

            dc.SetPen(self._tab_border_pen)
            dc.DrawLines(border_points, tab.rect.x, tab.rect.y)

            if tab.active:            
                # Give the tab a curved outward border at the bottom
                dc.DrawPoint(tab.rect.x, tab.rect.y + tab.rect.height - 2)
                dc.DrawPoint(tab.rect.x + tab.rect.width - 1, tab.rect.y + tab.rect.height - 2)

                p = wx.Pen(self._tab_active_background_gradient_colour)
                dc.SetPen(p)

                # Technically the first two points are the wrong colour, but they're near enough
                dc.DrawPoint(tab.rect.x + 1, tab.rect.y + tab.rect.height - 2)
                dc.DrawPoint(tab.rect.x + tab.rect.width - 2, tab.rect.y + tab.rect.height - 2)
                dc.DrawPoint(tab.rect.x + 1, tab.rect.y + tab.rect.height - 1)
                dc.DrawPoint(tab.rect.x, tab.rect.y + tab.rect.height - 1)
                dc.DrawPoint(tab.rect.x + tab.rect.width - 2, tab.rect.y + tab.rect.height - 1)
                dc.DrawPoint(tab.rect.x + tab.rect.width - 1, tab.rect.y + tab.rect.height - 1)
            
        if self._flags & RIBBON_BAR_SHOW_PAGE_ICONS:
            icon = tab.page.GetIcon()

            if icon.IsOk():
                x = tab.rect.x + 4
                if self._flags & RIBBON_BAR_SHOW_PAGE_LABELS == 0:
                    x = tab.rect.x + (tab.rect.width - icon.GetWidth()) / 2
                    
                dc.DrawBitmap(icon, x, tab.rect.y + 1 + (tab.rect.height - 1 - icon.GetHeight()) / 2, True)
        
        if self._flags & RIBBON_BAR_SHOW_PAGE_LABELS:
            label = tab.page.GetLabel()
            if label.strip():            
                dc.SetFont(self._tab_label_font)
                dc.SetTextForeground(self._tab_label_colour)
                dc.SetBackgroundMode(wx.TRANSPARENT)

                text_width, text_height = dc.GetTextExtent(label)
                width = tab.rect.width - 5
                x = tab.rect.x + 3
                
                if self._flags & RIBBON_BAR_SHOW_PAGE_ICONS:                
                    x += 3 + tab.page.GetIcon().GetWidth()
                    width -= 3 + tab.page.GetIcon().GetWidth()
                
                y = tab.rect.y + (tab.rect.height - text_height) / 2

                if width <= text_width:                
                    dc.SetClippingRegion(x, tab.rect.y, width, tab.rect.height)
                    dc.DrawText(label, x, y)
                else:                
                    dc.DrawText(label, x + (width - text_width) / 2 + 1, y)

                
    def DrawTabSeparator(self, dc, wnd, rect, visibility):
        """
        Draw a separator between two tabs in a ribbon bar.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto;
        :param `rect`: The rectangle within which to draw, which will be entirely
         within a rectangle on the same device context previously painted with
         :meth:`~RibbonMSWArtProvider.DrawTabCtrlBackground`;
        :param `visibility`: The opacity with which to draw the separator. Values
         are in the range [0, 1], with 0 being totally transparent, and 1 being totally
         opaque.

        """

        if visibility <= 0.0:        
            return
        
        if visibility > 1.0:        
            visibility = 1.0
        
        # The tab separator is relatively expensive to draw (for its size), and is
        # usually drawn multiple times sequentially (in different positions), so it
        # makes sense to draw it once and cache it.
        if not self._cached_tab_separator.IsOk() or self._cached_tab_separator.GetSize() != rect.GetSize() or \
           visibility != self._cached_tab_separator_visibility:
        
            size = wx.Rect(0, 0, *rect.GetSize())
            self.ReallyDrawTabSeparator(wnd, size, visibility)
        
        dc.DrawBitmap(self._cached_tab_separator, rect.x, rect.y, False)


    def ReallyDrawTabSeparator(self, wnd, rect, visibility):

        if not self._cached_tab_separator.IsOk() or self._cached_tab_separator.GetSize() != rect.GetSize():
            self._cached_tab_separator = wx.EmptyBitmap(*rect.GetSize())
    
        dc = wx.MemoryDC(self._cached_tab_separator)
        self.DrawTabCtrlBackground(dc, wnd, rect)

        x = rect.x + rect.width / 2
        h = float(rect.height - 1)

        r1 = self._tab_ctrl_background_brush.GetColour().Red() * (1.0 - visibility) + 0.5
        g1 = self._tab_ctrl_background_brush.GetColour().Green() * (1.0 - visibility) + 0.5
        b1 = self._tab_ctrl_background_brush.GetColour().Blue() * (1.0 - visibility) + 0.5
        r2 = self._tab_separator_colour.Red()
        g2 = self._tab_separator_colour.Green()
        b2 = self._tab_separator_colour.Blue()
        r3 = self._tab_separator_gradient_colour.Red()
        g3 = self._tab_separator_gradient_colour.Green()
        b3 = self._tab_separator_gradient_colour.Blue()

        for i in xrange(rect.height-1):
        
            p = float(i)/h

            r = (p * r3 + (1.0 - p) * r2) * visibility + r1
            g = (p * g3 + (1.0 - p) * g2) * visibility + g1
            b = (p * b3 + (1.0 - p) * b2) * visibility + b1

            P = wx.Pen(wx.Colour(r, g, b))
            dc.SetPen(P)
            dc.DrawPoint(x, rect.y + i)
        
        self._cached_tab_separator_visibility = visibility


    def DrawPartialPageBackground(self, dc, wnd, rect, allow_hovered_or_page=True, offset=None, hovered=False):

        if isinstance(allow_hovered_or_page, types.BooleanType):
            self.DrawPartialPageBackground2(dc, wnd, rect, allow_hovered_or_page)
        else:
            self.DrawPartialPageBackground1(dc, wnd, rect, allow_hovered_or_page, offset, hovered)
            

    def DrawPartialPageBackground1(self, dc, wnd, rect, page, offset, hovered=False):

        background = wx.Rect(0, 0, *page.GetSize())
        background = page.AdjustRectToIncludeScrollButtons(background)
        background.height -= 2
        
        # Page background isn't dependant upon the width of the page
        # (at least not the part of it intended to be painted by this
        # function). Set to wider than the page itself for when externally
        # expanded panels need a background - the expanded panel can be wider
        # than the bar.

        background.x = 0
        background.width = 10000

        # upper_rect, lower_rect, paint_rect are all in page co-ordinates
        upper_rect = wx.Rect(*background)
        upper_rect.height /= 5

        lower_rect = wx.Rect(*background)
        lower_rect.y += upper_rect.height
        lower_rect.height -= upper_rect.height

        paint_rect = wx.Rect(*rect)
        paint_rect.x += offset.x
        paint_rect.y += offset.y

        if hovered:        
            bg_top = self._page_hover_background_top_colour
            bg_top_grad = self._page_hover_background_top_gradient_colour
            bg_btm = self._page_hover_background_colour
            bg_btm_grad = self._page_hover_background_gradient_colour        
        else:        
            bg_top = self._page_background_top_colour
            bg_top_grad = self._page_background_top_gradient_colour
            bg_btm = self._page_background_colour
            bg_btm_grad = self._page_background_gradient_colour
        
        if paint_rect.Intersects(upper_rect):        
            rect = wx.Rect(*upper_rect)
            rect.Intersect(paint_rect)
            rect.x -= offset.x
            rect.y -= offset.y
            starting_colour = RibbonInterpolateColour(bg_top, bg_top_grad,
                                                      paint_rect.y, upper_rect.y,
                                                      upper_rect.y + upper_rect.height)
            ending_colour = RibbonInterpolateColour(bg_top, bg_top_grad,
                                                    paint_rect.y + paint_rect.height, upper_rect.y,
                                                    upper_rect.y + upper_rect.height)
            dc.GradientFillLinear(rect, starting_colour, ending_colour, wx.SOUTH)
        

        if paint_rect.Intersects(lower_rect):        
            rect = wx.Rect(*lower_rect)
            rect.Intersect(paint_rect)
            rect.x -= offset.x
            rect.y -= offset.y
            starting_colour = RibbonInterpolateColour(bg_btm, bg_btm_grad,
                                                      paint_rect.y, lower_rect.y,
                                                      lower_rect.y + lower_rect.height)
            ending_colour = RibbonInterpolateColour(bg_btm, bg_btm_grad,
                                                    paint_rect.y + paint_rect.height,
                                                    lower_rect.y, lower_rect.y + lower_rect.height)
            
            dc.GradientFillLinear(rect, starting_colour, ending_colour, wx.SOUTH)

        
    def DrawPageBackground(self, dc, wnd, rect):
        """
        Draw the background of a ribbon page.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto (which is commonly the
         :class:`~lib.agw.ribbon.page.RibbonPage` whose background is being drawn, but doesn't have to be);
        :param `rect`: The rectangle within which to draw.

        :see: :meth:`~RibbonMSWArtProvider.GetPageBackgroundRedrawArea`
        """

        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBrush(self._tab_ctrl_background_brush)
        
        edge = wx.Rect(*rect)

        edge.width = 2
        dc.DrawRectangle(edge.x, edge.y, edge.width, edge.height)

        edge.x += rect.width - 2
        dc.DrawRectangle(edge.x, edge.y, edge.width, edge.height)

        edge = wx.Rect(*rect)
        edge.height = 2
        edge.y += (rect.height - edge.height)
        dc.DrawRectangle(edge.x, edge.y, edge.width, edge.height)
    
        background = wx.Rect(*rect)
        background.x += 2
        background.width -= 4
        background.height -= 2

        background.height /= 5
        dc.GradientFillLinear(background, self._page_background_top_colour,
                              self._page_background_top_gradient_colour, wx.SOUTH)

        background.y += background.height
        background.height = rect.height - 2 - background.height
        dc.GradientFillLinear(background, self._page_background_colour,
                              self._page_background_gradient_colour, wx.SOUTH)
    
        border_points = [wx.Point() for i in xrange(8)]
        border_points[0] = wx.Point(2, 0)
        border_points[1] = wx.Point(1, 1)
        border_points[2] = wx.Point(1, rect.height - 4)
        border_points[3] = wx.Point(3, rect.height - 2)
        border_points[4] = wx.Point(rect.width - 4, rect.height - 2)
        border_points[5] = wx.Point(rect.width - 2, rect.height - 4)
        border_points[6] = wx.Point(rect.width - 2, 1)
        border_points[7] = wx.Point(rect.width - 4, -1)

        dc.SetPen(self._page_border_pen)
        dc.DrawLines(border_points, rect.x, rect.y)
        

    def DrawScrollButton(self, dc, wnd, rect_, style):
        """
        Draw a ribbon-style scroll button.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto;
        :param `rect`: The rectangle within which to draw. The size of this rectangle
         will be at least the size returned by :meth:`~RibbonMSWArtProvider.GetScrollButtonMinimumSize` for a
         scroll button with the same style. For tab scroll buttons, this rectangle
         will be entirely within a rectangle on the same device context previously
         painted with :meth:`~RibbonMSWArtProvider.DrawTabCtrlBackground`, but this is not guaranteed for other
         types of button (for example, page scroll buttons will not be painted on an
         area previously painted with :meth:`~RibbonMSWArtProvider.DrawPageBackground` );
        :param `style`: A combination of flags from `RibbonScrollButtonStyle`,
         including a direction, a for flag, and one or more states.

        """

        rect = wx.Rect(*rect_)

        if (style & RIBBON_SCROLL_BTN_FOR_MASK) == RIBBON_SCROLL_BTN_FOR_PAGE:

            # Page scroll buttons do not have the luxury of rendering on top of anything
            # else, and their size includes some padding, hence the background painting
            # and size adjustment.
            dc.SetPen(wx.TRANSPARENT_PEN)
            dc.SetBrush(self._tab_ctrl_background_brush)
            dc.DrawRectangleRect(rect)
            dc.SetClippingRect(rect)
            
            result = style & RIBBON_SCROLL_BTN_DIRECTION_MASK
            
            if result == RIBBON_SCROLL_BTN_LEFT:
                rect.x += 1
            elif result == RIBBON_SCROLL_BTN_RIGHT:
                rect.y -= 1
                rect.width -= 1
            elif result == RIBBON_SCROLL_BTN_UP:
                rect.x += 1
                rect.y -= 1
                rect.width -= 2
                rect.height += 1
            elif result == RIBBON_SCROLL_BTN_DOWN:
                rect.x += 1
                rect.width -= 2
                rect.height -= 1            
        
        background = wx.Rect(*rect)
        background.x += 1
        background.y += 1
        background.width -= 2
        background.height -= 2

        if style & RIBBON_SCROLL_BTN_UP:
            background.height /= 2
        else:
            background.height /= 5
            
        dc.GradientFillLinear(background, self._page_background_top_colour,
                              self._page_background_top_gradient_colour, wx.SOUTH)

        background.y += background.height
        background.height = rect.height - 2 - background.height
        dc.GradientFillLinear(background, self._page_background_colour,
                              self._page_background_gradient_colour, wx.SOUTH)
    
        border_points = [wx.Point() for i in xrange(7)]
        result = style & RIBBON_SCROLL_BTN_DIRECTION_MASK
        
        if result == RIBBON_SCROLL_BTN_LEFT:
            border_points[0] = wx.Point(2, 0)
            border_points[1] = wx.Point(rect.width - 1, 0)
            border_points[2] = wx.Point(rect.width - 1, rect.height - 1)
            border_points[3] = wx.Point(2, rect.height - 1)
            border_points[4] = wx.Point(0, rect.height - 3)
            border_points[5] = wx.Point(0, 2)

        elif result == RIBBON_SCROLL_BTN_RIGHT:
            border_points[0] = wx.Point(0, 0)
            border_points[1] = wx.Point(rect.width - 3, 0)
            border_points[2] = wx.Point(rect.width - 1, 2)
            border_points[3] = wx.Point(rect.width - 1, rect.height - 3)
            border_points[4] = wx.Point(rect.width - 3, rect.height - 1)
            border_points[5] = wx.Point(0, rect.height - 1)

        elif result == RIBBON_SCROLL_BTN_UP:
            border_points[0] = wx.Point(2, 0)
            border_points[1] = wx.Point(rect.width - 3, 0)
            border_points[2] = wx.Point(rect.width - 1, 2)
            border_points[3] = wx.Point(rect.width - 1, rect.height - 1)
            border_points[4] = wx.Point(0, rect.height - 1)
            border_points[5] = wx.Point(0, 2)

        elif result == RIBBON_SCROLL_BTN_DOWN:
            border_points[0] = wx.Point(0, 0)
            border_points[1] = wx.Point(rect.width - 1, 0)
            border_points[2] = wx.Point(rect.width - 1, rect.height - 3)
            border_points[3] = wx.Point(rect.width - 3, rect.height - 1)
            border_points[4] = wx.Point(2, rect.height - 1)
            border_points[5] = wx.Point(0, rect.height - 3)
        
        border_points[6] = border_points[0]

        dc.SetPen(self._page_border_pen)
        dc.DrawLines(border_points, rect.x, rect.y)
    
        # NB: Code for handling hovered/active state is temporary
        arrow_points = [wx.Point() for i in xrange(3)]
        result = style & RIBBON_SCROLL_BTN_DIRECTION_MASK
        
        if result == RIBBON_SCROLL_BTN_LEFT:
            arrow_points[0] = wx.Point(rect.width / 2 - 2, rect.height / 2)
            if style & RIBBON_SCROLL_BTN_ACTIVE:
                arrow_points[0].y += 1
            arrow_points[1] = arrow_points[0] + wx.Point(3, -3)
            arrow_points[2] = arrow_points[0] + wx.Point(3,  3)

        elif result == RIBBON_SCROLL_BTN_RIGHT:
            arrow_points[0] = wx.Point(rect.width / 2 + 2, rect.height / 2)
            if style & RIBBON_SCROLL_BTN_ACTIVE:
                arrow_points[0].y += 1
            arrow_points[1] = arrow_points[0] - wx.Point(3,  3)
            arrow_points[2] = arrow_points[0] - wx.Point(3, -3)

        elif result == RIBBON_SCROLL_BTN_UP:
            arrow_points[0] = wx.Point(rect.width / 2, rect.height / 2 - 2)
            if style & RIBBON_SCROLL_BTN_ACTIVE:
                arrow_points[0].y += 1
            arrow_points[1] = arrow_points[0] + wx.Point( 3, 3)
            arrow_points[2] = arrow_points[0] + wx.Point(-3, 3)

        elif result == RIBBON_SCROLL_BTN_DOWN:
            arrow_points[0] = wx.Point(rect.width / 2, rect.height / 2 + 2)
            if style & RIBBON_SCROLL_BTN_ACTIVE:
                arrow_points[0].y += 1
            arrow_points[1] = arrow_points[0] - wx.Point( 3, 3)
            arrow_points[2] = arrow_points[0] - wx.Point(-3, 3)
        
        dc.SetPen(wx.TRANSPARENT_PEN)
        B = wx.Brush((style & RIBBON_SCROLL_BTN_HOVERED and [self._tab_active_background_colour] or [self._tab_label_colour])[0])
        dc.SetBrush(B)
        dc.DrawPolygon(arrow_points, rect.x, rect.y)
    

    def DrawDropdownArrow(self, dc, x, y, colour):

        arrow_points = [wx.Point() for i in xrange(3)]
        brush = wx.Brush(colour)
        arrow_points[0] = wx.Point(1, 2)
        arrow_points[1] = arrow_points[0] + wx.Point(-3, -3)
        arrow_points[2] = arrow_points[0] + wx.Point( 3, -3)
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBrush(brush)
        dc.DrawPolygon(arrow_points, x, y)


    def RemovePanelPadding(self, rect):

        if self._flags & RIBBON_BAR_FLOW_VERTICAL:        
            rect.y += 1
            rect.height -= 2        
        else:        
            rect.x += 1
            rect.width -= 2
        
        return rect


    def DrawPanelBackground(self, dc, wnd, rect):
        """
        Draw the background and chrome for a ribbon panel.

        This should draw the border, background, label, and any other items of a panel
        which are outside the client area of a panel. Note that when a panel is
        minimised, this function is not called - only :meth:`~RibbonMSWArtProvider.DrawMinimisedPanel` is called,
        so a background should be explicitly painted by that if required.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto, which is always the panel
         whose background and chrome is being drawn. The panel label and other panel
         attributes can be obtained by querying this;
        :param `rect`: The rectangle within which to draw.

        """

        self.DrawPartialPageBackground(dc, wnd, rect, False)

        true_rect = wx.Rect(*rect)
        true_rect = self.RemovePanelPadding(true_rect)

        dc.SetFont(self._panel_label_font)
        dc.SetPen(wx.TRANSPARENT_PEN)

        has_ext_button = wnd.HasExtButton()
        
        if wnd.IsHovered():
            dc.SetBrush(self._panel_hover_label_background_brush)
            dc.SetTextForeground(self._panel_hover_label_colour)        
        else:        
            dc.SetBrush(self._panel_label_background_brush)
            dc.SetTextForeground(self._panel_label_colour)
        
        label_rect = wx.Rect(*true_rect)
        label = wnd.GetLabel().strip()
        clip_label = False
        label_size = wx.Size(*dc.GetTextExtent(label))

        label_rect.SetX(label_rect.GetX() + 1)
        label_rect.SetWidth(label_rect.GetWidth() - 2)
        label_rect.SetHeight(label_size.GetHeight() + 2)
        label_rect.SetY(true_rect.GetBottom() - label_rect.GetHeight())
        label_height = label_rect.GetHeight()

        label_bg_rect = wx.Rect(*label_rect)

        if has_ext_button:
            label_rect.SetWidth(label_rect.GetWidth() - 13)
            
        if label_size.GetWidth() > label_rect.GetWidth():        
            # Test if there is enough length for 3 letters and ...
            new_label = label[0:3] + "..."
            label_size = wx.Size(*dc.GetTextExtent(new_label))
            
            if label_size.GetWidth() > label_rect.GetWidth():            
                # Not enough room for three characters and ...
                # Display the entire label and just crop it
                clip_label = True
            else:            
                # Room for some characters and ...
                # Display as many characters as possible and append ...
                for l in xrange(len(label)-1, 3, -1):                
                    new_label = label[0:l] + "..."
                    label_size = wx.Size(*dc.GetTextExtent(new_label))
                    if label_size.GetWidth() <= label_rect.GetWidth():                    
                        label = new_label
                        break
                    
        dc.DrawRectangleRect(label_rect)
        
        if clip_label:        
            clip = wx.DCClipper(dc, label_rect)
            dc.DrawText(label, label_rect.x, label_rect.y + (label_rect.GetHeight() - label_size.GetHeight()) / 2)        
        else:        
            dc.DrawText(label, label_rect.x + (label_rect.GetWidth() - label_size.GetWidth()) / 2,
                        label_rect.y + (label_rect.GetHeight() - label_size.GetHeight()) / 2)

        if has_ext_button:
            if wnd.IsExtButtonHovered():
                dc.SetPen(self._panel_hover_button_border_pen)
                dc.SetBrush(self._panel_hover_button_background_brush)
                dc.DrawRoundedRectangle(label_rect.GetRight(), label_rect.GetBottom() - 13, 13, 13, 1)
                dc.DrawBitmap(self._panel_extension_bitmap[1], label_rect.GetRight() + 3, label_rect.GetBottom() - 10, True)
            else:
                dc.DrawBitmap(self._panel_extension_bitmap[0], label_rect.GetRight() + 3, label_rect.GetBottom() - 10, True)
        
        if wnd.IsHovered():        
            client_rect = wx.Rect(*true_rect)
            client_rect.x += 1
            client_rect.width -= 2
            client_rect.y += 1
            client_rect.height -= 2 + label_height
            self.DrawPartialPageBackground(dc, wnd, client_rect, True)
        
        self.DrawPanelBorder(dc, true_rect, self._panel_border_pen, self._panel_border_gradient_pen)


    def GetPanelExtButtonArea(self, dc, wnd, rect):
        """
        Retrieve the extension button area rectangle.

        :param `dc`: The device context used to measure text extents;
        :param `wnd`: The panel where the extension button resides;
        :param `rect`: The panel client rectangle.
        """

        true_rect = wx.Rect(*self.RemovePanelPadding(rect))
        true_rect = wx.Rect(true_rect.GetRight()-13, true_rect.GetBottom()-13, 13, 13)
        return true_rect


    def DrawGalleryBackground(self, dc, wnd, rect):
        """
        Draw the background and chrome for a :class:`~lib.agw.ribbon.gallery.RibbonGallery` control.

        This should draw the border, brackground, scroll buttons, extension button, and
        any other UI elements which are not attached to a specific gallery item.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto, which is always the gallery
         whose background and chrome is being drawn. Attributes used during drawing like
         the gallery hover state and individual button states can be queried from this
         parameter by :meth:`RibbonGallery.IsHovered() <RibbonGallery.IsHovered>`, :meth:`RibbonGallery.GetExtensionButtonState() <RibbonGallery.GetExtensionButtonState>`,
         :meth:`RibbonGallery.GetUpButtonState() <RibbonGallery.GetUpButtonState>`, and :meth:`RibbonGallery.GetDownButtonState() <RibbonGallery.GetDownButtonState>`;
        :param `rect`: The rectangle within which to draw. This rectangle is the entire
         area of the gallery control, not just the client rectangle.

        """

        self.DrawPartialPageBackground(dc, wnd, rect)

        if wnd.IsHovered():        
            dc.SetPen(wx.TRANSPARENT_PEN)
            dc.SetBrush(self._gallery_hover_background_brush)
            if self._flags & RIBBON_BAR_FLOW_VERTICAL:            
                dc.DrawRectangle(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 16)            
            else:            
                dc.DrawRectangle(rect.x + 1, rect.y + 1, rect.width - 16, rect.height - 2)
            
        dc.SetPen(self._gallery_border_pen)
        # Outline
        dc.DrawLine(rect.x + 1, rect.y, rect.x + rect.width - 1, rect.y)
        dc.DrawLine(rect.x, rect.y + 1, rect.x, rect.y + rect.height - 1)
        dc.DrawLine(rect.x + 1, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1)
        dc.DrawLine(rect.x + rect.width - 1, rect.y + 1, rect.x + rect.width - 1, rect.y + rect.height - 1)

        self.DrawGalleryBackgroundCommon(dc, wnd, rect)


    def DrawGalleryBackgroundCommon(self, dc, wnd, rect):

        if self._flags & RIBBON_BAR_FLOW_VERTICAL:        
            # Divider between items and buttons
            dc.DrawLine(rect.x, rect.y + rect.height - 15, rect.x + rect.width, rect.y + rect.height - 15)

            up_btn = wx.Rect(rect.x, rect.y + rect.height - 15, rect.width / 3, 15)
            down_btn = wx.Rect(up_btn.GetRight() + 1, up_btn.GetTop(), up_btn.GetWidth(), up_btn.GetHeight())
            dc.DrawLine(down_btn.GetLeft(), down_btn.GetTop(), down_btn.GetLeft(), down_btn.GetBottom())
            ext_btn = wx.Rect(down_btn.GetRight() + 1, up_btn.GetTop(), rect.width - up_btn.GetWidth() - down_btn.GetWidth() - 1, up_btn.GetHeight())
            dc.DrawLine(ext_btn.GetLeft(), ext_btn.GetTop(), ext_btn.GetLeft(), ext_btn.GetBottom())
        
        else:        
            # Divider between items and buttons
            dc.DrawLine(rect.x + rect.width - 15, rect.y, rect.x + rect.width - 15, rect.y + rect.height)
            
            up_btn = wx.Rect(rect.x + rect.width - 15, rect.y, 15, rect.height / 3)
            down_btn = wx.Rect(up_btn.GetLeft(), up_btn.GetBottom() + 1, up_btn.GetWidth(), up_btn.GetHeight())
            dc.DrawLine(down_btn.GetLeft(), down_btn.GetTop(), down_btn.GetRight(), down_btn.GetTop())
            ext_btn = wx.Rect(up_btn.GetLeft(), down_btn.GetBottom() + 1, up_btn.GetWidth(), rect.height - up_btn.GetHeight() - down_btn.GetHeight() - 1)
            dc.DrawLine(ext_btn.GetLeft(), ext_btn.GetTop(), ext_btn.GetRight(), ext_btn.GetTop())
        
        self.DrawGalleryButton(dc, up_btn, wnd.GetUpButtonState(), self._gallery_up_bitmap)
        self.DrawGalleryButton(dc, down_btn, wnd.GetDownButtonState(), self._gallery_down_bitmap)
        self.DrawGalleryButton(dc, ext_btn, wnd.GetExtensionButtonState(), self._gallery_extension_bitmap)


    def DrawGalleryButton(self, dc, rect, state, bitmaps):

        if state == RIBBON_GALLERY_BUTTON_NORMAL:
            btn_top_brush = self._gallery_button_background_top_brush
            btn_colour = self._gallery_button_background_colour
            btn_grad_colour = self._gallery_button_background_gradient_colour
            btn_bitmap = bitmaps[0]
        elif state == RIBBON_GALLERY_BUTTON_HOVERED:
            btn_top_brush = self._gallery_button_hover_background_top_brush
            btn_colour = self._gallery_button_hover_background_colour
            btn_grad_colour = self._gallery_button_hover_background_gradient_colour
            btn_bitmap = bitmaps[1]
        elif state == RIBBON_GALLERY_BUTTON_ACTIVE:
            btn_top_brush = self._gallery_button_active_background_top_brush
            btn_colour = self._gallery_button_active_background_colour
            btn_grad_colour = self._gallery_button_active_background_gradient_colour
            btn_bitmap = bitmaps[2]
        elif state == RIBBON_GALLERY_BUTTON_DISABLED:
            btn_top_brush = self._gallery_button_disabled_background_top_brush
            btn_colour = self._gallery_button_disabled_background_colour
            btn_grad_colour = self._gallery_button_disabled_background_gradient_colour
            btn_bitmap = bitmaps[3]

        rect.x += 1
        rect.y += 1
        
        if self._flags & RIBBON_BAR_FLOW_VERTICAL:        
            rect.width -= 1
            rect.height -= 2
        else:        
            rect.width -= 2
            rect.height -= 1
        
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBrush(btn_top_brush)
        dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height / 2)

        lower = wx.Rect(*rect)
        lower.height = (lower.height + 1) / 2
        lower.y += rect.height - lower.height
        dc.GradientFillLinear(lower, btn_colour, btn_grad_colour, wx.SOUTH)

        dc.DrawBitmap(btn_bitmap, rect.x + rect.width / 2 - 2, lower.y - 2, True)


    def DrawGalleryItemBackground(self, dc, wnd, rect, item):
        """
        Draw the background of a single item in a :class:`~lib.agw.ribbon.gallery.RibbonGallery` control.

        This is painted on top of a gallery background, and behind the items bitmap.
        Unlike :meth:`~RibbonMSWArtProvider.DrawButtonBarButton` and :meth:`~RibbonMSWArtProvider.DrawTool`, it is not expected to draw the
        item bitmap - that is done by the gallery control itself.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto, which is always the gallery
         which contains the item being drawn;
        :param `rect`: The rectangle within which to draw. The size of this rectangle
         will be the size of the item's bitmap, expanded by gallery item padding values
         (``RIBBON_ART_GALLERY_BITMAP_PADDING_LEFT_SIZE``, ``RIBBON_ART_GALLERY_BITMAP_PADDING_RIGHT_SIZE``,
         ``RIBBON_ART_GALLERY_BITMAP_PADDING_TOP_SIZE``, and ``RIBBON_ART_GALLERY_BITMAP_PADDING_BOTTOM_SIZE``).
         The drawing rectangle will be entirely within a rectangle on the same device
         context previously painted with :meth:`~RibbonMSWArtProvider.DrawGalleryBackground`;
        :param `item`: The item whose background is being painted. Typically the background
         will vary if the item is hovered, active, or selected; :meth:`RibbonGallery.GetSelection() <RibbonGallery.GetSelection>`,
         :meth:`RibbonGallery.GetActiveItem() <RibbonGallery.GetActiveItem>`, and :meth:`RibbonGallery.GetHoveredItem() <RibbonGallery.GetHoveredItem>` can be
         called to test if the given item is in one of these states.

        """

        if wnd.GetHoveredItem() != item and wnd.GetActiveItem() != item and \
           wnd.GetSelection() != item:
            return

        dc.SetPen(self._gallery_item_border_pen)
        dc.DrawLine(rect.x + 1, rect.y, rect.x + rect.width - 1, rect.y)
        dc.DrawLine(rect.x, rect.y + 1, rect.x, rect.y + rect.height - 1)
        dc.DrawLine(rect.x + 1, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1)
        dc.DrawLine(rect.x + rect.width - 1, rect.y + 1, rect.x + rect.width - 1, rect.y + rect.height - 1)

        if wnd.GetActiveItem() == item or wnd.GetSelection() == item:        
            top_brush = self._gallery_button_active_background_top_brush
            bg_colour = self._gallery_button_active_background_colour
            bg_gradient_colour = self._gallery_button_active_background_gradient_colour
        else:        
            top_brush = self._gallery_button_hover_background_top_brush
            bg_colour = self._gallery_button_hover_background_colour
            bg_gradient_colour = self._gallery_button_hover_background_gradient_colour
        
        upper = wx.Rect(*rect)
        upper.x += 1
        upper.width -= 2
        upper.y += 1
        upper.height /= 3
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.SetBrush(top_brush)
        dc.DrawRectangle(upper.x, upper.y, upper.width, upper.height)

        lower = wx.Rect(*upper)
        lower.y += lower.height
        lower.height = rect.height - 2 - lower.height
        dc.GradientFillLinear(lower, bg_colour, bg_gradient_colour, wx.SOUTH)


    def DrawPanelBorder(self, dc, rect, primary_colour, secondary_colour):

        border_points = [wx.Point() for i in xrange(9)]
        border_points[0] = wx.Point(2, 0)
        border_points[1] = wx.Point(rect.width - 3, 0)
        border_points[2] = wx.Point(rect.width - 1, 2)
        border_points[3] = wx.Point(rect.width - 1, rect.height - 3)
        border_points[4] = wx.Point(rect.width - 3, rect.height - 1)
        border_points[5] = wx.Point(2, rect.height - 1)
        border_points[6] = wx.Point(0, rect.height - 3)
        border_points[7] = wx.Point(0, 2)

        if primary_colour.GetColour() == secondary_colour.GetColour():        
            border_points[8] = border_points[0]
            dc.SetPen(primary_colour)
            dc.DrawLines(border_points, rect.x, rect.y)
        else:        
            dc.SetPen(primary_colour)
            dc.DrawLines(border_points[0:3], rect.x, rect.y)

            SingleLine(dc, rect, border_points[0], border_points[7])
            dc.SetPen(secondary_colour)
            dc.DrawLines(border_points[4:7], rect.x, rect.y)
            SingleLine(dc, rect, border_points[4], border_points[3])

            border_points[6] = border_points[2]
            RibbonDrawParallelGradientLines(dc, 2, border_points[6:8], 0, 1,
                                            border_points[3].y - border_points[2].y + 1, rect.x, rect.y,
                                            primary_colour.GetColour(), secondary_colour.GetColour())


    def DrawMinimisedPanel(self, dc, wnd, rect, bitmap):
        """
        Draw a minimised ribbon panel.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto, which is always the panel
         which is minimised. The panel label can be obtained from this window. The
         minimised icon obtained from querying the window may not be the size requested
         by :meth:`~RibbonMSWArtProvider.GetMinimisedPanelMinimumSize` - the argument contains the icon in the
         requested size;
        :param `rect`: The rectangle within which to draw. The size of the rectangle
         will be at least the size returned by :meth:`~RibbonMSWArtProvider.GetMinimisedPanelMinimumSize`;
        :param `bitmap`: A copy of the panel's minimised bitmap rescaled to the size
         returned by :meth:`~RibbonMSWArtProvider.GetMinimisedPanelMinimumSize`.

        """

        self.DrawPartialPageBackground(dc, wnd, rect, False)

        true_rect = wx.Rect(*rect)
        true_rect = self.RemovePanelPadding(true_rect)

        if wnd.GetExpandedPanel() != None:        
            client_rect = wx.Rect(*true_rect)
            client_rect.x += 1
            client_rect.width -= 2
            client_rect.y += 1
            client_rect.height = (rect.y + rect.height / 5) - client_rect.x
            dc.GradientFillLinear(client_rect,
                                  self._panel_active_background_top_colour,
                                  self._panel_active_background_top_gradient_colour, wx.SOUTH)

            client_rect.y += client_rect.height
            client_rect.height = (true_rect.y + true_rect.height) - client_rect.y
            dc.GradientFillLinear(client_rect,
                                  self._panel_active_background_colour,
                                  self._panel_active_background_gradient_colour, wx.SOUTH)
        
        elif wnd.IsHovered():
            client_rect = wx.Rect(*true_rect)
            client_rect.x += 1
            client_rect.width -= 2
            client_rect.y += 1
            client_rect.height -= 2
            self.DrawPartialPageBackground(dc, wnd, client_rect, True)

        preview = self.DrawMinimisedPanelCommon(dc, wnd, true_rect)

        dc.SetBrush(self._panel_hover_label_background_brush)
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangle(preview.x + 1, preview.y + preview.height - 8, preview.width - 2, 7)

        mid_pos = rect.y + rect.height / 5 - preview.y
        
        if mid_pos < 0 or mid_pos >= preview.height:        
            full_rect = wx.Rect(*preview)
            full_rect.x += 1
            full_rect.y += 1
            full_rect.width -= 2
            full_rect.height -= 9
            if mid_pos < 0:
                dc.GradientFillLinear(full_rect, self._page_hover_background_colour,
                                      self._page_hover_background_gradient_colour, wx.SOUTH)            
            else:            
                dc.GradientFillLinear(full_rect, self._page_hover_background_top_colour,
                                      self._page_hover_background_top_gradient_colour, wx.SOUTH)
                    
        else:
            top_rect = wx.Rect(*preview)
            top_rect.x += 1
            top_rect.y += 1
            top_rect.width -= 2
            top_rect.height = mid_pos
            dc.GradientFillLinear(top_rect, self._page_hover_background_top_colour,
                                  self._page_hover_background_top_gradient_colour, wx.SOUTH)

            btm_rect = wx.Rect(*top_rect)
            btm_rect.y = preview.y + mid_pos
            btm_rect.height = preview.y + preview.height - 7 - btm_rect.y
            dc.GradientFillLinear(btm_rect, self._page_hover_background_colour,
                                  self._page_hover_background_gradient_colour, wx.SOUTH)
        
        if bitmap.IsOk():        
            dc.DrawBitmap(bitmap, preview.x + (preview.width - bitmap.GetWidth()) / 2,
                          preview.y + (preview.height - 7 - bitmap.GetHeight()) / 2, True)
        
        self.DrawPanelBorder(dc, preview, self._panel_border_pen, self._panel_border_gradient_pen)
        self.DrawPanelBorder(dc, true_rect, self._panel_minimised_border_pen, self._panel_minimised_border_gradient_pen)


    def DrawMinimisedPanelCommon(self, dc, wnd, true_rect):

        preview = wx.Rect(0, 0, 32, 32)
        
        if self._flags & RIBBON_BAR_FLOW_VERTICAL:        
            preview.x = true_rect.x + 4
            preview.y = true_rect.y + (true_rect.height - preview.height) / 2
        else:        
            preview.x = true_rect.x + (true_rect.width - preview.width) / 2
            preview.y = true_rect.y + 4
        
        dc.SetFont(self._panel_label_font)
        label_width, label_height = dc.GetTextExtent(wnd.GetLabel())

        xpos = true_rect.x + (true_rect.width - label_width + 1) / 2
        ypos = preview.y + preview.height + 5

        if self._flags & RIBBON_BAR_FLOW_VERTICAL:        
            xpos = preview.x + preview.width + 5
            ypos = true_rect.y + (true_rect.height - label_height) / 2
        
        dc.SetTextForeground(self._panel_minimised_label_colour)
        dc.DrawText(wnd.GetLabel(), xpos, ypos)
        
        arrow_points = [wx.Point() for i in xrange(3)]
        
        if self._flags & RIBBON_BAR_FLOW_VERTICAL:
            xpos += label_width
            arrow_points[0] = wx.Point(xpos + 5, ypos + label_height / 2)
            arrow_points[1] = arrow_points[0] + wx.Point(-3,  3)
            arrow_points[2] = arrow_points[0] + wx.Point(-3, -3)
        else:        
            ypos += label_height
            arrow_points[0] = wx.Point(true_rect.width / 2, ypos + 5)
            arrow_points[1] = arrow_points[0] + wx.Point(-3, -3)
            arrow_points[2] = arrow_points[0] + wx.Point( 3, -3)
        
        dc.SetPen(wx.TRANSPARENT_PEN)
        B = wx.Brush(self._panel_minimised_label_colour)
        dc.SetBrush(B)
        dc.DrawPolygon(arrow_points, true_rect.x, true_rect.y)

        return preview


    def DrawButtonBarBackground(self, dc, wnd, rect):
        """
        Draw the background for a :class:`~lib.agw.ribbon.buttonbar.RibbonButtonBar` control.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto (which will typically be
         the button bar itself, though this is not guaranteed);
        :param `rect`: The rectangle within which to draw.

        """

        self.DrawPartialPageBackground(dc, wnd, rect, True)


    def DrawPartialPageBackground2(self, dc, wnd, rect, allow_hovered=True):

        # Assume the window is a child of a ribbon page, and also check for a
        # hovered panel somewhere between the window and the page, as it causes
        # the background to change.
        offset = wx.Point(*wnd.GetPosition())
        page = None
        parent = wnd.GetParent()
        hovered = False
        panel = None

        if isinstance(wnd, PANEL.RibbonPanel):
            panel = wnd
            hovered = allow_hovered and panel.IsHovered()
            if panel.GetExpandedDummy() != None:            
                offset = panel.GetExpandedDummy().GetPosition()
                parent = panel.GetExpandedDummy().GetParent()
            
        while 1:

            if panel is None:
                panel = parent
                if isinstance(panel, PANEL.RibbonPanel):
                    hovered = allow_hovered and panel.IsHovered()
                    if panel.GetExpandedDummy() != None:
                        parent = panel.GetExpandedDummy()

            page = parent                    
            if isinstance(page, PAGE.RibbonPage):
                break
            
            offset += parent.GetPosition()
            parent = parent.GetParent()
            if parent is None:
                break

        if page != None:
            self.DrawPartialPageBackground(dc, wnd, rect, page, offset, hovered)
            return
        
        # No page found - fallback to painting with a stock brush
        dc.SetBrush(wx.WHITE_BRUSH)
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)


    def DrawButtonBarButton(self, dc, wnd, rect, kind, state, label, bitmap_large, bitmap_small):
        """
        Draw a single button for a :class:`~lib.agw.ribbon.buttonbar.RibbonButtonBar` control.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto;
        :param `rect`: The rectangle within which to draw. The size of this rectangle
         will be a size previously returned by :meth:`~RibbonMSWArtProvider.GetButtonBarButtonSize`, and the
         rectangle will be entirely within a rectangle on the same device context
         previously painted with :meth:`~RibbonMSWArtProvider.DrawButtonBarBackground`;
        :param `kind`: The kind of button to draw (normal, dropdown or hybrid);
        :param `state`: Combination of a size flag and state flags from the
         `RibbonButtonBarButtonState` enumeration;
        :param `label`: The label of the button;
        :param `bitmap_large`: The large bitmap of the button (or the large disabled
         bitmap when ``RIBBON_BUTTONBAR_BUTTON_DISABLED`` is set in `state`);
        :param `bitmap_small`: The small bitmap of the button (or the small disabled
         bitmap when ``RIBBON_BUTTONBAR_BUTTON_DISABLED`` is set in `state`).
         
        """

        if kind == RIBBON_BUTTON_TOGGLE:
            kind = RIBBON_BUTTON_NORMAL
            if state & RIBBON_BUTTONBAR_BUTTON_TOGGLED:
                state ^= RIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK
    
        if state & (RIBBON_BUTTONBAR_BUTTON_HOVER_MASK | RIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK):        
            if state & RIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK:
                dc.SetPen(self._button_bar_active_border_pen)
            else:
                dc.SetPen(self._button_bar_hover_border_pen)

            bg_rect = wx.Rect(*rect)
            bg_rect.x += 1
            bg_rect.y += 1
            bg_rect.width -= 2
            bg_rect.height -= 2

            bg_rect_top = wx.Rect(*bg_rect)
            bg_rect_top.height /= 3
            bg_rect.y += bg_rect_top.height
            bg_rect.height -= bg_rect_top.height

            if kind == RIBBON_BUTTON_HYBRID:
            
                result = state & RIBBON_BUTTONBAR_BUTTON_SIZE_MASK
                
                if result == RIBBON_BUTTONBAR_BUTTON_LARGE:
                    iYBorder = rect.y + bitmap_large.GetHeight() + 4
                    partial_bg = wx.Rect(*rect)
                    
                    if state & RIBBON_BUTTONBAR_BUTTON_NORMAL_HOVERED:                        
                        partial_bg.SetBottom(iYBorder - 1)
                    else:
                        partial_bg.height -= (iYBorder - partial_bg.y + 1)
                        partial_bg.y = iYBorder + 1
                        
                    dc.DrawLine(rect.x, iYBorder, rect.x + rect.width, iYBorder)
                    bg_rect.Intersect(partial_bg)
                    bg_rect_top.Intersect(partial_bg)
                    
                elif result == RIBBON_BUTTONBAR_BUTTON_MEDIUM:
                    iArrowWidth = 9
                    
                    if state & RIBBON_BUTTONBAR_BUTTON_NORMAL_HOVERED:                    
                        bg_rect.width -= iArrowWidth
                        bg_rect_top.width -= iArrowWidth
                        dc.DrawLine(bg_rect_top.x + bg_rect_top.width, rect.y, bg_rect_top.x + bg_rect_top.width,
                                    rect.y + rect.height)                    
                    else:                    
                        iArrowWidth -= 1
                        bg_rect.x += bg_rect.width - iArrowWidth
                        bg_rect_top.x += bg_rect_top.width - iArrowWidth
                        bg_rect.width = iArrowWidth
                        bg_rect_top.width = iArrowWidth
                        dc.DrawLine(bg_rect_top.x - 1, rect.y, bg_rect_top.x - 1, rect.y + rect.height)
                        
            if state & RIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK:
            
                dc.GradientFillLinear(bg_rect_top, self._button_bar_active_background_top_colour,
                                      self._button_bar_active_background_top_gradient_colour, wx.SOUTH)
                dc.GradientFillLinear(bg_rect, self._button_bar_active_background_colour,
                                      self._button_bar_active_background_gradient_colour, wx.SOUTH)
            
            else:            
                dc.GradientFillLinear(bg_rect_top, self._button_bar_hover_background_top_colour,
                                      self._button_bar_hover_background_top_gradient_colour, wx.SOUTH)
                dc.GradientFillLinear(bg_rect, self._button_bar_hover_background_colour,
                                      self._button_bar_hover_background_gradient_colour, wx.SOUTH)

            border_points = [wx.Point() for i in xrange(9)]
            border_points[0] = wx.Point(2, 0)
            border_points[1] = wx.Point(rect.width - 3, 0)
            border_points[2] = wx.Point(rect.width - 1, 2)
            border_points[3] = wx.Point(rect.width - 1, rect.height - 3)
            border_points[4] = wx.Point(rect.width - 3, rect.height - 1)
            border_points[5] = wx.Point(2, rect.height - 1)
            border_points[6] = wx.Point(0, rect.height - 3)
            border_points[7] = wx.Point(0, 2)
            border_points[8] = border_points[0]

            dc.DrawLines(border_points, rect.x, rect.y)
        
        dc.SetFont(self._button_bar_label_font)
        dc.SetTextForeground(self._button_bar_label_colour)
        self.DrawButtonBarButtonForeground(dc, rect, kind, state, label, bitmap_large, bitmap_small)


    def DrawButtonBarButtonForeground(self, dc, rect, kind, state, label, bitmap_large, bitmap_small):

        result = state & RIBBON_BUTTONBAR_BUTTON_SIZE_MASK
        
        if result == RIBBON_BUTTONBAR_BUTTON_LARGE:
            
            padding = 2
            dc.DrawBitmap(bitmap_large, rect.x + (rect.width - bitmap_large.GetWidth()) / 2,
                          rect.y + padding, True)
            ypos = rect.y + padding + bitmap_large.GetHeight() + padding
            arrow_width = (kind == RIBBON_BUTTON_NORMAL and [0] or [8])[0]

            label_w, label_h = dc.GetTextExtent(label)
            
            if label_w + 2 * padding <= rect.width:
            
                dc.DrawText(label, rect.x + (rect.width - label_w) / 2, ypos)
                if arrow_width != 0:                
                    self.DrawDropdownArrow(dc, rect.x + rect.width / 2,
                                           ypos + (label_h * 3) / 2,
                                           self._button_bar_label_colour)
            else:
                breaki = len(label)
                
                while breaki > 0:                
                    breaki -= 1
                    if RibbonCanLabelBreakAtPosition(label, breaki):                    
                        label_top = label[0:breaki]
                        label_w, label_h = dc.GetTextExtent(label_top)

                        if label_w + 2 * padding <= rect.width:                        
                            dc.DrawText(label_top, rect.x + (rect.width - label_w) / 2, ypos)
                            ypos += label_h
                            label_bottom = label[breaki:]
                            label_w, label_h = dc.GetTextExtent(label_bottom)
                            label_w += arrow_width
                            iX = rect.x + (rect.width - label_w) / 2
                            dc.DrawText(label_bottom, iX, ypos)
                            
                            if arrow_width != 0:                            
                                self.DrawDropdownArrow(dc, iX + 2 +label_w - arrow_width,
                                                       ypos + label_h / 2 + 1,
                                                       self._button_bar_label_colour)
                            
                            break

        elif result == RIBBON_BUTTONBAR_BUTTON_MEDIUM:
        
            x_cursor = rect.x + 2
            dc.DrawBitmap(bitmap_small, x_cursor, rect.y + (rect.height - bitmap_small.GetHeight())/2, True)
            x_cursor += bitmap_small.GetWidth() + 2
            label_w, label_h = dc.GetTextExtent(label)
            dc.DrawText(label, x_cursor, rect.y + (rect.height - label_h) / 2)
            x_cursor += label_w + 3
            
            if kind != RIBBON_BUTTON_NORMAL:
                self.DrawDropdownArrow(dc, x_cursor, rect.y + rect.height / 2,
                                       self._button_bar_label_colour)
        
        else:
            # TODO
            pass
    

    def DrawToolBarBackground(self, dc, wnd, rect):
        """
        Draw the background for a :class:`~lib.agw.ribbon.toolbar.RibbonToolBar` control.


        :param `dc`: The device context to draw onto;
        :param `wnd`: The which is being drawn onto. In most cases this will be a
         :class:`~lib.agw.ribbon.toolbar.RibbonToolBar`, but it doesn't have to be;
        :param `rect`: The rectangle within which to draw. Some of this rectangle
         will later be drawn over using :meth:`~RibbonMSWArtProvider.DrawToolGroupBackground` and :meth:`~RibbonMSWArtProvider.DrawTool`,
         but not all of it will (unless there is only a single group of tools).

        """

        self.DrawPartialPageBackground(dc, wnd, rect)


    def DrawToolGroupBackground(self, dc, wnd, rect):
        """
        Draw the background for a group of tools on a :class:`~lib.agw.ribbon.toolbar.RibbonToolBar` control.

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto. In most cases this will
         be a :class:`~lib.agw.ribbon.toolbar.RibbonToolBar`, but it doesn't have to be;
        :param `rect`: The rectangle within which to draw. This rectangle is a union
         of the individual tools' rectangles. As there are no gaps between tools,
         this rectangle will be painted over exactly once by calls to :meth:`~RibbonMSWArtProvider.DrawTool`.
         The group background could therefore be painted by :meth:`~RibbonMSWArtProvider.DrawTool`, though it
         can be conceptually easier and more efficient to draw it all at once here.
         The rectangle will be entirely within a rectangle on the same device context
         previously painted with :meth:`~RibbonMSWArtProvider.DrawToolBarBackground`.

        """

        dc.SetPen(self._toolbar_border_pen)
        outline = [wx.Point() for i in xrange(9)]
        outline[0] = wx.Point(2, 0)
        outline[1] = wx.Point(rect.width - 3, 0)
        outline[2] = wx.Point(rect.width - 1, 2)
        outline[3] = wx.Point(rect.width - 1, rect.height - 3)
        outline[4] = wx.Point(rect.width - 3, rect.height - 1)
        outline[5] = wx.Point(2, rect.height - 1)
        outline[6] = wx.Point(0, rect.height - 3)
        outline[7] = wx.Point(0, 2)
        outline[8] = outline[0]

        dc.DrawLines(outline, rect.x, rect.y)


    def DrawTool(self, dc, wnd, rect, bitmap, kind, state):
        """
        Draw a single tool (for a :class:`~lib.agw.ribbon.toolbar.RibbonToolBar` control).

        :param `dc`: The device context to draw onto;
        :param `wnd`: The window which is being drawn onto. In most cases this will
         be a :class:`~lib.agw.ribbon.toolbar.RibbonToolBar`, but it doesn't have to be;
        :param `rect`: The rectangle within which to draw. The size of this rectangle
         will at least the size returned by :meth:`~RibbonMSWArtProvider.GetToolSize`, and the height of it will
         be equal for all tools within the same group. The rectangle will be entirely
         within a rectangle on the same device context previously painted with
         :meth:`~RibbonMSWArtProvider.DrawToolGroupBackground`;
        :param `bitmap`: The bitmap to use as the tool's foreground. If the tool is a
         hybrid or dropdown tool, then the foreground should also contain a standard
         dropdown button;
        :param `kind`: The kind of tool to draw (normal, dropdown, or hybrid);
        :param `state`: A combination of `RibbonToolBarToolState` flags giving the
         state of the tool and it's relative position within a tool group.

        """

        if kind == RIBBON_BUTTON_TOGGLE:
            if state & RIBBON_TOOLBAR_TOOL_TOGGLED:
                state ^= RIBBON_TOOLBAR_TOOL_ACTIVE_MASK

        bg_rect = wx.Rect(*rect)
        bg_rect.Deflate(1, 1)
        
        if state & RIBBON_TOOLBAR_TOOL_LAST == 0:
            bg_rect.width += 1
            
        is_split_hybrid = (kind == RIBBON_BUTTON_HYBRID and (state & (RIBBON_TOOLBAR_TOOL_HOVER_MASK | RIBBON_TOOLBAR_TOOL_ACTIVE_MASK)))

        # Background
        bg_rect_top = wx.Rect(*bg_rect)
        bg_rect_top.height = (bg_rect_top.height * 2) / 5
        bg_rect_btm = wx.Rect(*bg_rect)
        bg_rect_btm.y += bg_rect_top.height
        bg_rect_btm.height -= bg_rect_top.height

        bg_top_colour = self._tool_background_top_colour
        bg_top_grad_colour = self._tool_background_top_gradient_colour
        bg_colour = self._tool_background_colour
        bg_grad_colour = self._tool_background_gradient_colour
        
        if state & RIBBON_TOOLBAR_TOOL_ACTIVE_MASK:
            bg_top_colour = self._tool_active_background_top_colour
            bg_top_grad_colour = self._tool_active_background_top_gradient_colour
            bg_colour = self._tool_active_background_colour
            bg_grad_colour = self._tool_active_background_gradient_colour
        
        elif state & RIBBON_TOOLBAR_TOOL_HOVER_MASK:        
            bg_top_colour = self._tool_hover_background_top_colour
            bg_top_grad_colour = self._tool_hover_background_top_gradient_colour
            bg_colour = self._tool_hover_background_colour
            bg_grad_colour = self._tool_hover_background_gradient_colour
        
        dc.GradientFillLinear(bg_rect_top, bg_top_colour, bg_top_grad_colour, wx.SOUTH)
        dc.GradientFillLinear(bg_rect_btm, bg_colour, bg_grad_colour, wx.SOUTH)
        
        if is_split_hybrid:        
            nonrect = wx.Rect(*bg_rect)
            if state & (RIBBON_TOOLBAR_TOOL_DROPDOWN_HOVERED | RIBBON_TOOLBAR_TOOL_DROPDOWN_ACTIVE):            
                nonrect.width -= 8
            else:            
                nonrect.x += nonrect.width - 8
                nonrect.width = 8
            
            B = wx.Brush(self._tool_hover_background_top_colour)
            dc.SetPen(wx.TRANSPARENT_PEN)
            dc.SetBrush(B)
            dc.DrawRectangle(nonrect.x, nonrect.y, nonrect.width, nonrect.height)
        
        # Border
        dc.SetPen(self._toolbar_border_pen)
        
        if state & RIBBON_TOOLBAR_TOOL_FIRST:        
            dc.DrawPoint(rect.x + 1, rect.y + 1)
            dc.DrawPoint(rect.x + 1, rect.y + rect.height - 2)        
        else:
            dc.DrawLine(rect.x, rect.y + 1, rect.x, rect.y + rect.height - 1)   

        if state & RIBBON_TOOLBAR_TOOL_LAST:        
            dc.DrawPoint(rect.x + rect.width - 2, rect.y + 1)
            dc.DrawPoint(rect.x + rect.width - 2, rect.y + rect.height - 2)
        
        # Foreground
        avail_width = bg_rect.GetWidth()
        
        if kind & RIBBON_BUTTON_DROPDOWN:        
            avail_width -= 8
            if is_split_hybrid:            
                dc.DrawLine(rect.x + avail_width + 1, rect.y, rect.x + avail_width + 1, rect.y + rect.height)
            
            dc.DrawBitmap(self._toolbar_drop_bitmap, bg_rect.x + avail_width + 2,
                          bg_rect.y + (bg_rect.height / 2) - 2, True)
        
        dc.DrawBitmap(bitmap, bg_rect.x + (avail_width - bitmap.GetWidth()) / 2,
                      bg_rect.y + (bg_rect.height - bitmap.GetHeight()) / 2, True)


    def GetBarTabWidth(self, dc, wnd, label, bitmap, ideal=None, small_begin_need_separator=None,
                       small_must_have_separator=None, minimum=None):

        """
        Calculate the ideal and minimum width (in pixels) of a tab in a ribbon bar.

        :param `dc`: A device context to use when one is required for size calculations;
        :param `wnd`: The window onto which the tab will eventually be drawn;
        :param `label`: The tab's label (or "" if it has none);
        :param `bitmap`: The tab's icon (or :class:`NullBitmap` if it has none);
        :param `ideal`: The ideal width (in pixels) of the tab;
        :param `small_begin_need_separator`: A size less than the size, at which a
         tab separator should begin to be drawn (i.e. drawn, but still fairly transparent);
        :param `small_must_have_separator`: A size less than the size, at which a
         tab separator must be drawn (i.e. drawn at full opacity);
        :param `minimum`: A size less than the size, and greater than or equal to
         zero, which is the minimum pixel width for the tab.

        """

        width = 0
        mini = 0
        
        if (self._flags & RIBBON_BAR_SHOW_PAGE_LABELS) and label.strip():        
            dc.SetFont(self._tab_label_font)
            width += dc.GetTextExtent(label)[0]
            mini += min(25, width) # enough for a few chars
            
            if bitmap.IsOk():            
                # gap between label and bitmap
                width += 4
                mini += 2
            
        if (self._flags & RIBBON_BAR_SHOW_PAGE_ICONS) and bitmap.IsOk():
            width += bitmap.GetWidth()
            mini += bitmap.GetWidth()
        
        ideal = width + 30        
        small_begin_need_separator = width + 20        
        small_must_have_separator = width + 10        
        minimum = mini
        
        return ideal, small_begin_need_separator, small_must_have_separator, minimum


    def GetTabCtrlHeight(self, dc, wnd, pages):
        """
        Calculate the height (in pixels) of the tab region of a ribbon bar.

        Note that as the tab region can contain scroll buttons, the height should be
        greater than or equal to the minimum height for a tab scroll button.

        :param `dc`: A device context to use when one is required for size calculations;
        :param `wnd`: The window onto which the tabs will eventually be drawn;
        :param `pages`: The tabs which will acquire the returned height.

        """

        text_height = 0
        icon_height = 0

        if len(pages) <= 1 and (self._flags & RIBBON_BAR_ALWAYS_SHOW_TABS) == 0:
            # To preserve space, a single tab need not be displayed. We still need
            # two pixels of border / padding though.
            return 2
        
        if self._flags & RIBBON_BAR_SHOW_PAGE_LABELS:        
            dc.SetFont(self._tab_label_font)
            text_height = dc.GetTextExtent("ABCDEFXj")[1] + 10
        
        if self._flags & RIBBON_BAR_SHOW_PAGE_ICONS:
            for info in pages:
                if info.page.GetIcon().IsOk():                
                    icon_height = max(icon_height, info.page.GetIcon().GetHeight() + 4)

        return max(text_height, icon_height)


    def GetScrollButtonMinimumSize(self, dc, wnd, style):
        """
        Calculate the minimum size (in pixels) of a scroll button.

        :param `dc`: A device context to use when one is required for size calculations;
        :param `wnd`: The window onto which the scroll button will eventually be drawn;
        :param `style`: A combination of flags from `RibbonScrollButtonStyle`, including
         a direction, and a for flag (state flags may be given too, but should be ignored,
         as a button should retain a constant size, regardless of its state).

        """

        return wx.Size(12, 12)


    def GetPanelSize(self, dc, wnd, client_size, client_offset=None):
        """
        Calculate the size of a panel for a given client size.

        This should increment the given size by enough to fit the panel label and other
        chrome.

        :param `dc`: A device context to use if one is required for size calculations;
        :param `wnd`: The ribbon panel in question;
        :param `client_size`: The client size;
        :param `client_offset`: The offset where the client rectangle begins within the
         panel (may be ``None``).

        :see: :meth:`~RibbonMSWArtProvider.GetPanelClientSize`
        """

        dc.SetFont(self._panel_label_font)
        label_size = wx.Size(*dc.GetTextExtent(wnd.GetLabel()))

        client_size.IncBy(0, label_size.GetHeight())
        
        if self._flags & RIBBON_BAR_FLOW_VERTICAL:
            client_size.IncBy(4, 8)
        else:
            client_size.IncBy(6, 6)

        if client_offset != None:        
            if self._flags & RIBBON_BAR_FLOW_VERTICAL:
                client_offset = wx.Point(2, 3)
            else:
                client_offset = wx.Point(3, 2)
        
        return client_size


    def GetPanelClientSize(self, dc, wnd, size, client_offset=None):
        """
        Calculate the client size of a panel for a given overall size.

        This should act as the inverse to :meth:`~RibbonMSWArtProvider.GetPanelSize`, and decrement the given size
        by enough to fit the panel label and other chrome.

        :param `dc`: A device context to use if one is required for size calculations;
        :param `wnd`: The ribbon panel in question;
        :param `size`: The overall size to calculate client size for;
        :param `client_offset`: The offset where the returned client size begins within
         the given (may be ``None``).

        :see: :meth:`~RibbonMSWArtProvider.GetPanelSize`
        """

        dc.SetFont(self._panel_label_font)
        label_size = wx.Size(*dc.GetTextExtent(wnd.GetLabel()))

        size.DecBy(0, label_size.GetHeight())
        
        if self._flags & RIBBON_BAR_FLOW_VERTICAL:
            size.DecBy(4, 8)
        else:
            size.DecBy(6, 6)

        if client_offset != None:        
            if self._flags & RIBBON_BAR_FLOW_VERTICAL:
                client_offset = wx.Point(2, 3)
            else:
                client_offset = wx.Point(3, 2)

        if size.x < 0:
            size.x = 0
        if size.y < 0:
            size.y = 0
    
        return size, client_offset


    def GetGallerySize(self, dc, wnd, client_size):
        """
        Calculate the size of a :class:`~lib.agw.ribbon.gallery.RibbonGallery` control for a given client size.

        This should increment the given size by enough to fit the gallery border,
        buttons, and any other chrome.

        :param `dc`: A device context to use if one is required for size calculations;
        :param `wnd`: The gallery in question;
        :param `client_size`: The client size.

        :see: :meth:`~RibbonMSWArtProvider.GetGalleryClientSize`
        """

        client_size.IncBy(2, 1) # Left / top padding

        if self._flags & RIBBON_BAR_FLOW_VERTICAL:
            client_size.IncBy(1, 16) # Right / bottom padding
        else:
            client_size.IncBy(16, 1) # Right / bottom padding

        return client_size


    def GetGalleryClientSize(self, dc, wnd, size, client_offset=None, scroll_up_button=None,
                             scroll_down_button=None, extension_button=None):

        """
        Calculate the client size of a :class:`~lib.agw.ribbon.gallery.RibbonGallery` control for a given size.

        This should act as the inverse to :meth:`~RibbonMSWArtProvider.GetGallerySize`, and decrement the given
        size by enough to fir the gallery border, buttons, and other chrome.

        :param `dc`: A device context to use if one is required for size calculations;
        :param `wnd`: The gallery in question;
        :param `size`: The overall size to calculate the client size for;
        :param `client_offset`: The position within the given size at which the
         returned client size begins;
        :param `scroll_up_button`: The rectangle within the given size which the
         scroll up button occupies;
        :param `scroll_down_button`: The rectangle within the given size which the
         scroll down button occupies;
        :param `extension_button`: The rectangle within the given size which the
         extension button occupies.

        """

        scroll_up = wx.Rect()
        scroll_down = wx.Rect()
        extension = wx.Rect()
        
        if self._flags & RIBBON_BAR_FLOW_VERTICAL:
            # Flow is vertical - put buttons on bottom
            scroll_up.y = size.GetHeight() - 15
            scroll_up.height = 15
            scroll_up.x = 0
            scroll_up.width = (size.GetWidth() + 2) / 3
            scroll_down.y = scroll_up.y
            scroll_down.height = scroll_up.height
            scroll_down.x = scroll_up.x + scroll_up.width
            scroll_down.width = scroll_up.width        
            extension.y = scroll_down.y
            extension.height = scroll_down.height
            extension.x = scroll_down.x + scroll_down.width
            extension.width = size.GetWidth() - scroll_up.width - scroll_down.width
            size.DecBy(1, 16)
            size.DecBy(2, 1)
        
        else:
            # Flow is horizontal - put buttons on right
            scroll_up.x = size.GetWidth() - 15
            scroll_up.width = 15
            scroll_up.y = 0
            scroll_up.height = (size.GetHeight() + 2) / 3
            scroll_down.x = scroll_up.x
            scroll_down.width = scroll_up.width
            scroll_down.y = scroll_up.y + scroll_up.height
            scroll_down.height = scroll_up.height        
            extension.x = scroll_down.x
            extension.width = scroll_down.width
            extension.y = scroll_down.y + scroll_down.height
            extension.height = size.GetHeight() - scroll_up.height - scroll_down.height
            size.DecBy(16, 1)
            size.DecBy( 2, 1)
        
        client_offset = wx.Point(2, 1)
        scroll_up_button = scroll_up
        scroll_down_button = scroll_down
        extension_button = extension

        return size, client_offset, scroll_up_button, scroll_down_button, extension_button


    def GetPageBackgroundRedrawArea(self, dc, wnd, page_old_size, page_new_size):
        """
        Calculate the portion of a page background which needs to be redrawn when a page
        is resized.

        To optimise the drawing of page backgrounds, as small an area as possible should
        be returned. Of couse, if the way in which a background is drawn means that the
        entire background needs to be repainted on resize, then the entire new size
        should be returned.

        :param `dc`: A device context to use when one is required for size calculations;
        :param `wnd`: The page which is being resized;
        :param `page_old_size`: The size of the page prior to the resize (which has
         already been painted);
        :param `page_new_size`: The size of the page after the resize.

        """

        if page_new_size.GetWidth() != page_old_size.GetWidth():
            if page_new_size.GetHeight() != page_old_size.GetHeight():
                # Width and height both changed - redraw everything
                return wx.Rect(0, 0, *page_new_size)
            else:            
                # Only width changed - redraw right hand side
                right_edge_width = 4
                new_rect = wx.Rect(page_new_size.GetWidth() - right_edge_width, 0, right_edge_width, page_new_size.GetHeight())
                old_rect = wx.Rect(page_old_size.GetWidth() - right_edge_width, 0, right_edge_width, page_old_size.GetHeight())
            
        else:        
            if page_new_size.GetHeight() == page_old_size.GetHeight():            
                # Nothing changed (should never happen) - redraw nothing
                return wx.Rect(0, 0, 0, 0)
            else:            
                # Height changed - need to redraw everything (as the background
                # gradient is done vertically).
                return wx.Rect(0, 0, *page_new_size)
            
        new_rect.Union(old_rect)
        new_rect.Intersect(wx.Rect(0, 0, *page_new_size))
        return new_rect


    def GetButtonBarButtonSize(self, dc, wnd, kind, size, label, bitmap_size_large, bitmap_size_small,
                               button_size=None, normal_region=None, dropdown_region=None):
        """
        Calculate the size of a button within a :class:`~lib.agw.ribbon.buttonbar.RibbonButtonBar`.

        :param `dc`: A device context to use when one is required for size calculations;
        :param `wnd`: The window onto which the button will eventually be drawn
         (which is normally a :class:`~lib.agw.ribbon.buttonbar.RibbonButtonBar`, though this is not guaranteed);
        :param `kind`: The kind of button;
        :param `size`: The size-class to calculate the size for. Buttons on a button
         bar can have three distinct sizes: ``RIBBON_BUTTONBAR_BUTTON_SMALL``,
         ``RIBBON_BUTTONBAR_BUTTON_MEDIUM``, and ``RIBBON_BUTTONBAR_BUTTON_LARGE``.
         If the requested size-class is not applicable, then ``False`` should be returned;
        :param `label`: The label of the button;
        :param `bitmap_size_large`: The size of all "large" bitmaps on the button bar;
        :param `bitmap_size_small`: The size of all "small" bitmaps on the button bar;
        :param `button_size`: The size, in pixels, of the button;
        :param `normal_region`: The region of the button which constitutes the normal button;
        :param `dropdown_region`: The region of the button which constitutes the dropdown button.

        :returns: ``True`` if a size exists for the button, ``False`` otherwise.
        """

        drop_button_width = 8

        normal_region = wx.Rect()
        dropdown_region = wx.Rect()
        
        dc.SetFont(self._button_bar_label_font)
        result = size & RIBBON_BUTTONBAR_BUTTON_SIZE_MASK
        
        if result == RIBBON_BUTTONBAR_BUTTON_SMALL:
            # Small bitmap, no label
            button_size = bitmap_size_small + wx.Size(6, 4)
            
            if kind in [RIBBON_BUTTON_NORMAL, RIBBON_BUTTON_TOGGLE]:
                normal_region = wx.Rect(0, 0, *button_size)
                dropdown_region = wx.Rect(0, 0, 0, 0)

            elif kind == RIBBON_BUTTON_DROPDOWN:
                button_size += wx.Size(drop_button_width, 0)
                dropdown_region = wx.Rect(0, 0, *button_size)
                normal_region = wx.Rect(0, 0, 0, 0)

            elif kind == RIBBON_BUTTON_HYBRID:
                normal_region = wx.Rect(0, 0, *button_size)
                dropdown_region = wx.Rect(button_size.GetWidth(), 0, drop_button_width, button_size.GetHeight())
                button_size += wx.Size(drop_button_width, 0)

        elif result == RIBBON_BUTTONBAR_BUTTON_MEDIUM:
            # Small bitmap, with label to the right
            is_supported, button_size, normal_region, dropdown_region = self.GetButtonBarButtonSize(dc, wnd, kind,
                                                                                                    RIBBON_BUTTONBAR_BUTTON_SMALL,
                                                                                                    label, bitmap_size_large,
                                                                                                    bitmap_size_small)
            text_size = dc.GetTextExtent(label)[0]
            button_size.SetWidth(button_size.GetWidth() + text_size)

            if kind == RIBBON_BUTTON_DROPDOWN:
                dropdown_region.SetWidth(dropdown_region.GetWidth() + text_size)

            elif kind == RIBBON_BUTTON_HYBRID:
                dropdown_region.SetX(dropdown_region.GetX() + text_size)
                normal_region.SetWidth(normal_region.GetWidth() + text_size)
                # no break
            elif kind in [RIBBON_BUTTON_NORMAL, RIBBON_BUTTON_TOGGLE]:
                normal_region.SetWidth(normal_region.GetWidth() + text_size)
            
        elif result == RIBBON_BUTTONBAR_BUTTON_LARGE:
            # Large bitmap, with label below (possibly split over 2 lines)
            
            icon_size = wx.Size(*bitmap_size_large)
            icon_size += wx.Size(4, 4)
            best_width, label_height = dc.GetTextExtent(label)
            last_line_extra_width = 0
            
            if kind not in [RIBBON_BUTTON_NORMAL, RIBBON_BUTTON_TOGGLE]:
                last_line_extra_width += 8
            
            for i in xrange(0, len(label)):            
                if RibbonCanLabelBreakAtPosition(label, i):
                    
                    width = max(dc.GetTextExtent(label[0:i])[0],
                                dc.GetTextExtent(label[i+1:])[0] + last_line_extra_width)
                    if width < best_width:
                        best_width = width

            label_height *= 2 # Assume two lines even when only one is used
                              # (to give all buttons a consistent height)
            icon_size.SetWidth(max(icon_size.GetWidth(), best_width) + 6)
            icon_size.SetHeight(icon_size.GetHeight() + label_height)
            button_size = wx.Size(*icon_size)

            if kind == RIBBON_BUTTON_DROPDOWN:
                dropdown_region = wx.Rect(0, 0, *icon_size)
            elif kind == RIBBON_BUTTON_HYBRID:
                normal_region = wx.Rect(0, 0, *icon_size)
                normal_region.height -= 2 + label_height
                dropdown_region.x = 0
                dropdown_region.y = normal_region.height
                dropdown_region.width = icon_size.GetWidth()
                dropdown_region.height = icon_size.GetHeight() - normal_region.height
            elif kind in [RIBBON_BUTTON_NORMAL, RIBBON_BUTTON_TOGGLE]:
                normal_region = wx.Rect(0, 0, *icon_size)
            
        return True, button_size, normal_region, dropdown_region


    def GetMinimisedPanelMinimumSize(self, dc, wnd, desired_bitmap_size=None, expanded_panel_direction=None):
        """
        Calculate the size of a minimised ribbon panel.

        :param `dc`: A device context to use when one is required for size calculations;
        :param `wnd`: The ribbon panel in question. Attributes like the panel label can
         be queried from this;
        :param `desired_bitmap_size`: MISSING DESCRIPTION;
        :param `expanded_panel_direction`: MISSING DESCRIPTION.

        """

        if desired_bitmap_size != None:        
            desired_bitmap_size = wx.Size(16, 16)
        
        if expanded_panel_direction != None:        
            if self._flags & RIBBON_BAR_FLOW_VERTICAL:
                expanded_panel_direction = wx.EAST
            else:
                expanded_panel_direction = wx.SOUTH
        
        base_size = wx.Size(42, 42)

        dc.SetFont(self._panel_label_font)
        label_size = wx.Size(*dc.GetTextExtent(wnd.GetLabel()))
        label_size.IncBy(2, 2) # Allow for differences between this DC and a paint DC
        label_size.IncBy(6, 0) # Padding
        label_size.y *= 2 # Second line for dropdown button

        if self._flags & RIBBON_BAR_FLOW_VERTICAL:        
            # Label alongside icon
            return wx.Size(base_size.x + label_size.x, max(base_size.y, label_size.y)), \
                   desired_bitmap_size, expanded_panel_direction
        else:        
            # Label beneath icon
            return wx.Size(max(base_size.x, label_size.x), base_size.y + label_size.y), \
                   desired_bitmap_size, expanded_panel_direction
        

    def GetToolSize(self, dc, wnd, bitmap_size, kind, is_first, is_last, dropdown_region=None):
        """
        Calculate the size of a tool within a :class:`~lib.agw.ribbon.toolbar.RibbonToolBar`.

        :param `dc`: A device context to use when one is required for size calculations;
        :param `wnd`: The window onto which the tool will eventually be drawn;
        :param `bitmap_size`: The size of the tool's foreground bitmap;
        :param `kind`: The kind of tool (normal, dropdown, or hybrid);
        :param `is_first`: ``True`` if the tool is the first within its group. ``False``
         otherwise;
        :param `is_last`: ``True`` if the tool is the last within its group. ``False``
         otherwise;
        :param `dropdown_region`: For dropdown and hybrid tools, the region within the
         returned size which counts as the dropdown part.
        """

        size = wx.Size(*bitmap_size)
        size.IncBy(7, 6)

        if is_last:
            size.IncBy(1, 0)

        if kind & RIBBON_BUTTON_DROPDOWN:
            size.IncBy(8, 0)
            if kind == RIBBON_BUTTON_DROPDOWN:
                dropdown_region = wx.Rect(0, 0, *size)
            else:
                dropdown_region = wx.Rect(size.GetWidth() - 8, 0, 8, size.GetHeight())
        else:        
            dropdown_region = wx.Rect(0, 0, 0, 0)

        return size, dropdown_region