/usr/lib/python3/dist-packages/urwid/container.py is in python3-urwid 2.0.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/python
#
# Urwid container widget classes
# Copyright (C) 2004-2012 Ian Ward
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Urwid web site: http://excess.org/urwid/
from __future__ import division, print_function
from itertools import chain, repeat
from urwid.compat import xrange
from urwid.util import is_mouse_press
from urwid.widget import (Widget, Divider, FLOW, FIXED, PACK, BOX, WidgetWrap,
GIVEN, WEIGHT, LEFT, RIGHT, RELATIVE, TOP, BOTTOM, CLIP, RELATIVE_100)
from urwid.decoration import (Padding, Filler, calculate_left_right_padding,
calculate_top_bottom_filler, normalize_align, normalize_width,
normalize_valign, normalize_height, simplify_align, simplify_width,
simplify_valign, simplify_height)
from urwid.monitored_list import MonitoredList, MonitoredFocusList
from urwid.canvas import (CompositeCanvas, CanvasOverlay, CanvasCombine,
SolidCanvas, CanvasJoin)
class WidgetContainerMixin(object):
"""
Mixin class for widget containers implementing common container methods
"""
def __getitem__(self, position):
"""
Container short-cut for self.contents[position][0].base_widget
which means "give me the child widget at position without any
widget decorations".
This allows for concise traversal of nested container widgets
such as:
my_widget[position0][position1][position2] ...
"""
return self.contents[position][0].base_widget
def get_focus_path(self):
"""
Return the .focus_position values starting from this container
and proceeding along each child widget until reaching a leaf
(non-container) widget.
"""
out = []
w = self
while True:
try:
p = w.focus_position
except IndexError:
return out
out.append(p)
w = w.focus.base_widget
def set_focus_path(self, positions):
"""
Set the .focus_position property starting from this container
widget and proceeding along newly focused child widgets. Any
failed assignment due do incompatible position types or invalid
positions will raise an IndexError.
This method may be used to restore a particular widget to the
focus by passing in the value returned from an earlier call to
get_focus_path().
positions -- sequence of positions
"""
w = self
for p in positions:
if p != w.focus_position:
w.focus_position = p # modifies w.focus
w = w.focus.base_widget
def get_focus_widgets(self):
"""
Return the .focus values starting from this container
and proceeding along each child widget until reaching a leaf
(non-container) widget.
Note that the list does not contain the topmost container widget
(i.e., on which this method is called), but does include the
lowest leaf widget.
"""
out = []
w = self
while True:
w = w.base_widget.focus
if w is None:
return out
out.append(w)
class WidgetContainerListContentsMixin(object):
"""
Mixin class for widget containers whose positions are indexes into
a list available as self.contents.
"""
def __iter__(self):
"""
Return an iterable of positions for this container from first
to last.
"""
return iter(xrange(len(self.contents)))
def __reversed__(self):
"""
Return an iterable of positions for this container from last
to first.
"""
return iter(xrange(len(self.contents) - 1, -1, -1))
class GridFlowError(Exception):
pass
class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixin):
"""
The GridFlow widget is a flow widget that renders all the widgets it
contains the same width and it arranges them from left to right and top to
bottom.
"""
def sizing(self):
return frozenset([FLOW])
def __init__(self, cells, cell_width, h_sep, v_sep, align):
"""
:param cells: list of flow widgets to display
:param cell_width: column width for each cell
:param h_sep: blank columns between each cell horizontally
:param v_sep: blank rows between cells vertically
(if more than one row is required to display all the cells)
:param align: horizontal alignment of cells, one of\:
'left', 'center', 'right', ('relative', percentage 0=left 100=right)
"""
self._contents = MonitoredFocusList([
(w, (GIVEN, cell_width)) for w in cells])
self._contents.set_modified_callback(self._invalidate)
self._contents.set_focus_changed_callback(lambda f: self._invalidate())
self._contents.set_validate_contents_modified(self._contents_modified)
self._cell_width = cell_width
self.h_sep = h_sep
self.v_sep = v_sep
self.align = align
self._cache_maxcol = None
self.__super.__init__(None)
# set self._w to something other than None
self.get_display_widget(((h_sep+cell_width)*len(cells),))
def _invalidate(self):
self._cache_maxcol = None
self.__super._invalidate()
def _contents_modified(self, slc, new_items):
for item in new_items:
try:
w, (t, n) = item
if t != GIVEN:
raise ValueError
except (TypeError, ValueError):
raise GridFlowError("added content invalid %r" % (item,))
def _get_cells(self):
ml = MonitoredList(w for w, t in self.contents)
def user_modified():
self._set_cells(ml)
ml.set_modified_callback(user_modified)
return ml
def _set_cells(self, widgets):
focus_position = self.focus_position
self.contents = [
(new, (GIVEN, self._cell_width)) for new in widgets]
if focus_position < len(widgets):
self.focus_position = focus_position
cells = property(_get_cells, _set_cells, doc="""
A list of the widgets in this GridFlow
.. note:: only for backwards compatibility. You should use the new
use the new standard container property :attr:`contents` to modify
GridFlow contents.
""")
def _get_cell_width(self):
return self._cell_width
def _set_cell_width(self, width):
focus_position = self.focus_position
self.contents = [
(w, (GIVEN, width)) for (w, options) in self.contents]
self.focus_position = focus_position
self._cell_width = width
cell_width = property(_get_cell_width, _set_cell_width, doc="""
The width of each cell in the GridFlow. Setting this value affects
all cells.
""")
def _get_contents(self):
return self._contents
def _set_contents(self, c):
self._contents[:] = c
contents = property(_get_contents, _set_contents, doc="""
The contents of this GridFlow as a list of (widget, options)
tuples.
options is currently a tuple in the form `('fixed', number)`.
number is the number of screen columns to allocate to this cell.
'fixed' is the only type accepted at this time.
This list may be modified like a normal list and the GridFlow
widget will update automatically.
.. seealso:: Create new options tuples with the :meth:`options` method.
""")
def options(self, width_type=GIVEN, width_amount=None):
"""
Return a new options tuple for use in a GridFlow's .contents list.
width_type -- 'given' is the only value accepted
width_amount -- None to use the default cell_width for this GridFlow
"""
if width_type != GIVEN:
raise GridFlowError("invalid width_type: %r" % (width_type,))
if width_amount is None:
width_amount = self._cell_width
return (width_type, width_amount)
def set_focus(self, cell):
"""
Set the cell in focus, for backwards compatibility.
.. note:: only for backwards compatibility. You may also use the new
standard container property :attr:`focus_position` to get the focus.
:param cell: contained element to focus
:type cell: Widget or int
"""
if isinstance(cell, int):
return self._set_focus_position(cell)
return self._set_focus_cell(cell)
def get_focus(self):
"""
Return the widget in focus, for backwards compatibility.
.. note:: only for backwards compatibility. You may also use the new
standard container property :attr:`focus` to get the focus.
"""
if not self.contents:
return None
return self.contents[self.focus_position][0]
focus = property(get_focus,
doc="the child widget in focus or None when GridFlow is empty")
def _set_focus_cell(self, cell):
for i, (w, options) in enumerate(self.contents):
if cell == w:
self.focus_position = i
return
raise ValueError("Widget not found in GridFlow contents: %r" % (cell,))
focus_cell = property(get_focus, _set_focus_cell, doc="""
The widget in focus, for backwards compatibility.
.. note:: only for backwards compatibility. You should use the new
use the new standard container property :attr:`focus` to get the
widget in focus and :attr:`focus_position` to get/set the cell in
focus by index.
""")
def _get_focus_position(self):
"""
Return the index of the widget in focus or None if this GridFlow is
empty.
"""
if not self.contents:
raise IndexError("No focus_position, GridFlow is empty")
return self.contents.focus
def _set_focus_position(self, position):
"""
Set the widget in focus.
position -- index of child widget to be made focus
"""
try:
if position < 0 or position >= len(self.contents):
raise IndexError
except (TypeError, IndexError):
raise IndexError("No GridFlow child widget at position %s" % (position,))
self.contents.focus = position
focus_position = property(_get_focus_position, _set_focus_position, doc="""
index of child widget in focus. Raises :exc:`IndexError` if read when
GridFlow is empty, or when set to an invalid index.
""")
def get_display_widget(self, size):
"""
Arrange the cells into columns (and possibly a pile) for
display, input or to calculate rows, and update the display
widget.
"""
(maxcol,) = size
# use cache if possible
if self._cache_maxcol == maxcol:
return self._w
self._cache_maxcol = maxcol
self._w = self.generate_display_widget(size)
return self._w
def generate_display_widget(self, size):
"""
Actually generate display widget (ignoring cache)
"""
(maxcol,) = size
divider = Divider()
if not self.contents:
return divider
if self.v_sep > 1:
# increase size of divider
divider.top = self.v_sep-1
c = None
p = Pile([])
used_space = 0
for i, (w, (width_type, width_amount)) in enumerate(self.contents):
if c is None or maxcol - used_space < width_amount:
# starting a new row
if self.v_sep:
p.contents.append((divider, p.options()))
c = Columns([], self.h_sep)
column_focused = False
pad = Padding(c, self.align)
# extra attribute to reference contents position
pad.first_position = i
p.contents.append((pad, p.options()))
c.contents.append((w, c.options(GIVEN, width_amount)))
if ((i == self.focus_position) or
(not column_focused and w.selectable())):
c.focus_position = len(c.contents) - 1
column_focused = True
if i == self.focus_position:
p.focus_position = len(p.contents) - 1
used_space = (sum(x[1][1] for x in c.contents) +
self.h_sep * len(c.contents))
if width_amount > maxcol:
# special case: display is too narrow for the given
# width so we remove the Columns for better behaviour
# FIXME: determine why this is necessary
pad.original_widget=w
pad.width = used_space - self.h_sep
if self.v_sep:
# remove first divider
del p.contents[:1]
return p
def _set_focus_from_display_widget(self):
"""
Set the focus to the item in focus in the display widget.
"""
# display widget (self._w) is always built as:
#
# Pile([
# Padding(
# Columns([ # possibly
# cell, ...])),
# Divider(), # possibly
# ...])
pile_focus = self._w.focus
if not pile_focus:
return
c = pile_focus.base_widget
if c.focus:
col_focus_position = c.focus_position
else:
col_focus_position = 0
# pad.first_position was set by generate_display_widget() above
self.focus_position = pile_focus.first_position + col_focus_position
def keypress(self, size, key):
"""
Pass keypress to display widget for handling.
Captures focus changes.
"""
self.get_display_widget(size)
key = self.__super.keypress(size, key)
if key is None:
self._set_focus_from_display_widget()
return key
def rows(self, size, focus=False):
self.get_display_widget(size)
return self.__super.rows(size, focus=focus)
def render(self, size, focus=False ):
self.get_display_widget(size)
return self.__super.render(size, focus)
def get_cursor_coords(self, size):
"""Get cursor from display widget."""
self.get_display_widget(size)
return self.__super.get_cursor_coords(size)
def move_cursor_to_coords(self, size, col, row):
"""Set the widget in focus based on the col + row."""
self.get_display_widget(size)
rval = self.__super.move_cursor_to_coords(size, col, row)
self._set_focus_from_display_widget()
return rval
def mouse_event(self, size, event, button, col, row, focus):
self.get_display_widget(size)
self.__super.mouse_event(size, event, button, col, row, focus)
self._set_focus_from_display_widget()
return True # at a minimum we adjusted our focus
def get_pref_col(self, size):
"""Return pref col from display widget."""
self.get_display_widget(size)
return self.__super.get_pref_col(size)
class OverlayError(Exception):
pass
class Overlay(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin):
"""
Overlay contains two box widgets and renders one on top of the other
"""
_selectable = True
_sizing = frozenset([BOX])
_DEFAULT_BOTTOM_OPTIONS = (
LEFT, None, RELATIVE, 100, None, 0, 0,
TOP, None, RELATIVE, 100, None, 0, 0)
def __init__(self, top_w, bottom_w, align, width, valign, height,
min_width=None, min_height=None, left=0, right=0, top=0, bottom=0):
"""
:param top_w: a flow, box or fixed widget to overlay "on top"
:type top_w: Widget
:param bottom_w: a box widget to appear "below" previous widget
:type bottom_w: Widget
:param align: alignment, one of ``'left'``, ``'center'``, ``'right'`` or
(``'relative'``, *percentage* 0=left 100=right)
:type align: str
:param width: width type, one of:
``'pack'``
if *top_w* is a fixed widget
*given width*
integer number of columns wide
(``'relative'``, *percentage of total width*)
make *top_w* width related to container width
:param valign: alignment mode, one of ``'top'``, ``'middle'``, ``'bottom'`` or
(``'relative'``, *percentage* 0=top 100=bottom)
:param height: one of:
``'pack'``
if *top_w* is a flow or fixed widget
*given height*
integer number of rows high
(``'relative'``, *percentage of total height*)
make *top_w* height related to container height
:param min_width: the minimum number of columns for *top_w* when width
is not fixed
:type min_width: int
:param min_height: minimum number of rows for *top_w* when height
is not fixed
:type min_height: int
:param left: a fixed number of columns to add on the left
:type left: int
:param right: a fixed number of columns to add on the right
:type right: int
:param top: a fixed number of rows to add on the top
:type top: int
:param bottom: a fixed number of rows to add on the bottom
:type bottom: int
Overlay widgets behave similarly to :class:`Padding` and :class:`Filler`
widgets when determining the size and position of *top_w*. *bottom_w* is
always rendered the full size available "below" *top_w*.
"""
self.__super.__init__()
self.top_w = top_w
self.bottom_w = bottom_w
self.set_overlay_parameters(align, width, valign, height,
min_width, min_height, left, right, top, bottom)
def options(self, align_type, align_amount, width_type, width_amount,
valign_type, valign_amount, height_type, height_amount,
min_width=None, min_height=None, left=0, right=0, top=0, bottom=0):
"""
Return a new options tuple for use in this Overlay's .contents mapping.
This is the common container API to create options for replacing the
top widget of this Overlay. It is provided for completeness
but is not necessarily the easiest way to change the overlay parameters.
See also :meth:`.set_overlay_parameters`
"""
return (align_type, align_amount, width_type, width_amount,
min_width, left, right, valign_type, valign_amount,
height_type, height_amount, min_height, top, bottom)
def set_overlay_parameters(self, align, width, valign, height,
min_width=None, min_height=None, left=0, right=0, top=0, bottom=0):
"""
Adjust the overlay size and position parameters.
See :class:`__init__() <Overlay>` for a description of the parameters.
"""
# convert obsolete parameters 'fixed ...':
if isinstance(align, tuple):
if align[0] == 'fixed left':
left = align[1]
align = LEFT
elif align[0] == 'fixed right':
right = align[1]
align = RIGHT
if isinstance(width, tuple):
if width[0] == 'fixed left':
left = width[1]
width = RELATIVE_100
elif width[0] == 'fixed right':
right = width[1]
width = RELATIVE_100
if isinstance(valign, tuple):
if valign[0] == 'fixed top':
top = valign[1]
valign = TOP
elif valign[0] == 'fixed bottom':
bottom = valign[1]
valign = BOTTOM
if isinstance(height, tuple):
if height[0] == 'fixed bottom':
bottom = height[1]
height = RELATIVE_100
elif height[0] == 'fixed top':
top = height[1]
height = RELATIVE_100
if width is None: # more obsolete values accepted
width = PACK
if height is None:
height = PACK
align_type, align_amount = normalize_align(align, OverlayError)
width_type, width_amount = normalize_width(width, OverlayError)
valign_type, valign_amount = normalize_valign(valign, OverlayError)
height_type, height_amount = normalize_height(height, OverlayError)
if height_type in (GIVEN, PACK):
min_height = None
# use container API to set the parameters
self.contents[1] = (self.top_w, self.options(
align_type, align_amount, width_type, width_amount,
valign_type, valign_amount, height_type, height_amount,
min_width, min_height, left, right, top, bottom))
def selectable(self):
"""Return selectable from top_w."""
return self.top_w.selectable()
def keypress(self, size, key):
"""Pass keypress to top_w."""
return self.top_w.keypress(self.top_w_size(size,
*self.calculate_padding_filler(size, True)), key)
def _get_focus(self):
"""
Currently self.top_w is always the focus of an Overlay
"""
return self.top_w
focus = property(_get_focus,
doc="the top widget in this overlay is always in focus")
def _get_focus_position(self):
"""
Return the top widget position (currently always 1).
"""
return 1
def _set_focus_position(self, position):
"""
Set the widget in focus. Currently only position 0 is accepted.
position -- index of child widget to be made focus
"""
if position != 1:
raise IndexError("Overlay widget focus_position currently "
"must always be set to 1, not %s" % (position,))
focus_position = property(_get_focus_position, _set_focus_position,
doc="index of child widget in focus, currently always 1")
def _contents(self):
class OverlayContents(object):
def __len__(inner_self):
return 2
__getitem__ = self._contents__getitem__
__setitem__ = self._contents__setitem__
return OverlayContents()
def _contents__getitem__(self, index):
if index == 0:
return (self.bottom_w, self._DEFAULT_BOTTOM_OPTIONS)
if index == 1:
return (self.top_w, (
self.align_type, self.align_amount,
self.width_type, self.width_amount,
self.min_width, self.left,
self.right, self.valign_type, self.valign_amount,
self.height_type, self.height_amount,
self.min_height, self.top, self.bottom))
raise IndexError("Overlay.contents has no position %r"
% (index,))
def _contents__setitem__(self, index, value):
try:
value_w, value_options = value
except (ValueError, TypeError):
raise OverlayError("added content invalid: %r" % (value,))
if index == 0:
if value_options != self._DEFAULT_BOTTOM_OPTIONS:
raise OverlayError("bottom_options must be set to "
"%r" % (self._DEFAULT_BOTTOM_OPTIONS,))
self.bottom_w = value_w
elif index == 1:
try:
(align_type, align_amount, width_type, width_amount,
min_width, left, right, valign_type, valign_amount,
height_type, height_amount, min_height, top, bottom,
) = value_options
except (ValueError, TypeError):
raise OverlayError("top_options is invalid: %r"
% (value_options,))
# normalize first, this is where errors are raised
align_type, align_amount = normalize_align(
simplify_align(align_type, align_amount), OverlayError)
width_type, width_amount = normalize_width(
simplify_width(width_type, width_amount), OverlayError)
valign_type, valign_amoun = normalize_valign(
simplify_valign(valign_type, valign_amount), OverlayError)
height_type, height_amount = normalize_height(
simplify_height(height_type, height_amount), OverlayError)
self.align_type = align_type
self.align_amount = align_amount
self.width_type = width_type
self.width_amount = width_amount
self.valign_type = valign_type
self.valign_amount = valign_amount
self.height_type = height_type
self.height_amount = height_amount
self.left = left
self.right = right
self.top = top
self.bottom = bottom
self.min_width = min_width
self.min_height = min_height
else:
raise IndexError("Overlay.contents has no position %r"
% (index,))
self._invalidate()
contents = property(_contents, doc="""
a list-like object similar to::
[(bottom_w, bottom_options)),
(top_w, top_options)]
This object may be used to read or update top and bottom widgets and
top widgets's options, but no widgets may be added or removed.
`top_options` takes the form
`(align_type, align_amount, width_type, width_amount, min_width, left,
right, valign_type, valign_amount, height_type, height_amount,
min_height, top, bottom)`
bottom_options is always
`('left', None, 'relative', 100, None, 0, 0,
'top', None, 'relative', 100, None, 0, 0)`
which means that bottom widget always covers the full area of the Overlay.
writing a different value for `bottom_options` raises an
:exc:`OverlayError`.
""")
def get_cursor_coords(self, size):
"""Return cursor coords from top_w, if any."""
if not hasattr(self.top_w, 'get_cursor_coords'):
return None
(maxcol, maxrow) = size
left, right, top, bottom = self.calculate_padding_filler(size,
True)
x, y = self.top_w.get_cursor_coords(
(maxcol-left-right, maxrow-top-bottom) )
if y >= maxrow: # required??
y = maxrow-1
return x+left, y+top
def calculate_padding_filler(self, size, focus):
"""Return (padding left, right, filler top, bottom)."""
(maxcol, maxrow) = size
height = None
if self.width_type == PACK:
width, height = self.top_w.pack((),focus=focus)
if not height:
raise OverlayError("fixed widget must have a height")
left, right = calculate_left_right_padding(maxcol,
self.align_type, self.align_amount, CLIP, width,
None, self.left, self.right)
else:
left, right = calculate_left_right_padding(maxcol,
self.align_type, self.align_amount,
self.width_type, self.width_amount,
self.min_width, self.left, self.right)
if height:
# top_w is a fixed widget
top, bottom = calculate_top_bottom_filler(maxrow,
self.valign_type, self.valign_amount,
GIVEN, height, None, self.top, self.bottom)
if maxrow-top-bottom < height:
bottom = maxrow-top-height
elif self.height_type == PACK:
# top_w is a flow widget
height = self.top_w.rows((maxcol,),focus=focus)
top, bottom = calculate_top_bottom_filler(maxrow,
self.valign_type, self.valign_amount,
GIVEN, height, None, self.top, self.bottom)
if height > maxrow: # flow widget rendered too large
bottom = maxrow - height
else:
top, bottom = calculate_top_bottom_filler(maxrow,
self.valign_type, self.valign_amount,
self.height_type, self.height_amount,
self.min_height, self.top, self.bottom)
return left, right, top, bottom
def top_w_size(self, size, left, right, top, bottom):
"""Return the size to pass to top_w."""
if self.width_type == PACK:
# top_w is a fixed widget
return ()
maxcol, maxrow = size
if self.width_type != PACK and self.height_type == PACK:
# top_w is a flow widget
return (maxcol-left-right,)
return (maxcol-left-right, maxrow-top-bottom)
def render(self, size, focus=False):
"""Render top_w overlayed on bottom_w."""
left, right, top, bottom = self.calculate_padding_filler(size,
focus)
bottom_c = self.bottom_w.render(size)
if not bottom_c.cols() or not bottom_c.rows():
return CompositeCanvas(bottom_c)
top_c = self.top_w.render(
self.top_w_size(size, left, right, top, bottom), focus)
top_c = CompositeCanvas(top_c)
if left < 0 or right < 0:
top_c.pad_trim_left_right(min(0, left), min(0, right))
if top < 0 or bottom < 0:
top_c.pad_trim_top_bottom(min(0, top), min(0, bottom))
return CanvasOverlay(top_c, bottom_c, left, top)
def mouse_event(self, size, event, button, col, row, focus):
"""Pass event to top_w, ignore if outside of top_w."""
if not hasattr(self.top_w, 'mouse_event'):
return False
left, right, top, bottom = self.calculate_padding_filler(size,
focus)
maxcol, maxrow = size
if ( col<left or col>=maxcol-right or
row<top or row>=maxrow-bottom ):
return False
return self.top_w.mouse_event(
self.top_w_size(size, left, right, top, bottom),
event, button, col-left, row-top, focus )
class FrameError(Exception):
pass
class Frame(Widget, WidgetContainerMixin):
"""
Frame widget is a box widget with optional header and footer
flow widgets placed above and below the box widget.
.. note:: The main difference between a Frame and a :class:`Pile` widget
defined as: `Pile([('pack', header), body, ('pack', footer)])` is that
the Frame will not automatically change focus up and down in response to
keystrokes.
"""
_selectable = True
_sizing = frozenset([BOX])
def __init__(self, body, header=None, footer=None, focus_part='body'):
"""
:param body: a box widget for the body of the frame
:type body: Widget
:param header: a flow widget for above the body (or None)
:type header: Widget
:param footer: a flow widget for below the body (or None)
:type footer: Widget
:param focus_part: 'header', 'footer' or 'body'
:type focus_part: str
"""
self.__super.__init__()
self._header = header
self._body = body
self._footer = footer
self.focus_part = focus_part
def get_header(self):
return self._header
def set_header(self, header):
self._header = header
if header is None and self.focus_part == 'header':
self.focus_part = 'body'
self._invalidate()
header = property(get_header, set_header)
def get_body(self):
return self._body
def set_body(self, body):
self._body = body
self._invalidate()
body = property(get_body, set_body)
def get_footer(self):
return self._footer
def set_footer(self, footer):
self._footer = footer
if footer is None and self.focus_part == 'footer':
self.focus_part = 'body'
self._invalidate()
footer = property(get_footer, set_footer)
def set_focus(self, part):
"""
Determine which part of the frame is in focus.
.. note:: included for backwards compatibility. You should rather use
the container property :attr:`.focus_position` to set this value.
:param part: 'header', 'footer' or 'body'
:type part: str
"""
if part not in ('header', 'footer', 'body'):
raise IndexError('Invalid position for Frame: %s' % (part,))
if (part == 'header' and self._header is None) or (
part == 'footer' and self._footer is None):
raise IndexError('This Frame has no %s' % (part,))
self.focus_part = part
self._invalidate()
def get_focus(self):
"""
Return an indicator which part of the frame is in focus
.. note:: included for backwards compatibility. You should rather use
the container property :attr:`.focus_position` to get this value.
:returns: one of 'header', 'footer' or 'body'.
:rtype: str
"""
return self.focus_part
def _get_focus(self):
return {
'header': self._header,
'footer': self._footer,
'body': self._body
}[self.focus_part]
focus = property(_get_focus, doc="""
child :class:`Widget` in focus: the body, header or footer widget.
This is a read-only property.""")
focus_position = property(get_focus, set_focus, doc="""
writeable property containing an indicator which part of the frame
that is in focus: `'body', 'header'` or `'footer'`.
""")
def _contents(self):
class FrameContents(object):
def __len__(inner_self):
return len(inner_self.keys())
def items(inner_self):
return [(k, inner_self[k]) for k in inner_self.keys()]
def values(inner_self):
return [inner_self[k] for k in inner_self.keys()]
def update(inner_self, E=None, **F):
if E:
keys = getattr(E, 'keys', None)
if keys:
for k in E:
inner_self[k] = E[k]
else:
for k, v in E:
inner_self[k] = v
for k in F:
inner_self[k] = F[k]
keys = self._contents_keys
__getitem__ = self._contents__getitem__
__setitem__ = self._contents__setitem__
__delitem__ = self._contents__delitem__
return FrameContents()
def _contents_keys(self):
keys = ['body']
if self._header:
keys.append('header')
if self._footer:
keys.append('footer')
return keys
def _contents__getitem__(self, key):
if key == 'body':
return (self._body, None)
if key == 'header' and self._header:
return (self._header, None)
if key == 'footer' and self._footer:
return (self._footer, None)
raise KeyError("Frame.contents has no key: %r" % (key,))
def _contents__setitem__(self, key, value):
if key not in ('body', 'header', 'footer'):
raise KeyError("Frame.contents has no key: %r" % (key,))
try:
value_w, value_options = value
if value_options is not None:
raise ValueError
except (ValueError, TypeError):
raise FrameError("added content invalid: %r" % (value,))
if key == 'body':
self.body = value_w
elif key == 'footer':
self.footer = value_w
else:
self.header = value_w
def _contents__delitem__(self, key):
if key not in ('header', 'footer'):
raise KeyError("Frame.contents can't remove key: %r" % (key,))
if (key == 'header' and self._header is None
) or (key == 'footer' and self._footer is None):
raise KeyError("Frame.contents has no key: %r" % (key,))
if key == 'header':
self.header = None
else:
self.footer = None
contents = property(_contents, doc="""
a dict-like object similar to::
{
'body': (body_widget, None),
'header': (header_widget, None), # if frame has a header
'footer': (footer_widget, None) # if frame has a footer
}
This object may be used to read or update the contents of the Frame.
The values are similar to the list-like .contents objects used
in other containers with (:class:`Widget`, options) tuples, but are
constrained to keys for each of the three usual parts of a Frame.
When other keys are used a :exc:`KeyError` will be raised.
Currently all options are `None`, but using the :meth:`options` method
to create the options value is recommended for forwards
compatibility.
""")
def options(self):
"""
There are currently no options for Frame contents.
Return None as a placeholder for future options.
"""
return None
def frame_top_bottom(self, size, focus):
"""
Calculate the number of rows for the header and footer.
:param size: See :meth:`Widget.render` for details
:type size: widget size
:param focus: ``True`` if this widget is in focus
:type focus: bool
:returns: `(head rows, foot rows),(orig head, orig foot)`
orig head/foot are from rows() calls.
:rtype: (int, int), (int, int)
"""
(maxcol, maxrow) = size
frows = hrows = 0
if self.header:
hrows = self.header.rows((maxcol,),
self.focus_part=='header' and focus)
if self.footer:
frows = self.footer.rows((maxcol,),
self.focus_part=='footer' and focus)
remaining = maxrow
if self.focus_part == 'footer':
if frows >= remaining:
return (0, remaining),(hrows, frows)
remaining -= frows
if hrows >= remaining:
return (remaining, frows),(hrows, frows)
elif self.focus_part == 'header':
if hrows >= maxrow:
return (remaining, 0),(hrows, frows)
remaining -= hrows
if frows >= remaining:
return (hrows, remaining),(hrows, frows)
elif hrows + frows >= remaining:
# self.focus_part == 'body'
rless1 = max(0, remaining-1)
if frows >= remaining-1:
return (0, rless1),(hrows, frows)
remaining -= frows
rless1 = max(0, remaining-1)
return (rless1,frows),(hrows, frows)
return (hrows, frows),(hrows, frows)
def render(self, size, focus=False):
(maxcol, maxrow) = size
(htrim, ftrim),(hrows, frows) = self.frame_top_bottom(
(maxcol, maxrow), focus)
combinelist = []
depends_on = []
head = None
if htrim and htrim < hrows:
head = Filler(self.header, 'top').render(
(maxcol, htrim),
focus and self.focus_part == 'header')
elif htrim:
head = self.header.render((maxcol,),
focus and self.focus_part == 'header')
assert head.rows() == hrows, "rows, render mismatch"
if head:
combinelist.append((head, 'header',
self.focus_part == 'header'))
depends_on.append(self.header)
if ftrim+htrim < maxrow:
body = self.body.render((maxcol, maxrow-ftrim-htrim),
focus and self.focus_part == 'body')
combinelist.append((body, 'body',
self.focus_part == 'body'))
depends_on.append(self.body)
foot = None
if ftrim and ftrim < frows:
foot = Filler(self.footer, 'bottom').render(
(maxcol, ftrim),
focus and self.focus_part == 'footer')
elif ftrim:
foot = self.footer.render((maxcol,),
focus and self.focus_part == 'footer')
assert foot.rows() == frows, "rows, render mismatch"
if foot:
combinelist.append((foot, 'footer',
self.focus_part == 'footer'))
depends_on.append(self.footer)
return CanvasCombine(combinelist)
def keypress(self, size, key):
"""Pass keypress to widget in focus."""
(maxcol, maxrow) = size
if self.focus_part == 'header' and self.header is not None:
if not self.header.selectable():
return key
return self.header.keypress((maxcol,),key)
if self.focus_part == 'footer' and self.footer is not None:
if not self.footer.selectable():
return key
return self.footer.keypress((maxcol,),key)
if self.focus_part != 'body':
return key
remaining = maxrow
if self.header is not None:
remaining -= self.header.rows((maxcol,))
if self.footer is not None:
remaining -= self.footer.rows((maxcol,))
if remaining <= 0: return key
if not self.body.selectable():
return key
return self.body.keypress( (maxcol, remaining), key )
def mouse_event(self, size, event, button, col, row, focus):
"""
Pass mouse event to appropriate part of frame.
Focus may be changed on button 1 press.
"""
(maxcol, maxrow) = size
(htrim, ftrim),(hrows, frows) = self.frame_top_bottom(
(maxcol, maxrow), focus)
if row < htrim: # within header
focus = focus and self.focus_part == 'header'
if is_mouse_press(event) and button==1:
if self.header.selectable():
self.set_focus('header')
if not hasattr(self.header, 'mouse_event'):
return False
return self.header.mouse_event( (maxcol,), event,
button, col, row, focus )
if row >= maxrow-ftrim: # within footer
focus = focus and self.focus_part == 'footer'
if is_mouse_press(event) and button==1:
if self.footer.selectable():
self.set_focus('footer')
if not hasattr(self.footer, 'mouse_event'):
return False
return self.footer.mouse_event( (maxcol,), event,
button, col, row-maxrow+frows, focus )
# within body
focus = focus and self.focus_part == 'body'
if is_mouse_press(event) and button==1:
if self.body.selectable():
self.set_focus('body')
if not hasattr(self.body, 'mouse_event'):
return False
return self.body.mouse_event( (maxcol, maxrow-htrim-ftrim),
event, button, col, row-htrim, focus )
def __iter__(self):
"""
Return an iterator over the positions in this Frame top to bottom.
"""
if self._header:
yield 'header'
yield 'body'
if self._footer:
yield 'footer'
def __reversed__(self):
"""
Return an iterator over the positions in this Frame bottom to top.
"""
if self._footer:
yield 'footer'
yield 'body'
if self._header:
yield 'header'
class PileError(Exception):
pass
class Pile(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin):
"""
A pile of widgets stacked vertically from top to bottom
"""
_sizing = frozenset([FLOW, BOX])
def __init__(self, widget_list, focus_item=None):
"""
:param widget_list: child widgets
:type widget_list: iterable
:param focus_item: child widget that gets the focus initially.
Chooses the first selectable widget if unset.
:type focus_item: Widget or int
*widget_list* may also contain tuples such as:
(*given_height*, *widget*)
always treat *widget* as a box widget and give it *given_height* rows,
where given_height is an int
(``'pack'``, *widget*)
allow *widget* to calculate its own height by calling its :meth:`rows`
method, ie. treat it as a flow widget.
(``'weight'``, *weight*, *widget*)
if the pile is treated as a box widget then treat widget as a box
widget with a height based on its relative weight value, otherwise
treat the same as (``'pack'``, *widget*).
Widgets not in a tuple are the same as (``'weight'``, ``1``, *widget*)`
.. note:: If the Pile is treated as a box widget there must be at least
one ``'weight'`` tuple in :attr:`widget_list`.
"""
self.__super.__init__()
self._contents = MonitoredFocusList()
self._contents.set_modified_callback(self._invalidate)
self._contents.set_focus_changed_callback(lambda f: self._invalidate())
self._contents.set_validate_contents_modified(self._contents_modified)
focus_item = focus_item
for i, original in enumerate(widget_list):
w = original
if not isinstance(w, tuple):
self.contents.append((w, (WEIGHT, 1)))
elif w[0] in (FLOW, PACK):
f, w = w
self.contents.append((w, (PACK, None)))
elif len(w) == 2:
height, w = w
self.contents.append((w, (GIVEN, height)))
elif w[0] == FIXED: # backwards compatibility
_ignore, height, w = w
self.contents.append((w, (GIVEN, height)))
elif w[0] == WEIGHT:
f, height, w = w
self.contents.append((w, (f, height)))
else:
raise PileError(
"initial widget list item invalid %r" % (original,))
if focus_item is None and w.selectable():
focus_item = i
if self.contents and focus_item is not None:
self.set_focus(focus_item)
self.pref_col = 0
def _contents_modified(self, slc, new_items):
for item in new_items:
try:
w, (t, n) = item
if t not in (PACK, GIVEN, WEIGHT):
raise ValueError
except (TypeError, ValueError):
raise PileError("added content invalid: %r" % (item,))
def _get_widget_list(self):
ml = MonitoredList(w for w, t in self.contents)
def user_modified():
self._set_widget_list(ml)
ml.set_modified_callback(user_modified)
return ml
def _set_widget_list(self, widgets):
focus_position = self.focus_position
self.contents = [
(new, options) for (new, (w, options)) in zip(widgets,
# need to grow contents list if widgets is longer
chain(self.contents, repeat((None, (WEIGHT, 1)))))]
if focus_position < len(widgets):
self.focus_position = focus_position
widget_list = property(_get_widget_list, _set_widget_list, doc="""
A list of the widgets in this Pile
.. note:: only for backwards compatibility. You should use the new
standard container property :attr:`contents`.
""")
def _get_item_types(self):
ml = MonitoredList(
# return the old item type names
({GIVEN: FIXED, PACK: FLOW}.get(f, f), height)
for w, (f, height) in self.contents)
def user_modified():
self._set_item_types(ml)
ml.set_modified_callback(user_modified)
return ml
def _set_item_types(self, item_types):
focus_position = self.focus_position
self.contents = [
(w, ({FIXED: GIVEN, FLOW: PACK}.get(new_t, new_t), new_height))
for ((new_t, new_height), (w, options))
in zip(item_types, self.contents)]
if focus_position < len(item_types):
self.focus_position = focus_position
item_types = property(_get_item_types, _set_item_types, doc="""
A list of the options values for widgets in this Pile.
.. note:: only for backwards compatibility. You should use the new
standard container property :attr:`contents`.
""")
def _get_contents(self):
return self._contents
def _set_contents(self, c):
self._contents[:] = c
contents = property(_get_contents, _set_contents, doc="""
The contents of this Pile as a list of (widget, options) tuples.
options currently may be one of
(``'pack'``, ``None``)
allow widget to calculate its own height by calling its
:meth:`rows <Widget.rows>` method, i.e. treat it as a flow widget.
(``'given'``, *n*)
Always treat widget as a box widget with a given height of *n* rows.
(``'weight'``, *w*)
If the Pile itself is treated as a box widget then
the value *w* will be used as a relative weight for assigning rows
to this box widget. If the Pile is being treated as a flow
widget then this is the same as (``'pack'``, ``None``) and the *w*
value is ignored.
If the Pile itself is treated as a box widget then at least one
widget must have a (``'weight'``, *w*) options value, or the Pile will
not be able to grow to fill the required number of rows.
This list may be modified like a normal list and the Pile widget
will updated automatically.
.. seealso:: Create new options tuples with the :meth:`options` method
""")
def options(self, height_type=WEIGHT, height_amount=1):
"""
Return a new options tuple for use in a Pile's :attr:`contents` list.
:param height_type: ``'pack'``, ``'given'`` or ``'weight'``
:param height_amount: ``None`` for ``'pack'``, a number of rows for
``'fixed'`` or a weight value (number) for ``'weight'``
"""
if height_type == PACK:
return (PACK, None)
if height_type not in (GIVEN, WEIGHT):
raise PileError('invalid height_type: %r' % (height_type,))
return (height_type, height_amount)
def selectable(self):
"""Return True if the focus item is selectable."""
w = self.focus
return w is not None and w.selectable()
def set_focus(self, item):
"""
Set the item in focus, for backwards compatibility.
.. note:: only for backwards compatibility. You should use the new
standard container property :attr:`focus_position`.
to set the position by integer index instead.
:param item: element to focus
:type item: Widget or int
"""
if isinstance(item, int):
return self._set_focus_position(item)
for i, (w, options) in enumerate(self.contents):
if item == w:
self.focus_position = i
return
raise ValueError("Widget not found in Pile contents: %r" % (item,))
def get_focus(self):
"""
Return the widget in focus, for backwards compatibility. You may
also use the new standard container property .focus to get the
child widget in focus.
"""
if not self.contents:
return None
return self.contents[self.focus_position][0]
focus = property(get_focus,
doc="the child widget in focus or None when Pile is empty")
focus_item = property(get_focus, set_focus, doc="""
A property for reading and setting the widget in focus.
.. note::
only for backwards compatibility. You should use the new
standard container properties :attr:`focus` and
:attr:`focus_position` to get the child widget in focus or modify the
focus position.
""")
def _get_focus_position(self):
"""
Return the index of the widget in focus or None if this Pile is
empty.
"""
if not self.contents:
raise IndexError("No focus_position, Pile is empty")
return self.contents.focus
def _set_focus_position(self, position):
"""
Set the widget in focus.
position -- index of child widget to be made focus
"""
try:
if position < 0 or position >= len(self.contents):
raise IndexError
except (TypeError, IndexError):
raise IndexError("No Pile child widget at position %s" % (position,))
self.contents.focus = position
focus_position = property(_get_focus_position, _set_focus_position, doc="""
index of child widget in focus. Raises :exc:`IndexError` if read when
Pile is empty, or when set to an invalid index.
""")
def get_pref_col(self, size):
"""Return the preferred column for the cursor, or None."""
if not self.selectable():
return None
self._update_pref_col_from_focus(size)
return self.pref_col
def get_item_size(self, size, i, focus, item_rows=None):
"""
Return a size appropriate for passing to self.contents[i][0].render
"""
maxcol = size[0]
w, (f, height) = self.contents[i]
if f == GIVEN:
return (maxcol, height)
elif f == WEIGHT and len(size) == 2:
if not item_rows:
item_rows = self.get_item_rows(size, focus)
return (maxcol, item_rows[i])
else:
return (maxcol,)
def get_item_rows(self, size, focus):
"""
Return a list of the number of rows used by each widget
in self.contents
"""
remaining = None
maxcol = size[0]
if len(size) == 2:
remaining = size[1]
l = []
if remaining is None:
# pile is a flow widget
for w, (f, height) in self.contents:
if f == GIVEN:
l.append(height)
else:
l.append(w.rows((maxcol,),
focus=focus and self.focus_item == w))
return l
# pile is a box widget
# do an extra pass to calculate rows for each widget
wtotal = 0
for w, (f, height) in self.contents:
if f == PACK:
rows = w.rows((maxcol,), focus=focus and self.focus_item == w)
l.append(rows)
remaining -= rows
elif f == GIVEN:
l.append(height)
remaining -= height
elif height:
l.append(None)
wtotal += height
else:
l.append(0) # zero-weighted items treated as ('given', 0)
if wtotal == 0:
raise PileError("No weighted widgets found for Pile treated as a box widget")
if remaining < 0:
remaining = 0
for i, (w, (f, height)) in enumerate(self.contents):
li = l[i]
if li is None:
rows = int(float(remaining) * height / wtotal + 0.5)
l[i] = rows
remaining -= rows
wtotal -= height
return l
def render(self, size, focus=False):
maxcol = size[0]
item_rows = None
combinelist = []
for i, (w, (f, height)) in enumerate(self.contents):
item_focus = self.focus_item == w
canv = None
if f == GIVEN:
canv = w.render((maxcol, height), focus=focus and item_focus)
elif f == PACK or len(size)==1:
canv = w.render((maxcol,), focus=focus and item_focus)
else:
if item_rows is None:
item_rows = self.get_item_rows(size, focus)
rows = item_rows[i]
if rows>0:
canv = w.render((maxcol, rows), focus=focus and item_focus)
if canv:
combinelist.append((canv, i, item_focus))
if not combinelist:
return SolidCanvas(" ", size[0], (size[1:]+(0,))[0])
out = CanvasCombine(combinelist)
if len(size) == 2 and size[1] != out.rows():
# flow/fixed widgets rendered too large/small
out = CompositeCanvas(out)
out.pad_trim_top_bottom(0, size[1] - out.rows())
return out
def get_cursor_coords(self, size):
"""Return the cursor coordinates of the focus widget."""
if not self.selectable():
return None
if not hasattr(self.focus_item, 'get_cursor_coords'):
return None
i = self.focus_position
w, (f, height) = self.contents[i]
item_rows = None
maxcol = size[0]
if f == GIVEN or (f == WEIGHT and len(size) == 2):
if f == GIVEN:
maxrow = height
else:
if item_rows is None:
item_rows = self.get_item_rows(size, focus=True)
maxrow = item_rows[i]
coords = self.focus_item.get_cursor_coords((maxcol, maxrow))
else:
coords = self.focus_item.get_cursor_coords((maxcol,))
if coords is None:
return None
x,y = coords
if i > 0:
if item_rows is None:
item_rows = self.get_item_rows(size, focus=True)
for r in item_rows[:i]:
y += r
return x, y
def rows(self, size, focus=False ):
return sum(self.get_item_rows(size, focus))
def keypress(self, size, key ):
"""Pass the keypress to the widget in focus.
Unhandled 'up' and 'down' keys may cause a focus change."""
if not self.contents:
return key
item_rows = None
if len(size) == 2:
item_rows = self.get_item_rows(size, focus=True)
i = self.focus_position
if self.selectable():
tsize = self.get_item_size(size, i, True, item_rows)
key = self.focus.keypress(tsize, key)
if self._command_map[key] not in ('cursor up', 'cursor down'):
return key
if self._command_map[key] == 'cursor up':
candidates = list(range(i-1, -1, -1)) # count backwards to 0
else: # self._command_map[key] == 'cursor down'
candidates = list(range(i+1, len(self.contents)))
if not item_rows:
item_rows = self.get_item_rows(size, focus=True)
for j in candidates:
if not self.contents[j][0].selectable():
continue
self._update_pref_col_from_focus(size)
self.focus_position = j
if not hasattr(self.focus, 'move_cursor_to_coords'):
return
rows = item_rows[j]
if self._command_map[key] == 'cursor up':
rowlist = list(range(rows-1, -1, -1))
else: # self._command_map[key] == 'cursor down'
rowlist = list(range(rows))
for row in rowlist:
tsize = self.get_item_size(size, j, True, item_rows)
if self.focus_item.move_cursor_to_coords(
tsize, self.pref_col, row):
break
return
# nothing to select
return key
def _update_pref_col_from_focus(self, size):
"""Update self.pref_col from the focus widget."""
if not hasattr(self.focus, 'get_pref_col'):
return
i = self.focus_position
tsize = self.get_item_size(size, i, True)
pref_col = self.focus.get_pref_col(tsize)
if pref_col is not None:
self.pref_col = pref_col
def move_cursor_to_coords(self, size, col, row):
"""Capture pref col and set new focus."""
self.pref_col = col
#FIXME guessing focus==True
focus=True
wrow = 0
item_rows = self.get_item_rows(size, focus)
for i, (r, w) in enumerate(zip(item_rows,
(w for (w, options) in self.contents))):
if wrow + r > row:
break
wrow += r
else:
return False
if not w.selectable():
return False
if hasattr(w, 'move_cursor_to_coords'):
tsize = self.get_item_size(size, i, focus, item_rows)
rval = w.move_cursor_to_coords(tsize, col, row-wrow)
if rval is False:
return False
self.focus_position = i
return True
def mouse_event(self, size, event, button, col, row, focus):
"""
Pass the event to the contained widget.
May change focus on button 1 press.
"""
wrow = 0
item_rows = self.get_item_rows(size, focus)
for i, (r, w) in enumerate(zip(item_rows,
(w for (w, options) in self.contents))):
if wrow + r > row:
break
wrow += r
else:
return False
focus = focus and self.focus_item == w
if is_mouse_press(event) and button == 1:
if w.selectable():
self.focus_position = i
if not hasattr(w, 'mouse_event'):
return False
tsize = self.get_item_size(size, i, focus, item_rows)
return w.mouse_event(tsize, event, button, col, row-wrow,
focus)
class ColumnsError(Exception):
pass
class Columns(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin):
"""
Widgets arranged horizontally in columns from left to right
"""
_sizing = frozenset([FLOW, BOX])
def __init__(self, widget_list, dividechars=0, focus_column=None,
min_width=1, box_columns=None):
"""
:param widget_list: iterable of flow or box widgets
:param dividechars: number of blank characters between columns
:param focus_column: index into widget_list of column in focus,
if ``None`` the first selectable widget will be chosen.
:param min_width: minimum width for each column which is not
calling widget.pack() in *widget_list*.
:param box_columns: a list of column indexes containing box widgets
whose height is set to the maximum of the rows
required by columns not listed in *box_columns*.
*widget_list* may also contain tuples such as:
(*given_width*, *widget*)
make this column *given_width* screen columns wide, where *given_width*
is an int
(``'pack'``, *widget*)
call :meth:`pack() <Widget.pack>` to calculate the width of this column
(``'weight'``, *weight*, *widget*)
give this column a relative *weight* (number) to calculate its width from the
screen columns remaining
Widgets not in a tuple are the same as (``'weight'``, ``1``, *widget*)
If the Columns widget is treated as a box widget then all children
are treated as box widgets, and *box_columns* is ignored.
If the Columns widget is treated as a flow widget then the rows
are calculated as the largest rows() returned from all columns
except the ones listed in *box_columns*. The box widgets in
*box_columns* will be displayed with this calculated number of rows,
filling the full height.
"""
self.__super.__init__()
self._contents = MonitoredFocusList()
self._contents.set_modified_callback(self._invalidate)
self._contents.set_focus_changed_callback(lambda f: self._invalidate())
self._contents.set_validate_contents_modified(self._contents_modified)
box_columns = set(box_columns or ())
for i, original in enumerate(widget_list):
w = original
if not isinstance(w, tuple):
self.contents.append((w, (WEIGHT, 1, i in box_columns)))
elif w[0] in (FLOW, PACK): # 'pack' used to be called 'flow'
f = PACK
_ignored, w = w
self.contents.append((w, (f, None, i in box_columns)))
elif len(w) == 2:
width, w = w
self.contents.append((w, (GIVEN, width, i in box_columns)))
elif w[0] == FIXED: # backwards compatibility
f = GIVEN
_ignored, width, w = w
self.contents.append((w, (GIVEN, width, i in box_columns)))
elif w[0] == WEIGHT:
f, width, w = w
self.contents.append((w, (f, width, i in box_columns)))
else:
raise ColumnsError(
"initial widget list item invalid: %r" % (original,))
if focus_column is None and w.selectable():
focus_column = i
self.dividechars = dividechars
if self.contents and focus_column is not None:
self.focus_position = focus_column
if focus_column is None:
focus_column = 0
self.pref_col = None
self.min_width = min_width
self._cache_maxcol = None
def _contents_modified(self, slc, new_items):
for item in new_items:
try:
w, (t, n, b) = item
if t not in (PACK, GIVEN, WEIGHT):
raise ValueError
except (TypeError, ValueError):
raise ColumnsError("added content invalid %r" % (item,))
def _get_widget_list(self):
ml = MonitoredList(w for w, t in self.contents)
def user_modified():
self._set_widget_list(ml)
ml.set_modified_callback(user_modified)
return ml
def _set_widget_list(self, widgets):
focus_position = self.focus_position
self.contents = [
(new, options) for (new, (w, options)) in zip(widgets,
# need to grow contents list if widgets is longer
chain(self.contents, repeat((None, (WEIGHT, 1, False)))))]
if focus_position < len(widgets):
self.focus_position = focus_position
widget_list = property(_get_widget_list, _set_widget_list, doc="""
A list of the widgets in this Columns
.. note:: only for backwards compatibility. You should use the new
standard container property :attr:`contents`.
""")
def _get_column_types(self):
ml = MonitoredList(
# return the old column type names
({GIVEN: FIXED, PACK: FLOW}.get(t, t), n)
for w, (t, n, b) in self.contents)
def user_modified():
self._set_column_types(ml)
ml.set_modified_callback(user_modified)
return ml
def _set_column_types(self, column_types):
focus_position = self.focus_position
self.contents = [
(w, ({FIXED: GIVEN, FLOW: PACK}.get(new_t, new_t), new_n, b))
for ((new_t, new_n), (w, (t, n, b)))
in zip(column_types, self.contents)]
if focus_position < len(column_types):
self.focus_position = focus_position
column_types = property(_get_column_types, _set_column_types, doc="""
A list of the old partial options values for widgets in this Pile,
for backwards compatibility only. You should use the new standard
container property .contents to modify Pile contents.
""")
def _get_box_columns(self):
ml = MonitoredList(
i for i, (w, (t, n, b)) in enumerate(self.contents) if b)
def user_modified():
self._set_box_columns(ml)
ml.set_modified_callback(user_modified)
return ml
def _set_box_columns(self, box_columns):
box_columns = set(box_columns)
self.contents = [
(w, (t, n, i in box_columns))
for (i, (w, (t, n, b))) in enumerate(self.contents)]
box_columns = property(_get_box_columns, _set_box_columns, doc="""
A list of the indexes of the columns that are to be treated as
box widgets when the Columns is treated as a flow widget.
.. note:: only for backwards compatibility. You should use the new
standard container property :attr:`contents`.
""")
def _get_has_pack_type(self):
import warnings
warnings.warn(".has_flow_type is deprecated, "
"read values from .contents instead.", DeprecationWarning)
return PACK in self.column_types
def _set_has_pack_type(self, value):
import warnings
warnings.warn(".has_flow_type is deprecated, "
"read values from .contents instead.", DeprecationWarning)
has_flow_type = property(_get_has_pack_type, _set_has_pack_type, doc="""
.. deprecated:: 1.0 Read values from :attr:`contents` instead.
""")
def _get_contents(self):
return self._contents
def _set_contents(self, c):
self._contents[:] = c
contents = property(_get_contents, _set_contents, doc="""
The contents of this Columns as a list of `(widget, options)` tuples.
This list may be modified like a normal list and the Columns
widget will update automatically.
.. seealso:: Create new options tuples with the :meth:`options` method
""")
def options(self, width_type=WEIGHT, width_amount=1, box_widget=False):
"""
Return a new options tuple for use in a Pile's .contents list.
This sets an entry's width type: one of the following:
``'pack'``
Call the widget's :meth:`Widget.pack` method to determine how wide
this column should be. *width_amount* is ignored.
``'given'``
Make column exactly width_amount screen-columns wide.
``'weight'``
Allocate the remaining space to this column by using
*width_amount* as a weight value.
:param width_type: ``'pack'``, ``'given'`` or ``'weight'``
:param width_amount: ``None`` for ``'pack'``, a number of screen columns
for ``'given'`` or a weight value (number) for ``'weight'``
:param box_widget: set to `True` if this widget is to be treated as a box
widget when the Columns widget itself is treated as a flow widget.
:type box_widget: bool
"""
if width_type == PACK:
width_amount = None
if width_type not in (PACK, GIVEN, WEIGHT):
raise ColumnsError('invalid width_type: %r' % (width_type,))
return (width_type, width_amount, box_widget)
def _invalidate(self):
self._cache_maxcol = None
self.__super._invalidate()
def set_focus_column(self, num):
"""
Set the column in focus by its index in :attr:`widget_list`.
:param num: index of focus-to-be entry
:type num: int
.. note:: only for backwards compatibility. You may also use the new
standard container property :attr:`focus_position` to set the focus.
"""
self._set_focus_position(num)
def get_focus_column(self):
"""
Return the focus column index.
.. note:: only for backwards compatibility. You may also use the new
standard container property :attr:`focus_position` to get the focus.
"""
return self.focus_position
def set_focus(self, item):
"""
Set the item in focus
.. note:: only for backwards compatibility. You may also use the new
standard container property :attr:`focus_position` to get the focus.
:param item: widget or integer index"""
if isinstance(item, int):
return self._set_focus_position(item)
for i, (w, options) in enumerate(self.contents):
if item == w:
self.focus_position = i
return
raise ValueError("Widget not found in Columns contents: %r" % (item,))
def get_focus(self):
"""
Return the widget in focus, for backwards compatibility. You may
also use the new standard container property .focus to get the
child widget in focus.
"""
if not self.contents:
return None
return self.contents[self.focus_position][0]
focus = property(get_focus,
doc="the child widget in focus or None when Columns is empty")
def _get_focus_position(self):
"""
Return the index of the widget in focus or None if this Columns is
empty.
"""
if not self.widget_list:
raise IndexError("No focus_position, Columns is empty")
return self.contents.focus
def _set_focus_position(self, position):
"""
Set the widget in focus.
position -- index of child widget to be made focus
"""
try:
if position < 0 or position >= len(self.contents):
raise IndexError
except (TypeError, IndexError):
raise IndexError("No Columns child widget at position %s" % (position,))
self.contents.focus = position
focus_position = property(_get_focus_position, _set_focus_position, doc="""
index of child widget in focus. Raises :exc:`IndexError` if read when
Columns is empty, or when set to an invalid index.
""")
focus_col = property(_get_focus_position, _set_focus_position, doc="""
A property for reading and setting the index of the column in
focus.
.. note:: only for backwards compatibility. You may also use the new
standard container property :attr:`focus_position` to get the focus.
""")
def column_widths(self, size, focus=False):
"""
Return a list of column widths.
0 values in the list mean hide corresponding column completely
"""
maxcol = size[0]
# FIXME: get rid of this check and recalculate only when
# a 'pack' widget has been modified.
if maxcol == self._cache_maxcol and not any(
t == PACK for w, (t, n, b) in self.contents):
return self._cache_column_widths
widths = []
weighted = []
shared = maxcol + self.dividechars
for i, (w, (t, width, b)) in enumerate(self.contents):
if t == GIVEN:
static_w = width
elif t == PACK:
# FIXME: should be able to pack with a different
# maxcol value
static_w = w.pack((maxcol,), focus)[0]
else:
static_w = self.min_width
if shared < static_w + self.dividechars and i > self.focus_position:
break
widths.append(static_w)
shared -= static_w + self.dividechars
if t not in (GIVEN, PACK):
weighted.append((width, i))
# drop columns on the left until we fit
for i, w in enumerate(widths):
if shared >= 0:
break
shared += widths[i] + self.dividechars
widths[i] = 0
if weighted and weighted[0][1] == i:
del weighted[0]
if shared:
# divide up the remaining space between weighted cols
weighted.sort()
wtotal = sum(weight for weight, i in weighted)
grow = shared + len(weighted) * self.min_width
for weight, i in weighted:
width = int(float(grow) * weight / wtotal + 0.5)
width = max(self.min_width, width)
widths[i] = width
grow -= width
wtotal -= weight
self._cache_maxcol = maxcol
self._cache_column_widths = widths
return widths
def render(self, size, focus=False):
"""
Render columns and return canvas.
:param size: see :meth:`Widget.render` for details
:param focus: ``True`` if this widget is in focus
:type focus: bool
"""
widths = self.column_widths(size, focus)
box_maxrow = None
if len(size) == 1:
box_maxrow = 1
# two-pass mode to determine maxrow for box columns
for i, (mc, (w, (t, n, b))) in enumerate(zip(widths, self.contents)):
if b:
continue
rows = w.rows((mc,),
focus = focus and self.focus_position == i)
box_maxrow = max(box_maxrow, rows)
l = []
for i, (mc, (w, (t, n, b))) in enumerate(zip(widths, self.contents)):
# if the widget has a width of 0, hide it
if mc <= 0:
continue
if box_maxrow and b:
sub_size = (mc, box_maxrow)
else:
sub_size = (mc,) + size[1:]
canv = w.render(sub_size,
focus = focus and self.focus_position == i)
if i < len(widths) - 1:
mc += self.dividechars
l.append((canv, i, self.focus_position == i, mc))
if not l:
return SolidCanvas(" ", size[0], (size[1:]+(1,))[0])
canv = CanvasJoin(l)
if canv.cols() < size[0]:
canv.pad_trim_left_right(0, size[0] - canv.cols())
return canv
def get_cursor_coords(self, size):
"""Return the cursor coordinates from the focus widget."""
w, (t, n, b) = self.contents[self.focus_position]
if not w.selectable():
return None
if not hasattr(w, 'get_cursor_coords'):
return None
widths = self.column_widths(size)
if len(widths) <= self.focus_position:
return None
colw = widths[self.focus_position]
if len(size) == 1 and b:
coords = w.get_cursor_coords((colw, self.rows(size)))
else:
coords = w.get_cursor_coords((colw,)+size[1:])
if coords is None:
return None
x, y = coords
x += sum([self.dividechars + wc
for wc in widths[:self.focus_position] if wc > 0])
return x, y
def move_cursor_to_coords(self, size, col, row):
"""
Choose a selectable column to focus based on the coords.
see :meth:`Widget.move_cursor_coords` for details
"""
widths = self.column_widths(size)
best = None
x = 0
for i, (width, (w, options)) in enumerate(zip(widths, self.contents)):
end = x + width
if w.selectable():
if col != RIGHT and (col == LEFT or x > col) and best is None:
# no other choice
best = i, x, end, w, options
break
if col != RIGHT and x > col and col-best[2] < x-col:
# choose one on left
break
best = i, x, end, w, options
if col != RIGHT and col < end:
# choose this one
break
x = end + self.dividechars
if best is None:
return False
i, x, end, w, (t, n, b) = best
if hasattr(w, 'move_cursor_to_coords'):
if isinstance(col, int):
move_x = min(max(0, col - x), end - x - 1)
else:
move_x = col
if len(size) == 1 and b:
rval = w.move_cursor_to_coords((end - x, self.rows(size)),
move_x, row)
else:
rval = w.move_cursor_to_coords((end - x,) + size[1:],
move_x, row)
if rval is False:
return False
self.focus_position = i
self.pref_col = col
return True
def mouse_event(self, size, event, button, col, row, focus):
"""
Send event to appropriate column.
May change focus on button 1 press.
"""
widths = self.column_widths(size)
x = 0
for i, (width, (w, (t, n, b))) in enumerate(zip(widths, self.contents)):
if col < x:
return False
w = self.widget_list[i]
end = x + width
if col >= end:
x = end + self.dividechars
continue
focus = focus and self.focus_col == i
if is_mouse_press(event) and button == 1:
if w.selectable():
self.focus_position = i
if not hasattr(w, 'mouse_event'):
return False
if len(size) == 1 and b:
return w.mouse_event((end - x, self.rows(size)), event, button,
col - x, row, focus)
return w.mouse_event((end - x,) + size[1:], event, button,
col - x, row, focus)
return False
def get_pref_col(self, size):
"""Return the pref col from the column in focus."""
widths = self.column_widths(size)
w, (t, n, b) = self.contents[self.focus_position]
if len(widths) <= self.focus_position:
return 0
col = None
cwidth = widths[self.focus_position]
if hasattr(w, 'get_pref_col'):
if len(size) == 1 and b:
col = w.get_pref_col((cwidth, self.rows(size)))
else:
col = w.get_pref_col((cwidth,) + size[1:])
if isinstance(col, int):
col += self.focus_col * self.dividechars
col += sum(widths[:self.focus_position])
if col is None:
col = self.pref_col
if col is None and w.selectable():
col = cwidth // 2
col += self.focus_position * self.dividechars
col += sum(widths[:self.focus_position] )
return col
def rows(self, size, focus=False):
"""
Return the number of rows required by the columns.
This only makes sense if :attr:`widget_list` contains flow widgets.
see :meth:`Widget.rows` for details
"""
widths = self.column_widths(size, focus)
rows = 1
for i, (mc, (w, (t, n, b))) in enumerate(zip(widths, self.contents)):
if b:
continue
rows = max(rows,
w.rows((mc,), focus=focus and self.focus_position == i))
return rows
def keypress(self, size, key):
"""
Pass keypress to the focus column.
:param size: `(maxcol,)` if :attr:`widget_list` contains flow widgets or
`(maxcol, maxrow)` if it contains box widgets.
:type size: int, int
"""
if self.focus_position is None: return key
widths = self.column_widths(size)
if self.focus_position >= len(widths):
return key
i = self.focus_position
mc = widths[i]
w, (t, n, b) = self.contents[i]
if self._command_map[key] not in ('cursor up', 'cursor down',
'cursor page up', 'cursor page down'):
self.pref_col = None
if len(size) == 1 and b:
key = w.keypress((mc, self.rows(size, True)), key)
else:
key = w.keypress((mc,) + size[1:], key)
if self._command_map[key] not in ('cursor left', 'cursor right'):
return key
if self._command_map[key] == 'cursor left':
candidates = list(range(i-1, -1, -1)) # count backwards to 0
else: # key == 'right'
candidates = list(range(i+1, len(self.contents)))
for j in candidates:
if not self.contents[j][0].selectable():
continue
self.focus_position = j
return
return key
def selectable(self):
"""Return the selectable value of the focus column."""
w = self.focus
return w is not None and w.selectable()
def _test():
import doctest
doctest.testmod()
if __name__=='__main__':
_test()
|