Add a comment to clarify what kind of inputs the class handles
[LibreOffice.git] / sw / source / core / edit / eddel.cxx
blob654b9b0fb54782c8c01c8c8d4a7b9a891abc1ee6
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 goLeft,
37 bool* const pUndo)
39 const ExtendedSelection oSelectAll(StartsWith_() != SwCursorShell::StartsWith::None
40 ? ExtendedSelectedAll()
41 : ExtendedSelection{});
42 // only for selections
43 if (!rPam.HasMark()
44 || (*rPam.GetPoint() == *rPam.GetMark()
45 && !IsFlySelectedByCursor(*GetDoc(), *rPam.Start(), *rPam.End())))
47 return;
50 // Is the selection in a table? Then delete only the content of the selected boxes.
51 // Here, there are two cases:
52 // 1. Point and Mark are in one box, delete selection as usual
53 // 2. Point and Mark are in different boxes, search all selected boxes and delete content
54 // 3. Point and Mark are at the document start and end, Point is in a table: delete selection as usual
55 if( rPam.GetPointNode().FindTableNode() &&
56 rPam.GetPointNode().StartOfSectionNode() !=
57 rPam.GetMarkNode().StartOfSectionNode() && !oSelectAll)
59 // group the Undo in the table
60 if( pUndo && !*pUndo )
62 GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
63 *pUndo = true;
65 SwPaM aDelPam( *rPam.Start() );
66 const SwPosition* pEndSelPos = rPam.End();
67 do {
68 aDelPam.SetMark();
69 SwNode& rNd = aDelPam.GetPointNode();
70 const SwNode& rEndNd = *rNd.EndOfSectionNode();
71 if( pEndSelPos->GetNodeIndex() <= rEndNd.GetIndex() )
73 *aDelPam.GetPoint() = *pEndSelPos;
74 pEndSelPos = nullptr; // misuse a pointer as a flag
76 else
78 // then go to the end of the selection
79 aDelPam.GetPoint()->Assign(rEndNd);
80 aDelPam.Move( fnMoveBackward, GoInContent );
82 // skip protected boxes
83 if( !rNd.IsContentNode() ||
84 !rNd.IsInProtectSect() )
86 // delete everything
87 GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aDelPam );
88 SaveTableBoxContent( aDelPam.GetPoint() );
91 if( !pEndSelPos ) // at the end of a selection
92 break;
93 aDelPam.DeleteMark();
94 aDelPam.Move( fnMoveForward, GoInContent ); // next box
95 } while( pEndSelPos );
97 else
99 std::optional<SwPaM> pNewPam;
100 SwPaM * pPam = &rPam;
101 if (oSelectAll)
103 if (!oSelectAll->second.empty())
105 SwRewriter aRewriter;
106 aRewriter.AddRule(UndoArg1, SwResId(STR_MULTISEL));
107 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::DELETE, &aRewriter);
109 // tdf#155685 tables at the end must be deleted separately
110 for (SwTableNode *const pTable : oSelectAll->second)
112 GetDoc()->DelTable(pTable);
114 assert(dynamic_cast<SwShellCursor*>(&rPam)); // must be corrected pam
115 pNewPam.emplace(*rPam.GetMark(), *rPam.GetPoint());
116 // Selection starts at the first para of the first cell, but we
117 // want to delete the table node before the first cell as well.
118 pNewPam->Start()->Assign(*oSelectAll->first);
119 pPam = &*pNewPam;
121 // delete everything
122 GetDoc()->getIDocumentContentOperations().DeleteAndJoin(*pPam,
123 isArtificialSelection ? SwDeleteFlags::ArtificialSelection : SwDeleteFlags::Default);
124 SaveTableBoxContent( pPam->GetPoint() );
125 if (oSelectAll && !oSelectAll->second.empty())
127 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END, nullptr);
131 rPam.Normalize(goLeft); // change tracking case: will make sure to end up in the correct point
132 // Selection is not needed anymore
133 rPam.DeleteMark();
136 bool SwEditShell::Delete(bool const isArtificialSelection, bool goLeft)
138 CurrShell aCurr( this );
139 bool bRet = false;
140 if ( !HasReadonlySel() || CursorInsideInputField() )
142 if (HasHiddenSections() &&
143 officecfg::Office::Writer::Content::Display::ShowWarningHiddenSection::get())
145 if (!WarnHiddenSectionDialog())
147 bRet = RemoveParagraphMetadataFieldAtCursor();
148 return bRet;
152 StartAllAction();
153 bool bUndo = GetCursor()->GetNext() != GetCursor();
154 if( bUndo ) // more than one selection?
156 SwRewriter aRewriter;
157 aRewriter.AddRule(UndoArg1, SwResId(STR_MULTISEL));
159 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::DELETE, &aRewriter);
162 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
164 DeleteSel(rPaM, isArtificialSelection, goLeft, &bUndo);
167 // If undo container then close here
168 if( bUndo )
170 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END, nullptr);
172 EndAllAction();
173 bRet = true;
175 else
177 bRet = RemoveParagraphMetadataFieldAtCursor();
178 if (!bRet)
180 InfoReadOnlyDialog(false);
184 return bRet;
187 bool SwEditShell::Copy( SwEditShell& rDestShell )
189 CurrShell aCurr( &rDestShell );
191 // List of insert positions for smart insert of block selections
192 std::vector< std::shared_ptr<SwPosition> > aInsertList;
194 // Fill list of insert positions
196 SwPosition * pPos = nullptr;
197 std::shared_ptr<SwPosition> pInsertPos;
198 sal_uInt16 nMove = 0;
199 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
201 if( !pPos )
203 if( &rDestShell == this )
205 // First cursor represents the target position!!
206 rPaM.DeleteMark();
207 pPos = rPaM.GetPoint();
208 continue;
210 else
211 pPos = rDestShell.GetCursor()->GetPoint();
213 if( IsBlockMode() )
214 { // In block mode different insert positions will be calculated
215 // by simulated cursor movements from the given first insert position
216 if( nMove )
218 SwCursor aCursor( *pPos, nullptr);
219 if (aCursor.UpDown(false, nMove, nullptr, 0, *GetLayout()))
221 pInsertPos = std::make_shared<SwPosition>( *aCursor.GetPoint() );
222 aInsertList.push_back( pInsertPos );
225 else
226 pInsertPos = std::make_shared<SwPosition>( *pPos );
227 ++nMove;
229 SwPosition *pTmp = IsBlockMode() ? pInsertPos.get() : pPos;
230 // Check if a selection would be copied into itself
231 if( rDestShell.GetDoc() == GetDoc() &&
232 *rPaM.Start() <= *pTmp && *pTmp < *rPaM.End() )
233 return false;
237 rDestShell.StartAllAction();
238 SwPosition *pPos = nullptr;
239 bool bRet = false;
240 bool bFirstMove = true;
241 SwNodeIndex aSttNdIdx( rDestShell.GetDoc()->GetNodes() );
242 sal_Int32 nSttCntIdx = 0;
243 // For block selection this list is filled with the insert positions
244 auto pNextInsert = aInsertList.begin();
246 rDestShell.GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
247 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
249 if( !pPos )
251 if( &rDestShell == this )
253 // First cursor represents the target position!!
254 rPaM.DeleteMark();
255 pPos = rPaM.GetPoint();
256 continue;
258 else
259 pPos = rDestShell.GetCursor()->GetPoint();
261 if( !bFirstMove )
263 if( pNextInsert != aInsertList.end() )
265 pPos = pNextInsert->get();
266 ++pNextInsert;
268 else if( IsBlockMode() )
269 GetDoc()->getIDocumentContentOperations().SplitNode( *pPos, false );
272 // Only for a selection (non-text nodes have selection but Point/GetMark are equal)
273 if( !rPaM.HasMark() || *rPaM.GetPoint() == *rPaM.GetMark() )
274 continue;
276 if( bFirstMove )
278 // Store start position of the new area
279 aSttNdIdx = pPos->GetNodeIndex()-1;
280 nSttCntIdx = pPos->GetContentIndex();
281 bFirstMove = false;
284 const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange(rPaM, *pPos, SwCopyFlags::CheckPosInFly) );
285 if (!bSuccess)
286 continue;
288 SwPaM aInsertPaM(*pPos, SwPosition(aSttNdIdx));
289 rDestShell.GetDoc()->MakeUniqueNumRules(aInsertPaM);
291 bRet = true;
294 // Maybe nothing has been moved?
295 if( !bFirstMove )
297 SwPaM* pCursor = rDestShell.GetCursor();
298 pCursor->SetMark();
299 pCursor->GetPoint()->Assign( aSttNdIdx.GetIndex()+1, nSttCntIdx );
300 pCursor->Exchange();
302 else
304 // If the cursor moved during move process, move also its GetMark
305 rDestShell.GetCursor()->SetMark();
306 rDestShell.GetCursor()->DeleteMark();
308 #if OSL_DEBUG_LEVEL > 0
309 // check if the indices are registered in the correct nodes
311 for(SwPaM& rCmp : rDestShell.GetCursor()->GetRingContainer())
313 OSL_ENSURE( rCmp.GetPoint()->GetContentNode()
314 == rCmp.GetPointContentNode(), "Point in wrong Node" );
315 OSL_ENSURE( rCmp.GetMark()->GetContentNode()
316 == rCmp.GetMarkContentNode(), "Mark in wrong Node" );
319 #endif
321 // close Undo container here
322 rDestShell.GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
323 rDestShell.EndAllAction();
325 rDestShell.SaveTableBoxContent( rDestShell.GetCursor()->GetPoint() );
327 return bRet;
330 /** Replace a selected area in a text node with a given string.
332 * Intended for "search & replace".
334 * @param bRegExpRplc if <true> replace tabs (\\t) and replace with found string (not \&).
335 * E.g. [Fnd: "zzz", Repl: "xx\t\\t..&..\&"] --> "xx\t<Tab>..zzz..&"
337 bool SwEditShell::Replace( const OUString& rNewStr, bool bRegExpRplc )
339 CurrShell aCurr( this );
341 bool bRet = false;
342 if (!HasReadonlySel(true))
344 StartAllAction();
345 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY, nullptr);
347 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
349 if( rPaM.HasMark() && *rPaM.GetMark() != *rPaM.GetPoint() )
351 bRet = sw::ReplaceImpl(rPaM, rNewStr, bRegExpRplc, *GetDoc(), GetLayout())
352 || bRet;
353 SaveTableBoxContent( rPaM.GetPoint() );
357 // close Undo container here
358 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY, nullptr);
359 EndAllAction();
361 return bRet;
364 /** Replace a selected area in a text node with a given string.
366 * Method to replace a text selection with a new string while
367 * keeping possible comments (they will be moved to the end
368 * of the selection).
370 * @param rNewStr the new string which the selected area is to be replaced with
371 * @return true, if something has been replaced, false otherwise.
373 bool SwEditShell::ReplaceKeepComments( const OUString& rNewStr)
375 bool bRet = false;
376 SwPaM *pCursor = GetCursor();
378 if(pCursor != nullptr)
380 // go sure that the text selection pointed to by pCursor is valid
381 if(pCursor->HasMark())
383 // preserve comments inside of the number by deleting number portions starting from the back
384 OUString aSelectedText = pCursor->GetText();
385 sal_Int32 nCommentPos(aSelectedText.lastIndexOf(CH_TXTATR_INWORD));
386 // go sure that we have a valid selection and a comment has been found
387 while (nCommentPos > -1)
389 // select the part of the text after the last found comment
390 // selection start:
391 pCursor->GetPoint()->AdjustContent(nCommentPos + 1);
392 // selection end is left where it is -> will be adjusted later on
393 // delete the part of the word after the last found comment
394 Replace(OUString(), false);
395 // put the selection start back to the beginning of the word
396 pCursor->GetPoint()->AdjustContent(-(nCommentPos + 1));
397 // adjust the selection end, so that the last comment is no longer selected:
398 pCursor->GetMark()->AdjustContent(-1);
399 // search for the next possible comment
400 aSelectedText = pCursor->GetText();
401 nCommentPos = aSelectedText.lastIndexOf(CH_TXTATR_INWORD);
403 bRet = Replace(rNewStr, false);
407 return bRet;
410 /// special method for JOE's wizards
411 bool SwEditShell::DelFullPara()
413 bool bRet = false;
414 if( !IsTableMode() )
416 SwPaM* pCursor = GetCursor();
417 // no multi selection
418 if( !pCursor->IsMultiSelection() && !HasReadonlySel() )
420 CurrShell aCurr( this );
421 StartAllAction();
422 bRet = GetDoc()->getIDocumentContentOperations().DelFullPara( *pCursor );
423 EndAllAction();
426 return bRet;
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */