Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / edit / eddel.cxx
blob10d086bbac6378bcee3399c4fcd5368f8352bb8e
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>
30 #include <wrtsh.hxx>
31 #include <officecfg/Office/Writer.hxx>
33 #include <strings.hrc>
34 #include <vector>
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
42 if (!rPam.HasMark()
43 || (*rPam.GetPoint() == *rPam.GetMark()
44 && !IsFlySelectedByCursor(*GetDoc(), *rPam.Start(), *rPam.End())))
46 return;
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 );
62 *pUndo = true;
64 SwPaM aDelPam( *rPam.Start() );
65 const SwPosition* pEndSelPos = rPam.End();
66 do {
67 aDelPam.SetMark();
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
75 else
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() )
85 // delete everything
86 GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aDelPam );
87 SaveTableBoxContent( aDelPam.GetPoint() );
90 if( !pEndSelPos ) // at the end of a selection
91 break;
92 aDelPam.DeleteMark();
93 aDelPam.Move( fnMoveForward, GoInContent ); // next box
94 } while( pEndSelPos );
96 else
98 std::optional<SwPaM> pNewPam;
99 SwPaM * pPam = &rPam;
100 if (oSelectAll)
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);
118 pPam = &*pNewPam;
120 // delete everything
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
131 rPam.DeleteMark();
134 bool SwEditShell::Delete(bool const isArtificialSelection)
136 CurrShell aCurr( this );
137 bool bRet = false;
138 if ( !HasReadonlySel() || CursorInsideInputField() )
140 if (HasHiddenSections() &&
141 officecfg::Office::Writer::Content::Display::ShowWarningHiddenSection::get())
143 if (!WarnHiddenSectionDialog())
145 bRet = RemoveParagraphMetadataFieldAtCursor();
146 return bRet;
150 StartAllAction();
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
166 if( bUndo )
168 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END, nullptr);
170 EndAllAction();
171 bRet = true;
173 else
175 bRet = RemoveParagraphMetadataFieldAtCursor();
176 if (!bRet)
178 InfoReadOnlyDialog(false);
182 return bRet;
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())
199 if( !pPos )
201 if( &rDestShell == this )
203 // First cursor represents the target position!!
204 rPaM.DeleteMark();
205 pPos = rPaM.GetPoint();
206 continue;
208 else
209 pPos = rDestShell.GetCursor()->GetPoint();
211 if( IsBlockMode() )
212 { // In block mode different insert positions will be calculated
213 // by simulated cursor movements from the given first insert position
214 if( nMove )
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 );
223 else
224 pInsertPos = std::make_shared<SwPosition>( *pPos );
225 ++nMove;
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() )
231 return false;
235 rDestShell.StartAllAction();
236 SwPosition *pPos = nullptr;
237 bool bRet = false;
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())
247 if( !pPos )
249 if( &rDestShell == this )
251 // First cursor represents the target position!!
252 rPaM.DeleteMark();
253 pPos = rPaM.GetPoint();
254 continue;
256 else
257 pPos = rDestShell.GetCursor()->GetPoint();
259 if( !bFirstMove )
261 if( pNextInsert != aInsertList.end() )
263 pPos = pNextInsert->get();
264 ++pNextInsert;
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() )
272 continue;
274 if( bFirstMove )
276 // Store start position of the new area
277 aSttNdIdx = pPos->GetNodeIndex()-1;
278 nSttCntIdx = pPos->GetContentIndex();
279 bFirstMove = false;
282 const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange(rPaM, *pPos, SwCopyFlags::CheckPosInFly) );
283 if (!bSuccess)
284 continue;
286 SwPaM aInsertPaM(*pPos, SwPosition(aSttNdIdx));
287 rDestShell.GetDoc()->MakeUniqueNumRules(aInsertPaM);
289 bRet = true;
292 // Maybe nothing has been moved?
293 if( !bFirstMove )
295 SwPaM* pCursor = rDestShell.GetCursor();
296 pCursor->SetMark();
297 pCursor->GetPoint()->Assign( aSttNdIdx.GetIndex()+1, nSttCntIdx );
298 pCursor->Exchange();
300 else
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" );
317 #endif
319 // close Undo container here
320 rDestShell.GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
321 rDestShell.EndAllAction();
323 rDestShell.SaveTableBoxContent( rDestShell.GetCursor()->GetPoint() );
325 return bRet;
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 );
339 bool bRet = false;
340 if (!HasReadonlySel(true))
342 StartAllAction();
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())
350 || bRet;
351 SaveTableBoxContent( rPaM.GetPoint() );
355 // close Undo container here
356 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY, nullptr);
357 EndAllAction();
359 return bRet;
362 /// special method for JOE's wizards
363 bool SwEditShell::DelFullPara()
365 bool bRet = false;
366 if( !IsTableMode() )
368 SwPaM* pCursor = GetCursor();
369 // no multi selection
370 if( !pCursor->IsMultiSelection() && !HasReadonlySel() )
372 CurrShell aCurr( this );
373 StartAllAction();
374 bRet = GetDoc()->getIDocumentContentOperations().DelFullPara( *pCursor );
375 EndAllAction();
378 return bRet;
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */