nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / core / edit / eddel.cxx
blob87999e1f869e757054235fe5cdf54f13643913e9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <memory>
21 #include <doc.hxx>
22 #include <IDocumentUndoRedo.hxx>
23 #include <IDocumentContentOperations.hxx>
24 #include <editsh.hxx>
25 #include <pam.hxx>
26 #include <swundo.hxx>
27 #include <undobj.hxx>
28 #include <SwRewriter.hxx>
29 #include <osl/diagnose.h>
31 #include <strings.hrc>
32 #include <vector>
34 void SwEditShell::DeleteSel( SwPaM& rPam, bool* pUndo )
36 bool bSelectAll = StartsWithTable() && ExtendedSelectedAll();
37 // only for selections
38 if (!rPam.HasMark()
39 || (*rPam.GetPoint() == *rPam.GetMark()
40 && !IsFlySelectedByCursor(*GetDoc(), *rPam.Start(), *rPam.End())))
42 return;
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 );
58 *pUndo = true;
60 SwPaM aDelPam( *rPam.Start() );
61 const SwPosition* pEndSelPos = rPam.End();
62 do {
63 aDelPam.SetMark();
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
71 else
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() )
81 // delete everything
82 GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aDelPam );
83 SaveTableBoxContent( aDelPam.GetPoint() );
86 if( !pEndSelPos ) // at the end of a selection
87 break;
88 aDelPam.DeleteMark();
89 aDelPam.Move( fnMoveForward, GoInContent ); // next box
90 } while( pEndSelPos );
92 else
94 std::unique_ptr<SwPaM> pNewPam;
95 SwPaM * pPam = &rPam;
96 if (bSelectAll)
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();
116 // delete everything
117 GetDoc()->getIDocumentContentOperations().DeleteAndJoin(*pPam);
118 SaveTableBoxContent( pPam->GetPoint() );
121 // Selection is not needed anymore
122 rPam.DeleteMark();
125 bool SwEditShell::Delete()
127 CurrShell aCurr( this );
128 bool bRet = false;
129 if ( !HasReadonlySel() || CursorInsideInputField() )
131 StartAllAction();
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
148 if( bUndo )
150 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END, nullptr);
152 EndAllAction();
153 bRet = true;
155 else
157 bRet = RemoveParagraphMetadataFieldAtCursor();
160 return bRet;
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())
177 if( !pPos )
179 if( &rDestShell == this )
181 // First cursor represents the target position!!
182 rPaM.DeleteMark();
183 pPos = rPaM.GetPoint();
184 continue;
186 else
187 pPos = rDestShell.GetCursor()->GetPoint();
189 if( IsBlockMode() )
190 { // In block mode different insert positions will be calculated
191 // by simulated cursor movements from the given first insert position
192 if( nMove )
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 );
201 else
202 pInsertPos = std::make_shared<SwPosition>( *pPos );
203 ++nMove;
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() )
209 return false;
213 rDestShell.StartAllAction();
214 SwPosition *pPos = nullptr;
215 bool bRet = false;
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())
225 if( !pPos )
227 if( &rDestShell == this )
229 // First cursor represents the target position!!
230 rPaM.DeleteMark();
231 pPos = rPaM.GetPoint();
232 continue;
234 else
235 pPos = rDestShell.GetCursor()->GetPoint();
237 if( !bFirstMove )
239 if( pNextInsert != aInsertList.end() )
241 pPos = pNextInsert->get();
242 ++pNextInsert;
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() )
250 continue;
252 if( bFirstMove )
254 // Store start position of the new area
255 aSttNdIdx = pPos->nNode.GetIndex()-1;
256 nSttCntIdx = pPos->nContent.GetIndex();
257 bFirstMove = false;
260 const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange(rPaM, *pPos, SwCopyFlags::CheckPosInFly) );
261 if (!bSuccess)
262 continue;
264 SwPaM aInsertPaM(*pPos, SwPosition(aSttNdIdx));
265 rDestShell.GetDoc()->MakeUniqueNumRules(aInsertPaM);
267 bRet = true;
270 // Maybe nothing has been moved?
271 if( !bFirstMove )
273 SwPaM* pCursor = rDestShell.GetCursor();
274 pCursor->SetMark();
275 pCursor->GetPoint()->nNode = aSttNdIdx.GetIndex()+1;
276 pCursor->GetPoint()->nContent.Assign( pCursor->GetContentNode(),nSttCntIdx);
277 pCursor->Exchange();
279 else
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" );
296 #endif
298 // close Undo container here
299 rDestShell.GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
300 rDestShell.EndAllAction();
302 rDestShell.SaveTableBoxContent( rDestShell.GetCursor()->GetPoint() );
304 return bRet;
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 );
318 bool bRet = false;
319 if( !HasReadonlySel() )
321 StartAllAction();
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())
329 || bRet;
330 SaveTableBoxContent( rPaM.GetPoint() );
334 // close Undo container here
335 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY, nullptr);
336 EndAllAction();
338 return bRet;
341 /// special method for JOE's wizards
342 bool SwEditShell::DelFullPara()
344 bool bRet = false;
345 if( !IsTableMode() )
347 SwPaM* pCursor = GetCursor();
348 // no multi selection
349 if( !pCursor->IsMultiSelection() && !HasReadonlySel() )
351 CurrShell aCurr( this );
352 StartAllAction();
353 bRet = GetDoc()->getIDocumentContentOperations().DelFullPara( *pCursor );
354 EndAllAction();
357 return bRet;
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */