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
|
{ $Id: carbonwsstdctrls.pp 15309 2008-06-04 22:12:59Z vincents $}
{
*****************************************************************************
* CocoaWSStdCtrls.pp *
* --------------- *
* *
* *
*****************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit CocoaWSStdCtrls;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
{$include cocoadefines.inc}
interface
uses
// Libs
MacOSAll, CocoaAll, Classes, sysutils,
// LCL
Controls, StdCtrls, Graphics, LCLType, LMessages, LCLProc, LCLMessageGlue, Forms,
// LazUtils
LazUTF8, LazUTF8Classes, TextStrings,
// Widgetset
WSStdCtrls, WSLCLClasses, WSControls, WSProc,
// LCL Cocoa
CocoaWSCommon, CocoaPrivate, CocoaUtils, CocoaGDIObjects, CocoaButtons,
CocoaTables, CocoaTextEdits, CocoaScrollers, Cocoa_Extra;
type
{ TCocoaWSScrollBar }
TCocoaWSScrollBar = class(TWSScrollBar)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
class procedure SetKind(const AScrollBar: TCustomScrollBar; const AIsHorizontal: Boolean); override;
class procedure SetParams(const AScrollBar: TCustomScrollBar); override;
end;
{ TCocoaWSCustomGroupBox }
TCocoaWSCustomGroupBox = class(TWSCustomGroupBox)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
class function GetText(const AWinControl: TWinControl; var AText: String): Boolean; override;
class procedure SetText(const AWinControl: TWinControl; const AText: String); override;
end;
{ TLCLComboboxCallback }
TLCLComboboxCallback = class(TLCLCommonCallback, IComboBoxCallback)
public
isShowPopup: Boolean;
procedure ComboBoxWillPopUp;
procedure ComboBoxWillDismiss;
procedure ComboBoxSelectionDidChange;
procedure ComboBoxSelectionIsChanging;
procedure ComboBoxDrawItem(itemIndex: Integer; ctx: TCocoaContext;
const r: TRect; isSelected: Boolean);
end;
{ TCocoaWSCustomComboBox }
TCocoaWSCustomComboBox = class(TWSCustomComboBox)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
class procedure SetBorderStyle(const AWinControl: TWinControl; const ABorderStyle: TBorderStyle); override;
class function GetDroppedDown(const ACustomComboBox: TCustomComboBox): Boolean; override;
{class function GetSelStart(const ACustomComboBox: TCustomComboBox): integer; override;
class function GetSelLength(const ACustomComboBox: TCustomComboBox): integer; override;}
class function GetItemIndex(const ACustomComboBox: TCustomComboBox): integer; override;
{class function GetMaxLength(const ACustomComboBox: TCustomComboBox): integer; override;
class procedure SetSelStart(const ACustomComboBox: TCustomComboBox; NewStart: integer); override;
class procedure SetSelLength(const ACustomComboBox: TCustomComboBox; NewLength: integer); override;}
class procedure SetItemIndex(const ACustomComboBox: TCustomComboBox; NewIndex: integer); override;
{class procedure SetMaxLength(const ACustomComboBox: TCustomComboBox; NewLength: integer); override;
class procedure SetStyle(const ACustomComboBox: TCustomComboBox; NewStyle: TComboBoxStyle); override;}
class procedure SetDropDownCount(const ACustomComboBox: TCustomComboBox; NewCount: Integer); override;
class function GetItems(const ACustomComboBox: TCustomComboBox): TStrings; override;
{class procedure Sort(const ACustomComboBox: TCustomComboBox; AList: TStrings; IsSorted: boolean); override;}
class function GetItemHeight(const ACustomComboBox: TCustomComboBox): Integer; override;
class procedure SetItemHeight(const ACustomComboBox: TCustomComboBox; const AItemHeight: Integer); override;
class procedure GetPreferredSize(
const AWinControl: TWinControl; var PreferredWidth, PreferredHeight: integer;
WithThemeSpace: Boolean); override;
class procedure SetText(const AWinControl: TWinControl; const AText: String); override;
end;
{ TCocoaWSCustomListBox }
TCocoaWSCustomListBox = class(TWSCustomListBox)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
class function GetIndexAtXY(const ACustomListBox: TCustomListBox; X, Y: integer): integer; override;
class function GetItemIndex(const ACustomListBox: TCustomListBox): integer; override;
class function GetItemRect(const ACustomListBox: TCustomListBox; Index: integer; var ARect: TRect): boolean; override;
class function GetSelCount(const ACustomListBox: TCustomListBox): integer; override;
class function GetSelected(const ACustomListBox: TCustomListBox; const AIndex: integer): boolean; override;
class function GetStrings(const ACustomListBox: TCustomListBox): TStrings; override;
class function GetTopIndex(const ACustomListBox: TCustomListBox): integer; override;
class procedure SelectItem(const ACustomListBox: TCustomListBox; AIndex: integer; ASelected: boolean); override;
class procedure SetBorderStyle(const AWinControl: TWinControl; const ABorderStyle: TBorderStyle); override;
//class procedure SetBorder(const ACustomListBox: TCustomListBox); override;
class procedure SetItemIndex(const ACustomListBox: TCustomListBox; const AIndex: integer); override;
class procedure SetSelectionMode(const ACustomListBox: TCustomListBox; const AExtendedSelect, AMultiSelect: boolean); override;
class procedure SetStyle(const ACustomListBox: TCustomListBox); override;
{class procedure SetSorted(const ACustomListBox: TCustomListBox; AList: TStrings; ASorted: boolean); override;}
class procedure SetTopIndex(const ACustomListBox: TCustomListBox; const NewTopIndex: integer); override;
end;
{ TCocoaWSCustomEdit }
TCocoaWSCustomEdit = class(TWSCustomEdit)
public
class function GetTextField(AWinControl: TWinControl): TCocoaTextField;
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
// WSControl functions
class procedure SetColor(const AWinControl: TWinControl); override;
class procedure SetBorderStyle(const AWinControl: TWinControl; const ABorderStyle: TBorderStyle); override;
// WSEdit functions
class function GetSelStart(const ACustomEdit: TCustomEdit): integer; override;
class function GetSelLength(const ACustomEdit: TCustomEdit): integer; override;
class procedure SetAlignment(const ACustomEdit: TCustomEdit; const NewAlignment: TAlignment); override;
{class procedure SetCharCase(const ACustomEdit: TCustomEdit; NewCase: TEditCharCase); override;
class procedure SetEchoMode(const ACustomEdit: TCustomEdit; NewMode: TEchoMode); override;}
class procedure SetMaxLength(const ACustomEdit: TCustomEdit; NewLength: integer); override;
class procedure SetPasswordChar(const ACustomEdit: TCustomEdit; NewChar: char); override;
class procedure SetReadOnly(const ACustomEdit: TCustomEdit; NewReadOnly: boolean); override;
class procedure SetSelStart(const ACustomEdit: TCustomEdit; NewStart: integer); override;
class procedure SetSelLength(const ACustomEdit: TCustomEdit; NewLength: integer); override;
class procedure Cut(const ACustomEdit: TCustomEdit); override;
class procedure Copy(const ACustomEdit: TCustomEdit); override;
class procedure Paste(const ACustomEdit: TCustomEdit); override;
class procedure Undo(const ACustomEdit: TCustomEdit); override;
class procedure SetText(const AWinControl: TWinControl; const AText: String); override;
end;
{ TCocoaMemoStrings }
TCocoaMemoStrings = class(TCustomMemoStrings)
private
FTextView: TCocoaTextView;
public
class procedure GetLineStart(const s: AnsiString; LineIndex: Integer; var Offset, LinesSkipped: Integer);
class function GetLinesCount(const s: AnsiString): Integer;
protected
function GetTextStr: string; override;
procedure SetTextStr(const Value: string); override;
function GetCount: Integer; override;
function Get(Index: Integer): string; override;
public
constructor Create(ATextView: TCocoaTextView);
procedure Clear; override;
procedure Delete(Index: Integer); override;
procedure Insert(Index: Integer; const S: string); override;
procedure LoadFromFile(const FileName: string); override;
procedure SaveToFile(const FileName: string); override;
end;
{ TCocoaWSCustomMemo }
TCocoaWSCustomMemo = class(TWSCustomMemo)
public
class function GetTextView(AWinControl: TWinControl): TCocoaTextView;
class function GetScrollView(AWinControl: TWinControl): TCocoaScrollView;
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
// WSControl functions
class procedure SetColor(const AWinControl: TWinControl); override;
class procedure SetSelStart(const ACustomEdit: TCustomEdit; NewStart: integer); override;
class procedure SetSelLength(const ACustomEdit: TCustomEdit; NewLength: integer); override;
class procedure SetBorderStyle(const AWinControl: TWinControl; const ABorderStyle: TBorderStyle); override;
// WSEdit functions
//class function GetCanUndo(const ACustomEdit: TCustomEdit): Boolean; override;
class function GetCaretPos(const ACustomEdit: TCustomEdit): TPoint; override;
class function GetSelStart(const ACustomEdit: TCustomEdit): integer; override;
class function GetSelLength(const ACustomEdit: TCustomEdit): integer; override;
class procedure SetAlignment(const ACustomEdit: TCustomEdit; const NewAlignment: TAlignment); override;
// WSMemo functions
class function GetStrings(const ACustomMemo: TCustomMemo): TStrings; override;
class procedure AppendText(const ACustomMemo: TCustomMemo; const AText: string); override;
class procedure SetScrollbars(const ACustomMemo: TCustomMemo; const NewScrollbars: TScrollStyle); override;
class procedure SetWantReturns(const ACustomMemo: TCustomMemo; const NewWantReturns: boolean); override;
class procedure SetWantTabs(const ACustomMemo: TCustomMemo; const NewWantTabs: boolean); override;
class procedure SetWordWrap(const ACustomMemo: TCustomMemo; const NewWordWrap: boolean); override;
class procedure SetReadOnly(const ACustomEdit: TCustomEdit; NewReadOnly: boolean); override;
class procedure SetText(const AWinControl: TWinControl; const AText: String); override;
class function GetText(const AWinControl: TWinControl; var AText: String): Boolean; override;
end;
{ TLCLButtonCallback }
TLCLButtonCallback = class(TLCLCommonCallback, IButtonCallback)
public
procedure ButtonClick; virtual;
procedure Draw(ControlContext: NSGraphicsContext; const bounds, dirty: NSRect); override;
procedure GetAllowMixedState(var allowed: Boolean); virtual;
end;
TLCLButtonCallBackClass = class of TLCLButtonCallBack;
{ TLCLListBoxCallback }
TLCLListBoxCallback = class(TLCLCommonCallback, IListViewCallBack)
protected
function AllocStrings(ATable: NSTableView): TCocoaStringList; virtual;
public
listview : TCocoaTableListView;
strings : TCocoaStringList;
constructor CreateWithView(AOwner: TCocoaTableListView; ATarget: TWinControl);
destructor Destroy; override;
function ItemsCount: Integer; virtual;
function GetItemTextAt(ARow, ACol: Integer; var Text: String): Boolean; virtual;
function GetItemCheckedAt(ARow, ACol: Integer; var isChecked: Integer): Boolean; virtual;
function GetItemImageAt(ARow, ACol: Integer; var imgIdx: Integer): Boolean; virtual;
function GetImageFromIndex(imgIdx: Integer): NSImage; virtual;
procedure SetItemTextAt(ARow, ACol: Integer; const Text: String); virtual;
procedure SetItemCheckedAt(ARow, ACol: Integer; isChecked: Integer); virtual;
procedure tableSelectionChange(ARow: Integer; Added, Removed: NSIndexSet); virtual;
procedure ColumnClicked(ACol: Integer); virtual;
procedure DrawRow(rowidx: Integer; ctx: TCocoaContext; const r: TRect; state: TOwnerDrawState); virtual;
procedure GetRowHeight(rowidx: integer; var h: Integer); virtual;
end;
TLCLListBoxCallBackClass = class of TLCLListBoxCallBack;
{ TCocoaWSButton }
TCocoaWSButton = class(TWSButton)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
class procedure SetDefault(const AButton: TCustomButton; ADefault: Boolean); override;
class procedure SetText(const AWinControl: TWinControl; const AText: String); override;
class function GetText(const AWinControl: TWinControl; var AText: String): Boolean; override;
class function GetTextLen(const AWinControl: TWinControl; var ALength: Integer): Boolean; override;
class procedure SetFont(const AWinControl: TWinControl; const AFont: TFont); override;
end;
{ TLCLCheckBoxCallback }
TLCLCheckBoxCallback = class(TLCLButtonCallBack)
public
procedure ButtonClick; override;
procedure GetAllowMixedState(var allowed: Boolean); override;
end;
{ TCocoaWSCustomCheckBox }
TCocoaWSCustomCheckBox = class(TWSCustomCheckBox)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
class function RetrieveState(const ACustomCheckBox: TCustomCheckBox): TCheckBoxState; override;
class procedure SetState(const ACustomCheckBox: TCustomCheckBox; const NewState: TCheckBoxState); override;
//
class procedure GetPreferredSize(const AWinControl: TWinControl; var PreferredWidth, PreferredHeight: integer; {%H-}WithThemeSpace: Boolean); override;
class procedure SetText(const AWinControl: TWinControl; const AText: String); override;
class function GetText(const AWinControl: TWinControl; var AText: String): Boolean; override;
class function GetTextLen(const AWinControl: TWinControl; var ALength: Integer): Boolean; override;
end;
{ TCocoaWSToggleBox }
TCocoaWSToggleBox = class(TWSToggleBox)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
end;
{ TLCLRadioButtonCallback }
TLCLRadioButtonCallback = class(TLCLCheckBoxCallback)
public
procedure ButtonClick; override;
end;
{ TCocoaWSRadioButton }
TCocoaWSRadioButton = class(TWSRadioButton)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
class procedure SetState(const ACustomCheckBox: TCustomCheckBox; const NewState: TCheckBoxState); override;
end;
{ TCocoaWSCustomStaticText }
TCocoaWSCustomStaticText = class(TWSCustomStaticText)
private
protected
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
// class procedure SetAlignment(const ACustomStaticText: TCustomStaticText; const NewAlignment: TAlignment); override;
end;
function AllocTextView(ATarget: TWinControl; const AParams: TCreateParams; fieldEditor: Boolean): NSTextView;
function AllocButton(const ATarget: TWinControl; const ACallBackClass: TLCLButtonCallBackClass; const AParams: TCreateParams; btnBezel: NSBezelStyle; btnType: NSButtonType): TCocoaButton;
function AllocTextField(ATarget: TWinControl; const AParams: TCreateParams): TCocoaTextField;
function AllocSecureTextField(ATarget: TWinControl; const AParams: TCreateParams): TCocoaSecureTextField;
function GetListBox(AWinControl: TWinControl): TCocoaTableListView;
procedure ListBoxSetStyle(list: TCocoaTableListView; AStyle: TListBoxStyle);
procedure TextViewSetWordWrap(txt: NSTextView; lScroll: NSScrollView; NewWordWrap: Boolean);
function AlignmentLCLToCocoa(al: TAlignment): NSTextAlignment;
procedure TextViewSetAllignment(txt: NSTextView; align: TAlignment);
procedure TextFieldSetAllignment(txt: NSTextField; align: TAlignment);
procedure TextFieldSetBorderStyle(txt: NSTextField; astyle: TBorderStyle);
procedure RadioButtonSwitchSiblings(checkedRadio: NSButton);
procedure ButtonSetState(btn: NSButton; NewState: TCheckBoxState;
SkipChangeEvent: Boolean = true);
procedure ScrollViewSetScrollStyles(AScroll: TCocoaScrollView; AStyles: TScrollStyle);
function ComboBoxStyleIsReadOnly(AStyle: TComboBoxStyle): Boolean;
function ComboBoxIsReadOnly(cmb: TCustomComboBox): Boolean;
function ComboBoxIsOwnerDrawn(AStyle: TComboBoxStyle): Boolean;
function ComboBoxIsVariable(AStyle: TComboBoxStyle): Boolean;
procedure ComboBoxSetBorderStyle(box: NSComboBox; astyle: TBorderStyle);
// Sets the control text and then calls controls callback (if any)
// with TextChange (CM_TEXTCHANGED) event.
// Cocoa control do not fire a notification, if text is changed programmatically
// LCL expects a change notification in either way. (by software or by user)
procedure ControlSetTextWithChangeEvent(ctrl: NSControl; const text: string);
implementation
uses
CocoaInt;
const
VerticalScrollerVisible: array[TScrollStyle] of boolean = (
{ssNone } false,
{ssHorizontal } false,
{ssVertical } true,
{ssBoth } true,
{ssAutoHorizontal} false,
{ssAutoVertical } true,
{ssAutoBoth } true
);
HorizontalScrollerVisible: array[TScrollStyle] of boolean = (
{ssNone } false,
{ssHorizontal } true,
{ssVertical } false,
{ssBoth } true,
{ssAutoHorizontal} true,
{ssAutoVertical } false,
{ssAutoBoth } true
);
ScrollerAutoHide: array[TScrollStyle] of boolean = (
{ssNone } false,
{ssHorizontal } false,
{ssVertical } false,
{ssBoth } false,
{ssAutoHorizontal} true,
{ssAutoVertical } true,
{ssAutoBoth } true
);
function AllocButton(const ATarget: TWinControl; const ACallBackClass: TLCLButtonCallBackClass; const AParams: TCreateParams; btnBezel: NSBezelStyle; btnType: NSButtonType): TCocoaButton;
begin
Result := TCocoaButton.alloc.lclInitWithCreateParams(AParams);
if Assigned(Result) then
begin
TCocoaButton(Result).callback := ACallBackClass.Create(Result, ATarget);
Result.setTitle(ControlTitleToNSStr(AParams.Caption));
if btnBezel <> 0 then
Result.setBezelStyle(btnBezel);
Result.setButtonType(btnType);
end;
end;
function AllocTextView(ATarget: TWinControl; const AParams: TCreateParams; fieldEditor: Boolean): NSTextView;
begin
Result := TCocoaTextView.alloc.lclInitWithCreateParams(AParams);
if Assigned(Result) then
begin
TCocoaTextView(Result).callback := TLCLCommonCallback.Create(Result, ATarget);
end;
end;
function AllocTextField(ATarget: TWinControl; const AParams: TCreateParams): TCocoaTextField;
begin
Result := TCocoaTextField.alloc.lclInitWithCreateParams(AParams);
if Assigned(Result) then
begin
Result.setFont(NSFont.systemFontOfSize(NSFont.systemFontSize));
Result.callback := TLCLCommonCallback.Create(Result, ATarget);
SetNSControlValue(Result, AParams.Caption);
end;
end;
function AllocSecureTextField(ATarget: TWinControl; const AParams: TCreateParams): TCocoaSecureTextField;
begin
Result := TCocoaSecureTextField.alloc.lclInitWithCreateParams(AParams);
if Assigned(Result) then
begin
Result.setFont(NSFont.systemFontOfSize(NSFont.systemFontSize));
TCocoaSecureTextField(Result).callback := TLCLCommonCallback.Create(Result, ATarget);
SetNSText(Result.currentEditor, AParams.Caption);
end;
end;
procedure TextFieldSetBorderStyle(txt: NSTextField; astyle: TBorderStyle);
begin
if not Assigned(txt) then Exit;
{$ifdef BOOLFIX}
txt.setBezeled_(Ord(astyle <> bsNone));
{$else}
txt.setBezeled(astyle <> bsNone);
{$endif}
end;
procedure RadioButtonSwitchSiblings(checkedRadio: NSButton);
var
SubView : NSView;
begin
if not Assigned(checkedRadio) then Exit;
for SubView in checkedRadio.superView.subviews do
if (SubView <> checkedRadio) and (SubView.lclGetTarget is TRadioButton) then
begin
NSButton(SubView).setState(NSOffState);
end;
end;
procedure ButtonSetState(btn: NSButton; NewState: TCheckBoxState;
SkipChangeEvent: Boolean = true);
const
buttonState: array [TcheckBoxState] of NSInteger =
(NSOffState, NSOnState, NSMixedState);
var
cb : IButtonCallback;
begin
if NewState = cbGrayed then
{$ifdef BOOLFIX}
btn.setAllowsMixedState_(Ord(true));
{$else}
btn.setAllowsMixedState(true);
{$endif}
if SkipChangeEvent and (btn.isKindOfClass(TCocoaButton)) then
begin
//todo: This place needs a cleanup!
// Assigning state, while having callback removed
// TCocoaButton.setState is causing OnChange event, if callback is not nil
cb := TCocoaButton(btn).callback;
TCocoaButton(btn).callback := nil;
btn.setState(buttonState[NewState]);
TCocoaButton(btn).callback := cb;
end
else
btn.setState(buttonState[NewState]);
end;
procedure ScrollViewSetScrollStyles(AScroll: TCocoaScrollView; AStyles: TScrollStyle);
begin
AScroll.setHasVerticalScroller(VerticalScrollerVisible[AStyles]);
AScroll.setHasHorizontalScroller(HorizontalScrollerVisible[AStyles]);
AScroll.setAutohidesScrollers(ScrollerAutoHide[AStyles]);
end;
function ComboBoxStyleIsReadOnly(AStyle: TComboBoxStyle): Boolean;
begin
Result := AStyle in [csDropDownList, csOwnerDrawFixed, csOwnerDrawVariable];
end;
function ComboBoxIsReadOnly(cmb: TCustomComboBox): Boolean;
begin
Result := Assigned(cmb)
and (ComboBoxStyleIsReadOnly(cmb.Style) or cmb.ReadOnly);
end;
function ComboBoxIsOwnerDrawn(AStyle: TComboBoxStyle): Boolean;
begin
Result := AStyle in [csOwnerDrawFixed, csOwnerDrawVariable,
csOwnerDrawEditableFixed, csOwnerDrawEditableVariable];
end;
function ComboBoxIsVariable(AStyle: TComboBoxStyle): Boolean;
begin
Result := AStyle in [csOwnerDrawVariable, csOwnerDrawEditableVariable];
end;
procedure ComboBoxSetBorderStyle(box: NSComboBox; astyle: TBorderStyle);
begin
{$IFDEF BOOLFIX}
box.setBezeled_(Ord(astyle <> bsNone));
{$else}
box.setBezeled(astyle <> bsNone);
{$endif}
end;
{ TLCLRadioButtonCallback }
procedure TLCLRadioButtonCallback.ButtonClick;
var
SubView: NSView;
begin
if not Owner.lclIsEnabled() then Exit;
if NSButton(Owner).state = NSOnState then
RadioButtonSwitchSiblings(NSButton(Owner));
inherited ButtonClick;
end;
{ TLCLButtonCallback }
procedure TLCLButtonCallback.ButtonClick;
begin
if not Owner.lclIsEnabled() then Exit;
SendSimpleMessage(Target, LM_CLICKED);
end;
procedure TLCLButtonCallback.Draw(ControlContext: NSGraphicsContext;
const bounds, dirty: NSRect);
var
PS: PPaintStruct;
nsr:NSRect;
ctx: TCocoaContext;
begin
// todo: think more about draw call while previous draw still active
ctx := TCocoaContext.Create(ControlContext);
ctx.isControlDC := True;
try
nsr:=dirty;
nsr.origin.y:=bounds.size.height-dirty.origin.y-dirty.size.height;
New(PS);
try
FillChar(PS^, SizeOf(TPaintStruct), 0);
PS^.hdc := HDC(ctx);
PS^.rcPaint := NSRectToRect(nsr);
LCLSendPaintMsg(Target, HDC(ctx), PS);
finally
Dispose(PS);
end;
finally
FreeAndNil(ctx);
end;
end;
procedure TLCLButtonCallback.GetAllowMixedState(var allowed: Boolean);
begin
end;
{ TLCLListBoxCallback }
function TLCLListBoxCallback.AllocStrings(ATable: NSTableView
): TCocoaStringList;
begin
Result := TCocoaStringList.Create(ATable);
end;
constructor TLCLListBoxCallback.CreateWithView(AOwner: TCocoaTableListView;
ATarget: TWinControl);
begin
Create(AOwner, ATarget);
listview := AOwner;
strings := AllocStrings(AOwner);
end;
destructor TLCLListBoxCallback.Destroy;
begin
// "strings" are released with FreeStrings call
inherited Destroy;
end;
function TLCLListBoxCallback.ItemsCount: Integer;
begin
Result := strings.Count;
end;
function TLCLListBoxCallback.GetItemTextAt(ARow, ACol: Integer; var Text: String): Boolean;
begin
Result := (ARow>=0) and (ARow < strings.Count);
if Result then Text := strings[ARow];
end;
function TLCLListBoxCallback.GetItemCheckedAt(ARow, ACol: Integer;
var isChecked: Integer): Boolean;
begin
Result := false;
end;
function TLCLListBoxCallback.GetItemImageAt(ARow, ACol: Integer;
var imgIdx: Integer): Boolean;
begin
Result := false;
end;
function TLCLListBoxCallback.GetImageFromIndex(imgIdx: Integer): NSImage;
begin
Result := nil;
end;
procedure TLCLListBoxCallback.SetItemTextAt(ARow, ACol: Integer;
const Text: String);
begin
// todo:
end;
procedure TLCLListBoxCallback.SetItemCheckedAt(ARow, ACol: Integer;
isChecked: Integer);
begin
// do nothing
end;
procedure TLCLListBoxCallback.tableSelectionChange(ARow: Integer; Added,
Removed: NSIndexSet);
begin
// do not notify about selection changes while clearing
if Assigned(strings) and (strings.isClearing) then Exit;
SendSimpleMessage(Target, LM_SELCHANGE);
end;
procedure TLCLListBoxCallback.ColumnClicked(ACol: Integer);
begin
// not needed
end;
procedure TLCLListBoxCallback.DrawRow(rowidx: Integer; ctx: TCocoaContext;
const r: TRect; state: TOwnerDrawState);
var
DrawStruct: TDrawListItemStruct;
begin
if not listview.isOwnerDraw then Exit;
DrawStruct.ItemState := state;
DrawStruct.Area := r;
DrawStruct.DC := HDC(ctx);
DrawStruct.ItemID := rowIdx;
LCLSendDrawListItemMsg(Target, @DrawStruct);
end;
procedure TLCLListBoxCallback.GetRowHeight(rowidx: integer; var h: Integer);
begin
if TCustomListBox(Target).Style = lbOwnerDrawVariable then
TCustomListBox(Target).MeasureItem(rowidx, h);
end;
{ TLCLCheckBoxCallback }
procedure TLCLCheckBoxCallback.ButtonClick;
begin
inherited;
if not Owner.lclIsEnabled() then Exit;
SendSimpleMessage(Target, LM_CHANGED);
// todo: win32 has something about dbcheckbox handling here. so maybe we need to handle it special too
end;
procedure TLCLCheckBoxCallback.GetAllowMixedState(var allowed: Boolean);
begin
allowed := TCustomCheckBox(Target).AllowGrayed;
end;
{ TLCLComboboxCallback }
procedure TLCLComboboxCallback.ComboBoxWillPopUp;
begin
isShowPopup := true;
LCLSendDropDownMsg(Target);
end;
procedure TLCLComboboxCallback.ComboBoxWillDismiss;
begin
LCLSendCloseUpMsg(Target);
isShowPopup := false;
end;
procedure TLCLComboboxCallback.ComboBoxSelectionDidChange;
begin
SendSimpleMessage(Target, LM_SELCHANGE);
end;
procedure TLCLComboboxCallback.ComboBoxSelectionIsChanging;
begin
end;
procedure TLCLComboboxCallback.ComboBoxDrawItem(itemIndex: Integer;
ctx: TCocoaContext; const r: TRect; isSelected: Boolean);
var
itemstruct: TDrawListItemStruct;
begin
itemstruct.ItemID := UINT(itemIndex);
itemstruct.Area := r;
itemstruct.DC := HDC(ctx);
itemstruct.ItemState := [];
if isSelected then Include(itemstruct.ItemState, odSelected);
// we don't distingiush at the moment
if isSelected then Include(itemstruct.ItemState, odFocused);
LCLSendDrawListItemMsg(Target, @itemstruct);
end;
{ TCocoaWSButton }
{------------------------------------------------------------------------------
Method: TCocoaWSButton.CreateHandle
Params: AWinControl - LCL control
AParams - Creation parameters
Returns: Handle to the control in Cocoa interface
Creates new button control in Cocoa interface with the specified parameters
------------------------------------------------------------------------------}
class function TCocoaWSButton.CreateHandle(const AWinControl: TWinControl;
const AParams: TCreateParams): TLCLIntfHandle;
var
btn: TCocoaButton;
begin
btn := AllocButton(AWinControl, TLCLButtonCallback, AParams, NSRoundedBezelStyle, NSMomentaryPushInButton);
btn.smallHeight := PUSHBTN_SMALL_HEIGHT;
btn.miniHeight := PUSHBTN_MINI_HEIGHT;
btn.adjustFontToControlSize:=true;
Result := TLCLIntfHandle(btn);
end;
{------------------------------------------------------------------------------
Method: TCocoaWSButton.SetDefault
Params: AButton - LCL button control
ADefault
Sets button default indication in Cocoa interface
------------------------------------------------------------------------------}
class procedure TCocoaWSButton.SetDefault(const AButton: TCustomButton; ADefault: Boolean);
var
cf: NSString;
const
DefEq: array [Boolean] of String = (#0, #13);
begin
if not AButton.HandleAllocated then
Exit;
cf := NSStringUtf8(DefEq[ADefault]);
NSButton(AButton.Handle).setKeyEquivalent(cf);
cf.release;
end;
class procedure TCocoaWSButton.SetText(const AWinControl: TWinControl; const AText: String);
var
btn : NSButton;
begin
btn := NSButton(AWinControl.Handle);
btn.setTitle(ControlTitleToNSStr(AText));
end;
class function TCocoaWSButton.GetText(const AWinControl: TWinControl;
var AText: String): Boolean;
begin
// The text is static, so let the LCL fallback to FCaption
Result := false;
end;
class function TCocoaWSButton.GetTextLen(const AWinControl: TWinControl;
var ALength: Integer): Boolean;
begin
// The text is static, so let the LCL fallback to FCaption
Result := false;
end;
class procedure TCocoaWSButton.SetFont(const AWinControl: TWinControl;
const AFont: TFont);
begin
if not (AWinControl.HandleAllocated) then Exit;
TCocoaWSWinControl.SetFont(AWinControl, AFont);
TCocoaButton(AWinControl.Handle).adjustFontToControlSize := (AFont.Name = 'default')
and (AFont.Size = 0);
end;
{ TCocoaWSCustomCheckBox }
{------------------------------------------------------------------------------
Method: TCocoaWSCustomCheckBox.CreateHandle
Params: AWinControl - LCL control
AParams - Creation parameters
Returns: Handle to the control in Cocoa interface
Creates new check box in Cocoa interface with the specified parameters
------------------------------------------------------------------------------}
class function TCocoaWSCustomCheckBox.CreateHandle(const AWinControl: TWinControl;
const AParams: TCreateParams): TLCLIntfHandle;
var
btn: TCocoaButton;
cb: IButtonCallback;
begin
btn := AllocButton(AWinControl, TLCLCheckBoxCallBack, AParams, 0, NSSwitchButton);
// changes in AllowGrayed are never sent to WS!
// so it should be checked at create time (and at SetNextState?)
if TCustomCheckBox(AWinControl).AllowGrayed then
{$ifdef BOOLFIX}
NSButton(btn).setAllowsMixedState_(Ord(true));
{$else}
NSButton(btn).setAllowsMixedState(true);
{$endif}
;
Result := TLCLIntfHandle(btn);
end;
{------------------------------------------------------------------------------
Method: TCocoaWSCustomCheckBox.RetrieveState
Params: ACustomCheckBox - LCL custom check box
Returns: State of check box
Retrieves the state of check box in Cocoa interface
------------------------------------------------------------------------------}
class function TCocoaWSCustomCheckBox.RetrieveState(const ACustomCheckBox: TCustomCheckBox): TCheckBoxState;
var
state : NSInteger;
begin
Result := cbUnchecked;
if not ACustomCheckBox.HandleAllocated then
Exit;
state := NSButton(ACustomCheckBox.Handle).state;
case state of
NSOnState: Result := cbChecked;
NSMixedState: Result := cbGrayed;
end;
end;
{------------------------------------------------------------------------------
Method: TCocoaWSCustomCheckBox.SetState
Params: ACustomCheckBox - LCL custom check box
NewState - New state of check box
Sets the new state of check box in Cocoa interface
------------------------------------------------------------------------------}
class procedure TCocoaWSCustomCheckBox.SetState(
const ACustomCheckBox: TCustomCheckBox; const NewState: TCheckBoxState);
const
buttonState: array [TcheckBoxState] of NSInteger = (NSOffState, NSOnState, NSMixedState);
begin
if not ACustomCheckBox.HandleAllocated then Exit;
ButtonSetState(NSButton(ACustomCheckBox.Handle), NewState);
end;
class procedure TCocoaWSCustomCheckBox.GetPreferredSize(
const AWinControl: TWinControl; var PreferredWidth, PreferredHeight: integer;
WithThemeSpace: Boolean);
var
lButton: NSButton;
lOldSize: NSSize;
begin
if not AWinControl.HandleAllocated then Exit;
lButton := NSButton(AWinControl.Handle);
lOldSize := lButton.bounds.size;
lButton.sizeToFit();
PreferredWidth := round(lButton.bounds.size.width);
PreferredHeight := round(lButton.bounds.size.height);
//lButton.setBoundsSize(lOldSize); This causes problems in SetText
end;
class procedure TCocoaWSCustomCheckBox.SetText(const AWinControl: TWinControl;
const AText: String);
begin
TCocoaWSButton.SetText(AWinControl, AText);
end;
class function TCocoaWSCustomCheckBox.GetText(const AWinControl: TWinControl;
var AText: String): Boolean;
begin
Result := TCocoaWSButton.GetText(AWinControl, AText);
end;
class function TCocoaWSCustomCheckBox.GetTextLen(
const AWinControl: TWinControl; var ALength: Integer): Boolean;
begin
Result := TCocoaWSButton.GetTextLen(AWinControl, ALength);
end;
{ TCocoaWSRadioButton }
class function TCocoaWSRadioButton.CreateHandle(const AWinControl: TWinControl;
const AParams: TCreateParams): TLCLIntfHandle;
var
btn: TCocoaButton;
begin
btn := AllocButton(AWinControl, TLCLRadioButtonCallback, AParams, 0, NSRadioButton);
Result := TLCLIntfHandle(btn);
end;
class procedure TCocoaWSRadioButton.SetState(
const ACustomCheckBox: TCustomCheckBox; const NewState: TCheckBoxState);
var
btn : NSButton;
begin
if not ACustomCheckBox.HandleAllocated then Exit;
btn := NSButton(ACustomCheckBox.Handle);
if NewState = cbChecked then
RadioButtonSwitchSiblings(btn);
ButtonSetState(btn, NewState);
end;
{ TCocoaWSCustomStaticText }
class function TCocoaWSCustomStaticText.CreateHandle(const AWinControl: TWinControl;
const AParams: TCreateParams): TLCLIntfHandle;
var
field: NSTextField;
begin
field := NSTextField(AllocTextField(AWinControl, AParams));
{$ifdef BOOLFIX}
field.setBezeled_(Ord(False));
field.setDrawsBackground_(Ord(False));
field.setEditable_(Ord(False));
field.setSelectable_(Ord(False));
{$else}
field.setBezeled(False);
field.setDrawsBackground(False);
field.setEditable(False);
field.setSelectable(False);
{$endif}
Result:=TLCLIntfHandle(field);
end;
{ TCocoaWSCustomEdit }
class function TCocoaWSCustomEdit.GetTextField(AWinControl: TWinControl): TCocoaTextField;
begin
if not Assigned(AWinControl) or (not AWinControl.HandleAllocated) or (AWinControl.Handle=0) then
begin
Exit(nil);
end;
if AWinControl is TCustomMemo then
begin
//raise Exception.Create('[TCocoaWSCustomEdit.GetTextField] Called for TMemo, but TMemo has no text field');
Exit(nil);
end;
Result := TCocoaTextField(AWinControl.Handle);
end;
class function TCocoaWSCustomEdit.CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle;
var
field : NSTextField;
cell : NSTextFieldCell;
begin
if TCustomEdit(AWinControl).PasswordChar=#0
then field:=NSTextField(AllocTextField(AWinControl, AParams))
else field:=NSTextField(AllocSecureTextField(AWinControl, AParams));
if (field.respondsToSelector(ObjCSelector('cell'))) and Assigned(field.cell) then
begin
cell := NSTextFieldCell(field.cell);
cell.setWraps(false);
cell.setScrollable(true);
cell.setUsesSingleLineMode(true);
end;
TextFieldSetAllignment(field, TCustomEdit(AWinControl).Alignment);
TextFieldSetBorderStyle(field, TCustomEdit(AWinControl).BorderStyle);
UpdateFocusRing(field, TCustomEdit(AWinControl).BorderStyle);
Result:=TLCLIntfHandle(field);
end;
class procedure TCocoaWSCustomEdit.SetColor(const AWinControl: TWinControl);
var
field: TCocoaTextField;
begin
field := GetTextField(AWinControl);
if not Assigned(field) then Exit;
if (AWinControl.Color = clDefault) or (AWinControl.Color = clWindow) or (AWinControl.Color = clBackground) then
field.setBackgroundColor( NSColor.textBackgroundColor )
else
field.setBackgroundColor( ColorToNSColor(ColorToRGB(AWinControl.Color)));
end;
class procedure TCocoaWSCustomEdit.SetBorderStyle(
const AWinControl: TWinControl; const ABorderStyle: TBorderStyle);
var
field : TCocoaTextField;
begin
field := GetTextField(AWinControl);
if not Assigned(field) then Exit;
field.setBordered_( ObjCBool(ABorderStyle <> bsNone) );
field.setBezeled_( ObjCBool(ABorderStyle <> bsNone) );
UpdateFocusRing(field, ABorderStyle);
end;
class function TCocoaWSCustomEdit.GetSelStart(const ACustomEdit: TCustomEdit): integer;
var
field : TCocoaTextField;
txt : NSText;
begin
Result:=0;
field := GetTextField(ACustomEdit);
if not Assigned(field) then Exit;
txt:=NSText(field.currentEditor);
if not Assigned(txt) then Exit;
Result:=txt.selectedRange.location;
end;
class function TCocoaWSCustomEdit.GetSelLength(const ACustomEdit: TCustomEdit): integer;
var
field : TCocoaTextField;
txt : NSText;
begin
Result:=0;
field := GetTextField(ACustomEdit);
if not Assigned(field) then Exit;
txt:=NSText(field.currentEditor);
if not Assigned(txt) then Exit;
Result:=txt.selectedRange.length;
end;
class procedure TCocoaWSCustomEdit.SetAlignment(const ACustomEdit: TCustomEdit;
const NewAlignment: TAlignment);
var
field: TCocoaTextField;
begin
field := GetTextField(ACustomEdit);
if not Assigned(field) then Exit;
TextFieldSetAllignment(field, NewAlignment);
end;
class procedure TCocoaWSCustomEdit.SetMaxLength(const ACustomEdit: TCustomEdit;
NewLength: integer);
var
field: NSTextField;
begin
if not (ACustomEdit.HandleAllocated) then Exit;
field := NSTextField(ACustomEdit.Handle);
if not Assigned(field) then Exit;
if NSObject(field).respondsToSelector( ObjCSelector('lclSetMaxLength:') ) then
{%H-}NSTextField_LCLExt(field).lclSetMaxLength(NewLength);
end;
class procedure TCocoaWSCustomEdit.SetPasswordChar(const ACustomEdit: TCustomEdit; NewChar: char);
begin
if (NewChar<>#0) xor TCocoaTextField(ACustomEdit.Handle).isKindOfClass_(NSSecureTextField) then
RecreateWnd(ACustomEdit);
end;
class procedure TCocoaWSCustomEdit.SetReadOnly(const ACustomEdit: TCustomEdit; NewReadOnly: boolean);
var
lHandle: TCocoaTextField;
w : NSWindow;
t : NSText;
isFocused: Boolean;
r : Boolean;
b : Boolean;
rsp : NSResponder;
ed : TCocoaFieldEditor;
begin
lHandle := GetTextField(ACustomEdit);
if not Assigned(lHandle) then Exit;
ed := nil; //if lHandle is "focused" then ed would be <> nil
w := lHandle.window;
if not Assigned(w) then t := nil
else begin
rsp := w.firstResponder;
if (Assigned(rsp)) and (rsp.isKindOfClass(TCocoaFieldEditor)) then
begin
ed := TCocoaFieldEditor(rsp);
if (NSObject(ed.delegate) = lHandle) then
begin
ed.retain;
// the hack is needed to prevent infinite loop
// on switching editable (ReadOnly) status.
// without prevention of Editor focusing, AppKit goes into an infinite loop:
// AppKit`-[_NSKeyboardFocusClipView removeFromSuperview] + 55
// AppKit`-[NSWindow endEditingFor:] + 429
// AppKit`-[NSView removeFromSuperview] + 78
// AppKit`-[_NSKeyboardFocusClipView removeFromSuperview] + 55
// AppKit`-[NSWindow endEditingFor:] + 429
// AppKit`-[NSView removeFromSuperview] + 78
// AppKit`-[_NSKeyboardFocusClipView removeFromSuperview] + 55
ed.goingReadOnly := true;
end
else
ed := nil; // someone else is focused
end;
end;
lHandle.setEditable_(ObjCBool(not NewReadOnly));
lHandle.setSelectable_(1); // allow to select read-only text (LCL compatible)
if Assigned(ed) then begin
ed.goingReadOnly := false;
ed.release;
end;
end;
class procedure TCocoaWSCustomEdit.SetSelStart(const ACustomEdit: TCustomEdit; NewStart: integer);
var
lHandle: TCocoaTextField;
curEditor: NSText;
lRange: NSRange;
begin
lHandle := GetTextField(ACustomEdit);
if not Assigned(lHandle) then Exit;
curEditor := NSText(lHandle.currentEditor);
if not Assigned(curEditor) then Exit;
lRange := curEditor.selectedRange;
lRange.location := NewStart;
curEditor.setSelectedRange(lRange);
end;
class procedure TCocoaWSCustomEdit.SetSelLength(const ACustomEdit: TCustomEdit; NewLength: integer);
var
lHandle: TCocoaTextField;
curEditor: NSText;
lRange: NSRange;
begin
lHandle := GetTextField(ACustomEdit);
if not Assigned(lHandle) then Exit;
curEditor := NSText(lHandle.currentEditor);
if not Assigned(curEditor) then Exit;
lRange := curEditor.selectedRange;
lRange.length := NewLength;
curEditor.setSelectedRange(lRange);
end;
class procedure TCocoaWSCustomEdit.Cut(const ACustomEdit: TCustomEdit);
begin
if not Assigned(ACustomEdit) or not (ACustomEdit.HandleAllocated) then Exit;
NSApplication(NSApp).sendAction_to_from(objcselector('cut:'), nil, id(ACustomEdit.Handle));
end;
class procedure TCocoaWSCustomEdit.Copy(const ACustomEdit: TCustomEdit);
begin
if not Assigned(ACustomEdit) or not (ACustomEdit.HandleAllocated) then Exit;
NSApplication(NSApp).sendAction_to_from(objcselector('copy:'), nil, id(ACustomEdit.Handle));
end;
class procedure TCocoaWSCustomEdit.Paste(const ACustomEdit: TCustomEdit);
begin
if not Assigned(ACustomEdit) or not (ACustomEdit.HandleAllocated) then Exit;
NSApplication(NSApp).sendAction_to_from(objcselector('paste:'), nil, id(ACustomEdit.Handle));
end;
class procedure TCocoaWSCustomEdit.Undo(const ACustomEdit: TCustomEdit);
begin
if not Assigned(ACustomEdit) or not (ACustomEdit.HandleAllocated) then Exit;
NSApplication(NSApp).sendAction_to_from(objcselector('undo:'), nil, id(ACustomEdit.Handle));
end;
class procedure TCocoaWSCustomEdit.SetText(const AWinControl: TWinControl;
const AText: String);
var
txt : string;
mxl : Integer;
begin
if (AWinControl.HandleAllocated) then
begin
txt := AText;
mxl := TCustomEdit(AWinControl).MaxLength;
if (mxl > 0) and (UTF8Length(txt) > mxl) then
txt := UTF8Copy(txt, 1, mxl);
ControlSetTextWithChangeEvent(NSControl(AWinControl.Handle), txt);
end;
end;
{ TCocoaMemoStrings }
function LineBreaksToUnix(const src: string): string;
begin
// todo: need more effecient replacement
Result := StringReplace( StringReplace(
StringReplace(src, #10#13, #10, [rfReplaceAll])
, #13#10, #10, [rfReplaceAll])
, #13, #10, [rfReplaceAll]);
end;
constructor TCocoaMemoStrings.Create(ATextView: TCocoaTextView);
begin
inherited Create;
FTextView := ATextView;
end;
function TCocoaMemoStrings.GetTextStr: string;
begin
Result := NSStringToString(FTextView.string_);
end;
procedure TCocoaMemoStrings.SetTextStr(const Value: string);
begin
SetNSText(FTextView, LineBreaksToUnix(Value));
FTextView.textDidChange(nil);
end;
class procedure TCocoaMemoStrings.GetLineStart(const s: AnsiString; LineIndex: Integer; var Offset, LinesSkipped: Integer);
var
i : Integer;
begin
i:=1;
LinesSkipped:=0;
while (LinesSkipped<>LineIndex) and (i<=length(s)) do begin
if s[i] in [#10, #13] then begin
inc(i);
inc(LinesSkipped);
if (i<=length(s)) and (s[i] in [#10,#13]) and (s[i-1]<>s[i]) then
inc(i);
end else
inc(i);
end;
Offset:=i;
end;
class function TCocoaMemoStrings.GetLinesCount(const s: AnsiString): Integer;
var
ofs : Integer;
begin
Result:=0;
ofs:=0;
GetLineStart(s, -1, ofs, Result);
end;
function TCocoaMemoStrings.GetCount:Integer;
begin
Result:=GetLinesCount(GetTextStr);
inc(Result);
end;
function TCocoaMemoStrings.Get(Index:Integer):string;
var
s : AnsiString;
ofs : Integer;
eofs : Integer;
t : Integer;
begin
s:=GetTextStr;
t:=0;
ofs:=0;
GetLineStart(s, Index, ofs, t);
eofs:=ofs;
while (eofs<=length(s)) and not (s[eofs] in [#10,#13]) do
inc(eofs);
Result:=Copy(s, ofs, eofs-ofs);
end;
procedure TCocoaMemoStrings.Clear;
begin
SetTextStr('');
end;
procedure TCocoaMemoStrings.Delete(Index:Integer);
var
s : AnsiString;
ofs : Integer;
eofs : Integer;
t : Integer;
begin
s:=GetTextStr;
GetLineStart(s, Index, ofs, t);
eofs:=ofs;
while (eofs<=length(s)) and not (s[eofs] in [#10,#13]) do
inc(eofs);
if eofs<=length(s) then begin
inc(eofs);
if (eofs<=length(s)) and (s[eofs] in [#10,#13]) and (s[eofs-1]<>s[eofs]) then
inc(eofs);
end;
System.Delete(s, ofs, eofs-ofs);
SetTextStr(s);
end;
procedure TCocoaMemoStrings.Insert(Index:Integer;const S:string);
var
rng : NSRange;
st,ed : NSUInteger;
ced : NSUInteger;
ns : NSString;
idx : integer;
ro : Boolean;
const
LFSTR = #10;
begin
ns:=FTextView.string_;
idx:=0;
rng:=NSMakeRange(0,0);
while (idx<Index) and (rng.location<ns.length) do begin
ns.getLineStart_end_contentsEnd_forRange(nil, @st, @ced, rng);
inc(idx);
rng.location:=st;
rng.length:=0;
end;
// using selectedRange in order to be consistent with Windows widgetset
if rng.location>ns.length then rng.location:=ns.length;
inc(FTextView.supressTextChangeEvent);
ro := FTextView.isEditable; // checking for read-only flag;
if not ro then FTextView.setEditable(true);
FTextView.setSelectedRange(rng);
if (rng.location>=ns.length) and (st=ced) and (ns.length>0) then
FTextView.insertText( NSString.stringWithUTF8String( LFSTR ));
if S<>'' then
begin
FTextView.insertText( NSString.stringWithUTF8String( @S[1] ));
end;
dec(FTextView.supressTextChangeEvent);
FTextView.insertText( NSString.stringWithUTF8String( LFSTR ));
if not ro then FTextView.setEditable(ro);
FTextView.undoManager.removeAllActions;
end;
procedure TCocoaMemoStrings.LoadFromFile(const FileName: string);
var
TheStream: TFileStreamUTF8;
begin
TheStream:=TFileStreamUtf8.Create(FileName,fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(TheStream);
finally
TheStream.Free;
end;
end;
procedure TCocoaMemoStrings.SaveToFile(const FileName: string);
var
TheStream: TFileStreamUTF8;
begin
TheStream:=TFileStreamUtf8.Create(FileName,fmCreate);
try
SaveToStream(TheStream);
finally
TheStream.Free;
end;
end;
{ TCocoaWSCustomMemo }
procedure TextViewSetWordWrap(txt: NSTextView; lScroll: NSScrollView; NewWordWrap: Boolean);
var
layoutSize: NSSize;
begin
if NewWordWrap then
begin
layoutSize := lScroll.contentSize();
layoutSize := GetNSSize(layoutSize.width, CGFloat_Max);
txt.textContainer.setContainerSize(layoutSize);
txt.textContainer.setWidthTracksTextView(True);
txt.setHorizontallyResizable(false);
txt.setAutoresizingMask(NSViewWidthSizable);
layoutSize.height:=txt.frame.size.height;
txt.setFrameSize(layoutSize);
end
else
begin
txt.textContainer.setWidthTracksTextView(False);
layoutSize := GetNSSize(CGFloat_Max, CGFloat_Max);
txt.textContainer.setContainerSize(layoutSize);
txt.textContainer.setWidthTracksTextView(False);
txt.setHorizontallyResizable(true);
txt.setAutoresizingMask(0);
end;
txt.sizeToFit;
end;
function AlignmentLCLToCocoa(al: TAlignment): NSTextAlignment;
begin
case al of
taRightJustify:
Result := NSTextAlignmentRight;
taCenter:
Result := NSTextAlignmentCenter;
else
Result:= NSTextAlignmentLeft;
end;
end;
procedure TextViewSetAllignment(txt: NSTextView; align: TAlignment);
begin
//todo: for bidi modes, there's "NSTextAlignmentNatural"
txt.setAlignment( AlignmentLCLToCocoa(align) );
end;
procedure TextFieldSetAllignment(txt: NSTextField; align: TAlignment);
begin
//todo: for bidi modes, there's "NSTextAlignmentNatural"
txt.setAlignment( AlignmentLCLToCocoa(align) );
end;
class function TCocoaWSCustomMemo.GetTextView(AWinControl: TWinControl): TCocoaTextView;
var
lScroll: TCocoaScrollView;
begin
lScroll := GetScrollView(AWinControl);
if not Assigned(lScroll) then
begin
Exit(nil);
end;
Result := TCocoaTextView(lScroll.documentView);
end;
class function TCocoaWSCustomMemo.GetScrollView(AWinControl: TWinControl): TCocoaScrollView;
begin
if not Assigned(AWinControl) or (not AWinControl.HandleAllocated) or (AWinControl.Handle=0) then
begin
Exit(nil);
end;
Result := TCocoaScrollView(AWinControl.Handle);
end;
class function TCocoaWSCustomMemo.CreateHandle(const AWinControl: TWinControl;
const AParams: TCreateParams):TLCLIntfHandle;
var
txt: TCocoaTextView;
ns: NSString;
scr: TCocoaScrollView;
nr:NSRect;
r:TRect;
layoutSize: NSSize;
lcl: TLCLCommonCallback;
begin
scr := TCocoaScrollView(NSView(TCocoaScrollView.alloc).lclInitWithCreateParams(AParams));
nr.origin.x:=0;
nr.origin.y:=0;
nr.size.height:=0;
nr.size.width:=AParams.Width;
txt := TCocoaTextView.alloc.initwithframe(nr);
txt.setAllowsUndo(true);
// setting up a default system font (to be consistent with other widgetsets)
txt.setFont( NSFont.systemFontOfSize( NSFont.systemFontSizeForControlSize(NSRegularControlSize) ));
txt.setRichText(false);
txt.setImportsGraphics(false);
txt.setUsesRuler(false);
// this is necessary for Ward Wrap disabled, so NSViewText
// doesn't have a constraint to resize
// Apple default maxsize is InitialWidth, 10000000
// (MaxSize is also changed automatically, if NSViewText size is changed)
txt.setMaxSize(NSMakeSize(10000000, 10000000));
scr.setDocumentView(txt);
scr.setHasVerticalScroller(VerticalScrollerVisible[TMemo(AWinControl).ScrollBars]);
scr.setHasHorizontalScroller(HorizontalScrollerVisible[TMemo(AWinControl).ScrollBars]);
scr.setAutohidesScrollers(ScrollerAutoHide[TMemo(AWinControl).ScrollBars]);
scr.setDrawsBackground(false);
ScrollViewSetBorderStyle(scr, TCustomMemo(AWinControl).BorderStyle);
UpdateFocusRing(txt, TCustomMemo(AWinControl).BorderStyle);
nr:=scr.documentVisibleRect;
txt.setFrame(nr);
txt.lclSetEnabled(True);
// ToDo: This should be made selectable in the LCL
txt.setAutomaticQuoteSubstitutionEnabled(False);
txt.setAutomaticLinkDetectionEnabled(False);
// macOS 10.6 version
if txt.respondsToSelector(objcselector('setAutomaticDataDetectionEnabled:')) then
txt.setAutomaticDataDetectionEnabled(false);
if txt.respondsToSelector(objcselector('setAutomaticTextReplacementEnabled:')) then
txt.setAutomaticTextReplacementEnabled(False);
if txt.respondsToSelector(ObjCSelector('setAutomaticDashSubstitutionEnabled:')) then
txt.setAutomaticDashSubstitutionEnabled(False);
if txt.respondsToSelector(ObjCSelector('setAutomaticSpellingCorrectionEnabled:')) then
txt.setAutomaticSpellingCorrectionEnabled(False);
// defaulting to System colors
// This makes NSTextView to be responsive to theme color change (Mojave 10.14)
txt.setTextColor(NSColor.textColor);
txt.setBackgroundColor(NSColor.textBackgroundColor);
scr.setFocusRingType(NSFocusRingTypeExterior);
lcl := TLCLCommonCallback.Create(txt, AWinControl);
lcl.ForceReturnKeyDown := true;
txt.callback := lcl;
txt.setDelegate(txt);
SetNSText(txt, AParams.Caption);
scr.callback := txt.callback;
TextViewSetWordWrap(txt, scr, TCustomMemo(AWinControl).WordWrap);
TextViewSetAllignment(txt, TCustomMemo(AWinControl).Alignment);
txt.wantReturns := TCustomMemo(AWinControl).WantReturns;
txt.callback.SetTabSuppress(not TCustomMemo(AWinControl).WantTabs);
Result := TLCLIntfHandle(scr);
end;
class procedure TCocoaWSCustomMemo.SetColor(const AWinControl: TWinControl);
var
txt: TCocoaTextView;
begin
txt := GetTextView(AWinControl);
if not Assigned(txt) then Exit;
if (AWinControl.Color = clDefault) or (AWinControl.Color = clWindow) or (AWinControl.Color = clBackground) then
txt.setBackgroundColor( NSColor.textBackgroundColor )
else
txt.setBackgroundColor( ColorToNSColor(ColorToRGB(AWinControl.Color)));
end;
class procedure TCocoaWSCustomMemo.SetSelStart(const ACustomEdit: TCustomEdit;
NewStart: integer);
var
lRange: NSRange;
txt: TCocoaTextView;
begin
txt := GetTextView(ACustomEdit);
if not Assigned(txt) then Exit;
lRange := txt.selectedRange;
lRange.location := NewStart;
txt.setSelectedRange(lRange);
txt.scrollRangeToVisible(lRange);
end;
class procedure TCocoaWSCustomMemo.SetSelLength(const ACustomEdit: TCustomEdit;
NewLength: integer);
var
lRange: NSRange;
txt: TCocoaTextView;
begin
txt := GetTextView(ACustomEdit);
if not Assigned(txt) then Exit;
lRange := txt.selectedRange;
lRange.length := NewLength;
txt.setSelectedRange(lRange);
end;
class procedure TCocoaWSCustomMemo.SetBorderStyle(
const AWinControl: TWinControl; const ABorderStyle: TBorderStyle);
var
sv: TCocoaScrollView;
begin
sv := GetScrollView(AWinControl);
if not Assigned(sv) then Exit;
ScrollViewSetBorderStyle(sv, ABorderStyle);
UpdateFocusRing(NSView(sv.documentView), ABorderStyle);
end;
class function TCocoaWSCustomMemo.GetCaretPos(const ACustomEdit: TCustomEdit): TPoint;
var
txt: TCocoaTextView;
lValue: NSValue;
viewString: NSString;
paraStart: NSUInteger = 0;
paraEnd: NSUInteger = 0;
contentsEnd: NSUInteger = 0;
curLine: Integer = 0;
begin
Result := Point(0, 0);
txt := GetTextView(ACustomEdit);
if not Assigned(txt) then Exit;
lValue := NSValue(txt.selectedRanges.objectAtIndex(0));
if lValue = nil then Exit;
viewString := txt.string_;
Result.X := lValue.rangeValue.location;
// There is no simple function to do this in Cocoa :(
while (paraEnd < viewString.length) do
begin
viewString.getLineStart_end_contentsEnd_forRange(@paraStart,
@paraEnd, @contentsEnd, NSMakeRange(paraEnd, 0));
if (lValue.rangeValue.location >= paraStart) and
(lValue.rangeValue.location < paraEnd) then
begin
Break;
end
else
Result.X := Result.X - (paraEnd - paraStart);
Inc(curLine);
end;
Result.Y := curLine;
{This doesn't work :/
lineRange := viewString.lineRangeForRange(lValue.rangeValue);
Result.X := lineRange.location;}
end;
class function TCocoaWSCustomMemo.GetSelStart(const ACustomEdit: TCustomEdit): integer;
var
txt: TCocoaTextView;
begin
txt := GetTextView(ACustomEdit);
if not Assigned(txt) then
begin
Result:=0;
Exit;
end;
Result := txt.selectedRange.location;
end;
class function TCocoaWSCustomMemo.GetSelLength(const ACustomEdit: TCustomEdit): integer;
var
txt: TCocoaTextView;
ns: NSArray;
begin
txt := GetTextView(ACustomEdit);
if not Assigned(txt) then
begin
Result:=0;
Exit;
end;
Result := txt.selectedRange.length;
end;
class procedure TCocoaWSCustomMemo.SetAlignment(const ACustomEdit: TCustomEdit;
const NewAlignment: TAlignment);
var
txt: TCocoaTextView;
begin
txt := GetTextView(ACustomEdit);
if Assigned(txt) then
TextViewSetAllignment(txt, NewAlignment);
end;
class function TCocoaWSCustomMemo.GetStrings(const ACustomMemo: TCustomMemo): TStrings;
var
txt: TCocoaTextView;
begin
txt := GetTextView(ACustomMemo);
if Assigned(txt) then
Result := TCocoaMemoStrings.Create(txt)
else
Result := nil
end;
class procedure TCocoaWSCustomMemo.AppendText(const ACustomMemo: TCustomMemo;
const AText: string);
begin
//todo:
end;
class procedure TCocoaWSCustomMemo.SetReadOnly(const ACustomEdit:TCustomEdit;
NewReadOnly:boolean);
var
txt: TCocoaTextView;
begin
txt := GetTextView(ACustomEdit);
if Assigned(txt) then
txt.setEditable(not NewReadOnly);
end;
class procedure TCocoaWSCustomMemo.SetScrollbars(const ACustomMemo: TCustomMemo; const NewScrollbars: TScrollStyle);
begin
ScrollViewSetScrollStyles(TCocoaScrollView(ACustomMemo.Handle), NewScrollbars);
end;
class procedure TCocoaWSCustomMemo.SetWantTabs(const ACustomMemo: TCustomMemo;
const NewWantTabs: boolean);
var
txt: TCocoaTextView;
begin
txt := GetTextView(ACustomMemo);
if (not Assigned(txt)) then Exit;
txt.callback.SetTabSuppress(not NewWantTabs);
end;
class procedure TCocoaWSCustomMemo.SetWantReturns(const ACustomMemo: TCustomMemo;
const NewWantReturns: boolean);
var
txt: TCocoaTextView;
begin
txt := GetTextView(ACustomMemo);
if (not Assigned(txt)) then Exit;
txt.wantReturns := NewWantReturns;
end;
class procedure TCocoaWSCustomMemo.SetWordWrap(const ACustomMemo: TCustomMemo; const NewWordWrap: boolean);
var
txt: TCocoaTextView;
lScroll: TCocoaScrollView;
begin
txt := GetTextView(ACustomMemo);
lScroll := GetScrollView(ACustomMemo);
if (not Assigned(txt)) or (not Assigned(lScroll)) then Exit;
TextViewSetWordWrap(txt, lScroll, NewWordWrap);
end;
class procedure TCocoaWSCustomMemo.SetText(const AWinControl:TWinControl;const AText:String);
var
txt: TCocoaTextView;
begin
txt := GetTextView(AWinControl);
if not Assigned(txt) then Exit;
SetNSText(txt, LineBreaksToUnix(AText));
end;
class function TCocoaWSCustomMemo.GetText(const AWinControl: TWinControl; var AText: String): Boolean;
var
txt: TCocoaTextView;
begin
txt := GetTextView(AWinControl);
Result := Assigned(txt);
if Result then
AText := NSStringToString(txt.string_);
end;
{ TCocoaWSCustomComboBox }
type
TCustomComboBoxAccess = class(TCustomComboBox)
end;
class function TCocoaWSCustomComboBox.CreateHandle(const AWinControl:TWinControl;
const AParams:TCreateParams):TLCLIntfHandle;
var
cmb: TCocoaComboBox;
rocmb: TCocoaReadOnlyComboBox;
begin
Result:=0;
if ComboBoxIsReadOnly(TCustomComboBox(AWinControl)) then
begin
rocmb := NSView(TCocoaReadOnlyComboBox.alloc).lclInitWithCreateParams(AParams);
if not Assigned(rocmb) then Exit;
rocmb.list:=TCocoaReadOnlyComboBoxList.Create(rocmb);
rocmb.setTarget(rocmb);
rocmb.setAction(objcselector('comboboxAction:'));
rocmb.selectItemAtIndex(rocmb.lastSelectedItemIndex);
rocmb.callback:=TLCLComboboxCallback.Create(rocmb, AWinControl);
Result:=TLCLIntfHandle(rocmb);
rocmb.isOwnerDrawn := ComboBoxIsOwnerDrawn(TCustomComboBox(AWinControl).Style);
rocmb.isOwnerMeasure := ComboBoxIsVariable(TCustomComboBox(AWinControl).Style);
end
else
begin
cmb := NSView(TCocoaComboBox.alloc).lclInitWithCreateParams(AParams);
if not Assigned(cmb) then Exit;
//cmb.setCell(TCocoaComboBoxCell.alloc.initTextCell(NSString.string_));
cmb.list:=TCocoaEditComboBoxList.Create(cmb);
cmb.setUsesDataSource(true);
cmb.setDataSource(cmb);
cmb.setDelegate(cmb);
cmb.setStringValue(NSStringUtf8(AParams.Caption));
cmb.callback:=TLCLComboboxCallback.Create(cmb, AWinControl);
if (cmb.respondsToSelector(ObjCSelector('cell'))) and Assigned(cmb.cell) then
NSTextFieldCell(cmb.cell).setUsesSingleLineMode(true);
// default BorderStyle for TComboBox is bsNone! and it looks ugly!
// also, Win32 doesn't suppot borderstyle for TComboBox at all.
// to be tested and considered
//ComboBoxSetBorderStyle(cmb, TCustomComboBoxAccess(AWinControl).BorderStyle);
Result:=TLCLIntfHandle(cmb);
end;
//todo: 26 pixels is the height of 'normal' combobox. The value is taken from the Interface Builder!
// use the correct way to set the size constraints
AWinControl.Constraints.SetInterfaceConstraints(0,COMBOBOX_MINI_HEIGHT,0,COMBOBOX_REG_HEIGHT);
end;
class procedure TCocoaWSCustomComboBox.SetBorderStyle(
const AWinControl: TWinControl; const ABorderStyle: TBorderStyle);
var
ACustomComboBox : TCustomComboBox;
begin
if not Assigned(AWinControl) or not AWinControl.HandleAllocated then Exit;
ACustomComboBox:= TCustomComboBox(AWinControl);
if ACustomComboBox.ReadOnly then
//Result := TCocoaReadOnlyComboBox(ACustomComboBox.Handle).indexOfSelectedItem
else
begin
//todo: consider the use of border style
//ComboBoxSetBorderStyle(TCocoaComboBox(ACustomComboBox.Handle), ABorderStyle);
end;
end;
class function TCocoaWSCustomComboBox.GetDroppedDown(
const ACustomComboBox: TCustomComboBox): Boolean;
var
cb : ICommonCallback;
obj : TObject;
begin
Result:=false;
if (not Assigned(ACustomComboBox)) or (not ACustomComboBox.HandleAllocated) then
Exit;
cb := NSView(ACustomComboBox.Handle).lclGetCallback;
if Assigned(cb) then
begin
obj := cb.GetCallbackObject;
if (obj is TLCLComboboxCallback) then
Result := TLCLComboboxCallback(obj).isShowPopup;
end;
end;
class function TCocoaWSCustomComboBox.GetItemIndex(const ACustomComboBox:
TCustomComboBox):integer;
var
idx : NSInteger;
begin
if (not Assigned(ACustomComboBox)) or (not ACustomComboBox.HandleAllocated) then
begin
Result:=-1;
Exit;
end;
if ACustomComboBox.ReadOnly then
idx := TCocoaReadOnlyComboBox(ACustomComboBox.Handle).indexOfSelectedItem
else
idx := TCocoaComboBox(ACustomComboBox.Handle).indexOfSelectedItem;
if idx = NSNotFound then
Result := -1
else
Result := Integer(idx);
end;
class procedure TCocoaWSCustomComboBox.SetItemIndex(const ACustomComboBox:
TCustomComboBox;NewIndex:integer);
var
rocmb: TCocoaReadOnlyComboBox;
begin
if (not Assigned(ACustomComboBox)) or (not ACustomComboBox.HandleAllocated) then
Exit;
if ACustomComboBox.ReadOnly then
begin
rocmb := TCocoaReadOnlyComboBox(ACustomComboBox.Handle);
rocmb.lastSelectedItemIndex := NewIndex;
rocmb.selectItemAtIndex(NewIndex);
end
else
TCocoaComboBox(ACustomComboBox.Handle).selectItemAtIndex(NewIndex);
end;
class procedure TCocoaWSCustomComboBox.SetDropDownCount(const ACustomComboBox:
TCustomComboBox;NewCount:Integer);
begin
if (not Assigned(ACustomComboBox)) or (not ACustomComboBox.HandleAllocated) then
Exit;
if ACustomComboBox.ReadOnly then Exit;
TCocoaComboBox(ACustomComboBox.Handle).setNumberOfVisibleItems(NewCount);
end;
class function TCocoaWSCustomComboBox.GetItems(const ACustomComboBox: TCustomComboBox): TStrings;
begin
if (not Assigned(ACustomComboBox)) or (not ACustomComboBox.HandleAllocated) then
begin
Result:=nil;
Exit;
end;
if ACustomComboBox.ReadOnly then
Result:=TCocoaReadOnlyComboBox(ACustomComboBox.Handle).list
else
Result:=TCocoaComboBox(ACustomComboBox.Handle).list;
end;
class function TCocoaWSCustomComboBox.GetItemHeight(const ACustomComboBox:
TCustomComboBox):Integer;
begin
if (not Assigned(ACustomComboBox)) or (not ACustomComboBox.HandleAllocated) then
begin
Result:=0;
Exit;
end;
if ACustomComboBox.ReadOnly then
Result := 26 // ToDo
else
Result:=Round(TCocoaComboBox(ACustomComboBox.Handle).itemHeight);
end;
class procedure TCocoaWSCustomComboBox.SetItemHeight(const ACustomComboBox:
TCustomComboBox;const AItemHeight:Integer);
begin
if (not Assigned(ACustomComboBox)) or (not ACustomComboBox.HandleAllocated) then
Exit;
if ACustomComboBox.ReadOnly then
Exit // ToDo
else
TCocoaComboBox(ACustomComboBox.Handle).setItemHeight(AItemHeight);
end;
class procedure TCocoaWSCustomComboBox.GetPreferredSize(
const AWinControl: TWinControl; var PreferredWidth, PreferredHeight: integer;
WithThemeSpace: Boolean);
begin
// do not override PreferredWidth and Height
// see todo at TCocoaWSWinControl.GetPreferredSize
// once it's resolved, TCocoaWSCustomComboBox.GetPreferredSize could be removed
end;
class procedure TCocoaWSCustomComboBox.SetText(const AWinControl: TWinControl;
const AText: String);
begin
if (AWinControl.HandleAllocated) then
ControlSetTextWithChangeEvent(NSControl(AWinControl.Handle), AText);
end;
{ TCocoaWSToggleBox }
class function TCocoaWSToggleBox.CreateHandle(const AWinControl:TWinControl;
const AParams:TCreateParams):TLCLIntfHandle;
var
btn: NSButton;
cl: NSButtonCell;
begin
btn := AllocButton(AWinControl, TLCLCheckBoxCallback, AParams, CocoaToggleBezel, CocoaToggleType);
cl := NSButtonCell(NSButton(btn).cell);
cl.setShowsStateBy(cl.showsStateBy or NSContentsCellMask);
Result := TLCLIntfHandle(btn);
end;
{ TCocoaWSScrollBar }
class function TCocoaWSScrollBar.CreateHandle(const AWinControl:TWinControl;
const AParams:TCreateParams):TLCLIntfHandle;
var
scr : TCocoaScrollBar;
prm : TCreateParams;
const
ScrollBase = 15; // the shorter size of the scroller. There's a NSScroller class method for that
begin
prm := AParams;
// forcing the initial size to follow the designated kind of the scroll
if (TCustomScrollBar(AWinControl).Kind = sbVertical) then begin
prm.Width:=ScrollBase;
prm.Height:=ScrollBase*4;
end else
begin
prm.Width:=ScrollBase*4;
prm.Height:=ScrollBase;
end;
scr:=NSView(TCocoaScrollBar.alloc).lclInitWithCreateParams(prm);
scr.callback:=TLCLCommonCallback.Create(scr, AWinControl);
// OnChange (scrolling) event handling
scr.setTarget(scr);
scr.setAction(objcselector('actionScrolling:'));
scr.minInt:=TCustomScrollBar(AWinControl).Min;
scr.maxInt:=TCustomScrollBar(AWinControl).Max;
scr.pageInt:=TCustomScrollBar(AWinControl).PageSize;
scr.largeInc:=abs(TCustomScrollBar(AWinControl).LargeChange);
scr.smallInc:=abs(TCustomScrollBar(AWinControl).SmallChange);
if scr.largeInc=0 then scr.largeInc:=1;
if scr.smallInc=0 then scr.smallInc:=1;
Result:=TLCLIntfHandle(scr);
scr.lclSetFrame( Bounds(AParams.X, AParams.Y, AParams.Width, AParams.Height));
end;
// vertical/horizontal in Cocoa is set automatically according to
// the geometry of the scrollbar, it cannot be forced to an unusual value
class procedure TCocoaWSScrollBar.SetKind(const AScrollBar: TCustomScrollBar; const AIsHorizontal: Boolean);
begin
// the scroll type can be changed when creating a scroll.
// since the size got changed, we have to create the handle
RecreateWnd(AScrollBar);
end;
class procedure TCocoaWSScrollBar.SetParams(const AScrollBar:TCustomScrollBar);
var
lScroller: TCocoaScrollBar;
sz : integer;
begin
if not Assigned(AScrollBar) then Exit;
lScroller := TCocoaScrollBar(AScrollBar.Handle);
if (lScroller = nil) then Exit;
sz:=AScrollBar.Max - AScrollBar.PageSize;
if sz > 0 then
begin
lScroller.setDoubleValue( AScrollBar.Position / sz );
lScroller.setKnobProportion( AScrollBar.PageSize / AScrollBar.Max );
end;
end;
{ TCocoaWSCustomGroupBox }
class function TCocoaWSCustomGroupBox.CreateHandle(const AWinControl: TWinControl;
const AParams: TCreateParams): TLCLIntfHandle;
var
box: TCocoaGroupBox;
cap: NSString;
lGroupBoxContents: TCocoaCustomControl;
ns: NSRect;
//str: string;
begin
box := NSView(TCocoaGroupBox.alloc).lclInitWithCreateParams(AParams);
if Assigned(box) then
begin
box.callback := TLCLCommonCallback.Create(box, AWinControl);
TLCLCommonCallback(box.callback.GetCallbackObject).BlockCocoaUpDown := true;
cap := NSStringUTF8(AParams.Caption);
box.setTitle(cap);
cap.release;
// set a content view in order to be able to customize drawing for labels/color
ns := GetNSRect(AParams.X, AParams.Y, AParams.Width, AParams.Height);
lGroupBoxContents := TCocoaCustomControl(TCocoaCustomControl.alloc.initWithFrame(ns));
lGroupBoxContents.callback := box.callback; //TLCLCustomControlCallback.Create(lGroupBoxContents, AWinControl);
//str := Format('%X=%X', [PtrUInt(box.callback), PtrUInt(lGroupBoxContents.callback)]);
lGroupBoxContents.autorelease;
box.setContentView(lGroupBoxContents);
end;
Result := TLCLIntfHandle(box);
end;
class function TCocoaWSCustomGroupBox.GetText(const AWinControl: TWinControl; var AText: String): Boolean;
var
box: TCocoaGroupBox;
begin
box := TCocoaGroupBox(AWinControl.Handle);
Result:=Assigned(box);
if Result then
AText := NSStringToString(box.title);
end;
class procedure TCocoaWSCustomGroupBox.SetText(const AWinControl: TWinControl; const AText: String);
var
box: TCocoaGroupBox;
begin
box := TCocoaGroupBox(AWinControl.Handle);
box.setTitle(ControlTitleToNSStr(AText));
end;
{ TCocoaWSCustomListBox }
function GetListBox(AWinControl: TWinControl): TCocoaTableListView;
begin
if not Assigned(AWinControl) or (AWinControl.Handle=0) then
Result := nil
else
Result := TCocoaTableListView(TCocoaScrollView(AWinControl.Handle).documentView);
end;
function GetListBoxWithCb(AWinControl: TWinControl; out cb: TLCLListBoxCallback): TCocoaTableListView;
begin
Result := GetListBox(AWinControl);
if not Assigned(Result) then
cb := nil
else
cb := TLCLListBoxCallback(Result.lclGetCallback.GetCallbackObject)
end;
procedure ListBoxSetStyle(list: TCocoaTableListView; AStyle: TListBoxStyle);
begin
if not Assigned(list) then Exit;
list.isOwnerDraw := AStyle in [lbOwnerDrawFixed, lbOwnerDrawVariable];
list.isDynamicRowHeight := AStyle = lbOwnerDrawVariable;
//todo: if flag isCustomRowHeight changes in runtime
// noteHeightOfRowsWithIndexesChanged, should be sent to listview
end;
class function TCocoaWSCustomListBox.CreateHandle(const AWinControl:TWinControl;
const AParams:TCreateParams):TLCLIntfHandle;
var
list : TCocoaTableListView;
scroll : TCocoaScrollView;
lclListBox: TCustomListBox absolute AWinControl;
begin
list := AllocCocoaTableListView.lclInitWithCreateParams(AParams);
if not Assigned(list) then
begin
Result := 0;
Exit;
end;
list.callback := TLCLListBoxCallback.CreateWithView(list, AWinControl);
list.addTableColumn(NSTableColumn.alloc.init);
list.setHeaderView(nil);
list.setDataSource(list);
list.setDelegate(list);
list.setAllowsMultipleSelection(lclListBox.MultiSelect);
list.readOnly := true;
// LCL ItemHeight for TListBox can only be set during Recreation of Handle
if TCustomListBox(AWinControl).ItemHeight>0 then
begin
// Cocoa default is 16.
// Note that it might be different of Retina monitors
list.CustomRowHeight := TCustomListBox(AWinControl).ItemHeight;
list.setRowHeight(list.CustomRowHeight);
end;
ListBoxSetStyle(list, TCustomListBox(AWinControl).Style);
scroll := EmbedInScrollView(list);
if not Assigned(scroll) then
begin
list.dealloc;
Result := 0;
Exit;
end;
scroll.callback := list.callback;
scroll.setHasVerticalScroller(true);
scroll.setAutohidesScrollers(true);
ScrollViewSetBorderStyle(scroll, lclListBox.BorderStyle);
UpdateFocusRing(list, lclListBox.BorderStyle);
Result := TLCLIntfHandle(scroll);
end;
class function TCocoaWSCustomListBox.GetIndexAtXY(const ACustomListBox: TCustomListBox; X, Y: integer): integer;
var
list: TCocoaTableListView;
lPoint: NSPoint;
begin
list := GetListBox(ACustomListBox);
if not Assigned(list) then
begin
Result:=-1;
Exit();
end;
Result := LCLCoordToRow(list, x,y);
end;
class function TCocoaWSCustomListBox.GetItemRect(const ACustomListBox: TCustomListBox; Index: integer; var ARect: TRect): boolean;
var
view: TCocoaTableListView;
r:NSRect;
begin
Result := False;
view := GetListBox(ACustomListBox);
if not Assigned(view) then Exit(False);
Result := LCLGetItemRect(view, Index, 0, ARect);
end;
class function TCocoaWSCustomListBox.GetItemIndex(const ACustomListBox: TCustomListBox): integer;
var
view: TCocoaTableListView;
indexset: NSIndexSet;
begin
view:=GetListBox(ACustomListBox);
if not Assigned(view) then Exit(-1);
indexset:=view.selectedRowIndexes();
if indexset.count = 0 then
Result := -1
else
Result := indexset.firstIndex;
end;
class function TCocoaWSCustomListBox.GetSelCount(const ACustomListBox: TCustomListBox): integer;
var
view: TCocoaTableListView;
selection: NSIndexSet;
begin
view := GetListBox(ACustomListBox);
if not Assigned(view) then Exit(0);
selection := view.selectedRowIndexes();
Result := selection.count();
end;
class function TCocoaWSCustomListBox.GetSelected(const ACustomListBox: TCustomListBox; const AIndex: integer): boolean;
var
view: TCocoaTableListView;
selection: NSIndexSet;
begin
view := GetListBox(ACustomListBox);
if not Assigned(view) then Exit(False);
if AIndex < 0 then Exit(False);
selection := view.selectedRowIndexes();
Result := selection.containsIndex(AIndex);
end;
class function TCocoaWSCustomListBox.GetStrings(const ACustomListBox: TCustomListBox):TStrings;
var
view: TCocoaTableListView;
cb : TLCLListBoxCallback;
begin
view := GetListBoxWithCb(ACustomListBox, cb);
if not Assigned(view) then Exit(nil);
Result := cb.strings;
end;
class function TCocoaWSCustomListBox.GetTopIndex(const ACustomListBox: TCustomListBox): integer;
var
view: TCocoaTableListView;
begin
view := GetListBox(ACustomListBox);
if not Assigned(view) then Exit(-1);
Result := LCLGetTopRow(view);
end;
class procedure TCocoaWSCustomListBox.SelectItem(const ACustomListBox: TCustomListBox; AIndex: integer; ASelected: boolean);
var
list: TCocoaTableListView;
begin
list := GetListBox(ACustomListBox);
if not Assigned(list) then Exit();
if ASelected then
begin
list.selectRowIndexes_byExtendingSelection(NSIndexSet.indexSetWithIndex(AIndex), True)
end
else
list.deselectRow(AIndex);
end;
class procedure TCocoaWSCustomListBox.SetBorderStyle(
const AWinControl: TWinControl; const ABorderStyle: TBorderStyle);
var
list: TCocoaTableListView;
begin
list := GetListBox(AWinControl);
if not Assigned(list) then Exit;
ScrollViewSetBorderStyle(list.enclosingScrollView, ABorderStyle);
UpdateFocusRing(list, ABorderStyle);
end;
class procedure TCocoaWSCustomListBox.SetItemIndex(const ACustomListBox: TCustomListBox; const AIndex: integer);
var
list: TCocoaTableListView;
begin
list := GetListBox(ACustomListBox);
if not Assigned(list) then Exit();
if (AIndex < 0) then
list.deselectAll(nil)
else
begin
list.selectRowIndexes_byExtendingSelection(NSIndexSet.indexSetWithIndex(AIndex), false);
list.scrollRowToVisible(AIndex);
end;
end;
class procedure TCocoaWSCustomListBox.SetSelectionMode(const ACustomListBox: TCustomListBox; const AExtendedSelect, AMultiSelect: boolean);
var
list: TCocoaTableListView;
begin
list := GetListBox(ACustomListBox);
if not Assigned(list) then Exit();
list.setAllowsMultipleSelection(AMultiSelect);
end;
class procedure TCocoaWSCustomListBox.SetStyle(const ACustomListBox: TCustomListBox);
var
view: TCocoaTableListView;
begin
view := GetListBox(ACustomListBox);
ListBoxSetStyle(view, TCustomListBox(ACustomListBox).Style);
view.setNeedsDisplay_(true);
end;
class procedure TCocoaWSCustomListBox.SetTopIndex(const ACustomListBox: TCustomListBox; const NewTopIndex: integer);
var
view: TCocoaTableListView;
begin
view := GetListBox(ACustomListBox);
if not Assigned(view) then Exit();
view.scrollRowToVisible(NewTopIndex);
end;
procedure ControlSetTextWithChangeEvent(ctrl: NSControl; const text: string);
var
cb: ICommonCallBack;
begin
SetNSControlValue(ctrl, text);
cb := ctrl.lclGetcallback;
if Assigned(cb) then // cb.SendOnChange;
cb.SendOnTextChanged;
end;
end.
|