1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
|
{ $Id: $}
{ --------------------------------------------
cocoatextedits.pas - Cocoa internal classes
--------------------------------------------
This unit contains the private classhierarchy for the Cocoa implemetations
*****************************************************************************
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 CocoaTextEdits;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
{$interfaces corba}
{$include cocoadefines.inc}
{.$DEFINE COCOA_DEBUG_SETBOUNDS}
{.$DEFINE COCOA_SPIN_DEBUG}
{.$DEFINE COCOA_SPINEDIT_INSIDE_CONTAINER}
interface
uses
Types, Classes, SysUtils,
Math, // needed for MinDouble, MaxDouble
LCLType,
MacOSAll, CocoaAll, CocoaConfig, CocoaUtils, CocoaGDIObjects,
CocoaPrivate, CocoaCallback;
const
SPINEDIT_DEFAULT_STEPPER_WIDTH = 15;
SPINEDIT_EDIT_SPACING_FOR_SELECTION = 4;
// From Interface Builder MacOSX 10.6
// The heights are from layout rectangle.
COMBOBOX_REG_HEIGHT = 20;
COMBOBOX_SMALL_HEIGHT = 17;
COMBOBOX_MINI_HEIGHT = 14;
COMBOBOX_RO_REG_HEIGHT = 20;
COMBOBOX_RO_SMALL_HEIGHT = 17;
COMBOBOX_RO_MINI_HEIGHT = 15;
COMBOBOX_RO_ROUND_SIZE = 7;
COMBOBOX_RO_BUTTON_WIDTH = 18;
type
TCocoaFieldEditor = objcclass;
NSTextField_LCLExt = objcprotocol
procedure lclSetMaxLength(amax: integer); message 'lclSetMaxLength:';
end;
{ TCocoaTextField }
TCocoaTextField = objcclass(NSTextField, NSTextField_LCLExt)
callback: ICommonCallback;
maxLength: Integer;
fixedInitSetting: Boolean;
function acceptsFirstResponder: LCLObjCBoolean; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
// key
procedure textDidChange(notification: NSNotification); override;
function textView_shouldChangeTextInRange_replacementString (textView: NSTextView; affectedCharRange: NSRange; replacementString: NSString): ObjCBOOL; message 'textView:shouldChangeTextInRange:replacementString:';
// mouse
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
procedure scrollWheel(event: NSEvent); override;
procedure lclSetMaxLength(amax: integer);
end;
{ TCocoaSecureTextField }
TCocoaSecureTextField = objcclass(NSSecureTextField, NSTextField_LCLExt)
public
maxLength: Integer;
callback: ICommonCallback;
function acceptsFirstResponder: LCLObjCBoolean; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
// key
procedure textDidChange(notification: NSNotification); override;
// mouse
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
procedure scrollWheel(event: NSEvent); override;
procedure lclSetMaxLength(amax: integer);
end;
{ TCocoaTextView }
TCocoaTextView = objcclass(NSTextView, NSTextDelegateProtocol, NSTextViewDelegateProtocol)
public
callback: ICommonCallback;
FEnabled: Boolean;
FUndoManager: NSUndoManager;
supressTextChangeEvent: Integer; // if above zero, then don't send text change event
wantReturns: Boolean;
procedure dealloc; override;
function acceptsFirstResponder: LCLObjCBoolean; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
procedure changeColor(sender: id); override;
// keyboard
procedure cut(sender: id); override;
procedure paste(sender: id); override;
procedure insertNewline(sender: id); override;
// mouse
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseEntered(event: NSEvent); override;
procedure mouseExited(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
function lclIsEnabled: Boolean; override;
procedure lclSetEnabled(AEnabled: Boolean); override;
// delegate methods
procedure textDidChange(notification: NSNotification); message 'textDidChange:';
procedure lclExpectedKeys(var wantTabs, wantArrows, wantReturn, wantAll: Boolean); override;
function undoManagerForTextView(view: NSTextView): NSUndoManager; message 'undoManagerForTextView:';
end;
{ TCococaFieldEditorExt }
TCococaFieldEditorExt = objccategory(NSTextView)
// this override should take care of any Cocoa editors possible
// for example NSSecureTextView used with TCocoaSecureField aka NSSecureTextField
function lclGetCallback: ICommonCallback; reintroduce;
end;
{ TCocoaFieldEditor }
TCocoaFieldEditor = objcclass(NSTextView)
public
// This flag is used to hack an infinite loop
// when switching "editable" (readonly) mode of NSTextField
// see TCocoaWSCustomEdit.SetReadOnly
goingReadOnly: Boolean;
function lclGetCallback: ICommonCallback; override;
function becomeFirstResponder: LCLObjCBoolean; override;
procedure setDelegate(adelegate: NSTextDelegateProtocol); override;
procedure lclReviseCursorColor; message 'lclReviseCursorColor';
// keyboard
procedure cut(sender: id); override;
procedure paste(sender: id); override;
procedure insertNewline(sender: id); override;
// mouse
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
procedure scrollWheel(event: NSEvent); override;
end;
type
TCocoaComboBox = objcclass;
TCocoaReadOnlyComboBox = objcclass;
{ TCocoaComboBoxList }
TCocoaComboBoxList = class(TStringList);
TCocoaEditComboBoxList = class(TCocoaComboBoxList)
protected
FOwner: TCocoaComboBox;
procedure InsertItem(Index: Integer; const S: string; O: TObject); override;
procedure Changed; override;
public
// Pass only 1 owner and nil for the other ones
constructor Create(AOwner: TCocoaComboBox);
end;
IComboboxCallBack = interface(ICommonCallBack)
procedure ComboBoxWillPopUp;
procedure ComboBoxWillDismiss;
procedure ComboBoxSelectionDidChange;
procedure ComboBoxSelectionIsChanging;
procedure GetRowHeight(rowidx: integer; var h: Integer);
procedure ComboBoxDrawItem(itemIndex: Integer; ctx: TCocoaContext;
const r: TRect; isSelected: Boolean; backgroundPainted: Boolean );
end;
{ TCocoaComboBoxItemCell }
// represents an item in the combobox dropdown
// it should be able to call "draw" callback
TCocoaComboBoxItemCell = objcclass(NSTextFieldCell)
procedure drawWithFrame_inView(cellFrame: NSRect; controlView_: NSView); override;
end;
{ TCocoaComboBoxCell }
// represents combobox itself. All functionality is implemented
// in NSComboBoxCell. The cell is also acting as a delegate
// for NSTextView, that's used in popup drop-down window.
// Apple is deprecating "cells" so NSComboBox implementation
// will change in future and it must be expected that NSComboBoxCell
// would not be used in future.
TCocoaComboBoxCell = objcclass(NSComboBoxCell)
//function tableView_objectValueForTableColumn_row(tableView: NSTableView; tableColumn: NSTableColumn; row: NSInteger): id; message 'tableView:objectValueForTableColumn:row:';
function tableView_dataCellForTableColumn_row(tableView: NSTableView; tableColumn: NSTableColumn; row: NSInteger): NSCell; message 'tableView:dataCellForTableColumn:row:';
//function tableView_sizeToFitWidthOfColumn(tableView: NSTableView; column: NSInteger): CGFloat; message 'tableView:sizeToFitWidthOfColumn:';
//procedure tableView_willDisplayCell_forTableColumn_row(tableView: NSTableView; cell: id; tableColumn: NSTableColumn; row: NSInteger); message 'tableView:willDisplayCell:forTableColumn:row:';
//function tableView_heightOfRow(tableView: NSTableView; row: NSInteger): CGFloat; message 'tableView:heightOfRow:';
end;
{ TCocoaComboBox }
TCocoaComboBox = objcclass(NSComboBox, NSComboBoxDataSourceProtocol, NSComboBoxDelegateProtocol)
private
userSel: boolean;
public
callback: IComboboxCallBack;
list: TCocoaComboBoxList;
resultNS: NSString; //use to return values to combo
isDown: Boolean;
function acceptsFirstResponder: LCLObjCBoolean; override;
procedure textDidChange(notification: NSNotification); override;
// NSComboBoxDataSourceProtocol
function comboBox_objectValueForItemAtIndex_(combo: TCocoaComboBox; row: NSInteger): id; message 'comboBox:objectValueForItemAtIndex:';
function comboBox_indexOfItemWithStringValue(aComboBox: NSComboBox; string_: NSString): NSUInteger; message 'comboBox:indexOfItemWithStringValue:';
function numberOfItemsInComboBox(combo: TCocoaComboBox): NSInteger; message 'numberOfItemsInComboBox:';
//
procedure dealloc; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
// NSComboBoxDelegateProtocol
procedure comboBoxWillPopUp(notification: NSNotification); message 'comboBoxWillPopUp:';
procedure comboBoxWillDismiss(notification: NSNotification); message 'comboBoxWillDismiss:';
procedure comboBoxSelectionDidChange(notification: NSNotification); message 'comboBoxSelectionDidChange:';
procedure comboBoxSelectionIsChanging(notification: NSNotification); message 'comboBoxSelectionIsChanging:';
//
procedure setStringValue(avalue: NSString); override;
function lclGetFrameToLayoutDelta: TRect; override;
// mouse
function acceptsFirstMouse(event: NSEvent): LCLObjCBoolean; override;
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure rightMouseDragged(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure otherMouseDragged(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
procedure scrollWheel(event: NSEvent); override;
end;
{ TCocoaReadOnlyView }
TCocoaReadOnlyView = objcclass (NSView)
private
itemIndex: Integer;
combobox: TCocoaReadOnlyComboBox;
public
procedure drawRect(dirtyRect: NSRect); override;
procedure mouseUp(event: NSEvent); override;
end;
{ TCocoaReadOnlyComboBoxList }
TCocoaReadOnlyComboBoxList = class(TCocoaComboBoxList)
private
FId: Integer;
protected
FOwner: TCocoaReadOnlyComboBox;
procedure InsertItem(Index: Integer; const S: string; O: TObject); override;
procedure Put(Index: Integer; const S: string); override;
procedure UpdateItemSize;
public
// Pass only 1 owner and nil for the other ones
constructor Create(AOwner: TCocoaReadOnlyComboBox);
procedure Delete(Index: Integer); override;
procedure Clear; override;
end;
{ TCocoaReadOnlyComboBoxMenuDelegate }
TCocoaReadOnlyComboBoxMenuDelegate = objcclass( NSObject, NSMenuDelegateProtocol )
private
_lastHightlightItem: NSMenuItem;
_comboBox: NSPopUpButton;
procedure menu_willHighlightItem (menu: NSMenu; item: NSMenuItem);
procedure menuDidClose (menu: NSMenu);
end;
{ TCocoaReadOnlyComboBox }
TCocoaReadOnlyComboBox = objcclass(NSPopUpButton)
private
_menuDelegate: TCocoaReadOnlyComboBoxMenuDelegate;
_textColorAttribs: NSDictionary;
_defaultItemHeight: Integer;
public
//Owner: TCustomComboBox;
callback: IComboboxCallBack;
list: TCocoaComboBoxList;
resultNS: NSString; //use to return values to combo
lastSelectedItemIndex: Integer; // -1 means invalid or none selected
isOwnerDrawn: Boolean;
isOwnerMeasure: Boolean;
isComboBoxEx: Boolean;
function initWithFrame(frameRect: NSRect): id; override;
procedure setFrameSize(newSize: NSSize); override;
procedure dealloc; override;
function lclGetItemHeight( row: Integer ): Integer; message 'lclGetItemHeight:';
function lclGetDefaultItemHeight: Integer; message 'lclGetDefaultItemHeight';
procedure lclSetDefaultItemHeight(itemHeight: Integer); message 'lclSetItemHeight:';
function acceptsFirstResponder: LCLObjCBoolean; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
function lclGetFrameToLayoutDelta: TRect; override;
procedure comboboxAction(sender: id); message 'comboboxAction:';
function stringValue: NSString; override;
// drawing
procedure drawRect(dirtyRect: NSRect); override;
procedure setTextColor(newValue: NSColor); message 'setTextColor:';
function colorTitle(ATitle: NSString): NSAttributedString; message 'colorTitle:';
// mouse
function acceptsFirstMouse(event: NSEvent): LCLObjCBoolean; override;
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure rightMouseDragged(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure otherMouseDragged(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
procedure scrollWheel(event: NSEvent); override;
end;
{ TCocoaSpinEdit }
{$IFDEF COCOA_SPINEDIT_INSIDE_CONTAINER}
TCocoaSpinEdit = objcclass(NSControl)
public
callback: ICommonCallback;
Stepper: NSStepper;
Edit: NSTextField;
Spin: TCustomFloatSpinEdit;
procedure dealloc; override;
procedure UpdateControl(ASpinEdit: TCustomFloatSpinEdit); message 'UpdateControl:';
procedure CreateSubcontrols(ASpinEdit: TCustomFloatSpinEdit; const AParams: TCreateParams); message 'CreateSubControls:AParams:';
procedure PositionSubcontrols(const ALeft, ATop, AWidth, AHeight: Integer); message 'PositionSubcontrols:ATop:AWidth:AHeight:';
procedure CalculateSubcontrolPos(const ASpinLCLBounds: TRect; out AEditBounds, AStepperBounds: TRect); message 'CalculateSubcontrolPos:AEditBounds:AStepperBounds:';
procedure StepperChanged(sender: NSObject); message 'StepperChanged:';
// lcl
function acceptsFirstResponder: Boolean; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
// NSViewFix
function fittingSize: NSSize; override;
end;
{$ELSE}
{ TCocoaSpinEditStepper }
TCocoaSpinEditStepper = objcclass(NSStepper)
callback: ICommonCallback;
function acceptsFirstMouse(event: NSEvent): LCLObjCBoolean; override;
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure rightMouseDragged(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure otherMouseDragged(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
procedure scrollWheel(event: NSEvent); override;
end;
TCocoaSpinEdit = objcclass(TCocoaTextField)
Stepper: NSStepper;
NumberFormatter: NSNumberFormatter;
decimalPlaces: Integer;
avoidChangeEvent: Integer;
anyChange: Boolean;
//Spin: TCustomFloatSpinEdit;
procedure dealloc; override;
function updateStepper: boolean; message 'updateStepper';
procedure UpdateControl(min, max, inc, avalue: double; ADecimalPlaces: Integer; checkMinMax: Boolean); message 'UpdateControl::::::';
procedure lclCreateSubcontrols(const AParams: TCreateParams); message 'lclCreateSubControls:';
procedure lclReleaseSubcontrols; message 'lclReleaseSubcontrols';
procedure PositionSubcontrols(const ALeft, ATop, AWidth, AHeight: Integer); message 'PositionSubcontrols:ATop:AWidth:AHeight:';
procedure StepperChanged(sender: NSObject); message 'StepperChanged:';
procedure textDidChange(notification: NSNotification); override;
procedure textDidEndEditing(notification: NSNotification); override;
// lcl
function acceptsFirstResponder: LCLObjCBoolean; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
procedure lclSetVisible(AVisible: Boolean); override;
procedure lclSetFrame(const r: TRect); override;
// NSViewFix
function fittingSize: NSSize; override;
// mouse
function acceptsFirstMouse(event: NSEvent): LCLObjCBoolean; override;
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure rightMouseDragged(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure otherMouseDragged(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
end;
{$ENDIF}
// these constants are missing from CocoaAll for some reason
const
NSTextAlignmentLeft = 0;
NSTextAlignmentRight = {$ifdef USE_IOS_VALUES}2{$else}1{$endif}; // it's 2 for iOS and family
NSTextAlignmentCenter = {$ifdef USE_IOS_VALUES}1{$else}2{$endif}; // it's 1 for iOS and family
NSTextAlignmentJustified = 3;
NSTextAlignmentNatural = 4;
function GetFieldEditor(afield: NSTextField): TCocoaFieldEditor;
implementation
uses CocoaWSStdCtrls;
function GetFieldEditor(afield: NSTextField): TCocoaFieldEditor;
var
lFieldEditor: TCocoaFieldEditor;
lText: NSText;
window: NSWindow;
begin
Result := nil;
if not Assigned(afield) then Exit;
window := afield.window;
if window = nil then Exit;
{$ifdef BOOLFIX}
lText := window.fieldEditor_forObject_(Ord(True), afield);
{$else}
lText := window.fieldEditor_forObject(True, afield);
{$endif}
if (lText <> nil) and lText.isKindOfClass_(TCocoaFieldEditor) then
begin
Result := TCocoaFieldEditor(lText);
end;
end;
{ TCocoaReadOnlyComboBoxList }
procedure TCocoaReadOnlyComboBoxList.InsertItem(Index: Integer;
const S: string; O: TObject);
var
astr : NSString;
mn : NSMenuItem;
menuItem : TCocoaReadOnlyView;
begin
inherited InsertItem(Index, S, O);
// Adding an item with its final name will cause it to be deleted,
// so we need to first add all items with unique names, and then
// rename all of them, see bug 30847
astr := nsstr('@@@add');
if Index >= FOwner.numberOfItems then
begin
FOwner.addItemWithTitle(astr);
Index := FOwner.numberOfItems-1;
end else
FOwner.insertItemWithTitle_atIndex(astr, Index);
// ItemIndex may be changed in addItemWithTitle() / insertItemWithTitle_atIndex()
// keep compatibility with LCL by resetting ItemIndex
FOwner.selectItemAtIndex(FOwner.lastSelectedItemIndex);
mn := FOwner.itemAtIndex(Index);
if not Assigned(mn) then Exit;
// for TComboBoxEx, the parameter S passed in is always emtpy,
// and NSPopUpButton always automatically sets the itemIndex according to
// the title, so a unique title needs to be set.
if not FOwner.isComboBoxEx then
astr := NSStringUtf8(S)
else
begin
astr := NSStringUtf8(FId.ToString);
inc(FId);
end;
mn.setTitle(astr);
mn.setAttributedTitle( FOwner.colorTitle(astr) );
astr.release;
if FOwner.isOwnerDrawn then
begin
menuItem := TCocoaReadOnlyView.alloc.initWithFrame(
NSMakeRect(0,0, FOwner.frame.size.width, FOwner.lclGetItemHeight(index)) );
menuItem.itemIndex := Index;
menuItem.combobox := FOwner;
mn.setView(menuItem);
menuItem.release;
end;
end;
procedure TCocoaReadOnlyComboBoxList.Put(Index: Integer; const S: string);
var
astr: NSString;
mn : NSMenuItem;
begin
inherited Put(Index, S);
if ((index >= 0) and (Index <= FOwner.numberOfItems)) then
begin
mn := FOwner.itemAtIndex(Index);
astr := NSStringUtf8(S);
mn.setTitle(astr);
astr.release;
end;
end;
procedure TCocoaReadOnlyComboBoxList.UpdateItemSize;
var
menuItem: NSMenuItem;
itemView: TCocoaReadOnlyView;
size: NSSize;
begin
size.width:= FOwner.frame.size.width;
for menuItem in FOwner.menu.itemArray do begin
itemView:= TCocoaReadOnlyView( menuItem.view );
size.height:= itemView.frame.size.height;
itemView.setFrameSize( size );
end;
end;
constructor TCocoaReadOnlyComboBoxList.Create(AOwner: TCocoaReadOnlyComboBox);
begin
inherited Create;
FOwner := AOwner;
end;
procedure TCocoaReadOnlyComboBoxList.Delete(Index: Integer);
begin
inherited Delete(Index);
if (Index>=0) and (Index < FOwner.numberOfItems) then
FOwner.removeItemAtIndex(Index);
end;
procedure TCocoaReadOnlyComboBoxList.Clear;
begin
inherited Clear;
FOwner.removeAllItems;
end;
{ TCococaFieldEditorExt }
function TCococaFieldEditorExt.lclGetCallback: ICommonCallback;
begin
if isFieldEditor and Assigned(delegate) then
begin
Result := NSObject(delegate).lclGetCallback;
end
else
Result := inherited lclGetCallback;
end;
{ TCocoaSpinEditStepper }
function TCocoaSpinEditStepper.acceptsFirstMouse(event: NSEvent): LCLObjCBoolean;
begin
Result := NSViewCanFocus(Self);
end;
procedure TCocoaSpinEditStepper.mouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
begin
inherited mouseDown(event);
if Assigned(Callback) then
callback.MouseUpDownEvent(event, true);
end;
end;
procedure TCocoaSpinEditStepper.mouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited mouseUp(event);
end;
procedure TCocoaSpinEditStepper.rightMouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaSpinEditStepper.rightMouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaSpinEditStepper.rightMouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDragged(event);
end;
procedure TCocoaSpinEditStepper.otherMouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaSpinEditStepper.otherMouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaSpinEditStepper.otherMouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited otherMouseDragged(event);
end;
procedure TCocoaSpinEditStepper.mouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaSpinEditStepper.mouseMoved(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
procedure TCocoaSpinEditStepper.scrollWheel(event: NSEvent);
begin
if not Assigned(callback) or not callback.scrollWheel(event) then
inherited scrollWheel(event);
end;
{ TCocoaReadOnlyView }
procedure TCocoaReadOnlyView.drawRect(dirtyRect: NSRect);
var
ctx : TCocoaContext;
ctxRect: TRect;
isHighlighted: Boolean;
begin
inherited drawRect(dirtyRect);
if not Assigned(combobox) then Exit;
isHighlighted:= self.combobox.itemAtIndex(itemIndex).isHighlighted;
ctx := TCocoaContext.Create(NSGraphicsContext.currentContext);
try
ctxRect:= NSRectToRect( bounds );
ctx.InitDraw( ctxRect.Width, ctxRect.Height );
combobox.callback.ComboBoxDrawItem(itemIndex, ctx, ctxRect, isHighlighted, false);
finally
ctx.Free;
end;
end;
procedure TCocoaReadOnlyView.mouseUp(event: NSEvent);
begin
inherited mouseUp(event);
if Assigned(combobox) then
begin
combobox.selectItemAtIndex(itemIndex);
combobox.menu.cancelTracking;
end;
end;
{ TCocoaComboBoxItemCell }
procedure TCocoaComboBoxItemCell.drawWithFrame_inView(cellFrame: NSRect; controlView_: NSView);
begin
inherited drawWithFrame_inView(cellFrame, controlView_);
end;
function TCocoaComboBoxCell.tableView_dataCellForTableColumn_row(tableView: NSTableView; tableColumn: NSTableColumn; row: NSInteger): NSCell;
begin
Result := TCocoaComboBoxItemCell.alloc.initTextCell(NSString.string_);
end;
{
procedure TCocoaComboBoxCell.tableView_willDisplayCell_forTableColumn_row(
tableView: NSTableView; cell: id; tableColumn: NSTableColumn; row: NSInteger);
var
sz : NSSize;
pr : NSView;
frm : NSRect;
begin
writeln('will display ', row);
if row = 0 then
begin
sz := tableView.frame.size;
sz.width := 300;
tableView.setFrameSize(sz);
pr := tableView;
while Assigned(pr) do begin
writeln(pr.lclClassname);
pr := pr.superview;
end;
writeln('at 10: ', tableView.window.lclClassName);
writeln('max size = ', tableView.window.maxSize.width:0:0);
writeln('min size = ', tableView.window.minSize.width:0:0);
frm := tableView.window.frame;
writeln(' size = ', frm.size.width:0:0);
frm := NSView(tableView.window.contentView).frame;
writeln('clt size = ', frm.size.width:0:0);
frm.size.width := 96 * 2; //frm.size.width * 2;
tableView.window.setContentSize(frm.size);
writeln('clt size = ', frm.size.width:0:0);
end;
end;
}
{function TCocoaComboBoxCell.tableView_heightOfRow(tableView: NSTableView;
row: NSInteger): CGFloat;
begin
writeln('height of row ', row);
Result := 32;
end;}
{ TCocoaFieldEditor }
function GetEditBox(src: TCocoaFieldEditor): NSView;
var
v : NSObject;
begin
Result := nil;
if not Assigned(src) then Exit;
v := NSObject(src.delegate);
if Assigned(v) and (v.isKindOfClass(NSView)) then
Result := NSView(v);
end;
function TCocoaFieldEditor.lclGetCallback: ICommonCallback;
begin
if Assigned(delegate) then Result := NSObject(delegate).lclGetCallback
else Result := nil;
end;
function ReverseColor(clr: NSColor): NSColor;
var
r,g,b: byte;
begin
r := $FF xor byte(Round(clr.redComponent * 255));
g := $FF xor byte(Round(clr.greenComponent * 255));
b := $FF xor byte(Round(clr.blueComponent * 255));
Result := NSColor.colorWithDeviceRed_green_blue_alpha(r / 255, g / 255, b / 255, 1);
end;
function TCocoaFieldEditor.becomeFirstResponder: LCLObjCBoolean;
begin
if goingReadOnly then Result := false
else Result:=inherited becomeFirstResponder;
end;
procedure TCocoaFieldEditor.setDelegate(adelegate: NSTextDelegateProtocol);
begin
inherited setDelegate(adelegate);
lclReviseCursorColor;
end;
procedure TCocoaFieldEditor.lclReviseCursorColor;
var
clr :NSColor;
begin
if not Assigned(delegate) then Exit;
if not (NSObject(delegate).isKindOfClass(NSTextField)) then Exit;
clr := NSTextField(delegate).backgroundColor.colorUsingColorSpace(NSColorSpace.deviceRGBColorSpace);
if Assigned(clr) then
setInsertionPointColor(ReverseColor(clr));
end;
procedure TCocoaFieldEditor.cut(sender: id);
var
callback: ICommonCallback;
accept: Boolean = False;
begin
callback:= self.lclGetCallback;
if Assigned(callback) then
accept:= callback.SendOnEditCut;
if accept then
inherited cut(sender);
end;
procedure TCocoaFieldEditor.paste(sender: id);
var
callback: ICommonCallback;
accept: Boolean = False;
begin
callback:= self.lclGetCallback;
if Assigned(callback) then
accept:= callback.SendOnEditPaste;
if accept then
inherited paste(sender);
end;
procedure TCocoaFieldEditor.insertNewline(sender: id);
begin
// 10.6 cocoa handles the editors Return key as "insertNewLine" command (that makes sense)
// which turns into textDidEndEditing done command (that also makes sense)
// however, it ends up in an endless loop of "end-editing" calls.
//
// todo: find the reason for the endless loop and resolve it properly
end;
procedure TCocoaFieldEditor.mouseDown(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.MouseUpDownEvent(event) then
begin
inherited mouseDown(event);
// NSTextView runs internal mouse-tracking loop in it's mouseDown implemenation.
// Thus "inherited mouseDown" only returns after the mouse has been released.
// why is TCocoaTextView not affected?
if Assigned(v) and Assigned(v.lclGetCallback) then
v.lclGetCallback.MouseUpDownEvent(event, true);
end;
end else
inherited mouseDown(event);
end;
procedure TCocoaFieldEditor.mouseUp(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.MouseUpDownEvent(event) then
inherited mouseUp(event);
end else
inherited mouseUp(event);
end;
procedure TCocoaFieldEditor.rightMouseDown(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end else
inherited rightMouseDown(event);
end;
procedure TCocoaFieldEditor.rightMouseUp(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end else
inherited rightMouseUp(event);
end;
procedure TCocoaFieldEditor.otherMouseDown(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end else
inherited otherMouseDown(event);
end;
procedure TCocoaFieldEditor.otherMouseUp(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end else
inherited otherMouseUp(event);
end;
procedure TCocoaFieldEditor.mouseDragged(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.MouseMove(event) then
inherited mouseDragged(event);
end else
inherited mouseDragged(event);
end;
procedure TCocoaFieldEditor.mouseMoved(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.MouseMove(event) then
inherited mouseMoved(event);
end else
inherited mouseMoved(event);
end;
procedure TCocoaFieldEditor.scrollWheel(event: NSEvent);
var
v : NSView;
begin
v := GetEditBox(Self);
if Assigned(v) then
begin
if Assigned(v.lclGetCallback) and not v.lclGetCallback.scrollWheel(event) then
inherited mouseMoved(event);
end else
inherited scrollWheel(event);
end;
{ TCocoaTextField }
function TCocoaTextField.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := NSViewCanFocus(Self);
end;
function TCocoaTextField.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaTextField.lclClearCallback;
begin
callback := nil;
end;
procedure TCocoaTextField.textDidChange(notification: NSNotification);
begin
if (maxLength>0) and Assigned(stringValue) and (stringValue.length > maxLength) then
setStringValue(stringValue.substringWithRange(NSMakeRange(0,maxLength)));
if callback <> nil then
callback.SendOnTextChanged;
end;
// detect and remove line-break when entering text
// TextField (TEdit) should be single line
function TCocoaTextField.textView_shouldChangeTextInRange_replacementString (textView: NSTextView; affectedCharRange: NSRange; replacementString: NSString): ObjCBOOL;
var
newString: NSString; // need not release
begin
Result:= true;
newString:= NSStringRemoveLineBreak( replacementString );
if newString.length <> replacementString.length then
begin
// only handled if there is line-break in replacementString
// use insertText() for undo/redo support
// textDidChange will be called in insertText()
if newString.length>0 then currentEditor.insertText( newString );
Result:= false;
end;
end;
procedure TCocoaTextField.mouseDown(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
begin
inherited mouseDown(event);
// the text selection is handled withing mouseDown
if Assigned(callback) and isSelectable then
callback.MouseUpDownEvent(event, True);
end;
end;
procedure TCocoaTextField.mouseUp(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
begin
inherited mouseUp(event);
end;
end;
procedure TCocoaTextField.rightMouseDown(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaTextField.rightMouseUp(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaTextField.otherMouseDown(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaTextField.otherMouseUp(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaTextField.mouseDragged(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaTextField.mouseMoved(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
procedure TCocoaTextField.scrollWheel(event: NSEvent);
begin
if Assigned(callback) and not callback.scrollWheel(event) then
inherited scrollWheel(event);
end;
procedure TCocoaTextField.lclSetMaxLength(amax: integer);
begin
maxLength := amax;
end;
{ TCocoaTextView }
procedure TCocoaTextView.changeColor(sender: id);
begin
//preventing text color from being changed
//inherited changeColor(sender);
end;
procedure TCocoaTextView.cut(sender: id);
var
accept: Boolean = False;
begin
if Assigned(callback) then
accept:= callback.SendOnEditCut;
if accept then
inherited cut(sender);
end;
procedure TCocoaTextView.paste(sender: id);
var
accept: Boolean = False;
begin
if Assigned(callback) then
accept:= callback.SendOnEditPaste;
if accept then
inherited paste(sender);
end;
procedure TCocoaTextView.dealloc;
begin
if Assigned(FUndoManager) then
FUndoManager.release;
inherited dealloc;
end;
function TCocoaTextView.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := NSViewCanFocus(Self);
end;
function TCocoaTextView.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaTextView.lclClearCallback;
begin
callback := nil;
end;
procedure TCocoaTextView.insertNewline(sender: id);
begin
if wantReturns then
inherited insertNewline(sender);
end;
procedure TCocoaTextView.mouseDown(event: NSEvent);
begin
if Assigned(callback) then
begin
if not callback.MouseUpDownEvent(event) then
begin
inherited mouseDown(event);
// Cocoa doesn't call mouseUp for NSTextView, so we have to emulate it here :(
// See bug 29000
if Assigned(callback) then
callback.MouseUpDownEvent(event, True);
end;
end else
inherited mouseDown(event);
end;
procedure TCocoaTextView.mouseUp(event: NSEvent);
begin
if callback <> nil then
callback.MouseUpDownEvent(event);
inherited mouseUp(event);
end;
procedure TCocoaTextView.rightMouseDown(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaTextView.rightMouseUp(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaTextView.otherMouseDown(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaTextView.otherMouseUp(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaTextView.mouseDragged(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaTextView.mouseEntered(event: NSEvent);
begin
inherited mouseEntered(event);
end;
procedure TCocoaTextView.mouseExited(event: NSEvent);
begin
inherited mouseExited(event);
end;
procedure TCocoaTextView.mouseMoved(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
function TCocoaTextView.lclIsEnabled: Boolean;
begin
Result := FEnabled;
end;
procedure TCocoaTextView.lclSetEnabled(AEnabled: Boolean);
begin
FEnabled := AEnabled;
end;
procedure TCocoaTextView.textDidChange(notification: NSNotification);
begin
if (callback <> nil) and (supressTextChangeEvent = 0) then
callback.SendOnTextChanged;
end;
procedure TCocoaTextView.lclExpectedKeys(var wantTabs, wantArrows, wantReturn,
wantAll: Boolean);
begin
wantTabs := true;
wantArrows := true;
wantReturn := true;
wantAll := true;
end;
function TCocoaTextView.undoManagerForTextView(view: NSTextView): NSUndoManager;
begin
if not Assigned(FUndoManager) then
FUndoManager := NSUndoManager.alloc.init;
Result := FUndoManager;
end;
{ TCocoaSecureTextField }
function TCocoaSecureTextField.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := NSViewCanFocus(Self);
end;
function TCocoaSecureTextField.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaSecureTextField.lclClearCallback;
begin
callback := nil;
end;
procedure TCocoaSecureTextField.textDidChange(notification: NSNotification);
begin
inherited;
if (maxLength>0) and Assigned(stringValue) and (stringValue.length > maxLength) then
setStringValue(stringValue.substringWithRange(NSMakeRange(0,maxLength)));
if callback <> nil then
callback.SendOnTextChanged;
end;
procedure TCocoaSecureTextField.mouseDown(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
begin
inherited mouseDown(event);
if Assigned(callback) then
callback.MouseUpDownEvent(event, True);
end;
end;
procedure TCocoaSecureTextField.mouseUp(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited mouseUp(event);
end;
procedure TCocoaSecureTextField.rightMouseDown(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaSecureTextField.rightMouseUp(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaSecureTextField.otherMouseDown(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaSecureTextField.otherMouseUp(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaSecureTextField.mouseDragged(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaSecureTextField.mouseMoved(event: NSEvent);
begin
if Assigned(callback) and not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
procedure TCocoaSecureTextField.scrollWheel(event: NSEvent);
begin
if Assigned(callback) and not callback.scrollWheel(event) then
inherited scrollWheel(event);
end;
procedure TCocoaSecureTextField.lclSetMaxLength(amax: integer);
begin
MaxLength := amax;
end;
{ TCocoaEditComboBoxList }
procedure TCocoaEditComboBoxList.InsertItem(Index: Integer; const S: string;
O: TObject);
begin
inherited InsertItem(Index, S, O);
FOwner.noteNumberOfItemsChanged;
end;
procedure TCocoaEditComboBoxList.Changed;
begin
inherited Changed;
FOwner.reloadData;
end;
constructor TCocoaEditComboBoxList.Create(AOwner: TCocoaComboBox);
begin
inherited Create;
FOwner := AOwner;
end;
{ TCocoaComboBox }
procedure TCocoaComboBox.setStringValue(avalue: NSString);
var
ch : Boolean;
s : NSString;
begin
s := stringValue;
ch := (Assigned(s)
and Assigned(avalue)
and (s.compare(avalue) <> NSOrderedSame));
inherited setStringValue(avalue);
if ch and userSel and Assigned(callback) then
callback.SendOnChange;
end;
function TCocoaComboBox.lclGetFrameToLayoutDelta: TRect;
begin
// todo: on 10.7 or later there's a special API for that!
// The data is received from 10.6 Interface Builder
case NSCell(Self.Cell).controlSize of
NSSmallControlSize: begin
Result.Left := 0;
Result.Top := 1;
Result.Right := -3;
Result.Bottom := -4;
end;
NSMiniControlSize: begin
Result.Left := 0;
Result.Top := 1;
Result.Right := -2;
Result.Bottom := -4;
end;
else
// NSRegularControlSize
Result.Left := 0;
Result.Top := 2;
Result.Right := -3;
Result.Bottom := -4;
end;
end;
function TCocoaComboBox.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := NSViewCanFocus(Self);
end;
procedure TCocoaComboBox.textDidChange(notification: NSNotification);
begin
inherited textDidChange(notification);
if Assigned(callback) then
callback.SendOnChange;
end;
function TCocoaComboBox.comboBox_objectValueForItemAtIndex_(combo:TCocoaComboBox;
row: NSInteger):id;
var
ns : NSString;
begin
if not Assigned(list) or (row<0) or (row>=list.Count) then
Result:=nil
else
begin
ns := NSStringUtf8(list[row]);
Result := ns;
ns.autorelease;
end;
end;
function TCocoaComboBox.comboBox_indexOfItemWithStringValue(
aComboBox: NSComboBox; string_: NSString): NSUInteger;
var
idx : integer;
lclString: String;
lclCmb : TObject;
begin
idx := indexOfSelectedItem;
lclString := string_.UTF8String;
lclCmb := lclGetTarget;
if (idx>=0) and (idx<list.Count) and (list[idx]=lclString) then
// this is used for the case of the same items in the combobox
Result:=idx
else
begin
idx := TCocoaWSCustomComboBox.GetObjectItemIndex(lclCmb);
if idx<0 then
Result := NSNotFound
else
begin
// ComboBox.Text will be set to the List Item value after comboBox_indexOfItemWithStringValue()
// so if cbactRetainPrefixCase set, ComboBox.Text should be reset Async
TComboBoxAsyncHelper.ResetTextIfNecessary(self, lclString);
Result := idx;
end;
end;
end;
function TCocoaComboBox.numberOfItemsInComboBox(combo:TCocoaComboBox):NSInteger;
begin
if not Assigned(list) then Result:=0
else Result:=list.Count;
end;
procedure TCocoaComboBox.dealloc;
begin
FreeAndNil( list );
if Assigned(resultNS) then resultNS.release;
inherited dealloc;
end;
function TCocoaComboBox.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaComboBox.lclClearCallback;
begin
callback := nil;
end;
procedure TCocoaComboBox.comboBoxWillPopUp(notification: NSNotification);
begin
self.setCompletes( TCocoaWSCustomComboBox.GetObjectAutoComplete(lclGetTarget) );
callback.ComboBoxWillPopUp;
isDown:=true;
end;
procedure TCocoaComboBox.comboBoxWillDismiss(notification: NSNotification);
begin
self.setCompletes(false);
callback.ComboBoxWillDismiss;
isDown:=false;
end;
procedure TCocoaComboBox.comboBoxSelectionDidChange(notification: NSNotification);
var
txt : NSString;
begin
txt := comboBox_objectValueForItemAtIndex_(self, indexOfSelectedItem);
if Assigned(txt) then setStringValue( txt );
if userSel then
callback.ComboBoxSelectionDidChange;
userSel := false;
end;
procedure TCocoaComboBox.comboBoxSelectionIsChanging(notification: NSNotification);
begin
userSel := true;
callback.ComboBoxSelectionIsChanging;
end;
function TCocoaComboBox.acceptsFirstMouse(event: NSEvent): LCLObjCBoolean;
begin
Result:=true;
end;
procedure TCocoaComboBox.mouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited mouseDown(event);
end;
procedure TCocoaComboBox.mouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited mouseUp(event);
end;
procedure TCocoaComboBox.rightMouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaComboBox.rightMouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaComboBox.rightMouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDragged(event);
end;
procedure TCocoaComboBox.otherMouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaComboBox.otherMouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaComboBox.otherMouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited otherMouseDragged(event);
end;
procedure TCocoaComboBox.mouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaComboBox.mouseMoved(event: NSEvent);
begin
// NSMouseMove event is being sent even after the selection is made.
// In Cocoa world, the event is sent to the comboBox NSTextView edit section.
// (even is the cursor is NOT over the NSTextView itself, but rather the popup window)
//
// The CocoaWS forwards the event to LCL. And LCL recognizes the event as a mouse action
// beyond combobox boundries (it's NSMouseMove and not NSMouseDrag).
// causing the issues with Object Inspector (where the mouse move with a left button down)
// is recognized as a switch to a different row.
//
// WinAPI doesn't send MouseMove events when popup is dropped.
// Enforcing the same approach for Cocoa. If combobox is showing popup
// all mousemoves are suppressed
if isDown then Exit;
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
procedure TCocoaComboBox.scrollWheel(event: NSEvent);
begin
if not Assigned(callback) or not callback.scrollWheel(event) then
inherited scrollWheel(event);
end;
{ TCocoaReadOnlyComboBox }
function TCocoaReadOnlyComboBox.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := NSViewCanFocus(Self);
end;
function TCocoaReadOnlyComboBox.initWithFrame(frameRect: NSRect): id;
begin
Result:=inherited initWithFrame(frameRect);
_defaultItemHeight:= CocoaConfigComboBox.readOnly.item.defaultHeight;
_menuDelegate:= TCocoaReadOnlyComboBoxMenuDelegate.new;
_menuDelegate._comboBox:= self;
self.menu.setDelegate( _menuDelegate );
end;
procedure TCocoaReadOnlyComboBox.setFrameSize(newSize: NSSize);
begin
inherited setFrameSize(newSize);
TCocoaReadOnlyComboBoxList(self.list).UpdateItemSize;
end;
procedure TCocoaReadOnlyComboBox.dealloc;
begin
FreeAndNil( list );
_menuDelegate.release;
_textColorAttribs.release;
if resultNS <> nil then resultNS.release;
inherited dealloc;
end;
function TCocoaReadOnlyComboBox.lclGetDefaultItemHeight: Integer;
begin
Result:= _defaultItemHeight;
end;
function TCocoaReadOnlyComboBox.lclGetItemHeight( row: Integer ): Integer;
begin
if self.isOwnerMeasure and Assigned(self.callback) then
self.callback.GetRowHeight( row, Result )
else
Result:= _defaultItemHeight;
end;
procedure TCocoaReadOnlyComboBox.lclSetDefaultItemHeight(itemHeight: Integer);
begin
if itemHeight <= 0 then
_defaultItemHeight:= CocoaConfigComboBox.readOnly.item.defaultHeight
else
_defaultItemHeight:= itemHeight;
end;
function TCocoaReadOnlyComboBox.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaReadOnlyComboBox.lclClearCallback;
begin
callback := nil;
end;
function TCocoaReadOnlyComboBox.lclGetFrameToLayoutDelta: TRect;
begin
// todo: on 10.7 or later there's a special API for that!
// The data is received from 10.6 Interface Builder
case NSCell(Self.Cell).controlSize of
NSSmallControlSize: begin
Result.Left := 3;
Result.Top := 1;
Result.Right := -3;
Result.Bottom := -4;
end;
NSMiniControlSize: begin
Result.Left := 1;
Result.Top := 0;
Result.Right := -2;
Result.Bottom := 0;
end;
else
// NSRegularControlSize
Result.Left := 3;
Result.Top := 2;
Result.Right := -3;
Result.Bottom := -4;
end;
end;
procedure TCocoaReadOnlyComboBox.comboboxAction(sender: id);
begin
//setTitle(NSSTR(PChar(Format('%d=%d', [indexOfSelectedItem, lastSelectedItemIndex])))); // <= for debugging
if Assigned(callback) then
callback.SendOnChange;
if (indexOfSelectedItem <> lastSelectedItemIndex) and (callback <> nil) then
callback.ComboBoxSelectionDidChange;
lastSelectedItemIndex := indexOfSelectedItem;
end;
function TCocoaReadOnlyComboBox.stringValue: NSString;
begin
if Assigned(selectedItem) then
Result:=selectedItem.title
else
Result:=NSString.string_;
end;
procedure TCocoaReadOnlyComboBox.drawRect(dirtyRect: NSRect);
var
ctx : TCocoaContext;
r : NSRect;
rr : NSRect;
dr : TRect;
t : NSString;
begin
if isOwnerDrawn then
begin
t := title;
if Assigned(t) then t.retain;
setTitle(NSString.string_);
end else
t := nil;
inherited drawRect(dirtyRect);
if Assigned(t) then
begin
setTitle(t);
t.release;
end;
// if ownerDrawn style, then need to call "DrawItem" event
if isOwnerDrawn and Assigned(callback)
and (lastSelectedItemIndex>=0) and (lastSelectedItemIndex<list.Count)
then
begin
ctx := TCocoaContext.Create(NSGraphicsContext.currentContext);
try
// todo: it's possible to query "cell" using titleRectForBounds method
// it actually returns somewhat desired offsets.
// (however, one should be careful and take layout offsets into account!)
// on the other hand, "cells" themselves are being deprecated...
dr := lclFrame;
// crop the drawing rectangle according to the
// rounded corners and popup button of the ComboBox.
// the APP can get better effect by drawing in the cropped rectangle.
// the APP can also expand the rectangle if it knows what it is doing.
Types.OffsetRect(dr, -dr.Left, -dr.Top);
inc( dr.Left, COMBOBOX_RO_ROUND_SIZE );
inc( dr.Top, 2 );
dec( dr.Right, COMBOBOX_RO_BUTTON_WIDTH );
inc( dr.Bottom, 1 );
ctx.InitDraw(dr.Width, dr.Height);
callback.ComboBoxDrawItem(lastSelectedItemIndex, ctx, dr, False, True);
finally
ctx.Free;
end;
end;
end;
procedure TCocoaReadOnlyComboBox.setTextColor(newValue: NSColor);
var
item: NSMenuItem;
begin
if Assigned(_textColorAttribs) then
_textColorAttribs.release;
_textColorAttribs:= NSMutableDictionary.alloc.initWithCapacity(1);
_textColorAttribs.setValue_forKey( newValue, NSForegroundColorAttributeName );
for item in self.itemArray do begin
item.setAttributedTitle( self.colorTitle(item.title) );
end;
end;
function TCocoaReadOnlyComboBox.colorTitle(ATitle: NSString
): NSAttributedString;
begin
Result:= NSMutableAttributedString.alloc.initWithString_attributes(
ATitle,
_textColorAttribs );
Result.autorelease;
end;
function TCocoaReadOnlyComboBox.acceptsFirstMouse(event: NSEvent): LCLObjCBoolean;
begin
Result:=true;
end;
procedure TCocoaReadOnlyComboBox.mouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
begin
// a typical Apple "mouseDown" loop. The popup is shown on mouseDown event
// The event only exists, whenever the popup is closed (for whatever reason)
if Assigned(callback) then callback.ComboBoxWillPopUp;
inherited mouseDown(event);
if Assigned(callback) then callback.ComboBoxWillDismiss;
callback.MouseUpDownEvent(event, true);
end;
end;
procedure TCocoaReadOnlyComboBox.mouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited mouseUp(event);
end;
procedure TCocoaReadOnlyComboBox.rightMouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaReadOnlyComboBox.rightMouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaReadOnlyComboBox.rightMouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDragged(event);
end;
procedure TCocoaReadOnlyComboBox.otherMouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaReadOnlyComboBox.otherMouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaReadOnlyComboBox.otherMouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited otherMouseDragged(event);
end;
procedure TCocoaReadOnlyComboBox.mouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaReadOnlyComboBox.mouseMoved(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
procedure TCocoaReadOnlyComboBox.scrollWheel(event: NSEvent);
begin
if not Assigned(callback) or not callback.scrollWheel(event) then
inherited scrollWheel(event);
end;
{ TCocoaReadOnlyComboBoxMenuDelegate }
procedure TCocoaReadOnlyComboBoxMenuDelegate.menu_willHighlightItem(
menu: NSMenu; item: NSMenuItem);
begin
if Assigned(_lastHightlightItem) then
_lastHightlightItem.view.setNeedsDisplay_( True );
_lastHightlightItem:= item;
end;
procedure TCocoaReadOnlyComboBoxMenuDelegate.menuDidClose(menu: NSMenu);
begin
TCocoaReadOnlyComboBox(_comboBox).comboboxAction( nil );
end;
{ TCocoaSpinEdit }
{$IFDEF COCOA_SPINEDIT_INSIDE_CONTAINER}
procedure TCocoaSpinEdit.dealloc;
begin
if Stepper <> nil then
Stepper.release;
if Edit <> nil then
Edit.release;
inherited dealloc;
end;
procedure TCocoaSpinEdit.UpdateControl(ASpinEdit: TCustomFloatSpinEdit);
begin
Stepper.setMaxValue(ASpinEdit.MaxValue);
Stepper.setMinValue(ASpinEdit.MinValue);
Stepper.setIncrement(ASpinEdit.Increment);
Stepper.setDoubleValue(ASpinEdit.Value);
// update the UI too
StepperChanged(Self);
end;
procedure TCocoaSpinEdit.CreateSubcontrols(ASpinEdit: TCustomFloatSpinEdit; const AParams: TCreateParams);
var
lParams: TCreateParams;
lEditRect, lStepperRect: TRect;
begin
{$IFDEF COCOA_SPIN_DEBUG}
WriteLn('[TCocoaSpinEdit.CreateSubcontrols]');
{$ENDIF}
Spin := ASpinEdit;
CalculateSubcontrolPos(Types.Bounds(AParams.X, AParams.Y, AParams.Width,
AParams.Height), lEditRect, lStepperRect);
// Now creates the subcontrols
lParams := AParams;
lParams.WndParent := HWND(Self);
lParams.Style := AParams.Style or WS_VISIBLE;
// Stepper
lParams.X := lStepperRect.Left;
lParams.Y := lStepperRect.Top;
lParams.Width := lStepperRect.Right - lStepperRect.Left;
lParams.Height := lStepperRect.Bottom - lStepperRect.Top;
Stepper := NSStepper.alloc.lclInitWithCreateParams(lParams);
Stepper.setValueWraps(False);
// Edit
lParams.X := lEditRect.Left;
lParams.Y := lEditRect.Top;
lParams.Width := lEditRect.Right - lEditRect.Left;
lParams.Height := lEditRect.Bottom - lEditRect.Top;
Edit := NSTextField.alloc.lclInitWithCreateParams(lParams);
// Change event for the stepper
Stepper.setTarget(Self);
Stepper.setAction(objcselector('StepperChanged:'));
end;
procedure TCocoaSpinEdit.PositionSubcontrols(const ALeft, ATop, AWidth, AHeight: Integer);
var
lNSStepperRect, lRect: NSRect;
lStepperRect, lEditRect: TRect;
begin
{$IFDEF COCOA_SPIN_DEBUG}
WriteLn('[TCocoaSpinEdit.PositionSubcontrols] AHeight=', AHeight);
{$ENDIF}
CalculateSubcontrolPos(Types.Bounds(ALeft, ATop, AWidth, AHeight), lEditRect, lStepperRect);
// Stepper
LCLToNSRect(lStepperRect, AHeight, lNSStepperRect);
Stepper.setBounds(lNSStepperRect);
// Edit
LCLToNSRect(lEditRect, AHeight, lRect);
Edit.setBounds(lRect);
{$IFDEF COCOA_SPIN_DEBUG}
WriteLn(':<[TCocoaSpinEdit.PositionSubcontrols] Edit=> X=', lRect.origin.x,
' Y=', lRect.origin.y, ' W=', lRect.size.width, ' H=', lRect.size.height,
' Stepper X=', lNSStepperRect.origin.x, ' Y=', lNSStepperRect.origin.y,
' W=', lNSStepperRect.size.width, ' H=', lNSStepperRect.size.height,
' frame.size.height=', frame.size.height);
{$ENDIF}
end;
procedure TCocoaSpinEdit.CalculateSubcontrolPos(
const ASpinLCLBounds: TRect; out AEditBounds, AStepperBounds: TRect);
var
lWidth, lHeight: Integer;
begin
lWidth := ASpinLCLBounds.Right - ASpinLCLBounds.Left;
lHeight := ASpinLCLBounds.Bottom - ASpinLCLBounds.Top;
// Stepper
AStepperBounds.Left := lWidth - SPINEDIT_DEFAULT_STEPPER_WIDTH;
AStepperBounds.Top := SPINEDIT_EDIT_SPACING_FOR_SELECTION;
AStepperBounds.Right := lWidth;
AStepperBounds.Bottom := lHeight - SPINEDIT_EDIT_SPACING_FOR_SELECTION;
// Edit
AEditBounds.Left := SPINEDIT_EDIT_SPACING_FOR_SELECTION;
AEditBounds.Top := SPINEDIT_EDIT_SPACING_FOR_SELECTION;
AEditBounds.Right := lWidth - SPINEDIT_DEFAULT_STEPPER_WIDTH;
AEditBounds.Bottom := lHeight - SPINEDIT_EDIT_SPACING_FOR_SELECTION;
{$IFDEF COCOA_SPIN_DEBUG}
WriteLn('[TCocoaSpinEdit.CalculateSubcontrolPos] lWidth=', lWidth, ' lHeight=', lHeight,
' Stepper.Left=', AStepperBounds.Left, ' Stepper.Top=', AStepperBounds.Top,
' Stepper.Right=', AStepperBounds.Right, ' Stepper.Bottom=', AStepperBounds.Bottom,
' Edit.Left=', AEditBounds.Left, ' Edit.Top=', AEditBounds.Top,
' Edit.Right=', AEditBounds.Right, ' Edit.Bottom=', AEditBounds.Bottom
);
{$ENDIF}
end;
procedure TCocoaSpinEdit.StepperChanged(sender: NSObject);
var
lNSStr: NSString;
lStr: string;
begin
lStr := Format('%.*f', [Spin.DecimalPlaces, Stepper.doubleValue()]);
lNSStr := CocoaUtils.NSStringUtf8(lStr);
Edit.setStringValue(lNSStr);
lNSStr.release;
// This implements OnChange for both user and code changes
if callback <> nil then callback.SendOnTextChanged();
end;
function TCocoaSpinEdit.acceptsFirstResponder: Boolean;
begin
Result := True;
end;
function TCocoaSpinEdit.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaSpinEdit.lclClearCallback;
begin
callback := nil;
end;
function TCocoaSpinEdit.fittingSize: NSSize;
begin
Result.width := -1;
Edit.sizeToFit();
Result.height := Edit.bounds.size.height + SPINEDIT_EDIT_SPACING_FOR_SELECTION * 2;
{$IFDEF COCOA_SPIN_DEBUG}
WriteLn('[TCocoaSpinEdit.fittingSize] width=', Result.width,
' height=', Result.height);
{$ENDIF}
end;
{$ELSE}
procedure TCocoaSpinEdit.dealloc;
begin
lclReleaseSubControls;
inherited dealloc;
end;
function TCocoaSpinEdit.updateStepper: boolean;
var
lValid: Boolean = False;
lValue: String;
lFloat: Double;
begin
lValue := CocoaUtils.NSStringToString(stringValue());
lValid := SysUtils.TryStrToFloat(lValue, lFloat);
if lValid then
begin
Stepper.setDoubleValue(lFloat);
Result := true;
end else
Result := false;
end;
// * value - the current value to be selected. Note: it will be adjusted
// to "min" and "max", if "checkMinMax" is set to true
// * min, max - minimum maximum values allowed. for NSStepper, there's no "unlimited"
// check. "Something" should be set as minimum and maximum
// * checkMinMax - indicates if the min and max values should be verified.
// in LCL is Min = Max then there's not boundries check applied
// Since "min" and "max" are doubles, it's safer to use a booleanFlag
// so the comparison is done by the caller, rather than using approximation
// * inc - incremeantal value to be used when "up" or "down" are pressed
// * DecimalPlaces - the number of decimals to used when showing the value
procedure TCocoaSpinEdit.UpdateControl(min, max, inc, avalue: double; ADecimalPlaces: Integer; checkMinMax: Boolean);
var
notifyChange : Boolean;
v : double;
begin
Stepper.setIncrement(inc);
v := avalue;
if checkMinMax then
begin
if v < min then v := min
else if v > max then v := max;
end;
notifyChange := (v <> Stepper.doubleValue) or (decimalPlaces <> ADecimalPlaces);
// set min/max after checking for notify change
// .doubleValue would be adjusted by setting min/max
decimalPlaces := ADecimalPlaces;
if checkMinMax then begin
Stepper.setMinValue(min);
Stepper.setMaxValue(max);
end else begin
// "Math" unit declared MaxDouble and MinDouble constants
// however, using setMinValue to MinDouble, seems to be ignored
// and Cocoa falls back to the default "0". not sure why.
// Instead using -MaxDouble.
Stepper.setMinValue(-MaxDouble);
Stepper.setMaxValue(+MaxDouble);
end;
if notifychange then
begin
Stepper.setDoubleValue(v);
StepperChanged(Self);
end;
end;
procedure TCocoaSpinEdit.lclCreateSubcontrols(const AParams: TCreateParams);
var
lParams: TCreateParams;
begin
{$IFDEF COCOA_SPIN_DEBUG}
WriteLn('[TCocoaSpinEdit.CreateSubcontrols]');
{$ENDIF}
// Now creates the subcontrols
lParams := AParams;
//lParams.Style := AParams.Style or WS_VISIBLE;
// Stepper
lParams.X := AParams.X + AParams.Width - SPINEDIT_DEFAULT_STEPPER_WIDTH;
lParams.Width := SPINEDIT_DEFAULT_STEPPER_WIDTH;
Stepper := TCocoaSpinEditStepper.alloc.lclInitWithCreateParams(lParams);
TCocoaSpinEditStepper(Stepper).callback := callback;
Stepper.setValueWraps(False);
// Change event for the stepper
Stepper.setTarget(Self);
Stepper.setAction(objcselector('StepperChanged:'));
{ The default way to do this in Cocoa is with NSNumberFormatter
But it is a bit annoying, it just disallows losing focus from the control
instead of the Windows like solution to just override with the last value
If we ever want the Cocoa behavior, instead of implementing controlTextDidChange
do this:
var
lNSStr: NSString;
lStr: string;
i: Integer;
NumberFormatter := NSNumberFormatter.alloc.init;
lStr := '##0';
if ASpinEdit.DecimalPlaces > 0 then lStr := lStr + '.';
for i := 0 to ASpinEdit.DecimalPlaces-1 do
lStr := lStr + '0';
lNSStr := CocoaUtils.NSStringUtf8(lStr);
NumberFormatter.setFormat(lNSStr);
lNSStr.release;
NumberFormatter.setNumberStyle(NSNumberFormatterDecimalStyle);
setFormatter(NumberFormatter);}
end;
procedure TCocoaSpinEdit.lclReleaseSubcontrols;
begin
if Assigned(Stepper) then
begin
Stepper.removeFromSuperview;
Stepper.release;
Stepper := nil;
end;
if Assigned(NumberFormatter) then
begin
NumberFormatter.release;
NumberFormatter := nil;
end;
end;
procedure TCocoaSpinEdit.PositionSubcontrols(const ALeft, ATop, AWidth, AHeight: Integer);
begin
lclSetFrame(Types.Bounds(ALeft, ATop, AWidth, AHeight));
end;
procedure TCocoaSpinEdit.StepperChanged(sender: NSObject);
var
lNSStr: NSString;
lStr: string;
begin
// Stepper not might be assigend while creating or destroying handle
if not Assigned(Stepper) then Exit;
lStr := Format('%.*f', [DecimalPlaces, Stepper.doubleValue()]);
lNSStr := CocoaUtils.NSStringUtf8(lStr);
setStringValue(lNSStr);
lNSStr.release;
// This implements OnChange for both user and code changes
if (callback <> nil) and (avoidChangeEvent=0) then
callback.SendOnTextChanged();
end;
procedure TCocoaSpinEdit.textDidChange(notification: NSNotification);
var
w : NSWindow;
begin
w := Self.window;
if Assigned(w)
and (w.firstResponder.isKindOfClass(NSTextView))
and (NSObject(NSTextView(w.firstResponder).delegate) = self) // is focused
then
begin
anyChange:=true;
updateStepper; // just update float value, keep text as is!
if (callback <> nil) and (avoidChangeEvent=0) then
callback.SendOnTextChanged();
end
else
begin
// not focused
inc(avoidChangeEvent);
try
updateStepper;
StepperChanged(nil); // and refresh self
inherited textDidChange(notification);
finally
dec(avoidChangeEvent);
end;
end;
end;
procedure TCocoaSpinEdit.textDidEndEditing(notification: NSNotification);
begin
if anyChange then
begin
updateStepper;
StepperChanged(nil); // and refresh self
anyChange := false;
end;
inherited textDidEndEditing(notification);
end;
function TCocoaSpinEdit.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := NSViewCanFocus(Self);
end;
function TCocoaSpinEdit.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaSpinEdit.lclClearCallback;
begin
callback := nil;
end;
procedure TCocoaSpinEdit.lclSetVisible(AVisible: Boolean);
begin
inherited lclSetVisible(AVisible);
{$ifdef BOOLFIX}
Stepper.setHidden_(Ord(not AVisible));
{$else}
Stepper.setHidden(not AVisible);
{$endif}
end;
procedure TCocoaSpinEdit.lclSetFrame(const r: TRect);
var
ns, lStepperNS: NSRect;
svHeight: CGFloat;
lRect, lStepperRect: TRect;
begin
lRect := r;
lStepperRect := r;
lRect.Right := lRect.Right - SPINEDIT_DEFAULT_STEPPER_WIDTH;
lStepperRect.Left := lRect.Right;
svHeight := GetNSViewSuperViewHeight(Self);
if Assigned(superview) and NOT superview.isFlipped then
begin
LCLToNSRect(lRect, svHeight, ns);
LCLToNSRect(lStepperRect, svHeight, lStepperNS);
end
else
begin
ns := RectToNSRect(lRect);
lStepperNS := RectToNSRect(lStepperRect);
end;
{$IFDEF COCOA_DEBUG_SETBOUNDS}
WriteLn(Format('LCLViewExtension.lclSetFrame: %s Bounds=%s height=%d ns_pos=%d %d ns_size=%d %d',
[NSStringToString(Self.ClassName), dbgs(r), Round(svHeight),
Round(ns.origin.x), Round(ns.origin.y), Round(ns.size.width), Round(ns.size.height)]));
{$ENDIF}
setFrame(ns);
Stepper.setFrame(lStepperNS);
end;
function TCocoaSpinEdit.fittingSize: NSSize;
var
fr : NSRect;
begin
Result.width := -1;
fr:=frame;
sizeToFit();
Result.height := bounds.size.height;
if not NSEqualRects(frame, fr) then setFrame(fr); // prevent changes of frame after sizeToFit();
{$IFDEF COCOA_SPIN_DEBUG}
WriteLn('[TCocoaSpinEdit.fittingSize] width=', Result.width:0:0, ' height=', Result.height:0:0);
{$ENDIF}
end;
function TCocoaSpinEdit.acceptsFirstMouse(event: NSEvent): LCLObjCBoolean;
begin
Result:=true;
end;
procedure TCocoaSpinEdit.mouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
begin
inherited mouseDown(event);
if Assigned(callback) then
callback.MouseUpDownEvent(event, true);
end;
end;
procedure TCocoaSpinEdit.mouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited mouseUp(event);
end;
procedure TCocoaSpinEdit.rightMouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaSpinEdit.rightMouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaSpinEdit.rightMouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDragged(event);
end;
procedure TCocoaSpinEdit.otherMouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaSpinEdit.otherMouseUp(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaSpinEdit.otherMouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited otherMouseDragged(event);
end;
procedure TCocoaSpinEdit.mouseDragged(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaSpinEdit.mouseMoved(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
{$ENDIF}
end.
|