xref: /core/sw/source/core/undo/undobj.cxx (revision 12e48f91)
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 <libxml/xmlwriter.h>
21 
22 #include <IShellCursorSupplier.hxx>
23 #include <txtftn.hxx>
24 #include <fmtanchr.hxx>
25 #include <ftnidx.hxx>
26 #include <frmfmt.hxx>
27 #include <doc.hxx>
28 #include <UndoManager.hxx>
29 #include <IDocumentRedlineAccess.hxx>
30 #include <docary.hxx>
31 #include <swcrsr.hxx>
32 #include <swundo.hxx>
33 #include <pam.hxx>
34 #include <ndtxt.hxx>
35 #include <UndoCore.hxx>
36 #include <rolbck.hxx>
37 #include <ndnotxt.hxx>
38 #include <IMark.hxx>
39 #include <mvsave.hxx>
40 #include <redline.hxx>
41 #include <crossrefbookmark.hxx>
42 #include <strings.hrc>
43 #include <docsh.hxx>
44 #include <view.hxx>
45 #include <frameformats.hxx>
46 #include <o3tl/deleter.hxx>
47 #include <sal/log.hxx>
48 
49 // This class saves the Pam as integers and can recompose those into a PaM
SwUndRng()50 SwUndRng::SwUndRng()
51     : m_nSttNode( 0 ), m_nEndNode( 0 ), m_nSttContent( 0 ), m_nEndContent( 0 )
52 {
53 }
54 
SwUndRng(const SwPaM & rPam)55 SwUndRng::SwUndRng( const SwPaM& rPam )
56 {
57     SetValues( rPam );
58 }
59 
SetValues(const SwPaM & rPam)60 void SwUndRng::SetValues( const SwPaM& rPam )
61 {
62     const SwPosition *pStt = rPam.Start();
63     if( rPam.HasMark() )
64     {
65         const SwPosition *pEnd = rPam.End();
66         m_nEndNode = pEnd->GetNodeIndex();
67         m_nEndContent = pEnd->GetContentIndex();
68     }
69     else
70     {
71         // no selection !!
72         m_nEndNode = SwNodeOffset(0);
73         m_nEndContent = COMPLETE_STRING;
74     }
75 
76     m_nSttNode = pStt->GetNodeIndex();
77     m_nSttContent = pStt->GetContentIndex();
78 }
79 
SetPaM(SwPaM & rPam,bool bCorrToContent) const80 void SwUndRng::SetPaM( SwPaM & rPam, bool bCorrToContent ) const
81 {
82     rPam.DeleteMark();
83     rPam.GetPoint()->Assign( m_nSttNode, m_nSttContent );
84     SwNode& rNd = rPam.GetPointNode();
85     if( !rNd.IsContentNode() && bCorrToContent )
86         rPam.Move( fnMoveForward, GoInContent );
87 
88     if( !m_nEndNode && COMPLETE_STRING == m_nEndContent )       // no selection
89         return ;
90 
91     rPam.SetMark();
92     if( m_nSttNode == m_nEndNode && m_nSttContent == m_nEndContent )
93         return;                             // nothing left to do
94 
95     rPam.GetPoint()->Assign( m_nEndNode, m_nEndContent );
96     if( !rPam.GetPointNode().IsContentNode() && bCorrToContent )
97         rPam.Move( fnMoveBackward, GoInContent );
98 }
99 
AddUndoRedoPaM(::sw::UndoRedoContext & rContext,bool const bCorrToContent) const100 SwPaM & SwUndRng::AddUndoRedoPaM(
101         ::sw::UndoRedoContext & rContext, bool const bCorrToContent) const
102 {
103     SwCursor & rPaM( rContext.GetCursorSupplier().CreateNewShellCursor() );
104     SetPaM( rPaM, bCorrToContent );
105     return rPaM;
106 }
107 
RemoveIdxFromSection(SwDoc & rDoc,SwNodeOffset nSttIdx,const SwNodeOffset * pEndIdx)108 void SwUndo::RemoveIdxFromSection( SwDoc& rDoc, SwNodeOffset nSttIdx,
109                                     const SwNodeOffset* pEndIdx )
110 {
111     SwNodeIndex aIdx( rDoc.GetNodes(), nSttIdx );
112     SwNodeIndex aEndIdx( rDoc.GetNodes(), pEndIdx ? *pEndIdx
113                                     : aIdx.GetNode().EndOfSectionIndex() );
114     SwPosition aPos( rDoc.GetNodes().GetEndOfPostIts() );
115     SwDoc::CorrAbs( aIdx, aEndIdx, aPos, true );
116 }
117 
RemoveIdxFromRange(SwPaM & rPam,bool bMoveNext)118 void SwUndo::RemoveIdxFromRange( SwPaM& rPam, bool bMoveNext )
119 {
120     const SwPosition* pEnd = rPam.End();
121     if( bMoveNext )
122     {
123         if( pEnd != rPam.GetPoint() )
124             rPam.Exchange();
125 
126         SwNodeIndex aStt( rPam.GetMark()->GetNode() );
127         SwNodeIndex aEnd( rPam.GetPoint()->GetNode() );
128 
129         if( !rPam.Move( fnMoveForward ) )
130         {
131             rPam.Exchange();
132             if( !rPam.Move( fnMoveBackward ) )
133             {
134                 rPam.GetPoint()->Assign( rPam.GetDoc().GetNodes().GetEndOfPostIts() );
135             }
136         }
137 
138         SwDoc::CorrAbs( aStt, aEnd, *rPam.GetPoint(), true );
139     }
140     else
141         SwDoc::CorrAbs( rPam, *pEnd, true );
142 }
143 
RemoveIdxRel(SwNodeOffset nIdx,const SwPosition & rPos)144 void SwUndo::RemoveIdxRel( SwNodeOffset nIdx, const SwPosition& rPos )
145 {
146     // Move only the Cursor. Bookmarks/TOXMarks/etc. are done by the corresponding
147     // JoinNext/JoinPrev
148     ::PaMCorrRel( *rPos.GetNode().GetNodes()[nIdx], rPos );
149 }
150 
SwUndo(SwUndoId const nId,const SwDoc * pDoc)151 SwUndo::SwUndo(SwUndoId const nId, const SwDoc* pDoc)
152     : m_nId(nId), m_nOrigRedlineFlags(RedlineFlags::NONE)
153     , m_nViewShellId(CreateViewShellId(pDoc))
154     , m_isRepeatIgnored(false)
155     , m_bCacheComment(true)
156 {
157 }
158 
CreateViewShellId(const SwDoc * pDoc)159 ViewShellId SwUndo::CreateViewShellId(const SwDoc* pDoc)
160 {
161     ViewShellId nRet(-1);
162 
163     if (const SwDocShell* pDocShell = pDoc->GetDocShell())
164     {
165         if (const SwView* pView = pDocShell->GetView())
166             nRet = pView->GetViewShellId();
167     }
168 
169     return nRet;
170 }
171 
IsDelBox() const172 bool SwUndo::IsDelBox() const
173 {
174     return GetId() == SwUndoId::COL_DELETE || GetId() == SwUndoId::ROW_DELETE ||
175         GetId() == SwUndoId::TABLE_DELBOX;
176 }
177 
~SwUndo()178 SwUndo::~SwUndo()
179 {
180 }
181 
182 namespace {
183 
184 class UndoRedoRedlineGuard
185 {
186 public:
UndoRedoRedlineGuard(::sw::UndoRedoContext const & rContext,SwUndo const & rUndo)187     UndoRedoRedlineGuard(::sw::UndoRedoContext const & rContext, SwUndo const & rUndo)
188         : m_rRedlineAccess(rContext.GetDoc().getIDocumentRedlineAccess())
189         , m_eMode(m_rRedlineAccess.GetRedlineFlags())
190     {
191         RedlineFlags const eTmpMode = rUndo.GetRedlineFlags();
192         if ((RedlineFlags::ShowMask & eTmpMode) != (RedlineFlags::ShowMask & m_eMode))
193         {
194             m_rRedlineAccess.SetRedlineFlags( eTmpMode );
195         }
196         m_rRedlineAccess.SetRedlineFlags_intern( eTmpMode | RedlineFlags::Ignore );
197     }
~UndoRedoRedlineGuard()198     ~UndoRedoRedlineGuard()
199     {
200         m_rRedlineAccess.SetRedlineFlags(m_eMode);
201     }
202 private:
203     IDocumentRedlineAccess & m_rRedlineAccess;
204     RedlineFlags const m_eMode;
205 };
206 
207 }
208 
Undo()209 void SwUndo::Undo()
210 {
211     assert(false); // SwUndo::Undo(): ERROR: must call UndoWithContext instead
212 }
213 
Redo()214 void SwUndo::Redo()
215 {
216     assert(false); // SwUndo::Redo(): ERROR: must call RedoWithContext instead
217 }
218 
UndoWithContext(SfxUndoContext & rContext)219 void SwUndo::UndoWithContext(SfxUndoContext & rContext)
220 {
221     ::sw::UndoRedoContext *const pContext(
222             dynamic_cast< ::sw::UndoRedoContext * >(& rContext));
223     assert(pContext);
224     const UndoRedoRedlineGuard aUndoRedoRedlineGuard(*pContext, *this);
225     UndoImpl(*pContext);
226 }
227 
RedoWithContext(SfxUndoContext & rContext)228 void SwUndo::RedoWithContext(SfxUndoContext & rContext)
229 {
230     ::sw::UndoRedoContext *const pContext(
231             dynamic_cast< ::sw::UndoRedoContext * >(& rContext));
232     assert(pContext);
233     const UndoRedoRedlineGuard aUndoRedoRedlineGuard(*pContext, *this);
234     RedoImpl(*pContext);
235 }
236 
Repeat(SfxRepeatTarget & rContext)237 void SwUndo::Repeat(SfxRepeatTarget & rContext)
238 {
239     if (m_isRepeatIgnored)
240     {
241         return; // ignore Repeat for multi-selections
242     }
243     ::sw::RepeatContext *const pRepeatContext(
244             dynamic_cast< ::sw::RepeatContext * >(& rContext));
245     assert(pRepeatContext);
246     RepeatImpl(*pRepeatContext);
247 }
248 
CanRepeat(SfxRepeatTarget & rContext) const249 bool SwUndo::CanRepeat(SfxRepeatTarget & rContext) const
250 {
251     assert(dynamic_cast< ::sw::RepeatContext * >(& rContext));
252     (void)rContext;
253     // a MultiSelection action that doesn't do anything must still return true
254     return (SwUndoId::REPEAT_START <= GetId()) && (GetId() < SwUndoId::REPEAT_END);
255 }
256 
RepeatImpl(::sw::RepeatContext &)257 void SwUndo::RepeatImpl( ::sw::RepeatContext & )
258 {
259 }
260 
GetUndoComment(SwUndoId eId)261 OUString GetUndoComment(SwUndoId eId)
262 {
263     TranslateId pId;
264     switch (eId)
265     {
266         case SwUndoId::EMPTY:
267             pId = STR_CANT_UNDO;
268             break;
269         case SwUndoId::START:
270         case SwUndoId::END:
271             break;
272         case SwUndoId::DELETE:
273             pId = STR_DELETE_UNDO;
274             break;
275         case SwUndoId::INSERT:
276             pId = STR_INSERT_UNDO;
277             break;
278         case SwUndoId::OVERWRITE:
279             pId = STR_OVR_UNDO;
280             break;
281         case SwUndoId::SPLITNODE:
282             pId = STR_SPLITNODE_UNDO;
283             break;
284         case SwUndoId::INSATTR:
285             pId = STR_INSATTR_UNDO;
286             break;
287         case SwUndoId::SETFMTCOLL:
288             pId = STR_SETFMTCOLL_UNDO;
289             break;
290         case SwUndoId::RESETATTR:
291             pId = STR_RESET_ATTR_UNDO;
292             break;
293         case SwUndoId::INSFMTATTR:
294             pId = STR_INSFMT_ATTR_UNDO;
295             break;
296         case SwUndoId::INSDOKUMENT:
297             pId = STR_INSERT_DOC_UNDO;
298             break;
299         case SwUndoId::COPY:
300             pId = STR_COPY_UNDO;
301             break;
302         case SwUndoId::INSTABLE:
303             pId = STR_INSTABLE_UNDO;
304             break;
305         case SwUndoId::TABLETOTEXT:
306             pId = STR_TABLETOTEXT_UNDO;
307             break;
308         case SwUndoId::TEXTTOTABLE:
309             pId = STR_TEXTTOTABLE_UNDO;
310             break;
311         case SwUndoId::SORT_TXT:
312             pId = STR_SORT_TXT;
313             break;
314         case SwUndoId::INSLAYFMT:
315             pId = STR_INSERTFLY;
316             break;
317         case SwUndoId::TABLEHEADLINE:
318             pId = STR_TABLEHEADLINE;
319             break;
320         case SwUndoId::INSSECTION:
321             pId = STR_INSERTSECTION;
322             break;
323         case SwUndoId::OUTLINE_LR:
324             pId = STR_OUTLINE_LR;
325             break;
326         case SwUndoId::OUTLINE_UD:
327             pId = STR_OUTLINE_UD;
328             break;
329         case SwUndoId::OUTLINE_EDIT:
330             pId = STR_OUTLINE_EDIT;
331             break;
332         case SwUndoId::INSNUM:
333             pId = STR_INSNUM;
334             break;
335         case SwUndoId::NUMUP:
336             pId = STR_NUMUP;
337             break;
338         case SwUndoId::MOVENUM:
339             pId = STR_MOVENUM;
340             break;
341         case SwUndoId::INSDRAWFMT:
342             pId = STR_INSERTDRAW;
343             break;
344         case SwUndoId::NUMORNONUM:
345             pId = STR_NUMORNONUM;
346             break;
347         case SwUndoId::INC_LEFTMARGIN:
348             pId = STR_INC_LEFTMARGIN;
349             break;
350         case SwUndoId::DEC_LEFTMARGIN:
351             pId = STR_DEC_LEFTMARGIN;
352             break;
353         case SwUndoId::INSERTLABEL:
354             pId = STR_INSERTLABEL;
355             break;
356         case SwUndoId::SETNUMRULESTART:
357             pId = STR_SETNUMRULESTART;
358             break;
359         case SwUndoId::CHGFTN:
360             pId = STR_CHANGEFTN;
361             break;
362         case SwUndoId::REDLINE:
363             SAL_INFO("sw.core", "Should NEVER be used/translated");
364             return u"$1"_ustr;
365         case SwUndoId::ACCEPT_REDLINE:
366             pId = STR_ACCEPT_REDLINE;
367             break;
368         case SwUndoId::REJECT_REDLINE:
369             pId = STR_REJECT_REDLINE;
370             break;
371         case SwUndoId::SPLIT_TABLE:
372             pId = STR_SPLIT_TABLE;
373             break;
374         case SwUndoId::DONTEXPAND:
375             pId = STR_DONTEXPAND;
376             break;
377         case SwUndoId::AUTOCORRECT:
378             pId = STR_AUTOCORRECT;
379             break;
380         case SwUndoId::MERGE_TABLE:
381             pId = STR_MERGE_TABLE;
382             break;
383         case SwUndoId::TRANSLITERATE:
384             pId = STR_TRANSLITERATE;
385             break;
386         case SwUndoId::PASTE_CLIPBOARD:
387             pId = STR_PASTE_CLIPBOARD_UNDO;
388             break;
389         case SwUndoId::TYPING:
390             pId = STR_TYPING_UNDO;
391             break;
392         case SwUndoId::MOVE:
393             pId = STR_MOVE_UNDO;
394             break;
395         case SwUndoId::INSGLOSSARY:
396             pId = STR_INSERT_GLOSSARY;
397             break;
398         case SwUndoId::DELBOOKMARK:
399             pId = STR_DELBOOKMARK;
400             break;
401         case SwUndoId::INSBOOKMARK:
402             pId = STR_INSBOOKMARK;
403             break;
404         case SwUndoId::SORT_TBL:
405             pId = STR_SORT_TBL;
406             break;
407         case SwUndoId::DELLAYFMT:
408             pId = STR_DELETEFLY;
409             break;
410         case SwUndoId::AUTOFORMAT:
411             pId = STR_AUTOFORMAT;
412             break;
413         case SwUndoId::REPLACE:
414             pId = STR_REPLACE;
415             break;
416         case SwUndoId::DELSECTION:
417             pId = STR_DELETESECTION;
418             break;
419         case SwUndoId::CHGSECTION:
420             pId = STR_CHANGESECTION;
421             break;
422         case SwUndoId::SETDEFTATTR:
423             pId = STR_CHANGEDEFATTR;
424             break;
425         case SwUndoId::DELNUM:
426             pId = STR_DELNUM;
427             break;
428         case SwUndoId::DRAWUNDO:
429             pId = STR_DRAWUNDO;
430             break;
431         case SwUndoId::DRAWGROUP:
432             pId = STR_DRAWGROUP;
433             break;
434         case SwUndoId::DRAWUNGROUP:
435             pId = STR_DRAWUNGROUP;
436             break;
437         case SwUndoId::DRAWDELETE:
438             pId = STR_DRAWDELETE;
439             break;
440         case SwUndoId::REREAD:
441             pId = STR_REREAD;
442             break;
443         case SwUndoId::DELGRF:
444             pId = STR_DELGRF;
445             break;
446         case SwUndoId::TABLE_ATTR:
447             pId = STR_TABLE_ATTR;
448             break;
449         case SwUndoId::TABLE_AUTOFMT:
450             pId = STR_UNDO_TABLE_AUTOFMT;
451             break;
452         case SwUndoId::TABLE_INSCOL:
453             pId = STR_UNDO_TABLE_INSCOL;
454             break;
455         case SwUndoId::TABLE_INSROW:
456             pId = STR_UNDO_TABLE_INSROW;
457             break;
458         case SwUndoId::TABLE_DELBOX:
459             pId = STR_UNDO_TABLE_DELBOX;
460             break;
461         case SwUndoId::TABLE_SPLIT:
462             pId = STR_UNDO_TABLE_SPLIT;
463             break;
464         case SwUndoId::TABLE_MERGE:
465             pId = STR_UNDO_TABLE_MERGE;
466             break;
467         case SwUndoId::TBLNUMFMT:
468             pId = STR_TABLE_NUMFORMAT;
469             break;
470         case SwUndoId::INSTOX:
471             pId = STR_INSERT_TOX;
472             break;
473         case SwUndoId::CLEARTOXRANGE:
474             pId = STR_CLEAR_TOX_RANGE;
475             break;
476         case SwUndoId::TBLCPYTBL:
477             pId = STR_TABLE_TBLCPYTBL;
478             break;
479         case SwUndoId::CPYTBL:
480             pId = STR_TABLE_CPYTBL;
481             break;
482         case SwUndoId::INS_FROM_SHADOWCRSR:
483             pId = STR_INS_FROM_SHADOWCRSR;
484             break;
485         case SwUndoId::CHAINE:
486             pId = STR_UNDO_CHAIN;
487             break;
488         case SwUndoId::UNCHAIN:
489             pId = STR_UNDO_UNCHAIN;
490             break;
491         case SwUndoId::FTNINFO:
492             pId = STR_UNDO_FTNINFO;
493             break;
494         case SwUndoId::COMPAREDOC:
495             pId = STR_UNDO_COMPAREDOC;
496             break;
497         case SwUndoId::SETFLYFRMFMT:
498             pId = STR_UNDO_SETFLYFRMFMT;
499             break;
500         case SwUndoId::SETRUBYATTR:
501             pId = STR_UNDO_SETRUBYATTR;
502             break;
503         case SwUndoId::TOXCHANGE:
504             pId = STR_TOXCHANGE;
505             break;
506         case SwUndoId::CREATE_PAGEDESC:
507             pId = STR_UNDO_PAGEDESC_CREATE;
508             break;
509         case SwUndoId::CHANGE_PAGEDESC:
510             pId = STR_UNDO_PAGEDESC;
511             break;
512         case SwUndoId::DELETE_PAGEDESC:
513             pId = STR_UNDO_PAGEDESC_DELETE;
514             break;
515         case SwUndoId::HEADER_FOOTER:
516             pId = STR_UNDO_HEADER_FOOTER;
517             break;
518         case SwUndoId::FIELD:
519             pId = STR_UNDO_FIELD;
520             break;
521         case SwUndoId::TXTFMTCOL_CREATE:
522             pId = STR_UNDO_TXTFMTCOL_CREATE;
523             break;
524         case SwUndoId::TXTFMTCOL_DELETE:
525             pId = STR_UNDO_TXTFMTCOL_DELETE;
526             break;
527         case SwUndoId::TXTFMTCOL_RENAME:
528             pId = STR_UNDO_TXTFMTCOL_RENAME;
529             break;
530         case SwUndoId::CHARFMT_CREATE:
531             pId = STR_UNDO_CHARFMT_CREATE;
532             break;
533         case SwUndoId::CHARFMT_DELETE:
534             pId = STR_UNDO_CHARFMT_DELETE;
535             break;
536         case SwUndoId::CHARFMT_RENAME:
537             pId = STR_UNDO_CHARFMT_RENAME;
538             break;
539         case SwUndoId::FRMFMT_CREATE:
540             pId = STR_UNDO_FRMFMT_CREATE;
541             break;
542         case SwUndoId::FRMFMT_DELETE:
543             pId = STR_UNDO_FRMFMT_DELETE;
544             break;
545         case SwUndoId::FRMFMT_RENAME:
546             pId = STR_UNDO_FRMFMT_RENAME;
547             break;
548         case SwUndoId::NUMRULE_CREATE:
549             pId = STR_UNDO_NUMRULE_CREATE;
550             break;
551         case SwUndoId::NUMRULE_DELETE:
552             pId = STR_UNDO_NUMRULE_DELETE;
553             break;
554         case SwUndoId::NUMRULE_RENAME:
555             pId = STR_UNDO_NUMRULE_RENAME;
556             break;
557         case SwUndoId::BOOKMARK_RENAME:
558             pId = STR_UNDO_BOOKMARK_RENAME;
559             break;
560         case SwUndoId::INDEX_ENTRY_INSERT:
561             pId = STR_UNDO_INDEX_ENTRY_INSERT;
562             break;
563         case SwUndoId::INDEX_ENTRY_DELETE:
564             pId = STR_UNDO_INDEX_ENTRY_DELETE;
565             break;
566         case SwUndoId::COL_DELETE:
567             pId = STR_UNDO_COL_DELETE;
568             break;
569         case SwUndoId::ROW_DELETE:
570             pId = STR_UNDO_ROW_DELETE;
571             break;
572         case SwUndoId::RENAME_PAGEDESC:
573             pId = STR_UNDO_PAGEDESC_RENAME;
574             break;
575         case SwUndoId::NUMDOWN:
576             pId = STR_NUMDOWN;
577             break;
578         case SwUndoId::FLYFRMFMT_TITLE:
579             pId = STR_UNDO_FLYFRMFMT_TITLE;
580             break;
581         case SwUndoId::FLYFRMFMT_DESCRIPTION:
582             pId = STR_UNDO_FLYFRMFMT_DESCRIPTION;
583             break;
584         case SwUndoId::TBLSTYLE_CREATE:
585             pId = STR_UNDO_TBLSTYLE_CREATE;
586             break;
587         case SwUndoId::TBLSTYLE_DELETE:
588             pId = STR_UNDO_TBLSTYLE_DELETE;
589             break;
590         case SwUndoId::TBLSTYLE_UPDATE:
591             pId = STR_UNDO_TBLSTYLE_UPDATE;
592             break;
593         case SwUndoId::UI_REPLACE:
594             pId = STR_REPLACE_UNDO;
595             break;
596         case SwUndoId::UI_INSERT_PAGE_BREAK:
597             pId = STR_INSERT_PAGE_BREAK_UNDO;
598             break;
599         case SwUndoId::UI_INSERT_COLUMN_BREAK:
600             pId = STR_INSERT_COLUMN_BREAK_UNDO;
601             break;
602         case SwUndoId::UI_INSERT_ENVELOPE:
603             pId = STR_INSERT_ENV_UNDO;
604             break;
605         case SwUndoId::UI_DRAG_AND_COPY:
606             pId = STR_DRAG_AND_COPY;
607             break;
608         case SwUndoId::UI_DRAG_AND_MOVE:
609             pId = STR_DRAG_AND_MOVE;
610             break;
611         case SwUndoId::UI_INSERT_CHART:
612             pId = STR_INSERT_CHART;
613             break;
614         case SwUndoId::UI_INSERT_FOOTNOTE:
615             pId = STR_INSERT_FOOTNOTE;
616             break;
617         case SwUndoId::UI_INSERT_URLBTN:
618             pId = STR_INSERT_URLBTN;
619             break;
620         case SwUndoId::UI_INSERT_URLTXT:
621             pId = STR_INSERT_URLTXT;
622             break;
623         case SwUndoId::UI_DELETE_INVISIBLECNTNT:
624             pId = STR_DELETE_INVISIBLECNTNT;
625             break;
626         case SwUndoId::UI_REPLACE_STYLE:
627             pId = STR_REPLACE_STYLE;
628             break;
629         case SwUndoId::UI_DELETE_PAGE_BREAK:
630             pId = STR_DELETE_PAGE_BREAK;
631             break;
632         case SwUndoId::UI_TEXT_CORRECTION:
633             pId = STR_TEXT_CORRECTION;
634             break;
635         case SwUndoId::UI_TABLE_DELETE:
636             pId = STR_UNDO_TABLE_DELETE;
637             break;
638         case SwUndoId::CONFLICT:
639             break;
640         case SwUndoId::PARA_SIGN_ADD:
641             pId = STR_PARAGRAPH_SIGN_UNDO;
642             break;
643         case SwUndoId::INSERT_FORM_FIELD:
644             pId = STR_UNDO_INSERT_FORM_FIELD;
645             break;
646         case SwUndoId::INSERT_PAGE_NUMBER:
647             pId = STR_UNDO_INSERT_PAGE_NUMBER;
648             break;
649         case SwUndoId::UPDATE_FORM_FIELD:
650             pId = STR_UNDO_UPDATE_FORM_FIELD;
651             break;
652         case SwUndoId::UPDATE_FORM_FIELDS:
653             pId = STR_UNDO_UPDATE_FORM_FIELDS;
654             break;
655         case SwUndoId::DELETE_FORM_FIELDS:
656             pId = STR_UNDO_DELETE_FORM_FIELDS;
657             break;
658         case SwUndoId::UPDATE_BOOKMARK:
659             pId = STR_UPDATE_BOOKMARK;
660             break;
661         case SwUndoId::UPDATE_BOOKMARKS:
662             pId = STR_UPDATE_BOOKMARKS;
663             break;
664         case SwUndoId::DELETE_BOOKMARKS:
665             pId = STR_DELETE_BOOKMARKS;
666             break;
667         case SwUndoId::UPDATE_FIELD:
668             pId = STR_UPDATE_FIELD;
669             break;
670         case SwUndoId::UPDATE_FIELDS:
671             pId = STR_UPDATE_FIELDS;
672             break;
673         case SwUndoId::DELETE_FIELDS:
674             pId = STR_DELETE_FIELDS;
675             break;
676         case SwUndoId::UPDATE_SECTIONS:
677             pId = STR_UPDATE_SECTIONS;
678             break;
679         case SwUndoId::CHANGE_THEME:
680             pId = STR_UNDO_CHANGE_THEME_COLORS;
681             break;
682         case SwUndoId::DELETE_SECTIONS:
683             pId = STR_DELETE_SECTIONS;
684             break;
685         case SwUndoId::FLYFRMFMT_DECORATIVE:
686             pId = STR_UNDO_FLYFRMFMT_DECORATIVE;
687             break;
688     }
689 
690     assert(pId);
691     return SwResId(pId);
692 }
693 
GetComment() const694 OUString SwUndo::GetComment() const
695 {
696     OUString aResult;
697 
698     if (m_bCacheComment)
699     {
700         if (! maComment)
701         {
702             maComment = GetUndoComment(GetId());
703 
704             SwRewriter aRewriter = GetRewriter();
705 
706             maComment = aRewriter.Apply(*maComment);
707         }
708 
709         aResult = *maComment;
710     }
711     else
712     {
713         aResult = GetUndoComment(GetId());
714 
715         SwRewriter aRewriter = GetRewriter();
716 
717         aResult = aRewriter.Apply(aResult);
718     }
719 
720     return aResult;
721 }
722 
GetViewShellId() const723 ViewShellId SwUndo::GetViewShellId() const
724 {
725     return m_nViewShellId;
726 }
727 
GetRewriter() const728 SwRewriter SwUndo::GetRewriter() const
729 {
730     SwRewriter aResult;
731 
732     return aResult;
733 }
734 
SwUndoSaveContent()735 SwUndoSaveContent::SwUndoSaveContent()
736 {}
737 
~SwUndoSaveContent()738 SwUndoSaveContent::~SwUndoSaveContent() COVERITY_NOEXCEPT_FALSE
739 {
740 }
741 
dumpAsXml(xmlTextWriterPtr pWriter) const742 void SwUndoSaveContent::dumpAsXml(xmlTextWriterPtr pWriter) const
743 {
744     (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwUndoSaveContent"));
745     (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this);
746 
747     if (m_pHistory)
748     {
749         m_pHistory->dumpAsXml(pWriter);
750     }
751 
752     (void)xmlTextWriterEndElement(pWriter);
753 }
754 
755 // This is needed when deleting content. For REDO all contents will be moved
756 // into the UndoNodesArray. These methods always create a new node to insert
757 // content. As a result, the attributes will not be expanded.
758 // - MoveTo   moves from NodesArray into UndoNodesArray
759 // - MoveFrom moves from UndoNodesArray into NodesArray
760 
761 // If pEndNdIdx is given, Undo/Redo calls -Ins/DelFly. In that case the whole
762 // section should be moved.
MoveToUndoNds(SwPaM & rPaM,SwNodeIndex * pNodeIdx,SwNodeOffset * pEndNdIdx)763 void SwUndoSaveContent::MoveToUndoNds( SwPaM& rPaM, SwNodeIndex* pNodeIdx,
764                     SwNodeOffset* pEndNdIdx )
765 {
766     SwDoc& rDoc = rPaM.GetDoc();
767     ::sw::UndoGuard const undoGuard(rDoc.GetIDocumentUndoRedo());
768 
769     SwNoTextNode* pCpyNd = rPaM.GetPointNode().GetNoTextNode();
770 
771     // here comes the actual delete (move)
772     SwNodes & rNds = rDoc.GetUndoManager().GetUndoNodes();
773     SwPosition aPos( pEndNdIdx ? rNds.GetEndOfPostIts()
774                                : rNds.GetEndOfExtras() );
775 
776     const SwPosition* pStt = rPaM.Start(), *pEnd = rPaM.End();
777 
778     SwNodeOffset nTmpMvNode = aPos.GetNodeIndex();
779 
780     if( pCpyNd || pEndNdIdx )
781     {
782         SwNodeRange aRg( pStt->GetNode(), SwNodeOffset(0), pEnd->GetNode(), SwNodeOffset(1) );
783         rDoc.GetNodes().MoveNodes( aRg, rNds, aPos.GetNode(), true );
784         aPos.Adjust(SwNodeOffset(-1));
785     }
786     else
787     {
788         rDoc.GetNodes().MoveRange( rPaM, aPos, rNds );
789     }
790     if( pEndNdIdx )
791         *pEndNdIdx = aPos.GetNodeIndex();
792 
793     // old position
794     aPos.Assign(nTmpMvNode);
795     if( pNodeIdx )
796         *pNodeIdx = aPos.GetNode();
797 }
798 
MoveFromUndoNds(SwDoc & rDoc,SwNodeOffset nNodeIdx,SwPosition & rInsPos,const SwNodeOffset * pEndNdIdx,bool const bForceCreateFrames)799 void SwUndoSaveContent::MoveFromUndoNds( SwDoc& rDoc, SwNodeOffset nNodeIdx,
800                             SwPosition& rInsPos,
801             const SwNodeOffset* pEndNdIdx, bool const bForceCreateFrames)
802 {
803     // here comes the recovery
804     SwNodes & rNds = rDoc.GetUndoManager().GetUndoNodes();
805     if( nNodeIdx == rNds.GetEndOfPostIts().GetIndex() )
806         return;     // nothing saved
807 
808     ::sw::UndoGuard const undoGuard(rDoc.GetIDocumentUndoRedo());
809 
810     SwPaM aPaM( rInsPos );
811     if( pEndNdIdx )         // than get the section from it
812         aPaM.GetPoint()->Assign( *rNds[SwNodeOffset(0)], *pEndNdIdx );
813     else
814     {
815         aPaM.GetPoint()->Assign( rNds.GetEndOfExtras() );
816         GoInContent( aPaM, fnMoveBackward );
817     }
818 
819     SwTextNode* pTextNd = aPaM.GetPointNode().GetTextNode();
820     if (!pEndNdIdx && pTextNd)
821     {
822         aPaM.SetMark();
823         aPaM.GetPoint()->Assign(nNodeIdx, 0);
824 
825         SaveRedlEndPosForRestore aRedlRest( rInsPos.GetNode(), rInsPos.GetContentIndex() );
826 
827         rNds.MoveRange( aPaM, rInsPos, rDoc.GetNodes() );
828 
829         // delete the last Node as well
830         bool bDeleteLastNode = false;
831         if( !aPaM.GetPoint()->GetContentIndex() )
832             bDeleteLastNode = true;
833         else
834         {
835             // still empty Nodes at the end?
836             aPaM.GetPoint()->Adjust(SwNodeOffset(1));
837             if ( &rNds.GetEndOfExtras() != &aPaM.GetPoint()->GetNode() )
838                 bDeleteLastNode = true;
839         }
840         if( bDeleteLastNode )
841         {
842             SwNode& rDelNode = aPaM.GetPoint()->GetNode();
843             SwNodeOffset nDelOffset = rNds.GetEndOfExtras().GetIndex() -
844                         aPaM.GetPoint()->GetNodeIndex();
845             //move it so we don't have SwContentIndex pointing at a node when it is deleted.
846             aPaM.GetPoint()->Adjust(SwNodeOffset(-1));
847             aPaM.SetMark();
848             rNds.Delete( rDelNode, nDelOffset );
849         }
850 
851         aRedlRest.Restore();
852     }
853     else
854     {
855         SwNodeRange aRg( rNds, nNodeIdx, (pEndNdIdx
856                         ? ((*pEndNdIdx) + 1)
857                         : rNds.GetEndOfExtras().GetIndex() ) );
858         rNds.MoveNodes(aRg, rDoc.GetNodes(), rInsPos.GetNode(), nullptr == pEndNdIdx || bForceCreateFrames);
859 
860     }
861 }
862 
863 // These two methods save and restore the Point of PaM.
864 // If the point cannot be moved, a "backup" is created on the previous node.
865 // Either way, returned, inserting at its original position will not move it.
MovePtBackward(SwPaM & rPam)866 ::std::optional<SwNodeIndex> SwUndoSaveContent::MovePtBackward(SwPaM & rPam)
867 {
868     rPam.SetMark();
869     if( rPam.Move( fnMoveBackward ))
870         return {};
871 
872     return { SwNodeIndex(rPam.GetPoint()->GetNode(), -1) };
873 }
874 
MovePtForward(SwPaM & rPam,::std::optional<SwNodeIndex> && oMvBkwrd)875 void SwUndoSaveContent::MovePtForward(SwPaM& rPam, ::std::optional<SwNodeIndex> && oMvBkwrd)
876 {
877     // Was there content before this position?
878     if (!oMvBkwrd)
879         rPam.Move( fnMoveForward );
880     else
881     {
882         *rPam.GetPoint() = SwPosition(*oMvBkwrd);
883         rPam.GetPoint()->Adjust(SwNodeOffset(1));
884         SwContentNode* pCNd = rPam.GetPointContentNode();
885         if( !pCNd )
886             rPam.Move( fnMoveForward );
887     }
888 }
889 
890 // Delete all objects that have ContentIndices to the given area.
891 // Currently (1994) these exist:
892 //                  - Footnotes
893 //                  - Flys
894 //                  - Bookmarks
895 
896 // #i81002# - extending method
897 // delete certain (not all) cross-reference bookmarks at text node of <rMark>
898 // and at text node of <rPoint>, if these text nodes aren't the same.
DelContentIndex(const SwPosition & rMark,const SwPosition & rPoint,DelContentType nDelContentType)899 void SwUndoSaveContent::DelContentIndex( const SwPosition& rMark,
900                                      const SwPosition& rPoint,
901                                      DelContentType nDelContentType )
902 {
903     const SwPosition *pStt = rMark < rPoint ? &rMark : &rPoint,
904                     *pEnd = &rMark == pStt ? &rPoint : &rMark;
905 
906     SwDoc& rDoc = rMark.GetNode().GetDoc();
907 
908     // if it's not in the doc array, probably missing some invalidation somewhere
909     assert(&rPoint.GetNodes() == &rDoc.GetNodes());
910     assert(&rMark.GetNodes() == &rDoc.GetNodes());
911 
912     ::sw::UndoGuard const undoGuard(rDoc.GetIDocumentUndoRedo());
913 
914     // 1. Footnotes
915     if( DelContentType::Ftn & nDelContentType )
916     {
917         SwFootnoteIdxs& rFootnoteArr = rDoc.GetFootnoteIdxs();
918         if( !rFootnoteArr.empty() )
919         {
920             const SwNode* pFootnoteNd;
921             size_t nPos = 0;
922             rFootnoteArr.SeekEntry( pStt->GetNode(), &nPos );
923             SwTextFootnote* pSrch;
924 
925             // for now delete all that come afterwards
926             while( nPos < rFootnoteArr.size() && ( pFootnoteNd =
927                 &( pSrch = rFootnoteArr[ nPos ] )->GetTextNode())->GetIndex()
928                         <= pEnd->GetNodeIndex() )
929             {
930                 const sal_Int32 nFootnoteSttIdx = pSrch->GetStart();
931                 if( (DelContentType::CheckNoCntnt & nDelContentType )
932                     ? (&pEnd->GetNode() == pFootnoteNd )
933                     : (( &pStt->GetNode() == pFootnoteNd &&
934                     pStt->GetContentIndex() > nFootnoteSttIdx) ||
935                     ( &pEnd->GetNode() == pFootnoteNd &&
936                     nFootnoteSttIdx >= pEnd->GetContentIndex() )) )
937                 {
938                     ++nPos;     // continue searching
939                     continue;
940                 }
941 
942 // FIXME: duplicated code here and below -> refactor?
943                 // Unfortunately an index needs to be created. Otherwise there
944                 // will be problems with TextNode because the index will be
945                 // deleted in the DTOR of SwFootnote!
946                 SwTextNode* pTextNd = const_cast<SwTextNode*>(static_cast<const SwTextNode*>(pFootnoteNd));
947                 if( !m_pHistory )
948                     m_pHistory.reset( new SwHistory );
949                 SwTextAttr* const pFootnoteHint =
950                     pTextNd->GetTextAttrForCharAt( nFootnoteSttIdx );
951                 assert(pFootnoteHint);
952                 SwContentIndex aIdx( pTextNd, nFootnoteSttIdx );
953                 m_pHistory->AddTextAttr(pFootnoteHint, pTextNd->GetIndex(), false);
954                 pTextNd->EraseText( aIdx, 1 );
955             }
956 
957             while( nPos-- && ( pFootnoteNd = &( pSrch = rFootnoteArr[ nPos ] )->
958                     GetTextNode())->GetIndex() >= pStt->GetNodeIndex() )
959             {
960                 const sal_Int32 nFootnoteSttIdx = pSrch->GetStart();
961                 if( !(DelContentType::CheckNoCntnt & nDelContentType) && (
962                     ( &pStt->GetNode() == pFootnoteNd &&
963                     pStt->GetContentIndex() > nFootnoteSttIdx ) ||
964                     ( &pEnd->GetNode() == pFootnoteNd &&
965                     nFootnoteSttIdx >= pEnd->GetContentIndex() )))
966                     continue;               // continue searching
967 
968                 // Unfortunately an index needs to be created. Otherwise there
969                 // will be problems with TextNode because the index will be
970                 // deleted in the DTOR of SwFootnote!
971                 SwTextNode* pTextNd = const_cast<SwTextNode*>(static_cast<const SwTextNode*>(pFootnoteNd));
972                 if( !m_pHistory )
973                     m_pHistory.reset( new SwHistory );
974                 SwTextAttr* const pFootnoteHint =
975                     pTextNd->GetTextAttrForCharAt( nFootnoteSttIdx );
976                 assert(pFootnoteHint);
977                 SwContentIndex aIdx( pTextNd, nFootnoteSttIdx );
978                 m_pHistory->AddTextAttr(pFootnoteHint, pTextNd->GetIndex(), false);
979                 pTextNd->EraseText( aIdx, 1 );
980             }
981         }
982     }
983 
984     // 2. Flys
985     if( DelContentType::Fly & nDelContentType )
986     {
987         sal_uInt16 nChainInsPos = m_pHistory ? m_pHistory->Count() : 0;
988         const sw::SpzFrameFormats& rSpzArr = *rDoc.GetSpzFrameFormats();
989         if( !rSpzArr.empty() )
990         {
991             sw::SpzFrameFormat* pFormat;
992             const SwFormatAnchor* pAnchor;
993             size_t n = rSpzArr.size();
994             const SwPosition* pAPos;
995 
996             while( n && !rSpzArr.empty() )
997             {
998                 pFormat = rSpzArr[--n];
999                 pAnchor = &pFormat->GetAnchor();
1000                 switch( pAnchor->GetAnchorId() )
1001                 {
1002                 case RndStdIds::FLY_AS_CHAR:
1003                     if( nullptr != (pAPos = pAnchor->GetContentAnchor() ) &&
1004                         (( DelContentType::CheckNoCntnt & nDelContentType )
1005                         ? ( pStt->GetNode() <= pAPos->GetNode() &&
1006                             pAPos->GetNode() < pEnd->GetNode() )
1007                         : ( *pStt <= *pAPos && *pAPos < *pEnd )) )
1008                     {
1009                         if( !m_pHistory )
1010                             m_pHistory.reset( new SwHistory );
1011                         SwTextNode *const pTextNd =
1012                             pAPos->GetNode().GetTextNode();
1013                         SwTextAttr* const pFlyHint = pTextNd->GetTextAttrForCharAt(
1014                             pAPos->GetContentIndex());
1015                         assert(pFlyHint);
1016                         m_pHistory->AddTextAttr(pFlyHint, SwNodeOffset(0), false);
1017                         // reset n so that no Format is skipped
1018                         n = n >= rSpzArr.size() ? rSpzArr.size() : n+1;
1019                     }
1020                     break;
1021                 case RndStdIds::FLY_AT_PARA:
1022                     {
1023                         pAPos =  pAnchor->GetContentAnchor();
1024                         if (pAPos &&
1025                             pStt->GetNode() <= pAPos->GetNode() && pAPos->GetNode() <= pEnd->GetNode())
1026                         {
1027                             if (!m_pHistory)
1028                                 m_pHistory.reset( new SwHistory );
1029 
1030                             if (!(DelContentType::Replace & nDelContentType)
1031                                 && IsSelectFrameAnchoredAtPara(*pAPos, *pStt, *pEnd, nDelContentType))
1032                             {
1033                                 m_pHistory->AddDeleteFly(*pFormat, nChainInsPos);
1034                                 // reset n so that no Format is skipped
1035                                 n = n >= rSpzArr.size() ? rSpzArr.size() : n+1;
1036                             }
1037                             // Moving the anchor?
1038                             else if (!((DelContentType::CheckNoCntnt|DelContentType::ExcludeFlyAtStartEnd)
1039                                     & nDelContentType) &&
1040                                 // for SwUndoDelete: rPoint is the node that
1041                                 // will be Joined - so anchor should be moved
1042                                 // off it - but UndoImpl() split will insert
1043                                 // new node *before* existing one so a no-op
1044                                 // may need to be done here to add it to
1045                                 // history for Undo.
1046                                 (rPoint.GetNodeIndex() == pAPos->GetNodeIndex()
1047                                  || pStt->GetNodeIndex() == pAPos->GetNodeIndex())
1048                                 // Do not try to move the anchor to a table!
1049                                 && rMark.GetNode().IsTextNode())
1050                             {
1051                                 m_pHistory->AddChangeFlyAnchor(*pFormat);
1052                                 SwFormatAnchor aAnch( *pAnchor );
1053                                 SwPosition aPos( rMark.GetNode() );
1054                                 aAnch.SetAnchor( &aPos );
1055                                 pFormat->SetFormatAttr( aAnch );
1056                             }
1057                         }
1058                     }
1059                     break;
1060                 case RndStdIds::FLY_AT_CHAR:
1061                     if( nullptr != (pAPos = pAnchor->GetContentAnchor() ) &&
1062                         ( pStt->GetNode() <= pAPos->GetNode() && pAPos->GetNode() <= pEnd->GetNode() ) )
1063                     {
1064                         if( !m_pHistory )
1065                             m_pHistory.reset( new SwHistory );
1066                         if (!(DelContentType::Replace & nDelContentType)
1067                             && IsDestroyFrameAnchoredAtChar(
1068                                 *pAPos, *pStt, *pEnd, nDelContentType))
1069                         {
1070                             m_pHistory->AddDeleteFly(*pFormat, nChainInsPos);
1071                             n = n >= rSpzArr.size() ? rSpzArr.size() : n+1;
1072                         }
1073                         else if (!((DelContentType::CheckNoCntnt |
1074                                     DelContentType::ExcludeFlyAtStartEnd)
1075                                     & nDelContentType))
1076                         {
1077                             if( *pStt <= *pAPos && *pAPos < *pEnd )
1078                             {
1079                                 // These are the objects anchored
1080                                 // between section start and end position
1081                                 // Do not try to move the anchor to a table!
1082                                 if( rMark.GetNode().GetTextNode() )
1083                                 {
1084                                     m_pHistory->AddChangeFlyAnchor(*pFormat);
1085                                     SwFormatAnchor aAnch( *pAnchor );
1086                                     aAnch.SetAnchor( &rMark );
1087                                     pFormat->SetFormatAttr( aAnch );
1088                                 }
1089                             }
1090                         }
1091                     }
1092                     break;
1093                 case RndStdIds::FLY_AT_FLY:
1094 
1095                     if( nullptr != (pAPos = pAnchor->GetContentAnchor() ) &&
1096                         pStt->GetNode() == pAPos->GetNode() )
1097                     {
1098                         if( !m_pHistory )
1099                             m_pHistory.reset( new SwHistory );
1100 
1101                         m_pHistory->AddDeleteFly(*pFormat, nChainInsPos);
1102 
1103                         // reset n so that no Format is skipped
1104                         n = n >= rSpzArr.size() ? rSpzArr.size() : n+1;
1105                     }
1106                     break;
1107                 default: break;
1108                 }
1109             }
1110         }
1111     }
1112 
1113     // 3. Bookmarks
1114     if( !(DelContentType::Bkm & nDelContentType) )
1115         return;
1116 
1117     IDocumentMarkAccess* const pMarkAccess = rDoc.getIDocumentMarkAccess();
1118     if( !pMarkAccess->getAllMarksCount() )
1119         return;
1120 
1121     for( sal_Int32 n = 0; n < pMarkAccess->getAllMarksCount(); ++n )
1122     {
1123         // #i81002#
1124         bool bSavePos = false;
1125         bool bSaveOtherPos = false;
1126         bool bDelete = false;
1127         const ::sw::mark::IMark *const pBkmk = pMarkAccess->getAllMarksBegin()[n];
1128         auto const type(IDocumentMarkAccess::GetType(*pBkmk));
1129 
1130         if( DelContentType::CheckNoCntnt & nDelContentType )
1131         {
1132             if ( pStt->GetNode() <= pBkmk->GetMarkPos().GetNode()
1133                  && pBkmk->GetMarkPos().GetNode() < pEnd->GetNode() )
1134             {
1135                 bSavePos = true;
1136             }
1137             if ( pBkmk->IsExpanded()
1138                  && pStt->GetNode() <= pBkmk->GetOtherMarkPos().GetNode()
1139                  && pBkmk->GetOtherMarkPos().GetNode() < pEnd->GetNode() )
1140             {
1141                 bSaveOtherPos = true;
1142             }
1143             bDelete = bSavePos && bSaveOtherPos;
1144         }
1145         else
1146         {
1147             // #i92125#
1148             // keep cross-reference bookmarks, if content inside one paragraph is deleted.
1149             if ( rMark.GetNode() == rPoint.GetNode()
1150                 && (   type == IDocumentMarkAccess::MarkType::CROSSREF_HEADING_BOOKMARK
1151                     || type == IDocumentMarkAccess::MarkType::CROSSREF_NUMITEM_BOOKMARK))
1152             {
1153                 continue;
1154             }
1155 
1156             bool bMaybe = false;
1157             if ( *pStt <= pBkmk->GetMarkPos() && pBkmk->GetMarkPos() <= *pEnd )
1158             {
1159                 if ( pBkmk->GetMarkPos() == *pEnd
1160                      || ( *pStt == pBkmk->GetMarkPos() && pBkmk->IsExpanded() ) )
1161                     bMaybe = true;
1162                 else
1163                     bSavePos = true;
1164             }
1165             if( pBkmk->IsExpanded() &&
1166                 *pStt <= pBkmk->GetOtherMarkPos() && pBkmk->GetOtherMarkPos() <= *pEnd )
1167             {
1168                 assert(!bSaveOtherPos);
1169                 if (   bSavePos
1170                     || (*pStt < pBkmk->GetOtherMarkPos() && pBkmk->GetOtherMarkPos() < *pEnd)
1171                     || (bMaybe
1172                         && (   type == IDocumentMarkAccess::MarkType::TEXT_FIELDMARK
1173                             || type == IDocumentMarkAccess::MarkType::CHECKBOX_FIELDMARK
1174                             || type == IDocumentMarkAccess::MarkType::DROPDOWN_FIELDMARK
1175                             || type == IDocumentMarkAccess::MarkType::DATE_FIELDMARK))
1176                     || (bMaybe
1177                         && !(nDelContentType & DelContentType::Replace)
1178                         && type == IDocumentMarkAccess::MarkType::BOOKMARK
1179                         && pStt->GetContentIndex() == 0 // entire paragraph deleted?
1180                         && pEnd->GetContentIndex() == pEnd->GetNode().GetTextNode()->Len()))
1181                 {
1182                     if( bMaybe )
1183                         bSavePos = true;
1184                     bDelete = true;
1185                 }
1186                 if (bDelete || pBkmk->GetOtherMarkPos() == *pEnd)
1187                 {
1188                     bSaveOtherPos = true; // tdf#148389 always undo if at end
1189                 }
1190             }
1191             if (!bSavePos && bMaybe && pBkmk->IsExpanded() && *pStt == pBkmk->GetMarkPos())
1192             {
1193                 bSavePos = true; // tdf#148389 always undo if at start
1194             }
1195 
1196             if ( !bSavePos && !bSaveOtherPos
1197                  && dynamic_cast< const ::sw::mark::CrossRefBookmark* >(pBkmk) )
1198             {
1199                 // certain special handling for cross-reference bookmarks
1200                 const bool bDifferentTextNodesAtMarkAndPoint =
1201                     rMark.GetNode() != rPoint.GetNode()
1202                     && rMark.GetNode().GetTextNode()
1203                     && rPoint.GetNode().GetTextNode();
1204                 if ( bDifferentTextNodesAtMarkAndPoint )
1205                 {
1206                     // delete cross-reference bookmark at <pStt>, if only part of
1207                     // <pEnd> text node content is deleted.
1208                     if( pStt->GetNode() == pBkmk->GetMarkPos().GetNode()
1209                         && pEnd->GetContentIndex() != pEnd->GetNode().GetTextNode()->Len() )
1210                     {
1211                         bSavePos = true;
1212                         bSaveOtherPos = false; // cross-reference bookmarks are not expanded
1213                     }
1214                     // delete cross-reference bookmark at <pEnd>, if only part of
1215                     // <pStt> text node content is deleted.
1216                     else if( pEnd->GetNode() == pBkmk->GetMarkPos().GetNode() &&
1217                         pStt->GetContentIndex() != 0 )
1218                     {
1219                         bSavePos = true;
1220                         bSaveOtherPos = false; // cross-reference bookmarks are not expanded
1221                     }
1222                 }
1223             }
1224             else if (type == IDocumentMarkAccess::MarkType::ANNOTATIONMARK)
1225             {
1226                 // delete annotation marks, if its end position is covered by the deletion
1227                 const SwPosition& rAnnotationEndPos = pBkmk->GetMarkEnd();
1228                 if ( *pStt < rAnnotationEndPos && rAnnotationEndPos <= *pEnd )
1229                 {
1230                     bSavePos = true;
1231                     bSaveOtherPos = pBkmk->IsExpanded(); //tdf#90138, only save the other pos if there is one
1232                     bDelete = true;
1233                 }
1234             }
1235         }
1236 
1237         if ( bSavePos || bSaveOtherPos )
1238         {
1239             if (type != IDocumentMarkAccess::MarkType::UNO_BOOKMARK)
1240             {
1241                 if( !m_pHistory )
1242                     m_pHistory.reset( new SwHistory );
1243                 m_pHistory->AddIMark(*pBkmk, bSavePos, bSaveOtherPos);
1244             }
1245             if ( bSavePos
1246                  && (bDelete || !pBkmk->IsExpanded()))
1247             {
1248                 pMarkAccess->deleteMark(pMarkAccess->getAllMarksBegin()+n, false);
1249                 n--;
1250             }
1251         }
1252     }
1253 }
1254 
1255 // save a complete section into UndoNodes array
SwUndoSaveSection()1256 SwUndoSaveSection::SwUndoSaveSection()
1257     : m_nMoveLen( 0 ), m_nStartPos( NODE_OFFSET_MAX )
1258 {
1259 }
1260 
~SwUndoSaveSection()1261 SwUndoSaveSection::~SwUndoSaveSection()
1262 {
1263     if (m_oMovedStart) // delete also the section from UndoNodes array
1264     {
1265         // SaveSection saves the content in the PostIt section.
1266         SwNodes& rUNds = m_oMovedStart->GetNode().GetNodes();
1267         // cid#1486004 Uncaught exception
1268         suppress_fun_call_w_exception(rUNds.Delete(*m_oMovedStart, m_nMoveLen));
1269 
1270         m_oMovedStart.reset();
1271     }
1272     m_pRedlineSaveData.reset();
1273 }
1274 
SaveSection(const SwNodeIndex & rSttIdx)1275 void SwUndoSaveSection::SaveSection( const SwNodeIndex& rSttIdx )
1276 {
1277     SwNodeRange aRg( rSttIdx.GetNode(), *rSttIdx.GetNode().EndOfSectionNode() );
1278     SaveSection( aRg );
1279 }
1280 
SaveSection(const SwNodeRange & rRange,bool const bExpandNodes)1281 void SwUndoSaveSection::SaveSection(
1282     const SwNodeRange& rRange, bool const bExpandNodes)
1283 {
1284     SwPaM aPam( rRange.aStart, rRange.aEnd );
1285 
1286     // delete all footnotes, fly frames, bookmarks
1287     DelContentIndex( *aPam.GetMark(), *aPam.GetPoint() );
1288 
1289     // redlines *before* CorrAbs, because DelBookmarks will make them 0-length
1290     // but *after* DelContentIndex because that also may use FillSaveData (in
1291     // flys) and that will be restored *after* this one...
1292     m_pRedlineSaveData.reset( new SwRedlineSaveDatas );
1293     if (!SwUndo::FillSaveData( aPam, *m_pRedlineSaveData ))
1294     {
1295         m_pRedlineSaveData.reset();
1296     }
1297 
1298     {
1299         // move certain indexes out of deleted range
1300         SwNodeIndex aSttIdx( aPam.Start()->GetNode() );
1301         SwNodeIndex aEndIdx( aPam.End()->GetNode() );
1302         SwNodeIndex aMvStt( aEndIdx, 1 );
1303         SwDoc::CorrAbs( aSttIdx, aEndIdx, SwPosition( aMvStt ), true );
1304     }
1305 
1306     m_nStartPos = rRange.aStart.GetIndex();
1307 
1308     if (bExpandNodes)
1309     {
1310         aPam.GetPoint()->Adjust(SwNodeOffset(-1));
1311         aPam.GetMark()->Adjust(SwNodeOffset(+1));
1312     }
1313 
1314     SwContentNode* pCNd = aPam.GetMarkContentNode();
1315     if( pCNd )
1316         aPam.GetMark()->SetContent( 0 );
1317     pCNd = aPam.GetPointContentNode();
1318     if( nullptr != pCNd )
1319         aPam.GetPoint()->SetContent( pCNd->Len() );
1320 
1321     // Keep positions as SwContentIndex so that this section can be deleted in DTOR
1322     SwNodeOffset nEnd;
1323     m_oMovedStart = rRange.aStart;
1324     MoveToUndoNds(aPam, &*m_oMovedStart, &nEnd);
1325     m_nMoveLen = nEnd - m_oMovedStart->GetIndex() + 1;
1326 }
1327 
RestoreSection(SwDoc * pDoc,SwNodeIndex * pIdx,sal_uInt16 nSectType)1328 void SwUndoSaveSection::RestoreSection( SwDoc* pDoc, SwNodeIndex* pIdx,
1329                                         sal_uInt16 nSectType )
1330 {
1331     if( NODE_OFFSET_MAX == m_nStartPos )        // was there any content?
1332         return;
1333 
1334     // check if the content is at the old position
1335     SwNodeIndex aSttIdx( pDoc->GetNodes(), m_nStartPos );
1336 
1337     // move the content from UndoNodes array into Fly
1338     SwStartNode* pSttNd = SwNodes::MakeEmptySection( aSttIdx.GetNode(),
1339                                             static_cast<SwStartNodeType>(nSectType) );
1340 
1341     RestoreSection( pDoc, *pSttNd->EndOfSectionNode() );
1342 
1343     if( pIdx )
1344         *pIdx = *pSttNd;
1345 }
1346 
RestoreSection(SwDoc * const pDoc,const SwNode & rInsPos,bool bForceCreateFrames)1347 void SwUndoSaveSection::RestoreSection(
1348         SwDoc *const pDoc, const SwNode& rInsPos, bool bForceCreateFrames)
1349 {
1350     if( NODE_OFFSET_MAX == m_nStartPos )        // was there any content?
1351         return;
1352 
1353     SwPosition aInsPos( rInsPos );
1354     SwNodeOffset nEnd = m_oMovedStart->GetIndex() + m_nMoveLen - 1;
1355     MoveFromUndoNds(*pDoc, m_oMovedStart->GetIndex(), aInsPos, &nEnd, bForceCreateFrames);
1356 
1357     // destroy indices again, content was deleted from UndoNodes array
1358     m_oMovedStart.reset();
1359     m_nMoveLen = SwNodeOffset(0);
1360 
1361     if( m_pRedlineSaveData )
1362     {
1363         SwUndo::SetSaveData( *pDoc, *m_pRedlineSaveData );
1364         m_pRedlineSaveData.reset();
1365     }
1366 }
1367 
dumpAsXml(xmlTextWriterPtr pWriter) const1368 void SwUndoSaveSection::dumpAsXml(xmlTextWriterPtr pWriter) const
1369 {
1370     SwUndoSaveContent::dumpAsXml(pWriter);
1371 }
1372 
1373 // save and set the RedlineData
SwRedlineSaveData(SwComparePosition eCmpPos,const SwPosition & rSttPos,const SwPosition & rEndPos,SwRangeRedline & rRedl,bool bCopyNext)1374 SwRedlineSaveData::SwRedlineSaveData(
1375     SwComparePosition eCmpPos,
1376     const SwPosition& rSttPos,
1377     const SwPosition& rEndPos,
1378     SwRangeRedline& rRedl,
1379     bool bCopyNext )
1380     : SwUndRng( rRedl )
1381     , SwRedlineData( rRedl.GetRedlineData(), bCopyNext )
1382 {
1383     assert( SwComparePosition::Outside == eCmpPos ||
1384             !rRedl.GetContentIdx() ); // "Redline with Content"
1385 
1386     switch (eCmpPos)
1387     {
1388     case SwComparePosition::OverlapBefore:        // Pos1 overlaps Pos2 at the beginning
1389         m_nEndNode = rEndPos.GetNodeIndex();
1390         m_nEndContent = rEndPos.GetContentIndex();
1391         break;
1392 
1393     case SwComparePosition::OverlapBehind:        // Pos1 overlaps Pos2 at the end
1394         m_nSttNode = rSttPos.GetNodeIndex();
1395         m_nSttContent = rSttPos.GetContentIndex();
1396         break;
1397 
1398     case SwComparePosition::Inside:                // Pos1 lays completely in Pos2
1399         m_nSttNode = rSttPos.GetNodeIndex();
1400         m_nSttContent = rSttPos.GetContentIndex();
1401         m_nEndNode = rEndPos.GetNodeIndex();
1402         m_nEndContent = rEndPos.GetContentIndex();
1403         break;
1404 
1405     case SwComparePosition::Outside:               // Pos2 lays completely in Pos1
1406         if ( rRedl.GetContentIdx() )
1407         {
1408             // than move section into UndoArray and memorize it
1409             SaveSection( *rRedl.GetContentIdx() );
1410             rRedl.ClearContentIdx();
1411         }
1412         break;
1413 
1414     case SwComparePosition::Equal:                 // Pos1 is exactly as big as Pos2
1415         break;
1416 
1417     default:
1418         assert(false);
1419     }
1420 
1421 #if OSL_DEBUG_LEVEL > 0
1422     m_nRedlineCount = rSttPos.GetNode().GetDoc().getIDocumentRedlineAccess().GetRedlineTable().size();
1423     m_bRedlineCountDontCheck = false;
1424     m_bRedlineMoved = rRedl.IsMoved();
1425 #endif
1426 }
1427 
~SwRedlineSaveData()1428 SwRedlineSaveData::~SwRedlineSaveData()
1429 {
1430 }
1431 
RedlineToDoc(SwPaM const & rPam)1432 void SwRedlineSaveData::RedlineToDoc( SwPaM const & rPam )
1433 {
1434     SwDoc& rDoc = rPam.GetDoc();
1435     SwRangeRedline* pRedl = new SwRangeRedline( *this, rPam );
1436 
1437     if( GetMvSttIdx() )
1438     {
1439         SwNodeIndex aIdx( rDoc.GetNodes() );
1440         RestoreSection( &rDoc, &aIdx, SwNormalStartNode );
1441         if( GetHistory() )
1442             GetHistory()->Rollback( &rDoc );
1443         pRedl->SetContentIdx( aIdx );
1444     }
1445     SetPaM( *pRedl );
1446     // First, delete the "old" so that in an Append no unexpected things will
1447     // happen, e.g. a delete in an insert. In the latter case the just restored
1448     // content will be deleted and not the one you originally wanted.
1449     rDoc.getIDocumentRedlineAccess().DeleteRedline( *pRedl, false, RedlineType::Any );
1450 
1451     RedlineFlags eOld = rDoc.getIDocumentRedlineAccess().GetRedlineFlags();
1452     rDoc.getIDocumentRedlineAccess().SetRedlineFlags_intern( eOld | RedlineFlags::DontCombineRedlines );
1453     //#i92154# let UI know about a new redline with comment
1454     if (rDoc.GetDocShell() && (!pRedl->GetComment().isEmpty()) )
1455         rDoc.GetDocShell()->Broadcast(SwRedlineHint());
1456 
1457     auto const result(rDoc.getIDocumentRedlineAccess().AppendRedline(pRedl, true));
1458     assert(result != IDocumentRedlineAccess::AppendResult::IGNORED); // SwRedlineSaveData::RedlineToDoc: insert redline failed
1459     (void) result; // unused in non-debug
1460     rDoc.getIDocumentRedlineAccess().SetRedlineFlags_intern( eOld );
1461 }
1462 
FillSaveData(const SwPaM & rRange,SwRedlineSaveDatas & rSData,bool bDelRange,bool bCopyNext)1463 bool SwUndo::FillSaveData(
1464     const SwPaM& rRange,
1465     SwRedlineSaveDatas& rSData,
1466     bool bDelRange,
1467     bool bCopyNext )
1468 {
1469     rSData.clear();
1470 
1471     auto [pStt, pEnd] = rRange.StartEnd(); // SwPosition*
1472     const SwRedlineTable& rTable = rRange.GetDoc().getIDocumentRedlineAccess().GetRedlineTable();
1473     SwRedlineTable::size_type n = 0;
1474     rRange.GetDoc().getIDocumentRedlineAccess().GetRedline( *pStt, &n );
1475     for ( ; n < rTable.size(); ++n )
1476     {
1477         SwRangeRedline* pRedl = rTable[n];
1478 
1479         const SwComparePosition eCmpPos =
1480             ComparePosition( *pStt, *pEnd, *pRedl->Start(), *pRedl->End() );
1481         if ( eCmpPos != SwComparePosition::Before
1482              && eCmpPos != SwComparePosition::Behind
1483              && eCmpPos != SwComparePosition::CollideEnd
1484              && eCmpPos != SwComparePosition::CollideStart )
1485         {
1486 
1487             rSData.push_back(std::unique_ptr<SwRedlineSaveData>(new SwRedlineSaveData(eCmpPos, *pStt, *pEnd, *pRedl, bCopyNext)));
1488         }
1489     }
1490     if( !rSData.empty() && bDelRange )
1491     {
1492         rRange.GetDoc().getIDocumentRedlineAccess().DeleteRedline( rRange, false, RedlineType::Any );
1493     }
1494     return !rSData.empty();
1495 }
1496 
FillSaveDataForFormat(const SwPaM & rRange,SwRedlineSaveDatas & rSData)1497 bool SwUndo::FillSaveDataForFormat(
1498     const SwPaM& rRange,
1499     SwRedlineSaveDatas& rSData )
1500 {
1501     rSData.clear();
1502 
1503     const SwPosition *pStt = rRange.Start(), *pEnd = rRange.End();
1504     const SwRedlineTable& rTable = rRange.GetDoc().getIDocumentRedlineAccess().GetRedlineTable();
1505     SwRedlineTable::size_type n = 0;
1506     rRange.GetDoc().getIDocumentRedlineAccess().GetRedline( *pStt, &n );
1507     for ( ; n < rTable.size(); ++n )
1508     {
1509         SwRangeRedline* pRedl = rTable[n];
1510         if ( RedlineType::Format == pRedl->GetType() )
1511         {
1512             const SwComparePosition eCmpPos = ComparePosition( *pStt, *pEnd, *pRedl->Start(), *pRedl->End() );
1513             if ( eCmpPos != SwComparePosition::Before
1514                  && eCmpPos != SwComparePosition::Behind
1515                  && eCmpPos != SwComparePosition::CollideEnd
1516                  && eCmpPos != SwComparePosition::CollideStart )
1517             {
1518                 rSData.push_back(std::unique_ptr<SwRedlineSaveData>(new SwRedlineSaveData(eCmpPos, *pStt, *pEnd, *pRedl, true)));
1519             }
1520 
1521         }
1522     }
1523     return !rSData.empty();
1524 }
1525 
1526 
SetSaveData(SwDoc & rDoc,SwRedlineSaveDatas & rSData)1527 void SwUndo::SetSaveData( SwDoc& rDoc, SwRedlineSaveDatas& rSData )
1528 {
1529     RedlineFlags eOld = rDoc.getIDocumentRedlineAccess().GetRedlineFlags();
1530     rDoc.getIDocumentRedlineAccess().SetRedlineFlags_intern( ( eOld & ~RedlineFlags::Ignore) | RedlineFlags::On );
1531     SwPaM aPam( rDoc.GetNodes().GetEndOfContent() );
1532 
1533     for( size_t n = rSData.size(); n; )
1534         rSData[ --n ].RedlineToDoc( aPam );
1535 
1536 #if OSL_DEBUG_LEVEL > 0
1537     // check redline count against count saved in RedlineSaveData object
1538     // except in the case of moved redlines
1539     assert(
1540         rSData.empty() || rSData[0].m_bRedlineMoved || rSData[0].m_bRedlineCountDontCheck ||
1541            (rSData[0].m_nRedlineCount == rDoc.getIDocumentRedlineAccess().GetRedlineTable().size()));
1542             // "redline count not restored properly"
1543 #endif
1544 
1545     rDoc.getIDocumentRedlineAccess().SetRedlineFlags_intern( eOld );
1546 }
1547 
HasHiddenRedlines(const SwRedlineSaveDatas & rSData)1548 bool SwUndo::HasHiddenRedlines( const SwRedlineSaveDatas& rSData )
1549 {
1550     for( size_t n = rSData.size(); n; )
1551         if( rSData[ --n ].GetMvSttIdx() )
1552             return true;
1553     return false;
1554 }
1555 
CanRedlineGroup(SwRedlineSaveDatas & rCurr,const SwRedlineSaveDatas & rCheck,bool bCurrIsEnd)1556 bool SwUndo::CanRedlineGroup( SwRedlineSaveDatas& rCurr,
1557                         const SwRedlineSaveDatas& rCheck, bool bCurrIsEnd )
1558 {
1559     if( rCurr.size() != rCheck.size() )
1560         return false;
1561 
1562     for( size_t n = 0; n < rCurr.size(); ++n )
1563     {
1564         const SwRedlineSaveData& rSet = rCurr[ n ];
1565         const SwRedlineSaveData& rGet = rCheck[ n ];
1566         if( rSet.m_nSttNode != rGet.m_nSttNode ||
1567             rSet.GetMvSttIdx() || rGet.GetMvSttIdx() ||
1568             ( bCurrIsEnd ? rSet.m_nSttContent != rGet.m_nEndContent
1569                             : rSet.m_nEndContent != rGet.m_nSttContent ) ||
1570             !rGet.CanCombine( rSet ) )
1571         {
1572             return false;
1573         }
1574     }
1575 
1576     for( size_t n = 0; n < rCurr.size(); ++n )
1577     {
1578         SwRedlineSaveData& rSet = rCurr[ n ];
1579         const SwRedlineSaveData& rGet = rCheck[ n ];
1580         if( bCurrIsEnd )
1581             rSet.m_nSttContent = rGet.m_nSttContent;
1582         else
1583             rSet.m_nEndContent = rGet.m_nEndContent;
1584     }
1585     return true;
1586 }
1587 
ShortenString(const OUString & rStr,sal_Int32 nLength,std::u16string_view aFillStr)1588 OUString ShortenString(const OUString & rStr, sal_Int32 nLength, std::u16string_view aFillStr)
1589 {
1590     assert(nLength - aFillStr.size() >= 2);
1591 
1592     if (rStr.getLength() <= nLength)
1593         return rStr;
1594 
1595     nLength -= aFillStr.size();
1596     if ( nLength < 2 )
1597         nLength = 2;
1598 
1599     const sal_Int32 nFrontLen = nLength - nLength / 2;
1600     const sal_Int32 nBackLen = nLength - nFrontLen;
1601 
1602     return OUString::Concat(rStr.subView(0, nFrontLen))
1603            + aFillStr
1604            + rStr.subView(rStr.getLength() - nBackLen);
1605 }
1606 
IsAtEndOfSection(SwPosition const & rAnchorPos)1607 static bool IsAtEndOfSection(SwPosition const& rAnchorPos)
1608 {
1609     SwNodeIndex node(*rAnchorPos.GetNode().EndOfSectionNode());
1610     SwContentNode *const pNode(SwNodes::GoPrevious(&node));
1611     assert(pNode);
1612     assert(rAnchorPos.GetNode() <= node.GetNode()); // last valid anchor pos is last content
1613     return node == rAnchorPos.GetNode()
1614         // at-para fly has no SwContentIndex!
1615         && (rAnchorPos.GetContentIndex() == pNode->Len() || rAnchorPos.GetContentNode() == nullptr);
1616 }
1617 
IsAtStartOfSection(SwPosition const & rAnchorPos)1618 static bool IsAtStartOfSection(SwPosition const& rAnchorPos)
1619 {
1620     SwNodeIndex node(*rAnchorPos.GetNode().StartOfSectionNode());
1621     SwContentNode* const pNode(SwNodes::GoNext(&node));
1622     assert(pNode);
1623     (void) pNode;
1624     assert(node <= rAnchorPos.GetNode());
1625     return node == rAnchorPos.GetNode() && rAnchorPos.GetContentIndex() == 0;
1626 }
1627 
1628 /// passed start / end position could be on section start / end node
IsAtEndOfSection2(SwPosition const & rPos)1629 static bool IsAtEndOfSection2(SwPosition const& rPos)
1630 {
1631     return rPos.GetNode().IsEndNode()
1632         || IsAtEndOfSection(rPos);
1633 }
1634 
IsAtStartOfSection2(SwPosition const & rPos)1635 static bool IsAtStartOfSection2(SwPosition const& rPos)
1636 {
1637     return rPos.GetNode().IsStartNode()
1638         || IsAtStartOfSection(rPos);
1639 }
1640 
IsNotBackspaceHeuristic(SwPosition const & rStart,SwPosition const & rEnd)1641 static bool IsNotBackspaceHeuristic(
1642         SwPosition const& rStart, SwPosition const& rEnd)
1643 {
1644     // check if the selection is backspace/delete created by DelLeft/DelRight
1645     if (rStart.GetNodeIndex() + 1 != rEnd.GetNodeIndex())
1646         return true;
1647     if (rEnd.GetContentIndex() != 0)
1648         return true;
1649     const SwTextNode* pTextNode = rStart.GetNode().GetTextNode();
1650     if (!pTextNode || rStart.GetContentIndex() != pTextNode->Len())
1651         return true;
1652     return false;
1653 }
1654 
IsDestroyFrameAnchoredAtChar(SwPosition const & rAnchorPos,SwPosition const & rStart,SwPosition const & rEnd,DelContentType const nDelContentType)1655 bool IsDestroyFrameAnchoredAtChar(SwPosition const & rAnchorPos,
1656         SwPosition const & rStart, SwPosition const & rEnd,
1657         DelContentType const nDelContentType)
1658 {
1659     assert(rStart <= rEnd);
1660 
1661     // CheckNoCntnt means DelFullPara which is obvious to handle
1662     if (DelContentType::CheckNoCntnt & nDelContentType)
1663     {   // exclude selection end node because it won't be deleted
1664         return (rAnchorPos.GetNode() < rEnd.GetNode())
1665             && (rStart.GetNode() <= rAnchorPos.GetNode());
1666     }
1667 
1668     if ((nDelContentType & DelContentType::WriterfilterHack)
1669         && rAnchorPos.GetDoc().IsInWriterfilterImport())
1670     {   // FIXME hack for writerfilter RemoveLastParagraph() and MakeFlyAndMove(); can't test file format more specific?
1671         return (rStart < rAnchorPos) && (rAnchorPos < rEnd);
1672     }
1673 
1674     if (nDelContentType & DelContentType::ExcludeFlyAtStartEnd)
1675     {   // exclude selection start and end node
1676         return (rAnchorPos.GetNode() < rEnd.GetNode())
1677             && (rStart.GetNode() < rAnchorPos.GetNode());
1678     }
1679 
1680     // in general, exclude the start and end position
1681     return ((rStart < rAnchorPos)
1682             || (rStart == rAnchorPos
1683                 // special case: fully deleted node
1684                 && ((rStart.GetNode() != rEnd.GetNode() && rStart.GetContentIndex() == 0
1685                         // but not if the selection is backspace/delete!
1686                         && IsNotBackspaceHeuristic(rStart, rEnd))
1687                     || (IsAtStartOfSection(rAnchorPos) && IsAtEndOfSection2(rEnd)))))
1688         && ((rAnchorPos < rEnd)
1689             || (rAnchorPos == rEnd
1690                 // special case: fully deleted node
1691                 && ((rEnd.GetNode() != rStart.GetNode() && rEnd.GetContentIndex() == rEnd.GetNode().GetTextNode()->Len()
1692                         && IsNotBackspaceHeuristic(rStart, rEnd))
1693                     || (IsAtEndOfSection(rAnchorPos) && IsAtStartOfSection2(rStart)))));
1694 }
1695 
IsSelectFrameAnchoredAtPara(SwPosition const & rAnchorPos,SwPosition const & rStart,SwPosition const & rEnd,DelContentType const nDelContentType)1696 bool IsSelectFrameAnchoredAtPara(SwPosition const & rAnchorPos,
1697         SwPosition const & rStart, SwPosition const & rEnd,
1698         DelContentType const nDelContentType)
1699 {
1700     assert(rStart <= rEnd);
1701 
1702     // CheckNoCntnt means DelFullPara which is obvious to handle
1703     if (DelContentType::CheckNoCntnt & nDelContentType)
1704     {   // exclude selection end node because it won't be deleted
1705         return (rAnchorPos.GetNode() < rEnd.GetNode())
1706             && (rStart.GetNode() <= rAnchorPos.GetNode());
1707     }
1708 
1709     if ((nDelContentType & DelContentType::WriterfilterHack)
1710         && rAnchorPos.GetDoc().IsInWriterfilterImport())
1711     {   // FIXME hack for writerfilter RemoveLastParagraph() and MakeFlyAndMove(); can't test file format more specific?
1712         // but it MUST NOT be done during the SetRedlineFlags at the end of ODF
1713         // import, where the IsInXMLImport() cannot be checked because the
1714         // stupid code temp. overrides it - instead rely on setting the ALLFLYS
1715         // flag in MoveFromSection() and converting that to CheckNoCntnt with
1716         // adjusted cursor!
1717         return (rStart.GetNode() < rAnchorPos.GetNode()) && (rAnchorPos.GetNode() < rEnd.GetNode());
1718     }
1719 
1720     // in general, exclude the start and end position
1721     return ((rStart.GetNode() < rAnchorPos.GetNode())
1722             || (rStart.GetNode() == rAnchorPos.GetNode()
1723                 && !(nDelContentType & DelContentType::ExcludeFlyAtStartEnd)
1724                 // special case: fully deleted node
1725                 && ((rStart.GetNode() != rEnd.GetNode() && rStart.GetContentIndex() == 0
1726                         // but not if the selection is backspace/delete!
1727                         && IsNotBackspaceHeuristic(rStart, rEnd))
1728                     || (IsAtStartOfSection2(rStart) && IsAtEndOfSection2(rEnd)))))
1729         && ((rAnchorPos.GetNode() < rEnd.GetNode())
1730             || (rAnchorPos.GetNode() == rEnd.GetNode()
1731                 && !(nDelContentType & DelContentType::ExcludeFlyAtStartEnd)
1732                 // special case: fully deleted node
1733                 && ((rEnd.GetNode() != rStart.GetNode() && rEnd.GetContentIndex() == rEnd.GetNode().GetTextNode()->Len()
1734                         && IsNotBackspaceHeuristic(rStart, rEnd))
1735                     || (IsAtEndOfSection2(rEnd) && IsAtStartOfSection2(rStart)))));
1736 }
1737 
IsFlySelectedByCursor(SwDoc const & rDoc,SwPosition const & rStart,SwPosition const & rEnd)1738 bool IsFlySelectedByCursor(SwDoc const & rDoc,
1739         SwPosition const & rStart, SwPosition const & rEnd)
1740 {
1741     for (SwFrameFormat const*const pFly : *rDoc.GetSpzFrameFormats())
1742     {
1743         SwFormatAnchor const& rAnchor(pFly->GetAnchor());
1744         switch (rAnchor.GetAnchorId())
1745         {
1746             case RndStdIds::FLY_AT_CHAR:
1747             case RndStdIds::FLY_AT_PARA:
1748             {
1749                 SwPosition const*const pAnchorPos(rAnchor.GetContentAnchor());
1750                 // can this really be null?
1751                 if (pAnchorPos != nullptr
1752                     && ((rAnchor.GetAnchorId() == RndStdIds::FLY_AT_CHAR)
1753                         ? IsDestroyFrameAnchoredAtChar(*pAnchorPos, rStart, rEnd)
1754                         : IsSelectFrameAnchoredAtPara(*pAnchorPos, rStart, rEnd)))
1755                 {
1756                     return true;
1757                 }
1758             }
1759             break;
1760             default: // other types not relevant
1761             break;
1762         }
1763     }
1764     return false;
1765 }
1766 
1767 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1768