Line data Source code
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 <hintids.hxx>
21 : #include <doc.hxx>
22 : #include <IDocumentUndoRedo.hxx>
23 : #include <IDocumentContentOperations.hxx>
24 : #include <editsh.hxx>
25 : #include <cntfrm.hxx>
26 : #include <pam.hxx>
27 : #include <swundo.hxx>
28 : #include <edimp.hxx>
29 : #include <IMark.hxx>
30 : #include <docary.hxx>
31 : #include <SwRewriter.hxx>
32 : #include <globals.hrc>
33 :
34 : #include <comcore.hrc>
35 : #include <list>
36 :
37 6 : void SwEditShell::DeleteSel( SwPaM& rPam, bool* pUndo )
38 : {
39 6 : bool bSelectAll = StartsWithTable() && ExtendedSelectedAll(/*bFootnotes =*/ false);
40 : // only for selections
41 6 : if( !rPam.HasMark() || *rPam.GetPoint() == *rPam.GetMark())
42 6 : return;
43 :
44 : // Is the selection in a table? Then delete only the content of the selected boxes.
45 : // Here, there are two cases:
46 : // 1. Point and Mark are in one box, delete selection as usual
47 : // 2. Point and Mark are in different boxes, search all selected boxes and delete content
48 : // 3. Point and Mark are at the document start and end, Point is in a table: delete selection as usual
49 14 : if( rPam.GetNode().FindTableNode() &&
50 2 : rPam.GetNode().StartOfSectionNode() !=
51 9 : rPam.GetNode(false).StartOfSectionNode() && !bSelectAll )
52 : {
53 : // group the Undo in the table
54 0 : if( pUndo && !*pUndo )
55 : {
56 0 : GetDoc()->GetIDocumentUndoRedo().StartUndo( UNDO_START, NULL );
57 0 : *pUndo = true;
58 : }
59 0 : SwPaM aDelPam( *rPam.Start() );
60 0 : const SwPosition* pEndSelPos = rPam.End();
61 0 : do {
62 0 : aDelPam.SetMark();
63 0 : SwNode& rNd = aDelPam.GetNode();
64 0 : const SwNode& rEndNd = *rNd.EndOfSectionNode();
65 0 : if( pEndSelPos->nNode.GetIndex() <= rEndNd.GetIndex() )
66 : {
67 0 : *aDelPam.GetPoint() = *pEndSelPos;
68 0 : pEndSelPos = 0; // misuse a pointer as a flag
69 : }
70 : else
71 : {
72 : // then go to the end of the selection
73 0 : aDelPam.GetPoint()->nNode = rEndNd;
74 0 : aDelPam.Move( fnMoveBackward, fnGoContent );
75 : }
76 : // skip protected boxes
77 0 : if( !rNd.IsContentNode() ||
78 0 : !rNd.IsInProtectSect() )
79 : {
80 : // delete everything
81 0 : GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aDelPam );
82 0 : SaveTableBoxContent( aDelPam.GetPoint() );
83 : }
84 :
85 0 : if( !pEndSelPos ) // at the end of a selection
86 0 : break;
87 0 : aDelPam.DeleteMark();
88 0 : aDelPam.Move( fnMoveForward, fnGoContent ); // next box
89 0 : } while( pEndSelPos );
90 : }
91 : else
92 : {
93 6 : std::unique_ptr<SwPaM> pNewPam;
94 6 : SwPaM * pPam = &rPam;
95 6 : if (bSelectAll)
96 : {
97 : assert(dynamic_cast<SwShellCrsr*>(&rPam)); // must be corrected pam
98 1 : pNewPam.reset(new SwPaM(*rPam.GetMark(), *rPam.GetPoint()));
99 : // Selection starts at the first para of the first cell, but we
100 : // want to delete the table node before the first cell as well.
101 1 : pNewPam->Start()->nNode = pNewPam->Start()->nNode.GetNode().FindTableNode()->GetIndex();
102 1 : pNewPam->Start()->nContent.Assign(nullptr, 0);
103 1 : pPam = pNewPam.get();
104 : }
105 : // delete everything
106 6 : GetDoc()->getIDocumentContentOperations().DeleteAndJoin(*pPam);
107 6 : SaveTableBoxContent( pPam->GetPoint() );
108 : }
109 :
110 : // Selection is not needed anymore
111 6 : rPam.DeleteMark();
112 : }
113 :
114 6 : long SwEditShell::Delete()
115 : {
116 6 : SET_CURR_SHELL( this );
117 6 : long nRet = 0;
118 6 : if ( !HasReadonlySel() || CrsrInsideInputField() )
119 : {
120 6 : StartAllAction();
121 :
122 6 : bool bUndo = GetCrsr()->GetNext() != GetCrsr();
123 6 : if( bUndo ) // more than one selection?
124 : {
125 0 : SwRewriter aRewriter;
126 0 : aRewriter.AddRule(UndoArg1, SW_RESSTR(STR_MULTISEL));
127 :
128 0 : GetDoc()->GetIDocumentUndoRedo().StartUndo(UNDO_DELETE, &aRewriter);
129 : }
130 :
131 12 : for(SwPaM& rPaM : GetCrsr()->GetRingContainer())
132 : {
133 6 : DeleteSel( rPaM, &bUndo );
134 : }
135 :
136 : // If undo container then close here
137 6 : if( bUndo )
138 : {
139 0 : GetDoc()->GetIDocumentUndoRedo().EndUndo(UNDO_END, 0);
140 : }
141 6 : EndAllAction();
142 6 : nRet = 1;
143 : }
144 6 : return nRet;
145 : }
146 :
147 0 : bool SwEditShell::Copy( SwEditShell* pDestShell )
148 : {
149 0 : if( !pDestShell )
150 0 : pDestShell = this;
151 :
152 0 : SET_CURR_SHELL( pDestShell );
153 :
154 : // List of insert positions for smart insert of block selections
155 0 : std::list< boost::shared_ptr<SwPosition> > aInsertList;
156 :
157 : // Fill list of insert positions
158 : {
159 0 : SwPosition * pPos = 0;
160 0 : boost::shared_ptr<SwPosition> pInsertPos;
161 0 : sal_uInt16 nMove = 0;
162 0 : for(SwPaM& rPaM : GetCrsr()->GetRingContainer())
163 : {
164 0 : if( !pPos )
165 : {
166 0 : if( pDestShell == this )
167 : {
168 : // First cursor represents the target position!!
169 0 : rPaM.DeleteMark();
170 0 : pPos = rPaM.GetPoint();
171 0 : continue;
172 : }
173 : else
174 0 : pPos = pDestShell->GetCrsr()->GetPoint();
175 : }
176 0 : if( IsBlockMode() )
177 : { // In block mode different insert positions will be calculated
178 : // by simulated cursor movements from the given first insert position
179 0 : if( nMove )
180 : {
181 0 : SwCursor aCrsr( *pPos, 0, false);
182 0 : if( aCrsr.UpDown( false, nMove, 0, 0 ) )
183 : {
184 0 : pInsertPos.reset( new SwPosition( *aCrsr.GetPoint() ) );
185 0 : aInsertList.push_back( pInsertPos );
186 0 : }
187 : }
188 : else
189 0 : pInsertPos.reset( new SwPosition( *pPos ) );
190 0 : ++nMove;
191 : }
192 0 : SwPosition *pTmp = IsBlockMode() ? pInsertPos.get() : pPos;
193 : // Check if a selection would be copied into itself
194 0 : if( pDestShell->GetDoc() == GetDoc() &&
195 0 : *rPaM.Start() <= *pTmp && *pTmp < *rPaM.End() )
196 0 : return false;
197 0 : }
198 : }
199 :
200 0 : pDestShell->StartAllAction();
201 0 : SwPosition *pPos = 0;
202 0 : bool bRet = false;
203 0 : bool bFirstMove = true;
204 0 : SwNodeIndex aSttNdIdx( pDestShell->GetDoc()->GetNodes() );
205 0 : sal_Int32 nSttCntIdx = 0;
206 : // For block selection this list is filled with the insert positions
207 0 : std::list< boost::shared_ptr<SwPosition> >::iterator pNextInsert = aInsertList.begin();
208 :
209 0 : pDestShell->GetDoc()->GetIDocumentUndoRedo().StartUndo( UNDO_START, NULL );
210 0 : for(SwPaM& rPaM : GetCrsr()->GetRingContainer())
211 : {
212 0 : if( !pPos )
213 : {
214 0 : if( pDestShell == this )
215 : {
216 : // First cursor represents the target position!!
217 0 : rPaM.DeleteMark();
218 0 : pPos = rPaM.GetPoint();
219 0 : continue;
220 : }
221 : else
222 0 : pPos = pDestShell->GetCrsr()->GetPoint();
223 : }
224 0 : if( !bFirstMove )
225 : {
226 0 : if( pNextInsert != aInsertList.end() )
227 : {
228 0 : pPos = pNextInsert->get();
229 0 : ++pNextInsert;
230 : }
231 0 : else if( IsBlockMode() )
232 0 : GetDoc()->getIDocumentContentOperations().SplitNode( *pPos, false );
233 : }
234 :
235 : // Only for a selection (non-text nodes have selection but Point/GetMark are equal)
236 0 : if( !rPaM.HasMark() || *rPaM.GetPoint() == *rPaM.GetMark() )
237 0 : continue;
238 :
239 0 : if( bFirstMove )
240 : {
241 : // Store start position of the new area
242 0 : aSttNdIdx = pPos->nNode.GetIndex()-1;
243 0 : nSttCntIdx = pPos->nContent.GetIndex();
244 0 : bFirstMove = false;
245 : }
246 :
247 0 : const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange( rPaM, *pPos, /*bCopyAll=*/false, /*bCheckPos=*/true ) );
248 0 : if (!bSuccess)
249 0 : continue;
250 :
251 0 : SwPaM aInsertPaM(*pPos, SwPosition(aSttNdIdx));
252 0 : pDestShell->GetDoc()->MakeUniqueNumRules(aInsertPaM);
253 :
254 0 : bRet = true;
255 0 : }
256 :
257 : // Maybe nothing has been moved?
258 0 : if( !bFirstMove )
259 : {
260 0 : SwPaM* pCrsr = pDestShell->GetCrsr();
261 0 : pCrsr->SetMark();
262 0 : pCrsr->GetPoint()->nNode = aSttNdIdx.GetIndex()+1;
263 0 : pCrsr->GetPoint()->nContent.Assign( pCrsr->GetContentNode(),nSttCntIdx);
264 0 : pCrsr->Exchange();
265 : }
266 : else
267 : {
268 : // If the cursor moved during move process, move also its GetMark
269 0 : pDestShell->GetCrsr()->SetMark();
270 0 : pDestShell->GetCrsr()->DeleteMark();
271 : }
272 : #if OSL_DEBUG_LEVEL > 0
273 : // check if the indices are registered in the correct nodes
274 : {
275 : for(SwPaM& rCmp : pDestShell->GetCrsr()->GetRingContainer())
276 : {
277 : OSL_ENSURE( rCmp.GetPoint()->nContent.GetIdxReg()
278 : == rCmp.GetContentNode(), "Point in wrong Node" );
279 : OSL_ENSURE( rCmp.GetMark()->nContent.GetIdxReg()
280 : == rCmp.GetContentNode(false), "Mark in wrong Node" );
281 : bool bTst = *rCmp.GetPoint() == *rCmp.GetMark();
282 : (void) bTst;
283 : }
284 : }
285 : #endif
286 :
287 : // close Undo container here
288 0 : pDestShell->GetDoc()->GetIDocumentUndoRedo().EndUndo( UNDO_END, NULL );
289 0 : pDestShell->EndAllAction();
290 :
291 0 : pDestShell->SaveTableBoxContent( pDestShell->GetCrsr()->GetPoint() );
292 :
293 0 : return bRet;
294 : }
295 :
296 : /** Replace a selected area in a text node with a given string.
297 : *
298 : * Intended for "search & replace".
299 : *
300 : * @param bRegExpRplc if <true> replace tabs (\\t) and replace with found string (not \&).
301 : * E.g. [Fnd: "zzz", Repl: "xx\t\\t..&..\&"] --> "xx\t<Tab>..zzz..&"
302 : */
303 0 : bool SwEditShell::Replace( const OUString& rNewStr, bool bRegExpRplc )
304 : {
305 0 : SET_CURR_SHELL( this );
306 :
307 0 : bool bRet = false;
308 0 : if( !HasReadonlySel() )
309 : {
310 0 : StartAllAction();
311 0 : GetDoc()->GetIDocumentUndoRedo().StartUndo(UNDO_EMPTY, NULL);
312 :
313 0 : for(SwPaM& rPaM : GetCrsr()->GetRingContainer())
314 : {
315 0 : if( rPaM.HasMark() && *rPaM.GetMark() != *rPaM.GetPoint() )
316 : {
317 0 : bRet = GetDoc()->getIDocumentContentOperations().ReplaceRange( rPaM, rNewStr, bRegExpRplc )
318 0 : || bRet;
319 0 : SaveTableBoxContent( rPaM.GetPoint() );
320 : }
321 : }
322 :
323 : // close Undo container here
324 0 : GetDoc()->GetIDocumentUndoRedo().EndUndo(UNDO_EMPTY, NULL);
325 0 : EndAllAction();
326 : }
327 0 : return bRet;
328 : }
329 :
330 : /// special method for JOE's wizards
331 0 : bool SwEditShell::DelFullPara()
332 : {
333 0 : bool bRet = false;
334 0 : if( !IsTableMode() )
335 : {
336 0 : SwPaM* pCrsr = GetCrsr();
337 : // no multi selection
338 0 : if( !pCrsr->IsMultiSelection() && !HasReadonlySel() )
339 : {
340 0 : SET_CURR_SHELL( this );
341 0 : StartAllAction();
342 0 : bRet = GetDoc()->getIDocumentContentOperations().DelFullPara( *pCrsr );
343 0 : EndAllAction();
344 : }
345 : }
346 0 : return bRet;
347 177 : }
348 :
349 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|