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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 <memory>
#include <utility>
#include <MarkManager.hxx>
#include <bookmark.hxx>
#include <crossrefbookmark.hxx>
#include <crsrsh.hxx>
#include <annotationmark.hxx>
#include <doc.hxx>
#include <IDocumentRedlineAccess.hxx>
#include <IDocumentState.hxx>
#include <IDocumentUndoRedo.hxx>
#include <docary.hxx>
#include <xmloff/odffields.hxx>
#include <mvsave.hxx>
#include <ndtxt.hxx>
#include <node.hxx>
#include <pam.hxx>
#include <redline.hxx>
#include <rolbck.hxx>
#include <rtl/ustring.hxx>
#include <sal/types.h>
#include <sal/log.hxx>
#include <UndoBookmark.hxx>
#include <tools/datetimeutils.hxx>
#include <txtfrm.hxx>
#include <view.hxx>
#include <libxml/xmlstring.h>
#include <libxml/xmlwriter.h>
#include <comphelper/lok.hxx>
#include <strings.hrc>
constexpr OUString S_ANNOTATION_BOOKMARK = u"____"_ustr;
using namespace ::sw::mark;
static bool IsAnnotationMark(const sw::mark::MarkBase& rBkmk);
namespace
{
bool lcl_GreaterThan( const SwPosition& rPos, const SwNode& rNdIdx, std::optional<sal_Int32> oContentIdx )
{
return oContentIdx.has_value()
? ( rPos.GetNode() > rNdIdx
|| ( rPos.GetNode() == rNdIdx
&& rPos.GetContentIndex() >= *oContentIdx ) )
: rPos.GetNode() >= rNdIdx;
}
bool lcl_Lower( const SwPosition& rPos, const SwNode& rNdIdx, std::optional<sal_Int32> oContentIdx )
{
if (rPos.GetNode() < rNdIdx)
return true;
if (rPos.GetNode() != rNdIdx || !oContentIdx)
return false;
if (rPos.GetContentIndex() < *oContentIdx)
return true;
// paragraph end selected?
return rNdIdx.IsTextNode() && *oContentIdx == rNdIdx.GetTextNode()->Len();
}
template<class MarkT>
bool lcl_MarkOrderingByStart(const MarkT *const pFirst,
const MarkT *const pSecond)
{
SwPosition const& rFirstStart(pFirst->GetMarkStart());
SwPosition const& rSecondStart(pSecond->GetMarkStart());
if (rFirstStart.GetNode() != rSecondStart.GetNode())
{
return rFirstStart.GetNode() < rSecondStart.GetNode();
}
const sal_Int32 nFirstContent = rFirstStart.GetContentIndex();
const sal_Int32 nSecondContent = rSecondStart.GetContentIndex();
if (nFirstContent != 0 || nSecondContent != 0)
{
return nFirstContent < nSecondContent;
}
SwContentNode const*const pFirstNode(rFirstStart.nContent.GetContentNode());
SwContentNode const*const pSecondNode(rSecondStart.nContent.GetContentNode());
if ((pFirstNode != nullptr) != (pSecondNode != nullptr))
{ // consistency with SwPosition::operator<
return pSecondNode != nullptr;
}
bool const bCRFirst (IsCrossRefBookmark(*pFirst));
bool const bCRSecond(IsCrossRefBookmark(*pSecond));
if (bCRFirst == bCRSecond)
{
return false; // equal
}
return bCRFirst; // cross-ref sorts *before*
}
// specialise to avoid loplugin:faileddyncast
template<>
bool lcl_MarkOrderingByStart(const AnnotationMark *const pFirst,
const AnnotationMark *const pSecond)
{
SwPosition const& rFirstStart(pFirst->GetMarkStart());
SwPosition const& rSecondStart(pSecond->GetMarkStart());
if (rFirstStart.GetNode() != rSecondStart.GetNode())
{
return rFirstStart.GetNode() < rSecondStart.GetNode();
}
const sal_Int32 nFirstContent = rFirstStart.GetContentIndex();
const sal_Int32 nSecondContent = rSecondStart.GetContentIndex();
if (nFirstContent != 0 || nSecondContent != 0)
{
return nFirstContent < nSecondContent;
}
SwContentNode const*const pFirstNode(rFirstStart.nContent.GetContentNode());
SwContentNode const*const pSecondNode(rSecondStart.nContent.GetContentNode());
if ((pFirstNode != nullptr) != (pSecondNode != nullptr))
{ // consistency with SwPosition::operator<
return pSecondNode != nullptr;
}
return false; // equal
}
template<class MarkT>
bool lcl_MarkOrderingByEnd(const MarkT *const pFirst,
const MarkT *const pSecond)
{
return pFirst->GetMarkEnd() < pSecond->GetMarkEnd();
}
template<class MarkT>
void lcl_InsertMarkSorted(std::vector<MarkT*>& io_vMarks,
MarkT *const pMark)
{
io_vMarks.insert(
lower_bound(
io_vMarks.begin(),
io_vMarks.end(),
pMark,
&lcl_MarkOrderingByStart<MarkT>),
pMark);
}
void lcl_PositionFromContentNode(
std::optional<SwPosition>& rFoundPos,
const SwContentNode * const pContentNode,
const bool bAtEnd)
{
rFoundPos.emplace(*pContentNode, bAtEnd ? pContentNode->Len() : 0);
}
// return a position at the begin of rEnd, if it is a ContentNode
// else set it to the begin of the Node after rEnd, if there is one
// else set it to the end of the node before rStt
// else set it to the ContentNode of the Pos outside the Range
void lcl_FindExpelPosition(
std::optional<SwPosition>& rFoundPos,
const SwNode& rStt,
const SwNode& rEnd,
const SwPosition& rOtherPosition)
{
const SwContentNode * pNode = rEnd.GetContentNode();
bool bPosAtEndOfNode = false;
if ( pNode == nullptr)
{
SwNodeIndex aEnd(rEnd);
pNode = SwNodes::GoNext(&aEnd);
bPosAtEndOfNode = false;
}
if ( pNode == nullptr )
{
SwNodeIndex aStt(rStt);
pNode = SwNodes::GoPrevious(&aStt);
bPosAtEndOfNode = true;
}
if ( pNode != nullptr )
{
lcl_PositionFromContentNode( rFoundPos, pNode, bPosAtEndOfNode );
return;
}
rFoundPos = rOtherPosition;
}
template<class MarkT>
struct CompareIMarkStartsBefore
{
bool operator()(SwPosition const& rPos, const MarkT* pMark)
{
return rPos < pMark->GetMarkStart();
}
bool operator()(const MarkT* pMark, SwPosition const& rPos)
{
return pMark->GetMarkStart() < rPos;
}
bool operator()(const MarkT* pMark, SwNode const& rPos)
{
return pMark->GetMarkStart().GetNode() < rPos;
}
};
// Apple llvm-g++ 4.2.1 with _GLIBCXX_DEBUG won't eat boost::bind for this
// Neither will MSVC 2008 with _DEBUG
template<class MarkT>
struct CompareIMarkStartsAfter
{
bool operator()(SwPosition const& rPos, const MarkT* pMark)
{
return pMark->GetMarkStart() > rPos;
}
};
struct CompareIMarkStartsAfterReverse
{
bool operator()(const sw::mark::MarkBase* pMark, SwPosition const& rPos)
{
return pMark->GetMarkStart() > rPos;
}
};
template<class MarkT>
MarkT* lcl_getMarkAfter(const std::vector<MarkT*>& rMarks, const SwPosition& rPos,
bool bLoop)
{
auto const pMarkAfter = upper_bound(
rMarks.begin(),
rMarks.end(),
rPos,
CompareIMarkStartsAfter<MarkT>());
if(pMarkAfter == rMarks.end())
{
if (bLoop && rMarks.begin() != rMarks.end())
return *rMarks.begin();
return nullptr;
}
return *pMarkAfter;
};
template<class MarkT>
MarkT* lcl_getMarkBefore(const std::vector<MarkT*>& rMarks, const SwPosition& rPos,
bool bLoop)
{
// candidates from which to choose the mark before
std::vector<MarkT*> vCandidates;
// no need to consider marks starting after rPos
auto const pCandidatesEnd = upper_bound(
rMarks.begin(),
rMarks.end(),
rPos,
CompareIMarkStartsAfter<MarkT>());
vCandidates.reserve(pCandidatesEnd - rMarks.begin());
// only marks ending before are candidates
remove_copy_if(
rMarks.begin(),
pCandidatesEnd,
back_inserter(vCandidates),
[&rPos] (const MarkT *const pMark) { return !(pMark->GetMarkEnd() < rPos); } );
// no candidate left => we are in front of the first mark or there are none
if(vCandidates.empty())
{
if (bLoop && rMarks.begin() != rMarks.end())
return *(rMarks.end() - 1);
return nullptr;
}
// return the highest (last) candidate using mark end ordering
return *max_element(vCandidates.begin(), vCandidates.end(), &lcl_MarkOrderingByEnd<MarkT>);
}
bool lcl_FixCorrectedMark(
const bool bChangedPos,
const bool bChangedOPos,
MarkBase* io_pMark )
{
if ( IsAnnotationMark(*io_pMark) )
{
// annotation marks are allowed to span a table cell range.
// but trigger sorting to be save
return true;
}
if ( ( bChangedPos || bChangedOPos )
&& io_pMark->IsExpanded()
&& io_pMark->GetOtherMarkPos().GetNode().FindTableBoxStartNode() !=
io_pMark->GetMarkPos().GetNode().FindTableBoxStartNode() )
{
if ( !bChangedOPos )
{
io_pMark->SetMarkPos( io_pMark->GetOtherMarkPos() );
}
io_pMark->ClearOtherMarkPos();
DdeBookmark * const pDdeBkmk = dynamic_cast< DdeBookmark*>(io_pMark);
if ( pDdeBkmk != nullptr
&& pDdeBkmk->IsServer() )
{
pDdeBkmk->SetRefObject(nullptr);
}
return true;
}
return false;
}
template<class MarkT>
bool lcl_MarkEqualByStart(const MarkT *const pFirst,
const MarkT *const pSecond)
{
return !lcl_MarkOrderingByStart<MarkT>(pFirst, pSecond) &&
!lcl_MarkOrderingByStart<MarkT>(pSecond, pFirst);
}
template<class MarkT>
typename std::vector<MarkT*>::const_iterator lcl_FindMark(
std::vector<MarkT*>& rMarks,
const MarkT *const pMarkToFind)
{
auto ppCurrentMark = lower_bound(
rMarks.begin(), rMarks.end(),
pMarkToFind, &lcl_MarkOrderingByStart<MarkT>);
// since there are usually not too many marks on the same start
// position, we are not doing a bisect search for the upper bound
// but instead start to iterate from pMarkLow directly
while (ppCurrentMark != rMarks.end() && lcl_MarkEqualByStart<MarkT>(*ppCurrentMark, pMarkToFind))
{
if(*ppCurrentMark == pMarkToFind)
{
return ppCurrentMark;
}
++ppCurrentMark;
}
// reached a mark starting on a later start pos or the end of the
// vector => not found
return rMarks.end();
};
template<class MarkT>
typename std::vector<MarkT*>::const_iterator lcl_FindMarkAtPos(
std::vector<MarkT*>& rMarks,
const SwPosition& rPos,
const IDocumentMarkAccess::MarkType eType)
{
for (auto ppCurrentMark = lower_bound(
rMarks.begin(), rMarks.end(),
rPos,
CompareIMarkStartsBefore<MarkT>());
ppCurrentMark != rMarks.end();
++ppCurrentMark)
{
// Once we reach a mark starting after the target pos
// we do not need to continue
if((*ppCurrentMark)->GetMarkStart() > rPos)
break;
if(IDocumentMarkAccess::GetType(**ppCurrentMark) == eType)
{
return ppCurrentMark;
}
}
// reached a mark starting on a later start pos or the end of the
// vector => not found
return rMarks.end();
};
template <class container_t>
auto lcl_FindMarkByName(const SwMarkName& rName, const container_t& container)
{
return find_if(begin(container), end(container),
[&rName](const auto* item) { return item->GetName() == rName; });
}
template<class MarkT>
void lcl_DebugMarks(std::vector<MarkT*> const& rMarks)
{
#if OSL_DEBUG_LEVEL > 0
SAL_INFO("sw.core", rMarks.size() << " Marks");
for (auto ppMark = rMarks.begin();
ppMark != rMarks.end();
++ppMark)
{
MarkT* pMark = *ppMark;
const SwPosition* const pStPos = &pMark->GetMarkStart();
const SwPosition* const pEndPos = &pMark->GetMarkEnd();
SAL_INFO("sw.core",
sal_Int32(pStPos->GetNodeIndex()) << "," <<
pStPos->GetContentIndex() << " " <<
sal_Int32(pEndPos->GetNodeIndex()) << "," <<
pEndPos->GetContentIndex() << " " <<
typeid(*pMark).name() << " " <<
pMark->GetName().toString());
}
#else
(void) rMarks;
#endif
assert(std::is_sorted(rMarks.begin(), rMarks.end(), lcl_MarkOrderingByStart<MarkT>));
};
}
static bool IsNavigatorReminder(const MarkBase& rBkmk)
{
const std::type_info* const pMarkTypeInfo = &typeid(rBkmk);
// not using dynamic_cast<> here for performance
return (*pMarkTypeInfo == typeid(NavigatorReminder));
}
static bool IsCrossRefBookmark(const sw::mark::MarkBase& rBkmk)
{
// not using dynamic_cast<> here for performance
const std::type_info* const pMarkTypeInfo = &typeid(rBkmk);
return (*pMarkTypeInfo == typeid(CrossRefHeadingBookmark))
|| (*pMarkTypeInfo == typeid(CrossRefNumItemBookmark));
}
static bool IsAnnotationMark(const sw::mark::MarkBase& rBkmk)
{
// not using dynamic_cast<> here for performance
const std::type_info* const pMarkTypeInfo = &typeid(rBkmk);
return (*pMarkTypeInfo == typeid(AnnotationMark));
}
IDocumentMarkAccess::MarkType IDocumentMarkAccess::GetType(const MarkBase& rBkmk)
{
const std::type_info* const pMarkTypeInfo = &typeid(rBkmk);
// not using dynamic_cast<> here for performance
if(*pMarkTypeInfo == typeid(UnoMark))
return MarkType::UNO_BOOKMARK;
else if(*pMarkTypeInfo == typeid(DdeBookmark))
return MarkType::DDE_BOOKMARK;
else if(*pMarkTypeInfo == typeid(Bookmark))
return MarkType::BOOKMARK;
else if(*pMarkTypeInfo == typeid(CrossRefHeadingBookmark))
return MarkType::CROSSREF_HEADING_BOOKMARK;
else if(*pMarkTypeInfo == typeid(CrossRefNumItemBookmark))
return MarkType::CROSSREF_NUMITEM_BOOKMARK;
else if(*pMarkTypeInfo == typeid(AnnotationMark))
return MarkType::ANNOTATIONMARK;
else if(*pMarkTypeInfo == typeid(TextFieldmark))
return MarkType::TEXT_FIELDMARK;
else if(*pMarkTypeInfo == typeid(CheckboxFieldmark))
return MarkType::CHECKBOX_FIELDMARK;
else if(*pMarkTypeInfo == typeid(DropDownFieldmark))
return MarkType::DROPDOWN_FIELDMARK;
else if(*pMarkTypeInfo == typeid(DateFieldmark))
return MarkType::DATE_FIELDMARK;
else if(*pMarkTypeInfo == typeid(NavigatorReminder))
return MarkType::NAVIGATOR_REMINDER;
else
{
assert(false && "IDocumentMarkAccess::GetType(..)"
" - unknown MarkType. This needs to be fixed!");
return MarkType::UNO_BOOKMARK;
}
}
OUString IDocumentMarkAccess::GetCrossRefHeadingBookmarkNamePrefix()
{
return u"__RefHeading__"_ustr;
}
bool IDocumentMarkAccess::IsLegalPaMForCrossRefHeadingBookmark( const SwPaM& rPaM )
{
return rPaM.Start()->GetNode().IsTextNode() &&
rPaM.Start()->GetContentIndex() == 0 &&
( !rPaM.HasMark() ||
( rPaM.GetMark()->GetNode() == rPaM.GetPoint()->GetNode() &&
rPaM.End()->GetContentIndex() == rPaM.End()->GetNode().GetTextNode()->Len() ) );
}
void IDocumentMarkAccess::DeleteFieldmarkCommand(::sw::mark::Fieldmark const& rMark)
{
if (GetType(rMark) != MarkType::TEXT_FIELDMARK)
{
return; // TODO FORMDATE has no command?
}
SwPaM pam(sw::mark::FindFieldSep(rMark), rMark.GetMarkStart());
pam.GetPoint()->AdjustContent(+1); // skip CH_TXT_ATR_FIELDSTART
pam.GetDoc().getIDocumentContentOperations().DeleteAndJoin(pam);
}
namespace sw::mark
{
MarkManager::MarkManager(SwDoc& rDoc)
: m_rDoc(rDoc)
, m_pLastActiveFieldmark(nullptr)
{ }
// In the mode where the names are not checked, we need to avoid a case where there was a
// bookmark, and a file is inserted at an earlier point, with the same-name bookmark, causing
// a rename of the pre-existing bookmark. usedNames and uncheckedNameMarks are used for that.
// usedNames is pre-populated with the existing names (at the moment when the mode started);
// and uncheckedNameMarks stores only the marks needing the checks.
class UniqueNameChecksGuard_impl : public MarkManager::UniqueNameChecksGuard
{
public:
UniqueNameChecksGuard_impl(MarkManager& aParent)
: parent(aParent)
{
assert(parent.m_pUniqueNameChecksGuard == nullptr);
parent.m_pUniqueNameChecksGuard = this;
// Populate the pre-existing already deduplicated names
for (auto& pMark : parent.m_vAllMarks)
usedNames.insert(pMark->GetName().toString());
}
void ImplDestroy()
{
assert(parent.m_pUniqueNameChecksGuard == this);
for (auto& pMark : parent.m_vAllMarks)
{
if (!uncheckedNameMarks.contains(pMark))
continue;
pMark->SetName(
parent.getUniqueMarkName(pMark->GetName(), [this](const SwMarkName& n)
{ return usedNames.insert(n.toString()).second; }));
}
parent.m_pUniqueNameChecksGuard = nullptr;
}
~UniqueNameChecksGuard_impl() override
{
suppress_fun_call_w_exception(ImplDestroy());
}
void add(sw::mark::MarkBase* mark) { uncheckedNameMarks.insert(mark); }
private:
MarkManager& parent;
// marks with possibly duplicating names
std::unordered_set<sw::mark::MarkBase*> uncheckedNameMarks;
// container for deduplicating names
std::unordered_set<OUString> usedNames;
};
std::unique_ptr<MarkManager::UniqueNameChecksGuard> MarkManager::disableUniqueNameChecks()
{
if (m_pUniqueNameChecksGuard)
return {}; // nested call
return std::unique_ptr<MarkManager::UniqueNameChecksGuard>(
new UniqueNameChecksGuard_impl(*this));
}
::sw::mark::MarkBase* MarkManager::makeMark(const SwPaM& rPaM,
const SwMarkName& rName,
const IDocumentMarkAccess::MarkType eType,
sw::mark::InsertMode const eMode,
SwPosition const*const pSepPos)
{
#if OSL_DEBUG_LEVEL > 0
{
const SwPosition* const pPos1 = rPaM.GetPoint();
const SwPosition* pPos2 = pPos1;
if(rPaM.HasMark())
pPos2 = rPaM.GetMark();
SAL_INFO("sw.core",
rName.toString() << " " <<
sal_Int32(pPos1->GetNodeIndex() )<< "," <<
pPos1->GetContentIndex() << " " <<
sal_Int32(pPos2->GetNodeIndex()) << "," <<
pPos2->GetContentIndex());
}
#endif
if ( (!rPaM.GetPoint()->GetNode().IsTextNode()
&& (eType != MarkType::UNO_BOOKMARK
// SwXTextRange can be on table node or plain start node (FLY_AT_FLY)
|| !rPaM.GetPoint()->GetNode().IsStartNode()))
|| (!rPaM.GetMark()->GetNode().IsTextNode()
&& (eType != MarkType::UNO_BOOKMARK
|| !rPaM.GetMark()->GetNode().IsStartNode())))
{
SAL_WARN("sw.core", "MarkManager::makeMark(..)"
" - refusing to create mark on non-textnode");
return nullptr;
}
// There should only be one CrossRefBookmark per Textnode per Type
if ((eType == MarkType::CROSSREF_NUMITEM_BOOKMARK || eType == MarkType::CROSSREF_HEADING_BOOKMARK)
&& (lcl_FindMarkAtPos(m_vBookmarks, *rPaM.Start(), eType) != m_vBookmarks.end()))
{ // this can happen via UNO API
SAL_WARN("sw.core", "MarkManager::makeMark(..)"
" - refusing to create duplicate CrossRefBookmark");
return nullptr;
}
if ((eType == MarkType::CHECKBOX_FIELDMARK || eType == MarkType::DROPDOWN_FIELDMARK)
&& (eMode == InsertMode::New
? *rPaM.GetPoint() != *rPaM.GetMark()
// CopyText: pam covers CH_TXT_ATR_FORMELEMENT
: (rPaM.GetPoint()->GetNode() != rPaM.GetMark()->GetNode()
|| rPaM.Start()->GetContentIndex() + 1 != rPaM.End()->GetContentIndex())))
{
SAL_WARN("sw.core", "MarkManager::makeMark(..)"
" - invalid range on point fieldmark");
return nullptr;
}
if ((eType == MarkType::TEXT_FIELDMARK || eType == MarkType::DATE_FIELDMARK)
&& (rPaM.GetPoint()->GetNode().StartOfSectionNode() != rPaM.GetMark()->GetNode().StartOfSectionNode()
|| (pSepPos && rPaM.GetPoint()->GetNode().StartOfSectionNode() != pSepPos->GetNode().StartOfSectionNode())))
{
SAL_WARN("sw.core", "MarkManager::makeMark(..)"
" - invalid range on fieldmark, different nodes array sections");
return nullptr;
}
if ((eType == MarkType::TEXT_FIELDMARK || eType == MarkType::DATE_FIELDMARK)
// can't check for Copy - it asserts - but it's also obviously unnecessary
&& eMode == InsertMode::New
&& sw::mark::IsFieldmarkOverlap(rPaM))
{
SAL_WARN("sw.core", "MarkManager::makeMark(..)"
" - invalid range on fieldmark, overlaps existing fieldmark or meta-field");
return nullptr;
}
// create mark
std::unique_ptr<::sw::mark::MarkBase> pMark;
switch(eType)
{
case IDocumentMarkAccess::MarkType::TEXT_FIELDMARK:
pMark = std::make_unique<TextFieldmark>(rPaM, rName);
break;
case IDocumentMarkAccess::MarkType::CHECKBOX_FIELDMARK:
pMark = std::make_unique<CheckboxFieldmark>(rPaM, rName);
break;
case IDocumentMarkAccess::MarkType::DROPDOWN_FIELDMARK:
pMark = std::make_unique<DropDownFieldmark>(rPaM, rName);
break;
case IDocumentMarkAccess::MarkType::DATE_FIELDMARK:
pMark = std::make_unique<DateFieldmark>(rPaM);
break;
case IDocumentMarkAccess::MarkType::NAVIGATOR_REMINDER:
pMark = std::make_unique<NavigatorReminder>(rPaM);
break;
case IDocumentMarkAccess::MarkType::BOOKMARK:
pMark = std::make_unique<Bookmark>(rPaM, vcl::KeyCode(), rName);
break;
case IDocumentMarkAccess::MarkType::DDE_BOOKMARK:
pMark = std::make_unique<DdeBookmark>(rPaM);
break;
case IDocumentMarkAccess::MarkType::CROSSREF_HEADING_BOOKMARK:
pMark = std::make_unique<CrossRefHeadingBookmark>(rPaM, vcl::KeyCode(), rName);
break;
case IDocumentMarkAccess::MarkType::CROSSREF_NUMITEM_BOOKMARK:
pMark = std::make_unique<CrossRefNumItemBookmark>(rPaM, vcl::KeyCode(), rName);
break;
case IDocumentMarkAccess::MarkType::UNO_BOOKMARK:
pMark = std::make_unique<UnoMark>(rPaM);
break;
case IDocumentMarkAccess::MarkType::ANNOTATIONMARK:
pMark = std::make_unique<AnnotationMark>( rPaM, rName );
break;
}
assert(pMark && "MarkManager::makeMark(..) - Mark was not created.");
if(pMark->GetMarkPos() != pMark->GetMarkStart())
pMark->Swap();
// for performance reasons, we trust UnoMarks to have a (generated) unique name
if (eType != IDocumentMarkAccess::MarkType::UNO_BOOKMARK)
{
if (!m_pUniqueNameChecksGuard)
{
pMark->SetName(getUniqueMarkName(
pMark->GetName(), [this](const SwMarkName& n)
{ return lcl_FindMarkByName(n, m_vAllMarks) == m_vAllMarks.end(); }));
}
else
{
m_pUniqueNameChecksGuard->add(pMark.get());
}
}
// insert any dummy chars before inserting into sorted vectors
pMark->InitDoc(m_rDoc, eMode, pSepPos);
// register mark
lcl_InsertMarkSorted(m_vAllMarks, pMark.get());
switch(eType)
{
case IDocumentMarkAccess::MarkType::BOOKMARK:
case IDocumentMarkAccess::MarkType::CROSSREF_NUMITEM_BOOKMARK:
case IDocumentMarkAccess::MarkType::CROSSREF_HEADING_BOOKMARK:
lcl_InsertMarkSorted(m_vBookmarks, static_cast<Bookmark*>(pMark.get()));
break;
case IDocumentMarkAccess::MarkType::TEXT_FIELDMARK:
case IDocumentMarkAccess::MarkType::CHECKBOX_FIELDMARK:
case IDocumentMarkAccess::MarkType::DROPDOWN_FIELDMARK:
case IDocumentMarkAccess::MarkType::DATE_FIELDMARK:
lcl_InsertMarkSorted(m_vFieldmarks, static_cast<Fieldmark*>(pMark.get()));
break;
case IDocumentMarkAccess::MarkType::ANNOTATIONMARK:
lcl_InsertMarkSorted( m_vAnnotationMarks, static_cast<AnnotationMark*>(pMark.get()) );
break;
case IDocumentMarkAccess::MarkType::NAVIGATOR_REMINDER:
case IDocumentMarkAccess::MarkType::DDE_BOOKMARK:
case IDocumentMarkAccess::MarkType::UNO_BOOKMARK:
// no special array for these
break;
}
if (eMode == InsertMode::New
&& (eType == IDocumentMarkAccess::MarkType::TEXT_FIELDMARK
|| eType == IDocumentMarkAccess::MarkType::DATE_FIELDMARK))
{
// due to sw::InsertText notifications everything is visible now - tell
// layout to hide as appropriate
// note: we don't know how many layouts there are and which
// parts they hide, so just notify the entire fieldmark, it
// should give the right result if not in the most efficient way
// note2: can't be done in InitDoc() because it requires the mark
// to be inserted in the vectors.
SwPaM const tmp(pMark->GetMarkPos(), pMark->GetOtherMarkPos());
sw::UpdateFramesForAddDeleteRedline(m_rDoc, tmp);
}
SAL_INFO("sw.core", "--- makeType ---");
SAL_INFO("sw.core", "Marks");
lcl_DebugMarks(m_vAllMarks);
SAL_INFO("sw.core", "Bookmarks");
lcl_DebugMarks(m_vBookmarks);
SAL_INFO("sw.core", "Fieldmarks");
lcl_DebugMarks(m_vFieldmarks);
return pMark.release();
}
::sw::mark::Fieldmark* MarkManager::makeFieldBookmark(
const SwPaM& rPaM,
const SwMarkName& rName,
const OUString& rType,
SwPosition const*const pSepPos)
{
// Disable undo, because we handle it using SwUndoInsTextFieldmark
bool bUndoIsEnabled = m_rDoc.GetIDocumentUndoRedo().DoesUndo();
m_rDoc.GetIDocumentUndoRedo().DoUndo(false);
sw::mark::MarkBase* pMark = nullptr;
if(rType == ODF_FORMDATE)
{
pMark = makeMark(rPaM, rName,
IDocumentMarkAccess::MarkType::DATE_FIELDMARK,
sw::mark::InsertMode::New,
pSepPos);
}
else
{
pMark = makeMark(rPaM, rName,
IDocumentMarkAccess::MarkType::TEXT_FIELDMARK,
sw::mark::InsertMode::New,
pSepPos);
}
sw::mark::Fieldmark* pFieldMark = dynamic_cast<sw::mark::Fieldmark*>( pMark );
if (pFieldMark)
pFieldMark->SetFieldname( rType );
if (bUndoIsEnabled)
{
m_rDoc.GetIDocumentUndoRedo().DoUndo(bUndoIsEnabled);
if (pFieldMark)
m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::make_unique<SwUndoInsTextFieldmark>(*pFieldMark));
}
return pFieldMark;
}
::sw::mark::Fieldmark* MarkManager::makeNoTextFieldBookmark(
const SwPaM& rPaM,
const SwMarkName& rName,
const OUString& rType)
{
// Disable undo, because we handle it using SwUndoInsNoTextFieldmark
bool bUndoIsEnabled = m_rDoc.GetIDocumentUndoRedo().DoesUndo();
m_rDoc.GetIDocumentUndoRedo().DoUndo(false);
bool bEnableSetModified = m_rDoc.getIDocumentState().IsEnableSetModified();
m_rDoc.getIDocumentState().SetEnableSetModified(false);
sw::mark::MarkBase* pMark = nullptr;
if(rType == ODF_FORMCHECKBOX)
{
pMark = makeMark( rPaM, rName,
IDocumentMarkAccess::MarkType::CHECKBOX_FIELDMARK,
sw::mark::InsertMode::New);
}
else if(rType == ODF_FORMDROPDOWN)
{
pMark = makeMark( rPaM, rName,
IDocumentMarkAccess::MarkType::DROPDOWN_FIELDMARK,
sw::mark::InsertMode::New);
}
else if(rType == ODF_FORMDATE)
{
pMark = makeMark( rPaM, rName,
IDocumentMarkAccess::MarkType::DATE_FIELDMARK,
sw::mark::InsertMode::New);
}
sw::mark::Fieldmark* pFieldMark = dynamic_cast<sw::mark::Fieldmark*>( pMark );
if (pFieldMark)
pFieldMark->SetFieldname( rType );
if (bUndoIsEnabled)
{
m_rDoc.GetIDocumentUndoRedo().DoUndo(bUndoIsEnabled);
if (pFieldMark)
m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::make_unique<SwUndoInsNoTextFieldmark>(*pFieldMark));
}
m_rDoc.getIDocumentState().SetEnableSetModified(bEnableSetModified);
m_rDoc.getIDocumentState().SetModified();
return pFieldMark;
}
::sw::mark::MarkBase* MarkManager::getMarkForTextNode(
const SwTextNode& rTextNode,
const IDocumentMarkAccess::MarkType eType )
{
SwPosition aPos(rTextNode);
auto const ppExistingMark = lcl_FindMarkAtPos(m_vBookmarks, aPos, eType);
if(ppExistingMark != m_vBookmarks.end())
return *ppExistingMark;
const SwPaM aPaM(aPos);
return makeMark(aPaM, SwMarkName(), eType, sw::mark::InsertMode::New);
}
sw::mark::MarkBase* MarkManager::makeAnnotationMark(
const SwPaM& rPaM,
const SwMarkName& rName )
{
return makeMark(rPaM, rName, IDocumentMarkAccess::MarkType::ANNOTATIONMARK,
sw::mark::InsertMode::New);
}
void MarkManager::repositionMark(
::sw::mark::MarkBase* const io_pMark,
const SwPaM& rPaM)
{
assert(&io_pMark->GetMarkPos().GetDoc() == &m_rDoc &&
"<MarkManager::repositionMark(..)>"
" - Mark is not in my doc.");
MarkBase* const pMarkBase = io_pMark;
if (!pMarkBase)
return;
pMarkBase->InvalidateFrames();
pMarkBase->SetMarkPos(*(rPaM.GetPoint()));
if(rPaM.HasMark())
pMarkBase->SetOtherMarkPos(*(rPaM.GetMark()));
else
pMarkBase->ClearOtherMarkPos();
if(pMarkBase->GetMarkPos() != pMarkBase->GetMarkStart())
pMarkBase->Swap();
pMarkBase->InvalidateFrames();
sortMarks();
}
bool MarkManager::renameMark(
::sw::mark::MarkBase* io_pMark,
const SwMarkName& rNewName )
{
assert(&io_pMark->GetMarkPos().GetDoc() == &m_rDoc &&
"<MarkManager::renameMark(..)>"
" - Mark is not in my doc.");
if ( io_pMark->GetName() == rNewName )
return true;
if (lcl_FindMarkByName(rNewName, m_vAllMarks) != m_vAllMarks.end())
return false;
const SwMarkName sOldName(io_pMark->GetName());
io_pMark->SetName(rNewName);
if (dynamic_cast< ::sw::mark::Bookmark* >(io_pMark))
{
if (m_rDoc.GetIDocumentUndoRedo().DoesUndo())
{
m_rDoc.GetIDocumentUndoRedo().AppendUndo(
std::make_unique<SwUndoRenameBookmark>(sOldName, rNewName, m_rDoc));
}
m_rDoc.getIDocumentState().SetModified();
}
return true;
}
void MarkManager::correctMarksAbsolute(
const SwNode& rOldNode,
const SwPosition& rNewPos,
const sal_Int32 nOffset)
{
const SwNode* const pOldNode = &rOldNode;
SwPosition aNewPos(rNewPos);
aNewPos.AdjustContent(nOffset);
bool isSortingNeeded = false;
for (auto ppMark = m_vAllMarks.begin();
ppMark != m_vAllMarks.end();
++ppMark)
{
::sw::mark::MarkBase *const pMark = *ppMark;
// correction of non-existent non-MarkBase instances cannot be done
assert(pMark);
// is on position ??
bool bChangedPos = false;
if(&pMark->GetMarkPos().GetNode() == pOldNode)
{
pMark->SetMarkPos(aNewPos);
bChangedPos = true;
isSortingNeeded = true;
}
bool bChangedOPos = false;
if (pMark->IsExpanded() &&
&pMark->GetOtherMarkPos().GetNode() == pOldNode)
{
// shift the OtherMark to aNewPos
pMark->SetOtherMarkPos(aNewPos);
bChangedOPos= true;
isSortingNeeded = true;
}
// illegal selection? collapse the mark and restore sorting later
isSortingNeeded |= lcl_FixCorrectedMark(bChangedPos, bChangedOPos, pMark);
}
// restore sorting if needed
if(isSortingNeeded)
sortMarks();
SAL_INFO("sw.core", "correctMarksAbsolute");
lcl_DebugMarks(m_vAllMarks);
}
void MarkManager::correctMarksRelative(const SwNode& rOldNode, const SwPosition& rNewPos, const sal_Int32 nOffset)
{
const SwNode* const pOldNode = &rOldNode;
SwPosition aNewPos(rNewPos);
aNewPos.AdjustContent(nOffset);
bool isSortingNeeded = false;
for (auto ppMark = m_vAllMarks.begin();
ppMark != m_vAllMarks.end();
++ppMark)
{
// is on position ??
bool bChangedPos = false, bChangedOPos = false;
::sw::mark::MarkBase* const pMark = *ppMark;
// correction of non-existent non-MarkBase instances cannot be done
assert(pMark);
if(&pMark->GetMarkPos().GetNode() == pOldNode)
{
SwPosition aNewPosRel(aNewPos);
if (dynamic_cast< ::sw::mark::CrossRefBookmark *>(pMark))
{
// ensure that cross ref bookmark always starts at 0
aNewPosRel.SetContent(0); // HACK for WW8 import
isSortingNeeded = true; // and sort them to be safe...
}
aNewPosRel.AdjustContent(pMark->GetMarkPos().GetContentIndex());
pMark->SetMarkPos(aNewPosRel);
bChangedPos = true;
}
if(pMark->IsExpanded() &&
&pMark->GetOtherMarkPos().GetNode() == pOldNode)
{
SwPosition aNewPosRel(aNewPos);
aNewPosRel.AdjustContent(pMark->GetOtherMarkPos().GetContentIndex());
pMark->SetOtherMarkPos(aNewPosRel);
bChangedOPos = true;
}
// illegal selection? collapse the mark and restore sorting later
isSortingNeeded |= lcl_FixCorrectedMark(bChangedPos, bChangedOPos, pMark);
}
// restore sorting if needed
if(isSortingNeeded)
sortMarks();
SAL_INFO("sw.core", "correctMarksRelative");
lcl_DebugMarks(m_vAllMarks);
}
static bool isDeleteMark(
::sw::mark::MarkBase const*const pMark,
bool const isReplace,
SwNode const& rStt,
SwNode const& rEnd,
std::optional<sal_Int32> oStartContentIdx,
std::optional<sal_Int32> oEndContentIdx,
bool & rbIsPosInRange,
bool & rbIsOtherPosInRange)
{
assert(pMark);
// navigator marks should not be moved
// TODO: Check if this might make them invalid
if (IsNavigatorReminder(*pMark))
{
return false;
}
// on position ??
rbIsPosInRange = lcl_GreaterThan(pMark->GetMarkPos(), rStt, oStartContentIdx)
&& lcl_Lower(pMark->GetMarkPos(), rEnd, oEndContentIdx);
rbIsOtherPosInRange = pMark->IsExpanded()
&& lcl_GreaterThan(pMark->GetOtherMarkPos(), rStt, oStartContentIdx)
&& lcl_Lower(pMark->GetOtherMarkPos(), rEnd, oEndContentIdx);
// special case: completely in range, touching the end?
if ( oEndContentIdx.has_value()
&& !(isReplace && IDocumentMarkAccess::GetType(*pMark)
== IDocumentMarkAccess::MarkType::BOOKMARK)
&& ( ( rbIsOtherPosInRange
&& pMark->GetMarkPos().GetNode() == rEnd
&& pMark->GetMarkPos().GetContentIndex() == *oEndContentIdx )
|| ( rbIsPosInRange
&& pMark->IsExpanded()
&& pMark->GetOtherMarkPos().GetNode() == rEnd
&& pMark->GetOtherMarkPos().GetContentIndex() == *oEndContentIdx ) ) )
{
rbIsPosInRange = true;
rbIsOtherPosInRange = true;
}
if (rbIsPosInRange
&& (rbIsOtherPosInRange
|| !pMark->IsExpanded()))
{
// completely in range
bool bDeleteMark = true;
{
switch ( IDocumentMarkAccess::GetType( *pMark ) )
{
case IDocumentMarkAccess::MarkType::CROSSREF_HEADING_BOOKMARK:
case IDocumentMarkAccess::MarkType::CROSSREF_NUMITEM_BOOKMARK:
// no delete of cross-reference bookmarks, if range is inside one paragraph
bDeleteMark = &rStt != &rEnd;
break;
case IDocumentMarkAccess::MarkType::UNO_BOOKMARK:
// no delete of UNO mark, if it is not expanded and only touches the start of the range
bDeleteMark = rbIsOtherPosInRange
|| pMark->IsExpanded()
|| !oStartContentIdx.has_value()
|| pMark->GetMarkPos().GetNode() != rStt
|| pMark->GetMarkPos().GetContentIndex() != *oStartContentIdx;
break;
default:
break;
}
}
return bDeleteMark;
}
return false;
}
bool MarkManager::isBookmarkDeleted(SwPaM const& rPaM, bool const isReplace) const
{
SwPosition const& rStart(*rPaM.Start());
SwPosition const& rEnd(*rPaM.End());
for (auto ppMark = m_vBookmarks.begin();
ppMark != m_vBookmarks.end();
++ppMark)
{
bool bIsPosInRange(false);
bool bIsOtherPosInRange(false);
bool const bDeleteMark = isDeleteMark(*ppMark, isReplace,
rStart.GetNode(), rEnd.GetNode(), rStart.GetContentIndex(), rEnd.GetContentIndex(),
bIsPosInRange, bIsOtherPosInRange);
if (bDeleteMark
&& IDocumentMarkAccess::GetType(**ppMark) == MarkType::BOOKMARK)
{
return true;
}
}
return false;
}
void MarkManager::deleteMarks(
const SwNode& rStt,
const SwNode& rEnd,
std::vector<SaveBookmark>* pSaveBkmk,
std::optional<sal_Int32> oStartContentIdx,
std::optional<sal_Int32> oEndContentIdx,
bool const isReplace)
{
std::vector<const_iterator> vMarksToDelete;
bool bIsSortingNeeded = false;
// boolean indicating, if at least one mark has been moved while collecting marks for deletion
bool bMarksMoved = false;
// have marks in the range been skipped instead of deleted
bool bMarksSkipDeletion = false;
// copy all bookmarks in the move area to a vector storing all position data as offset
// reassignment is performed after the move
for (auto ppMark = m_vAllMarks.begin();
ppMark != m_vAllMarks.end();
++ppMark)
{
::sw::mark::MarkBase *const pMark = *ppMark;
bool bIsPosInRange(false);
bool bIsOtherPosInRange(false);
bool const bDeleteMark = isDeleteMark(pMark, isReplace, rStt, rEnd,
oStartContentIdx, oEndContentIdx, bIsPosInRange, bIsOtherPosInRange);
if ( bIsPosInRange
&& ( bIsOtherPosInRange
|| !pMark->IsExpanded() ) )
{
if ( bDeleteMark )
{
if ( pSaveBkmk )
{
pSaveBkmk->push_back( SaveBookmark( *pMark, rStt, oStartContentIdx ) );
}
vMarksToDelete.emplace_back(ppMark);
}
else
{
bMarksSkipDeletion = true;
}
}
else if ( bIsPosInRange != bIsOtherPosInRange )
{
// the bookmark is partially in the range
// move position of that is in the range out of it
std::optional< SwPosition > oNewPos;
if ( oEndContentIdx )
{
oNewPos.emplace( *rEnd.GetContentNode(), *oEndContentIdx );
}
else
{
lcl_FindExpelPosition( oNewPos, rStt, rEnd, bIsPosInRange ? pMark->GetOtherMarkPos() : pMark->GetMarkPos() );
}
bool bMoveMark = true;
{
switch ( IDocumentMarkAccess::GetType( *pMark ) )
{
case IDocumentMarkAccess::MarkType::CROSSREF_HEADING_BOOKMARK:
case IDocumentMarkAccess::MarkType::CROSSREF_NUMITEM_BOOKMARK:
// no move of cross-reference bookmarks, if move occurs inside a certain node
bMoveMark = pMark->GetMarkPos().GetNode() != oNewPos->GetNode();
break;
case IDocumentMarkAccess::MarkType::ANNOTATIONMARK:
// no move of annotation marks, if method is called to collect deleted marks
bMoveMark = pSaveBkmk == nullptr;
break;
default:
bMoveMark = true;
break;
}
}
if ( bMoveMark )
{
if ( bIsPosInRange )
pMark->SetMarkPos(*oNewPos);
else
pMark->SetOtherMarkPos(*oNewPos);
bMarksMoved = true;
// illegal selection? collapse the mark and restore sorting later
bIsSortingNeeded |= lcl_FixCorrectedMark( bIsPosInRange, bIsOtherPosInRange, pMark );
}
}
}
{
// fdo#61016 delay the deletion of the fieldmark characters
// to prevent that from deleting the marks on that position
// which would invalidate the iterators in vMarksToDelete
std::vector< std::unique_ptr<ILazyDeleter> > vDelay;
vDelay.reserve(vMarksToDelete.size());
// If needed, sort mark containers containing subsets of the marks
// in order to assure sorting. The sorting is critical for the
// deletion of a mark as it is searched in these container for
// deletion.
if ( !vMarksToDelete.empty() && bMarksMoved )
{
sortSubsetMarks();
}
// we just remembered the iterators to delete, so we do not need to search
// for the shared_ptr<> (the entry in m_vAllMarks) again
// reverse iteration, since erasing an entry invalidates iterators
// behind it (the iterators in vMarksToDelete are sorted)
for ( std::vector< const_iterator >::reverse_iterator pppMark = vMarksToDelete.rbegin();
pppMark != vMarksToDelete.rend();
++pppMark )
{
vDelay.push_back(deleteMark(*pppMark, pSaveBkmk != nullptr));
}
} // scope to kill vDelay
// also need to sort if both marks were moved and not-deleted because
// the not-deleted marks could be in wrong order vs. the moved ones
if (bIsSortingNeeded || (bMarksMoved && bMarksSkipDeletion))
{
sortMarks();
}
SAL_INFO("sw.core", "deleteMarks");
lcl_DebugMarks(m_vAllMarks);
}
namespace {
struct LazyFieldmarkDeleter : public IDocumentMarkAccess::ILazyDeleter
{
std::unique_ptr<Fieldmark> m_pFieldmark;
SwDoc& m_rDoc;
bool const m_isMoveNodes;
LazyFieldmarkDeleter(Fieldmark *const pMark, SwDoc& rDoc, bool const isMoveNodes)
: m_pFieldmark(pMark), m_rDoc(rDoc), m_isMoveNodes(isMoveNodes)
{
assert(m_pFieldmark);
}
virtual ~LazyFieldmarkDeleter() override
{
// note: because of the call chain from SwUndoDelete, the field
// command *cannot* be deleted here as it would create a separate
// SwUndoDelete that's interleaved with the SwHistory of the outer
// one - only delete the CH_TXT_ATR_FIELD*!
if (!m_isMoveNodes)
{
m_pFieldmark->ReleaseDoc(m_rDoc);
}
}
};
// Call DeregisterFromDoc() lazily, because it can call selection change listeners, which
// may mutate the marks container
struct LazyDdeBookmarkDeleter : public IDocumentMarkAccess::ILazyDeleter
{
std::unique_ptr<DdeBookmark> m_pDdeBookmark;
SwDoc& m_rDoc;
LazyDdeBookmarkDeleter(DdeBookmark *const pDdeBookmark, SwDoc& rDoc)
: m_pDdeBookmark(pDdeBookmark), m_rDoc(rDoc)
{
assert(pDdeBookmark);
}
virtual ~LazyDdeBookmarkDeleter() override
{
m_pDdeBookmark->DeregisterFromDoc(m_rDoc);
}
};
}
std::unique_ptr<IDocumentMarkAccess::ILazyDeleter>
MarkManager::deleteMark(const const_iterator& ppMark, bool const isMoveNodes)
{
std::unique_ptr<ILazyDeleter> ret;
if (ppMark == m_vAllMarks.end())
return ret;
MarkBase* pMark = *ppMark;
switch(IDocumentMarkAccess::GetType(*pMark))
{
case IDocumentMarkAccess::MarkType::BOOKMARK:
{
auto const ppBookmark = lcl_FindMark(m_vBookmarks, static_cast<sw::mark::Bookmark*>(pMark));
if ( ppBookmark != m_vBookmarks.end() )
{
Bookmark* pBookmark = *ppBookmark;
if(pBookmark)
pBookmark->sendLOKDeleteCallback();
m_vBookmarks.erase(ppBookmark);
}
else
{
assert(false &&
"<MarkManager::deleteMark(..)> - Bookmark not found in Bookmark container.");
}
}
break;
case IDocumentMarkAccess::MarkType::CROSSREF_HEADING_BOOKMARK:
case IDocumentMarkAccess::MarkType::CROSSREF_NUMITEM_BOOKMARK:
{
auto const ppBookmark = lcl_FindMark(m_vBookmarks, static_cast<Bookmark*>(pMark));
if ( ppBookmark != m_vBookmarks.end() )
{
m_vBookmarks.erase(ppBookmark);
}
else
{
assert(false &&
"<MarkManager::deleteMark(..)> - Bookmark not found in Bookmark container.");
}
}
break;
case IDocumentMarkAccess::MarkType::TEXT_FIELDMARK:
case IDocumentMarkAccess::MarkType::CHECKBOX_FIELDMARK:
case IDocumentMarkAccess::MarkType::DROPDOWN_FIELDMARK:
case IDocumentMarkAccess::MarkType::DATE_FIELDMARK:
{
auto const ppFieldmark = lcl_FindMark(m_vFieldmarks, static_cast<Fieldmark*>(pMark));
if ( ppFieldmark != m_vFieldmarks.end() )
{
if(m_pLastActiveFieldmark == *ppFieldmark)
ClearFieldActivation();
m_vFieldmarks.erase(ppFieldmark);
ret.reset(new LazyFieldmarkDeleter(static_cast<Fieldmark*>(pMark), m_rDoc, isMoveNodes));
pMark = nullptr;
}
else
{
assert(false &&
"<MarkManager::deleteMark(..)> - Fieldmark not found in Fieldmark container.");
}
}
break;
case IDocumentMarkAccess::MarkType::ANNOTATIONMARK:
{
auto const ppAnnotationMark = lcl_FindMark(m_vAnnotationMarks, static_cast<AnnotationMark*>(pMark));
assert(ppAnnotationMark != m_vAnnotationMarks.end() &&
"<MarkManager::deleteMark(..)> - Annotation Mark not found in Annotation Mark container.");
m_vAnnotationMarks.erase(ppAnnotationMark);
}
break;
case IDocumentMarkAccess::MarkType::DDE_BOOKMARK:
case IDocumentMarkAccess::MarkType::NAVIGATOR_REMINDER:
case IDocumentMarkAccess::MarkType::UNO_BOOKMARK:
// no special marks container
break;
}
//Effective STL Item 27, get a non-const iterator aI at the same
//position as const iterator ppMark was
auto aI = m_vAllMarks.begin();
std::advance(aI, std::distance<container_t::const_iterator>(aI, ppMark));
DdeBookmark* const pDdeBookmark = dynamic_cast<DdeBookmark*>(pMark);
if (pDdeBookmark)
{
ret.reset(new LazyDdeBookmarkDeleter(pDdeBookmark, m_rDoc));
pMark = nullptr;
}
m_vAllMarks.erase(aI);
// delete after we remove from the list, because the destructor can
// recursively call into this method.
delete pMark; // If we have a lazy deleter, pMark was null-ed
return ret;
}
void MarkManager::deleteMark(const MarkBase* const pMark)
{
assert(&pMark->GetMarkPos().GetDoc() == &m_rDoc &&
"<MarkManager::deleteMark(..)>"
" - Mark is not in my doc.");
// finds the last Mark that is starting before pMark
// (pMarkLow < pMark)
auto [it, endIt] = equal_range(
m_vAllMarks.begin(),
m_vAllMarks.end(),
pMark->GetMarkStart(),
CompareIMarkStartsBefore<MarkBase>());
for ( ; it != endIt; ++it)
if (*it == pMark)
{
deleteMark(it, false);
break;
}
}
void MarkManager::clearAllMarks()
{
ClearFieldActivation();
m_vFieldmarks.clear();
m_vBookmarks.clear();
m_vAnnotationMarks.clear();
for (const auto & p : m_vAllMarks)
delete p;
m_vAllMarks.clear();
}
IDocumentMarkAccess::const_iterator MarkManager::findMark(const SwMarkName& rName) const
{
return lcl_FindMarkByName(rName, m_vAllMarks);
}
std::vector<sw::mark::Bookmark*>::const_iterator MarkManager::findBookmark(const SwMarkName& rName) const
{
return lcl_FindMarkByName(rName, m_vBookmarks);
}
// find the first Bookmark that does not start before
std::vector<sw::mark::Bookmark*>::const_iterator MarkManager::findFirstBookmarkNotStartsBefore(const SwPosition& rPos) const
{
return std::lower_bound(
m_vBookmarks.begin(),
m_vBookmarks.end(),
rPos,
CompareIMarkStartsBefore<Bookmark>());
}
IDocumentMarkAccess::const_iterator MarkManager::getAllMarksBegin() const
{ return m_vAllMarks.begin(); }
IDocumentMarkAccess::const_iterator MarkManager::getAllMarksEnd() const
{ return m_vAllMarks.end(); }
sal_Int32 MarkManager::getAllMarksCount() const
{ return m_vAllMarks.size(); }
std::vector<sw::mark::Bookmark*>::const_iterator MarkManager::getBookmarksBegin() const
{ return m_vBookmarks.begin(); }
std::vector<sw::mark::Bookmark*>::const_iterator MarkManager::getBookmarksEnd() const
{ return m_vBookmarks.end(); }
sal_Int32 MarkManager::getBookmarksCount() const
{ return m_vBookmarks.size(); }
std::vector<Fieldmark*>::const_iterator MarkManager::getFieldmarksBegin() const
{ return m_vFieldmarks.begin(); }
std::vector<Fieldmark*>::const_iterator MarkManager::getFieldmarksEnd() const
{ return m_vFieldmarks.end(); }
sal_Int32 MarkManager::getFieldmarksCount() const { return m_vFieldmarks.size(); }
// finds the first that is starting after
std::vector<sw::mark::Bookmark*>::const_iterator MarkManager::findFirstBookmarkStartsAfter(const SwPosition& rPos) const
{
return std::upper_bound(
m_vBookmarks.begin(),
m_vBookmarks.end(),
rPos,
CompareIMarkStartsAfter<sw::mark::Bookmark>());
}
Fieldmark* MarkManager::getFieldmarkAt(const SwPosition& rPos) const
{
auto const pFieldmark = find_if(
m_vFieldmarks.begin(),
m_vFieldmarks.end(),
[&rPos] (::sw::mark::MarkBase const*const pMark) {
auto [/*const SwPosition&*/ rStartPos, rEndPos] = pMark->GetMarkStartEnd();
return rStartPos == rPos
// end position includes the CH_TXT_ATR_FIELDEND
|| (rEndPos.GetContentIndex() == rPos.GetContentIndex() + 1
&& rEndPos.GetNode() == rPos.GetNode());
} );
return (pFieldmark == m_vFieldmarks.end())
? nullptr
: *pFieldmark;
}
Fieldmark* MarkManager::getInnerFieldmarkFor(const SwPosition& rPos) const
{
// find the first mark starting on or before the position in reverse order
// (as we are reverse searching, this is the one closest to the position)
// m_vFieldmarks should be ordered by mark start, so we can bisect with lower_bound
auto itEnd = m_vFieldmarks.rend();
auto itStart = lower_bound(
m_vFieldmarks.rbegin(),
itEnd,
rPos,
CompareIMarkStartsAfterReverse());
// now continue a linear search for the first (still in reverse order) ending behind the position
auto itCurrent = find_if(
itStart,
itEnd,
[&rPos](const sw::mark::MarkBase* const pMark) { return rPos < pMark->GetMarkEnd(); });
// if we reached the end (in reverse order) there is no match
if(itCurrent == itEnd)
return nullptr;
// we found our first candidate covering the position ...
auto pMark = *itCurrent;
auto aMarkStartEndPair = pMark->GetMarkStartEnd();
const SwPosition* pMarkStart = &aMarkStartEndPair.first;
const SwPosition* pMarkEnd = &aMarkStartEndPair.second;
// ... however we still need to check if there is a smaller/'more inner' one with the same start position
for(++itCurrent; itCurrent != itEnd; ++itCurrent)
{
if((*itCurrent)->GetMarkStart() < *pMarkStart)
// any following mark (in reverse order) will have an earlier
// start and thus can not be more 'inner' than our previous
// match, so we are done.
break;
const SwPosition& rCurrentMarkEnd = (*itCurrent)->GetMarkEnd();
if(rPos < rCurrentMarkEnd && rCurrentMarkEnd <= *pMarkEnd)
{
// both covering the position and more inner/smaller => use this one instead
pMark = *itCurrent;
pMarkEnd = &rCurrentMarkEnd;
}
}
return pMark;
}
sw::mark::Bookmark* MarkManager::getOneInnermostBookmarkFor(const SwPosition& rPos) const
{
auto it = std::find_if(m_vBookmarks.begin(), m_vBookmarks.end(),
[&rPos](const sw::mark::Bookmark* pMark)
{ return pMark->IsCoveringPosition(rPos); });
if (it == m_vBookmarks.end())
{
return nullptr;
}
sw::mark::Bookmark* pBookmark = *it;
// See if any bookmarks after the first hit are closer to rPos.
++it;
for (; it != m_vBookmarks.end(); ++it)
{
// Find the innermost bookmark.
auto [/*const SwPosition&*/ rMarkStart, rMarkEnd] = (*it)->GetMarkStartEnd();
if (rMarkStart > rPos)
break;
if (rPos < rMarkEnd
&& (pBookmark->GetMarkStart() < rMarkStart
|| rMarkEnd < pBookmark->GetMarkEnd()))
{
pBookmark = *it;
}
}
return pBookmark;
}
void MarkManager::deleteFieldmarkAt(const SwPosition& rPos)
{
Fieldmark* const pFieldmark = getFieldmarkAt(rPos);
assert(pFieldmark); // currently all callers require it to be there
deleteMark(lcl_FindMark<MarkBase>(m_vAllMarks, pFieldmark), false);
}
::sw::mark::Fieldmark* MarkManager::changeFormFieldmarkType(::sw::mark::Fieldmark* pFieldmark, const OUString& rNewType)
{
bool bActualChange = false;
if(rNewType == ODF_FORMDROPDOWN)
{
if (!dynamic_cast<::sw::mark::DropDownFieldmark*>(pFieldmark))
bActualChange = true;
if (!dynamic_cast<::sw::mark::CheckboxFieldmark*>(pFieldmark)) // only allowed converting between checkbox <-> dropdown
return nullptr;
}
else if(rNewType == ODF_FORMCHECKBOX)
{
if (!dynamic_cast<::sw::mark::CheckboxFieldmark*>(pFieldmark))
bActualChange = true;
if (!dynamic_cast<::sw::mark::DropDownFieldmark*>(pFieldmark)) // only allowed converting between checkbox <-> dropdown
return nullptr;
}
else if(rNewType == ODF_FORMDATE)
{
if (!dynamic_cast<::sw::mark::DateFieldmark*>(pFieldmark))
bActualChange = true;
if (!dynamic_cast<::sw::mark::TextFieldmark*>(pFieldmark)) // only allowed converting between date field <-> text field
return nullptr;
}
if (!bActualChange)
return nullptr;
// Store attributes needed to create the new fieldmark
SwMarkName sName = pFieldmark->GetName();
SwPaM const aPaM(pFieldmark->GetMarkStart());
// Remove the old fieldmark and create a new one with the new type
if (rNewType == ODF_FORMDROPDOWN || rNewType == ODF_FORMCHECKBOX)
{
SwPosition aNewPos (*aPaM.GetPoint());
deleteFieldmarkAt(aNewPos);
return makeNoTextFieldBookmark(aPaM, sName, rNewType);
}
else if(rNewType == ODF_FORMDATE)
{
SwPosition aPos (*aPaM.GetPoint());
SwPaM aNewPaM(pFieldmark->GetMarkStart(), pFieldmark->GetMarkEnd());
deleteFieldmarkAt(aPos);
// HACK: hard-code the separator position here at the start because
// writerfilter put it in the wrong place (at the end) on attach()
SwPosition const sepPos(*aNewPaM.Start());
return makeFieldBookmark(aNewPaM, sName, rNewType, &sepPos);
}
return nullptr;
}
void MarkManager::NotifyCursorUpdate(const SwCursorShell& rCursorShell)
{
SwView* pSwView = dynamic_cast<SwView *>(rCursorShell.GetSfxViewShell());
if(!pSwView)
return;
SwEditWin& rEditWin = pSwView->GetEditWin();
SwPosition aPos(*rCursorShell.GetCursor()->GetPoint());
Fieldmark* pFieldBM = getInnerFieldmarkFor(aPos);
FieldmarkWithDropDownButton* pNewActiveFieldmark = nullptr;
if ((!pFieldBM || (pFieldBM->GetFieldname() != ODF_FORMDROPDOWN && pFieldBM->GetFieldname() != ODF_FORMDATE))
&& aPos.GetContentIndex() > 0 )
{
aPos.AdjustContent(-1);
pFieldBM = getInnerFieldmarkFor(aPos);
}
if ( pFieldBM && (pFieldBM->GetFieldname() == ODF_FORMDROPDOWN ||
pFieldBM->GetFieldname() == ODF_FORMDATE))
{
if (m_pLastActiveFieldmark != pFieldBM)
{
FieldmarkWithDropDownButton& rFormField = dynamic_cast<FieldmarkWithDropDownButton&>(*pFieldBM);
pNewActiveFieldmark = &rFormField;
}
else
{
pNewActiveFieldmark = m_pLastActiveFieldmark;
}
}
if(pNewActiveFieldmark != m_pLastActiveFieldmark)
{
ClearFieldActivation();
m_pLastActiveFieldmark = pNewActiveFieldmark;
if(pNewActiveFieldmark)
pNewActiveFieldmark->ShowButton(&rEditWin);
}
LOKUpdateActiveField(pSwView);
}
void MarkManager::ClearFieldActivation()
{
if(m_pLastActiveFieldmark)
m_pLastActiveFieldmark->RemoveButton();
m_pLastActiveFieldmark = nullptr;
}
void MarkManager::LOKUpdateActiveField(const SfxViewShell* pViewShell)
{
if (!comphelper::LibreOfficeKit::isActive())
return;
if (m_pLastActiveFieldmark)
{
if (auto pDrowDown = m_pLastActiveFieldmark->GetFieldname() == ODF_FORMDROPDOWN ?
dynamic_cast<::sw::mark::DropDownFieldmark*>(m_pLastActiveFieldmark) :
nullptr)
{
pDrowDown->SendLOKShowMessage(pViewShell);
}
}
else
{
// Check whether we have any drop down fieldmark at all.
bool bDropDownFieldExist = false;
for (auto aIter = m_vFieldmarks.begin(); aIter != m_vFieldmarks.end(); ++aIter)
{
Fieldmark *pMark = *aIter;
if (pMark && pMark->GetFieldname() == ODF_FORMDROPDOWN)
{
bDropDownFieldExist = true;
break;
}
}
if (bDropDownFieldExist)
::sw::mark::DropDownFieldmark::SendLOKHideMessage(pViewShell);
}
}
Fieldmark* MarkManager::getDropDownFor(const SwPosition& rPos) const
{
Fieldmark *pMark = getFieldmarkAt(rPos);
if (!pMark || pMark->GetFieldname() != ODF_FORMDROPDOWN)
return nullptr;
return pMark;
}
std::vector<Fieldmark*> MarkManager::getNoTextFieldmarksIn(const SwPaM &rPaM) const
{
std::vector<Fieldmark*> aRet;
for (auto aI = m_vFieldmarks.begin(),
aEnd = m_vFieldmarks.end(); aI != aEnd; ++aI)
{
::sw::mark::MarkBase* pI = *aI;
const SwPosition &rStart = pI->GetMarkPos();
if (!rPaM.ContainsPosition(rStart))
continue;
Fieldmark *pMark = dynamic_cast<Fieldmark*>(pI);
if (!pMark || (pMark->GetFieldname() != ODF_FORMDROPDOWN
&& pMark->GetFieldname() != ODF_FORMCHECKBOX))
{
continue;
}
aRet.push_back(pMark);
}
return aRet;
}
Fieldmark* MarkManager::getFieldmarkAfter(const SwPosition& rPos, bool bLoop) const
{ return lcl_getMarkAfter(m_vFieldmarks, rPos, bLoop); }
Fieldmark* MarkManager::getFieldmarkBefore(const SwPosition& rPos, bool bLoop) const
{ return lcl_getMarkBefore(m_vFieldmarks, rPos, bLoop); }
std::vector<sw::mark::AnnotationMark*>::const_iterator MarkManager::getAnnotationMarksBegin() const
{
return m_vAnnotationMarks.begin();
}
std::vector<sw::mark::AnnotationMark*>::const_iterator MarkManager::getAnnotationMarksEnd() const
{
return m_vAnnotationMarks.end();
}
sal_Int32 MarkManager::getAnnotationMarksCount() const
{
return m_vAnnotationMarks.size();
}
std::vector<sw::mark::AnnotationMark*>::const_iterator MarkManager::findAnnotationMark( const SwMarkName& rName ) const
{
return lcl_FindMarkByName( rName, m_vAnnotationMarks );
}
AnnotationMark* MarkManager::getAnnotationMarkFor(const SwPosition& rPos) const
{
auto const pAnnotationMark = find_if(
m_vAnnotationMarks.begin(),
m_vAnnotationMarks.end(),
[&rPos] (const ::sw::mark::AnnotationMark *const pMark) { return pMark->IsCoveringPosition(rPos); } );
if (pAnnotationMark == m_vAnnotationMarks.end())
return nullptr;
return *pAnnotationMark;
}
// create helper bookmark for annotations on tracked deletions
::sw::mark::Bookmark* MarkManager::makeAnnotationBookmark(const SwPaM& rPaM,
const SwMarkName& rName,
sw::mark::InsertMode const eMode,
SwPosition const*const pSepPos)
{
SwMarkName sAnnotationBookmarkName(rName.toString() + S_ANNOTATION_BOOKMARK);
return static_cast<sw::mark::Bookmark*>(makeMark( rPaM, sAnnotationBookmarkName, MarkType::BOOKMARK, eMode, pSepPos));
}
// find the first AnnotationMark that does not start before
std::vector<sw::mark::AnnotationMark*>::const_iterator MarkManager::findFirstAnnotationMarkNotStartsBefore(const SwPosition& rPos) const
{
return std::lower_bound(
m_vAnnotationMarks.begin(),
m_vAnnotationMarks.end(),
rPos,
CompareIMarkStartsBefore<AnnotationMark>());
}
// find the first AnnotationMark that does not start before
std::vector<sw::mark::AnnotationMark*>::const_iterator MarkManager::findFirstAnnotationMarkNotStartsBefore(const SwNode& rPos) const
{
return std::lower_bound(
m_vAnnotationMarks.begin(),
m_vAnnotationMarks.end(),
rPos,
CompareIMarkStartsBefore<AnnotationMark>());
}
// find helper bookmark of annotations on tracked deletions
std::vector<sw::mark::Bookmark*>::const_iterator MarkManager::findAnnotationBookmark(const SwMarkName& rName) const
{
SwMarkName sAnnotationBookmarkName(rName.toString() + S_ANNOTATION_BOOKMARK);
return findBookmark(sAnnotationBookmarkName);
}
// restore text ranges of annotations on tracked deletions
// based on the helper bookmarks (which can survive I/O and hiding redlines)
void MarkManager::restoreAnnotationMarks(bool bDelete)
{
for (auto iter = getBookmarksBegin();
iter != getBookmarksEnd(); )
{
const SwMarkName & rBookmarkName = (**iter).GetName();
sal_Int32 nPos;
if ( rBookmarkName.toString().startsWith("__Annotation__") &&
(nPos = rBookmarkName.toString().indexOf(S_ANNOTATION_BOOKMARK)) > -1 )
{
::sw::UndoGuard const undoGuard(m_rDoc.GetIDocumentUndoRedo());
auto pMark = findAnnotationMark(SwMarkName(rBookmarkName.toString().copy(0, nPos)));
if ( pMark != getAnnotationMarksEnd() )
{
const SwPaM aPam((**iter).GetMarkStart(), (**pMark).GetMarkEnd());
repositionMark(*pMark, aPam);
}
if (bDelete)
{
deleteMark(&**iter);
// this invalidates iter, have to start over...
iter = getBookmarksBegin();
}
else
++iter;
}
else
++iter;
}
}
template <class IsNameUniqueFunc>
requires std::is_invocable_r_v<bool, IsNameUniqueFunc, const SwMarkName&>
SwMarkName MarkManager::getUniqueMarkName(const SwMarkName& rName, IsNameUniqueFunc f) const
{
OSL_ENSURE(rName.toString().getLength(),
"<MarkManager::getUniqueMarkName(..)> - a name should be proposed");
if( m_rDoc.IsInMailMerge())
{
OUString newName = rName.toString() + "MailMergeMark"
+ DateTimeToOUString( DateTime( DateTime::SYSTEM ) )
+ OUString::number( m_vAllMarks.size() + 1 );
return SwMarkName(newName);
}
if (f(rName))
{
return rName;
}
OUString sTmp;
// try the name "<rName>XXX" (where XXX is a number starting from 1) unless there is
// an unused name. Due to performance-reasons (especially in mailmerge-scenarios) there
// is a map m_aMarkBasenameMapUniqueOffset which holds the next possible offset (XXX) for
// rName (so there is no need to test for nCnt-values smaller than the offset).
sal_Int32 nCnt = 1;
MarkBasenameMapUniqueOffset_t::const_iterator aIter = m_aMarkBasenameMapUniqueOffset.find(rName);
if(aIter != m_aMarkBasenameMapUniqueOffset.end()) nCnt = aIter->second;
OUString aPrefix = SwResId(STR_MARK_COPY).replaceFirst("%1", rName.toString());
while(nCnt < SAL_MAX_INT32)
{
sTmp = aPrefix + OUString::number(nCnt);
nCnt++;
if (f(SwMarkName(sTmp)))
{
break;
}
}
m_aMarkBasenameMapUniqueOffset[rName] = nCnt;
return SwMarkName(sTmp);
}
void MarkManager::assureSortedMarkContainers() const
{
const_cast< MarkManager* >(this)->sortMarks();
}
void MarkManager::sortMarks()
{
sort(m_vAllMarks.begin(), m_vAllMarks.end(), &lcl_MarkOrderingByStart<MarkBase>);
sortSubsetMarks();
}
void MarkManager::sortSubsetMarks()
{
stable_sort(m_vBookmarks.begin(), m_vBookmarks.end(), &lcl_MarkOrderingByStart<MarkBase>);
sort(m_vFieldmarks.begin(), m_vFieldmarks.end(), &lcl_MarkOrderingByStart<Fieldmark>);
sort(m_vAnnotationMarks.begin(), m_vAnnotationMarks.end(), &lcl_MarkOrderingByStart<MarkBase>);
}
template<class MarkT>
static void lcl_assureSortedMarkContainers(typename std::vector<MarkT*>& rContainer,
sal_Int32 nMinIndexModified)
{
// We know that the range nMinIndexModified.. has been modified, now we need to extend that range
// to find the total range of elements that need to be sorted.
// We know that the marks have been modified in fairly limited ways, see ContentIdxStoreImpl.
sal_Int32 nMin = nMinIndexModified;
while (nMin != 0)
{
nMin--;
if (rContainer[nMin]->GetMarkStart() < rContainer[nMinIndexModified]->GetMarkStart())
break;
}
sort(rContainer.begin() + nMin, rContainer.end(), &lcl_MarkOrderingByStart<MarkT>);
}
template<class MarkT>
static void lcl_assureSortedMarkSubContainers(typename std::vector<MarkT*>& rContainer,
MarkT* pFound)
{
if (pFound)
{
auto it = std::find(rContainer.rbegin(), rContainer.rend(), pFound);
sal_Int32 nFirstModified = std::distance(rContainer.begin(), (it+1).base());
lcl_assureSortedMarkContainers<MarkT>(rContainer, nFirstModified);
}
}
/**
* called when we need to sort a sub-range of the container, elements starting
* at nMinIndexModified were modified. This is used from ContentIdxStoreImpl::RestoreBkmks,
* where we are only modifying a small range at the end of the container.
*/
void MarkManager::assureSortedMarkContainers(sal_Int32 nMinIndexModified) const
{
// check if the modified range contains elements from the other sorted containers
Bookmark* pBookmark = nullptr;
Fieldmark* pFieldmark = nullptr;
AnnotationMark* pAnnotationMark = nullptr;
for (auto it = m_vAllMarks.begin() + nMinIndexModified; it != m_vAllMarks.end(); ++it)
{
switch(IDocumentMarkAccess::GetType(**it))
{
case IDocumentMarkAccess::MarkType::BOOKMARK:
case IDocumentMarkAccess::MarkType::CROSSREF_HEADING_BOOKMARK:
case IDocumentMarkAccess::MarkType::CROSSREF_NUMITEM_BOOKMARK:
if (!pBookmark)
pBookmark = static_cast<Bookmark*>(*it);
break;
case IDocumentMarkAccess::MarkType::TEXT_FIELDMARK:
case IDocumentMarkAccess::MarkType::CHECKBOX_FIELDMARK:
case IDocumentMarkAccess::MarkType::DROPDOWN_FIELDMARK:
case IDocumentMarkAccess::MarkType::DATE_FIELDMARK:
if (!pFieldmark)
pFieldmark = static_cast<Fieldmark*>(*it);
break;
case IDocumentMarkAccess::MarkType::ANNOTATIONMARK:
if (!pAnnotationMark)
pAnnotationMark = static_cast<AnnotationMark*>(*it);
break;
case IDocumentMarkAccess::MarkType::DDE_BOOKMARK:
case IDocumentMarkAccess::MarkType::NAVIGATOR_REMINDER:
case IDocumentMarkAccess::MarkType::UNO_BOOKMARK:
// no special marks container
break;
}
}
auto pThis = const_cast<MarkManager*>(this);
lcl_assureSortedMarkContainers<MarkBase>(pThis->m_vAllMarks, nMinIndexModified);
lcl_assureSortedMarkSubContainers<Bookmark>(pThis->m_vBookmarks, pBookmark);
lcl_assureSortedMarkSubContainers<Fieldmark>(pThis->m_vFieldmarks, pFieldmark);
lcl_assureSortedMarkSubContainers<AnnotationMark>(pThis->m_vAnnotationMarks, pAnnotationMark);
}
template<class MarkT>
static void dumpContainerAsXml(xmlTextWriterPtr pWriter,
const std::vector<MarkT*>& rContainer,
const char* pName)
{
if (!rContainer.empty())
{
(void)xmlTextWriterStartElement(pWriter, BAD_CAST(pName));
for (auto it = rContainer.begin(); it != rContainer.end(); ++it)
(*it)->dumpAsXml(pWriter);
(void)xmlTextWriterEndElement(pWriter);
}
}
void MarkManager::dumpAsXml(xmlTextWriterPtr pWriter) const
{
(void)xmlTextWriterStartElement(pWriter, BAD_CAST("MarkManager"));
dumpContainerAsXml(pWriter, m_vAllMarks, "allmarks");
dumpContainerAsXml(pWriter, m_vBookmarks, "bookmarks");
dumpContainerAsXml(pWriter, m_vFieldmarks, "fieldmarks");
dumpContainerAsXml(pWriter, m_vAnnotationMarks, "annotationmarks");
(void)xmlTextWriterEndElement(pWriter);
}
} // namespace ::sw::mark
namespace
{
bool lcl_Greater( const SwPosition& rPos, const SwNode& rNdIdx, std::optional<sal_Int32> oContentIdx )
{
return rPos.GetNode() > rNdIdx ||
( oContentIdx && rPos.GetNode() == rNdIdx && rPos.GetContentIndex() > *oContentIdx );
}
}
MarkManager& SwDoc::GetMarkManager()
{
return *mpMarkManager;
}
// IDocumentMarkAccess for SwDoc
IDocumentMarkAccess* SwDoc::getIDocumentMarkAccess()
{ return static_cast< IDocumentMarkAccess* >(mpMarkManager.get()); }
const IDocumentMarkAccess* SwDoc::getIDocumentMarkAccess() const
{ return static_cast< IDocumentMarkAccess* >(mpMarkManager.get()); }
SaveBookmark::SaveBookmark(
const MarkBase& rBkmk,
const SwNode& rMvPos,
std::optional<sal_Int32> oContentIdx)
: m_aName(rBkmk.GetName())
, m_bHidden(false)
, m_eOrigBkmType(IDocumentMarkAccess::GetType(rBkmk))
{
const Bookmark* const pBookmark = dynamic_cast< const Bookmark* >(&rBkmk);
if(pBookmark)
{
m_aShortName = pBookmark->GetShortName();
m_aCode = pBookmark->GetKeyCode();
m_bHidden = pBookmark->IsHidden();
m_aHideCondition = pBookmark->GetHideCondition();
m_pMetadataUndo = pBookmark->CreateUndo();
}
m_nNode1 = rBkmk.GetMarkPos().GetNodeIndex();
m_nContent1 = rBkmk.GetMarkPos().GetContentIndex();
m_nNode1 -= rMvPos.GetIndex();
if(oContentIdx && !m_nNode1)
m_nContent1 -= *oContentIdx;
if(rBkmk.IsExpanded())
{
m_nNode2 = rBkmk.GetOtherMarkPos().GetNodeIndex();
m_nContent2 = rBkmk.GetOtherMarkPos().GetContentIndex();
m_nNode2 -= rMvPos.GetIndex();
if(oContentIdx && !m_nNode2)
m_nContent2 -= *oContentIdx;
}
else
{
m_nNode2 = NODE_OFFSET_MAX;
m_nContent2 = -1;
}
}
void SaveBookmark::SetInDoc(
SwDoc* pDoc,
const SwNode& rNewPos,
std::optional<sal_Int32> oContentIdx)
{
SwPaM aPam(rNewPos);
if(oContentIdx)
{
if (aPam.GetPoint()->GetNode().IsContentNode())
aPam.GetPoint()->SetContent( *oContentIdx );
else
SAL_WARN("sw", "trying to sent content index, but point node is not a content node");
}
if(NODE_OFFSET_MAX != m_nNode2)
{
aPam.SetMark();
aPam.GetMark()->Adjust(m_nNode2);
if (aPam.GetMark()->GetNode().IsContentNode())
{
if(oContentIdx && !m_nNode2)
aPam.GetMark()->SetContent(*oContentIdx + m_nContent2);
else
aPam.GetMark()->SetContent(m_nContent2);
}
else
SAL_WARN("sw", "trying to sent content index, but mark node is not a content node");
}
aPam.GetPoint()->Adjust(m_nNode1);
if (aPam.GetPoint()->GetNode().IsContentNode())
{
if(oContentIdx && !m_nNode1)
aPam.GetPoint()->SetContent(*oContentIdx + m_nContent1);
else
aPam.GetPoint()->SetContent(m_nContent1);
}
if(aPam.HasMark()
&& !CheckNodesRange(aPam.GetPoint()->GetNode(), aPam.GetMark()->GetNode(), true))
return;
::sw::mark::Bookmark* const pBookmark = dynamic_cast<::sw::mark::Bookmark*>(
pDoc->getIDocumentMarkAccess()->makeMark(aPam, m_aName,
m_eOrigBkmType, sw::mark::InsertMode::CopyText));
if(!pBookmark)
return;
pBookmark->SetKeyCode(m_aCode);
pBookmark->SetShortName(m_aShortName);
pBookmark->Hide(m_bHidden);
pBookmark->SetHideCondition(m_aHideCondition);
if (m_pMetadataUndo)
pBookmark->RestoreMetadata(m_pMetadataUndo);
}
// DelBookmarks
void DelBookmarks(
SwNode& rStt,
const SwNode& rEnd,
std::vector<SaveBookmark> * pSaveBkmk,
std::optional<sal_Int32> oStartContentIdx,
std::optional<sal_Int32> oEndContentIdx,
bool const isReplace)
{
// illegal range ??
if(rStt.GetIndex() > rEnd.GetIndex()
|| (&rStt == &rEnd && (!oStartContentIdx || !oEndContentIdx || *oStartContentIdx >= *oEndContentIdx)))
return;
SwDoc& rDoc = rStt.GetDoc();
rDoc.getIDocumentMarkAccess()->deleteMarks(rStt, rEnd, pSaveBkmk,
oStartContentIdx,
oEndContentIdx,
isReplace);
// Copy all Redlines which are in the move area into an array
// which holds all position information as offset.
// Assignment happens after moving.
SwRedlineTable& rTable = rDoc.getIDocumentRedlineAccess().GetRedlineTable();
for(SwRangeRedline* pRedl : rTable)
{
// Is at position?
auto [pRStt, pREnd] = pRedl->StartEnd();
if( lcl_Greater( *pRStt, rStt, oStartContentIdx ) && lcl_Lower( *pRStt, rEnd, oEndContentIdx ))
{
pRStt->Assign( rEnd );
if( oEndContentIdx )
pRStt->SetContent( *oEndContentIdx );
else
{
bool bStt = true;
SwContentNode* pCNd = pRStt->GetNode().GetContentNode();
if( !pCNd )
pCNd = SwNodes::GoNext(pRStt);
if (!pCNd)
{
bStt = false;
pRStt->Assign(rStt);
pCNd = SwNodes::GoPrevious( pRStt );
if( !pCNd )
{
*pRStt = *pREnd;
pCNd = pRStt->GetNode().GetContentNode();
}
}
if (pCNd && !bStt)
pRStt->AssignEndIndex( *pCNd );
}
}
if( lcl_Greater( *pREnd, rStt, oStartContentIdx ) && lcl_Lower( *pREnd, rEnd, oEndContentIdx ))
{
pREnd->Assign( rStt );
if (oStartContentIdx && rStt.IsContentNode())
pREnd->SetContent( *oStartContentIdx );
else
{
bool bStt = false;
SwContentNode* pCNd = pREnd->GetNode().GetContentNode();
if( !pCNd )
pCNd = SwNodes::GoPrevious( pREnd );
if( !pCNd )
{
bStt = true;
pREnd->Assign(rEnd);
pCNd = SwNodes::GoNext(pREnd);
if( !pCNd )
{
*pREnd = *pRStt;
pCNd = pREnd->GetNode().GetContentNode();
}
}
if (pCNd && !bStt)
pREnd->AssignEndIndex( *pCNd );
}
if( lcl_Greater( *pRStt, rEnd, oEndContentIdx ) )
break;
}
}
}
namespace sw {
InsertText MakeInsertText(SwTextNode& rNode, const sal_Int32 nPos, const sal_Int32 nLen)
{
SwCursor cursor(SwPosition(rNode, nPos), nullptr);
bool isInsideFieldmarkCommand(false);
bool isInsideFieldmarkResult(false);
while (auto const*const pMark = rNode.GetDoc().getIDocumentMarkAccess()->getInnerFieldmarkFor(*cursor.GetPoint()))
{
if (sw::mark::FindFieldSep(*pMark) < *cursor.GetPoint())
{
isInsideFieldmarkResult = true;
}
else
{
isInsideFieldmarkCommand = true;
}
*cursor.GetPoint() = pMark->GetMarkStart();
if (!cursor.Left(1))
{
break;
}
}
return InsertText(nPos, nLen, isInsideFieldmarkCommand, isInsideFieldmarkResult);
}
} // namespace sw
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|