1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <IDocumentUndoRedo.hxx>
23 #include <IDocumentContentOperations.hxx>
28 #include <SwRewriter.hxx>
29 #include <osl/diagnose.h>
31 #include <officecfg/Office/Writer.hxx>
33 #include <strings.hrc>
36 void SwEditShell::DeleteSel(SwPaM
& rPam
, bool const isArtificialSelection
, bool *const pUndo
)
38 auto const oSelectAll(StartsWith_() != SwCursorShell::StartsWith::None
39 ? ExtendedSelectedAll()
40 : ::std::optional
<::std::pair
<SwNode
const*, ::std::vector
<SwTableNode
*>>>{});
41 // only for selections
43 || (*rPam
.GetPoint() == *rPam
.GetMark()
44 && !IsFlySelectedByCursor(*GetDoc(), *rPam
.Start(), *rPam
.End())))
49 // Is the selection in a table? Then delete only the content of the selected boxes.
50 // Here, there are two cases:
51 // 1. Point and Mark are in one box, delete selection as usual
52 // 2. Point and Mark are in different boxes, search all selected boxes and delete content
53 // 3. Point and Mark are at the document start and end, Point is in a table: delete selection as usual
54 if( rPam
.GetPointNode().FindTableNode() &&
55 rPam
.GetPointNode().StartOfSectionNode() !=
56 rPam
.GetMarkNode().StartOfSectionNode() && !oSelectAll
)
58 // group the Undo in the table
59 if( pUndo
&& !*pUndo
)
61 GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START
, nullptr );
64 SwPaM
aDelPam( *rPam
.Start() );
65 const SwPosition
* pEndSelPos
= rPam
.End();
68 SwNode
& rNd
= aDelPam
.GetPointNode();
69 const SwNode
& rEndNd
= *rNd
.EndOfSectionNode();
70 if( pEndSelPos
->GetNodeIndex() <= rEndNd
.GetIndex() )
72 *aDelPam
.GetPoint() = *pEndSelPos
;
73 pEndSelPos
= nullptr; // misuse a pointer as a flag
77 // then go to the end of the selection
78 aDelPam
.GetPoint()->Assign(rEndNd
);
79 aDelPam
.Move( fnMoveBackward
, GoInContent
);
81 // skip protected boxes
82 if( !rNd
.IsContentNode() ||
83 !rNd
.IsInProtectSect() )
86 GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aDelPam
);
87 SaveTableBoxContent( aDelPam
.GetPoint() );
90 if( !pEndSelPos
) // at the end of a selection
93 aDelPam
.Move( fnMoveForward
, GoInContent
); // next box
94 } while( pEndSelPos
);
98 std::optional
<SwPaM
> pNewPam
;
102 if (!oSelectAll
->second
.empty())
104 SwRewriter aRewriter
;
105 aRewriter
.AddRule(UndoArg1
, SwResId(STR_MULTISEL
));
106 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::DELETE
, &aRewriter
);
108 // tdf#155685 tables at the end must be deleted separately
109 for (SwTableNode
*const pTable
: oSelectAll
->second
)
111 GetDoc()->DelTable(pTable
);
113 assert(dynamic_cast<SwShellCursor
*>(&rPam
)); // must be corrected pam
114 pNewPam
.emplace(*rPam
.GetMark(), *rPam
.GetPoint());
115 // Selection starts at the first para of the first cell, but we
116 // want to delete the table node before the first cell as well.
117 pNewPam
->Start()->Assign(*oSelectAll
->first
);
121 GetDoc()->getIDocumentContentOperations().DeleteAndJoin(*pPam
,
122 isArtificialSelection
? SwDeleteFlags::ArtificialSelection
: SwDeleteFlags::Default
);
123 SaveTableBoxContent( pPam
->GetPoint() );
124 if (oSelectAll
&& !oSelectAll
->second
.empty())
126 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END
, nullptr);
130 // Selection is not needed anymore
134 bool SwEditShell::Delete(bool const isArtificialSelection
)
136 CurrShell
aCurr( this );
138 if ( !HasReadonlySel() || CursorInsideInputField() )
140 if (HasHiddenSections() &&
141 officecfg::Office::Writer::Content::Display::ShowWarningHiddenSection::get())
143 if (!WarnHiddenSectionDialog())
145 bRet
= RemoveParagraphMetadataFieldAtCursor();
151 bool bUndo
= GetCursor()->GetNext() != GetCursor();
152 if( bUndo
) // more than one selection?
154 SwRewriter aRewriter
;
155 aRewriter
.AddRule(UndoArg1
, SwResId(STR_MULTISEL
));
157 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::DELETE
, &aRewriter
);
160 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
162 DeleteSel(rPaM
, isArtificialSelection
, &bUndo
);
165 // If undo container then close here
168 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END
, nullptr);
175 bRet
= RemoveParagraphMetadataFieldAtCursor();
178 InfoReadOnlyDialog(false);
185 bool SwEditShell::Copy( SwEditShell
& rDestShell
)
187 CurrShell
aCurr( &rDestShell
);
189 // List of insert positions for smart insert of block selections
190 std::vector
< std::shared_ptr
<SwPosition
> > aInsertList
;
192 // Fill list of insert positions
194 SwPosition
* pPos
= nullptr;
195 std::shared_ptr
<SwPosition
> pInsertPos
;
196 sal_uInt16 nMove
= 0;
197 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
201 if( &rDestShell
== this )
203 // First cursor represents the target position!!
205 pPos
= rPaM
.GetPoint();
209 pPos
= rDestShell
.GetCursor()->GetPoint();
212 { // In block mode different insert positions will be calculated
213 // by simulated cursor movements from the given first insert position
216 SwCursor
aCursor( *pPos
, nullptr);
217 if (aCursor
.UpDown(false, nMove
, nullptr, 0, *GetLayout()))
219 pInsertPos
= std::make_shared
<SwPosition
>( *aCursor
.GetPoint() );
220 aInsertList
.push_back( pInsertPos
);
224 pInsertPos
= std::make_shared
<SwPosition
>( *pPos
);
227 SwPosition
*pTmp
= IsBlockMode() ? pInsertPos
.get() : pPos
;
228 // Check if a selection would be copied into itself
229 if( rDestShell
.GetDoc() == GetDoc() &&
230 *rPaM
.Start() <= *pTmp
&& *pTmp
< *rPaM
.End() )
235 rDestShell
.StartAllAction();
236 SwPosition
*pPos
= nullptr;
238 bool bFirstMove
= true;
239 SwNodeIndex
aSttNdIdx( rDestShell
.GetDoc()->GetNodes() );
240 sal_Int32 nSttCntIdx
= 0;
241 // For block selection this list is filled with the insert positions
242 auto pNextInsert
= aInsertList
.begin();
244 rDestShell
.GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START
, nullptr );
245 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
249 if( &rDestShell
== this )
251 // First cursor represents the target position!!
253 pPos
= rPaM
.GetPoint();
257 pPos
= rDestShell
.GetCursor()->GetPoint();
261 if( pNextInsert
!= aInsertList
.end() )
263 pPos
= pNextInsert
->get();
266 else if( IsBlockMode() )
267 GetDoc()->getIDocumentContentOperations().SplitNode( *pPos
, false );
270 // Only for a selection (non-text nodes have selection but Point/GetMark are equal)
271 if( !rPaM
.HasMark() || *rPaM
.GetPoint() == *rPaM
.GetMark() )
276 // Store start position of the new area
277 aSttNdIdx
= pPos
->GetNodeIndex()-1;
278 nSttCntIdx
= pPos
->GetContentIndex();
282 const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange(rPaM
, *pPos
, SwCopyFlags::CheckPosInFly
) );
286 SwPaM
aInsertPaM(*pPos
, SwPosition(aSttNdIdx
));
287 rDestShell
.GetDoc()->MakeUniqueNumRules(aInsertPaM
);
292 // Maybe nothing has been moved?
295 SwPaM
* pCursor
= rDestShell
.GetCursor();
297 pCursor
->GetPoint()->Assign( aSttNdIdx
.GetIndex()+1, nSttCntIdx
);
302 // If the cursor moved during move process, move also its GetMark
303 rDestShell
.GetCursor()->SetMark();
304 rDestShell
.GetCursor()->DeleteMark();
306 #if OSL_DEBUG_LEVEL > 0
307 // check if the indices are registered in the correct nodes
309 for(SwPaM
& rCmp
: rDestShell
.GetCursor()->GetRingContainer())
311 OSL_ENSURE( rCmp
.GetPoint()->GetContentNode()
312 == rCmp
.GetPointContentNode(), "Point in wrong Node" );
313 OSL_ENSURE( rCmp
.GetMark()->GetContentNode()
314 == rCmp
.GetMarkContentNode(), "Mark in wrong Node" );
319 // close Undo container here
320 rDestShell
.GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END
, nullptr );
321 rDestShell
.EndAllAction();
323 rDestShell
.SaveTableBoxContent( rDestShell
.GetCursor()->GetPoint() );
328 /** Replace a selected area in a text node with a given string.
330 * Intended for "search & replace".
332 * @param bRegExpRplc if <true> replace tabs (\\t) and replace with found string (not \&).
333 * E.g. [Fnd: "zzz", Repl: "xx\t\\t..&..\&"] --> "xx\t<Tab>..zzz..&"
335 bool SwEditShell::Replace( const OUString
& rNewStr
, bool bRegExpRplc
)
337 CurrShell
aCurr( this );
340 if (!HasReadonlySel(true))
343 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY
, nullptr);
345 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
347 if( rPaM
.HasMark() && *rPaM
.GetMark() != *rPaM
.GetPoint() )
349 bRet
= sw::ReplaceImpl(rPaM
, rNewStr
, bRegExpRplc
, *GetDoc(), GetLayout())
351 SaveTableBoxContent( rPaM
.GetPoint() );
355 // close Undo container here
356 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY
, nullptr);
362 /// special method for JOE's wizards
363 bool SwEditShell::DelFullPara()
368 SwPaM
* pCursor
= GetCursor();
369 // no multi selection
370 if( !pCursor
->IsMultiSelection() && !HasReadonlySel() )
372 CurrShell
aCurr( this );
374 bRet
= GetDoc()->getIDocumentContentOperations().DelFullPara( *pCursor
);
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */