LibreOffice Module sw (master) 1
eddel.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <memory>
21#include <doc.hxx>
22#include <IDocumentUndoRedo.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>
32
33#include <strings.hrc>
34#include <vector>
35
36void SwEditShell::DeleteSel(SwPaM& rPam, bool const isArtificialSelection, bool *const pUndo)
37{
38 auto const oSelectAll(StartsWith_() != SwCursorShell::StartsWith::None
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())))
45 {
46 return;
47 }
48
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() &&
56 rPam.GetMarkNode().StartOfSectionNode() && !oSelectAll)
57 {
58 // group the Undo in the table
59 if( pUndo && !*pUndo )
60 {
61 GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
62 *pUndo = true;
63 }
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() )
71 {
72 *aDelPam.GetPoint() = *pEndSelPos;
73 pEndSelPos = nullptr; // misuse a pointer as a flag
74 }
75 else
76 {
77 // then go to the end of the selection
78 aDelPam.GetPoint()->Assign(rEndNd);
80 }
81 // skip protected boxes
82 if( !rNd.IsContentNode() ||
83 !rNd.IsInProtectSect() )
84 {
85 // delete everything
87 SaveTableBoxContent( aDelPam.GetPoint() );
88 }
89
90 if( !pEndSelPos ) // at the end of a selection
91 break;
92 aDelPam.DeleteMark();
93 aDelPam.Move( fnMoveForward, GoInContent ); // next box
94 } while( pEndSelPos );
95 }
96 else
97 {
98 std::optional<SwPaM> pNewPam;
99 SwPaM * pPam = &rPam;
100 if (oSelectAll)
101 {
102 if (!oSelectAll->second.empty())
103 {
104 SwRewriter aRewriter;
105 aRewriter.AddRule(UndoArg1, SwResId(STR_MULTISEL));
106 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::DELETE, &aRewriter);
107 }
108 // tdf#155685 tables at the end must be deleted separately
109 for (SwTableNode *const pTable : oSelectAll->second)
110 {
111 GetDoc()->DelTable(pTable);
112 }
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;
119 }
120 // delete everything
123 SaveTableBoxContent( pPam->GetPoint() );
124 if (oSelectAll && !oSelectAll->second.empty())
125 {
126 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END, nullptr);
127 }
128 }
129
130 // Selection is not needed anymore
131 rPam.DeleteMark();
132}
133
134bool SwEditShell::Delete(bool const isArtificialSelection)
135{
136 CurrShell aCurr( this );
137 bool bRet = false;
139 {
140 if (HasHiddenSections() &&
142 {
144 {
146 return bRet;
147 }
148 }
149
151 bool bUndo = GetCursor()->GetNext() != GetCursor();
152 if( bUndo ) // more than one selection?
153 {
154 SwRewriter aRewriter;
155 aRewriter.AddRule(UndoArg1, SwResId(STR_MULTISEL));
156
157 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::DELETE, &aRewriter);
158 }
159
160 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
161 {
162 DeleteSel(rPaM, isArtificialSelection, &bUndo);
163 }
164
165 // If undo container then close here
166 if( bUndo )
167 {
168 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::END, nullptr);
169 }
170 EndAllAction();
171 bRet = true;
172 }
173 else
174 {
176 if (!bRet)
177 {
178 InfoReadOnlyDialog(false);
179 }
180 }
181
182 return bRet;
183}
184
186{
187 CurrShell aCurr( &rDestShell );
188
189 // List of insert positions for smart insert of block selections
190 std::vector< std::shared_ptr<SwPosition> > aInsertList;
191
192 // Fill list of insert positions
193 {
194 SwPosition * pPos = nullptr;
195 std::shared_ptr<SwPosition> pInsertPos;
196 sal_uInt16 nMove = 0;
197 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
198 {
199 if( !pPos )
200 {
201 if( &rDestShell == this )
202 {
203 // First cursor represents the target position!!
204 rPaM.DeleteMark();
205 pPos = rPaM.GetPoint();
206 continue;
207 }
208 else
209 pPos = rDestShell.GetCursor()->GetPoint();
210 }
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 )
215 {
216 SwCursor aCursor( *pPos, nullptr);
217 if (aCursor.UpDown(false, nMove, nullptr, 0, *GetLayout()))
218 {
219 pInsertPos = std::make_shared<SwPosition>( *aCursor.GetPoint() );
220 aInsertList.push_back( pInsertPos );
221 }
222 }
223 else
224 pInsertPos = std::make_shared<SwPosition>( *pPos );
225 ++nMove;
226 }
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;
232 }
233 }
234
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();
243
244 rDestShell.GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
245 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
246 {
247 if( !pPos )
248 {
249 if( &rDestShell == this )
250 {
251 // First cursor represents the target position!!
252 rPaM.DeleteMark();
253 pPos = rPaM.GetPoint();
254 continue;
255 }
256 else
257 pPos = rDestShell.GetCursor()->GetPoint();
258 }
259 if( !bFirstMove )
260 {
261 if( pNextInsert != aInsertList.end() )
262 {
263 pPos = pNextInsert->get();
264 ++pNextInsert;
265 }
266 else if( IsBlockMode() )
268 }
269
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;
273
274 if( bFirstMove )
275 {
276 // Store start position of the new area
277 aSttNdIdx = pPos->GetNodeIndex()-1;
278 nSttCntIdx = pPos->GetContentIndex();
279 bFirstMove = false;
280 }
281
282 const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange(rPaM, *pPos, SwCopyFlags::CheckPosInFly) );
283 if (!bSuccess)
284 continue;
285
286 SwPaM aInsertPaM(*pPos, SwPosition(aSttNdIdx));
287 rDestShell.GetDoc()->MakeUniqueNumRules(aInsertPaM);
288
289 bRet = true;
290 }
291
292 // Maybe nothing has been moved?
293 if( !bFirstMove )
294 {
295 SwPaM* pCursor = rDestShell.GetCursor();
296 pCursor->SetMark();
297 pCursor->GetPoint()->Assign( aSttNdIdx.GetIndex()+1, nSttCntIdx );
298 pCursor->Exchange();
299 }
300 else
301 {
302 // If the cursor moved during move process, move also its GetMark
303 rDestShell.GetCursor()->SetMark();
304 rDestShell.GetCursor()->DeleteMark();
305 }
306#if OSL_DEBUG_LEVEL > 0
307 // check if the indices are registered in the correct nodes
308 {
309 for(SwPaM& rCmp : rDestShell.GetCursor()->GetRingContainer())
310 {
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" );
315 }
316 }
317#endif
318
319 // close Undo container here
320 rDestShell.GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
321 rDestShell.EndAllAction();
322
323 rDestShell.SaveTableBoxContent( rDestShell.GetCursor()->GetPoint() );
324
325 return bRet;
326}
327
335bool SwEditShell::Replace( const OUString& rNewStr, bool bRegExpRplc )
336{
337 CurrShell aCurr( this );
338
339 bool bRet = false;
340 if (!HasReadonlySel(true))
341 {
343 GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY, nullptr);
344
345 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
346 {
347 if( rPaM.HasMark() && *rPaM.GetMark() != *rPaM.GetPoint() )
348 {
349 bRet = sw::ReplaceImpl(rPaM, rNewStr, bRegExpRplc, *GetDoc(), GetLayout())
350 || bRet;
351 SaveTableBoxContent( rPaM.GetPoint() );
352 }
353 }
354
355 // close Undo container here
356 GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY, nullptr);
357 EndAllAction();
358 }
359 return bRet;
360}
361
364{
365 bool bRet = false;
366 if( !IsTableMode() )
367 {
368 SwPaM* pCursor = GetCursor();
369 // no multi selection
370 if( !pCursor->IsMultiSelection() && !HasReadonlySel() )
371 {
372 CurrShell aCurr( this );
374 bRet = GetDoc()->getIDocumentContentOperations().DelFullPara( *pCursor );
375 EndAllAction();
376 }
377 }
378 return bRet;
379}
380
381/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
@ CheckPosInFly
check if target position is in fly anchored at source range
@ ArtificialSelection
keyboard delete, artificial selection, avoid deleting flys
@ UndoArg1
Definition: SwRewriter.hxx:29
virtual bool DeleteAndJoin(SwPaM &, SwDeleteFlags flags=SwDeleteFlags::Default)=0
complete delete of a given PaM
virtual bool SplitNode(const SwPosition &rPos, bool bChkTableStart)=0
Split a node at rPos (implemented only for TextNode).
virtual bool DelFullPara(SwPaM &)=0
Delete full paragraphs.
StartsWith StartsWith_()
If document body starts with a table or starts/ends with hidden paragraph.
Definition: crsrsh.cxx:895
SwCursor * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:194
bool CursorInsideInputField() const
Definition: crstrvl.cxx:1094
bool HasReadonlySel(bool isReplace=false) const
Definition: crsrsh.cxx:3662
bool IsTableMode() const
Definition: crsrsh.hxx:668
bool IsBlockMode() const
Definition: crsrsh.hxx:665
void SaveTableBoxContent(const SwPosition *pPos=nullptr)
Definition: trvltbl.cxx:867
bool HasHiddenSections() const
Definition: crsrsh.cxx:3699
::std::optional<::std::pair< SwNode const *, ::std::vector< SwTableNode * > > > ExtendedSelectedAll() const
If ExtendedSelectAll() was called and selection didn't change since then.
Definition: crsrsh.cxx:823
SwCursor * GetNext()
Definition: swcrsr.hxx:219
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:329
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:158
SwNodes & GetNodes()
Definition: doc.hxx:422
void DelTable(SwTableNode *pTable)
Definition: ndtbl.cxx:1927
void MakeUniqueNumRules(const SwPaM &rPaM)
Definition: docnum.cxx:1230
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
bool RemoveParagraphMetadataFieldAtCursor()
Removes the paragraph metadata field at the current cursor, if any.
Definition: edfcol.cxx:2070
SAL_DLLPRIVATE void DeleteSel(SwPaM &rPam, bool isArtificialSelection, bool *pUndo=nullptr)
Definition: eddel.cxx:36
bool Delete(bool isArtificialSelection=false)
Delete content of all ranges.
Definition: eddel.cxx:134
bool DelFullPara()
Remove a complete paragraph.
Definition: eddel.cxx:363
bool Replace(const OUString &rNewStr, bool bRegExpRplc)
Replace a selected range in a TextNode by given string.
Definition: eddel.cxx:335
virtual bool WarnHiddenSectionDialog() const
Selected area has hidden content.
Definition: editsh.hxx:634
bool Copy(SwEditShell &rDestShell)
Copy content of all ranges at current position of cursor to given Shell.
Definition: eddel.cxx:185
void EndAllAction()
Definition: edws.cxx:97
virtual void InfoReadOnlyDialog(bool) const
Selected area has readonly content.
Definition: editsh.hxx:628
Marks a node in the document model.
Definition: ndindex.hxx:31
Base class of the Writer document model elements.
Definition: node.hxx:98
SwNodeOffset GetIndex() const
Definition: node.hxx:312
bool IsInProtectSect() const
Is node in a protected area?
Definition: node.cxx:439
bool IsContentNode() const
Definition: node.hxx:188
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:380
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:153
const SwEndNode * EndOfSectionNode() const
Definition: node.hxx:695
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:188
const SwPosition * GetMark() const
Definition: pam.hxx:255
SwNode & GetPointNode() const
Definition: pam.hxx:275
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:643
void Exchange()
Definition: pam.hxx:242
bool IsMultiSelection() const
Definition: pam.hxx:322
bool Move(SwMoveFnCollection const &fnMove=fnMoveForward, SwGoInDoc fnGo=GoInContent)
Movement of cursor.
Definition: pam.cxx:657
const SwPosition * End() const
Definition: pam.hxx:263
void DeleteMark()
Definition: pam.hxx:232
SwNode & GetMarkNode() const
Definition: pam.hxx:276
const SwPosition * GetPoint() const
Definition: pam.hxx:253
const SwPosition * Start() const
Definition: pam.hxx:258
bool HasMark() const
A PaM marks a selection if Point and Mark are distinct positions.
Definition: pam.hxx:251
void AddRule(SwUndoArg eWhat, const OUString &rWith)
Definition: SwRewriter.cxx:25
Represents the current text cursor of one opened edit window.
Definition: viscrs.hxx:140
IDocumentContentOperations & getIDocumentContentOperations()
Provides access to the content operations interface.
Definition: viewsh.cxx:2829
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2163
SwDoc * GetDoc() const
Definition: viewsh.hxx:308
ring_container GetRingContainer()
Definition: ring.hxx:240
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
bool ReplaceImpl(SwPaM &rCursor, OUString const &rReplacement, bool const bRegExp, SwDoc &rDoc, SwRootFrame const *const pLayout)
Definition: findtxt.cxx:1040
bool GoInContent(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:1203
SwMoveFnCollection const & fnMoveBackward
Definition: paminit.cxx:60
SwMoveFnCollection const & fnMoveForward
SwPam::Move()/Find() default argument.
Definition: paminit.cxx:61
Marks a position in the document model.
Definition: pam.hxx:38
void Assign(const SwNode &rNd, SwNodeOffset nDelta, sal_Int32 nContentOffset=0)
These all set both nNode and nContent.
Definition: pam.cxx:231
SwNodeOffset GetNodeIndex() const
Definition: pam.hxx:78
sal_Int32 GetContentIndex() const
Definition: pam.hxx:85
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:168
bool IsFlySelectedByCursor(SwDoc const &rDoc, SwPosition const &rStart, SwPosition const &rEnd)
check at-char and at-para flys in rDoc
Definition: undobj.cxx:1730