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 <strings.hrc>
34 void SwEditShell::DeleteSel( SwPaM
& rPam
, bool* pUndo
)
36 bool bSelectAll
= StartsWithTable() && ExtendedSelectedAll();
37 // only for selections
39 || (*rPam
.GetPoint() == *rPam
.GetMark()
40 && !IsFlySelectedByCursor(*GetDoc(), *rPam
.Start(), *rPam
.End())))
45 // Is the selection in a table? Then delete only the content of the selected boxes.
46 // Here, there are two cases:
47 // 1. Point and Mark are in one box, delete selection as usual
48 // 2. Point and Mark are in different boxes, search all selected boxes and delete content
49 // 3. Point and Mark are at the document start and end, Point is in a table: delete selection as usual
50 if( rPam
.GetNode().FindTableNode() &&
51 rPam
.GetNode().StartOfSectionNode() !=
52 rPam
.GetNode(false).StartOfSectionNode() && !bSelectAll
)
54 // group the Undo in the table
55 if( pUndo
&& !*pUndo
)
57 GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START
, nullptr );
60 SwPaM
aDelPam( *rPam
.Start() );
61 const SwPosition
* pEndSelPos
= rPam
.End();
64 SwNode
& rNd
= aDelPam
.GetNode();
65 const SwNode
& rEndNd
= *rNd
.EndOfSectionNode();
66 if( pEndSelPos
->nNode
.GetIndex() <= rEndNd
.GetIndex() )
68 *aDelPam
.GetPoint() = *pEndSelPos
;
69 pEndSelPos
= nullptr; // misuse a pointer as a flag
73 // then go to the end of the selection
74 aDelPam
.GetPoint()->nNode
= rEndNd
;
75 aDelPam
.Move( fnMoveBackward
, GoInContent
);
77 // skip protected boxes
78 if( !rNd
.IsContentNode() ||
79 !rNd
.IsInProtectSect() )
82 GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aDelPam
);
83 SaveTableBoxContent( aDelPam
.GetPoint() );
86 if( !pEndSelPos
) // at the end of a selection
89 aDelPam
.Move( fnMoveForward
, GoInContent
); // next box
90 } while( pEndSelPos
);
94 std::unique_ptr
<SwPaM
> pNewPam
;
98 assert(dynamic_cast<SwShellCursor
*>(&rPam
)); // must be corrected pam
99 pNewPam
.reset(new SwPaM(*rPam
.GetMark(), *rPam
.GetPoint()));
100 // Selection starts at the first para of the first cell, but we
101 // want to delete the table node before the first cell as well.
102 while (SwTableNode
const* pTableNode
=
103 pNewPam
->Start()->nNode
.GetNode().StartOfSectionNode()->FindTableNode())
105 pNewPam
->Start()->nNode
= *pTableNode
;
107 // tdf#133990 ensure section is included in SwUndoDelete
108 while (SwSectionNode
const* pSectionNode
=
109 pNewPam
->Start()->nNode
.GetNode().StartOfSectionNode()->FindSectionNode())
111 pNewPam
->Start()->nNode
= *pSectionNode
;
113 pNewPam
->Start()->nContent
.Assign(nullptr, 0);
114 pPam
= pNewPam
.get();
117 GetDoc()->getIDocumentContentOperations().DeleteAndJoin(*pPam
);
118 SaveTableBoxContent( pPam
->GetPoint() );
121 // Selection is not needed anymore
125 bool SwEditShell::Delete()
127 CurrShell
aCurr( this );
129 if ( !HasReadonlySel() || CursorInsideInputField() )
133 bool bUndo
= GetCursor()->GetNext() != GetCursor();
134 if( bUndo
) // more than one selection?
136 SwRewriter aRewriter
;
137 aRewriter
.AddRule(UndoArg1
, SwResId(STR_MULTISEL
));
139 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::DELETE
, &aRewriter
);
142 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
144 DeleteSel( rPaM
, &bUndo
);
147 // If undo container then close here
150 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END
, nullptr);
157 bRet
= RemoveParagraphMetadataFieldAtCursor();
163 bool SwEditShell::Copy( SwEditShell
& rDestShell
)
165 CurrShell
aCurr( &rDestShell
);
167 // List of insert positions for smart insert of block selections
168 std::vector
< std::shared_ptr
<SwPosition
> > aInsertList
;
170 // Fill list of insert positions
172 SwPosition
* pPos
= nullptr;
173 std::shared_ptr
<SwPosition
> pInsertPos
;
174 sal_uInt16 nMove
= 0;
175 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
179 if( &rDestShell
== this )
181 // First cursor represents the target position!!
183 pPos
= rPaM
.GetPoint();
187 pPos
= rDestShell
.GetCursor()->GetPoint();
190 { // In block mode different insert positions will be calculated
191 // by simulated cursor movements from the given first insert position
194 SwCursor
aCursor( *pPos
, nullptr);
195 if (aCursor
.UpDown(false, nMove
, nullptr, 0, *GetLayout()))
197 pInsertPos
= std::make_shared
<SwPosition
>( *aCursor
.GetPoint() );
198 aInsertList
.push_back( pInsertPos
);
202 pInsertPos
= std::make_shared
<SwPosition
>( *pPos
);
205 SwPosition
*pTmp
= IsBlockMode() ? pInsertPos
.get() : pPos
;
206 // Check if a selection would be copied into itself
207 if( rDestShell
.GetDoc() == GetDoc() &&
208 *rPaM
.Start() <= *pTmp
&& *pTmp
< *rPaM
.End() )
213 rDestShell
.StartAllAction();
214 SwPosition
*pPos
= nullptr;
216 bool bFirstMove
= true;
217 SwNodeIndex
aSttNdIdx( rDestShell
.GetDoc()->GetNodes() );
218 sal_Int32 nSttCntIdx
= 0;
219 // For block selection this list is filled with the insert positions
220 auto pNextInsert
= aInsertList
.begin();
222 rDestShell
.GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START
, nullptr );
223 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
227 if( &rDestShell
== this )
229 // First cursor represents the target position!!
231 pPos
= rPaM
.GetPoint();
235 pPos
= rDestShell
.GetCursor()->GetPoint();
239 if( pNextInsert
!= aInsertList
.end() )
241 pPos
= pNextInsert
->get();
244 else if( IsBlockMode() )
245 GetDoc()->getIDocumentContentOperations().SplitNode( *pPos
, false );
248 // Only for a selection (non-text nodes have selection but Point/GetMark are equal)
249 if( !rPaM
.HasMark() || *rPaM
.GetPoint() == *rPaM
.GetMark() )
254 // Store start position of the new area
255 aSttNdIdx
= pPos
->nNode
.GetIndex()-1;
256 nSttCntIdx
= pPos
->nContent
.GetIndex();
260 const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange(rPaM
, *pPos
, SwCopyFlags::CheckPosInFly
) );
264 SwPaM
aInsertPaM(*pPos
, SwPosition(aSttNdIdx
));
265 rDestShell
.GetDoc()->MakeUniqueNumRules(aInsertPaM
);
270 // Maybe nothing has been moved?
273 SwPaM
* pCursor
= rDestShell
.GetCursor();
275 pCursor
->GetPoint()->nNode
= aSttNdIdx
.GetIndex()+1;
276 pCursor
->GetPoint()->nContent
.Assign( pCursor
->GetContentNode(),nSttCntIdx
);
281 // If the cursor moved during move process, move also its GetMark
282 rDestShell
.GetCursor()->SetMark();
283 rDestShell
.GetCursor()->DeleteMark();
285 #if OSL_DEBUG_LEVEL > 0
286 // check if the indices are registered in the correct nodes
288 for(SwPaM
& rCmp
: rDestShell
.GetCursor()->GetRingContainer())
290 OSL_ENSURE( rCmp
.GetPoint()->nContent
.GetIdxReg()
291 == rCmp
.GetContentNode(), "Point in wrong Node" );
292 OSL_ENSURE( rCmp
.GetMark()->nContent
.GetIdxReg()
293 == rCmp
.GetContentNode(false), "Mark in wrong Node" );
298 // close Undo container here
299 rDestShell
.GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END
, nullptr );
300 rDestShell
.EndAllAction();
302 rDestShell
.SaveTableBoxContent( rDestShell
.GetCursor()->GetPoint() );
307 /** Replace a selected area in a text node with a given string.
309 * Intended for "search & replace".
311 * @param bRegExpRplc if <true> replace tabs (\\t) and replace with found string (not \&).
312 * E.g. [Fnd: "zzz", Repl: "xx\t\\t..&..\&"] --> "xx\t<Tab>..zzz..&"
314 bool SwEditShell::Replace( const OUString
& rNewStr
, bool bRegExpRplc
)
316 CurrShell
aCurr( this );
319 if( !HasReadonlySel() )
322 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY
, nullptr);
324 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
326 if( rPaM
.HasMark() && *rPaM
.GetMark() != *rPaM
.GetPoint() )
328 bRet
= sw::ReplaceImpl(rPaM
, rNewStr
, bRegExpRplc
, *GetDoc(), GetLayout())
330 SaveTableBoxContent( rPaM
.GetPoint() );
334 // close Undo container here
335 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY
, nullptr);
341 /// special method for JOE's wizards
342 bool SwEditShell::DelFullPara()
347 SwPaM
* pCursor
= GetCursor();
348 // no multi selection
349 if( !pCursor
->IsMultiSelection() && !HasReadonlySel() )
351 CurrShell
aCurr( this );
353 bRet
= GetDoc()->getIDocumentContentOperations().DelFullPara( *pCursor
);
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */