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 <doc.hxx>
21 : #include <node.hxx>
22 : #include <rootfrm.hxx>
23 : #include <editsh.hxx>
24 : #include <viscrs.hxx>
25 : #include <IMark.hxx>
26 : #include <bookmrk.hxx>
27 : #include <redline.hxx>
28 : #include <mvsave.hxx>
29 : #include <docary.hxx>
30 : #include <unocrsr.hxx>
31 : #include <swundo.hxx>
32 : #include <hints.hxx>
33 :
34 : /*
35 : * Macros to iterate over all CrsrShells
36 : */
37 : #define PCURSH ((SwCrsrShell*)_pStartShell)
38 : #define FOREACHSHELL_START( pEShell ) \
39 : {\
40 : ViewShell *_pStartShell = pEShell; \
41 : do { \
42 : if( _pStartShell->IsA( TYPE( SwCrsrShell )) ) \
43 : {
44 :
45 : #define FOREACHSHELL_END( pEShell ) \
46 : } \
47 : } while((_pStartShell=(ViewShell*)_pStartShell->GetNext())!= pEShell ); \
48 : }
49 :
50 : #define PCURCRSR (_pCurrCrsr)
51 : #define FOREACHPAM_START(pSttCrsr) \
52 : {\
53 : SwPaM *_pStartCrsr = pSttCrsr, *_pCurrCrsr = pSttCrsr; \
54 : do {
55 :
56 : #define FOREACHPAM_END() \
57 : } while( (_pCurrCrsr=(SwPaM *)_pCurrCrsr->GetNext()) != _pStartCrsr ); \
58 : }
59 :
60 : namespace
61 : {
62 : // find the relevant section in which the SwUnoCrsr may wander.
63 : // returns NULL if no restrictions apply
64 2434 : static const SwStartNode* lcl_FindUnoCrsrSection( const SwNode& rNode )
65 : {
66 2434 : const SwStartNode* pStartNode = rNode.StartOfSectionNode();
67 13696 : while( ( pStartNode != NULL ) &&
68 4407 : ( pStartNode->StartOfSectionNode() != pStartNode ) &&
69 2448 : ( pStartNode->GetStartNodeType() == SwNormalStartNode ) )
70 1973 : pStartNode = pStartNode->StartOfSectionNode();
71 :
72 2434 : return pStartNode;
73 : }
74 :
75 2734 : static inline bool lcl_PosCorrAbs(SwPosition & rPos,
76 : const SwPosition& rStart,
77 : const SwPosition& rEnd,
78 : const SwPosition& rNewPos)
79 : {
80 2734 : if ((rStart <= rPos) && (rPos <= rEnd))
81 : {
82 972 : rPos = rNewPos;
83 972 : return true;
84 : }
85 1762 : return false;
86 : };
87 :
88 1367 : static inline bool lcl_PaMCorrAbs(SwPaM & rPam,
89 : const SwPosition& rStart,
90 : const SwPosition& rEnd,
91 : const SwPosition& rNewPos)
92 : {
93 1367 : bool bRet = false;
94 1367 : bRet |= lcl_PosCorrAbs(rPam.GetBound(true ), rStart, rEnd, rNewPos);
95 1367 : bRet |= lcl_PosCorrAbs(rPam.GetBound(false), rStart, rEnd, rNewPos);
96 1367 : return bRet;
97 : };
98 :
99 535 : static inline void lcl_PaMCorrRel1(SwPaM * pPam,
100 : SwNode const * const pOldNode,
101 : const SwPosition& rNewPos,
102 : const xub_StrLen nCntIdx)
103 : {
104 1605 : for(int nb = 0; nb < 2; ++nb)
105 1070 : if(&((pPam)->GetBound(sal_Bool(nb)).nNode.GetNode()) == pOldNode)
106 : {
107 405 : (pPam)->GetBound(sal_Bool(nb)).nNode = rNewPos.nNode;
108 405 : (pPam)->GetBound(sal_Bool(nb)).nContent.Assign(
109 405 : const_cast<SwIndexReg*>(rNewPos.nContent.GetIdxReg()),
110 810 : nCntIdx + (pPam)->GetBound(sal_Bool(nb)).nContent.GetIndex());
111 : }
112 535 : }
113 : }
114 :
115 :
116 1346 : void PaMCorrAbs( const SwPaM& rRange,
117 : const SwPosition& rNewPos )
118 : {
119 1346 : SwPosition const aStart( *rRange.Start() );
120 1346 : SwPosition const aEnd( *rRange.End() );
121 1346 : SwPosition const aNewPos( rNewPos );
122 1346 : SwDoc *const pDoc = aStart.nNode.GetNode().GetDoc();
123 1346 : SwCrsrShell *const pShell = pDoc->GetEditShell();
124 :
125 1346 : if( pShell )
126 : {
127 0 : FOREACHSHELL_START( pShell )
128 0 : SwPaM *_pStkCrsr = PCURSH->GetStkCrsr();
129 0 : if( _pStkCrsr )
130 0 : do {
131 0 : lcl_PaMCorrAbs( *_pStkCrsr, aStart, aEnd, aNewPos );
132 : } while ( (_pStkCrsr != 0 ) &&
133 0 : ((_pStkCrsr=(SwPaM *)_pStkCrsr->GetNext()) != PCURSH->GetStkCrsr()) );
134 :
135 0 : FOREACHPAM_START( PCURSH->_GetCrsr() )
136 0 : lcl_PaMCorrAbs( *PCURCRSR, aStart, aEnd, aNewPos );
137 0 : FOREACHPAM_END()
138 :
139 0 : if( PCURSH->IsTableMode() )
140 0 : lcl_PaMCorrAbs( *PCURSH->GetTblCrs(), aStart, aEnd, aNewPos );
141 :
142 0 : FOREACHSHELL_END( pShell )
143 : }
144 : {
145 1346 : SwUnoCrsrTbl& rTbl = const_cast<SwUnoCrsrTbl&>(pDoc->GetUnoCrsrTbl());
146 :
147 2713 : for( SwUnoCrsrTbl::iterator it = rTbl.begin(); it != rTbl.end(); ++it )
148 : {
149 1367 : SwUnoCrsr *const pUnoCursor = *it;
150 :
151 1367 : bool bChange = false; // has the UNO cursor been corrected?
152 :
153 : // determine whether the UNO cursor will leave it's designated
154 : // section
155 : bool const bLeaveSection =
156 1367 : pUnoCursor->IsRemainInSection() &&
157 1217 : ( lcl_FindUnoCrsrSection( aNewPos.nNode.GetNode() ) !=
158 : lcl_FindUnoCrsrSection(
159 2584 : pUnoCursor->GetPoint()->nNode.GetNode() ) );
160 :
161 2734 : FOREACHPAM_START( pUnoCursor )
162 1367 : bChange |= lcl_PaMCorrAbs( *PCURCRSR, aStart, aEnd, aNewPos );
163 1367 : FOREACHPAM_END()
164 :
165 : SwUnoTableCrsr *const pUnoTblCrsr =
166 1367 : dynamic_cast<SwUnoTableCrsr *>(*it);
167 1367 : if( pUnoTblCrsr )
168 : {
169 0 : FOREACHPAM_START( &pUnoTblCrsr->GetSelRing() )
170 : bChange |=
171 0 : lcl_PaMCorrAbs( *PCURCRSR, aStart, aEnd, aNewPos );
172 0 : FOREACHPAM_END()
173 : }
174 :
175 : // if a UNO cursor leaves its designated section, we must inform
176 : // (and invalidate) said cursor
177 1367 : if (bChange && bLeaveSection)
178 : {
179 : // the UNO cursor has left its section. We need to notify it!
180 117 : SwMsgPoolItem aHint( RES_UNOCURSOR_LEAVES_SECTION );
181 117 : pUnoCursor->ModifyNotification( &aHint, NULL );
182 : }
183 : }
184 1346 : }
185 1346 : }
186 :
187 178 : void SwDoc::CorrAbs(const SwNodeIndex& rOldNode,
188 : const SwPosition& rNewPos,
189 : const xub_StrLen nOffset,
190 : sal_Bool bMoveCrsr)
191 : {
192 178 : SwCntntNode *const pCntntNode( rOldNode.GetNode().GetCntntNode() );
193 : SwPaM const aPam(rOldNode, 0,
194 178 : rOldNode, (pCntntNode) ? pCntntNode->Len() : 0);
195 178 : SwPosition aNewPos(rNewPos);
196 178 : aNewPos.nContent += nOffset;
197 :
198 178 : getIDocumentMarkAccess()->correctMarksAbsolute(rOldNode, rNewPos, nOffset);
199 : { // fix redlines
200 178 : SwRedlineTbl& rTbl = *pRedlineTbl;
201 356 : for (sal_uInt16 n = 0; n < rTbl.size(); )
202 : {
203 : // is on position ??
204 0 : SwRedline *const pRedline( rTbl[ n ] );
205 : bool const bChanged =
206 0 : lcl_PaMCorrAbs(*pRedline, *aPam.Start(), *aPam.End(), aNewPos);
207 : // clean up empty redlines: docredln.cxx asserts these as invalid
208 0 : if (bChanged && (*pRedline->GetPoint() == *pRedline->GetMark())
209 0 : && (pRedline->GetContentIdx() == NULL))
210 : {
211 0 : rTbl.DeleteAndDestroy(n);
212 : }
213 : else
214 : {
215 0 : ++n;
216 : }
217 : }
218 : }
219 :
220 178 : if(bMoveCrsr)
221 : {
222 178 : ::PaMCorrAbs(aPam, aNewPos);
223 178 : }
224 178 : }
225 :
226 0 : void SwDoc::CorrAbs(const SwPaM& rRange,
227 : const SwPosition& rNewPos,
228 : sal_Bool bMoveCrsr)
229 : {
230 0 : SwPosition aStart(*rRange.Start());
231 0 : SwPosition aEnd(*rRange.End());
232 0 : SwPosition aNewPos(rNewPos);
233 :
234 : _DelBookmarks(aStart.nNode, aEnd.nNode, NULL,
235 0 : &aStart.nContent, &aEnd.nContent);
236 0 : if(bMoveCrsr)
237 0 : ::PaMCorrAbs(rRange, rNewPos);
238 0 : }
239 :
240 181 : void SwDoc::CorrAbs(const SwNodeIndex& rStartNode,
241 : const SwNodeIndex& rEndNode,
242 : const SwPosition& rNewPos,
243 : sal_Bool bMoveCrsr)
244 : {
245 181 : _DelBookmarks(rStartNode, rEndNode);
246 :
247 181 : if(bMoveCrsr)
248 : {
249 181 : SwCntntNode *const pCntntNode( rEndNode.GetNode().GetCntntNode() );
250 : SwPaM const aPam(rStartNode, 0,
251 181 : rEndNode, (pCntntNode) ? pCntntNode->Len() : 0);
252 181 : ::PaMCorrAbs(aPam, rNewPos);
253 : }
254 181 : }
255 :
256 :
257 :
258 :
259 :
260 268 : void PaMCorrRel( const SwNodeIndex &rOldNode,
261 : const SwPosition &rNewPos,
262 : const xub_StrLen nOffset )
263 : {
264 268 : const SwNode* pOldNode = &rOldNode.GetNode();
265 268 : SwPosition aNewPos( rNewPos );
266 268 : const SwDoc* pDoc = pOldNode->GetDoc();
267 :
268 268 : xub_StrLen nCntIdx = rNewPos.nContent.GetIndex() + nOffset;
269 :
270 268 : SwCrsrShell* pShell = pDoc->GetEditShell();
271 268 : if( pShell )
272 : {
273 0 : FOREACHSHELL_START( pShell )
274 0 : SwPaM *_pStkCrsr = PCURSH->GetStkCrsr();
275 0 : if( _pStkCrsr )
276 0 : do {
277 0 : lcl_PaMCorrRel1( _pStkCrsr, pOldNode, aNewPos, nCntIdx );
278 : } while ( (_pStkCrsr != 0 ) &&
279 0 : ((_pStkCrsr=(SwPaM *)_pStkCrsr->GetNext()) != PCURSH->GetStkCrsr()) );
280 :
281 0 : FOREACHPAM_START( PCURSH->_GetCrsr() )
282 0 : lcl_PaMCorrRel1( PCURCRSR, pOldNode, aNewPos, nCntIdx);
283 0 : FOREACHPAM_END()
284 :
285 0 : if( PCURSH->IsTableMode() )
286 0 : lcl_PaMCorrRel1( PCURSH->GetTblCrs(), pOldNode, aNewPos, nCntIdx );
287 :
288 0 : FOREACHSHELL_END( pShell )
289 : }
290 : {
291 268 : SwUnoCrsrTbl& rTbl = (SwUnoCrsrTbl&)pDoc->GetUnoCrsrTbl();
292 801 : for( SwUnoCrsrTbl::iterator it = rTbl.begin(); it != rTbl.end(); ++it )
293 : {
294 1066 : FOREACHPAM_START( *it )
295 533 : lcl_PaMCorrRel1( PCURCRSR, pOldNode, aNewPos, nCntIdx );
296 533 : FOREACHPAM_END()
297 :
298 : SwUnoTableCrsr* pUnoTblCrsr =
299 533 : dynamic_cast<SwUnoTableCrsr*>(*it);
300 533 : if( pUnoTblCrsr )
301 : {
302 0 : FOREACHPAM_START( &pUnoTblCrsr->GetSelRing() )
303 0 : lcl_PaMCorrRel1( PCURCRSR, pOldNode, aNewPos, nCntIdx );
304 0 : FOREACHPAM_END()
305 : }
306 : }
307 268 : }
308 268 : }
309 :
310 268 : void SwDoc::CorrRel(const SwNodeIndex& rOldNode,
311 : const SwPosition& rNewPos,
312 : const xub_StrLen nOffset,
313 : sal_Bool bMoveCrsr)
314 : {
315 268 : getIDocumentMarkAccess()->correctMarksRelative(rOldNode, rNewPos, nOffset);
316 :
317 : { // fix the Redlines
318 268 : SwRedlineTbl& rTbl = *pRedlineTbl;
319 268 : SwPosition aNewPos(rNewPos);
320 270 : for( sal_uInt16 n = 0; n < rTbl.size(); ++n )
321 : {
322 : // liegt auf der Position ??
323 2 : lcl_PaMCorrRel1( rTbl[ n ], &rOldNode.GetNode(), aNewPos, aNewPos.nContent.GetIndex() + nOffset );
324 268 : }
325 : }
326 :
327 268 : if(bMoveCrsr)
328 268 : ::PaMCorrRel(rOldNode, rNewPos, nOffset);
329 268 : }
330 :
331 :
332 2912 : SwEditShell* SwDoc::GetEditShell( ViewShell** ppSh ) const
333 : {
334 : // Layout and OLE shells should be available
335 2912 : if( pCurrentView )
336 : {
337 255 : ViewShell *pSh = pCurrentView, *pVSh = pSh;
338 255 : if( ppSh )
339 179 : *ppSh = pSh;
340 :
341 : // look for an EditShell (if it exists)
342 0 : do {
343 255 : if( pSh->IsA( TYPE( SwEditShell ) ) )
344 255 : return (SwEditShell*)pSh;
345 :
346 0 : } while( pVSh != ( pSh = (ViewShell*)pSh->GetNext() ));
347 : }
348 2657 : else if( ppSh )
349 7 : *ppSh = 0; //swmod 071029//swmod 071225
350 :
351 2657 : return 0;
352 : }
353 :
354 0 : ::sw::IShellCursorSupplier * SwDoc::GetIShellCursorSupplier()
355 : {
356 0 : return GetEditShell(0);
357 : }
358 :
359 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|