LibreOffice Module sw (master) 1
wrong.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <swtypes.hxx>
21
22#include <SwGrammarMarkUp.hxx>
23#include <ndtxt.hxx>
24#include <txtfrm.hxx>
25#include <utility>
26
27#include <osl/diagnose.h>
28
29SwWrongArea::SwWrongArea( OUString aType, WrongListType listType,
30 css::uno::Reference< css::container::XStringKeyMap > const & xPropertyBag,
31 sal_Int32 nPos,
32 sal_Int32 nLen)
33: maType(std::move(aType)), mxPropertyBag(xPropertyBag), mnPos(nPos), mnLen(nLen), mpSubList(nullptr)
34{
35 mColor = getWrongAreaColor(listType, xPropertyBag);
36 mLineType = getWrongAreaLineType(listType, xPropertyBag);
37}
38
40 css::uno::Reference< css::container::XStringKeyMap > const & xPropertyBag,
41 sal_Int32 nPos,
42 sal_Int32 nLen,
43 SwWrongList* pSubList)
44: maType(std::move(aType)), mxPropertyBag(xPropertyBag), mnPos(nPos), mnLen(nLen), mpSubList(pSubList), mLineType(WRONGAREA_NONE)
45{
46 if (pSubList != nullptr)
47 {
48 mColor = getWrongAreaColor(pSubList->GetWrongListType(), xPropertyBag);
49 mLineType = getWrongAreaLineType(pSubList->GetWrongListType(), xPropertyBag);
50 }
51}
52
54 meType (eType),
55 mnBeginInvalid(COMPLETE_STRING), // everything correct... (the invalid area starts beyond the string)
56 mnEndInvalid (COMPLETE_STRING)
57{
58 maList.reserve( 5 );
59}
60
62{
63 ClearList();
64}
65
67{
68 SwWrongList* pClone = new SwWrongList( meType );
69 pClone->CopyFrom( *this );
70 return pClone;
71}
72
74{
75 maList = rCopy.maList;
76 meType = rCopy.meType;
79 for(SwWrongArea & i : maList)
80 {
81 if( i.mpSubList )
82 i.mpSubList = i.mpSubList->Clone();
83 }
84}
85
87{
88 for (SwWrongArea & i : maList)
89 {
90 delete i.mpSubList;
91 i.mpSubList = nullptr;
92 }
93 maList.clear();
94}
95
103bool SwWrongList::InWrongWord( sal_Int32 &rChk, sal_Int32 &rLn ) const
104{
105 const sal_uInt16 nPos = GetWrongPos( rChk );
106 if ( nPos >= Count() )
107 return false;
108 const sal_Int32 nWrPos = Pos( nPos );
109 if ( nWrPos <= rChk )
110 {
111 rLn = Len( nPos );
112 if( nWrPos + rLn <= rChk )
113 return false;
114 rChk = nWrPos;
115 return true;
116 }
117 return false;
118}
119
127bool SwWrongList::Check( sal_Int32 &rChk, sal_Int32 &rLn ) const
128{
129 sal_uInt16 nPos = GetWrongPos( rChk );
130 rLn += rChk;
131
132 if( nPos == Count() )
133 return false;
134
135 sal_Int32 nWrPos = Pos( nPos );
136 sal_Int32 nEnd = nWrPos + Len( nPos );
137 if( nEnd == rChk )
138 {
139 ++nPos;
140 if( nPos == Count() )
141 return false;
142
143 nWrPos = Pos( nPos );
144 nEnd = nWrPos + Len( nPos );
145 }
146 if( nEnd > rChk && nWrPos < rLn )
147 {
148 if( nWrPos > rChk )
149 rChk = nWrPos;
150 if( nEnd < rLn )
151 rLn = nEnd;
152 rLn -= rChk;
153 return 0 != rLn;
154 }
155 return false;
156}
157
164sal_Int32 SwWrongList::NextWrong( sal_Int32 nChk ) const
165{
166 sal_Int32 nRet = COMPLETE_STRING;
167 sal_uInt16 nPos = GetWrongPos( nChk );
168 if( nPos < Count() )
169 {
170 nRet = Pos( nPos );
171 if( nRet < nChk && nRet + Len( nPos ) <= nChk )
172 {
173 if( ++nPos < Count() )
174 nRet = Pos( nPos );
175 else
176 nRet = COMPLETE_STRING;
177 }
178 }
179 if( nRet > GetBeginInv() && nChk < GetEndInv() )
180 nRet = std::max(nChk, GetBeginInv());
181 return nRet;
182}
183
191sal_uInt16 SwWrongList::GetWrongPos( sal_Int32 nValue ) const
192{
193 sal_uInt16 nMax = Count();
194 sal_uInt16 nMin = 0;
195
196 if( nMax > 0 )
197 {
198 // For smart tag lists, we may not use a binary search. We return the
199 // position of the first smart tag which covers nValue
200 if ( !maList[0].maType.isEmpty() || maList[0].mpSubList )
201 {
202 auto aIter = std::find_if(maList.begin(), maList.end(),
203 [nValue](const SwWrongArea& rST) {
204 return (rST.mnPos <= nValue && nValue < rST.mnPos + rST.mnLen)
205 || (rST.mnPos > nValue);
206 });
207 return o3tl::narrowing<sal_uInt16>(std::distance(maList.begin(), aIter));
208 }
209
210 --nMax;
211 sal_uInt16 nMid = 0;
212 while( nMin <= nMax )
213 {
214 nMid = nMin + ( nMax - nMin ) / 2;
215 const sal_Int32 nTmp = Pos( nMid );
216 if( nTmp == nValue )
217 {
218 nMin = nMid;
219 break;
220 }
221 else if( nTmp < nValue )
222 {
223 if( nTmp + Len( nMid ) >= nValue )
224 {
225 nMin = nMid;
226 break;
227 }
228 nMin = nMid + 1;
229 }
230 else if( nMid == 0 )
231 {
232 break;
233 }
234 else
235 nMax = nMid - 1;
236 }
237 }
238
239 // nMin now points to an index i into the wrong list which
240 // 1. nValue is inside [ Area[i].pos, Area[i].pos + Area[i].len ] (inclusive!!!)
241 // 2. nValue < Area[i].pos
242
243 return nMin;
244}
245
246void SwWrongList::Invalidate_( sal_Int32 nBegin, sal_Int32 nEnd )
247{
248 if ( nBegin < GetBeginInv() )
249 mnBeginInvalid = nBegin;
250 if ( nEnd > GetEndInv() || GetEndInv() == COMPLETE_STRING )
251 mnEndInvalid = nEnd;
252}
253
254void SwWrongList::SetInvalid( sal_Int32 nBegin, sal_Int32 nEnd )
255{
256 mnBeginInvalid = nBegin;
257 mnEndInvalid = nEnd;
258}
259
267void SwWrongList::Move( sal_Int32 nPos, sal_Int32 nDiff )
268{
269 sal_uInt16 i = GetWrongPos( nPos );
270 if( nDiff < 0 )
271 {
272 const sal_Int32 nEnd = nPos - nDiff;
273 sal_uInt16 nLst = i;
274 bool bJump = false;
275 while( nLst < Count() && Pos( nLst ) < nEnd )
276 ++nLst;
277 if( nLst > i )
278 {
279 const sal_Int32 nWrPos = Pos( nLst - 1 );
280 if ( nWrPos <= nPos )
281 {
282 sal_Int32 nWrLen = Len( nLst - 1 );
283 // calculate new length of word
284 nWrLen = ( nEnd > nWrPos + nWrLen ) ?
285 nPos - nWrPos :
286 nWrLen + nDiff;
287 if( nWrLen )
288 {
289 maList[--nLst].mnLen = nWrLen;
290 bJump = true;
291 }
292 }
293 }
294 Remove( i, nLst - i );
295
296 if ( bJump )
297 ++i;
299 SetInvalid( nPos ? nPos - 1 : nPos, nPos + 1 );
300 else
301 {
302 ShiftLeft( mnBeginInvalid, nPos, nEnd );
304 ShiftLeft( mnEndInvalid, nPos, nEnd );
305 Invalidate_( nPos ? nPos - 1 : nPos, nPos + 1 );
306 }
307 }
308 else
309 {
310 const sal_Int32 nEnd = nPos + nDiff;
312 {
313 if( mnBeginInvalid > nPos )
314 mnBeginInvalid += nDiff;
316 mnEndInvalid += nDiff;
317 }
318 // If the pointer is in the middle of a wrong word,
319 // invalidation must happen from the beginning of that word.
320 if( i < Count() )
321 {
322 const sal_Int32 nWrPos = Pos( i );
323 if (nPos >= nWrPos)
324 {
325 Invalidate( nWrPos, nEnd );
326 const sal_Int32 nWrLen = Len( i ) + nDiff;
327 maList[i++].mnLen = nWrLen;
328 Invalidate( nWrPos, nWrPos + nWrLen );
329 }
330 }
331 else
332 Invalidate( nPos, nEnd );
333 }
334 while( i < Count() )
335 {
336 maList[i++].mnPos += nDiff;
337 }
338}
339
340// TODO: Complete documentation
356auto SwWrongList::Fresh( sal_Int32 &rStart, sal_Int32 &rEnd, sal_Int32 nPos,
357 sal_Int32 nLen, sal_uInt16 nIndex, sal_Int32 nCursorPos ) -> FreshState
358{
359 // length of word must be greater than 0
360 // only report a spelling error if the cursor position is outside the word,
361 // so that the user is not annoyed while typing
362 FreshState eRet = nLen
363 ? (nCursorPos > nPos + nLen || nCursorPos < nPos)
364 ? FreshState::FRESH
365 : FreshState::CURSOR
366 : FreshState::NOTHING;
367
368 sal_Int32 nWrPos = 0;
369 sal_Int32 nWrEnd = rEnd;
370 sal_uInt16 nCnt = nIndex;
371 if( nCnt < Count() )
372 {
373 nWrPos = Pos( nCnt );
374 if( nWrPos < nPos && rStart > nWrPos )
375 rStart = nWrPos;
376 }
377
378 while( nCnt < Count() )
379 {
380 nWrPos = Pos( nCnt );
381 if ( nWrPos >= nPos )
382 break;
383 nWrEnd = nWrPos + Len( nCnt++ );
384 }
385
386 if( nCnt < Count() && nWrPos == nPos && Len( nCnt ) == nLen )
387 {
388 ++nCnt;
389 eRet = FreshState::FRESH;
390 }
391 else
392 {
393 if (FreshState::FRESH == eRet)
394 {
395 if( rStart > nPos )
396 rStart = nPos;
397 nWrEnd = nPos + nLen;
398 }
399 }
400
401 nPos += nLen;
402
403 if( nCnt < Count() )
404 {
405 nWrPos = Pos( nCnt );
406 if( nWrPos < nPos && rStart > nWrPos )
407 rStart = nWrPos;
408 }
409
410 while( nCnt < Count() )
411 {
412 nWrPos = Pos( nCnt );
413 if ( nWrPos >= nPos )
414 break;
415 nWrEnd = nWrPos + Len( nCnt++ );
416 }
417
418 if( rEnd < nWrEnd )
419 rEnd = nWrEnd;
420
421 Remove( nIndex, nCnt - nIndex );
422
423 return eRet;
424}
425
426void SwWrongList::Invalidate( sal_Int32 nBegin, sal_Int32 nEnd )
427{
429 SetInvalid( nBegin, nEnd );
430 else
431 Invalidate_( nBegin, nEnd );
432}
433
435{
436 if( Count() )
437 {
438 const sal_Int32 nFirst = Pos( 0 );
439 const sal_Int32 nLast = Pos( Count() - 1 ) + Len( Count() - 1 );
440 Invalidate( nFirst, nLast );
441 return true;
442 }
443 return false;
444}
445
446std::unique_ptr<SwWrongList> SwWrongList::SplitList( sal_Int32 nSplitPos )
447{
448 std::unique_ptr<SwWrongList> pRet;
449 sal_uInt16 nLst = 0;
450 while( nLst < Count() && Pos( nLst ) < nSplitPos )
451 ++nLst;
452 if( nLst )
453 {
454 sal_Int32 nWrPos = Pos( nLst - 1 );
455 sal_Int32 nWrLen = Len( nLst - 1 );
456 if ( nWrPos+nWrLen > nSplitPos )
457 {
458 nWrLen += nWrPos - nSplitPos;
459 maList[--nLst].mnPos = nSplitPos;
460 maList[nLst].mnLen = nWrLen;
461 }
462 }
463 if( nLst )
464 {
466 pRet.reset(new SwGrammarMarkUp());
467 else
468 pRet.reset(new SwWrongList( GetWrongListType() ));
469 pRet->Insert(0, maList.begin(), ( nLst >= maList.size() ? maList.end() : maList.begin() + nLst ) );
470 pRet->SetInvalid( GetBeginInv(), GetEndInv() );
471 pRet->Invalidate_( nSplitPos ? nSplitPos - 1 : nSplitPos, nSplitPos );
472 Remove( 0, nLst );
473 }
475 SetInvalid( 0, 1 );
476 else
477 {
478 ShiftLeft( mnBeginInvalid, 0, nSplitPos );
480 ShiftLeft( mnEndInvalid, 0, nSplitPos );
481 Invalidate_( 0, 1 );
482 }
483 for (nLst = 0; nLst < Count(); ++nLst )
484 {
485 maList[nLst].mnPos -= nSplitPos;
486 }
487 return pRet;
488}
489
490void SwWrongList::JoinList( SwWrongList* pNext, sal_Int32 nInsertPos )
491{
492 if (pNext)
493 {
494 OSL_ENSURE( GetWrongListType() == pNext->GetWrongListType(), "type mismatch with next list" );
495
496 sal_uInt16 nCnt = Count();
497 pNext->Move( 0, nInsertPos );
498 Insert(nCnt, pNext->maList.begin(), pNext->maList.end());
499
500 Invalidate( pNext->GetBeginInv(), pNext->GetEndInv() );
501 if( nCnt && Count() > nCnt )
502 {
503 sal_Int32 nWrPos = Pos( nCnt );
504 sal_Int32 nWrLen = Len( nCnt );
505 if( !nWrPos )
506 {
507 nWrPos += nInsertPos;
508 nWrLen -= nInsertPos;
509 maList[nCnt].mnPos = nWrPos;
510 maList[nCnt].mnLen = nWrLen;
511 }
512 if( nWrPos == Pos( nCnt - 1 ) + Len( nCnt - 1 ) )
513 {
514 nWrLen += Len( nCnt - 1 );
515 maList[nCnt - 1].mnLen = nWrLen;
516 Remove( nCnt, 1 );
517 }
518 }
519 }
520 Invalidate( nInsertPos ? nInsertPos - 1 : nInsertPos, nInsertPos + 1 );
521}
522
523void SwWrongList::InsertSubList( sal_Int32 nNewPos, sal_Int32 nNewLen, sal_uInt16 nWhere, SwWrongList* pSubList )
524{
525 if (pSubList)
526 {
527 OSL_ENSURE( GetWrongListType() == pSubList->GetWrongListType(), "type mismatch with sub list" );
528 }
529 std::vector<SwWrongArea>::iterator i = maList.begin();
530 if ( nWhere >= maList.size() )
531 i = maList.end(); // robust
532 else
533 i += nWhere;
534 maList.insert(i, SwWrongArea( OUString(), nullptr, nNewPos, nNewLen, pSubList ) );
535}
536
537// New functions: Necessary because SwWrongList has been changed to use std::vector
538void SwWrongList::Insert(sal_uInt16 nWhere, std::vector<SwWrongArea>::iterator startPos, std::vector<SwWrongArea>::iterator const & endPos)
539{
540 std::vector<SwWrongArea>::iterator i = maList.begin();
541 if ( nWhere >= maList.size() )
542 i = maList.end(); // robust
543 else
544 i += nWhere;
545 maList.insert(i, startPos, endPos); // insert [startPos, endPos[ before i
546
547 // ownership of the sublist is passed to maList, therefore we have to set the
548 // pSubList-Pointers to 0
549 while ( startPos != endPos )
550 {
551 (*startPos).mpSubList = nullptr;
552 ++startPos;
553 }
554}
555
556void SwWrongList::Remove(sal_uInt16 nIdx, sal_uInt16 nLen )
557{
558 if ( nIdx >= maList.size() ) return;
559 std::vector<SwWrongArea>::iterator i1 = maList.begin();
560 i1 += nIdx;
561
562 std::vector<SwWrongArea>::iterator i2 = i1;
563 if ( nIdx + nLen >= o3tl::narrowing<sal_uInt16>(maList.size()) )
564 i2 = maList.end(); // robust
565 else
566 i2 += nLen;
567
568 std::vector<SwWrongArea>::iterator iLoop = i1;
569 while ( iLoop != i2 )
570 {
571 delete (*iLoop).mpSubList;
572 ++iLoop;
573 }
574
575#if OSL_DEBUG_LEVEL > 0
576 const int nOldSize = Count();
577#endif
578
579 maList.erase(i1, i2);
580
581#if OSL_DEBUG_LEVEL > 0
582 OSL_ENSURE( Count() + nLen == nOldSize, "SwWrongList::Remove() trouble" );
583#endif
584}
585
586void SwWrongList::RemoveEntry( sal_Int32 nBegin, sal_Int32 nEnd ) {
587 std::vector<SwWrongArea>::const_iterator aEnd(maList.end());
588 auto aDelIter = std::find_if(maList.cbegin(), aEnd,
589 [nBegin](const SwWrongArea& rST) { return rST.mnPos >= nBegin; });
590 auto aIter = aDelIter;
592 {
593 if( nBegin < nEnd )
594 {
595 aIter = std::find_if(aDelIter, aEnd,
596 [nEnd](const SwWrongArea& rST) { return rST.mnPos >= nEnd; });
597 }
598 }
599 else
600 {
601 aIter = std::find_if(aDelIter, aEnd,
602 [nBegin, nEnd](const SwWrongArea& rST) {
603 return (rST.mnPos != nBegin) || ((rST.mnPos + rST.mnLen) != nEnd);
604 });
605 }
606 auto nDel = o3tl::narrowing<sal_uInt16>(std::distance(aDelIter, aIter));
607 if( nDel )
608 {
609 auto nDelPos = o3tl::narrowing<sal_uInt16>(std::distance(maList.cbegin(), aDelIter));
610 Remove( nDelPos, nDel );
611 }
612}
613
614bool SwWrongList::LookForEntry( sal_Int32 nBegin, sal_Int32 nEnd ) {
615 auto aIter = std::find_if(maList.begin(), maList.end(),
616 [nBegin](const SwWrongArea& rST) { return rST.mnPos >= nBegin; });
617 return aIter != maList.end()
618 && nBegin == (*aIter).mnPos
619 && nEnd == (*aIter).mnPos + (*aIter).mnLen;
620}
621
622void SwWrongList::Insert( const OUString& rType,
623 css::uno::Reference< css::container::XStringKeyMap > const & xPropertyBag,
624 sal_Int32 nNewPos, sal_Int32 nNewLen )
625{
626 auto aIter = std::find_if(maList.begin(), maList.end(),
627 [nNewPos](const SwWrongArea& rST) { return nNewPos <= rST.mnPos; });
628 if ( aIter != maList.end() && nNewPos == (*aIter).mnPos )
629 {
630 const sal_Int32 nSTPos = (*aIter).mnPos;
631
632 aIter = std::find_if(aIter, maList.end(),
633 [nSTPos, nNewLen](const SwWrongArea& rST) { return rST.mnPos != nSTPos || nNewLen < rST.mnLen; });
634 }
635
636 maList.insert(aIter, SwWrongArea( rType, meType, xPropertyBag, nNewPos, nNewLen) );
637}
638
639namespace sw {
640
642 SwWrongList const* (SwTextNode::*pGetWrongList)() const)
643 : m_pGetWrongList(pGetWrongList)
644 , m_pMergedPara(rFrame.GetMergedPara())
645 , m_CurrentExtent(0)
646 , m_CurrentIndex(0)
647 , m_pWrongList(m_pMergedPara
648 ? nullptr
649 : (rFrame.GetTextNodeFirst()->*pGetWrongList)())
650{
651}
652
654 : m_pGetWrongList(nullptr)
655 , m_pMergedPara(nullptr)
656 , m_CurrentExtent(0)
657 , m_CurrentIndex(0)
658 , m_pWrongList(&rWrongList)
659{
660}
661
663 SwWrongList const* (SwTextNode::*pGetWrongList)() const)
664 : WrongListIteratorBase(rFrame, pGetWrongList)
665{
666}
667
669 : WrongListIteratorBase(rWrongList)
670{
671}
672
674{
675 if (m_pMergedPara)
676 {
677 if (rStart < m_CurrentIndex)
678 { // rewind
679 m_CurrentExtent = 0;
681 }
682 while (m_CurrentExtent < m_pMergedPara->extents.size())
683 {
685 if (rStart + rLen <= m_CurrentIndex)
686 {
687 return false;
688 }
689 else if (rStart < m_CurrentIndex)
690 {
691 rLen -= m_CurrentIndex - rStart;
692 assert(0 < sal_Int32(rLen));
693 rStart = m_CurrentIndex;
694 }
695 if (m_CurrentIndex <= rStart &&
696 rStart < m_CurrentIndex + TextFrameIndex(rExtent.nEnd - rExtent.nStart))
697 {
698 SwWrongList const*const pWrongList((rExtent.pNode->*m_pGetWrongList)());
699 // found the extent containing start - first, call Check
700 sal_Int32 nStart(rExtent.nStart + sal_Int32(rStart - m_CurrentIndex)); // (m_CurrentIndex - m_CurrentNodeIndex));
701 sal_Int32 nLen;
702 if (sal_Int32(rLen) < rExtent.nEnd - nStart)
703 {
704 nLen = sal_Int32(rLen);
705 }
706 else
707 {
708 sal_Int32 nInLen(rLen);
709 nLen = rExtent.nEnd - nStart;
710 nInLen -= nLen;
711 for (size_t i = m_CurrentExtent + 1;
712 i < m_pMergedPara->extents.size(); ++i)
713 {
714 sw::Extent const& rExtentEnd(m_pMergedPara->extents[i]);
715 if (rExtentEnd.pNode != rExtent.pNode)
716 {
717 break;
718 }
719 // add gap too
720 nLen += rExtentEnd.nStart - m_pMergedPara->extents[i-1].nEnd;
721 if (nInLen <= rExtentEnd.nEnd - rExtentEnd.nStart)
722 {
723 nLen += nInLen;
724 break;
725 }
726 nLen += rExtentEnd.nEnd - rExtentEnd.nStart;
727 nInLen -= rExtentEnd.nEnd - rExtentEnd.nStart;
728 }
729 }
730 if (pWrongList && pWrongList->Check(nStart, nLen))
731 {
732 // check if there's overlap with this extent
733 if (rExtent.nStart <= nStart && nStart < rExtent.nEnd)
734 {
735 // yes - now compute end position / length
736 sal_Int32 const nEnd(nStart + nLen);
737 rStart = m_CurrentIndex + TextFrameIndex(nStart - rExtent.nStart);
738 TextFrameIndex const nOrigLen(rLen);
739 if (nEnd <= rExtent.nEnd)
740 {
741 rLen = TextFrameIndex(nEnd - nStart);
742 }
743 else // have to search other extents for the end...
744 {
745 rLen = TextFrameIndex(rExtent.nEnd - nStart);
746 for (size_t i = m_CurrentExtent + 1;
747 i < m_pMergedPara->extents.size(); ++i)
748 {
749 sw::Extent const& rExtentEnd(m_pMergedPara->extents[i]);
750 if (rExtentEnd.pNode != rExtent.pNode
751 || nEnd <= rExtentEnd.nStart)
752 {
753 break;
754 }
755 if (nEnd <= rExtentEnd.nEnd)
756 {
757 rLen += TextFrameIndex(nEnd - rExtentEnd.nStart);
758 break;
759 }
760 rLen += TextFrameIndex(rExtentEnd.nEnd - rExtentEnd.nStart);
761 }
762 }
763 assert(rLen <= nOrigLen); (void) nOrigLen;
764 return true;
765 }
766 }
767 }
768 m_CurrentIndex += TextFrameIndex(rExtent.nEnd - rExtent.nStart);
770 }
771 return false;
772 }
773 else if (m_pWrongList)
774 {
775 sal_Int32 nStart(rStart);
776 sal_Int32 nLen(rLen);
777 bool const bRet(m_pWrongList->Check(nStart, nLen));
778 rStart = TextFrameIndex(nStart);
779 rLen = TextFrameIndex(nLen);
780 return bRet;
781 }
782 return false;
783}
784
785const SwWrongArea*
787{
788 if (m_pMergedPara)
789 {
790 if (nStart < m_CurrentIndex)
791 { // rewind
792 m_CurrentExtent = 0;
794 }
795 while (m_CurrentExtent < m_pMergedPara->extents.size())
796 {
798 if (m_CurrentIndex <= nStart &&
799 nStart <= m_CurrentIndex + TextFrameIndex(rExtent.nEnd - rExtent.nStart))
800 {
801 // note: the returned object isn't wrapped because fntcache.cxx
802 // does not look at its positions, only its formatting props
803 SwWrongList const*const pWrongList((rExtent.pNode->*m_pGetWrongList)());
804 if (pWrongList)
805 {
806 sal_Int32 const nNStart(rExtent.nStart + sal_Int32(nStart - m_CurrentIndex)); // (m_CurrentIndex - m_CurrentNodeIndex));
807 sal_Int16 const nPos(pWrongList->GetWrongPos(nNStart));
808 return pWrongList->GetElement(nPos);
809 }
810 }
811 m_CurrentIndex += TextFrameIndex(rExtent.nEnd - rExtent.nStart);
813 }
814 return nullptr;
815 }
816 else if (m_pWrongList)
817 {
818 sal_Int16 const nPos(m_pWrongList->GetWrongPos(sal_Int32(nStart)));
820 }
821 return nullptr;
822}
823
825 SwWrongList const* (SwTextNode::*pGetWrongList)() const)
826 : WrongListIteratorBase(rFrame, pGetWrongList)
827{
828}
829
831 : WrongListIteratorBase(rWrongList)
832{
833}
834
836{
837 if (m_pMergedPara)
838 {
839 sal_uInt16 nRet(0);
840 m_CurrentExtent = 0;
842 SwNode const* pNode(nullptr);
843 sal_uInt16 InCurrentNode(0);
844 while (m_CurrentExtent < m_pMergedPara->extents.size())
845 {
847 if (rExtent.pNode != pNode)
848 {
849 InCurrentNode = 0;
850 pNode = rExtent.pNode;
851 }
852 SwWrongList const*const pWrongList((rExtent.pNode->*m_pGetWrongList)());
853 for (; pWrongList && InCurrentNode < pWrongList->Count(); ++InCurrentNode)
854 {
855 SwWrongArea const*const pWrong(pWrongList->GetElement(InCurrentNode));
856 TextFrameIndex const nExtentEnd(
857 m_CurrentIndex + TextFrameIndex(rExtent.nEnd - rExtent.nStart));
858 if (nExtentEnd <= TextFrameIndex(pWrong->mnPos))
859 {
860 break; // continue outer loop
861 }
862 if (m_CurrentIndex < TextFrameIndex(pWrong->mnPos + pWrong->mnLen))
863 {
864 ++nRet;
865 }
866 }
867 m_CurrentIndex += TextFrameIndex(rExtent.nEnd - rExtent.nStart);
869 }
870 return nRet;
871 }
872 else if (m_pWrongList)
873 {
874 return m_pWrongList->Count();
875 }
876 return 0;
877}
878
879std::optional<std::pair<TextFrameIndex, TextFrameIndex>>
881{
882 if (m_pMergedPara)
883 {
884 m_CurrentExtent = 0;
886 SwNode const* pNode(nullptr);
887 sal_uInt16 InCurrentNode(0);
888 while (m_CurrentExtent < m_pMergedPara->extents.size())
889 {
891 if (rExtent.pNode != pNode)
892 {
893 InCurrentNode = 0;
894 pNode = rExtent.pNode;
895 }
896 SwWrongList const*const pWrongList((rExtent.pNode->*m_pGetWrongList)());
897 for (; pWrongList && InCurrentNode < pWrongList->Count(); ++InCurrentNode)
898 {
899 SwWrongArea const*const pWrong(pWrongList->GetElement(InCurrentNode));
900 TextFrameIndex const nExtentEnd(
901 m_CurrentIndex + TextFrameIndex(rExtent.nEnd - rExtent.nStart));
902 if (nExtentEnd <= TextFrameIndex(pWrong->mnPos))
903 {
904 break; // continue outer loop
905 }
906 if (m_CurrentIndex < TextFrameIndex(pWrong->mnPos + pWrong->mnLen))
907 {
908 if (nIndex == 0)
909 {
910 return std::optional<std::pair<TextFrameIndex, TextFrameIndex>>(
911 std::pair<TextFrameIndex, TextFrameIndex>(
913 std::max(rExtent.nStart, pWrong->mnPos)),
915 std::min(pWrong->mnPos + pWrong->mnLen, rExtent.nEnd))));
916 }
917 --nIndex;
918 }
919 }
920 m_CurrentIndex += TextFrameIndex(rExtent.nEnd - rExtent.nStart);
922 }
923 return std::optional<std::pair<TextFrameIndex, TextFrameIndex>>();
924 }
925 else if (m_pWrongList)
926 {
927 SwWrongArea const*const pWrong(m_pWrongList->GetElement(nIndex));
928 return std::optional<std::pair<TextFrameIndex, TextFrameIndex>>(
929 std::pair<TextFrameIndex, TextFrameIndex>(
930 TextFrameIndex(pWrong->mnPos),
931 TextFrameIndex(pWrong->mnPos + pWrong->mnLen)));
932 }
933 return std::optional<std::pair<TextFrameIndex, TextFrameIndex>>();
934}
935
936} // namespace sw
937
938/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
o3tl::strong_int< sal_Int32, struct Tag_TextFrameIndex > TextFrameIndex
Denotes a character index in a text frame at a layout level, after extent mapping from a text node at...
Base class of the Writer document model elements.
Definition: node.hxx:98
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:165
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:111
Color mColor
Definition: wrong.hxx:74
sal_Int32 mnPos
Definition: wrong.hxx:70
static Color getWrongAreaColor(WrongListType listType, css::uno::Reference< css::container::XStringKeyMap > const &xPropertyBag)
Definition: wrong.hxx:222
WrongAreaLineType mLineType
Definition: wrong.hxx:75
SwWrongArea(OUString aType, WrongListType listType, css::uno::Reference< css::container::XStringKeyMap > const &xPropertyBag, sal_Int32 nPos, sal_Int32 nLen)
Definition: wrong.cxx:29
sal_Int32 mnLen
Definition: wrong.hxx:71
static WrongAreaLineType getWrongAreaLineType(WrongListType listType, css::uno::Reference< css::container::XStringKeyMap > const &xPropertyBag)
Definition: wrong.hxx:241
void ClearList()
Definition: wrong.cxx:86
bool InvalidateWrong()
Definition: wrong.cxx:434
sal_uInt16 Count() const
Definition: wrong.hxx:323
sal_Int32 GetEndInv() const
Definition: wrong.hxx:289
void RemoveEntry(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:586
std::unique_ptr< SwWrongList > SplitList(sal_Int32 nSplitPos)
Definition: wrong.cxx:446
WrongListType GetWrongListType() const
Definition: wrong.hxx:287
void JoinList(SwWrongList *pNext, sal_Int32 nInsertPos)
Definition: wrong.cxx:490
const SwWrongArea * GetElement(sal_uInt16 nIdx) const
Definition: wrong.hxx:349
bool Check(sal_Int32 &rChk, sal_Int32 &rLn) const
Calculate first incorrectly selected area.
Definition: wrong.cxx:127
virtual SwWrongList * Clone()
Definition: wrong.cxx:66
void Invalidate(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:426
std::vector< SwWrongArea > maList
Definition: wrong.hxx:264
sal_Int32 mnEndInvalid
Definition: wrong.hxx:268
sal_Int32 Pos(sal_uInt16 nIdx) const
Definition: wrong.hxx:318
void InsertSubList(sal_Int32 nNewPos, sal_Int32 nNewLen, sal_uInt16 nWhere, SwWrongList *pSubList)
Definition: wrong.cxx:523
SwWrongList(const SwWrongList &rCpy)=delete
FreshState Fresh(sal_Int32 &rStart, sal_Int32 &rEnd, sal_Int32 nPos, sal_Int32 nLen, sal_uInt16 nIndex, sal_Int32 nCursorPos)
Remove given range of entries.
Definition: wrong.cxx:356
virtual void CopyFrom(const SwWrongList &rCopy)
Definition: wrong.cxx:73
sal_uInt16 GetWrongPos(sal_Int32 nValue) const
Find the first position that is greater or equal to the given value.
Definition: wrong.cxx:191
bool InWrongWord(sal_Int32 &rChk, sal_Int32 &rLn) const
If a word is incorrectly selected, this method returns begin and length of it.
Definition: wrong.cxx:103
void Invalidate_(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:246
void Remove(sal_uInt16 nIdx, sal_uInt16 nLen)
Definition: wrong.cxx:556
void SetInvalid(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:254
static void ShiftLeft(sal_Int32 &rPos, sal_Int32 nStart, sal_Int32 nEnd)
Definition: wrong.hxx:270
void Move(sal_Int32 nPos, sal_Int32 nDiff)
Change all values after the given position.
Definition: wrong.cxx:267
void Insert(sal_uInt16 nWhere, std::vector< SwWrongArea >::iterator startPos, std::vector< SwWrongArea >::iterator const &endPos)
Definition: wrong.cxx:538
sal_Int32 GetBeginInv() const
Definition: wrong.hxx:288
virtual ~SwWrongList()
Definition: wrong.cxx:61
WrongListType meType
Definition: wrong.hxx:265
bool LookForEntry(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:614
sal_Int32 mnBeginInvalid
Definition: wrong.hxx:267
sal_Int32 Len(sal_uInt16 nIdx) const
Definition: wrong.hxx:313
sal_Int32 NextWrong(sal_Int32 nChk) const
Find next incorrectly selected position.
Definition: wrong.cxx:164
sw::MergedPara const *const m_pMergedPara
Definition: wrong.hxx:368
SwWrongList const *(SwTextNode::*const m_pGetWrongList)() const
Definition: wrong.hxx:367
WrongListIteratorBase(SwTextFrame const &rFrame, SwWrongList const *(SwTextNode::*pGetWrongList)() const)
for the text frame
Definition: wrong.cxx:641
SwWrongList const *const m_pWrongList
Definition: wrong.hxx:371
TextFrameIndex m_CurrentIndex
Definition: wrong.hxx:370
sal_uInt16 GetElementCount()
Definition: wrong.cxx:835
std::optional< std::pair< TextFrameIndex, TextFrameIndex > > GetElementAt(sal_uInt16 nIndex)
Definition: wrong.cxx:880
WrongListIteratorCounter(SwTextFrame const &rFrame, SwWrongList const *(SwTextNode::*pGetWrongList)() const)
Definition: wrong.cxx:824
bool Check(TextFrameIndex &rStart, TextFrameIndex &rLen)
Definition: wrong.cxx:673
WrongListIterator(SwTextFrame const &rFrame, SwWrongList const *(SwTextNode::*pGetWrongList)() const)
for the text frame
Definition: wrong.cxx:662
const SwWrongArea * GetWrongElement(TextFrameIndex nStart)
Definition: wrong.cxx:786
DocumentType eType
sal_Int16 nValue
sal_Int32 nIndex
sal_uInt16 nPos
int i
Count
Dialog to specify the properties of date form field.
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
Describes a part of a single text node, which will be part of a text frame, even when redlines are hi...
Definition: txtfrm.hxx:88
sal_Int32 nEnd
Definition: txtfrm.hxx:91
sal_Int32 nStart
Definition: txtfrm.hxx:90
SwTextNode * pNode
Definition: txtfrm.hxx:89
std::vector< Extent > extents
Definition: txtfrm.hxx:965
constexpr sal_Int32 COMPLETE_STRING
Definition: swtypes.hxx:57
OUString maType
RedlineType meType
WrongListType
Definition: wrong.hxx:57
@ WRONGLIST_GRAMMAR
Definition: wrong.hxx:59
@ WRONGAREA_NONE
Definition: wrong.hxx:49