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 8 : void SwEditShell::DeleteSel( SwPaM& rPam, bool* pUndo )
38 : {
39 8 : bool bSelectAll = StartsWithTable() && ExtendedSelectedAll(/*bFootnotes =*/ false);
40 : // only for selections
41 8 : if( !rPam.HasMark() || *rPam.GetPoint() == *rPam.GetMark())
42 8 : 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 20 : if( rPam.GetNode().FindTableNode() &&
50 4 : rPam.GetNode().StartOfSectionNode() !=
51 16 : 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, fnGoCntnt );
75 : }
76 : // skip protected boxes
77 0 : if( !rNd.IsCntntNode() ||
78 0 : !rNd.IsInProtectSect() )
79 : {
80 : // delete everything
81 0 : GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aDelPam );
82 0 : SaveTblBoxCntnt( 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, fnGoCntnt ); // next box
89 0 : } while( pEndSelPos );
90 : }
91 : else
92 : {
93 8 : SwPaM aPam(rPam);
94 8 : if (bSelectAll)
95 : // Selection starts at the first para of the first cell, but we
96 : // want to delete the table node before the first cell as well.
97 4 : aPam.Start()->nNode = aPam.Start()->nNode.GetNode().FindTableNode()->GetIndex();
98 : // delete everything
99 8 : GetDoc()->getIDocumentContentOperations().DeleteAndJoin( aPam );
100 8 : SaveTblBoxCntnt( aPam.GetPoint() );
101 : }
102 :
103 : // Selection is not needed anymore
104 8 : rPam.DeleteMark();
105 : }
106 :
107 8 : long SwEditShell::Delete()
108 : {
109 8 : SET_CURR_SHELL( this );
110 8 : long nRet = 0;
111 8 : if ( !HasReadonlySel() || CrsrInsideInputFld() )
112 : {
113 8 : StartAllAction();
114 :
115 8 : bool bUndo = GetCrsr()->GetNext() != GetCrsr();
116 8 : if( bUndo ) // more than one selection?
117 : {
118 0 : SwRewriter aRewriter;
119 0 : aRewriter.AddRule(UndoArg1, SW_RESSTR(STR_MULTISEL));
120 :
121 0 : GetDoc()->GetIDocumentUndoRedo().StartUndo(UNDO_DELETE, &aRewriter);
122 : }
123 :
124 16 : FOREACHPAM_START(GetCrsr())
125 8 : DeleteSel( *PCURCRSR, &bUndo );
126 8 : FOREACHPAM_END()
127 :
128 : // If undo container then close here
129 8 : if( bUndo )
130 : {
131 0 : GetDoc()->GetIDocumentUndoRedo().EndUndo(UNDO_END, 0);
132 : }
133 8 : EndAllAction();
134 8 : nRet = 1;
135 : }
136 8 : return nRet;
137 : }
138 :
139 0 : long SwEditShell::Copy( SwEditShell* pDestShell )
140 : {
141 0 : if( !pDestShell )
142 0 : pDestShell = this;
143 :
144 0 : SET_CURR_SHELL( pDestShell );
145 :
146 : // List of insert positions for smart insert of block selections
147 0 : std::list< boost::shared_ptr<SwPosition> > aInsertList;
148 :
149 : // Fill list of insert positions
150 : {
151 0 : SwPosition * pPos = 0;
152 0 : boost::shared_ptr<SwPosition> pInsertPos;
153 0 : sal_uInt16 nMove = 0;
154 0 : FOREACHPAM_START(GetCrsr())
155 :
156 0 : if( !pPos )
157 : {
158 0 : if( pDestShell == this )
159 : {
160 : // First cursor represents the target position!!
161 0 : PCURCRSR->DeleteMark();
162 0 : pPos = (SwPosition*)PCURCRSR->GetPoint();
163 0 : continue;
164 : }
165 : else
166 0 : pPos = pDestShell->GetCrsr()->GetPoint();
167 : }
168 0 : if( IsBlockMode() )
169 : { // In block mode different insert positions will be calculated
170 : // by simulated cursor movements from the given first insert position
171 0 : if( nMove )
172 : {
173 0 : SwCursor aCrsr( *pPos, 0, false);
174 0 : if( aCrsr.UpDown( false, nMove, 0, 0 ) )
175 : {
176 0 : pInsertPos.reset( new SwPosition( *aCrsr.GetPoint() ) );
177 0 : aInsertList.push_back( pInsertPos );
178 0 : }
179 : }
180 : else
181 0 : pInsertPos.reset( new SwPosition( *pPos ) );
182 0 : ++nMove;
183 : }
184 0 : SwPosition *pTmp = IsBlockMode() ? pInsertPos.get() : pPos;
185 : // Check if a selection would be copied into itself
186 0 : if( pDestShell->GetDoc() == GetDoc() &&
187 0 : *PCURCRSR->Start() <= *pTmp && *pTmp < *PCURCRSR->End() )
188 0 : return sal_False;
189 0 : FOREACHPAM_END()
190 : }
191 :
192 0 : pDestShell->StartAllAction();
193 0 : SwPosition *pPos = 0;
194 0 : bool bRet = false;
195 0 : bool bFirstMove = true;
196 0 : SwNodeIndex aSttNdIdx( pDestShell->GetDoc()->GetNodes() );
197 0 : sal_Int32 nSttCntIdx = 0;
198 : // For block selection this list is filled with the insert positions
199 0 : std::list< boost::shared_ptr<SwPosition> >::iterator pNextInsert = aInsertList.begin();
200 :
201 0 : pDestShell->GetDoc()->GetIDocumentUndoRedo().StartUndo( UNDO_START, NULL );
202 0 : FOREACHPAM_START(GetCrsr())
203 :
204 0 : if( !pPos )
205 : {
206 0 : if( pDestShell == this )
207 : {
208 : // First cursor represents the target position!!
209 0 : PCURCRSR->DeleteMark();
210 0 : pPos = (SwPosition*)PCURCRSR->GetPoint();
211 0 : continue;
212 : }
213 : else
214 0 : pPos = pDestShell->GetCrsr()->GetPoint();
215 : }
216 0 : if( !bFirstMove )
217 : {
218 0 : if( pNextInsert != aInsertList.end() )
219 : {
220 0 : pPos = pNextInsert->get();
221 0 : ++pNextInsert;
222 : }
223 0 : else if( IsBlockMode() )
224 0 : GetDoc()->getIDocumentContentOperations().SplitNode( *pPos, false );
225 : }
226 :
227 : // Only for a selection (non-text nodes have selection but Point/GetMark are equal)
228 0 : if( !PCURCRSR->HasMark() || *PCURCRSR->GetPoint() == *PCURCRSR->GetMark() )
229 0 : continue;
230 :
231 0 : if( bFirstMove )
232 : {
233 : // Store start position of the new area
234 0 : aSttNdIdx = pPos->nNode.GetIndex()-1;
235 0 : nSttCntIdx = pPos->nContent.GetIndex();
236 0 : bFirstMove = false;
237 : }
238 :
239 0 : const bool bSuccess( GetDoc()->getIDocumentContentOperations().CopyRange( *PCURCRSR, *pPos, false ) );
240 0 : if (!bSuccess)
241 0 : continue;
242 :
243 0 : SwPaM aInsertPaM(*pPos, SwPosition(aSttNdIdx));
244 0 : pDestShell->GetDoc()->MakeUniqueNumRules(aInsertPaM);
245 :
246 0 : bRet = true;
247 0 : FOREACHPAM_END()
248 :
249 : // Maybe nothing has been moved?
250 0 : if( !bFirstMove )
251 : {
252 0 : SwPaM* pCrsr = pDestShell->GetCrsr();
253 0 : pCrsr->SetMark();
254 0 : pCrsr->GetPoint()->nNode = aSttNdIdx.GetIndex()+1;
255 0 : pCrsr->GetPoint()->nContent.Assign( pCrsr->GetCntntNode(),nSttCntIdx);
256 0 : pCrsr->Exchange();
257 : }
258 : else
259 : {
260 : // If the cursor moved during move process, move also its GetMark
261 0 : pDestShell->GetCrsr()->SetMark();
262 0 : pDestShell->GetCrsr()->DeleteMark();
263 : }
264 : #if OSL_DEBUG_LEVEL > 0
265 : // check if the indices are registered in the correct nodes
266 : {
267 : SwPaM* pCmp = (SwPaM*)pDestShell->GetCrsr(); // store pointer to cursor
268 : do {
269 : OSL_ENSURE( pCmp->GetPoint()->nContent.GetIdxReg()
270 : == pCmp->GetCntntNode(), "Point in wrong Node" );
271 : OSL_ENSURE( pCmp->GetMark()->nContent.GetIdxReg()
272 : == pCmp->GetCntntNode(false), "Mark in wrong Node" );
273 : bool bTst = *pCmp->GetPoint() == *pCmp->GetMark();
274 : (void) bTst;
275 : } while( pDestShell->GetCrsr() != ( pCmp = (SwPaM*)pCmp->GetNext() ) );
276 : }
277 : #endif
278 :
279 : // close Undo container here
280 0 : pDestShell->GetDoc()->GetIDocumentUndoRedo().EndUndo( UNDO_END, NULL );
281 0 : pDestShell->EndAllAction();
282 :
283 0 : pDestShell->SaveTblBoxCntnt( pDestShell->GetCrsr()->GetPoint() );
284 :
285 0 : return (long)bRet;
286 : }
287 :
288 : /** Replace a selected area in a text node with a given string.
289 : *
290 : * Intended for "search & replace".
291 : *
292 : * @param bRegExpRplc if <true> replace tabs (\\t) and replace with found string (not \&).
293 : * E.g. [Fnd: "zzz", Repl: "xx\t\\t..&..\&"] --> "xx\t<Tab>..zzz..&"
294 : */
295 0 : bool SwEditShell::Replace( const OUString& rNewStr, bool bRegExpRplc )
296 : {
297 0 : SET_CURR_SHELL( this );
298 :
299 0 : bool bRet = false;
300 0 : if( !HasReadonlySel() )
301 : {
302 0 : StartAllAction();
303 0 : GetDoc()->GetIDocumentUndoRedo().StartUndo(UNDO_EMPTY, NULL);
304 :
305 0 : FOREACHPAM_START(GetCrsr())
306 0 : if( PCURCRSR->HasMark() && *PCURCRSR->GetMark() != *PCURCRSR->GetPoint() )
307 : {
308 0 : bRet = GetDoc()->getIDocumentContentOperations().ReplaceRange( *PCURCRSR, rNewStr, bRegExpRplc )
309 0 : || bRet;
310 0 : SaveTblBoxCntnt( PCURCRSR->GetPoint() );
311 : }
312 0 : FOREACHPAM_END()
313 :
314 : // close Undo container here
315 0 : GetDoc()->GetIDocumentUndoRedo().EndUndo(UNDO_EMPTY, NULL);
316 0 : EndAllAction();
317 : }
318 0 : return bRet;
319 : }
320 :
321 : /// special method for JOE's wizards
322 0 : bool SwEditShell::DelFullPara()
323 : {
324 0 : bool bRet = false;
325 0 : if( !IsTableMode() )
326 : {
327 0 : SwPaM* pCrsr = GetCrsr();
328 : // no multi selection
329 0 : if( pCrsr->GetNext() == pCrsr && !HasReadonlySel() )
330 : {
331 0 : SET_CURR_SHELL( this );
332 0 : StartAllAction();
333 0 : bRet = GetDoc()->getIDocumentContentOperations().DelFullPara( *pCrsr );
334 0 : EndAllAction();
335 : }
336 : }
337 0 : return bRet;
338 270 : }
339 :
340 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|