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 goLeft
,
39 const ExtendedSelection
oSelectAll(StartsWith_() != SwCursorShell::StartsWith::None
40 ? ExtendedSelectedAll()
41 : ExtendedSelection
{});
42 // only for selections
44 || (*rPam
.GetPoint() == *rPam
.GetMark()
45 && !IsFlySelectedByCursor(*GetDoc(), *rPam
.Start(), *rPam
.End())))
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 );
65 SwPaM
aDelPam( *rPam
.Start() );
66 const SwPosition
* pEndSelPos
= rPam
.End();
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
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() )
87 GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aDelPam
);
88 SaveTableBoxContent( aDelPam
.GetPoint() );
91 if( !pEndSelPos
) // at the end of a selection
94 aDelPam
.Move( fnMoveForward
, GoInContent
); // next box
95 } while( pEndSelPos
);
99 std::optional
<SwPaM
> pNewPam
;
100 SwPaM
* pPam
= &rPam
;
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
);
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
136 bool SwEditShell::Delete(bool const isArtificialSelection
, bool goLeft
)
138 CurrShell
aCurr( this );
140 if ( !HasReadonlySel() || CursorInsideInputField() )
142 if (HasHiddenSections() &&
143 officecfg::Office::Writer::Content::Display::ShowWarningHiddenSection::get())
145 if (!WarnHiddenSectionDialog())
147 bRet
= RemoveParagraphMetadataFieldAtCursor();
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
170 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END
, nullptr);
177 bRet
= RemoveParagraphMetadataFieldAtCursor();
180 InfoReadOnlyDialog(false);
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())
203 if( &rDestShell
== this )
205 // First cursor represents the target position!!
207 pPos
= rPaM
.GetPoint();
211 pPos
= rDestShell
.GetCursor()->GetPoint();
214 { // In block mode different insert positions will be calculated
215 // by simulated cursor movements from the given first insert position
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
);
226 pInsertPos
= std::make_shared
<SwPosition
>( *pPos
);
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() )
237 rDestShell
.StartAllAction();
238 SwPosition
*pPos
= nullptr;
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())
251 if( &rDestShell
== this )
253 // First cursor represents the target position!!
255 pPos
= rPaM
.GetPoint();
259 pPos
= rDestShell
.GetCursor()->GetPoint();
263 if( pNextInsert
!= aInsertList
.end() )
265 pPos
= pNextInsert
->get();
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() )
278 // Store start position of the new area
279 aSttNdIdx
= pPos
->GetNodeIndex()-1;
280 nSttCntIdx
= pPos
->GetContentIndex();
284 const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange(rPaM
, *pPos
, SwCopyFlags::CheckPosInFly
) );
288 SwPaM
aInsertPaM(*pPos
, SwPosition(aSttNdIdx
));
289 rDestShell
.GetDoc()->MakeUniqueNumRules(aInsertPaM
);
294 // Maybe nothing has been moved?
297 SwPaM
* pCursor
= rDestShell
.GetCursor();
299 pCursor
->GetPoint()->Assign( aSttNdIdx
.GetIndex()+1, nSttCntIdx
);
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" );
321 // close Undo container here
322 rDestShell
.GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END
, nullptr );
323 rDestShell
.EndAllAction();
325 rDestShell
.SaveTableBoxContent( rDestShell
.GetCursor()->GetPoint() );
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 );
342 if (!HasReadonlySel(true))
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())
353 SaveTableBoxContent( rPaM
.GetPoint() );
357 // close Undo container here
358 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY
, nullptr);
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
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
)
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
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);
410 /// special method for JOE's wizards
411 bool SwEditShell::DelFullPara()
416 SwPaM
* pCursor
= GetCursor();
417 // no multi selection
418 if( !pCursor
->IsMultiSelection() && !HasReadonlySel() )
420 CurrShell
aCurr( this );
422 bRet
= GetDoc()->getIDocumentContentOperations().DelFullPara( *pCursor
);
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */