/usr/lib/lazarus/0.9.30.4/lcl/dbgrids.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 | { $Id: dbgrids.pas 31451 2011-06-29 07:18:33Z juha $}
{
/***************************************************************************
DBGrids.pas
-----------
An interface to DB aware Controls
Initial Revision : Sun Sep 14 2003
***************************************************************************/
*****************************************************************************
* *
* This file is part of the Lazarus Component Library (LCL) *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
*****************************************************************************
}
{
TDBGrid and TComponentDataLink for Lazarus
Copyright (C) 2003 Jesus Reyes Aguilar.
email: jesusrmx@yahoo.com.mx
TComponentDatalink idea was taken from Joanna Carter's article
"The Ultimate Datalink?" Delphi Magazine Issue #30 February 1998
}
unit DBGrids;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Math, FileUtil, DB,
LCLStrConsts, LCLIntf, LCLProc, LCLType, LMessages, LResources,
Controls, StdCtrls, Graphics, Grids, Dialogs, Themes, Variants;
type
TCustomDbGrid = class;
TColumn = class;
EInvalidGridOperation = class(Exception);
TDBGridOption = (
dgEditing, // Ya
dgTitles, // Ya
dgIndicator, // Ya
dgColumnResize, // Ya
dgColumnMove, // Ya
dgColLines, // Ya
dgRowLines, // Ya
dgTabs, // Ya
dgAlwaysShowEditor, // Ya
dgRowSelect, // Ya
dgAlwaysShowSelection, // Ya
dgConfirmDelete,
dgCancelOnExit, // Ya
dgMultiselect, // Ya
dgHeaderHotTracking,
dgHeaderPushedLook,
dgPersistentMultiSelect,
dgAutoSizeColumns
);
TDbGridOptions = set of TDbGridOption;
TDbGridExtraOption = (
dgeAutoColumns, // if uncustomized columns, add them anyway?
dgeCheckboxColumn // enable the use of checkbox in columns
);
TDbGridExtraOptions = set of TDbGridExtraOption;
TDbGridStatusItem = (gsUpdatingData, gsAddingAutoColumns,
gsRemovingAutoColumns, gsAutoSized, gsStartEditing);
TDbGridStatus = set of TDbGridStatusItem;
TDataSetScrolledEvent =
procedure(DataSet: TDataSet; Distance: Integer) of object;
TDBGridClickEvent =
procedure(Column: TColumn) of object;
TMovedEvent =
procedure(Sender: TObject; FromIndex, ToIndex: Integer) of object;
TDrawColumnCellEvent =
procedure(Sender: TObject; const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState) of object;
TGetDbEditMaskEvent =
procedure (Sender: TObject; const Field: TField;
var Value: string) of object;
TDbGridSelEditorEvent =
procedure(Sender: TObject; Column: TColumn;
var Editor: TWinControl) of object;
TPrepareDbGridCanvasEvent =
procedure(sender: TObject; DataCol: Integer;
Column: TColumn; AState: TGridDrawState) of object;
TDbGridCheckBoxBitmapEvent =
procedure(Sender: TObject; const CheckedState: TCheckboxState;
var ABitmap: TBitmap) of object;
type
{ TBMStringList }
TBMStringList = class (TStringList)
protected
function DoCompareText(const s1,s2 : string) : PtrInt; override;
end;
{ TBookmarkList }
TBookmarkList=class
private
FList: TBMStringList;
FGrid: TCustomDbGrid;
function GetCount: integer;
function GetCurrentRowSelected: boolean;
function GetItem(AIndex: Integer): TBookmarkStr;
procedure SetCurrentRowSelected(const AValue: boolean);
procedure CheckActive;
public
constructor Create(AGrid: TCustomDbGrid);
destructor Destroy; override;
procedure Clear;
procedure Delete;
function Find(const Item: TBookmarkStr; var AIndex: Integer): boolean;
function IndexOf(const Item: TBookmarkStr): Integer;
function Refresh: boolean;
property Count: integer read GetCount;
property CurrentRowSelected: boolean
read GetCurrentRowSelected write SetCurrentRowSelected;
property Items[AIndex: Integer]: TBookmarkStr read GetItem; default;
end;
{ TComponentDataLink }
TComponentDataLink=class(TDatalink)
private
FDataSet: TDataSet;
FDataSetName: string;
FModified: Boolean;
FOnDatasetChanged: TDatasetNotifyEvent;
fOnDataSetClose: TDataSetNotifyEvent;
fOnDataSetOpen: TDataSetNotifyEvent;
FOnDataSetScrolled: TDataSetScrolledEvent;
FOnEditingChanged: TDataSetNotifyEvent;
fOnInvalidDataSet: TDataSetNotifyEvent;
fOnInvalidDataSource: TDataSetNotifyEvent;
FOnLayoutChanged: TDataSetNotifyEvent;
fOnNewDataSet: TDataSetNotifyEvent;
FOnRecordChanged: TFieldNotifyEvent;
FOnUpdateData: TDataSetNotifyEvent;
function GetDataSetName: string;
function GetFields(Index: Integer): TField;
procedure SetDataSetName(const AValue: string);
protected
procedure RecordChanged(Field: TField); override;
procedure DataSetChanged; override;
procedure ActiveChanged; override;
procedure LayoutChanged; override;
procedure DataSetScrolled(Distance: Integer); override;
procedure FocusControl(Field: TFieldRef); override;
// Testing Events
procedure CheckBrowseMode; override;
procedure EditingChanged; override;
procedure UpdateData; override;
function MoveBy(Distance: Integer): Integer; override;
property Modified: Boolean read FModified write FModified;
public
property OnRecordChanged: TFieldNotifyEvent read FOnRecordChanged write FOnRecordChanged;
property OnDataSetChanged: TDatasetNotifyEvent read FOnDatasetChanged write FOnDataSetChanged;
property OnNewDataSet: TDataSetNotifyEvent read fOnNewDataSet write fOnNewDataSet;
property OnDataSetOpen: TDataSetNotifyEvent read fOnDataSetOpen write fOnDataSetOpen;
property OnInvalidDataSet: TDataSetNotifyEvent read fOnInvalidDataSet write fOnInvalidDataSet;
property OnInvalidDataSource: TDataSetNotifyEvent read fOnInvalidDataSource write fOnInvalidDataSource;
property OnLayoutChanged: TDataSetNotifyEvent read FOnLayoutChanged write FOnLayoutChanged;
property OnDataSetClose: TDataSetNotifyEvent read fOnDataSetClose write fOnDataSetClose;
property OnDataSetScrolled: TDataSetScrolledEvent read FOnDataSetScrolled write FOnDataSetScrolled;
property OnEditingChanged: TDataSetNotifyEvent read FOnEditingChanged write FOnEditingChanged;
property OnUpdateData: TDataSetNotifyEvent read FOnUpdateData write FOnUpdateData;
property DataSetName:string read GetDataSetName write SetDataSetName;
property Fields[Index: Integer]: TField read GetFields;
property VisualControl;
end;
{ TColumn }
TColumnTitle = class(TGridColumnTitle)
protected
function GetDefaultCaption: string; override;
end;
{ TColumn }
TColumn = class(TGridColumn)
private
FDisplayFormat: String;
FDisplayFormatChanged: boolean;
FFieldName: String;
FField: TField;
FIsAutomaticColumn: boolean;
FDesignIndex: Integer;
procedure ApplyDisplayFormat;
function GetDataSet: TDataSet;
function GetDisplayFormat: string;
function GetField: TField;
function GetIsDesignColumn: boolean;
function IsDisplayFormatStored: boolean;
procedure SetDisplayFormat(const AValue: string);
procedure SetField(const AValue: TField);
procedure SetFieldName(const AValue: String);
protected
function CreateTitle: TGridColumnTitle; override;
function GetDefaultAlignment: TAlignment; override;
function GetDefaultDisplayFormat: string;
function GetDefaultValueChecked: string; override;
function GetDefaultValueUnchecked: string; override;
function GetDefaultVisible: boolean; override;
function GetDisplayName: string; override;
function GetDefaultReadOnly: boolean; override;
function GetDefaultWidth: Integer; override;
function GetPickList: TStrings; override;
property IsAutomaticColumn: boolean read FIsAutomaticColumn;
property IsDesignColumn: boolean read GetIsDesignColumn;
procedure LinkField;
public
constructor Create(ACollection: TCollection); override;
procedure Assign(Source: TPersistent); override;
function IsDefault: boolean; override;
property DesignIndex: integer read FDesignIndex;
property Field: TField read GetField write SetField;
published
property FieldName: String read FFieldName write SetFieldName;
property DisplayFormat: string read GetDisplayFormat write SetDisplayFormat
stored IsDisplayFormatStored;
end;
TColumnOrder = (coDesignOrder, coFieldIndexOrder);
{ TDBGridColumns }
TDBGridColumns = class(TGridColumns)
private
function GetColumn(Index: Integer): TColumn;
procedure SetColumn(Index: Integer; Value: TColumn);
protected
procedure Update(Item: TCollectionItem); override;
function ColumnFromField(Field: TField): TColumn;
function HasAutomaticColumns: boolean;
function HasDesignColumns: boolean;
procedure RemoveAutoColumns;
public
function Add: TColumn;
procedure LinkFields;
procedure ResetColumnsOrder(ColumnOrder: TColumnOrder);
property Items[Index: Integer]: TColumn read GetColumn write SetColumn; default;
end;
{ TCustomDBGrid }
TCustomDBGrid=class(TCustomGrid)
private
FDataLink: TComponentDataLink;
FExtraOptions: TDBGridExtraOptions;
FOnCellClick: TDBGridClickEvent;
FOnColEnter,FOnColExit: TNotifyEvent;
FOnColumnMoved: TMovedEvent;
FOnColumnSized: TNotifyEvent;
FOnDrawColumnCell: TDrawColumnCellEvent;
FOnFieldEditMask: TGetDbEditMaskEvent;
FOnTitleClick: TDBGridClickEvent;
FOnSelectEditor: TDbGridSelEditorEvent;
FOnCheckboxBitmap: TDbGridCheckBoxBitmapEvent;
FOptions: TDBGridOptions;
FReadOnly: Boolean;
FColEnterPending: Boolean;
FLayoutChangedCount: integer;
FTempText : string;
FDrawingActiveRecord: Boolean;
FDrawingMultiSelRecord: Boolean;
FDrawingEmptyDataset: Boolean;
FEditingColumn: Integer;
FOldPosition: Integer;
FDefaultColWidths: boolean;
FGridStatus: TDBGridStatus;
FOldControlStyle: TControlStyle;
FSelectedRows: TBookmarkList;
FOnPrepareCanvas: TPrepareDbGridCanvasEvent;
FKeyBookmark: TBookmarkStr;
FKeySign: Integer;
procedure EmptyGrid;
function GetColumns: TDBGridColumns;
function GetCurrentColumn: TColumn;
function GetCurrentField: TField;
function GetDataSource: TDataSource;
function GetRecordCount: Integer;
function GetSelectedFieldRect: TRect;
function GetSelectedIndex: Integer;
function GetThumbTracking: boolean;
procedure OnRecordChanged(Field:TField);
procedure OnDataSetChanged(aDataSet: TDataSet);
procedure OnDataSetOpen(aDataSet: TDataSet);
procedure OnDataSetClose(aDataSet: TDataSet);
procedure OnEditingChanged(aDataSet: TDataSet);
procedure OnInvalidDataSet(aDataSet: TDataSet);
procedure OnInvalidDataSource(aDataSet: TDataset);
procedure OnLayoutChanged(aDataSet: TDataSet);
procedure OnNewDataSet(aDataSet: TDataset);
procedure OnDataSetScrolled(aDataSet:TDataSet; Distance: Integer);
procedure OnUpdateData(aDataSet: TDataSet);
procedure SetColumns(const AValue: TDBGridColumns);
//procedure ReadColumns(Reader: TReader);
//procedure SetColumns(const AValue: TDBGridColumns);
procedure SetCurrentField(const AValue: TField);
procedure SetDataSource(const AValue: TDataSource);
procedure SetExtraOptions(const AValue: TDBGridExtraOptions);
procedure SetOptions(const AValue: TDBGridOptions);
procedure SetSelectedIndex(const AValue: Integer);
procedure SetThumbTracking(const AValue: boolean);
procedure UpdateBufferCount;
// Temporal
function GetColumnCount: Integer;
function DefaultFieldColWidth(F: TField): Integer;
procedure UpdateGridColumnSizes;
procedure UpdateScrollbarRange;
procedure DoLayoutChanged;
//procedure WriteColumns(Writer: TWriter);
procedure RestoreEditor;
function ISEOF: boolean;
function ValidDataSet: boolean;
function InsertCancelable: boolean;
procedure StartUpdating;
procedure EndUpdating;
function UpdatingData: boolean;
procedure SwapCheckBox;
procedure ToggleSelectedRow;
procedure SelectRecord(AValue: boolean);
procedure GetScrollbarParams(out aRange, aPage, aPos: Integer);
procedure CMGetDataLink(var Message: TLMessage); message CM_GETDATALINK;
procedure ClearSelection(selCurrent:boolean=false);
function NeedAutoSizeColumns: boolean;
procedure RenewColWidths;
protected
procedure AddAutomaticColumns;
procedure AssignTo(Dest: TPersistent); override;
procedure BeforeMoveSelection(const DCol,DRow: Integer); override;
procedure BeginLayout;
procedure CellClick(const aCol,aRow: Integer; const Button:TMouseButton); override;
procedure InvalidateSizes;
procedure ColRowMoved(IsColumn: Boolean; FromIndex,ToIndex: Integer); override;
function ColumnEditorStyle(aCol: Integer; F: TField): TColumnButtonStyle;
function CreateColumns: TGridColumns; override;
procedure CreateWnd; override;
procedure DefineProperties(Filer: TFiler); override;
procedure DefaultDrawCell(aCol,aRow: Integer; aRect: TRect; aState: TGridDrawState);
function DefaultEditorStyle(const Style:TColumnButtonStyle; const F:TField): TColumnButtonStyle;
procedure DoExit; override;
function DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint): Boolean; override;
function DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint): Boolean; override;
procedure DoOnChangeBounds; override;
procedure DoPrepareCanvas(aCol,aRow:Integer; aState: TGridDrawState); override;
procedure DrawAllRows; override;
procedure DrawFocusRect(aCol,aRow:Integer; ARect:TRect); override;
procedure DrawRow(ARow: Integer); override;
procedure DrawCell(aCol,aRow: Integer; aRect: TRect; aState:TGridDrawState); override;
procedure DrawCheckboxBitmaps(aCol: Integer; aRect: TRect; F: TField);
procedure DrawFixedText(aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState);
procedure DrawColumnText(aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState); override;
procedure DrawIndicator(ACanvas: TCanvas; R: TRect; Opt: TDataSetState; MultiSel: boolean); virtual;
procedure EditingColumn(aCol: Integer; Ok: boolean);
procedure EditorCancelEditing;
procedure EditorDoGetValue; override;
function EditorCanAcceptKey(const ch: TUTF8Char): boolean; override;
function EditorIsReadOnly: boolean; override;
procedure EditorTextChanged(const aCol,aRow: Integer; const aText:string); override;
procedure EndLayout;
function FieldIndexFromGridColumn(AGridCol: Integer): Integer;
function FirstGridColumn: Integer; override;
function GetBufferCount: integer;
function GetDefaultColumnAlignment(Column: Integer): TAlignment; override;
function GetDefaultColumnWidth(Column: Integer): Integer; override;
function GetDefaultColumnReadOnly(Column: Integer): boolean; override;
function GetDefaultColumnTitle(Column: Integer): string; override;
function GetDefaultRowHeight: integer; override;
function GetDsFieldFromGridColumn(Column: Integer): TField;
function GetEditMask(aCol, aRow: Longint): string; override;
function GetEditText(aCol, aRow: Longint): string; override;
function GetFieldFromGridColumn(Column: Integer): TField;
function GetGridColumnFromField(F: TField): Integer;
function GetImageForCheckBox(const aCol,aRow: Integer;
CheckBoxView: TCheckBoxState): TBitmap; override;
function GetIsCellSelected(aCol, aRow: Integer): boolean; override;
function GetIsCellTitle(aCol,aRow: Integer): boolean; override;
procedure GetSelectedState(AState: TGridDrawState; out IsSelected:boolean); override;
function GridCanModify: boolean;
procedure GetSBVisibility(out HsbVisible,VsbVisible:boolean);override;
procedure GetSBRanges(const HsbVisible,VsbVisible: boolean;
out HsbRange,VsbRange,HsbPage,VsbPage,HsbPos,VsbPos:Integer); override;
procedure HeaderClick(IsColumn: Boolean; index: Integer); override;
procedure HeaderSized(IsColumn: Boolean; Index: Integer); override;
function IsValidChar(AField: TField; AChar: TUTF8Char): boolean;
procedure KeyDown(var Key : Word; Shift : TShiftState); override;
procedure LinkActive(Value: Boolean); virtual;
procedure LayoutChanged; virtual;
procedure Loaded; override;
procedure MoveSelection; override;
procedure MouseDown(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure PrepareCanvas(aCol,aRow: Integer; aState:TGridDrawState); override;
procedure RemoveAutomaticColumns;
procedure ResetSizes; override;
procedure SelectEditor; override;
procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
procedure SetFixedCols(const AValue: Integer); override;
function SelectCell(aCol, aRow: Integer): boolean; override;
procedure UpdateActive; virtual;
procedure UpdateAutoSizeColumns;
procedure UpdateData; virtual;
function UpdateGridCounts: Integer;
procedure WMVScroll(var Message : TLMVScroll); message LM_VScroll;
procedure WndProc(var TheMessage : TLMessage); override;
property Columns: TDBGridColumns read GetColumns write SetColumns;
property GridStatus: TDBGridStatus read FGridStatus write FGridStatus;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property Options: TDBGridOptions read FOptions write SetOptions default
[dgColumnResize, dgColumnMove, dgTitles, dgIndicator, dgRowLines,
dgColLines, dgConfirmDelete, dgCancelOnExit, dgTabs, dgEditing,
dgAlwaysShowSelection];
property OptionsExtra: TDBGridExtraOptions read FExtraOptions
write SetExtraOptions default [dgeAutoColumns, dgeCheckboxColumn];
property ReadOnly: Boolean read FReadOnly write FReadOnly default false;
property SelectedRows: TBookmarkList read FSelectedRows;
property OnCellClick: TDBGridClickEvent read FOnCellClick write FOnCellClick;
property OnColEnter: TNotifyEvent read FOnColEnter write FOnColEnter;
property OnColExit: TNotifyEvent read FOnColExit write FOnColExit;
property OnColumnMoved: TMovedEvent read FOnColumnMoved write FOnColumnMoved;
property OnColumnSized: TNotifyEvent read FOnColumnSized write FOnColumnSized;
property OnDrawColumnCell: TDrawColumnCellEvent read FOnDrawColumnCell write FOnDrawColumnCell;
property OnFieldEditMask: TGetDbEditMaskEvent read FOnFieldEditMask write FOnFieldEditMask;
property OnPrepareCanvas: TPrepareDbGridCanvasEvent read FOnPrepareCanvas write FOnPrepareCanvas;
property OnSelectEditor: TDbGridSelEditorEvent read FOnSelectEditor write FOnSelectEditor;
property OnTitleClick: TDBGridClickEvent read FOnTitleClick write FOnTitleClick;
property OnUserCheckboxBitmap: TDbGridCheckboxBitmapEvent read FOnCheckboxBitmap write FOnCheckboxBitmap;
public
constructor Create(AOwner: TComponent); override;
procedure AutoSizeColumns;
procedure InitiateAction; override;
procedure DefaultDrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
function EditorByStyle(Style: TColumnButtonStyle): TWinControl; override;
procedure ResetColWidths;
destructor Destroy; override;
property SelectedField: TField read GetCurrentField write SetCurrentField;
property SelectedIndex: Integer read GetSelectedIndex write SetSelectedIndex;
property SelectedColumn: TColumn read GetCurrentColumn;
property SelectedFieldRect: TRect read GetSelectedFieldRect;
property ThumbTracking: boolean read GetThumbTracking write SetThumbTracking;
end;
TDBGrid=class(TCustomDBGrid)
public
property BorderColor;
property Canvas;
property DefaultTextStyle;
property EditorBorderStyle;
property EditorMode;
property ExtendedColSizing;
property FastEditing;
property FocusColor;
property FocusRectVisible;
property GridLineColor;
property GridLineStyle;
property SelectedColor;
property SelectedRows;
published
property Align;
property AlternateColor;
property Anchors;
property AutoAdvance default aaRightDown;
property AutoEdit;
property AutoFillColumns;
//property BiDiMode;
property BorderSpacing;
property BorderStyle;
property Color;
property Columns; // stored false;
property Constraints;
property DataSource;
property DefaultDrawing;
property DefaultRowHeight;
property DragCursor;
//property DragKind;
property DragMode;
property Enabled;
property FixedColor;
property FixedCols;
property FixedHotColor;
property Flat;
property Font;
property HeaderHotZones;
property HeaderPushZones;
//property ImeMode;
//property ImeName;
property Options;
property OptionsExtra;
//property ParentBiDiMode;
property ParentColor default false;
property ParentFont;
//property ParentShowHint;
property PopupMenu;
property ReadOnly;
property Scrollbars default ssBoth;
property ShowHint;
property TabOrder;
property TabStop;
property TitleFont;
property TitleImageList;
property TitleStyle;
property UseXORFeatures;
property Visible;
property OnCellClick;
property OnColEnter;
property OnColExit;
property OnColumnMoved;
property OnColumnSized;
property OnContextPopup;
property OnDrawColumnCell;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEditButtonClick;
property OnEditingDone;
//property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnFieldEditMask;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnPrepareCanvas;
property OnSelectEditor;
//property OnStartDock;
property OnStartDrag;
property OnTitleClick;
property OnUserCheckboxBitmap;
property OnUTF8KeyPress;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Data Controls',[TDBGrid]);
end;
function CalcCanvasCharWidth(Canvas:TCanvas): integer;
begin
if Canvas.HandleAllocated then
result := Canvas.TextWidth('MX') div 2
else
result := 8;
end;
function CalcColumnFieldWidth(Canvas: TCanvas; hasTitle: boolean;
aTitle: String; aTitleFont: TFont; Field: TField): Integer;
var
aCharWidth: Integer;
aFont: TFont;
UseTitleFont: boolean;
begin
if (Field=nil) or (Field.DisplayWidth=0) then
Result := DEFCOLWIDTH
else begin
aCharWidth := CalcCanvasCharWidth(Canvas);
if Field.DisplayWidth>Length(aTitle) then
result := aCharWidth * Field.DisplayWidth
else
result := aCharWidth * Length(aTitle);
if HasTitle then begin
UseTitleFont :=
(Canvas.Font.Size<>aTitleFont.Size) or
(Canvas.Font.Style<>aTitleFont.Style) or
(Canvas.Font.CharSet<>aTitleFont.CharSet) or
(Canvas.Font.Name<>aTitleFont.Name);
if UseTitleFont then begin
aFont := TFont.Create;
aFont.Assign(Canvas.Font);
Canvas.Font := aTitleFont;
end;
try
aCharWidth := Canvas.TextWidth(ATitle)+6;
if aCharWidth>Result then
Result := aCharWidth;
finally
if UseTitleFont then begin
Canvas.Font := aFont;
aFont.Free;
end;
end;
end; // if HasTitle ...
end; // if (Field=nil) or (Field.DisplayWidth=0) ...
end;
var
LookupTmpSetActive: Boolean;
LookupBookMark: TBookmark;
procedure LookupGetBookMark(ALookupField: TField);
begin
LookupTmpSetActive := not ALookupField.LookupDataSet.Active;
if LookupTmpSetActive then
ALookupField.LookupDataSet.Active := True
else
begin
LookupBookMark := ALookupField.LookupDataSet.GetBookmark;
ALookupField.LookupDataSet.DisableControls;
end;
end;
procedure LookupGotoBookMark(ALookupField: TField);
begin
if LookupTmpSetActive then
begin
ALookupField.LookupDataSet.Active := False;
LookupTmpSetActive := False;
end
else
try
ALookupField.LookupDataSet.GotoBookmark(LookupBookMark);
ALookupField.LookupDataSet.FreeBookmark(LookupBookMark);
finally
ALookupField.LookupDataSet.EnableControls;
end;
end;
{ TCustomDBGrid }
procedure TCustomDBGrid.OnRecordChanged(Field: TField);
var
c: Integer;
begin
{$IfDef dbgDBGrid}
DBGOut('('+name+') ','TCustomDBGrid.OnRecordChanged(Field=');
if Field=nil then DebugLn('nil)')
else DebugLn(Field.FieldName,')');
{$Endif}
if Field=nil then
UpdateActive
else begin
c := GetGridColumnFromField(Field);
if c>0 then
InvalidateCell(C, Row)
else
UpdateActive;
end;
end;
function TCustomDBGrid.GetDataSource: TDataSource;
begin
Result:= FDataLink.DataSource;
end;
function TCustomDBGrid.GetRecordCount: Integer;
begin
result := FDataLink.DataSet.RecordCount;
end;
function TCustomDBGrid.GetSelectedFieldRect: TRect;
begin
result := CellRect(Col,Row);
end;
function TCustomDBGrid.GetSelectedIndex: Integer;
begin
if Columns.Enabled then
Result := ColumnIndexFromGridColumn( Col )
else
Result := FieldIndexFromGridColumn( Col );
end;
function TCustomDBGrid.GetThumbTracking: boolean;
begin
Result := goThumbTracking in inherited Options;
end;
procedure TCustomDBGrid.EmptyGrid;
var
OldFixedCols, OldFixedRows: Integer;
begin
OldFixedCols := FixedCols;
OldFixedRows := FixedRows;
Clear;
RowCount := OldFixedRows + 1;
ColCount := OldFixedCols + 1;
if dgIndicator in Options then
ColWidths[0]:=12;
end;
function TCustomDBGrid.GetColumns: TDBGridColumns;
begin
result := TDBGridColumns( inherited Columns );
end;
procedure TCustomDBGrid.InvalidateSizes;
begin
GridFlags := GridFlags + [gfVisualChange];
end;
function TCustomDBGrid.GetCurrentColumn: TColumn;
begin
if Columns.Enabled then
Result := TColumn(Columns[SelectedIndex])
else
Result := nil;
end;
function TCustomDBGrid.GetCurrentField: TField;
begin
result := GetFieldFromGridColumn( Col );
end;
procedure TCustomDBGrid.OnDataSetChanged(aDataSet: TDataSet);
begin
{$Ifdef dbgDBGrid}
DebugLn('(%s) TCustomDBDrid.OnDataSetChanged(aDataSet=%s) INIT',[name,dbgsname(ADataset)]);
{$endif}
if not (gsStartEditing in FGridStatus) then begin
if EditorMode then
EditorMode := False;
LayoutChanged;
end;
UpdateActive;
if not (gsStartEditing in FGridStatus) then begin
SelectEditor;
if (dgAlwaysShowEditor in Options) and not EditorMode then
EditorMode := true;
end;
{$Ifdef dbgDBGrid}
DebugLn('(%s) TCustomDBDrid.OnDataSetChanged(aDataSet=%s) DONE',[name,dbgsname(ADataset)]);
{$endif}
end;
procedure TCustomDBGrid.OnDataSetOpen(aDataSet: TDataSet);
begin
{$Ifdef dbgDBGrid}
DebugLn('(%s) TCustomDBDrid.OnDataSetOpen INIT',[name]);
{$endif}
RenewColWidths;
LinkActive(True);
UpdateActive;
SelectEditor;
{$Ifdef dbgDBGrid}
DebugLn('(%s) TCustomDBDrid.OnDataSetOpen DONE',[name]);
{$endif}
end;
procedure TCustomDBGrid.OnDataSetClose(aDataSet: TDataSet);
begin
{$ifdef dbgDBGrid}
DebugLn('(',name,') ','TCustomDBGrid.OnDataSetClose');
{$endif}
LinkActive(False);
end;
procedure TCustomDBGrid.OnEditingChanged(aDataSet: TDataSet);
begin
{$ifdef dbgDBGrid}
DebugLn('(',name,') ','TCustomDBGrid.OnEditingChanged');
if aDataSet<>nil then begin
DebugLn(['Editing=', dsEdit = aDataSet.State]);
DebugLn(['Inserting=',dsInsert = aDataSet.State]);
end else
DebugLn('Dataset=nil');
{$endif}
FDataLink.Modified := False;
UpdateActive;
end;
procedure TCustomDBGrid.OnInvalidDataSet(aDataSet: TDataSet);
begin
{$ifdef dbgDBGrid}
DebugLn('(',name,') ','TCustomDBGrid.OnInvalidDataSet');
{$endif}
LinkActive(False);
end;
procedure TCustomDBGrid.OnInvalidDataSource(aDataSet: TDataset);
begin
{$ifdef dbgDBGrid}
DebugLn('(',name,') ','TCustomDBGrid.OnInvalidDataSource');
{$endif}
LinkActive(False);
end;
procedure TCustomDBGrid.OnLayoutChanged(aDataSet: TDataSet);
begin
{$ifdef dbgDBGrid}
DebugLn('(',name,') ','TCustomDBGrid.OnLayoutChanged');
{$endif}
LayoutChanged;
end;
procedure TCustomDBGrid.OnNewDataSet(aDataSet: TDataset);
begin
{$Ifdef dbgDBGrid}
DebugLn('(%s) TCustomDBDrid.OnNewDataset INIT',[name]);
{$endif}
RenewColWidths;
LinkActive(True);
UpdateActive;
SelectEditor;
{$Ifdef dbgDBGrid}
DebugLn('(%s) TCustomDBDrid.OnNewDataset DONE',[name]);
{$endif}
end;
procedure TCustomDBGrid.OnDataSetScrolled(aDataset: TDataSet; Distance: Integer);
var
OldEditorMode: boolean;
begin
{$ifdef dbgDBGrid}
DebugLn(ClassName, ' (',name,')', '.OnDataSetScrolled(',IntToStr(Distance),')');
Debugln('Dataset.RecordCount=',IntToStr(aDataSet.RecordCount));
{$endif}
UpdateScrollBarRange;
// todo: Use a fast interface method to scroll a rectangular section of window
// if distance=+, Row[Distance] to Row[RowCount-2] UP
// if distance=-, Row[FixedRows+1] to Row[RowCount+Distance] DOWN
OldEditorMode := EditorMode;
if OldEditorMode then
EditorMode := False;
if Distance<>0 then begin
Row:= FixedRows + FDataLink.ActiveRecord;
Invalidate;
end else
UpdateActive;
if OldEditorMode and (dgAlwaysShowEditor in Options) then
EditorMode := True;
end;
procedure TCustomDBGrid.OnUpdateData(aDataSet: TDataSet);
begin
UpdateData;
end;
procedure TCustomDBGrid.SetColumns(const AValue: TDBGridColumns);
begin
inherited Columns := TGridColumns(AValue);
end;
{
procedure TCustomDBGrid.ReadColumns(Reader: TReader);
begin
Columns.Clear;
Reader.ReadValue;
Reader.ReadCollection(Columns);
end;
procedure TCustomDBGrid.SetColumns(const AValue: TDBGridColumns);
begin
Columns.Assign(AValue);
end;
}
procedure TCustomDBGrid.SetCurrentField(const AValue: TField);
var
i: Integer;
begin
if Avalue<>SelectedField then begin
i := GetGridColumnFromField( AValue );
if (i>=FirstGridColumn) and (i>=FixedCols) then
Col := i;
end;
end;
procedure TCustomDBGrid.SetDataSource(const AValue: TDataSource);
begin
if AValue = FDatalink.Datasource then Exit;
RenewColWidths;
FDataLink.DataSource := AValue;
UpdateActive;
end;
procedure TCustomDBGrid.SetExtraOptions(const AValue: TDBGridExtraOptions);
var
OldOptions: TDBGridExtraOptions;
function IsOptionChanged(Op: TDBGridExtraOption): boolean;
begin
result := ((op in OldOptions) and not (op in AValue)) or
(not (op in OldOptions) and (op in AValue));
end;
begin
if FExtraOptions=AValue then exit;
OldOptions := FExtraOptions;
FExtraOptions := AValue;
if IsOptionChanged(dgeCheckboxColumn) then
Invalidate;
if IsOptionChanged(dgeAutoColumns) then begin
if dgeAutoColumns in aValue then
AddAutomaticColumns
else if TDBGridColumns(Columns).HasAutomaticColumns then
RemoveAutomaticColumns;
UpdateActive;
end;
end;
procedure TCustomDBGrid.SetOptions(const AValue: TDBGridOptions);
var
OldOptions: TGridOptions;
ChangedOptions: TDbGridOptions;
MultiSel: boolean;
begin
if FOptions<>AValue then begin
MultiSel := dgMultiSelect in FOptions;
ChangedOptions := (FOptions-AValue) + (AValue-FOptions);
FOptions:=AValue;
OldOptions := inherited Options;
if dgRowSelect in FOptions then
FOptions := FOptions - [dgEditing, dgAlwaysShowEditor];
BeginLayout;
if dgRowLines in fOptions then
Include(OldOptions, goHorzLine)
else
Exclude(OldOptions, goHorzLine);
if dgColLines in fOptions then
Include(OldOptions, goVertLine)
else
Exclude(OldOptions, goVertLine);
if dgColumnResize in fOptions then
Include(OldOptions, goColSizing)
else
Exclude(OldOptions, goColSizing);
if dgColumnMove in fOptions then
Include(OldOptions, goColMoving)
else
Exclude(OldOptions, goColMoving);
if dgAlwaysShowEditor in FOptions then
Include(OldOptions, goAlwaysShowEditor)
else
Exclude(OldOptions, goAlwaysShowEditor);
if dgRowSelect in FOptions then
Include(OldOptions, goRowSelect)
else
Exclude(OldOptions, goRowSelect);
if dgEditing in FOptions then
Include(OldOptions, goEditing)
else
Exclude(OldOptions, goediting);
if dgTabs in FOptions then
Include(OldOptions, goTabs)
else
Exclude(OldOptions, goTabs);
if dgHeaderHotTracking in FOptions then
Include(OldOptions, goHeaderHotTracking)
else
Exclude(OldOptions, goHeaderHotTracking);
if dgHeaderPushedLook in FOptions then
Include(OldOptions, goHeaderPushedLook)
else
Exclude(OldOptions, goHeaderPushedLook);
if (dgIndicator in ChangedOptions) then begin
if (dgIndicator in FOptions) then
FixedCols := FixedCols + 1
else
FixedCols := FixedCols - 1;
end;
if (dgTitles in ChangedOptions) then begin
if dgTitles in FOptions then
FixedRows := FixedRows + 1
else
FixedRows := FixedRows - 1;
end;
if (dgAutoSizeColumns in ChangedOptions) then begin
Exclude(FGridStatus, gsAutoSized);
end;
inherited Options := OldOptions;
if MultiSel and not (dgMultiSelect in FOptions) then begin
FSelectedRows.Clear;
FKeyBookmark:='';
end;
EndLayout;
end;
end;
procedure TCustomDBGrid.SetSelectedIndex(const AValue: Integer);
begin
Col := FirstGridColumn + AValue;
end;
procedure TCustomDBGrid.SetThumbTracking(const AValue: boolean);
begin
BeginUpdate;
if Avalue then
inherited Options := inherited Options + [goThumbTracking]
else
inherited Options := inherited Options - [goThumbTracking];
EndUpdate(false);
end;
procedure TCustomDBGrid.UpdateBufferCount;
var
BCount: Integer;
begin
if FDataLink.Active then begin
BCount := GetBufferCount;
if BCount<1 then
BCount := 1;
FDataLink.BufferCount:= BCount;
{$ifdef dbgDBGrid}
DebugLn('%s (%s), FDatalink.BufferCount=%d',[ClassName,Name,FDataLink.BufferCount]);
{$endif}
end;
end;
procedure TCustomDBGrid.UpdateData;
var
selField,edField: TField;
LookupKeyValues: Variant;
begin
// get Editor text and update field content
if not UpdatingData and (FEditingColumn>-1) and FDatalink.Editing then begin
SelField := SelectedField;
edField := GetFieldFromGridColumn(FEditingColumn);
if (edField<>nil) and (edField = SelField) then begin
{$ifdef dbgDBGrid}
DebugLn('---> UpdateData: Field[', edField.Fieldname, '(',edField.AsString,')]=', FTempText,' INIT');
{$endif}
StartUpdating;
edField.Text := FTempText;
if edField.Lookup then
begin
LookupKeyValues := Null;
if edField.LookupCache then
LookupKeyValues := edField.LookupList.FirstKeyByValue(FTempText)
else
begin
LookupGetBookMark(edField);
try
if edField.LookupDataSet.Locate(edField.LookupResultField,
VarArrayOf([FTempText]), []) then
LookupKeyValues :=
edField.LookupDataSet.FieldValues[edField.LookupKeyFields];
finally
LookupGotoBookMark(edField);
end;
end;
edField.DataSet.FieldValues[edField.KeyFields] := LookupKeyValues;
end;
EndUpdating;
EditingColumn(FEditingColumn, False);
{$ifdef dbgDBGrid}
DebugLn('<--- UpdateData: Chk: Field:=',edField.ASString,' END');
{$endif}
end;
end;
end;
{$ifdef dbgDBGrid}
function SBCodeToStr(Code: Integer): String;
begin
Case Code of
SB_LINEUP : result := 'SB_LINEUP';
SB_LINEDOWN: result := 'SB_LINEDOWN';
SB_PAGEUP: result := 'SB_PAGEUP';
SB_PAGEDOWN: result := 'SB_PAGEDOWN';
SB_THUMBTRACK: result := 'SB_THUMBTRACK';
SB_THUMBPOSITION: result := 'SB_THUMBPOSITION';
SB_ENDSCROLL: result := 'SB_SCROLLEND';
SB_TOP: result := 'SB_TOP';
SB_BOTTOM: result := 'SB_BOTTOM';
else result :=IntToStr(Code)+ ' -> ?';
end;
end;
{$endif}
procedure TCustomDBGrid.WMVScroll(var Message: TLMVScroll);
var
IsSeq: boolean;
aPos, aRange, aPage: Integer;
DeltaRec: integer;
function MaxPos: Integer;
begin
if IsSeq then
result := GetRecordCount - 1
else
result := 4;
end;
procedure DsMoveBy(Delta: Integer);
begin
FDataLink.MoveBy(Delta);
GetScrollbarParams(aRange, aPage, aPos);
end;
procedure DsGoto(BOF: boolean);
begin
if BOF then FDatalink.DataSet.First
else FDataLink.DataSet.Last;
GetScrollbarParams(aRange, aPage, aPos);
end;
begin
if not FDatalink.Active then exit;
{$ifdef dbgDBGrid}
DebugLn('VSCROLL: Code=',SbCodeToStr(Message.ScrollCode),
' Position=', dbgs(Message.Pos),' OldPos=',Dbgs(FOldPosition));
{$endif}
IsSeq := FDatalink.DataSet.IsSequenced;
case Message.ScrollCode of
SB_TOP:
DsGoto(True);
SB_BOTTOM:
DsGoto(False);
SB_PAGEUP:
DsMoveBy(-VisibleRowCount);
SB_LINEUP:
DsMoveBy(-1);
SB_LINEDOWN:
DsMoveBy(1);
SB_PAGEDOWN:
DsMoveBy(VisibleRowCount);
SB_THUMBPOSITION:
begin
aPos := Message.Pos;
if aPos=FOldPosition then
exit;
if aPos>=MaxPos then
dsGoto(False)
else if aPos<=0 then
dsGoto(True)
else if IsSeq then
FDatalink.DataSet.RecNo := aPos + 1
else begin
DeltaRec := Message.Pos - FOldPosition;
if DeltaRec=0 then
exit
else if DeltaRec<-1 then
DsMoveBy(-VisibleRowCount)
else if DeltaRec>1 then
DsMoveBy(VisibleRowCount)
else
DsMoveBy(DeltaRec);
end;
end;
else
Exit;
end;
ScrollBarPosition(SB_VERT, aPos);
FOldPosition:=aPos;
if EditorMode then
RestoreEditor;
{$ifdef dbgDBGrid}
DebugLn('---- Diff=',dbgs(DeltaRec), ' FinalPos=',dbgs(aPos));
{$endif}
end;
procedure TCustomDBGrid.WndProc(var TheMessage: TLMessage);
begin
if (TheMessage.Msg=LM_SETFOCUS) and (gsUpdatingData in FGridStatus) then begin
{$ifdef dbgGrid}DebugLn('DBGrid.LM_SETFOCUS while updating');{$endif}
if EditorMode then begin
LCLIntf.SetFocus(Editor.Handle);
EditorSelectAll;
end;
exit;
end;
inherited WndProc(TheMessage);
end;
function TCustomDBGrid.DefaultFieldColWidth(F: TField): Integer;
begin
if not HandleAllocated or (F=nil) then
result:=DefaultColWidth
else begin
if F.DisplayWidth = 0 then
if Canvas.HandleAllocated then
result := Canvas.TextWidth( F.DisplayName ) + 3
else
Result := DefaultColWidth
else
result := F.DisplayWidth * CalcCanvasCharWidth(Canvas);
end;
end;
function TCustomDBGrid.GetColumnCount: Integer;
var
i: integer;
F: TField;
begin
result := 0;
if Columns.Enabled then
result := Columns.VisibleCount
else
if (dgeAutoColumns in OptionsExtra) and FDataLink.Active then
for i:=0 to FDataLink.DataSet.FieldCount-1 do begin
F:= FDataLink.DataSet.Fields[i];
if (F<>nil) and F.Visible then
Inc(Result);
end;
end;
// Get the visible field (from dataset fields) that corresponds to given column
function TCustomDBGrid.GetDsFieldFromGridColumn(Column: Integer): TField;
var
i: Integer;
begin
i := FieldIndexFromGridColumn( Column );
if i>=0 then
Result := FDataLink.DataSet.Fields[i]
else
Result := nil;
end;
function TCustomDBGrid.FirstGridColumn: Integer;
begin
if (dgIndicator in Options) then
Result := 1
else
Result := 0;
end;
// obtain the field either from a Db column or directly from dataset fields
function TCustomDBGrid.GetFieldFromGridColumn(Column: Integer): TField;
var
i: integer;
begin
if Columns.Enabled then begin
i := ColumnIndexFromGridColumn( Column );
if i>=0 then
result := TDBGridColumns(Columns)[i].FField
else
result := nil;
end else
result := GetDsFieldFromGridColumn(Column);
end;
// obtain the corresponding grid column for the given field
function TCustomDBGrid.GetGridColumnFromField(F: TField): Integer;
var
i: Integer;
begin
result := -1;
for i:=FirstGridColumn to ColCount-1 do begin
if GetFieldFromGridColumn(i) = F then begin
result := i;
break;
end;
end;
end;
function TCustomDBGrid.GetImageForCheckBox(const aCol, aRow: Integer;
CheckBoxView: TCheckBoxState): TBitmap;
begin
Result:=inherited GetImageForCheckBox(aCol, aRow, CheckBoxView);
if Assigned(OnUserCheckboxBitmap) then
OnUserCheckboxBitmap(Self, CheckBoxView, Result);
end;
// obtain the visible field index corresponding to the grid column index
function TCustomDBGrid.FieldIndexFromGridColumn(AGridCol: Integer): Integer;
var
i: Integer;
Column: TColumn;
begin
result := -1;
if not FDatalink.Active then
exit;
if Columns.Enabled then begin
Column := TColumn(ColumnFromGridColumn(AGridCol));
if (Column<>nil) and (Column.Field<>nil) and Column.Field.Visible then
Result := FDatalink.Dataset.Fields.IndexOf(Column.Field)
end else begin
AGridCol := AGridCol - FirstGridColumn;
i := 0;
while (AGridCol>=0) and (i<FDatalink.DataSet.FieldCount) do begin
if FDatalink.Fields[i].Visible then begin
Dec(AGridCol);
if AGridCol<0 then begin
Result := i;
break;
end;
end;
inc(i);
end;
end;
end;
function TCustomDBGrid.GetBufferCount: integer;
begin
Result := ClientHeight div DefaultRowHeight;
if dgTitles in Options then
Dec(Result, 1);
end;
procedure TCustomDBGrid.UpdateGridColumnSizes;
var
i: Integer;
begin
if FDefaultColWidths then begin
if dgIndicator in Options then
ColWidths[0]:=12;
if NeedAutoSizeColumns then
UpdateAutoSizeColumns
else
for i:=FirstGridColumn to ColCount-1 do
ColWidths[i] := GetColumnWidth(i);
end;
end;
procedure TCustomDBGrid.UpdateScrollbarRange;
var
aRange, aPage, aPos: Integer;
ScrollInfo: TScrollInfo;
begin
if not HandleAllocated then exit;
GetScrollBarParams(aRange, aPage, aPos);
FillChar(ScrollInfo, SizeOf(ScrollInfo), 0);
ScrollInfo.cbSize := SizeOf(ScrollInfo);
{TODO: try to move this out}
{$ifdef WINDOWS}
ScrollInfo.fMask := SIF_ALL or SIF_DISABLENOSCROLL;
ScrollInfo.ntrackPos := 0;
{$else}
ScrollInfo.fMask := SIF_ALL or SIF_UPDATEPOLICY;
//ScrollInfo.ntrackPos := SB_POLICY_CONTINUOUS;
ScrollInfo.ntrackPos := SB_POLICY_DISCONTINUOUS;
{$endif}
ScrollInfo.nMin := 0;
ScrollInfo.nMax := aRange;
ScrollInfo.nPos := Min(aPos,aRange-aPage);
ScrollInfo.nPage := aPage;
// the redraw argument of SetScrollInfo means under gtk
// if the scrollbar is visible or not, in windows it
// seems to mean if the scrollbar is redrawn or not
// to reflect the scrollbar changes made
SetScrollInfo(Handle, SB_VERT, ScrollInfo,
(ScrollBars in [ssBoth, ssVertical]) or
((Scrollbars in [ssAutoVertical, ssAutoBoth]) and (aRange>aPAge))
);
FOldPosition := aPos;
{$ifdef dbgDBGrid}
DebugLn('UpdateScrollBarRange: Handle=',IntToStr(Handle),
' aRange=', IntToStr(aRange),
' aPage=', IntToStr(aPage), ' aPos=', IntToStr(aPos));
{$endif}
end;
procedure TCustomDBGrid.doLayoutChanged;
begin
if csDestroying in ComponentState then
exit;
{$ifdef dbgDBGrid} DebugLn('doLayoutChanged INIT'); {$endif}
BeginUpdate;
if UpdateGridCounts=0 then
EmptyGrid;
EndUpdate;
UpdateScrollbarRange;
{$ifdef dbgDBGrid} DebugLn('doLayoutChanged FIN'); {$endif}
end;
{
procedure TCustomDBGrid.WriteColumns(Writer: TWriter);
begin
if Columns.IsDefault then
Writer.WriteCollection(nil)
else
Writer.WriteCollection(Columns);
end;
}
procedure TCustomDBGrid.RestoreEditor;
begin
if EditorMode then begin
EditorMode := False;
EditorMode := True;
end;
end;
function TCustomDBGrid.IsEOF: boolean;
begin
with FDatalink do
result :=
Active and DataSet.EOF;
end;
function TCustomDBGrid.ValidDataSet: boolean;
begin
result := FDatalink.Active And (FDatalink.DataSet<>nil)
end;
function TCustomDBGrid.InsertCancelable: boolean;
begin
with FDatalink.DataSet do
Result := (State=dsInsert) and not (Modified or FDataLink.FModified);
end;
procedure TCustomDBGrid.StartUpdating;
begin
if not UpdatingData then begin
{$ifdef dbgDBGrid} DebugLn('DBGrid.StartUpdating');{$endif}
Include(FGridStatus, gsUpdatingData);
FOldControlStyle := ControlStyle;
ControlStyle := ControlStyle + [csActionClient];
LockEditor;
end
else
{$ifdef dbgDBGrid} DebugLn('WARNING: multiple call to StartUpdating');{$endif}
end;
procedure TCustomDBGrid.EndUpdating;
begin
{$ifdef dbgDBGrid} DebugLn('DBGrid.EndUpdating');{$endif}
Exclude(FGridStatus, gsUpdatingData);
ControlStyle := FOldControlStyle;
UnLockEditor;
if csActionClient in ControlStyle then
DebugLn('WARNING: still got csActionClient');
end;
function TCustomDBGrid.UpdatingData: boolean;
begin
result := gsUpdatingData in FGridStatus;
end;
procedure TCustomDBGrid.AddAutomaticColumns;
var
i: Integer;
F: TField;
begin
// add as many columns as there are fields in the dataset
// do this only at runtime.
if (csDesigning in ComponentState) or not FDatalink.Active or
(gsRemovingAutoColumns in FGridStatus) or
not (dgeAutoColumns in OptionsExtra)
then
exit;
Include(FGridStatus, gsAddingAutoColumns);
try
for i:=0 to FDataLink.DataSet.FieldCount-1 do begin
F:= FDataLink.DataSet.Fields[i];
if TDBGridColumns(Columns).ColumnFromField(F) <> nil then
// this field is already in the collection. This could only happen
// if AddAutomaticColumns was called out of LayoutChanged.
// to avoid duplicate columns skip this field.
continue;
if (F<>nil) then begin
with TDBGridColumns(Columns).Add do begin
FIsAutomaticColumn := True;
Field := F;
Visible := F.Visible;
end;
end;
end;
// honor the field.index
TDBGridColumns(Columns).ResetColumnsOrder(coFieldIndexOrder);
finally
Exclude(FGridStatus, gsAddingAutoColumns);
end;
end;
procedure TCustomDBGrid.AssignTo(Dest: TPersistent);
begin
if Dest is TCustomDbGrid then begin
// TODO
end else
inherited AssignTo(Dest);
end;
procedure TCustomDBGrid.UpdateAutoSizeColumns;
var
ACol,ARow,w: Integer;
DatalinkActive: boolean;
CurActiveRecord: Integer;
Field: TField;
ColWidth: Integer;
tmpCanvas: TCanvas;
C: TGridColumn;
s: string;
begin
if gsAutoSized in GridStatus then
exit;
BeginLayout;
DatalinkActive := FDatalink.Active;
if DatalinkActive then
CurActiveRecord := FDatalink.ActiveRecord;
tmpCanvas := GetWorkingCanvas(Canvas);
try
for aCol:=FixedCols to ColCount-1 do begin
Field := GetFieldFromGridColumn(ACol);
C := ColumnFromGridColumn(ACol);
if (C<>nil) and (C.Title<>nil) then begin
tmpCanvas.Font := C.Title.Font;
ColWidth := tmpCanvas.TextWidth(trim(C.Title.Caption));
tmpCanvas.Font := C.Font;
end else begin
if (Field<>nil) then begin
tmpCanvas.Font := TitleFont;
ColWidth := tmpCanvas.TextWidth(Field.FieldName);
end
else
ColWidth := 0;
tmpCanvas.Font := Font;
end;
if (Field<>nil) and DatalinkActive then
for ARow := FixedRows to RowCount-1 do begin
FDatalink.ActiveRecord := ARow - FixedRows;
if Field.dataType<>ftBlob then
s := trim(Field.DisplayText)
else
s := '(blob)';
w := tmpCanvas.TextWidth(s);
if w>ColWidth then
ColWidth := w;
end;
if ColWidth=0 then
ColWidth := GetColumnWidth(ACol);
ColWidths[ACol] := ColWidth + 15;
end;
finally
if TmpCanvas<>Canvas then
FreeWorkingCanvas(tmpCanvas);
if DatalinkActive then
FDatalink.ActiveRecord := CurActiveRecord;
include(FGridStatus, gsAutoSized);
EndLayout;
end;
end;
procedure TCustomDBGrid.SwapCheckBox;
var
SelField: TField;
TempColumn: TColumn;
begin
if not GridCanModify then
exit;
SelField := SelectedField;
TempColumn := TColumn(ColumnFromGridColumn(Col));
if (SelField<>nil) and (TempColumn<>nil) and not TempColumn.ReadOnly and
FDatalink.Edit then
begin
if SelField.DataType=ftBoolean then
SelField.AsBoolean := not SelField.AsBoolean
else
begin
if TempColumn.ValueChecked=SelField.AsString then
SelField.AsString := TempColumn.ValueUnchecked
else
SelField.AsString := TempColumn.ValueChecked;
end;
end;
end;
procedure TCustomDBGrid.ToggleSelectedRow;
begin
SelectRecord(not FSelectedRows.CurrentRowSelected);
end;
procedure TCustomDBGrid.LinkActive(Value: Boolean);
begin
if not Value then
RemoveAutomaticColumns;
LayoutChanged;
end;
procedure TCustomDBGrid.LayoutChanged;
begin
if csDestroying in ComponentState then
exit;
if FLayoutChangedCount=0 then begin
BeginLayout;
if Columns.Count>0 then
TDBGridColumns(Columns).LinkFields
else if not FDataLink.Active then
FDataLink.BufferCount := 0
else
AddAutomaticColumns;
EndLayout;
end;
end;
procedure TCustomDBGrid.Loaded;
begin
LayoutChanged;
inherited Loaded;
end;
type
TProtFields=class(TFields)
end;
procedure TCustomDBGrid.ColRowMoved(IsColumn: Boolean; FromIndex,
ToIndex: Integer);
var
F: TField;
begin
if IsColumn then begin
if Columns.Enabled then
inherited ColRowMoved(IsColumn, FromIndex, ToIndex)
else if FDatalink.Active and (FDataLink.DataSet<>nil) then begin
F := GetDsFieldFromGridColumn(FromIndex);
if F<>nil then begin
TProtFields(FDatalink.DataSet.Fields).SetFieldIndex( F, ToIndex - FirstGridColumn );
end;
end;
if Assigned(OnColumnMoved) then
OnColumnMoved(Self, FromIndex, ToIndex);
end;
end;
function TCustomDBGrid.ColumnEditorStyle(aCol: Integer; F: TField): TColumnButtonStyle;
begin
result := cbsAuto;
if Columns.Enabled then
result := ColumnFromGridColumn(aCol).ButtonStyle;
result := DefaultEditorStyle(result, F);
end;
function TCustomDBGrid.CreateColumns: TGridColumns;
begin
result := TDBGridColumns.Create(Self, TColumn);
end;
procedure TCustomDBGrid.CreateWnd;
begin
inherited CreateWnd;
LayoutChanged;
if Scrollbars in [ssBoth, ssVertical, ssAutoBoth, ssAutoVertical] then
ScrollBarShow(SB_VERT, True);
end;
procedure TCustomDBGrid.DefineProperties(Filer: TFiler);
{
function HasColumns: boolean;
var
C: TGridColumns;
begin
if Filer.Ancestor <> nil then
C := TCustomGrid(Filer.Ancestor).Columns
else
C := Columns;
if C<>nil then
result := not C.IsDefault
else
result := false;
end;
}
begin
// simply avoid to call TCustomGrid.DefineProperties method
// which defines ColWidths,Rowheights,Cells
//Filer.DefineProperty('Columns', @ReadColumns, @WriteColumns, HasColumns);
end;
procedure TCustomDBGrid.DefaultDrawCell(aCol, aRow: Integer; aRect: TRect;
aState: TGridDrawState);
var
S: string;
F: TField;
cbs: TColumnButtonStyle;
begin
// background
if (gdFixed in aState) and (TitleStyle=tsNative) then
DrawThemedCell(aCol, aRow, aRect, aState)
else
Canvas.FillRect(aRect);
if gdFixed in aState then
DrawFixedText(aCol, aRow, aRect, aState)
else
if not FDrawingEmptyDataset then begin
F := GetFieldFromGridColumn(aCol);
cbs := ColumnEditorStyle(aCol, F);
case cbs of
cbsCheckBoxColumn:
DrawCheckBoxBitmaps(aCol, aRect, F);
else
begin
if cbs=cbsButtonColumn then
DrawButtonCell(aCol, aRow, aRect, aState);
{$ifdef dbggridpaint}
DbgOut(' Col=%d',[ACol]);
{$endif}
if F<>nil then begin
{$ifdef dbgGridPaint}
DbgOut(' Field=%s',[F.FieldName]);
{$endif}
if F.dataType <> ftBlob then
S := F.DisplayText
else
S := '(blob)';
end else
S := '';
{$ifdef dbggridpaint}
DbgOut(' Value=%s ',[S]);
{$endif}
DrawCellText(aCol,aRow,aRect,aState,S);
end;
end;
end;
end;
function TCustomDBGrid.DefaultEditorStyle(const Style: TColumnButtonStyle;
const F: TField): TColumnButtonStyle;
begin
result := Style;
if (Result=cbsAuto) and (F<>nil) then
case F.DataType of
ftBoolean: Result := cbsCheckboxColumn;
end;
if (result = cbsCheckBoxColumn) and not (dgeCheckboxColumn in FExtraOptions) then
Result := cbsAuto;
end;
procedure TCustomDBGrid.DoOnChangeBounds;
begin
BeginUpdate;
inherited DoOnChangeBounds;
if HandleAllocated then
LayoutChanged;
EndUpdate;
end;
procedure TCustomDBGrid.DoPrepareCanvas(aCol, aRow: Integer;
aState: TGridDrawState);
var
DataCol: Integer;
begin
if Assigned(OnPrepareCanvas) and (ARow>=FixedRows) then begin
DataCol := ColumnIndexFromGridColumn(aCol);
if DataCol>=0 then
OnPrepareCanvas(Self, DataCol, TColumn(Columns[DataCol]), aState);
end;
end;
procedure TCustomDBGrid.BeforeMoveSelection(const DCol,DRow: Integer);
begin
{$ifdef dbgDBGrid}DebugLn('DBGrid.BefMovSel INIT');{$endif}
inherited BeforeMoveSelection(DCol, DRow);
if DCol<>Col then begin
if assigned(OnColExit) then
OnColExit(Self);
FColEnterPending:=True;
end;
{$ifdef dbgDBGrid}DebugLn('DBGrid.BefMovSel END');{$endif}
end;
procedure TCustomDBGrid.HeaderClick(IsColumn: Boolean; index: Integer);
var
Column: TColumn;
begin
if Assigned(OnTitleClick) and IsColumn then begin
Column := TColumn(ColumnFromGridColumn(Index));
if Column<>nil then
OnTitleClick(Column);
end;
end;
procedure TCustomDBGrid.KeyDown(var Key: Word; Shift: TShiftState);
type
TOperation=(opMoveBy,opCancel,opAppend,opInsert,opDelete);
var
DeltaCol,DeltaRow: Integer;
procedure DoOnKeyDown;
begin
{$ifdef dbgGrid}DebugLn('DoOnKeyDown INIT');{$endif}
if Assigned(OnKeyDown) then
OnKeyDown(Self, Key, Shift);
{$ifdef dbgGrid}DebugLn('DoOnKeyDown FIN');{$endif}
end;
{$ifdef dbgGrid}
function OperToStr(AOper: TOperation): string;
begin
case AOper of
opMoveBy: result := 'opMoveBy';
opCancel: result := 'opCancel';
opAppend: result := 'opAppend';
opInsert: result := 'opInsert';
opDelete: result := 'opDelete';
end;
end;
{$endif}
procedure DoOperation(AOper: TOperation; Arg: Integer = 0);
begin
{$IfDef dbgGrid}DebugLn('KeyDown.DoOperation(%s,%d) INIT',[OperToStr(AOper),arg]);{$Endif}
GridFlags := GridFlags + [gfEditingDone];
case AOper of
opMoveBy:
FDatalink.MoveBy(Arg);
opCancel:
begin
if EditorMode then
EditorCancelEditing;
FDatalink.Dataset.Cancel;
end;
opAppend:
FDatalink.Dataset.Append;
opInsert:
FDatalink.Dataset.Insert;
opDelete:
FDatalink.Dataset.Delete;
end;
GridFlags := GridFlags - [gfEditingDone];
{$IfDef dbgGrid}DebugLn('KeyDown.DoOperation(%s,%d) DONE',[OperToStr(AOper),arg]);{$Endif}
end;
procedure SelectNext(const AStart,ADown:Boolean);
var
N: Integer;
CurBookmark: TBookmarkStr;
begin
if dgPersistentMultiSelect in Options then
exit;
if (ssShift in Shift) then begin
CurBookmark := FDatalink.DataSet.Bookmark;
if FKeyBookmark='' then
FKeyBookmark:=CurBookmark;
if (FKeyBookmark=CurBookmark) then begin
if AStart then begin
SelectRecord(true);
if ADown then
FKeySign := 1
else
FKeySign := -1;
exit;
end;
FKeySign := 0;
end;
n := 4*Ord(FKeySign>=0) + 2*Ord(ADown) + 1*Ord(AStart);
case n of
0,6,8..11:
begin
SelectRecord(True);
end;
3,5:
begin
SelectRecord(False);
end;
end;
end else
ClearSelection(true);
end;
function doVKDown: boolean;
begin
{$ifdef dbgGrid}DebugLn('DoVKDown INIT');{$endif}
if InsertCancelable then
begin
if IsEOF then
result:=true
else begin
doOperation(opCancel);
result := false;
end;
end else begin
result:=false;
SelectNext(true,true);
doOperation(opMoveBy, 1);
if GridCanModify and FDataLink.EOF then
doOperation(opAppend)
else
SelectNext(false,true);
end;
{$ifdef dbgGrid}DebugLn('DoVKDown FIN');{$endif}
end;
function DoVKUP: boolean;
begin
{$ifdef dbgGrid}DebugLn('DoVKUP INIT');{$endif}
if InsertCancelable then
doOperation(opCancel)
else begin
SelectNext(true, false);
doOperation(opMoveBy, -1);
SelectNext(false, false);
end;
result := FDatalink.DataSet.BOF;
{$ifdef dbgGrid}DebugLn('DoVKUP FIN');{$endif}
end;
procedure MoveSel(AReset: boolean);
begin
if (DeltaCol<>0) or (DeltaRow<>0) then begin
if DeltaRow > 0 then begin
if doVKDown then
//DeltaCol:=0; // tochk: strict? already in EOF, don't change column
end else
if DeltaRow < 0 then begin
if doVKUP then
//DeltaCol:=0; // tochk: strict? already in BOF, don't change column
end;
GridFlags := GridFlags + [gfEditingDone];
if (DeltaCol<>0) then
Col := Col + DeltaCol;
GridFlags := GridFlags - [gfEditingDone];
end else
if AReset then
ResetEditor;
end;
begin
{$IfDef dbgGrid}DebugLn('DBGrid.KeyDown %s INIT Key=%d',[Name,Key]);{$Endif}
case Key of
VK_TAB:
begin
doOnKeyDown;
if Key<>0 then begin
if dgTabs in Options then begin
if ((ssShift in shift) and
(Col<=GetFirstVisibleColumn) and (Row<=GetFirstVisibleRow)) then begin
if EditorKey then
GridFlags := GridFlags + [gfRevEditorTab];
exit;
end;
GetDeltaMoveNext(ssShift in Shift, DeltaCol, DeltaRow);
if (not (ssShift in Shift)) and (Row>=GetLastVisibleRow) and
(DeltaRow>0) and (Col=GetLastVisibleColumn) and
(FDatalink.Editing or not GridCanModify) then begin
exit;
end;
MoveSel(false);
Key := 0;
end;
end;
end;
VK_RETURN:
begin
doOnKeyDown;
if Key<>0 then begin
key:=0;
if (dgEditing in Options) and not EditorMode then
EditorMode:=true
else begin
GetDeltaMoveNext(ssShift in Shift, DeltaCol, DeltaRow);
MoveSel(True);
end;
end;
end;
VK_DELETE:
begin
doOnKeyDown;
if (Key<>0) and (ssCtrl in Shift) and GridCanModify and
not FDataLink.DataSet.IsEmpty then begin
if not (dgConfirmDelete in Options) or
(MessageDlg(rsDeleteRecord, mtConfirmation, mbOKCancel, 0 )<>mrCancel)
then begin
doOperation(opDelete);
key := 0;
end;
end;
end;
VK_DOWN:
if ValidDataSet then begin
DoOnKeyDown;
if Key<>0 then begin
doVKDown;
Key := 0;
end;
end;
VK_UP:
if ValidDataSet then begin
doOnKeyDown;
if Key<>0 then begin
doVKUp;
key := 0;
end;
end;
VK_NEXT:
begin
doOnKeyDown;
if Key<>0 then begin
doOperation(opMoveBy, VisibleRowCount);
ClearSelection(true);
Key := 0;
end;
end;
VK_PRIOR:
begin
doOnKeyDown;
if Key<>0 then begin
doOperation(opMoveBy, -VisibleRowCount);
ClearSelection(true);
key := 0;
end;
end;
VK_ESCAPE:
begin
doOnKeyDown;
if Key<>0 then begin
if EditorMode then begin
EditorCancelEditing;
if FDatalink.Active and not FDatalink.Dataset.Modified then
FDatalink.Modified := False;
Key := 0;
end else
if FDataLink.Active then
doOperation(opCancel);
end;
end;
VK_INSERT:
begin
doOnKeyDown;
if Key<>0 then
if GridCanModify then begin
doOperation(opInsert);
Key:=0;
end;
end;
VK_HOME:
begin
doOnKeyDown;
if Key<>0 then begin
if FDatalink.Active then begin
GridFlags := GridFlags + [gfEditingDone];
if ssCTRL in Shift then
FDataLink.DataSet.First
else
MoveNextSelectable(False, FixedCols, Row);
GridFlags := GridFlags - [gfEditingDone];
ClearSelection(true);
Key:=0;
end;
end;
end;
VK_END:
begin
doOnKeyDown;
if Key<>0 then begin
if FDatalink.Active then begin
GridFlags := GridFlags + [gfEditingDone];
if ssCTRL in shift then
FDatalink.DataSet.Last
else
MoveNextSelectable(False, ColCount-1, Row);
GridFlags := GridFlags - [gfEditingDone];
ClearSelection(true);
Key:=0;
end;
end;
end;
VK_SPACE:
begin
doOnKeyDown;
if Key<>0 then begin
if ColumnEditorStyle(Col, SelectedField) = cbsCheckboxColumn then begin
SwapCheckBox;
Key:=0;
end;
end;
end;
VK_MULTIPLY:
begin
if ssCtrl in Shift then
ToggleSelectedRow;
end;
else
inherited KeyDown(Key, Shift);
end;
{$IfDef dbgGrid}DebugLn('DBGrid.KeyDown END Key= %d',[Key]);{$Endif}
end;
procedure TCustomDBGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
var
Gz: TGridZone;
P: TPoint;
procedure doMouseDown;
begin
if assigned(OnMouseDown) then
OnMouseDown(Self, Button, Shift, X, Y);
end;
procedure doInherited;
begin
inherited MouseDown(Button, Shift, X, Y);
end;
procedure doMoveBy;
begin
{$IfDef dbgGrid} DebugLn('DBGrid.MouseDown MoveBy INIT'); {$Endif}
FDatalink.MoveBy(P.Y - Row);
{$IfDef dbgGrid} DebugLn('DBGrid.MouseDown MoveBy END'); {$Endif}
end;
procedure doMoveToColumn;
begin
{$IfDef dbgGrid} DebugLn('DBGrid.MouseDown MoveToCol INIT Col=', IntToStr(P.X)); {$Endif}
Col := P.X;
{$IfDef dbgGrid} DebugLn('DBGrid.MouseDown MoveToCol END'); {$Endif}
end;
procedure DoCancel;
begin
{$IfDef dbgGrid}DebugLn('DBGrid.MouseDown Dataset.CANCEL INIT');{$Endif}
if EditorMode then
EditorCancelEditing;
FDatalink.Dataset.cancel;
{$IfDef dbgGrid}DebugLn('DBGrid.MouseDown Dataset.CANCEL FIN');{$Endif}
end;
procedure DoAcceptValue;
begin
if EditorMode and FDatalink.FModified then
EditorMode := False;
end;
begin
{$ifdef dbgDBGrid}DebugLn('DBGrid.mousedown - INIT');{$endif}
if (csDesigning in componentState) {or not GCache.ValidGrid }then
exit;
if UpdatingData then begin
{$ifdef dbgDBGrid}DebugLn('DBGrid.MouseDown - UpdatingData');{$endif}
exit;
end;
if button<>mbLeft then begin
doInherited;
exit;
end;
{$IfDef dbgGrid} DebugLn('DBGrid.MouseDown INIT'); {$Endif}
Gz:=MouseToGridZone(X,Y);
case Gz of
gzFixedCells, gzFixedCols:
doInherited;
else
begin
FKeyBookmark:=''; // force new keyboard selection start
SetFocus;
P:=MouseToCell(Point(X,Y));
if Gz=gzFixedRows then
P.X := Col;
if P.Y=Row then begin
//doAcceptValue;
if ssCtrl in Shift then begin
doMouseDown;
ToggleSelectedRow;
end
else begin
ClearSelection(true);
if gz=gzFixedRows then
doMouseDown
else
doInherited;
end;
end else begin
doMouseDown;
if ValidDataSet then begin
if InsertCancelable and IsEOF then
doCancel;
doMoveBy;
end;
if ssCtrl in Shift then
ToggleSelectedRow
else begin
ClearSelection(true);
doMoveToColumn;
end;
end;
end;
end;
{$IfDef dbgGrid} DebugLn('DBGrid.MouseDown END'); {$Endif}
end;
procedure TCustomDBGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
if fGridState=gsSelecting then
exit
else
inherited MouseMove(Shift, X, Y);
end;
procedure TCustomDBGrid.PrepareCanvas(aCol, aRow: Integer;
aState: TGridDrawState);
begin
inherited PrepareCanvas(aCol, aRow, aState);
if (not FDatalink.Active) and ((gdSelected in aState) or (gdFocused in aState)) then
Canvas.Brush.Color := Self.Color;
end;
procedure TCustomDBGrid.RemoveAutomaticColumns;
begin
if not (csDesigning in ComponentState) then
TDBGridColumns(Columns).RemoveAutoColumns;
end;
procedure TCustomDBGrid.ResetSizes;
begin
LayoutChanged;
inherited ResetSizes;
end;
procedure TCustomDBGrid.SelectEditor;
var
aEditor: TWinControl;
begin
{$ifdef dbgDBGrid}
DebugLn('TCustomDBGrid.SelectEditor INIT Editor=',dbgsname(editor));
{$endif}
if (FDatalink<>nil) and FDatalink.Active then begin
inherited SelectEditor;
if Assigned(OnSelectEditor) then begin
aEditor:=Editor;
OnSelectEditor(Self, SelectedColumn, aEditor);
Editor:=aEditor;
end;
end else
Editor := nil;
{$ifdef dbgDBGrid}
DebugLn('TCustomDBGrid.SelectEditor DONE Editor=',dbgsname(editor));
{$endif}
end;
procedure TCustomDBGrid.SetEditText(ACol, ARow: Longint; const Value: string);
begin
FTempText := Value;
end;
procedure TCustomDBGrid.SetFixedCols(const AValue: Integer);
begin
if (FixedCols=AValue) or (AValue<FirstGridColumn) then
exit;
inherited SetFixedCols(AValue);
end;
function TCustomDBGrid.SelectCell(aCol, aRow: Integer): boolean;
begin
Result:= (ColWidths[aCol] > 0) and (RowHeights[aRow] > 0);
end;
procedure TCustomDBGrid.BeginLayout;
begin
inc(FLayoutChangedCount);
end;
procedure TCustomDBGrid.EditingColumn(aCol: Integer; Ok: Boolean);
begin
{$ifdef dbgDBGrid} DebugLn(['DBGrid.EditingColumn INIT aCol=', aCol, ' Ok=', ok]); {$endif}
if Ok then begin
FEditingColumn := aCol;
FDatalink.Modified := True;
end
else
FEditingColumn := -1;
{$ifdef dbgDBGrid} DebugLn('DBGrid.EditingColumn END'); {$endif}
end;
procedure TCustomDBGrid.EditorCancelEditing;
begin
EditingColumn(FEditingColumn, False); // prevents updating the value
if EditorMode then begin
EditorMode := False;
if dgAlwaysShowEditor in Options then
EditorMode := True;
end;
end;
procedure TCustomDBGrid.EditorDoGetValue;
begin
{$ifdef dbgDBGrid}DebugLn('DBGrid.EditorDoGetValue INIT');{$endif}
inherited EditordoGetValue;
UpdateData;
{$ifdef dbgDBGrid}DebugLn('DBGrid.EditorDoGetValue FIN');{$endif}
end;
procedure TCustomDBGrid.CellClick(const aCol, aRow: Integer; const Button:TMouseButton);
begin
if Button<>mbLeft then
exit;
if (aCol>=FirstGridColumn)and(aRow>=FixedRows) then
begin
if ColumnEditorStyle(ACol, SelectedField) = cbsCheckboxColumn then begin
// react only if overriden editor is hidden
if (Editor=nil) or not EditorMode then
SwapCheckBox
end;
end;
if Assigned(OnCellClick) then
OnCellClick(TColumn(ColumnFromGridColumn(aCol)));
end;
procedure TCustomDBGrid.EndLayout;
begin
dec(FLayoutChangedCount);
if FLayoutChangedCount = 0 then
DoLayoutChanged;
end;
function TCustomDBGrid.GetDefaultColumnAlignment(Column: Integer): TAlignment;
var
F: TField;
begin
F := GetDsFieldFromGridColumn(Column);
if F<>nil then
result := F.Alignment
else
result := taLeftJustify;
end;
function TCustomDBGrid.GetDefaultColumnWidth(Column: Integer): Integer;
begin
Result := DefaultFieldColWidth(GetDsFieldFromGridColumn(Column));
end;
function TCustomDBGrid.GetDefaultColumnReadOnly(Column: Integer): boolean;
var
F: Tfield;
begin
result := true;
if not Self.ReadOnly and (FDataLink.Active and not FDatalink.ReadOnly) then begin
F := GetDsFieldFromGridColumn(Column);
result := (F=nil) or F.ReadOnly;
end;
end;
function TCustomDBGrid.GetDefaultColumnTitle(Column: Integer): string;
var
F: Tfield;
begin
F := GetDsFieldFromGridColumn(Column);
if F<>nil then
Result := F.DisplayName
else
Result := '';
end;
function TCustomDBGrid.GetDefaultRowHeight: integer;
begin
result := inherited GetDefaultRowHeight;
Dec(Result, 2); // a litle smaller for dbgrid
end;
procedure TCustomDBGrid.DoExit;
begin
{$ifdef dbgDBGrid}DebugLn('DBGrid.DoExit INIT');{$Endif}
if ValidDataSet and (dgCancelOnExit in Options) and
InsertCancelable then
begin
FDataLink.DataSet.Cancel;
EditingColumn(FEditingColumn, False);
end;
inherited DoExit;
{$ifdef dbgDBGrid}DebugLn('DBGrid.DoExit FIN');{$Endif}
end;
function TCustomDBGrid.DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint
): Boolean;
begin
Result := False;
if Assigned(OnMouseWheelDown) then
OnMouseWheelDown(Self, Shift, MousePos, Result);
if not Result and FDatalink.Active then begin
FDatalink.MoveBy(1);
Result := True;
end;
end;
function TCustomDBGrid.DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint
): Boolean;
begin
Result := False;
if Assigned(OnMouseWheelUp) then
OnMouseWheelUp(Self, Shift, MousePos, Result);
if not Result and FDatalink.Active then begin
FDatalink.MoveBy(-1);
Result := True;
end;
end;
function TCustomDBGrid.GetEditMask(aCol, aRow: Longint): string;
var
aField: TField;
begin
Result := '';
if FDataLink.Active then begin
aField := GetFieldFromGridColumn(aCol);
if (aField<>nil) then begin
{$ifdef EnableFieldEditMask}
// enable following line if TField gets in the future a MaskEdit property
//Result := aField.EditMask;
if assigned(OnFieldEditMask) then
OnFieldEditMask(Self, AField, Result);
{$endif}
end;
end;
end;
function TCustomDBGrid.GetEditText(aCol, aRow: Longint): string;
var
aField: TField;
begin
if FDataLink.Active then begin
aField := GetFieldFromGridColumn(aCol);
if aField<>nil then begin
Result := aField.Text;
end;
end;
end;
function TCustomDBGrid.GetIsCellSelected(aCol, aRow: Integer): boolean;
begin
Result:=inherited GetIsCellSelected(aCol, aRow) or
FDrawingMultiSelRecord;
end;
function TCustomDBGrid.GetIsCellTitle(aCol, aRow: Integer): boolean;
begin
result := (FixedRows>0) and (aRow=0);
if result and Columns.Enabled then
result := (aCol>=FirstGridColumn);
end;
procedure TCustomDBGrid.GetSelectedState(AState: TGridDrawState; out
IsSelected: boolean);
begin
inherited GetSelectedState(AState, IsSelected);
if IsSelected and not Self.Focused and not(dgAlwaysShowSelection in Options) then
IsSelected := false;
end;
function TCustomDBGrid.GridCanModify: boolean;
begin
result := not ReadOnly and (dgEditing in Options) and not FDataLink.ReadOnly
and FDataLink.Active and FDatalink.DataSet.CanModify;
end;
procedure TCustomDBGrid.GetSBVisibility(out HsbVisible, VsbVisible: boolean);
var
aRange,aPage,aPos: Integer;
begin
inherited GetSBVisibility(HsbVisible, VsbVisible);
VSbVisible := (ScrollBars in [ssVertical, ssBoth]);
if not VSbVisible and ScrollBarAutomatic(ssVertical) then begin
GetScrollbarParams(aRange,aPage, aPos);
if ARange>aPage then
VSbVisible:=True;
end;
end;
procedure TCustomDBGrid.GetSBRanges(const HsbVisible, VsbVisible: boolean; out
HsbRange, VsbRange, HsbPage, VsbPage, HsbPos, VsbPos: Integer);
begin
inherited GetSBRanges(HsbVisible, VsbVisible, HsbRange, VsbRange, HsbPage, VsbPage, HsbPos, VsbPos);
if VSbVisible then
GetScrollbarParams(VsbRange, VsbPage, VsbPos)
else begin
VsbRange := 0;
VsbPage := 0;
VsbPos := 0;
end;
end;
procedure TCustomDBGrid.MoveSelection;
begin
{$ifdef dbgDBGrid}DebugLn('DBGrid.MoveSelection INIT');{$Endif}
inherited MoveSelection;
if FColEnterPending and Assigned(OnColEnter) then begin
OnColEnter(Self);
end;
FColEnterPending:=False;
UpdateActive;
{$ifdef dbgDBGrid}DebugLn('DBGrid.MoveSelection FIN');{$Endif}
end;
procedure TCustomDBGrid.DrawAllRows;
var
CurActiveRecord: Integer;
begin
if FDataLink.Active then begin
{$ifdef dbgGridPaint}
DebugLn;
DebugLn('%s DrawAllRows: Link.ActiveRecord=%d, Row=%d',[Name, FDataLink.ActiveRecord, Row]);
{$endif}
CurActiveRecord:=FDataLink.ActiveRecord;
FDrawingEmptyDataset:=FDatalink.DataSet.IsEmpty;
end else
FDrawingEmptyDataset:=True;
try
inherited DrawAllRows;
finally
if FDataLink.Active then begin
FDataLink.ActiveRecord:=CurActiveRecord;
{$ifdef dbgGridPaint}
DebugLn('%s DrawAllRows END Link.ActiveRecord=%d, Row=%d',[Name, FDataLink.ActiveRecord, Row]);
DebugLn;
{$endif}
end;
end;
end;
procedure TCustomDBGrid.DrawFocusRect(aCol, aRow: Integer; ARect: TRect);
begin
// Draw focused cell if we have the focus
if Self.Focused and (dgAlwaysShowSelection in Options) and
FDatalink.Active and DefaultDrawing then
begin
CalcFocusRect(aRect);
DrawRubberRect(Canvas, aRect, FocusColor);
end;
end;
//
procedure TCustomDBGrid.DrawRow(ARow: Integer);
begin
if (ARow>=FixedRows) and FDataLink.Active then begin
//if (Arow>=FixedRows) and FCanBrowse then
FDataLink.ActiveRecord:=ARow-FixedRows;
FDrawingActiveRecord := ARow = Row;
FDrawingMultiSelRecord := (dgMultiSelect in Options) and
SelectedRows.CurrentRowSelected
end else begin
FDrawingActiveRecord := False;
FDrawingMultiSelRecord := False;
end;
{$ifdef dbgGridPaint}
DbgOut('DrawRow Row=', IntToStr(ARow), ' Act=', dbgs(FDrawingActiveRecord));
{$endif}
inherited DrawRow(ARow);
{$ifdef dbgGridPaint}
DebugLn(' End Row')
{$endif}
end;
procedure TCustomDBGrid.DrawCell(aCol, aRow: Integer; aRect: TRect;
aState: TGridDrawState);
var
DataCol: Integer;
begin
PrepareCanvas(aCol, aRow, aState);
{$ifdef dbgGridPaint}
DbgOut(' ',IntToStr(aCol));
if gdSelected in aState then DbgOut('S');
if gdFocused in aState then DbgOut('*');
if gdFixed in aState then DbgOut('F');
{$endif dbgGridPaint}
if DefaultDrawing then
DefaultDrawCell(aCol, aRow, aRect, aState);
if (ARow>=FixedRows) and Assigned(OnDrawColumnCell) and
not (csDesigning in ComponentState) then begin
DataCol := ColumnIndexFromGridColumn(aCol);
if DataCol>=0 then
OnDrawColumnCell(Self, aRect, DataCol, TColumn(Columns[DataCol]), aState);
end;
DrawCellGrid(aCol, aRow, aRect, aState);
end;
procedure TCustomDBGrid.DrawCheckboxBitmaps(aCol: Integer; aRect: TRect;
F: TField);
var
AState: TCheckboxState;
begin
if (aCol=Col) and FDrawingActiveRecord then begin
// show checkbox only if overriden editor is hidden
if EditorMode then
exit;
end;
// by SSY
if (F<>nil) then
if F.DataType=ftBoolean then
if F.IsNull then
AState := cbGrayed
else
if F.AsBoolean then
AState := cbChecked
else
AState := cbUnChecked
else
if F.AsString=ColumnFromGridColumn(aCol).ValueChecked then
AState := cbChecked
else
if F.AsString=ColumnFromGridColumn(aCol).ValueUnChecked then
AState := cbUnChecked
else
AState := cbGrayed
else
AState := cbGrayed;
DrawGridCheckboxBitmaps(aCol, Row{dummy}, ARect, AState);
end;
procedure TCustomDBGrid.DrawFixedText(aCol, aRow: Integer; aRect: TRect;
aState: TGridDrawState);
function GetDatasetState: TDataSetState;
begin
if FDatalink.Active then
result := FDataLink.DataSet.State
else
result := dsInactive;
end;
begin
if (ACol=0) and (dgIndicator in Options) and FDrawingActiveRecord then begin
DrawIndicator(Canvas, aRect, GetDataSetState, FDrawingMultiSelRecord);
{$ifdef dbgGridPaint}
dbgOut('>');
{$endif}
end else
if (ACol=0) and (dgIndicator in Options) and FDrawingMultiSelRecord then
DrawIndicator(Canvas, aRect, dsCurValue{dummy}, True)
else
DrawColumnText(aCol, aRow, aRect, aState);
end;
procedure TCustomDBGrid.DrawColumnText(aCol, aRow: Integer; aRect: TRect;
aState: TGridDrawState);
var
F: TField;
begin
if GetIsCellTitle(aCol, aRow) then
inherited DrawColumnText(aCol, aRow, aRect, aState)
else begin
F := GetFieldFromGridColumn(aCol);
if F<>nil then
DrawCellText(aCol, aRow, aRect, aState, F.DisplayText)
end;
end;
procedure TCustomDBGrid.DrawIndicator(ACanvas: TCanvas; R: TRect;
Opt: TDataSetState; MultiSel: boolean);
var
dx,dy, x, y: Integer;
procedure CenterY;
begin
y := R.Top + (R.Bottom-R.Top) div 2;
end;
procedure CenterX;
begin
X := R.Left + (R.Right-R.Left) div 2;
end;
procedure DrawEdit(clr: Tcolor);
begin
ACanvas.Pen.Color := clr;
CenterY;
CenterX;
ACanvas.MoveTo(X-2, Y-Dy);
ACanvas.LineTo(X+3, Y-Dy);
ACanvas.MoveTo(X, Y-Dy);
ACanvas.LineTo(X, Y+Dy);
ACanvas.MoveTo(X-2, Y+Dy);
ACanvas.LineTo(X+3, Y+Dy);
end;
begin
dx := 6;
dy := 6;
case Opt of
dsBrowse:
begin //
ACanvas.Brush.Color:=clBlack;
ACanvas.Pen.Color:=clBlack;
CenterY;
x:= R.Left+3;
if MultiSel then begin
ACanvas.Polyline([point(x,y-dy), point(x+dx,y),point(x,y+dy), point(x,y+dy-1)]);
ACanvas.Polyline([point(x,y-dy+1),point(x+dx-1,y),point(x, y+dy-1), point(x,y+dy-2)]);
CenterX;
Dec(X,3);
ACanvas.Ellipse(Rect(X-2,Y-2,X+2,Y+2));
end else
ACanvas.Polygon([point(x,y-dy),point(x+dx,y),point(x, y+dy),point(x,y-dy)]);
end;
dsEdit:
DrawEdit(clBlack);
dsInsert:
DrawEdit(clGreen);
else
if MultiSel then begin
ACanvas.Brush.Color:=clBlack;
ACanvas.Pen.Color:=clBlack;
CenterX;
CenterY;
ACanvas.Ellipse(Rect(X-3,Y-3,X+3,Y+3));
end;
end;
end;
function TCustomDBGrid.EditorCanAcceptKey(const ch: TUTF8Char): boolean;
var
aField: TField;
begin
result := False;
if FDataLink.Active then begin
aField := SelectedField;
if aField<>nil then begin
Result := IsValidChar(AField, Ch) and not aField.Calculated and
(aField.DataType<>ftAutoInc) and not aField.IsBlob;
end;
end;
end;
function TCustomDBGrid.EditorIsReadOnly: boolean;
var
AField : TField;
FieldList: TList;
I: Integer;
begin
Result := inherited EditorIsReadOnly;
if not Result then begin
AField := GetFieldFromGridColumn(Col);
if assigned(AField) then begin
// if field can't be modified, it's assumed readonly
result := not AField.CanModify;
// if field is readonly, check if it's a lookup field
if result and AField.Lookup then begin
FieldList := TList.Create;
try
AField.DataSet.GetFieldList(FieldList, AField.KeyFields);
// check if any keyfields are there
result := (FieldList.Count=0); // if not simply is still readonly
// if yes assumed keyfields are modifiable
for I := 0 to FieldList.Count-1 do
if not TField(FieldList[I]).CanModify then begin
result := true; // at least one keyfield is readonly
break;
end;
finally
FieldList.Free;
end;
end;
// if it's not readonly and is not already editing, start editing.
if not result and not FDatalink.Editing then begin
Include(FGridStatus, gsStartEditing);
Result := not FDataLink.Edit;
Exclude(FGridStatus, gsStartEditing);
end;
end
else
result := true; // field is nil so it's readonly
EditingColumn(Col, not Result);
end;
end;
procedure TCustomDBGrid.EditorTextChanged(const aCol, aRow: Integer;
const aText: string);
begin
if not EditorIsReadonly then
SetEditText(aCol, aRow, aText);
end;
procedure TCustomDBGrid.HeaderSized(IsColumn: Boolean; Index: Integer);
var
i: Integer;
begin
if IsColumn then begin
if Columns.Enabled then begin
i := ColumnIndexFromGridColumn(Index);
if i>=0 then
Columns[i].Width := ColWidths[Index];
end;
FDefaultColWidths := False;
if Assigned(OnColumnSized) then
OnColumnSized(Self);
end;
end;
function TCustomDBGrid.IsValidChar(AField: TField; AChar: TUTF8Char): boolean;
begin
result := False;
if Length(AChar)>1 then begin
// problem: AField should validate a unicode char, but AField has no
// such facility, ask the user, if user is not interested
// do ansi convertion and try with field.
{ TODO: is this really necessary?
if Assigned(FOnValidateUTF8Char) then begin
result := true;
OnValidateUT8Char(Self, AField, AChar, Result)
exit;
end else
}
AChar := UTF8ToSys(AChar);
end else
if Length(AChar)=0 then
exit;
Result := AField.IsValidChar(AChar[1])
end;
procedure TCustomDBGrid.UpdateActive;
var
PrevRow: Integer;
begin
if (csDestroying in ComponentState) or
(FDatalink=nil) or (not FDatalink.Active) or
(FDatalink.ActiveRecord<0) then
exit;
{$IfDef dbgDBGrid}
DebugLn(Name,'.UpdateActive: ActiveRecord=', dbgs(FDataLink.ActiveRecord),
' FixedRows=',dbgs(FixedRows), ' Row=', dbgs(Row));
{$endif}
PrevRow := Row;
Row:= FixedRows + FDataLink.ActiveRecord;
if PrevRow<>Row then
InvalidateCell(0, PrevRow);//(InvalidateRow(PrevRow);
InvalidateRow(Row);
end;
function TCustomDBGrid.UpdateGridCounts: Integer;
var
RecCount: Integer;
FRCount, FCCount: Integer;
begin
// find out the column count, if result=0 then
// there are no visible columns defined or dataset is inactive
// or there are no visible fields, ie the grid is blank
{$IfDef dbgDBGrid}DebugLn('TCustomDbgrid.UpdateGridCounts INIT');{$endif}
BeginUpdate;
try
Result := GetColumnCount;
if Result > 0 then begin
if dgTitles in Options then FRCount := 1 else FRCount := 0;
if dgIndicator in Options then FCCount := 1 else FCCount := 0;
InternalSetColCount(Result + FCCount);
if FDataLink.Active then begin
UpdateBufferCount;
RecCount := FDataLink.RecordCount;
if RecCount<1 then
RecCount := 1;
end else begin
RecCount := 0;
if FRCount=0 then
// need to be large enough to hold indicator
// if there is one, and if there are no titles
RecCount := FCCount;
end;
Inc(RecCount, FRCount);
RowCount := RecCount;
FixedRows := FRCount;
UpdateGridColumnSizes;
if FDatalink.Active and (FDatalink.ActiveRecord>=0) then
AdjustEditorBounds(Col, FixedRows + FDatalink.ActiveRecord);
end;
finally
EndUpdate;
end;
{$IfDef dbgDBGrid}DebugLn('TCustomDbgrid.UpdateGridCounts END');{$endif}
end;
constructor TCustomDBGrid.Create(AOwner: TComponent);
begin
FEditingColumn:=-1;
DragDx:=5;
inherited Create(AOwner);
FDataLink := TComponentDataLink.Create;//(Self);
FDataLink.OnRecordChanged:=@OnRecordChanged;
FDataLink.OnDatasetChanged:=@OnDataSetChanged;
FDataLink.OnDataSetOpen:=@OnDataSetOpen;
FDataLink.OnDataSetClose:=@OnDataSetClose;
FDataLink.OnNewDataSet:=@OnNewDataSet;
FDataLink.OnInvalidDataSet:=@OnInvalidDataset;
FDataLink.OnInvalidDataSource:=@OnInvalidDataSource;
FDataLink.OnDataSetScrolled:=@OnDataSetScrolled;
FDataLink.OnLayoutChanged:=@OnLayoutChanged;
FDataLink.OnEditingChanged:=@OnEditingChanged;
FDataLink.OnUpdateData:=@OnUpdateData;
FDataLink.VisualControl:= True;
FSelectedRows := TBookmarkList.Create(Self);
RenewColWidths;
FOptions := [dgColumnResize, dgColumnMove, dgTitles, dgIndicator, dgRowLines,
dgColLines, dgConfirmDelete, dgCancelOnExit, dgTabs, dgEditing,
dgAlwaysShowSelection];
inherited Options :=
[goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
goSmoothScroll, goColMoving, goTabs, goEditing, goDrawFocusSelected,
goColSizing ];
FExtraOptions := [dgeAutoColumns, dgeCheckboxColumn];
AutoAdvance := aaRightDown;
// What a dilema!, we need ssAutoHorizontal and ssVertical!!!
ScrollBars:=ssBoth;
end;
procedure TCustomDBGrid.AutoSizeColumns;
begin
RenewColWidths;
LayoutChanged;
end;
procedure TCustomDBGrid.InitiateAction;
begin
{$ifdef dbgDBGrid}DebugLn('===> DBGrid.InitiateAction INIT');{$endif}
inherited InitiateAction;
if (gsUpdatingData in FGridStatus) then begin
EndUpdating;
{
if EditorMode then begin
Editor.SetFocus;
EditorSelectAll;
end;
}
end;
{$ifdef dbgDBGrid}DebugLn('<=== DBGrid.InitiateAction FIN');{$endif}
end;
procedure TCustomDBGrid.DefaultDrawColumnCell(const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
S: string;
F: TField;
DataRow: Integer;
begin
F := Column.Field;
DataCol := GridColumnFromColumnIndex(DataCol);
if FDataLink.Active then
DataRow := FixedRows + FDataLink.ActiveRecord
else
DataRow := 0;
if DataCol>=FirstGridColumn then
case ColumnEditorStyle(DataCol, F) of
cbsCheckBoxColumn:
DrawCheckBoxBitmaps(DataCol, Rect, F);
else begin
if F<>nil then begin
if F.dataType <> ftBlob then
S := F.DisplayText
else
S := '(blob)';
end else
S := '';
DrawCellText(DataCol, DataRow, Rect, State, S);
end;
end;
end;
function TCustomDBGrid.EditorByStyle(Style: TColumnButtonStyle): TWinControl;
begin
// we want override the editor style if it is cbsAuto because
// field.datatype might be ftBoolean or some other cases
if Style=cbsAuto then
Style := ColumnEditorStyle(Col, SelectedField);
Result:=inherited EditorByStyle(Style);
end;
procedure TCustomDBGrid.ResetColWidths;
begin
if not FDefaultColWidths then begin
RenewColWidths;
LayoutChanged;
end;
end;
procedure TCustomDBGrid.SelectRecord(AValue: boolean);
begin
if dgMultiSelect in Options then
FSelectedRows.CurrentRowSelected := AValue;
end;
procedure TCustomDBGrid.GetScrollbarParams(out aRange, aPage, aPos: Integer);
begin
if (FDatalink<>nil) and FDatalink.Active then begin
if FDatalink.dataset.IsSequenced then begin
aRange := GetRecordCount + VisibleRowCount - 1;
aPage := VisibleRowCount;
if aPage<1 then aPage := 1;
if FDatalink.BOF then aPos := 0 else
if FDatalink.EOF then aPos := aRange
else
aPos := FDataLink.DataSet.RecNo - 1; // RecNo is 1 based
if aPos<0 then aPos:=0;
end else begin
aRange := 6;
aPage := 2;
if FDatalink.EOF then aPos := 4 else
if FDatalink.BOF then aPos := 0
else aPos := 2;
end;
end else begin
aRange := 0;
aPage := 0;
aPos := 0;
end;
end;
procedure TCustomDBGrid.CMGetDataLink(var Message: TLMessage);
begin
Message.Result := PtrUInt(FDataLink);
end;
procedure TCustomDBGrid.ClearSelection(selCurrent:boolean=false);
begin
if [dgMultiSelect,dgPersistentMultiSelect]*Options=[dgMultiSelect] then begin
if SelectedRows.Count>0 then
SelectedRows.Clear;
if SelCurrent then
SelectRecord(true);
end;
FKeyBookmark:='';
end;
function TCustomDBGrid.NeedAutoSizeColumns: boolean;
begin
result := (dgAutoSizeColumns in Options)
//and (HandleAllocated)
;
end;
procedure TCustomDBGrid.RenewColWidths;
begin
FDefaultColWidths := True;
exclude(FGridStatus, gsAutoSized);
end;
destructor TCustomDBGrid.Destroy;
begin
FSelectedRows.Free;
FDataLink.OnDataSetChanged:=nil;
FDataLink.OnRecordChanged:=nil;
FDataLink.Free;
inherited Destroy;
end;
{ TComponentDataLink }
function TComponentDataLink.GetFields(Index: Integer): TField;
begin
if (index>=0)and(index<DataSet.FieldCount) then result:=DataSet.Fields[index];
end;
function TComponentDataLink.GetDataSetName: string;
begin
Result:=FDataSetName;
if DataSet<>nil then Result:=DataSet.Name;
end;
procedure TComponentDataLink.SetDataSetName(const AValue: string);
begin
if FDataSetName<>AValue then FDataSetName:=AValue;
end;
procedure TComponentDataLink.RecordChanged(Field: TField);
begin
{$ifdef dbgDBGrid}
DebugLn('TComponentDataLink.RecordChanged');
{$endif}
if Assigned(OnRecordChanged) then
OnRecordChanged(Field);
end;
procedure TComponentDataLink.DataSetChanged;
begin
{$ifdef dbgDBGrid}
DebugLn('TComponentDataLink.DataSetChanged, FirstRecord=', dbgs(FirstRecord));
{$Endif}
// todo: improve this routine, for example: OnDatasetInserted
if Assigned(OnDataSetChanged) then
OnDataSetChanged(DataSet);
end;
procedure TComponentDataLink.ActiveChanged;
begin
{$ifdef dbgDBGrid}
DebugLn('TComponentDataLink.ActiveChanged');
{$endif}
if Active then begin
fDataSet := DataSet;
if DataSetName <> fDataSetName then begin
fDataSetName := DataSetName;
if Assigned(fOnNewDataSet) then fOnNewDataSet(DataSet);
end else
if Assigned(fOnDataSetOpen) then fOnDataSetOpen(DataSet);
end else begin
BufferCount := 0;
if (DataSource = nil)then begin
if Assigned(fOnInvalidDataSource) then fOnInvalidDataSource(fDataSet);
fDataSet := nil;
fDataSetName := '[???]';
end else begin
if (DataSet=nil)or(csDestroying in DataSet.ComponentState) then begin
if Assigned(fOnInvalidDataSet) then fOnInvalidDataSet(fDataSet);
fDataSet := nil;
fDataSetName := '[???]';
end else begin
if Assigned(fOnDataSetClose) then fOnDataSetClose(DataSet);
if DataSet <> nil then FDataSetName := DataSetName;
end;
end;
end;
end;
procedure TComponentDataLink.LayoutChanged;
begin
{$ifdef dbgDBGrid}
DebugLn('TComponentDataLink.LayoutChanged');
{$Endif}
if Assigned(OnLayoutChanged) then
OnLayoutChanged(DataSet);
end;
procedure TComponentDataLink.DataSetScrolled(Distance: Integer);
begin
{$ifdef dbgDBGrid}
DebugLn('TComponentDataLink.DataSetScrolled(',IntToStr(Distance),')');
{$endif}
if Assigned(OnDataSetScrolled) then
OnDataSetScrolled(DataSet, Distance);
end;
procedure TComponentDataLink.FocusControl(Field: TFieldRef);
begin
{$ifdef dbgDBGrid}
DebugLn('TComponentDataLink.FocusControl');
{$endif}
end;
procedure TComponentDataLink.CheckBrowseMode;
begin
{$ifdef dbgDBGrid}
DebugLn(ClassName,'.CheckBrowseMode');
{$endif}
inherited CheckBrowseMode;
end;
procedure TComponentDataLink.EditingChanged;
begin
{$ifdef dbgDBGrid}
DebugLn(ClassName,'.EditingChanged');
{$endif}
if Assigned(OnEditingChanged) then
OnEditingChanged(DataSet);
end;
procedure TComponentDataLink.UpdateData;
begin
{$ifdef dbgDBGrid}
DebugLn(ClassName,'.UpdateData');
{$endif}
if Assigned(OnUpdatedata) then
OnUpdateData(DataSet);
end;
function TComponentDataLink.MoveBy(Distance: Integer): Integer;
begin
(*
{$ifdef dbgDBGrid}
DebugLn(ClassName,'.MoveBy INIT: Distance=',Distance);
{$endif}
*)
Result:=inherited MoveBy(Distance);
(*
{$ifdef dbgDBGrid}
DebugLn(ClassName,'.MoveBy END: Distance=',Distance);
{$endif}
*)
end;
{ TDBGridColumns }
function TDBGridColumns.GetColumn(Index: Integer): TColumn;
begin
result := TColumn( inherited Items[Index] );
end;
procedure TDBGridColumns.SetColumn(Index: Integer; Value: TColumn);
begin
Items[Index].Assign( Value );
end;
procedure TDBGridColumns.Update(Item: TCollectionItem);
begin
if (Grid<>nil) and not (csLoading in Grid.ComponentState) then
TCustomDBGrid(Grid).LayoutChanged;
end;
function TDBGridColumns.ColumnFromField(Field: TField): TColumn;
var
i: Integer;
begin
if Field<>nil then
for i:=0 to Count-1 do begin
result := Items[i];
if (result<>nil)and(result.Field=Field) then
exit;
end;
result:=nil;
end;
function TDBGridColumns.HasAutomaticColumns: boolean;
var
i: Integer;
begin
Result := False;
for i:=0 to Count-1 do
if Items[i].IsAutomaticColumn then begin
Result := true;
break;
end;
end;
function TDBGridColumns.HasDesignColumns: boolean;
var
i: Integer;
begin
Result := False;
for i:=0 to Count-1 do
if Items[i].IsDesignColumn then begin
Result := true;
break;
end;
end;
procedure TDBGridColumns.RemoveAutoColumns;
var
i: Integer;
G: TCustomDBGrid;
begin
if HasAutomaticColumns then begin
G := TCustomDBGrid(Grid);
G.GridStatus := G.GridStatus + [gsRemovingAutoColumns];
BeginUpdate;
try
for i:=Count-1 downto 0 do
if Items[i].IsAutomaticColumn then
Delete(i);
finally
EndUpdate;
G.GridStatus := G.GridStatus - [gsRemovingAutoColumns];
end;
end;
end;
function CompareFieldIndex(P1,P2:Pointer): integer;
begin
if P1=P2 then
Result := 0
else if (P1=nil) or (TColumn(P1).Field=nil) then
Result := 1
else if (P2=nil) or (TColumn(P2).Field=nil) then
Result := -1
else
Result := TColumn(P1).Field.Index - TColumn(P2).Field.Index;
end;
function CompareDesignIndex(P1,P2:Pointer): integer;
begin
result := TColumn(P1).DesignIndex - TColumn(P2).DesignIndex;
end;
procedure TDBGridColumns.ResetColumnsOrder(ColumnOrder: TColumnOrder);
var
L: TList;
i: Integer;
begin
L := TList.Create;
try
for i:=0 to Count-1 do
L.Add(Items[i]);
case ColumnOrder of
coDesignOrder:
begin
if not HasDesignColumns then
exit;
L.Sort(@CompareDesignIndex)
end;
coFieldIndexOrder:
L.Sort(@CompareFieldIndex);
else
exit;
end;
for i:=0 to L.Count-1 do
TColumn(L.Items[i]).Index := i;
finally
L.Free;
end;
end;
function TDBGridColumns.Add: TColumn;
var
G: TCustomDBGrid;
begin
G := TCustomDBGrid(Grid);
if G<>nil then begin
// remove automatic columns before adding user columns
if not (gsAddingAutoColumns in G.GridStatus) then
RemoveAutoColumns;
end;
result := TColumn( inherited add );
end;
procedure TDBGridColumns.LinkFields;
var
i: Integer;
G: TCustomDBGrid;
begin
G := TCustomDBGrid(Grid);
if G<>nil then
G.BeginLayout;
for i:=0 to Count-1 do
Items[i].LinkField;
if G<>nil then
G.EndLayout;
end;
{ TColumn }
function TColumn.GetField: TField;
begin
if (FFieldName<>'') and (FField<>nil) then
LinkField;
result := FField;
end;
function TColumn.GetIsDesignColumn: boolean;
begin
result := (DesignIndex>=0) and (DesignIndex<10000);
end;
function TColumn.GetPickList: TStrings;
begin
Result := inherited GetPickList;
if (Field<>nil) and FField.Lookup then
begin
if FField.LookupCache then
FField.LookupList.ValuesToStrings(Result)
else
begin
Result.Clear;
LookupGetBookMark(FField);
try
with FField.LookupDataSet do
begin
First;
while not EOF do
begin
Result.Add(FieldbyName(FField.LookupResultField).AsString);
Next;
end;
end;
finally
LookupGotoBookMark(FField);
end;
end;
end;
end;
procedure TColumn.ApplyDisplayFormat;
begin
if (FField <> nil) and FDisplayFormatChanged then begin
if (FField is TNumericField) then
TNumericField(FField).DisplayFormat := DisplayFormat
else if (FField is TDateTimeField) then
TDateTimeField(FField).DisplayFormat := DisplayFormat;
end;
end;
function TColumn.GetDisplayFormat: string;
begin
if not FDisplayFormatChanged then
Result := GetDefaultDisplayFormat
else
result := FDisplayFormat;
end;
function TColumn.IsDisplayFormatStored: boolean;
begin
Result := FDisplayFormatChanged;
end;
procedure TColumn.SetDisplayFormat(const AValue: string);
begin
if (not FDisplayFormatChanged)or(CompareText(AValue, FDisplayFormat)<>0) then begin
FDisplayFormat := AValue;
FDisplayFormatChanged:=True;
ColumnChanged;
end;
end;
procedure TColumn.SetField(const AValue: TField);
begin
if FField <> AValue then begin
FField := AValue;
if FField<>nil then
FFieldName := FField.FieldName;
ColumnChanged;
end;
end;
procedure TColumn.SetFieldName(const AValue: String);
begin
if FFieldName=AValue then exit;
FFieldName:=AValue;
LinkField;
ColumnChanged;
end;
function TColumn.GetDataSet: TDataSet;
var
AGrid: TCustomDBGrid;
begin
AGrid := TCustomDBGrid(Grid);
if (AGrid<>nil) then
result := AGrid.FDataLink.DataSet
else
result :=nil;
end;
procedure TColumn.Assign(Source: TPersistent);
begin
if Source is TColumn then begin
//DebugLn('Assigning TColumn[',dbgs(Index),'] a TColumn')
Collection.BeginUpdate;
try
inherited Assign(Source);
FieldName := TColumn(Source).FieldName;
DisplayFormat := TColumn(Source).DisplayFormat;
ValueChecked := TColumn(Source).ValueChecked;
ValueUnchecked := TColumn(Source).ValueUnchecked;
finally
Collection.EndUpdate;
end;
end else
inherited Assign(Source);
end;
function TColumn.GetDefaultWidth: Integer;
var
AGrid: TCustomDBGrid;
tmpCanvas: TCanvas;
begin
AGrid := TCustomDBGrid(Grid);
if AGrid<>nil then begin
tmpCanvas := GetWorkingCanvas(aGrid.Canvas);
tmpCanvas.Font := aGrid.Font;
if FField<>nil then
result := CalcColumnFieldWidth(
tmpCanvas,
dgTitles in aGrid.Options,
Title.Caption,
Title.Font,
FField)
else
result := AGrid.DefaultColWidth;
if tmpCanvas<>AGrid.Canvas then
FreeWorkingCanvas(tmpCanvas);
end else
result := DEFCOLWIDTH;
end;
function TColumn.CreateTitle: TGridColumnTitle;
begin
Result := TColumnTitle.Create(Self);
end;
constructor TColumn.Create(ACollection: TCollection);
var
AGrid: TCustomGrid;
begin
inherited Create(ACollection);
if ACollection is TDBGridColumns then begin
AGrid := TDBGridColumns(ACollection).Grid;
if (AGrid<>nil) and (csLoading in AGrid.ComponentState) then
FDesignIndex := Index
else
FDesignIndex := 10000;
end;
end;
function TColumn.IsDefault: boolean;
begin
result := not FDisplayFormatChanged and (inherited IsDefault());
end;
procedure TColumn.LinkField;
var
AGrid: TCustomDBGrid;
begin
AGrid:= TCustomDBGrid(Grid);
if (AGrid<>nil) and AGrid.FDatalink.Active then begin
Field := AGrid.FDataLink.DataSet.FindField(FFieldName);
ApplyDisplayFormat;
end else
Field := nil;
end;
function TColumn.GetDefaultDisplayFormat: string;
begin
Result := '';
if FField<>nil then begin
if FField is TNumericField then
Result := TNumericField(FField).DisplayFormat
else if FField is TDateTimeField then
Result := TDateTimeField(FField).DisplayFormat
end;
end;
function TColumn.GetDefaultValueChecked: string;
begin
if (FField<>nil) and (FField.Datatype=ftBoolean) then
Result := BoolToStr(True)
else
Result := '1';
end;
function TColumn.GetDefaultValueUnchecked: string;
begin
if (FField<>nil) and (FField.DataType=ftBoolean) then
Result := BoolToStr(False)
else
Result := '0';
end;
function TColumn.GetDefaultReadOnly: boolean;
var
AGrid: TCustomDBGrid;
begin
AGrid := TCustomDBGrid(Grid);
Result := ((AGrid<>nil)and(AGrid.ReadOnly)) or ((FField<>nil)And(FField.ReadOnly))
end;
function TColumn.GetDefaultVisible: boolean;
begin
if FField<>nil then
result := FField.Visible
else
result := True;
end;
function TColumn.GetDisplayName: string;
begin
if FFieldName<>'' then
Result:=FFieldName
else
Result:=inherited GetDisplayName;
end;
function TColumn.GetDefaultAlignment: TAlignment;
var
Bs: set of TColumnButtonStyle;
begin
bs := [buttonStyle];
if Grid<>nil then
Include(bs, TCustomDbGrid(Grid).DefaultEditorStyle(ButtonStyle, FField));
if bs*[cbsCheckboxColumn,cbsButtonColumn]<>[] then
result := taCenter
else
if FField<>nil then
result := FField.Alignment
else
Result := taLeftJustify;
end;
{ TColumnTitle }
function TColumnTitle.GetDefaultCaption: string;
begin
with (Column as TColumn) do begin
if FieldName<>'' then begin
if FField<>nil then
Result := Field.DisplayName
else
Result := Fieldname;
end else
Result := inherited GetDefaultCaption;
end;
end;
{ TBookmarkList }
function TBookmarkList.GetCount: integer;
begin
result := FList.Count;
end;
function TBookmarkList.GetCurrentRowSelected: boolean;
begin
CheckActive;
Result := IndexOf(FGrid.Datasource.Dataset.Bookmark)>=0;
end;
function TBookmarkList.GetItem(AIndex: Integer): TBookmarkStr;
begin
Result := FList[AIndex];
end;
procedure TBookmarkList.SetCurrentRowSelected(const AValue: boolean);
var
aBookStr: TBookmarkstr;
aIndex: Integer;
begin
CheckActive;
aBookStr := FGrid.Datasource.Dataset.Bookmark;
if ABookStr='' then
exit;
if Find(ABookStr, aIndex) then begin
if not AValue then begin
FList.Delete(aIndex);
FGrid.Invalidate;
end;
end else begin
if AValue then begin
FList.Add(ABookStr);
FGrid.Invalidate;
end;
end;
end;
procedure TBookmarkList.CheckActive;
begin
if not Fgrid.FDataLink.Active then
raise EInvalidGridOperation.Create('Dataset Inactive');
end;
constructor TBookmarkList.Create(AGrid: TCustomDBGrid);
begin
inherited Create;
FGrid := AGrid;
FList := TBMStringList.Create;
FList.CaseSensitive:=True;
FList.Sorted:=True;
end;
destructor TBookmarkList.Destroy;
begin
Clear;
FList.Free;
inherited Destroy;
end;
procedure TBookmarkList.Clear;
begin
FList.Clear;
FGrid.Invalidate;
end;
procedure TBookmarkList.Delete;
var
i: Integer;
ds: TDataSet;
begin
ds := FGrid.Datasource.Dataset;
for i:=0 to FList.Count-1 do begin
ds.Bookmark := Items[i];
ds.Delete;
FList.delete(i);
end;
end;
function TBookmarkList.Find(const Item: TBookmarkStr; var AIndex: Integer): boolean;
var
Indx: integer;
begin
Indx := FList.IndexOf(Item);
if indx<0 then
Result := False
else begin
Result := True;
AIndex := indx;
end;
end;
function TBookmarkList.IndexOf(const Item: TBookmarkStr): Integer;
begin
result := FList.IndexOf(Item)
end;
function TBookmarkList.Refresh: boolean;
var
ds: TDataset;
i: LongInt;
begin
Result := False;
ds := FGrid.Datasource.Dataset;
for i:=FList.Count-1 downto 0 do
if not ds.BookmarkValid(TBookMark(Items[i])) then begin
Result := True;
Flist.Delete(i);
end;
if Result then
FGrid.Invalidate;
end;
{ TBMStringList }
function TBMStringList.DoCompareText(const s1, s2: string): PtrInt;
begin
if Length(s1)<Length(s2) then
result := -1
else
if Length(s1)>Length(s2) then
result := 1
else
result := CompareMemRange(@s1[1],@s2[1], Length(s1));
end;
end.
|