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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <column.hxx>
#include <docsh.hxx>
#include <scitems.hxx>
#include <formulacell.hxx>
#include <document.hxx>
#include <drwlayer.hxx>
#include <attarray.hxx>
#include <patattr.hxx>
#include <cellform.hxx>
#include <editutil.hxx>
#include <subtotal.hxx>
#include <markdata.hxx>
#include <fillinfo.hxx>
#include <segmenttree.hxx>
#include <docparam.hxx>
#include <cellvalue.hxx>
#include <tokenarray.hxx>
#include <formulagroup.hxx>
#include <listenercontext.hxx>
#include <mtvcellfunc.hxx>
#include <progress.hxx>
#include <scmatrix.hxx>
#include <rowheightcontext.hxx>
#include <tokenstringcontext.hxx>
#include <recursionhelper.hxx>
#include <editeng/eeitem.hxx>
#include <o3tl/safeint.hxx>
#include <svx/algitem.hxx>
#include <editeng/editobj.hxx>
#include <editeng/editstat.hxx>
#include <editeng/emphasismarkitem.hxx>
#include <editeng/fhgtitem.hxx>
#include <svx/rotmodit.hxx>
#include <editeng/unolingu.hxx>
#include <editeng/justifyitem.hxx>
#include <svl/zforlist.hxx>
#include <svl/broadcast.hxx>
#include <vcl/outdev.hxx>
#include <formula/errorcodes.hxx>
#include <formula/vectortoken.hxx>
#include <algorithm>
#include <limits>
#include <memory>
#include <numeric>
#include <math.h>
// factor from font size to optimal cell height (text width)
#define SC_ROT_BREAK_FACTOR 6
static bool IsAmbiguousScript( SvtScriptType nScript )
{
//TODO: move to a header file
return ( nScript != SvtScriptType::LATIN &&
nScript != SvtScriptType::ASIAN &&
nScript != SvtScriptType::COMPLEX );
}
// Data operations
long ScColumn::GetNeededSize(
SCROW nRow, OutputDevice* pDev, double nPPTX, double nPPTY,
const Fraction& rZoomX, const Fraction& rZoomY,
bool bWidth, const ScNeededSizeOptions& rOptions,
const ScPatternAttr** ppPatternChange ) const
{
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(nRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it == maCells.end() || it->type == sc::element_type_empty)
// Empty cell, or invalid row.
return 0;
long nValue = 0;
ScRefCellValue aCell = GetCellValue(it, aPos.second);
double nPPT = bWidth ? nPPTX : nPPTY;
const ScPatternAttr* pPattern = rOptions.pPattern;
if (!pPattern)
pPattern = pAttrArray->GetPattern( nRow );
// merged?
// Do not merge in conditional formatting
const ScMergeAttr* pMerge = &pPattern->GetItem(ATTR_MERGE);
const ScMergeFlagAttr* pFlag = &pPattern->GetItem(ATTR_MERGE_FLAG);
if ( bWidth )
{
if ( pFlag->IsHorOverlapped() )
return 0;
if ( rOptions.bSkipMerged && pMerge->GetColMerge() > 1 )
return 0;
}
else
{
if ( pFlag->IsVerOverlapped() )
return 0;
if ( rOptions.bSkipMerged && pMerge->GetRowMerge() > 1 )
return 0;
}
// conditional formatting
ScDocument* pDocument = GetDoc();
const SfxItemSet* pCondSet = pDocument->GetCondResult( nCol, nRow, nTab );
//The pPattern may change in GetCondResult
if (aCell.meType == CELLTYPE_FORMULA)
{
pPattern = pAttrArray->GetPattern( nRow );
if (ppPatternChange)
*ppPatternChange = pPattern;
}
// line break?
const SfxPoolItem* pCondItem;
SvxCellHorJustify eHorJust;
if (pCondSet &&
pCondSet->GetItemState(ATTR_HOR_JUSTIFY, true, &pCondItem) == SfxItemState::SET)
eHorJust = static_cast<const SvxHorJustifyItem*>(pCondItem)->GetValue();
else
eHorJust = pPattern->GetItem( ATTR_HOR_JUSTIFY ).GetValue();
bool bBreak;
if ( eHorJust == SvxCellHorJustify::Block )
bBreak = true;
else if ( pCondSet &&
pCondSet->GetItemState(ATTR_LINEBREAK, true, &pCondItem) == SfxItemState::SET)
bBreak = static_cast<const ScLineBreakCell*>(pCondItem)->GetValue();
else
bBreak = pPattern->GetItem(ATTR_LINEBREAK).GetValue();
SvNumberFormatter* pFormatter = pDocument->GetFormatTable();
sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
// get "cell is value" flag
// Must be synchronized with ScOutputData::LayoutStrings()
bool bCellIsValue = (aCell.meType == CELLTYPE_VALUE);
if (aCell.meType == CELLTYPE_FORMULA)
{
ScFormulaCell* pFCell = aCell.mpFormula;
bCellIsValue = pFCell->IsRunning() || pFCell->IsValue();
}
// #i111387#, tdf#121040: disable automatic line breaks for all number formats
if (bBreak && bCellIsValue && (pFormatter->GetType(nFormat) == SvNumFormatType::NUMBER))
{
// If a formula cell needs to be interpreted during aCell.hasNumeric()
// to determine the type, the pattern may get invalidated because the
// result may set a number format. In which case there's also the
// General format not set anymore...
bool bMayInvalidatePattern = (aCell.meType == CELLTYPE_FORMULA);
const ScPatternAttr* pOldPattern = pPattern;
bool bNumeric = aCell.hasNumeric();
if (bMayInvalidatePattern)
{
pPattern = pAttrArray->GetPattern( nRow );
if (ppPatternChange)
*ppPatternChange = pPattern; // XXX caller may have to check for change!
}
if (bNumeric)
{
if (!bMayInvalidatePattern || pPattern == pOldPattern)
bBreak = false;
else
{
nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
if (pFormatter->GetType(nFormat) == SvNumFormatType::NUMBER)
bBreak = false;
}
}
}
// get other attributes from pattern and conditional formatting
SvxCellOrientation eOrient = pPattern->GetCellOrientation( pCondSet );
bool bAsianVertical = ( eOrient == SvxCellOrientation::Stacked &&
pPattern->GetItem( ATTR_VERTICAL_ASIAN, pCondSet ).GetValue() );
if ( bAsianVertical )
bBreak = false;
if ( bWidth && bBreak ) // after determining bAsianVertical (bBreak may be reset)
return 0;
long nRotate = 0;
SvxRotateMode eRotMode = SVX_ROTATE_MODE_STANDARD;
if ( eOrient == SvxCellOrientation::Standard )
{
if (pCondSet &&
pCondSet->GetItemState(ATTR_ROTATE_VALUE, true, &pCondItem) == SfxItemState::SET)
nRotate = static_cast<const ScRotateValueItem*>(pCondItem)->GetValue();
else
nRotate = pPattern->GetItem(ATTR_ROTATE_VALUE).GetValue();
if ( nRotate )
{
if (pCondSet &&
pCondSet->GetItemState(ATTR_ROTATE_MODE, true, &pCondItem) == SfxItemState::SET)
eRotMode = static_cast<const SvxRotateModeItem*>(pCondItem)->GetValue();
else
eRotMode = pPattern->GetItem(ATTR_ROTATE_MODE).GetValue();
if ( nRotate == 18000 )
eRotMode = SVX_ROTATE_MODE_STANDARD; // no overflow
}
}
if ( eHorJust == SvxCellHorJustify::Repeat )
{
// ignore orientation/rotation if "repeat" is active
eOrient = SvxCellOrientation::Standard;
nRotate = 0;
bAsianVertical = false;
}
const SvxMarginItem* pMargin;
if (pCondSet &&
pCondSet->GetItemState(ATTR_MARGIN, true, &pCondItem) == SfxItemState::SET)
pMargin = static_cast<const SvxMarginItem*>(pCondItem);
else
pMargin = &pPattern->GetItem(ATTR_MARGIN);
sal_uInt16 nIndent = 0;
if ( eHorJust == SvxCellHorJustify::Left )
{
if (pCondSet &&
pCondSet->GetItemState(ATTR_INDENT, true, &pCondItem) == SfxItemState::SET)
nIndent = static_cast<const ScIndentItem*>(pCondItem)->GetValue();
else
nIndent = pPattern->GetItem(ATTR_INDENT).GetValue();
}
SvtScriptType nScript = pDocument->GetScriptType(nCol, nRow, nTab);
if (nScript == SvtScriptType::NONE) nScript = ScGlobal::GetDefaultScriptType();
// also call SetFont for edit cells, because bGetFont may be set only once
// bGetFont is set also if script type changes
if (rOptions.bGetFont)
{
Fraction aFontZoom = ( eOrient == SvxCellOrientation::Standard ) ? rZoomX : rZoomY;
vcl::Font aFont;
// font color doesn't matter here
pPattern->GetFont( aFont, SC_AUTOCOL_BLACK, pDev, &aFontZoom, pCondSet, nScript );
pDev->SetFont(aFont);
}
bool bAddMargin = true;
CellType eCellType = aCell.meType;
bool bEditEngine = (eCellType == CELLTYPE_EDIT ||
eOrient == SvxCellOrientation::Stacked ||
IsAmbiguousScript(nScript) ||
((eCellType == CELLTYPE_FORMULA) && aCell.mpFormula->IsMultilineResult()));
if (!bEditEngine) // direct output
{
Color* pColor;
OUString aValStr;
ScCellFormat::GetString(
aCell, nFormat, aValStr, &pColor, *pFormatter, pDocument, true, rOptions.bFormula);
if (!aValStr.isEmpty())
{
// SetFont is moved up
Size aSize( pDev->GetTextWidth( aValStr ), pDev->GetTextHeight() );
if ( eOrient != SvxCellOrientation::Standard )
{
long nTemp = aSize.Width();
aSize.setWidth( aSize.Height() );
aSize.setHeight( nTemp );
}
else if ( nRotate )
{
//TODO: take different X/Y scaling into consideration
double nRealOrient = nRotate * F_PI18000; // nRotate is in 1/100 Grad
double nCosAbs = fabs( cos( nRealOrient ) );
double nSinAbs = fabs( sin( nRealOrient ) );
long nHeight = static_cast<long>( aSize.Height() * nCosAbs + aSize.Width() * nSinAbs );
long nWidth;
if ( eRotMode == SVX_ROTATE_MODE_STANDARD )
nWidth = static_cast<long>( aSize.Width() * nCosAbs + aSize.Height() * nSinAbs );
else if ( rOptions.bTotalSize )
{
nWidth = static_cast<long>( pDocument->GetColWidth( nCol,nTab ) * nPPT );
bAddMargin = false;
// only to the right:
//TODO: differ on direction up/down (only Text/whole height)
if ( pPattern->GetRotateDir( pCondSet ) == ScRotateDir::Right )
nWidth += static_cast<long>( pDocument->GetRowHeight( nRow,nTab ) *
nPPT * nCosAbs / nSinAbs );
}
else
nWidth = static_cast<long>( aSize.Height() / nSinAbs ); //TODO: limit?
if ( bBreak && !rOptions.bTotalSize )
{
// limit size for line break
long nCmp = pDev->GetFont().GetFontSize().Height() * SC_ROT_BREAK_FACTOR;
if ( nHeight > nCmp )
nHeight = nCmp;
}
aSize = Size( nWidth, nHeight );
}
nValue = bWidth ? aSize.Width() : aSize.Height();
if ( bAddMargin )
{
if (bWidth)
{
nValue += static_cast<long>( pMargin->GetLeftMargin() * nPPT ) +
static_cast<long>( pMargin->GetRightMargin() * nPPT );
if ( nIndent )
nValue += static_cast<long>( nIndent * nPPT );
}
else
nValue += static_cast<long>( pMargin->GetTopMargin() * nPPT ) +
static_cast<long>( pMargin->GetBottomMargin() * nPPT );
}
// linebreak done ?
if ( bBreak && !bWidth )
{
// test with EditEngine the safety at 90%
// (due to rounding errors and because EditEngine formats partially differently)
long nDocPixel = static_cast<long>( ( pDocument->GetColWidth( nCol,nTab ) -
pMargin->GetLeftMargin() - pMargin->GetRightMargin() -
nIndent )
* nPPTX );
nDocPixel = (nDocPixel * 9) / 10; // for safety
if ( aSize.Width() > nDocPixel )
bEditEngine = true;
}
}
}
if (bEditEngine)
{
// the font is not reset each time with !bEditEngine
vcl::Font aOldFont = pDev->GetFont();
MapMode aHMMMode( MapUnit::Map100thMM, Point(), rZoomX, rZoomY );
// save in document ?
std::unique_ptr<ScFieldEditEngine> pEngine = pDocument->CreateFieldEditEngine();
pEngine->SetUpdateMode( false );
bool bTextWysiwyg = ( pDev->GetOutDevType() == OUTDEV_PRINTER );
EEControlBits nCtrl = pEngine->GetControlWord();
if ( bTextWysiwyg )
nCtrl |= EEControlBits::FORMAT100;
else
nCtrl &= ~EEControlBits::FORMAT100;
pEngine->SetControlWord( nCtrl );
MapMode aOld = pDev->GetMapMode();
pDev->SetMapMode( aHMMMode );
pEngine->SetRefDevice( pDev );
pDocument->ApplyAsianEditSettings( *pEngine );
std::unique_ptr<SfxItemSet> pSet(new SfxItemSet( pEngine->GetEmptyItemSet() ));
if ( ScStyleSheet* pPreviewStyle = pDocument->GetPreviewCellStyle( nCol, nRow, nTab ) )
{
std::unique_ptr<ScPatternAttr> pPreviewPattern(new ScPatternAttr( *pPattern ));
pPreviewPattern->SetStyleSheet(pPreviewStyle);
pPreviewPattern->FillEditItemSet( pSet.get(), pCondSet );
}
else
{
SfxItemSet* pFontSet = pDocument->GetPreviewFont( nCol, nRow, nTab );
pPattern->FillEditItemSet( pSet.get(), pFontSet ? pFontSet : pCondSet );
}
// no longer needed, are set with the text (is faster)
// pEngine->SetDefaults( pSet );
if ( pSet->Get(EE_PARA_HYPHENATE).GetValue() ) {
css::uno::Reference<css::linguistic2::XHyphenator> xXHyphenator( LinguMgr::GetHyphenator() );
pEngine->SetHyphenator( xXHyphenator );
}
Size aPaper( 1000000, 1000000 );
if ( eOrient==SvxCellOrientation::Stacked && !bAsianVertical )
aPaper.setWidth( 1 );
else if (bBreak)
{
double fWidthFactor = nPPTX;
if ( bTextWysiwyg )
{
// if text is formatted for printer, don't use PixelToLogic,
// to ensure the exact same paper width (and same line breaks) as in
// ScEditUtil::GetEditArea, used for output.
fWidthFactor = HMM_PER_TWIPS;
}
// use original width for hidden columns:
long nDocWidth = static_cast<long>( pDocument->GetOriginalWidth(nCol,nTab) * fWidthFactor );
SCCOL nColMerge = pMerge->GetColMerge();
if (nColMerge > 1)
for (SCCOL nColAdd=1; nColAdd<nColMerge; nColAdd++)
nDocWidth += static_cast<long>( pDocument->GetColWidth(nCol+nColAdd,nTab) * fWidthFactor );
nDocWidth -= static_cast<long>( pMargin->GetLeftMargin() * fWidthFactor )
+ static_cast<long>( pMargin->GetRightMargin() * fWidthFactor )
+ 1; // output size is width-1 pixel (due to gridline)
if ( nIndent )
nDocWidth -= static_cast<long>( nIndent * fWidthFactor );
// space for AutoFilter button: 20 * nZoom/100
if ( pFlag->HasAutoFilter() && !bTextWysiwyg )
nDocWidth -= long(rZoomX*20);
aPaper.setWidth( nDocWidth );
if ( !bTextWysiwyg )
aPaper = pDev->PixelToLogic( aPaper, aHMMMode );
}
pEngine->SetPaperSize(aPaper);
if (aCell.meType == CELLTYPE_EDIT)
{
pEngine->SetTextNewDefaults(*aCell.mpEditText, std::move(pSet));
}
else
{
Color* pColor;
OUString aString;
ScCellFormat::GetString(
aCell, nFormat, aString, &pColor, *pFormatter, pDocument, true,
rOptions.bFormula);
if (!aString.isEmpty())
pEngine->SetTextNewDefaults(aString, std::move(pSet));
else
pEngine->SetDefaults(std::move(pSet));
}
bool bEngineVertical = pEngine->IsVertical();
pEngine->SetVertical( bAsianVertical );
pEngine->SetUpdateMode( true );
bool bEdWidth = bWidth;
if ( eOrient != SvxCellOrientation::Standard && eOrient != SvxCellOrientation::Stacked )
bEdWidth = !bEdWidth;
if ( nRotate )
{
//TODO: take different X/Y scaling into consideration
Size aSize( pEngine->CalcTextWidth(), pEngine->GetTextHeight() );
double nRealOrient = nRotate * F_PI18000; // nRotate is in 1/100 Grad
double nCosAbs = fabs( cos( nRealOrient ) );
double nSinAbs = fabs( sin( nRealOrient ) );
long nHeight = static_cast<long>( aSize.Height() * nCosAbs + aSize.Width() * nSinAbs );
long nWidth;
if ( eRotMode == SVX_ROTATE_MODE_STANDARD )
nWidth = static_cast<long>( aSize.Width() * nCosAbs + aSize.Height() * nSinAbs );
else if ( rOptions.bTotalSize )
{
nWidth = static_cast<long>( pDocument->GetColWidth( nCol,nTab ) * nPPT );
bAddMargin = false;
if ( pPattern->GetRotateDir( pCondSet ) == ScRotateDir::Right )
nWidth += static_cast<long>( pDocument->GetRowHeight( nRow,nTab ) *
nPPT * nCosAbs / nSinAbs );
}
else
nWidth = static_cast<long>( aSize.Height() / nSinAbs ); //TODO: limit?
aSize = Size( nWidth, nHeight );
Size aPixSize = pDev->LogicToPixel( aSize, aHMMMode );
if ( bEdWidth )
nValue = aPixSize.Width();
else
{
nValue = aPixSize.Height();
if ( bBreak && !rOptions.bTotalSize )
{
// limit size for line break
long nCmp = aOldFont.GetFontSize().Height() * SC_ROT_BREAK_FACTOR;
if ( nValue > nCmp )
nValue = nCmp;
}
}
}
else if ( bEdWidth )
{
if (bBreak)
nValue = 0;
else
nValue = pDev->LogicToPixel(Size( pEngine->CalcTextWidth(), 0 ),
aHMMMode).Width();
}
else // height
{
nValue = pDev->LogicToPixel(Size( 0, pEngine->GetTextHeight() ),
aHMMMode).Height();
// With non-100% zoom and several lines or paragraphs, don't shrink below the result with FORMAT100 set
if ( !bTextWysiwyg && ( rZoomY.GetNumerator() != 1 || rZoomY.GetDenominator() != 1 ) &&
( pEngine->GetParagraphCount() > 1 || ( bBreak && pEngine->GetLineCount(0) > 1 ) ) )
{
pEngine->SetControlWord( nCtrl | EEControlBits::FORMAT100 );
pEngine->QuickFormatDoc( true );
long nSecondValue = pDev->LogicToPixel(Size( 0, pEngine->GetTextHeight() ), aHMMMode).Height();
if ( nSecondValue > nValue )
nValue = nSecondValue;
}
}
if ( nValue && bAddMargin )
{
if (bWidth)
{
nValue += static_cast<long>( pMargin->GetLeftMargin() * nPPT ) +
static_cast<long>( pMargin->GetRightMargin() * nPPT );
if (nIndent)
nValue += static_cast<long>( nIndent * nPPT );
}
else
{
nValue += static_cast<long>( pMargin->GetTopMargin() * nPPT ) +
static_cast<long>( pMargin->GetBottomMargin() * nPPT );
if ( bAsianVertical && pDev->GetOutDevType() != OUTDEV_PRINTER )
{
// add 1pt extra (default margin value) for line breaks with SetVertical
nValue += static_cast<long>( 20 * nPPT );
}
}
}
// EditEngine is cached and re-used, so the old vertical flag must be restored
pEngine->SetVertical( bEngineVertical );
pDocument->DisposeFieldEditEngine(pEngine);
pDev->SetMapMode( aOld );
pDev->SetFont( aOldFont );
}
if (bWidth)
{
// place for Autofilter Button
// 20 * nZoom/100
// Conditional formatting is not interesting here
ScMF nFlags = pPattern->GetItem(ATTR_MERGE_FLAG).GetValue();
if (nFlags & ScMF::Auto)
nValue += long(rZoomX*20);
}
return nValue;
}
namespace {
class MaxStrLenFinder
{
ScDocument& mrDoc;
sal_uInt32 mnFormat;
OUString maMaxLenStr;
sal_Int32 mnMaxLen;
void checkLength(const ScRefCellValue& rCell)
{
Color* pColor;
OUString aValStr;
ScCellFormat::GetString(
rCell, mnFormat, aValStr, &pColor, *mrDoc.GetFormatTable(), &mrDoc);
if (aValStr.getLength() > mnMaxLen)
{
mnMaxLen = aValStr.getLength();
maMaxLenStr = aValStr;
}
}
public:
MaxStrLenFinder(ScDocument& rDoc, sal_uInt32 nFormat) :
mrDoc(rDoc), mnFormat(nFormat), mnMaxLen(0) {}
void operator() (size_t /*nRow*/, double f)
{
ScRefCellValue aCell(f);
checkLength(aCell);
}
void operator() (size_t /*nRow*/, const svl::SharedString& rSS)
{
if (rSS.getLength() > mnMaxLen)
{
mnMaxLen = rSS.getLength();
maMaxLenStr = rSS.getString();
}
}
void operator() (size_t /*nRow*/, const EditTextObject* p)
{
ScRefCellValue aCell(p);
checkLength(aCell);
}
void operator() (size_t /*nRow*/, const ScFormulaCell* p)
{
ScRefCellValue aCell(const_cast<ScFormulaCell*>(p));
checkLength(aCell);
}
const OUString& getMaxLenStr() const { return maMaxLenStr; }
};
}
sal_uInt16 ScColumn::GetOptimalColWidth(
OutputDevice* pDev, double nPPTX, double nPPTY, const Fraction& rZoomX, const Fraction& rZoomY,
bool bFormula, sal_uInt16 nOldWidth, const ScMarkData* pMarkData, const ScColWidthParam* pParam) const
{
if (maCells.block_size() == 1 && maCells.begin()->type == sc::element_type_empty)
// All cells are empty.
return nOldWidth;
sc::SingleColumnSpanSet aSpanSet;
sc::SingleColumnSpanSet::SpansType aMarkedSpans;
if (pMarkData && (pMarkData->IsMarked() || pMarkData->IsMultiMarked()))
{
aSpanSet.scan(*pMarkData, nTab, nCol);
aSpanSet.getSpans(aMarkedSpans);
}
else
// "Select" the entire column if no selection exists.
aMarkedSpans.emplace_back(0, GetDoc()->MaxRow());
sal_uInt16 nWidth = static_cast<sal_uInt16>(nOldWidth*nPPTX);
bool bFound = false;
ScDocument* pDocument = GetDoc();
if ( pParam && pParam->mbSimpleText )
{ // all the same except for number format
const ScPatternAttr* pPattern = GetPattern( 0 );
vcl::Font aFont;
// font color doesn't matter here
pPattern->GetFont( aFont, SC_AUTOCOL_BLACK, pDev, &rZoomX );
pDev->SetFont( aFont );
const SvxMarginItem* pMargin = &pPattern->GetItem(ATTR_MARGIN);
long nMargin = static_cast<long>( pMargin->GetLeftMargin() * nPPTX ) +
static_cast<long>( pMargin->GetRightMargin() * nPPTX );
// Try to find the row that has the longest string, and measure the width of that string.
SvNumberFormatter* pFormatter = pDocument->GetFormatTable();
sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter );
OUString aLongStr;
Color* pColor;
if (pParam->mnMaxTextRow >= 0)
{
ScRefCellValue aCell = GetCellValue(pParam->mnMaxTextRow);
ScCellFormat::GetString(
aCell, nFormat, aLongStr, &pColor, *pFormatter, pDocument);
}
else
{
// Go though all non-empty cells within selection.
MaxStrLenFinder aFunc(*pDocument, nFormat);
sc::CellStoreType::const_iterator itPos = maCells.begin();
for (const auto& rMarkedSpan : aMarkedSpans)
itPos = sc::ParseAllNonEmpty(itPos, maCells, rMarkedSpan.mnRow1, rMarkedSpan.mnRow2, aFunc);
aLongStr = aFunc.getMaxLenStr();
}
if (!aLongStr.isEmpty())
{
nWidth = pDev->GetTextWidth(aLongStr) + static_cast<sal_uInt16>(nMargin);
bFound = true;
}
}
else
{
ScNeededSizeOptions aOptions;
aOptions.bFormula = bFormula;
const ScPatternAttr* pOldPattern = nullptr;
// Go though all non-empty cells within selection.
sc::CellStoreType::const_iterator itPos = maCells.begin();
for (const auto& rMarkedSpan : aMarkedSpans)
{
SCROW nRow1 = rMarkedSpan.mnRow1, nRow2 = rMarkedSpan.mnRow2;
SCROW nRow = nRow1;
while (nRow <= nRow2)
{
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(itPos, nRow);
itPos = aPos.first;
if (itPos->type == sc::element_type_empty)
{
// Skip empty cells.
nRow += itPos->size - aPos.second;
continue;
}
for (size_t nOffset = aPos.second; nOffset < itPos->size; ++nOffset, ++nRow)
{
SvtScriptType nScript = pDocument->GetScriptType(nCol, nRow, nTab);
if (nScript == SvtScriptType::NONE)
nScript = ScGlobal::GetDefaultScriptType();
const ScPatternAttr* pPattern = GetPattern(nRow);
aOptions.pPattern = pPattern;
aOptions.bGetFont = (pPattern != pOldPattern || nScript != SvtScriptType::NONE);
pOldPattern = pPattern;
sal_uInt16 nThis = static_cast<sal_uInt16>(GetNeededSize(
nRow, pDev, nPPTX, nPPTY, rZoomX, rZoomY, true, aOptions, &pOldPattern));
if (nThis && (nThis > nWidth || !bFound))
{
nWidth = nThis;
bFound = true;
}
}
}
}
}
if (bFound)
{
nWidth += 2;
sal_uInt16 nTwips = static_cast<sal_uInt16>(
std::min(nWidth / nPPTX, double(std::numeric_limits<sal_uInt16>::max())));
return nTwips;
}
else
return nOldWidth;
}
static sal_uInt16 lcl_GetAttribHeight( const ScPatternAttr& rPattern, sal_uInt16 nFontHeightId )
{
const SvxFontHeightItem& rFontHeight =
static_cast<const SvxFontHeightItem&>(rPattern.GetItem(nFontHeightId));
sal_uInt16 nHeight = rFontHeight.GetHeight();
nHeight *= 1.18;
if ( rPattern.GetItem(ATTR_FONT_EMPHASISMARK).GetEmphasisMark() != FontEmphasisMark::NONE )
{
// add height for emphasis marks
//TODO: font metrics should be used instead
nHeight += nHeight / 4;
}
const SvxMarginItem& rMargin = rPattern.GetItem(ATTR_MARGIN);
nHeight += rMargin.GetTopMargin() + rMargin.GetBottomMargin();
if (nHeight > STD_ROWHEIGHT_DIFF)
nHeight -= STD_ROWHEIGHT_DIFF;
if (nHeight < ScGlobal::nStdRowHeight)
nHeight = ScGlobal::nStdRowHeight;
return nHeight;
}
// pHeight in Twips
// optimize nMinHeight, nMinStart : with nRow >= nMinStart is at least nMinHeight
// (is only evaluated with bStdAllowed)
void ScColumn::GetOptimalHeight(
sc::RowHeightContext& rCxt, SCROW nStartRow, SCROW nEndRow, sal_uInt16 nMinHeight, SCROW nMinStart )
{
ScDocument* pDocument = GetDoc();
RowHeightsArray& rHeights = rCxt.getHeightArray();
ScAttrIterator aIter( pAttrArray.get(), nStartRow, nEndRow, pDocument->GetDefPattern() );
SCROW nStart = -1;
SCROW nEnd = -1;
SCROW nEditPos = 0;
SCROW nNextEnd = 0;
// with conditional formatting, always consider the individual cells
const ScPatternAttr* pPattern = aIter.Next(nStart,nEnd);
while ( pPattern )
{
const ScMergeAttr* pMerge = &pPattern->GetItem(ATTR_MERGE);
const ScMergeFlagAttr* pFlag = &pPattern->GetItem(ATTR_MERGE_FLAG);
if ( pMerge->GetRowMerge() > 1 || pFlag->IsOverlapped() )
{
// do nothing - vertically with merged and overlapping,
// horizontally only with overlapped (invisible) -
// only one horizontal merged is always considered
}
else
{
bool bStdAllowed = (pPattern->GetCellOrientation() == SvxCellOrientation::Standard);
bool bStdOnly = false;
if (bStdAllowed)
{
bool bBreak = pPattern->GetItem(ATTR_LINEBREAK).GetValue() ||
(pPattern->GetItem( ATTR_HOR_JUSTIFY ).GetValue() ==
SvxCellHorJustify::Block);
bStdOnly = !bBreak;
// conditional formatting: loop all cells
if (bStdOnly &&
!pPattern->GetItem(ATTR_CONDITIONAL).GetCondFormatData().empty())
{
bStdOnly = false;
}
// rotated text: loop all cells
if ( bStdOnly && pPattern->GetItem(ATTR_ROTATE_VALUE).GetValue() )
bStdOnly = false;
}
if (bStdOnly)
{
bool bHasEditCells = HasEditCells(nStart,nEnd,nEditPos);
// Call to HasEditCells() may change pattern due to
// calculation, => sync always.
// We don't know which row changed first, but as pPattern
// covered nStart to nEnd we can pick nStart. Worst case we
// have to repeat that for every row in range if every row
// changed.
pPattern = aIter.Resync( nStart, nStart, nEnd);
if (bHasEditCells && nEnd < nEditPos)
bHasEditCells = false; // run into that again
if (bHasEditCells) // includes mixed script types
{
if (nEditPos == nStart)
{
bStdOnly = false;
if (nEnd > nEditPos)
nNextEnd = nEnd;
nEnd = nEditPos; // calculate single
bStdAllowed = false; // will be computed in any case per cell
}
else
{
nNextEnd = nEnd;
nEnd = nEditPos - 1; // standard - part
}
}
}
sc::SingleColumnSpanSet aSpanSet;
aSpanSet.scan(*this, nStart, nEnd);
sc::SingleColumnSpanSet::SpansType aSpans;
aSpanSet.getSpans(aSpans);
if (bStdAllowed)
{
sal_uInt16 nLatHeight = 0;
sal_uInt16 nCjkHeight = 0;
sal_uInt16 nCtlHeight = 0;
sal_uInt16 nDefHeight;
SvtScriptType nDefScript = ScGlobal::GetDefaultScriptType();
if ( nDefScript == SvtScriptType::ASIAN )
nDefHeight = nCjkHeight = lcl_GetAttribHeight( *pPattern, ATTR_CJK_FONT_HEIGHT );
else if ( nDefScript == SvtScriptType::COMPLEX )
nDefHeight = nCtlHeight = lcl_GetAttribHeight( *pPattern, ATTR_CTL_FONT_HEIGHT );
else
nDefHeight = nLatHeight = lcl_GetAttribHeight( *pPattern, ATTR_FONT_HEIGHT );
// if everything below is already larger, the loop doesn't have to
// be run again
SCROW nStdEnd = nEnd;
if ( nDefHeight <= nMinHeight && nStdEnd >= nMinStart )
nStdEnd = (nMinStart>0) ? nMinStart-1 : 0;
if (nStart <= nStdEnd)
{
SCROW nRow = nStart;
for (;;)
{
size_t nIndex;
SCROW nRangeEnd;
sal_uInt16 nRangeHeight = rHeights.GetValue(nRow, nIndex, nRangeEnd);
if (nRangeHeight < nDefHeight)
rHeights.SetValue(nRow, std::min(nRangeEnd, nStdEnd), nDefHeight);
nRow = nRangeEnd + 1;
if (nRow > nStdEnd)
break;
}
}
if ( bStdOnly )
{
// if cells are not handled individually below,
// check for cells with different script type
sc::CellTextAttrStoreType::iterator itAttr = maCellTextAttrs.begin();
sc::CellStoreType::iterator itCells = maCells.begin();
for (const auto& rSpan : aSpans)
{
for (SCROW nRow = rSpan.mnRow1; nRow <= rSpan.mnRow2; ++nRow)
{
SvtScriptType nScript = GetRangeScriptType(itAttr, nRow, nRow, itCells);
if (nScript == nDefScript)
continue;
if ( nScript == SvtScriptType::ASIAN )
{
if ( nCjkHeight == 0 )
nCjkHeight = lcl_GetAttribHeight( *pPattern, ATTR_CJK_FONT_HEIGHT );
if (nCjkHeight > rHeights.GetValue(nRow))
rHeights.SetValue(nRow, nRow, nCjkHeight);
}
else if ( nScript == SvtScriptType::COMPLEX )
{
if ( nCtlHeight == 0 )
nCtlHeight = lcl_GetAttribHeight( *pPattern, ATTR_CTL_FONT_HEIGHT );
if (nCtlHeight > rHeights.GetValue(nRow))
rHeights.SetValue(nRow, nRow, nCtlHeight);
}
else
{
if ( nLatHeight == 0 )
nLatHeight = lcl_GetAttribHeight( *pPattern, ATTR_FONT_HEIGHT );
if (nLatHeight > rHeights.GetValue(nRow))
rHeights.SetValue(nRow, nRow, nLatHeight);
}
}
}
}
}
if (!bStdOnly) // search covered cells
{
ScNeededSizeOptions aOptions;
for (const auto& rSpan : aSpans)
{
for (SCROW nRow = rSpan.mnRow1; nRow <= rSpan.mnRow2; ++nRow)
{
// only calculate the cell height when it's used later (#37928#)
if (rCxt.isForceAutoSize() || !(pDocument->GetRowFlags(nRow, nTab) & CRFlags::ManualSize) )
{
aOptions.pPattern = pPattern;
const ScPatternAttr* pOldPattern = pPattern;
sal_uInt16 nHeight = static_cast<sal_uInt16>(
std::min(
GetNeededSize( nRow, rCxt.getOutputDevice(), rCxt.getPPTX(), rCxt.getPPTY(),
rCxt.getZoomX(), rCxt.getZoomY(), false, aOptions,
&pPattern) / rCxt.getPPTY(),
double(std::numeric_limits<sal_uInt16>::max())));
if (nHeight > rHeights.GetValue(nRow))
rHeights.SetValue(nRow, nRow, nHeight);
// Pattern changed due to calculation? => sync.
if (pPattern != pOldPattern)
{
pPattern = aIter.Resync( nRow, nStart, nEnd);
nNextEnd = 0;
}
}
}
}
}
}
if (nNextEnd > 0)
{
nStart = nEnd + 1;
nEnd = nNextEnd;
nNextEnd = 0;
}
else
pPattern = aIter.Next(nStart,nEnd);
}
}
bool ScColumn::GetNextSpellingCell(SCROW& nRow, bool bInSel, const ScMarkData& rData) const
{
ScDocument* pDocument = GetDoc();
bool bStop = false;
sc::CellStoreType::const_iterator it = maCells.position(nRow).first;
mdds::mtv::element_t eType = it->type;
if (!bInSel && it != maCells.end() && eType != sc::element_type_empty)
{
if ( (eType == sc::element_type_string || eType == sc::element_type_edittext) &&
!(HasAttrib( nRow, nRow, HasAttrFlags::Protected) &&
pDocument->IsTabProtected(nTab)) )
return true;
}
while (!bStop)
{
if (bInSel)
{
nRow = rData.GetNextMarked(nCol, nRow, false);
if (!pDocument->ValidRow(nRow))
{
nRow = GetDoc()->MaxRow()+1;
bStop = true;
}
else
{
it = maCells.position(it, nRow).first;
eType = it->type;
if ( (eType == sc::element_type_string || eType == sc::element_type_edittext) &&
!(HasAttrib( nRow, nRow, HasAttrFlags::Protected) &&
pDocument->IsTabProtected(nTab)) )
return true;
else
nRow++;
}
}
else if (GetNextDataPos(nRow))
{
it = maCells.position(it, nRow).first;
eType = it->type;
if ( (eType == sc::element_type_string || eType == sc::element_type_edittext) &&
!(HasAttrib( nRow, nRow, HasAttrFlags::Protected) &&
pDocument->IsTabProtected(nTab)) )
return true;
else
nRow++;
}
else
{
nRow = GetDoc()->MaxRow()+1;
bStop = true;
}
}
return false;
}
namespace {
class StrEntries
{
sc::CellStoreType& mrCells;
protected:
struct StrEntry
{
SCROW mnRow;
OUString maStr;
StrEntry(SCROW nRow, const OUString& rStr) : mnRow(nRow), maStr(rStr) {}
};
std::vector<StrEntry> maStrEntries;
ScDocument* mpDoc;
StrEntries(sc::CellStoreType& rCells, ScDocument* pDoc) : mrCells(rCells), mpDoc(pDoc) {}
public:
void commitStrings()
{
svl::SharedStringPool& rPool = mpDoc->GetSharedStringPool();
sc::CellStoreType::iterator it = mrCells.begin();
for (const auto& rStrEntry : maStrEntries)
it = mrCells.set(it, rStrEntry.mnRow, rPool.intern(rStrEntry.maStr));
}
};
class RemoveEditAttribsHandler : public StrEntries
{
std::unique_ptr<ScFieldEditEngine> mpEngine;
public:
RemoveEditAttribsHandler(sc::CellStoreType& rCells, ScDocument* pDoc) : StrEntries(rCells, pDoc) {}
void operator() (size_t nRow, EditTextObject*& pObj)
{
// For the test on hard formatting (ScEditAttrTester), are the defaults in the
// EditEngine of no importance. When the tester would later recognise the same
// attributes in default and hard formatting and has to remove them, the correct
// defaults must be set in the EditEngine for each cell.
// test for attributes
if (!mpEngine)
{
mpEngine.reset(new ScFieldEditEngine(mpDoc, mpDoc->GetEditPool()));
// EEControlBits::ONLINESPELLING if there are errors already
mpEngine->SetControlWord(mpEngine->GetControlWord() | EEControlBits::ONLINESPELLING);
mpDoc->ApplyAsianEditSettings(*mpEngine);
}
mpEngine->SetTextCurrentDefaults(*pObj);
sal_Int32 nParCount = mpEngine->GetParagraphCount();
for (sal_Int32 nPar=0; nPar<nParCount; nPar++)
{
mpEngine->RemoveCharAttribs(nPar);
const SfxItemSet& rOld = mpEngine->GetParaAttribs(nPar);
if ( rOld.Count() )
{
SfxItemSet aNew( *rOld.GetPool(), rOld.GetRanges() ); // empty
mpEngine->SetParaAttribs( nPar, aNew );
}
}
// change URL field to text (not possible otherwise, thus pType=0)
mpEngine->RemoveFields();
bool bSpellErrors = mpEngine->HasOnlineSpellErrors();
bool bNeedObject = bSpellErrors || nParCount>1; // keep errors/paragraphs
// ScEditAttrTester is not needed anymore, arrays are gone
if (bNeedObject) // remains edit cell
{
EEControlBits nCtrl = mpEngine->GetControlWord();
EEControlBits nWantBig = bSpellErrors ? EEControlBits::ALLOWBIGOBJS : EEControlBits::NONE;
if ( ( nCtrl & EEControlBits::ALLOWBIGOBJS ) != nWantBig )
mpEngine->SetControlWord( (nCtrl & ~EEControlBits::ALLOWBIGOBJS) | nWantBig );
// Overwrite the existing object.
delete pObj;
pObj = mpEngine->CreateTextObject().release();
}
else // create String
{
// Store the string replacement for later commits.
OUString aText = ScEditUtil::GetSpaceDelimitedString(*mpEngine);
maStrEntries.emplace_back(nRow, aText);
}
}
};
class TestTabRefAbsHandler
{
SCTAB mnTab;
bool mbTestResult;
public:
explicit TestTabRefAbsHandler(SCTAB nTab) : mnTab(nTab), mbTestResult(false) {}
void operator() (size_t /*nRow*/, const ScFormulaCell* pCell)
{
if (const_cast<ScFormulaCell*>(pCell)->TestTabRefAbs(mnTab))
mbTestResult = true;
}
bool getTestResult() const { return mbTestResult; }
};
}
void ScColumn::RemoveEditAttribs( SCROW nStartRow, SCROW nEndRow )
{
RemoveEditAttribsHandler aFunc(maCells, GetDoc());
sc::ProcessEditText(maCells.begin(), maCells, nStartRow, nEndRow, aFunc);
aFunc.commitStrings();
}
bool ScColumn::TestTabRefAbs(SCTAB nTable) const
{
TestTabRefAbsHandler aFunc(nTable);
sc::ParseFormula(maCells, aFunc);
return aFunc.getTestResult();
}
bool ScColumn::IsEmptyData() const
{
return maCells.block_size() == 1 && maCells.begin()->type == sc::element_type_empty;
}
namespace {
class CellCounter
{
size_t mnCount;
public:
CellCounter() : mnCount(0) {}
void operator() (
const sc::CellStoreType::value_type& node, size_t /*nOffset*/, size_t nDataSize)
{
if (node.type == sc::element_type_empty)
return;
mnCount += nDataSize;
}
size_t getCount() const { return mnCount; }
};
}
SCSIZE ScColumn::VisibleCount( SCROW nStartRow, SCROW nEndRow ) const
{
CellCounter aFunc;
sc::ParseBlock(maCells.begin(), maCells, aFunc, nStartRow, nEndRow);
return aFunc.getCount();
}
bool ScColumn::HasVisibleDataAt(SCROW nRow) const
{
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(nRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it == maCells.end())
// Likely invalid row number.
return false;
return it->type != sc::element_type_empty;
}
bool ScColumn::IsEmptyAttr() const
{
if (pAttrArray)
return pAttrArray->IsEmpty();
else
return true;
}
bool ScColumn::IsEmptyBlock(SCROW nStartRow, SCROW nEndRow) const
{
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(nStartRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it == maCells.end())
// Invalid row number.
return false;
if (it->type != sc::element_type_empty)
// Non-empty cell at the start position.
return false;
// start position of next block which is not empty.
SCROW nNextRow = nStartRow + it->size - aPos.second;
return nEndRow < nNextRow;
}
bool ScColumn::IsNotesEmptyBlock(SCROW nStartRow, SCROW nEndRow) const
{
std::pair<sc::CellNoteStoreType::const_iterator,size_t> aPos = maCellNotes.position(nStartRow);
sc::CellNoteStoreType::const_iterator it = aPos.first;
if (it == maCellNotes.end())
// Invalid row number.
return false;
if (it->type != sc::element_type_empty)
// Non-empty cell at the start position.
return false;
// start position of next block which is not empty.
SCROW nNextRow = nStartRow + it->size - aPos.second;
return nEndRow < nNextRow;
}
SCSIZE ScColumn::GetEmptyLinesInBlock( SCROW nStartRow, SCROW nEndRow, ScDirection eDir ) const
{
// Given a range of rows, find a top or bottom empty segment. Skip the start row.
switch (eDir)
{
case DIR_TOP:
{
// Determine the length of empty head segment.
size_t nLength = nEndRow - nStartRow;
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(nStartRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it->type != sc::element_type_empty)
// First row is already not empty.
return 0;
// length of this empty block minus the offset.
size_t nThisLen = it->size - aPos.second;
return std::min(nThisLen, nLength);
}
break;
case DIR_BOTTOM:
{
// Determine the length of empty tail segment.
size_t nLength = nEndRow - nStartRow;
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(nEndRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it->type != sc::element_type_empty)
// end row is already not empty.
return 0;
// length of this empty block from the tip to the end row position.
size_t nThisLen = aPos.second + 1;
return std::min(nThisLen, nLength);
}
break;
default:
;
}
return 0;
}
SCROW ScColumn::GetFirstDataPos() const
{
if (IsEmptyData())
return 0;
sc::CellStoreType::const_iterator it = maCells.begin();
if (it->type != sc::element_type_empty)
return 0;
return it->size;
}
SCROW ScColumn::GetLastDataPos() const
{
if (IsEmptyData())
return 0;
sc::CellStoreType::const_reverse_iterator it = maCells.rbegin();
if (it->type != sc::element_type_empty)
return GetDoc()->MaxRow();
return GetDoc()->MaxRow() - static_cast<SCROW>(it->size);
}
SCROW ScColumn::GetLastDataPos( SCROW nLastRow, bool bConsiderCellNotes,
bool bConsiderCellDrawObjects ) const
{
sc::CellStoreType::const_position_type aPos = maCells.position(std::min(nLastRow,GetDoc()->MaxRow()));
if (bConsiderCellNotes && !IsNotesEmptyBlock(nLastRow, nLastRow))
return nLastRow;
if (bConsiderCellDrawObjects && !IsDrawObjectsEmptyBlock(nLastRow, nLastRow))
return nLastRow;
if (aPos.first->type != sc::element_type_empty)
return nLastRow;
if (aPos.first == maCells.begin())
// This is the first block, and is empty.
return 0;
return static_cast<SCROW>(aPos.first->position - 1);
}
bool ScColumn::GetPrevDataPos(SCROW& rRow) const
{
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(rRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it == maCells.end())
return false;
if (it->type == sc::element_type_empty)
{
if (it == maCells.begin())
// No more previous non-empty cell.
return false;
rRow -= aPos.second + 1; // Last row position of the previous block.
return true;
}
// This block is not empty.
if (aPos.second)
{
// There are preceding cells in this block. Simply move back one cell.
--rRow;
return true;
}
// This is the first cell in a non-empty block. Move back to the previous block.
if (it == maCells.begin())
// No more preceding block.
return false;
--rRow; // Move to the last cell of the previous block.
--it;
if (it->type == sc::element_type_empty)
{
// This block is empty.
if (it == maCells.begin())
// No more preceding blocks.
return false;
// Skip the whole empty block segment.
rRow -= it->size;
}
return true;
}
bool ScColumn::GetNextDataPos(SCROW& rRow) const // greater than rRow
{
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(rRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it == maCells.end())
return false;
if (it->type == sc::element_type_empty)
{
// This block is empty. Skip ahead to the next block (if exists).
rRow += it->size - aPos.second;
++it;
if (it == maCells.end())
// No more next block.
return false;
// Next block exists, and is non-empty.
return true;
}
if (aPos.second < it->size - 1)
{
// There are still cells following the current position.
++rRow;
return true;
}
// This is the last cell in the block. Move ahead to the next block.
rRow += it->size - aPos.second; // First cell in the next block.
++it;
if (it == maCells.end())
// No more next block.
return false;
if (it->type == sc::element_type_empty)
{
// Next block is empty. Move to the next block.
rRow += it->size;
++it;
if (it == maCells.end())
return false;
}
return true;
}
bool ScColumn::TrimEmptyBlocks(SCROW& rRowStart, SCROW& rRowEnd) const
{
assert(rRowStart <= rRowEnd);
SCROW nRowStartNew = rRowStart, nRowEndNew = rRowEnd;
// Trim down rRowStart first
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(rRowStart);
sc::CellStoreType::const_iterator it = aPos.first;
if (it == maCells.end())
return false;
if (it->type == sc::element_type_empty)
{
// This block is empty. Skip ahead to the next block (if exists).
nRowStartNew += it->size - aPos.second;
if (nRowStartNew > rRowEnd)
return false;
++it;
if (it == maCells.end())
// No more next block.
return false;
}
// Trim up rRowEnd next
aPos = maCells.position(rRowEnd);
it = aPos.first;
if (it == maCells.end())
{
rRowStart = nRowStartNew;
return true; // Because trimming of rRowStart is ok
}
if (it->type == sc::element_type_empty)
{
// rRowEnd cannot be in the first block which is empty !
assert(it != maCells.begin());
// This block is empty. Skip to the previous block (it exists).
nRowEndNew -= aPos.second + 1; // Last row position of the previous block.
assert(nRowStartNew <= nRowEndNew);
}
rRowStart = nRowStartNew;
rRowEnd = nRowEndNew;
return true;
}
SCROW ScColumn::FindNextVisibleRow(SCROW nRow, bool bForward) const
{
if(bForward)
{
nRow++;
SCROW nEndRow = 0;
bool bHidden = GetDoc()->RowHidden(nRow, nTab, nullptr, &nEndRow);
if(bHidden)
return std::min<SCROW>(GetDoc()->MaxRow(), nEndRow + 1);
else
return nRow;
}
else
{
nRow--;
SCROW nStartRow = GetDoc()->MaxRow();
bool bHidden = GetDoc()->RowHidden(nRow, nTab, &nStartRow);
if(bHidden)
return std::max<SCROW>(0, nStartRow - 1);
else
return nRow;
}
}
SCROW ScColumn::FindNextVisibleRowWithContent(
sc::CellStoreType::const_iterator& itPos, SCROW nRow, bool bForward) const
{
ScDocument* pDocument = GetDoc();
if (bForward)
{
do
{
nRow++;
SCROW nEndRow = 0;
bool bHidden = pDocument->RowHidden(nRow, nTab, nullptr, &nEndRow);
if (bHidden)
{
nRow = nEndRow + 1;
if(nRow >= GetDoc()->MaxRow())
return GetDoc()->MaxRow();
}
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(itPos, nRow);
itPos = aPos.first;
if (itPos == maCells.end())
// Invalid row.
return GetDoc()->MaxRow();
if (itPos->type != sc::element_type_empty)
return nRow;
// Move to the last cell of the current empty block.
nRow += itPos->size - aPos.second - 1;
}
while (nRow < GetDoc()->MaxRow());
return GetDoc()->MaxRow();
}
do
{
nRow--;
SCROW nStartRow = GetDoc()->MaxRow();
bool bHidden = pDocument->RowHidden(nRow, nTab, &nStartRow);
if (bHidden)
{
nRow = nStartRow - 1;
if(nRow <= 0)
return 0;
}
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(itPos, nRow);
itPos = aPos.first;
if (itPos == maCells.end())
// Invalid row.
return 0;
if (itPos->type != sc::element_type_empty)
return nRow;
// Move to the first cell of the current empty block.
nRow -= aPos.second;
}
while (nRow > 0);
return 0;
}
void ScColumn::CellStorageModified()
{
// Remove cached values. Given how often this function is called and how (not that) often
// the cached values are used, it should be more efficient to just discard everything
// instead of trying to figure out each time exactly what to discard.
GetDoc()->DiscardFormulaGroupContext();
// TODO: Update column's "last updated" timestamp here.
#if DEBUG_COLUMN_STORAGE
if (maCells.size() != MAXROWCOUNT)
{
cout << "ScColumn::CellStorageModified: Size of the cell array is incorrect." << endl;
cout.flush();
abort();
}
if (maCellTextAttrs.size() != MAXROWCOUNT)
{
cout << "ScColumn::CellStorageModified: Size of the cell text attribute array is incorrect." << endl;
cout.flush();
abort();
}
if (maBroadcasters.size() != MAXROWCOUNT)
{
cout << "ScColumn::CellStorageModified: Size of the broadcaster array is incorrect." << endl;
cout.flush();
abort();
}
// Make sure that these two containers are synchronized wrt empty segments.
auto lIsEmptyType = [](const auto& rElement) { return rElement.type == sc::element_type_empty; };
// Move to the first empty blocks.
auto itCell = std::find_if(maCells.begin(), maCells.end(), lIsEmptyType);
auto itAttr = std::find_if(maCellTextAttrs.begin(), maCellTextAttrs.end(), lIsEmptyType);
while (itCell != maCells.end())
{
if (itCell->position != itAttr->position || itCell->size != itAttr->size)
{
cout << "ScColumn::CellStorageModified: Cell array and cell text attribute array are out of sync." << endl;
cout << "-- cell array" << endl;
maCells.dump_blocks(cout);
cout << "-- attribute array" << endl;
maCellTextAttrs.dump_blocks(cout);
cout.flush();
abort();
}
// Move to the next empty blocks.
++itCell;
itCell = std::find_if(itCell, maCells.end(), lIsEmptyType);
++itAttr;
itAttr = std::find_if(itAttr, maCellTextAttrs.end(), lIsEmptyType);
}
#endif
}
#if DUMP_COLUMN_STORAGE
namespace {
#define DUMP_FORMULA_RESULTS 0
struct ColumnStorageDumper
{
const ScDocument* mpDoc;
ColumnStorageDumper( const ScDocument* pDoc ) : mpDoc(pDoc) {}
void operator() (const sc::CellStoreType::value_type& rNode) const
{
switch (rNode.type)
{
case sc::element_type_numeric:
cout << " * numeric block (pos=" << rNode.position << ", length=" << rNode.size << ")" << endl;
break;
case sc::element_type_string:
cout << " * string block (pos=" << rNode.position << ", length=" << rNode.size << ")" << endl;
break;
case sc::element_type_edittext:
cout << " * edit-text block (pos=" << rNode.position << ", length=" << rNode.size << ")" << endl;
break;
case sc::element_type_formula:
dumpFormulaBlock(rNode);
break;
case sc::element_type_empty:
cout << " * empty block (pos=" << rNode.position << ", length=" << rNode.size << ")" << endl;
break;
default:
cout << " * unknown block" << endl;
}
}
void dumpFormulaBlock(const sc::CellStoreType::value_type& rNode) const
{
cout << " * formula block (pos=" << rNode.position << ", length=" << rNode.size << ")" << endl;
sc::formula_block::const_iterator it = sc::formula_block::begin(*rNode.data);
sc::formula_block::const_iterator itEnd = sc::formula_block::end(*rNode.data);
for (; it != itEnd; ++it)
{
const ScFormulaCell* pCell = *it;
if (!pCell->IsShared())
{
cout << " * row " << pCell->aPos.Row() << " not shared" << endl;
printFormula(pCell);
printResult(pCell);
continue;
}
if (pCell->GetSharedTopRow() != pCell->aPos.Row())
{
cout << " * row " << pCell->aPos.Row() << " shared with top row "
<< pCell->GetSharedTopRow() << " with length " << pCell->GetSharedLength()
<< endl;
continue;
}
SCROW nLen = pCell->GetSharedLength();
cout << " * group: start=" << pCell->aPos.Row() << ", length=" << nLen << endl;
printFormula(pCell);
printResult(pCell);
if (nLen > 1)
{
for (SCROW i = 0; i < nLen-1; ++i, ++it)
{
pCell = *it;
printResult(pCell);
}
}
}
}
void printFormula(const ScFormulaCell* pCell) const
{
sc::TokenStringContext aCxt(mpDoc, mpDoc->GetGrammar());
OUString aFormula = pCell->GetCode()->CreateString(aCxt, pCell->aPos);
cout << " * formula: " << aFormula << endl;
}
#if DUMP_FORMULA_RESULTS
void printResult(const ScFormulaCell* pCell) const
{
sc::FormulaResultValue aRes = pCell->GetResult();
cout << " * result: ";
switch (aRes.meType)
{
case sc::FormulaResultValue::Value:
cout << aRes.mfValue << " (type: value)";
break;
case sc::FormulaResultValue::String:
cout << "'" << aRes.maString.getString() << "' (type: string)";
break;
case sc::FormulaResultValue::Error:
cout << "error (" << static_cast<int>(aRes.mnError) << ")";
break;
case sc::FormulaResultValue::Invalid:
cout << "invalid";
break;
}
cout << endl;
}
#else
void printResult(const ScFormulaCell*) const
{
(void) this; /* loplugin:staticmethods */
}
#endif
};
}
void ScColumn::DumpColumnStorage() const
{
cout << "-- table: " << nTab << "; column: " << nCol << endl;
std::for_each(maCells.begin(), maCells.end(), ColumnStorageDumper(GetDoc()));
cout << "--" << endl;
}
#endif
void ScColumn::CopyCellTextAttrsToDocument(SCROW nRow1, SCROW nRow2, ScColumn& rDestCol) const
{
rDestCol.maCellTextAttrs.set_empty(nRow1, nRow2); // Empty the destination range first.
sc::CellTextAttrStoreType::const_iterator itBlk = maCellTextAttrs.begin(), itBlkEnd = maCellTextAttrs.end();
// Locate the top row position.
size_t nBlockStart = 0, nRowPos = static_cast<size_t>(nRow1);
itBlk = std::find_if(itBlk, itBlkEnd, [&nRowPos, &nBlockStart](const auto& rAttr) {
return nBlockStart <= nRowPos && nRowPos < nBlockStart + rAttr.size; });
if (itBlk == itBlkEnd)
// Specified range not found. Bail out.
return;
size_t nBlockEnd;
size_t nOffsetInBlock = nRowPos - nBlockStart;
nRowPos = static_cast<size_t>(nRow2); // End row position.
// Keep copying until we hit the end row position.
sc::celltextattr_block::const_iterator itData, itDataEnd;
for (; itBlk != itBlkEnd; ++itBlk, nBlockStart = nBlockEnd, nOffsetInBlock = 0)
{
nBlockEnd = nBlockStart + itBlk->size;
if (!itBlk->data)
{
// Empty block.
if (nBlockStart <= nRowPos && nRowPos < nBlockEnd)
// This block contains the end row.
rDestCol.maCellTextAttrs.set_empty(nBlockStart + nOffsetInBlock, nRowPos);
else
rDestCol.maCellTextAttrs.set_empty(nBlockStart + nOffsetInBlock, nBlockEnd-1);
continue;
}
// Non-empty block.
itData = sc::celltextattr_block::begin(*itBlk->data);
itDataEnd = sc::celltextattr_block::end(*itBlk->data);
std::advance(itData, nOffsetInBlock);
if (nBlockStart <= nRowPos && nRowPos < nBlockEnd)
{
// This block contains the end row. Only copy partially.
size_t nOffset = nRowPos - nBlockStart + 1;
itDataEnd = sc::celltextattr_block::begin(*itBlk->data);
std::advance(itDataEnd, nOffset);
rDestCol.maCellTextAttrs.set(nBlockStart + nOffsetInBlock, itData, itDataEnd);
break;
}
rDestCol.maCellTextAttrs.set(nBlockStart + nOffsetInBlock, itData, itDataEnd);
}
}
namespace {
class CopyCellNotesHandler
{
ScColumn& mrDestCol;
sc::CellNoteStoreType& mrDestNotes;
sc::CellNoteStoreType::iterator miPos;
SCTAB mnSrcTab;
SCCOL mnSrcCol;
SCTAB mnDestTab;
SCCOL mnDestCol;
SCROW mnDestOffset; /// Add this to the source row position to get the destination row.
bool mbCloneCaption;
public:
CopyCellNotesHandler( const ScColumn& rSrcCol, ScColumn& rDestCol, SCROW nDestOffset, bool bCloneCaption ) :
mrDestCol(rDestCol),
mrDestNotes(rDestCol.GetCellNoteStore()),
miPos(mrDestNotes.begin()),
mnSrcTab(rSrcCol.GetTab()),
mnSrcCol(rSrcCol.GetCol()),
mnDestTab(rDestCol.GetTab()),
mnDestCol(rDestCol.GetCol()),
mnDestOffset(nDestOffset),
mbCloneCaption(bCloneCaption) {}
void operator() ( size_t nRow, const ScPostIt* p )
{
SCROW nDestRow = nRow + mnDestOffset;
ScAddress aSrcPos(mnSrcCol, nRow, mnSrcTab);
ScAddress aDestPos(mnDestCol, nDestRow, mnDestTab);
miPos = mrDestNotes.set(miPos, nDestRow, p->Clone(aSrcPos, *mrDestCol.GetDoc(), aDestPos, mbCloneCaption).release());
// Notify our LOK clients also
ScDocShell::LOKCommentNotify(LOKCommentNotificationType::Add, mrDestCol.GetDoc(), aDestPos, p);
}
};
}
void ScColumn::CopyCellNotesToDocument(
SCROW nRow1, SCROW nRow2, ScColumn& rDestCol, bool bCloneCaption, SCROW nRowOffsetDest ) const
{
if (IsNotesEmptyBlock(nRow1, nRow2))
// The column has no cell notes to copy between specified rows.
return;
ScDrawLayer *pDrawLayer = rDestCol.GetDoc()->GetDrawLayer();
bool bWasLocked = bool();
if (pDrawLayer)
{
// Avoid O(n^2) by temporary locking SdrModel which disables broadcasting.
// Each cell note adds undo listener, and all of them would be woken up in ScPostIt::CreateCaption.
bWasLocked = pDrawLayer->isLocked();
pDrawLayer->setLock(true);
}
CopyCellNotesHandler aFunc(*this, rDestCol, nRowOffsetDest, bCloneCaption);
sc::ParseNote(maCellNotes.begin(), maCellNotes, nRow1, nRow2, aFunc);
if (pDrawLayer)
pDrawLayer->setLock(bWasLocked);
}
void ScColumn::DuplicateNotes(SCROW nStartRow, size_t nDataSize, ScColumn& rDestCol, sc::ColumnBlockPosition& maDestBlockPos,
bool bCloneCaption, SCROW nRowOffsetDest ) const
{
CopyCellNotesToDocument(nStartRow, nStartRow + nDataSize -1, rDestCol, bCloneCaption, nRowOffsetDest);
maDestBlockPos.miCellNotePos = rDestCol.maCellNotes.begin();
}
SvtBroadcaster* ScColumn::GetBroadcaster(SCROW nRow)
{
return maBroadcasters.get<SvtBroadcaster*>(nRow);
}
const SvtBroadcaster* ScColumn::GetBroadcaster(SCROW nRow) const
{
return maBroadcasters.get<SvtBroadcaster*>(nRow);
}
void ScColumn::DeleteBroadcasters( sc::ColumnBlockPosition& rBlockPos, SCROW nRow1, SCROW nRow2 )
{
rBlockPos.miBroadcasterPos =
maBroadcasters.set_empty(rBlockPos.miBroadcasterPos, nRow1, nRow2);
}
void ScColumn::PrepareBroadcastersForDestruction()
{
for (auto& rBroadcaster : maBroadcasters)
{
if (rBroadcaster.type == sc::element_type_broadcaster)
{
sc::broadcaster_block::iterator it = sc::broadcaster_block::begin(*rBroadcaster.data);
sc::broadcaster_block::iterator itEnd = sc::broadcaster_block::end(*rBroadcaster.data);
for (; it != itEnd; ++it)
(*it)->PrepareForDestruction();
}
}
}
ScPostIt* ScColumn::GetCellNote(SCROW nRow)
{
return maCellNotes.get<ScPostIt*>(nRow);
}
const ScPostIt* ScColumn::GetCellNote(SCROW nRow) const
{
return maCellNotes.get<ScPostIt*>(nRow);
}
const ScPostIt* ScColumn::GetCellNote( sc::ColumnBlockConstPosition& rBlockPos, SCROW nRow ) const
{
sc::CellNoteStoreType::const_position_type aPos = maCellNotes.position(rBlockPos.miCellNotePos, nRow);
rBlockPos.miCellNotePos = aPos.first;
if (aPos.first->type != sc::element_type_cellnote)
return nullptr;
return sc::cellnote_block::at(*aPos.first->data, aPos.second);
}
ScPostIt* ScColumn::GetCellNote( sc::ColumnBlockConstPosition& rBlockPos, SCROW nRow )
{
return const_cast<ScPostIt*>(const_cast<const ScColumn*>(this)->GetCellNote( rBlockPos, nRow ));
}
void ScColumn::SetCellNote(SCROW nRow, std::unique_ptr<ScPostIt> pNote)
{
//pNote->UpdateCaptionPos(ScAddress(nCol, nRow, nTab)); // TODO notes useful ? slow import with many notes
maCellNotes.set(nRow, pNote.release());
}
namespace {
class CellNoteHandler
{
const ScDocument* m_pDocument;
const ScAddress m_aAddress; // 'incomplete' address consisting of tab, column
const bool m_bForgetCaptionOwnership;
public:
CellNoteHandler(const ScDocument* pDocument, const ScAddress& rPos, bool bForgetCaptionOwnership) :
m_pDocument(pDocument),
m_aAddress(rPos),
m_bForgetCaptionOwnership(bForgetCaptionOwnership) {}
void operator() ( size_t nRow, ScPostIt* p )
{
if (m_bForgetCaptionOwnership)
p->ForgetCaption();
// Create a 'complete' address object
ScAddress aAddr(m_aAddress);
aAddr.SetRow(nRow);
// Notify our LOK clients
ScDocShell::LOKCommentNotify(LOKCommentNotificationType::Remove, m_pDocument, aAddr, p);
}
};
} // anonymous namespace
void ScColumn::CellNotesDeleting(SCROW nRow1, SCROW nRow2, bool bForgetCaptionOwnership)
{
ScAddress aAddr(nCol, 0, nTab);
CellNoteHandler aFunc(GetDoc(), aAddr, bForgetCaptionOwnership);
sc::ParseNote(maCellNotes.begin(), maCellNotes, nRow1, nRow2, aFunc);
}
void ScColumn::DeleteCellNotes( sc::ColumnBlockPosition& rBlockPos, SCROW nRow1, SCROW nRow2, bool bForgetCaptionOwnership )
{
CellNotesDeleting(nRow1, nRow2, bForgetCaptionOwnership);
rBlockPos.miCellNotePos =
maCellNotes.set_empty(rBlockPos.miCellNotePos, nRow1, nRow2);
}
bool ScColumn::HasCellNotes() const
{
return std::any_of(maCellNotes.begin(), maCellNotes.end(),
[](const auto& rCellNote) {
// Having a cellnote block automatically means there is at least one cell note.
return rCellNote.type == sc::element_type_cellnote; });
}
SCROW ScColumn::GetCellNotesMaxRow() const
{
// hypothesis : the column has cell notes (should be checked before)
SCROW maxRow = 0;
for (const auto& rCellNote : maCellNotes)
{
if (rCellNote.type == sc::element_type_cellnote)
maxRow = rCellNote.position + rCellNote.size -1;
}
return maxRow;
}
SCROW ScColumn::GetCellNotesMinRow() const
{
// hypothesis : the column has cell notes (should be checked before)
SCROW minRow = 0;
sc::CellNoteStoreType::const_iterator it = std::find_if(maCellNotes.begin(), maCellNotes.end(),
[](const auto& rCellNote) { return rCellNote.type == sc::element_type_cellnote; });
if (it != maCellNotes.end())
minRow = it->position;
return minRow;
}
sal_uInt16 ScColumn::GetTextWidth(SCROW nRow) const
{
return maCellTextAttrs.get<sc::CellTextAttr>(nRow).mnTextWidth;
}
void ScColumn::SetTextWidth(SCROW nRow, sal_uInt16 nWidth)
{
sc::CellTextAttrStoreType::position_type aPos = maCellTextAttrs.position(nRow);
if (aPos.first->type != sc::element_type_celltextattr)
return;
// Set new value only when the slot is not empty.
sc::celltextattr_block::at(*aPos.first->data, aPos.second).mnTextWidth = nWidth;
CellStorageModified();
}
SvtScriptType ScColumn::GetScriptType( SCROW nRow ) const
{
if (!GetDoc()->ValidRow(nRow) || maCellTextAttrs.is_empty(nRow))
return SvtScriptType::NONE;
return maCellTextAttrs.get<sc::CellTextAttr>(nRow).mnScriptType;
}
SvtScriptType ScColumn::GetRangeScriptType(
sc::CellTextAttrStoreType::iterator& itPos, SCROW nRow1, SCROW nRow2, const sc::CellStoreType::iterator& itrCells_ )
{
if (!GetDoc()->ValidRow(nRow1) || !GetDoc()->ValidRow(nRow2) || nRow1 > nRow2)
return SvtScriptType::NONE;
SCROW nRow = nRow1;
std::pair<sc::CellTextAttrStoreType::iterator,size_t> aRet =
maCellTextAttrs.position(itPos, nRow1);
itPos = aRet.first; // Track the position of cell text attribute array.
sc::CellStoreType::iterator itrCells = itrCells_;
SvtScriptType nScriptType = SvtScriptType::NONE;
bool bUpdated = false;
if (itPos->type == sc::element_type_celltextattr)
{
sc::celltextattr_block::iterator it = sc::celltextattr_block::begin(*itPos->data);
sc::celltextattr_block::iterator itEnd = sc::celltextattr_block::end(*itPos->data);
std::advance(it, aRet.second);
for (; it != itEnd; ++it, ++nRow)
{
if (nRow > nRow2)
return nScriptType;
sc::CellTextAttr& rVal = *it;
if (UpdateScriptType(rVal, nRow, itrCells))
bUpdated = true;
nScriptType |= rVal.mnScriptType;
}
}
else
{
// Skip this whole block.
nRow += itPos->size - aRet.second;
}
while (nRow <= nRow2)
{
++itPos;
if (itPos == maCellTextAttrs.end())
return nScriptType;
if (itPos->type != sc::element_type_celltextattr)
{
// Skip this whole block.
nRow += itPos->size;
continue;
}
sc::celltextattr_block::iterator it = sc::celltextattr_block::begin(*itPos->data);
sc::celltextattr_block::iterator itEnd = sc::celltextattr_block::end(*itPos->data);
for (; it != itEnd; ++it, ++nRow)
{
if (nRow > nRow2)
return nScriptType;
sc::CellTextAttr& rVal = *it;
if (UpdateScriptType(rVal, nRow, itrCells))
bUpdated = true;
nScriptType |= rVal.mnScriptType;
}
}
if (bUpdated)
CellStorageModified();
return nScriptType;
}
void ScColumn::SetScriptType( SCROW nRow, SvtScriptType nType )
{
if (!GetDoc()->ValidRow(nRow))
return;
sc::CellTextAttrStoreType::position_type aPos = maCellTextAttrs.position(nRow);
if (aPos.first->type != sc::element_type_celltextattr)
// Set new value only when the slot is already set.
return;
sc::celltextattr_block::at(*aPos.first->data, aPos.second).mnScriptType = nType;
CellStorageModified();
}
size_t ScColumn::GetFormulaHash( SCROW nRow ) const
{
const ScFormulaCell* pCell = FetchFormulaCell(nRow);
return pCell ? pCell->GetHash() : 0;
}
ScFormulaVectorState ScColumn::GetFormulaVectorState( SCROW nRow ) const
{
const ScFormulaCell* pCell = FetchFormulaCell(nRow);
return pCell ? pCell->GetVectorState() : FormulaVectorUnknown;
}
formula::FormulaTokenRef ScColumn::ResolveStaticReference( SCROW nRow )
{
std::pair<sc::CellStoreType::iterator,size_t> aPos = maCells.position(nRow);
sc::CellStoreType::iterator it = aPos.first;
if (it == maCells.end())
// Invalid row. Return a null token.
return formula::FormulaTokenRef();
switch (it->type)
{
case sc::element_type_numeric:
{
double fVal = sc::numeric_block::at(*it->data, aPos.second);
return formula::FormulaTokenRef(new formula::FormulaDoubleToken(fVal));
}
case sc::element_type_formula:
{
ScFormulaCell* p = sc::formula_block::at(*it->data, aPos.second);
if (p->IsValue())
return formula::FormulaTokenRef(new formula::FormulaDoubleToken(p->GetValue()));
return formula::FormulaTokenRef(new formula::FormulaStringToken(p->GetString()));
}
case sc::element_type_string:
{
const svl::SharedString& rSS = sc::string_block::at(*it->data, aPos.second);
return formula::FormulaTokenRef(new formula::FormulaStringToken(rSS));
}
case sc::element_type_edittext:
{
const EditTextObject* pText = sc::edittext_block::at(*it->data, aPos.second);
OUString aStr = ScEditUtil::GetString(*pText, GetDoc());
svl::SharedString aSS( GetDoc()->GetSharedStringPool().intern(aStr));
return formula::FormulaTokenRef(new formula::FormulaStringToken(aSS));
}
case sc::element_type_empty:
default:
// Return a value of 0.0 in all the other cases.
return formula::FormulaTokenRef(new formula::FormulaDoubleToken(0.0));
}
}
namespace {
class ToMatrixHandler
{
ScMatrix& mrMat;
SCCOL mnMatCol;
SCROW mnTopRow;
ScDocument* mpDoc;
svl::SharedStringPool& mrStrPool;
public:
ToMatrixHandler(ScMatrix& rMat, SCCOL nMatCol, SCROW nTopRow, ScDocument* pDoc) :
mrMat(rMat), mnMatCol(nMatCol), mnTopRow(nTopRow),
mpDoc(pDoc), mrStrPool(pDoc->GetSharedStringPool()) {}
void operator() (size_t nRow, double fVal)
{
mrMat.PutDouble(fVal, mnMatCol, nRow - mnTopRow);
}
void operator() (size_t nRow, const ScFormulaCell* p)
{
// Formula cell may need to re-calculate.
ScFormulaCell& rCell = const_cast<ScFormulaCell&>(*p);
if (rCell.IsValue())
mrMat.PutDouble(rCell.GetValue(), mnMatCol, nRow - mnTopRow);
else
mrMat.PutString(rCell.GetString(), mnMatCol, nRow - mnTopRow);
}
void operator() (size_t nRow, const svl::SharedString& rSS)
{
mrMat.PutString(rSS, mnMatCol, nRow - mnTopRow);
}
void operator() (size_t nRow, const EditTextObject* pStr)
{
mrMat.PutString(mrStrPool.intern(ScEditUtil::GetString(*pStr, mpDoc)), mnMatCol, nRow - mnTopRow);
}
};
}
bool ScColumn::ResolveStaticReference( ScMatrix& rMat, SCCOL nMatCol, SCROW nRow1, SCROW nRow2 )
{
if (nRow1 > nRow2)
return false;
ToMatrixHandler aFunc(rMat, nMatCol, nRow1, GetDoc());
sc::ParseAllNonEmpty(maCells.begin(), maCells, nRow1, nRow2, aFunc);
return true;
}
namespace {
struct CellBucket
{
SCSIZE mnEmpValStart;
SCSIZE mnNumValStart;
SCSIZE mnStrValStart;
SCSIZE mnEmpValCount;
std::vector<double> maNumVals;
std::vector<svl::SharedString> maStrVals;
CellBucket() : mnEmpValStart(0), mnNumValStart(0), mnStrValStart(0), mnEmpValCount(0) {}
void flush(ScMatrix& rMat, SCSIZE nCol)
{
if (mnEmpValCount)
{
rMat.PutEmptyResultVector(mnEmpValCount, nCol, mnEmpValStart);
reset();
}
else if (!maNumVals.empty())
{
const double* p = maNumVals.data();
rMat.PutDouble(p, maNumVals.size(), nCol, mnNumValStart);
reset();
}
else if (!maStrVals.empty())
{
const svl::SharedString* p = maStrVals.data();
rMat.PutString(p, maStrVals.size(), nCol, mnStrValStart);
reset();
}
}
void reset()
{
mnEmpValStart = mnNumValStart = mnStrValStart = 0;
mnEmpValCount = 0;
maNumVals.clear();
maStrVals.clear();
}
};
class FillMatrixHandler
{
ScMatrix& mrMat;
size_t mnMatCol;
size_t mnTopRow;
ScDocument* mpDoc;
svl::SharedStringPool& mrPool;
svl::SharedStringPool* mpPool; // if matrix is not in the same document
public:
FillMatrixHandler(ScMatrix& rMat, size_t nMatCol, size_t nTopRow, ScDocument* pDoc, svl::SharedStringPool* pPool) :
mrMat(rMat), mnMatCol(nMatCol), mnTopRow(nTopRow),
mpDoc(pDoc), mrPool(pDoc->GetSharedStringPool()), mpPool(pPool) {}
void operator() (const sc::CellStoreType::value_type& node, size_t nOffset, size_t nDataSize)
{
size_t nMatRow = node.position + nOffset - mnTopRow;
switch (node.type)
{
case sc::element_type_numeric:
{
const double* p = &sc::numeric_block::at(*node.data, nOffset);
mrMat.PutDouble(p, nDataSize, mnMatCol, nMatRow);
}
break;
case sc::element_type_string:
{
if (!mpPool)
{
const svl::SharedString* p = &sc::string_block::at(*node.data, nOffset);
mrMat.PutString(p, nDataSize, mnMatCol, nMatRow);
}
else
{
std::vector<svl::SharedString> aStrings;
aStrings.reserve(nDataSize);
const svl::SharedString* p = &sc::string_block::at(*node.data, nOffset);
for (size_t i = 0; i < nDataSize; ++i)
{
aStrings.push_back(mpPool->intern(p[i].getString()));
}
mrMat.PutString(aStrings.data(), aStrings.size(), mnMatCol, nMatRow);
}
}
break;
case sc::element_type_edittext:
{
std::vector<svl::SharedString> aSSs;
aSSs.reserve(nDataSize);
sc::edittext_block::const_iterator it = sc::edittext_block::begin(*node.data);
std::advance(it, nOffset);
sc::edittext_block::const_iterator itEnd = it;
std::advance(itEnd, nDataSize);
for (; it != itEnd; ++it)
{
OUString aStr = ScEditUtil::GetString(**it, mpDoc);
if (!mpPool)
aSSs.push_back(mrPool.intern(aStr));
else
aSSs.push_back(mpPool->intern(aStr));
}
const svl::SharedString* p = aSSs.data();
mrMat.PutString(p, nDataSize, mnMatCol, nMatRow);
}
break;
case sc::element_type_formula:
{
CellBucket aBucket;
sc::formula_block::const_iterator it = sc::formula_block::begin(*node.data);
std::advance(it, nOffset);
sc::formula_block::const_iterator itEnd = it;
std::advance(itEnd, nDataSize);
size_t nPrevRow = 0, nThisRow = node.position + nOffset;
for (; it != itEnd; ++it, nPrevRow = nThisRow, ++nThisRow)
{
ScFormulaCell& rCell = **it;
if (rCell.IsEmpty())
{
if (aBucket.mnEmpValCount && nThisRow == nPrevRow + 1)
{
// Secondary empty results.
++aBucket.mnEmpValCount;
}
else
{
// First empty result.
aBucket.flush(mrMat, mnMatCol);
aBucket.mnEmpValStart = nThisRow - mnTopRow;
++aBucket.mnEmpValCount;
}
continue;
}
FormulaError nErr;
double fVal;
if (rCell.GetErrorOrValue(nErr, fVal))
{
if (nErr != FormulaError::NONE)
fVal = CreateDoubleError(nErr);
if (!aBucket.maNumVals.empty() && nThisRow == nPrevRow + 1)
{
// Secondary numbers.
aBucket.maNumVals.push_back(fVal);
}
else
{
// First number.
aBucket.flush(mrMat, mnMatCol);
aBucket.mnNumValStart = nThisRow - mnTopRow;
aBucket.maNumVals.push_back(fVal);
}
continue;
}
svl::SharedString aStr = rCell.GetString();
if (mpPool)
aStr = mpPool->intern(aStr.getString());
if (!aBucket.maStrVals.empty() && nThisRow == nPrevRow + 1)
{
// Secondary strings.
aBucket.maStrVals.push_back(aStr);
}
else
{
// First string.
aBucket.flush(mrMat, mnMatCol);
aBucket.mnStrValStart = nThisRow - mnTopRow;
aBucket.maStrVals.push_back(aStr);
}
}
aBucket.flush(mrMat, mnMatCol);
}
break;
default:
;
}
}
};
}
void ScColumn::FillMatrix( ScMatrix& rMat, size_t nMatCol, SCROW nRow1, SCROW nRow2, svl::SharedStringPool* pPool ) const
{
FillMatrixHandler aFunc(rMat, nMatCol, nRow1, GetDoc(), pPool);
sc::ParseBlock(maCells.begin(), maCells, aFunc, nRow1, nRow2);
}
namespace {
template<typename Blk>
void getBlockIterators(
const sc::CellStoreType::iterator& it, size_t& rLenRemain,
typename Blk::iterator& rData, typename Blk::iterator& rDataEnd )
{
rData = Blk::begin(*it->data);
if (rLenRemain >= it->size)
{
// Block is shorter than the remaining requested length.
rDataEnd = Blk::end(*it->data);
rLenRemain -= it->size;
}
else
{
rDataEnd = rData;
std::advance(rDataEnd, rLenRemain);
rLenRemain = 0;
}
}
bool appendToBlock(
ScDocument* pDoc, sc::FormulaGroupContext& rCxt, sc::FormulaGroupContext::ColArray& rColArray,
size_t nPos, size_t nArrayLen, const sc::CellStoreType::iterator& _it, const sc::CellStoreType::iterator& itEnd )
{
svl::SharedStringPool& rPool = pDoc->GetSharedStringPool();
size_t nLenRemain = nArrayLen - nPos;
double fNan;
rtl::math::setNan(&fNan);
for (sc::CellStoreType::iterator it = _it; it != itEnd; ++it)
{
switch (it->type)
{
case sc::element_type_string:
{
sc::string_block::iterator itData, itDataEnd;
getBlockIterators<sc::string_block>(it, nLenRemain, itData, itDataEnd);
rCxt.ensureStrArray(rColArray, nArrayLen);
for (; itData != itDataEnd; ++itData, ++nPos)
(*rColArray.mpStrArray)[nPos] = itData->getData();
}
break;
case sc::element_type_edittext:
{
sc::edittext_block::iterator itData, itDataEnd;
getBlockIterators<sc::edittext_block>(it, nLenRemain, itData, itDataEnd);
rCxt.ensureStrArray(rColArray, nArrayLen);
for (; itData != itDataEnd; ++itData, ++nPos)
{
OUString aStr = ScEditUtil::GetString(**itData, pDoc);
(*rColArray.mpStrArray)[nPos] = rPool.intern(aStr).getData();
}
}
break;
case sc::element_type_formula:
{
sc::formula_block::iterator itData, itDataEnd;
getBlockIterators<sc::formula_block>(it, nLenRemain, itData, itDataEnd);
/* tdf#91416 setting progress in triggers a resize of the window
and so ScTabView::DoResize and an InterpretVisible and
InterpretDirtyCells which resets the mpFormulaGroupCxt that
the current rCxt points to, which is bad, so disable progress
during GetResult
*/
ScProgress *pProgress = ScProgress::GetInterpretProgress();
bool bTempDisableProgress = pProgress && pProgress->Enabled();
if (bTempDisableProgress)
pProgress->Disable();
for (; itData != itDataEnd; ++itData, ++nPos)
{
ScFormulaCell& rFC = **itData;
sc::FormulaResultValue aRes = rFC.GetResult();
if (aRes.meType == sc::FormulaResultValue::Invalid || aRes.mnError != FormulaError::NONE)
{
if (aRes.mnError == FormulaError::CircularReference)
{
// This cell needs to be recalculated on next visit.
rFC.SetErrCode(FormulaError::NONE);
rFC.SetDirtyVar();
}
return false;
}
if (aRes.meType == sc::FormulaResultValue::String)
{
rCxt.ensureStrArray(rColArray, nArrayLen);
(*rColArray.mpStrArray)[nPos] = aRes.maString.getData();
}
else
{
rCxt.ensureNumArray(rColArray, nArrayLen);
(*rColArray.mpNumArray)[nPos] = aRes.mfValue;
}
}
if (bTempDisableProgress)
pProgress->Enable();
}
break;
case sc::element_type_empty:
{
if (nLenRemain > it->size)
{
nPos += it->size;
nLenRemain -= it->size;
}
else
{
nPos = nArrayLen;
nLenRemain = 0;
}
}
break;
case sc::element_type_numeric:
{
sc::numeric_block::iterator itData, itDataEnd;
getBlockIterators<sc::numeric_block>(it, nLenRemain, itData, itDataEnd);
rCxt.ensureNumArray(rColArray, nArrayLen);
for (; itData != itDataEnd; ++itData, ++nPos)
(*rColArray.mpNumArray)[nPos] = *itData;
}
break;
default:
return false;
}
if (!nLenRemain)
return true;
}
return false;
}
void copyFirstStringBlock(
ScDocument& rDoc, sc::FormulaGroupContext::StrArrayType& rArray, size_t nLen, const sc::CellStoreType::iterator& itBlk )
{
sc::FormulaGroupContext::StrArrayType::iterator itArray = rArray.begin();
switch (itBlk->type)
{
case sc::element_type_string:
{
sc::string_block::iterator it = sc::string_block::begin(*itBlk->data);
sc::string_block::iterator itEnd = it;
std::advance(itEnd, nLen);
for (; it != itEnd; ++it, ++itArray)
*itArray = it->getData();
}
break;
case sc::element_type_edittext:
{
sc::edittext_block::iterator it = sc::edittext_block::begin(*itBlk->data);
sc::edittext_block::iterator itEnd = it;
std::advance(itEnd, nLen);
svl::SharedStringPool& rPool = rDoc.GetSharedStringPool();
for (; it != itEnd; ++it, ++itArray)
{
EditTextObject* pText = *it;
OUString aStr = ScEditUtil::GetString(*pText, &rDoc);
*itArray = rPool.intern(aStr).getData();
}
}
break;
default:
;
}
}
sc::FormulaGroupContext::ColArray*
copyFirstFormulaBlock(
sc::FormulaGroupContext& rCxt, const sc::CellStoreType::iterator& itBlk, size_t nArrayLen,
SCTAB nTab, SCCOL nCol )
{
double fNan;
rtl::math::setNan(&fNan);
size_t nLen = std::min(itBlk->size, nArrayLen);
sc::formula_block::iterator it = sc::formula_block::begin(*itBlk->data);
sc::formula_block::iterator itEnd;
sc::FormulaGroupContext::NumArrayType* pNumArray = nullptr;
sc::FormulaGroupContext::StrArrayType* pStrArray = nullptr;
itEnd = it;
std::advance(itEnd, nLen);
size_t nPos = 0;
for (; it != itEnd; ++it, ++nPos)
{
ScFormulaCell& rFC = **it;
sc::FormulaResultValue aRes = rFC.GetResult();
if (aRes.meType == sc::FormulaResultValue::Invalid || aRes.mnError != FormulaError::NONE)
{
if (aRes.mnError == FormulaError::CircularReference)
{
// This cell needs to be recalculated on next visit.
rFC.SetErrCode(FormulaError::NONE);
rFC.SetDirtyVar();
}
return nullptr;
}
if (aRes.meType == sc::FormulaResultValue::Value)
{
if (!pNumArray)
{
rCxt.m_NumArrays.push_back(
std::make_unique<sc::FormulaGroupContext::NumArrayType>(nArrayLen, fNan));
pNumArray = rCxt.m_NumArrays.back().get();
}
(*pNumArray)[nPos] = aRes.mfValue;
}
else
{
if (!pStrArray)
{
rCxt.m_StrArrays.push_back(
std::make_unique<sc::FormulaGroupContext::StrArrayType>(nArrayLen, nullptr));
pStrArray = rCxt.m_StrArrays.back().get();
}
(*pStrArray)[nPos] = aRes.maString.getData();
}
}
if (!pNumArray && !pStrArray)
// At least one of these arrays should be allocated.
return nullptr;
return rCxt.setCachedColArray(nTab, nCol, pNumArray, pStrArray);
}
struct NonNullStringFinder
{
bool operator() (const rtl_uString* p) const { return p != nullptr; }
};
bool hasNonEmpty( const sc::FormulaGroupContext::StrArrayType& rArray, SCROW nRow1, SCROW nRow2 )
{
// The caller has to make sure the array is at least nRow2+1 long.
sc::FormulaGroupContext::StrArrayType::const_iterator it = rArray.begin();
std::advance(it, nRow1);
sc::FormulaGroupContext::StrArrayType::const_iterator itEnd = it;
std::advance(itEnd, nRow2-nRow1+1);
return std::any_of(it, itEnd, NonNullStringFinder());
}
struct ProtectFormulaGroupContext
{
ProtectFormulaGroupContext( ScDocument* d )
: doc( d ) { doc->BlockFormulaGroupContextDiscard( true ); }
~ProtectFormulaGroupContext()
{ doc->BlockFormulaGroupContextDiscard( false ); }
ScDocument* doc;
};
}
formula::VectorRefArray ScColumn::FetchVectorRefArray( SCROW nRow1, SCROW nRow2 )
{
if (nRow1 > nRow2)
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
// See if the requested range is already cached.
ScDocument* pDocument = GetDoc();
sc::FormulaGroupContext& rCxt = *(pDocument->GetFormulaGroupContext());
sc::FormulaGroupContext::ColArray* pColArray = rCxt.getCachedColArray(nTab, nCol, nRow2+1);
if (pColArray)
{
const double* pNum = nullptr;
if (pColArray->mpNumArray)
pNum = &(*pColArray->mpNumArray)[nRow1];
rtl_uString** pStr = nullptr;
if (pColArray->mpStrArray && hasNonEmpty(*pColArray->mpStrArray, nRow1, nRow2))
pStr = &(*pColArray->mpStrArray)[nRow1];
return formula::VectorRefArray(pNum, pStr);
}
// ScColumn::CellStorageModified() simply discards the entire cache (FormulaGroupContext)
// on any modification. However getting cell values may cause this to be called
// if interpreting a cell results in a change to it (not just its result though).
// So temporarily block the discarding.
ProtectFormulaGroupContext protectContext( GetDoc());
double fNan;
rtl::math::setNan(&fNan);
// We need to fetch all cell values from row 0 to nRow2 for caching purposes.
sc::CellStoreType::iterator itBlk = maCells.begin();
switch (itBlk->type)
{
case sc::element_type_numeric:
{
if (o3tl::make_unsigned(nRow2) < itBlk->size)
{
// Requested range falls within the first block. No need to cache.
const double* p = &sc::numeric_block::at(*itBlk->data, nRow1);
return formula::VectorRefArray(p);
}
// Allocate a new array and copy the values to it.
sc::numeric_block::const_iterator it = sc::numeric_block::begin(*itBlk->data);
sc::numeric_block::const_iterator itEnd = sc::numeric_block::end(*itBlk->data);
rCxt.m_NumArrays.push_back(
std::make_unique<sc::FormulaGroupContext::NumArrayType>(it, itEnd));
sc::FormulaGroupContext::NumArrayType& rArray = *rCxt.m_NumArrays.back();
rArray.resize(nRow2+1, fNan); // allocate to the requested length.
pColArray = rCxt.setCachedColArray(nTab, nCol, &rArray, nullptr);
if (!pColArray)
// Failed to insert a new cached column array.
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
// Fill the remaining array with values from the following blocks.
size_t nPos = itBlk->size;
++itBlk;
if (!appendToBlock(pDocument, rCxt, *pColArray, nPos, nRow2+1, itBlk, maCells.end()))
{
rCxt.discardCachedColArray(nTab, nCol);
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
}
rtl_uString** pStr = nullptr;
if (pColArray->mpStrArray && hasNonEmpty(*pColArray->mpStrArray, nRow1, nRow2))
pStr = &(*pColArray->mpStrArray)[nRow1];
return formula::VectorRefArray(&(*pColArray->mpNumArray)[nRow1], pStr);
}
break;
case sc::element_type_string:
case sc::element_type_edittext:
{
rCxt.m_StrArrays.push_back(
std::make_unique<sc::FormulaGroupContext::StrArrayType>(nRow2+1, nullptr));
sc::FormulaGroupContext::StrArrayType& rArray = *rCxt.m_StrArrays.back();
pColArray = rCxt.setCachedColArray(nTab, nCol, nullptr, &rArray);
if (!pColArray)
// Failed to insert a new cached column array.
return formula::VectorRefArray();
if (o3tl::make_unsigned(nRow2) < itBlk->size)
{
// Requested range falls within the first block.
copyFirstStringBlock(*pDocument, rArray, nRow2+1, itBlk);
return formula::VectorRefArray(&rArray[nRow1]);
}
copyFirstStringBlock(*pDocument, rArray, itBlk->size, itBlk);
// Fill the remaining array with values from the following blocks.
size_t nPos = itBlk->size;
++itBlk;
if (!appendToBlock(pDocument, rCxt, *pColArray, nPos, nRow2+1, itBlk, maCells.end()))
{
rCxt.discardCachedColArray(nTab, nCol);
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
}
assert(pColArray->mpStrArray);
rtl_uString** pStr = nullptr;
if (hasNonEmpty(*pColArray->mpStrArray, nRow1, nRow2))
pStr = &(*pColArray->mpStrArray)[nRow1];
if (pColArray->mpNumArray)
return formula::VectorRefArray(&(*pColArray->mpNumArray)[nRow1], pStr);
else
return formula::VectorRefArray(pStr);
}
break;
case sc::element_type_formula:
{
if (o3tl::make_unsigned(nRow2) < itBlk->size)
{
// Requested length is within a single block, and the data is
// not cached.
pColArray = copyFirstFormulaBlock(rCxt, itBlk, nRow2+1, nTab, nCol);
if (!pColArray)
// Failed to insert a new cached column array.
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
const double* pNum = nullptr;
rtl_uString** pStr = nullptr;
if (pColArray->mpNumArray)
pNum = &(*pColArray->mpNumArray)[nRow1];
if (pColArray->mpStrArray)
pStr = &(*pColArray->mpStrArray)[nRow1];
return formula::VectorRefArray(pNum, pStr);
}
pColArray = copyFirstFormulaBlock(rCxt, itBlk, nRow2+1, nTab, nCol);
if (!pColArray)
{
// Failed to insert a new cached column array.
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
}
size_t nPos = itBlk->size;
++itBlk;
if (!appendToBlock(pDocument, rCxt, *pColArray, nPos, nRow2+1, itBlk, maCells.end()))
{
rCxt.discardCachedColArray(nTab, nCol);
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
}
const double* pNum = nullptr;
rtl_uString** pStr = nullptr;
if (pColArray->mpNumArray)
pNum = &(*pColArray->mpNumArray)[nRow1];
if (pColArray->mpStrArray && hasNonEmpty(*pColArray->mpStrArray, nRow1, nRow2))
pStr = &(*pColArray->mpStrArray)[nRow1];
return formula::VectorRefArray(pNum, pStr);
}
break;
case sc::element_type_empty:
{
// Fill the whole length with NaN's.
rCxt.m_NumArrays.push_back(
std::make_unique<sc::FormulaGroupContext::NumArrayType>(nRow2+1, fNan));
sc::FormulaGroupContext::NumArrayType& rArray = *rCxt.m_NumArrays.back();
pColArray = rCxt.setCachedColArray(nTab, nCol, &rArray, nullptr);
if (!pColArray)
// Failed to insert a new cached column array.
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
if (o3tl::make_unsigned(nRow2) < itBlk->size)
return formula::VectorRefArray(&(*pColArray->mpNumArray)[nRow1]);
// Fill the remaining array with values from the following blocks.
size_t nPos = itBlk->size;
++itBlk;
if (!appendToBlock(pDocument, rCxt, *pColArray, nPos, nRow2+1, itBlk, maCells.end()))
{
rCxt.discardCachedColArray(nTab, nCol);
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
}
if (pColArray->mpStrArray && hasNonEmpty(*pColArray->mpStrArray, nRow1, nRow2))
return formula::VectorRefArray(&(*pColArray->mpNumArray)[nRow1], &(*pColArray->mpStrArray)[nRow1]);
else
return formula::VectorRefArray(&(*pColArray->mpNumArray)[nRow1]);
}
break;
default:
;
}
return formula::VectorRefArray(formula::VectorRefArray::Invalid);
}
#ifdef DBG_UTIL
static void assertNoInterpretNeededHelper( const sc::CellStoreType::value_type& node,
size_t nOffset, size_t nDataSize )
{
switch (node.type)
{
case sc::element_type_formula:
{
sc::formula_block::const_iterator it = sc::formula_block::begin(*node.data);
std::advance(it, nOffset);
sc::formula_block::const_iterator itEnd = it;
std::advance(itEnd, nDataSize);
for (; it != itEnd; ++it)
{
const ScFormulaCell* pCell = *it;
assert( !pCell->NeedsInterpret());
}
break;
}
}
}
void ScColumn::AssertNoInterpretNeeded( SCROW nRow1, SCROW nRow2 )
{
assert(nRow2 >= nRow1);
sc::ParseBlock( maCells.begin(), maCells, assertNoInterpretNeededHelper, 0, nRow2 );
}
#endif
void ScColumn::SetFormulaResults( SCROW nRow, const double* pResults, size_t nLen )
{
sc::CellStoreType::position_type aPos = maCells.position(nRow);
sc::CellStoreType::iterator it = aPos.first;
if (it->type != sc::element_type_formula)
{
// This is not a formula block.
assert( false );
return;
}
size_t nBlockLen = it->size - aPos.second;
if (nBlockLen < nLen)
// Result array is longer than the length of formula cells. Not good.
return;
sc::formula_block::iterator itCell = sc::formula_block::begin(*it->data);
std::advance(itCell, aPos.second);
const double* pResEnd = pResults + nLen;
for (; pResults != pResEnd; ++pResults, ++itCell)
{
ScFormulaCell& rCell = **itCell;
FormulaError nErr = GetDoubleErrorValue(*pResults);
if (nErr != FormulaError::NONE)
rCell.SetResultError(nErr);
else
rCell.SetResultDouble(*pResults);
rCell.ResetDirty();
rCell.SetChanged(true);
}
}
void ScColumn::CalculateInThread( ScInterpreterContext& rContext, SCROW nRow, size_t nLen, size_t nOffset,
unsigned nThisThread, unsigned nThreadsTotal)
{
assert(GetDoc()->IsThreadedGroupCalcInProgress());
sc::CellStoreType::position_type aPos = maCells.position(nRow);
sc::CellStoreType::iterator it = aPos.first;
if (it->type != sc::element_type_formula)
{
// This is not a formula block.
assert( false );
return;
}
size_t nBlockLen = it->size - aPos.second;
if (nBlockLen < nLen)
// Length is longer than the length of formula cells. Not good.
return;
sc::formula_block::iterator itCell = sc::formula_block::begin(*it->data);
std::advance(itCell, aPos.second);
for (size_t i = 0; i < nLen; ++i, ++itCell)
{
if (nThreadsTotal > 0 && ((i + nOffset) % nThreadsTotal) != nThisThread)
continue;
ScFormulaCell& rCell = **itCell;
if (!rCell.NeedsInterpret())
continue;
// Here we don't call IncInterpretLevel() and DecInterpretLevel() as this call site is
// always in a threaded calculation.
rCell.InterpretTail(rContext, ScFormulaCell::SCITP_NORMAL);
}
}
void ScColumn::HandleStuffAfterParallelCalculation( SCROW nRow, size_t nLen, ScInterpreter* pInterpreter )
{
sc::CellStoreType::position_type aPos = maCells.position(nRow);
sc::CellStoreType::iterator it = aPos.first;
if (it->type != sc::element_type_formula)
{
// This is not a formula block.
assert( false );
return;
}
size_t nBlockLen = it->size - aPos.second;
if (nBlockLen < nLen)
// Length is longer than the length of formula cells. Not good.
return;
sc::formula_block::iterator itCell = sc::formula_block::begin(*it->data);
std::advance(itCell, aPos.second);
for (size_t i = 0; i < nLen; ++i, ++itCell)
{
ScFormulaCell& rCell = **itCell;
rCell.HandleStuffAfterParallelCalculation(pInterpreter);
}
}
void ScColumn::SetNumberFormat( SCROW nRow, sal_uInt32 nNumberFormat )
{
ApplyAttr(nRow, SfxUInt32Item(ATTR_VALUE_FORMAT, nNumberFormat));
}
ScFormulaCell * const * ScColumn::GetFormulaCellBlockAddress( SCROW nRow, size_t& rBlockSize ) const
{
if (!GetDoc()->ValidRow(nRow))
{
rBlockSize = 0;
return nullptr;
}
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(nRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it == maCells.end())
{
rBlockSize = 0;
return nullptr;
}
if (it->type != sc::element_type_formula)
{
// Not a formula cell.
rBlockSize = 0;
return nullptr;
}
rBlockSize = it->size;
return &sc::formula_block::at(*it->data, aPos.second);
}
const ScFormulaCell* ScColumn::FetchFormulaCell( SCROW nRow ) const
{
size_t nBlockSize = 0;
ScFormulaCell const * const * pp = GetFormulaCellBlockAddress( nRow, nBlockSize );
return pp ? *pp : nullptr;
}
void ScColumn::FindDataAreaPos(SCROW& rRow, bool bDown) const
{
// If the cell is empty, find the next non-empty cell position. If the
// cell is not empty, find the last non-empty cell position in the current
// contiguous cell block.
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(rRow);
sc::CellStoreType::const_iterator it = aPos.first;
if (it == maCells.end())
// Invalid row.
return;
if (it->type == sc::element_type_empty)
{
// Current cell is empty. Find the next non-empty cell.
rRow = FindNextVisibleRowWithContent(it, rRow, bDown);
return;
}
// Current cell is not empty.
SCROW nNextRow = FindNextVisibleRow(rRow, bDown);
aPos = maCells.position(it, nNextRow);
it = aPos.first;
if (it->type == sc::element_type_empty)
{
// Next visible cell is empty. Find the next non-empty cell.
rRow = FindNextVisibleRowWithContent(it, nNextRow, bDown);
return;
}
// Next visible cell is non-empty. Find the edge that's still visible.
SCROW nLastRow = nNextRow;
do
{
nNextRow = FindNextVisibleRow(nLastRow, bDown);
if (nNextRow == nLastRow)
break;
aPos = maCells.position(it, nNextRow);
it = aPos.first;
if (it->type != sc::element_type_empty)
nLastRow = nNextRow;
}
while (it->type != sc::element_type_empty);
rRow = nLastRow;
}
bool ScColumn::HasDataAt(SCROW nRow, bool bConsiderCellNotes, bool bConsiderCellDrawObjects) const
{
if (bConsiderCellNotes && !IsNotesEmptyBlock(nRow, nRow))
return true;
if (bConsiderCellDrawObjects && !IsDrawObjectsEmptyBlock(nRow, nRow))
return true;
return maCells.get_type(nRow) != sc::element_type_empty;
}
bool ScColumn::HasDataAt(sc::ColumnBlockConstPosition& rBlockPos, SCROW nRow,
bool bConsiderCellNotes, bool bConsiderCellDrawObjects) const
{
if (bConsiderCellNotes && !IsNotesEmptyBlock(nRow, nRow))
return true;
if (bConsiderCellDrawObjects && !IsDrawObjectsEmptyBlock(nRow, nRow))
return true;
std::pair<sc::CellStoreType::const_iterator,size_t> aPos = maCells.position(rBlockPos.miCellPos, nRow);
if (aPos.first == maCells.end())
return false;
rBlockPos.miCellPos = aPos.first; // Store this for next call.
return aPos.first->type != sc::element_type_empty;
}
bool ScColumn::HasDataAt(sc::ColumnBlockPosition& rBlockPos, SCROW nRow,
bool bConsiderCellNotes, bool bConsiderCellDrawObjects)
{
if (bConsiderCellNotes && !IsNotesEmptyBlock(nRow, nRow))
return true;
if (bConsiderCellDrawObjects && !IsDrawObjectsEmptyBlock(nRow, nRow))
return true;
std::pair<sc::CellStoreType::iterator,size_t> aPos = maCells.position(rBlockPos.miCellPos, nRow);
if (aPos.first == maCells.end())
return false;
rBlockPos.miCellPos = aPos.first; // Store this for next call.
return aPos.first->type != sc::element_type_empty;
}
bool ScColumn::IsAllAttrEqual( const ScColumn& rCol, SCROW nStartRow, SCROW nEndRow ) const
{
if (pAttrArray && rCol.pAttrArray)
return pAttrArray->IsAllEqual( *rCol.pAttrArray, nStartRow, nEndRow );
else
return !pAttrArray && !rCol.pAttrArray;
}
bool ScColumn::IsVisibleAttrEqual( const ScColumn& rCol, SCROW nStartRow, SCROW nEndRow ) const
{
if (pAttrArray && rCol.pAttrArray)
return pAttrArray->IsVisibleEqual( *rCol.pAttrArray, nStartRow, nEndRow );
else
return !pAttrArray && !rCol.pAttrArray;
}
bool ScColumn::GetFirstVisibleAttr( SCROW& rFirstRow ) const
{
if (pAttrArray)
return pAttrArray->GetFirstVisibleAttr( rFirstRow );
else
return false;
}
bool ScColumn::GetLastVisibleAttr( SCROW& rLastRow ) const
{
if (pAttrArray)
{
// row of last cell is needed
SCROW nLastData = GetLastDataPos(); // always including notes, 0 if none
return pAttrArray->GetLastVisibleAttr( rLastRow, nLastData );
}
else
return false;
}
bool ScColumn::HasVisibleAttrIn( SCROW nStartRow, SCROW nEndRow ) const
{
if (pAttrArray)
return pAttrArray->HasVisibleAttrIn( nStartRow, nEndRow );
else
return false;
}
namespace {
class FindUsedRowsHandler
{
typedef mdds::flat_segment_tree<SCROW,bool> UsedRowsType;
UsedRowsType& mrUsed;
UsedRowsType::const_iterator miUsed;
public:
explicit FindUsedRowsHandler(UsedRowsType& rUsed) : mrUsed(rUsed), miUsed(rUsed.begin()) {}
void operator() (const sc::CellStoreType::value_type& node, size_t nOffset, size_t nDataSize)
{
if (node.type == sc::element_type_empty)
return;
SCROW nRow1 = node.position + nOffset;
SCROW nRow2 = nRow1 + nDataSize - 1;
miUsed = mrUsed.insert(miUsed, nRow1, nRow2+1, true).first;
}
};
}
void ScColumn::FindUsed( SCROW nStartRow, SCROW nEndRow, mdds::flat_segment_tree<SCROW,bool>& rUsed ) const
{
FindUsedRowsHandler aFunc(rUsed);
sc::ParseBlock(maCells.begin(), maCells, aFunc, nStartRow, nEndRow);
}
namespace {
void startListening(
sc::BroadcasterStoreType& rStore, sc::BroadcasterStoreType::iterator& itBlockPos, size_t nElemPos,
SCROW nRow, SvtListener& rLst)
{
switch (itBlockPos->type)
{
case sc::element_type_broadcaster:
{
// Broadcaster already exists here.
SvtBroadcaster* pBC = sc::broadcaster_block::at(*itBlockPos->data, nElemPos);
rLst.StartListening(*pBC);
}
break;
case mdds::mtv::element_type_empty:
{
// No broadcaster exists at this position yet.
SvtBroadcaster* pBC = new SvtBroadcaster;
rLst.StartListening(*pBC);
itBlockPos = rStore.set(itBlockPos, nRow, pBC); // Store the block position for next iteration.
}
break;
default:
#if DEBUG_COLUMN_STORAGE
cout << "ScColumn::StartListening: wrong block type encountered in the broadcaster storage." << endl;
cout.flush();
abort();
#else
;
#endif
}
}
}
void ScColumn::StartListening( SvtListener& rLst, SCROW nRow )
{
std::pair<sc::BroadcasterStoreType::iterator,size_t> aPos = maBroadcasters.position(nRow);
startListening(maBroadcasters, aPos.first, aPos.second, nRow, rLst);
}
void ScColumn::EndListening( SvtListener& rLst, SCROW nRow )
{
SvtBroadcaster* pBC = GetBroadcaster(nRow);
if (!pBC)
return;
rLst.EndListening(*pBC);
if (!pBC->HasListeners())
// There is no more listeners for this cell. Remove the broadcaster.
maBroadcasters.set_empty(nRow, nRow);
}
void ScColumn::StartListening( sc::StartListeningContext& rCxt, const ScAddress& rAddress, SvtListener& rLst )
{
if (!GetDoc()->ValidRow(rAddress.Row()))
return;
sc::ColumnBlockPosition* p = rCxt.getBlockPosition(rAddress.Tab(), rAddress.Col());
if (!p)
return;
sc::BroadcasterStoreType::iterator& it = p->miBroadcasterPos;
std::pair<sc::BroadcasterStoreType::iterator,size_t> aPos = maBroadcasters.position(it, rAddress.Row());
it = aPos.first; // store the block position for next iteration.
startListening(maBroadcasters, it, aPos.second, rAddress.Row(), rLst);
}
void ScColumn::EndListening( sc::EndListeningContext& rCxt, const ScAddress& rAddress, SvtListener& rListener )
{
sc::ColumnBlockPosition* p = rCxt.getBlockPosition(rAddress.Tab(), rAddress.Col());
if (!p)
return;
sc::BroadcasterStoreType::iterator& it = p->miBroadcasterPos;
std::pair<sc::BroadcasterStoreType::iterator,size_t> aPos = maBroadcasters.position(it, rAddress.Row());
it = aPos.first; // store the block position for next iteration.
if (it->type != sc::element_type_broadcaster)
return;
SvtBroadcaster* pBC = sc::broadcaster_block::at(*it->data, aPos.second);
assert(pBC);
rListener.EndListening(*pBC);
if (!pBC->HasListeners())
// There is no more listeners for this cell. Add it to the purge list for later purging.
rCxt.addEmptyBroadcasterPosition(rAddress.Tab(), rAddress.Col(), rAddress.Row());
}
namespace {
class CompileDBFormulaHandler
{
sc::CompileFormulaContext& mrCxt;
public:
explicit CompileDBFormulaHandler( sc::CompileFormulaContext& rCxt ) :
mrCxt(rCxt) {}
void operator() (size_t, ScFormulaCell* p)
{
p->CompileDBFormula(mrCxt);
}
};
struct CompileColRowNameFormulaHandler
{
sc::CompileFormulaContext& mrCxt;
public:
explicit CompileColRowNameFormulaHandler( sc::CompileFormulaContext& rCxt ) : mrCxt(rCxt) {}
void operator() (size_t, ScFormulaCell* p)
{
p->CompileColRowNameFormula(mrCxt);
}
};
}
void ScColumn::CompileDBFormula( sc::CompileFormulaContext& rCxt )
{
CompileDBFormulaHandler aFunc(rCxt);
sc::ProcessFormula(maCells, aFunc);
RegroupFormulaCells();
}
void ScColumn::CompileColRowNameFormula( sc::CompileFormulaContext& rCxt )
{
CompileColRowNameFormulaHandler aFunc(rCxt);
sc::ProcessFormula(maCells, aFunc);
RegroupFormulaCells();
}
namespace {
class UpdateSubTotalHandler
{
ScFunctionData& mrData;
void update(double fVal, bool bVal)
{
if (mrData.getError())
return;
switch (mrData.getFunc())
{
case SUBTOTAL_FUNC_CNT2: // everything
mrData.update( fVal);
break;
default: // only numeric values
if (bVal)
mrData.update( fVal);
}
}
public:
explicit UpdateSubTotalHandler(ScFunctionData& rData) : mrData(rData) {}
void operator() (size_t /*nRow*/, double fVal)
{
update(fVal, true);
}
void operator() (size_t /*nRow*/, const svl::SharedString&)
{
update(0.0, false);
}
void operator() (size_t /*nRow*/, const EditTextObject*)
{
update(0.0, false);
}
void operator() (size_t /*nRow*/, ScFormulaCell* pCell)
{
double fVal = 0.0;
bool bVal = false;
if (mrData.getFunc() != SUBTOTAL_FUNC_CNT2) // it doesn't interest us
{
if (pCell->GetErrCode() != FormulaError::NONE)
{
if (mrData.getFunc() != SUBTOTAL_FUNC_CNT) // simply remove from count
mrData.setError();
}
else if (pCell->IsValue())
{
fVal = pCell->GetValue();
bVal = true;
}
// otherwise text
}
update(fVal, bVal);
}
};
}
// multiple selections:
void ScColumn::UpdateSelectionFunction(
const ScRangeList& rRanges, ScFunctionData& rData, const ScFlatBoolRowSegments& rHiddenRows )
{
sc::SingleColumnSpanSet aSpanSet;
aSpanSet.scan(rRanges, nTab, nCol); // mark all selected rows.
if (aSpanSet.empty())
return; // nothing to do, bail out
// Exclude all hidden rows.
ScFlatBoolRowSegments::RangeData aRange;
SCROW nRow = 0;
while (nRow <= GetDoc()->MaxRow())
{
if (!rHiddenRows.getRangeData(nRow, aRange))
break;
if (aRange.mbValue)
// Hidden range detected.
aSpanSet.set(nRow, aRange.mnRow2, false);
nRow = aRange.mnRow2 + 1;
}
sc::SingleColumnSpanSet::SpansType aSpans;
aSpanSet.getSpans(aSpans);
switch (rData.getFunc())
{
case SUBTOTAL_FUNC_SELECTION_COUNT:
{
// Simply count selected rows regardless of cell contents.
for (const auto& rSpan : aSpans)
rData.update( rSpan.mnRow2 - rSpan.mnRow1 + 1);
}
break;
case SUBTOTAL_FUNC_CNT2:
{
// We need to parse all non-empty cells.
sc::CellStoreType::const_iterator itCellPos = maCells.begin();
UpdateSubTotalHandler aFunc(rData);
for (const auto& rSpan : aSpans)
{
itCellPos = sc::ParseAllNonEmpty(
itCellPos, maCells, rSpan.mnRow1, rSpan.mnRow2, aFunc);
}
}
break;
default:
{
// We need to parse only numeric values.
sc::CellStoreType::const_iterator itCellPos = maCells.begin();
UpdateSubTotalHandler aFunc(rData);
for (const auto& rSpan : aSpans)
{
itCellPos = sc::ParseFormulaNumeric(
itCellPos, maCells, rSpan.mnRow1, rSpan.mnRow2, aFunc);
}
}
}
}
namespace {
class WeightedCounter
{
sal_uLong mnCount;
public:
WeightedCounter() : mnCount(0) {}
void operator() (const sc::CellStoreType::value_type& node)
{
mnCount += getWeight(node);
}
static sal_uLong getWeight(const sc::CellStoreType::value_type& node)
{
switch (node.type)
{
case sc::element_type_numeric:
case sc::element_type_string:
return node.size;
break;
case sc::element_type_formula:
{
// Each formula cell is worth its code length plus 5.
return std::accumulate(sc::formula_block::begin(*node.data), sc::formula_block::end(*node.data), size_t(0),
[](const size_t& rCount, const ScFormulaCell* p) { return rCount + 5 + p->GetCode()->GetCodeLen(); });
}
break;
case sc::element_type_edittext:
// each edit-text cell is worth 50.
return node.size * 50;
break;
default:
return 0;
}
}
sal_uLong getCount() const { return mnCount; }
};
class WeightedCounterWithRows
{
const SCROW mnStartRow;
const SCROW mnEndRow;
sal_uLong mnCount;
public:
WeightedCounterWithRows(SCROW nStartRow, SCROW nEndRow)
: mnStartRow(nStartRow)
, mnEndRow(nEndRow)
, mnCount(0)
{
}
void operator() (const sc::CellStoreType::value_type& node)
{
const SCROW nRow1 = node.position;
const SCROW nRow2 = nRow1 + 1;
if (! ((nRow2 < mnStartRow) || (nRow1 > mnEndRow)))
{
mnCount += WeightedCounter::getWeight(node);
}
}
sal_uLong getCount() const { return mnCount; }
};
}
sal_uLong ScColumn::GetWeightedCount() const
{
const WeightedCounter aFunc = std::for_each(maCells.begin(), maCells.end(),
WeightedCounter());
return aFunc.getCount();
}
sal_uLong ScColumn::GetWeightedCount(SCROW nStartRow, SCROW nEndRow) const
{
const WeightedCounterWithRows aFunc = std::for_each(maCells.begin(), maCells.end(),
WeightedCounterWithRows(nStartRow, nEndRow));
return aFunc.getCount();
}
namespace {
class CodeCounter
{
size_t mnCount;
public:
CodeCounter() : mnCount(0) {}
void operator() (size_t, const ScFormulaCell* p)
{
mnCount += p->GetCode()->GetCodeLen();
}
size_t getCount() const { return mnCount; }
};
}
sal_uInt32 ScColumn::GetCodeCount() const
{
CodeCounter aFunc;
sc::ParseFormula(maCells, aFunc);
return aFunc.getCount();
}
SCSIZE ScColumn::GetPatternCount() const
{
return pAttrArray ? pAttrArray->Count() : 0;
}
SCSIZE ScColumn::GetPatternCount( SCROW nRow1, SCROW nRow2 ) const
{
return pAttrArray ? pAttrArray->Count( nRow1, nRow2 ) : 0;
}
bool ScColumn::ReservePatternCount( SCSIZE nReserve )
{
return pAttrArray && pAttrArray->Reserve( nReserve );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|