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 <rtl/random.h>
22 : #include <tools/resid.hxx>
23 : #include <editeng/lrspitem.hxx>
24 : #include <ftninfo.hxx>
25 : #include <ftnidx.hxx>
26 : #include <doc.hxx>
27 : #include <IDocumentUndoRedo.hxx>
28 : #include <pam.hxx>
29 : #include <ndtxt.hxx>
30 : #include <doctxm.hxx>
31 : #include <poolfmt.hxx>
32 : #include <UndoCore.hxx>
33 : #include <UndoRedline.hxx>
34 : #include <UndoNumbering.hxx>
35 : #include <swundo.hxx>
36 : #include <SwUndoFmt.hxx>
37 : #include <rolbck.hxx>
38 : #include <paratr.hxx>
39 : #include <docary.hxx>
40 : #include <mvsave.hxx>
41 : #include <txtfrm.hxx>
42 : #include <pamtyp.hxx>
43 : #include <redline.hxx>
44 : #include <comcore.hrc>
45 : #include <editeng/adjustitem.hxx>
46 : #include <editeng/frmdiritem.hxx>
47 : #include <frmatr.hxx>
48 : #include <SwStyleNameMapper.hxx>
49 : #include <SwNodeNum.hxx>
50 : #include <list.hxx>
51 : #include <listfunc.hxx>
52 : #include <switerator.hxx>
53 : #include <comphelper/string.hxx>
54 :
55 : #include <cstdlib>
56 : #include <map>
57 :
58 : namespace {
59 0 : static void lcl_ResetIndentAttrs(SwDoc *pDoc, const SwPaM &rPam, sal_uInt16 marker )
60 : {
61 0 : std::set<sal_uInt16> aResetAttrsArray;
62 0 : aResetAttrsArray.insert( marker );
63 : // #i114929#
64 : // On a selection setup a corresponding Point-and-Mark in order to get
65 : // the indentation attribute reset on all paragraphs touched by the selection
66 0 : if ( rPam.HasMark() &&
67 0 : rPam.End()->nNode.GetNode().GetTxtNode() )
68 : {
69 0 : SwPaM aPam( rPam.Start()->nNode,
70 0 : rPam.End()->nNode );
71 0 : aPam.Start()->nContent = 0;
72 0 : aPam.End()->nContent = rPam.End()->nNode.GetNode().GetTxtNode()->Len();
73 0 : pDoc->ResetAttrs( aPam, false, aResetAttrsArray );
74 : }
75 : else
76 : {
77 0 : pDoc->ResetAttrs( rPam, false, aResetAttrsArray );
78 0 : }
79 0 : }
80 : }
81 :
82 : #include <stdlib.h>
83 :
84 0 : inline sal_uInt8 GetUpperLvlChg( sal_uInt8 nCurLvl, sal_uInt8 nLevel, sal_uInt16 nMask )
85 : {
86 0 : if( 1 < nLevel )
87 : {
88 0 : if( nCurLvl + 1 >= nLevel )
89 0 : nCurLvl -= nLevel - 1;
90 : else
91 0 : nCurLvl = 0;
92 : }
93 0 : return static_cast<sal_uInt8>((nMask - 1) & ~(( 1 << nCurLvl ) - 1));
94 : }
95 :
96 0 : void SwDoc::SetOutlineNumRule( const SwNumRule& rRule )
97 : {
98 0 : if( mpOutlineRule )
99 0 : (*mpOutlineRule) = rRule;
100 : else
101 : {
102 0 : mpOutlineRule = new SwNumRule( rRule );
103 :
104 0 : AddNumRule(mpOutlineRule); // #i36749#
105 : }
106 :
107 0 : mpOutlineRule->SetRuleType( OUTLINE_RULE );
108 0 : mpOutlineRule->SetName(SwNumRule::GetOutlineRuleName(), *this);
109 :
110 : // assure that the outline numbering rule is an automatic rule
111 0 : mpOutlineRule->SetAutoRule( sal_True );
112 :
113 : // test whether the optional CharFormats are defined in this Document
114 0 : mpOutlineRule->CheckCharFmts( this );
115 :
116 : // notify text nodes, which are registered at the outline style, about the
117 : // changed outline style
118 0 : SwNumRule::tTxtNodeList aTxtNodeList;
119 0 : mpOutlineRule->GetTxtNodeList( aTxtNodeList );
120 0 : for ( SwNumRule::tTxtNodeList::iterator aIter = aTxtNodeList.begin();
121 0 : aIter != aTxtNodeList.end(); ++aIter )
122 : {
123 0 : SwTxtNode* pTxtNd = *aIter;
124 0 : pTxtNd->NumRuleChgd();
125 :
126 : // assure that list level corresponds to outline level
127 0 : if ( pTxtNd->GetTxtColl()->IsAssignedToListLevelOfOutlineStyle() &&
128 0 : pTxtNd->GetAttrListLevel() != pTxtNd->GetTxtColl()->GetAssignedOutlineStyleLevel() )
129 : {
130 0 : pTxtNd->SetAttrListLevel( pTxtNd->GetTxtColl()->GetAssignedOutlineStyleLevel() );
131 : }
132 : }
133 :
134 0 : PropagateOutlineRule();
135 0 : mpOutlineRule->SetInvalidRule(sal_True);
136 0 : UpdateNumRule();
137 :
138 : // update if we have foot notes && numbering by chapter
139 0 : if( !GetFtnIdxs().empty() && FTNNUM_CHAPTER == GetFtnInfo().eNum )
140 0 : GetFtnIdxs().UpdateAllFtn();
141 :
142 0 : UpdateExpFlds(NULL, true);
143 :
144 0 : SetModified();
145 0 : }
146 :
147 0 : void SwDoc::PropagateOutlineRule()
148 : {
149 0 : for (sal_uInt16 n = 0; n < mpTxtFmtCollTbl->size(); n++)
150 : {
151 0 : SwTxtFmtColl *pColl = (*mpTxtFmtCollTbl)[n];
152 :
153 0 : if(pColl->IsAssignedToListLevelOfOutlineStyle())
154 : {
155 : // Check only the list style, which is set at the paragraph style
156 0 : const SwNumRuleItem & rCollRuleItem = pColl->GetNumRule( sal_False );
157 :
158 : // Check on document setting OUTLINE_LEVEL_YIELDS_OUTLINE_RULE no longer needed.
159 0 : if ( rCollRuleItem.GetValue().isEmpty() )
160 : {
161 0 : SwNumRule * pMyOutlineRule = GetOutlineNumRule();
162 :
163 0 : if (pMyOutlineRule)
164 : {
165 0 : SwNumRuleItem aNumItem( pMyOutlineRule->GetName() );
166 :
167 0 : pColl->SetFmtAttr(aNumItem);
168 : }
169 : }
170 : }
171 : }
172 0 : }
173 :
174 : // Increase/Decrease
175 0 : bool SwDoc::OutlineUpDown( const SwPaM& rPam, short nOffset )
176 : {
177 0 : if( GetNodes().GetOutLineNds().empty() || !nOffset )
178 0 : return false;
179 :
180 : // calculate the range
181 0 : const SwOutlineNodes& rOutlNds = GetNodes().GetOutLineNds();
182 0 : const SwNodePtr pSttNd = (SwNodePtr)&rPam.Start()->nNode.GetNode();
183 0 : const SwNodePtr pEndNd = (SwNodePtr)&rPam.End()->nNode.GetNode();
184 : sal_uInt16 nSttPos, nEndPos;
185 :
186 0 : if( !rOutlNds.Seek_Entry( pSttNd, &nSttPos ) &&
187 0 : !nSttPos-- )
188 : // we're not in an "Outline section"
189 0 : return false;
190 :
191 0 : if( rOutlNds.Seek_Entry( pEndNd, &nEndPos ) )
192 0 : ++nEndPos;
193 :
194 : // We now have the wanted range in the OutlineNodes array,
195 : // so check now if we're not invalidating sublevels
196 : // (stepping over the limits)
197 : sal_uInt16 n;
198 :
199 : // Here we go:
200 : // 1. Create the style array:
201 : SwTxtFmtColl* aCollArr[ MAXLEVEL ];
202 0 : memset( aCollArr, 0, sizeof( SwTxtFmtColl* ) * MAXLEVEL );
203 :
204 0 : for( n = 0; n < mpTxtFmtCollTbl->size(); ++n )
205 : {
206 0 : if((*mpTxtFmtCollTbl)[ n ]->IsAssignedToListLevelOfOutlineStyle())
207 : {
208 0 : const int nLevel = (*mpTxtFmtCollTbl)[ n ]->GetAssignedOutlineStyleLevel();
209 0 : aCollArr[ nLevel ] = (*mpTxtFmtCollTbl)[ n ];
210 : }
211 : }
212 :
213 : /* Find the last occupied level (backward). */
214 0 : for (n = MAXLEVEL - 1; n > 0; n--)
215 : {
216 0 : if (aCollArr[n] != 0)
217 0 : break;
218 : }
219 :
220 : /* If an occupied level is found, choose next level (which IS
221 : unoccupied) until a valid level is found. If no occupied level
222 : was found n is 0 and aCollArr[0] is 0. In this case no demoting
223 : is possible. */
224 0 : if (aCollArr[n] != 0)
225 : {
226 0 : while (n < MAXLEVEL - 1)
227 : {
228 0 : n++;
229 :
230 : SwTxtFmtColl *aTmpColl =
231 0 : GetTxtCollFromPool(static_cast<sal_uInt16>(RES_POOLCOLL_HEADLINE1 + n));
232 :
233 0 : if( aTmpColl->IsAssignedToListLevelOfOutlineStyle() &&
234 0 : aTmpColl->GetAssignedOutlineStyleLevel() == n )
235 : {
236 0 : aCollArr[n] = aTmpColl;
237 0 : break;
238 : }
239 : }
240 : }
241 :
242 : /* Find the first occupied level (forward). */
243 0 : for (n = 0; n < MAXLEVEL - 1; n++)
244 : {
245 0 : if (aCollArr[n] != 0)
246 0 : break;
247 : }
248 :
249 : /* If an occupied level is found, choose previous level (which IS
250 : unoccupied) until a valid level is found. If no occupied level
251 : was found n is MAXLEVEL - 1 and aCollArr[MAXLEVEL - 1] is 0. In
252 : this case no demoting is possible. */
253 0 : if (aCollArr[n] != 0)
254 : {
255 0 : while (n > 0)
256 : {
257 0 : n--;
258 :
259 : SwTxtFmtColl *aTmpColl =
260 0 : GetTxtCollFromPool(static_cast<sal_uInt16>(RES_POOLCOLL_HEADLINE1 + n));
261 :
262 0 : if( aTmpColl->IsAssignedToListLevelOfOutlineStyle() &&
263 0 : aTmpColl->GetAssignedOutlineStyleLevel() == n )
264 : {
265 0 : aCollArr[n] = aTmpColl;
266 0 : break;
267 : }
268 : }
269 : }
270 :
271 : /* --> #i13747#
272 :
273 : Build a move table that states from which level to which other level
274 : an outline will be moved.
275 :
276 : the move table:
277 : aMoveArr[n] = m: replace aCollArr[n] with aCollArr[m]
278 : */
279 : int aMoveArr[MAXLEVEL];
280 : int nStep; // step size for searching in aCollArr: -1 or 1
281 : int nNum; // amount of steps for stepping in aCollArr
282 :
283 0 : if (nOffset < 0)
284 : {
285 0 : nStep = -1;
286 0 : nNum = -nOffset;
287 : }
288 : else
289 : {
290 0 : nStep = 1;
291 0 : nNum = nOffset;
292 : }
293 :
294 : /* traverse aCollArr */
295 0 : for (n = 0; n < MAXLEVEL; n++)
296 : {
297 : /* If outline level n has an assigned paragraph style step
298 : nNum steps forwards (nStep == 1) or backwards (nStep ==
299 : -1). One step is to go to the next non-null entry in
300 : aCollArr in the selected direction. If nNum steps were
301 : possible write the index of the entry found to aCollArr[n],
302 : i.e. outline level n will be replaced by outline level
303 : aCollArr[n].
304 :
305 : If outline level n has no assigned paragraph style
306 : aMoveArr[n] is set to -1.
307 : */
308 0 : if (aCollArr[n] != NULL)
309 : {
310 0 : sal_uInt16 m = n;
311 0 : int nCount = nNum;
312 :
313 0 : while (nCount > 0 && m + nStep >= 0 && m + nStep < MAXLEVEL)
314 : {
315 0 : m = static_cast<sal_uInt16>(m + nStep);
316 :
317 0 : if (aCollArr[m] != NULL)
318 0 : nCount--;
319 : }
320 :
321 0 : if (nCount == 0)
322 0 : aMoveArr[n] = m;
323 : else
324 0 : aMoveArr[n] = -1;
325 : }
326 : else
327 0 : aMoveArr[n] = -1;
328 : }
329 :
330 : /* If moving of the outline levels is applicable, i.e. for all
331 : outline levels occuring in the document there has to be a valid
332 : target outline level implied by aMoveArr. */
333 0 : bool bMoveApplicable = true;
334 0 : for (n = nSttPos; n < nEndPos; n++)
335 : {
336 0 : SwTxtNode* pTxtNd = rOutlNds[ n ]->GetTxtNode();
337 0 : SwTxtFmtColl* pColl = pTxtNd->GetTxtColl();
338 :
339 0 : if( pColl->IsAssignedToListLevelOfOutlineStyle() )
340 : {
341 0 : const int nLevel = pColl->GetAssignedOutlineStyleLevel();
342 0 : if (aMoveArr[nLevel] == -1)
343 0 : bMoveApplicable = false;
344 : }
345 :
346 : // Check on outline level attribute of text node, if text node is
347 : // not an outline via a to outline style assigned paragraph style.
348 : else
349 : {
350 0 : const int nNewOutlineLevel = pTxtNd->GetAttrOutlineLevel() + nOffset;
351 0 : if ( nNewOutlineLevel < 1 || nNewOutlineLevel > MAXLEVEL )
352 : {
353 0 : bMoveApplicable = false;
354 : }
355 : }
356 : }
357 :
358 0 : if (! bMoveApplicable )
359 0 : return false;
360 :
361 0 : if (GetIDocumentUndoRedo().DoesUndo())
362 : {
363 0 : GetIDocumentUndoRedo().StartUndo(UNDO_OUTLINE_LR, NULL);
364 0 : SwUndo *const pUndoOLR( new SwUndoOutlineLeftRight( rPam, nOffset ) );
365 0 : GetIDocumentUndoRedo().AppendUndo(pUndoOLR);
366 : }
367 :
368 : // 2. Apply the new style to all Nodes
369 :
370 0 : n = nSttPos;
371 0 : while( n < nEndPos)
372 : {
373 0 : SwTxtNode* pTxtNd = rOutlNds[ n ]->GetTxtNode();
374 0 : SwTxtFmtColl* pColl = pTxtNd->GetTxtColl();
375 :
376 0 : if( pColl->IsAssignedToListLevelOfOutlineStyle() )
377 : {
378 0 : const int nLevel = pColl->GetAssignedOutlineStyleLevel();
379 :
380 : OSL_ENSURE(aMoveArr[nLevel] >= 0,
381 : "move table: current TxtColl not found when building table!");
382 :
383 0 : if (nLevel < MAXLEVEL && aMoveArr[nLevel] >= 0)
384 : {
385 0 : pColl = aCollArr[ aMoveArr[nLevel] ];
386 :
387 0 : if (pColl != NULL)
388 0 : pTxtNd->ChgFmtColl( pColl );
389 : }
390 :
391 : }
392 0 : else if( pTxtNd->GetAttrOutlineLevel() > 0)
393 : {
394 0 : int nLevel = pTxtNd->GetAttrOutlineLevel() + nOffset;
395 0 : if( 0 <= nLevel && nLevel <= MAXLEVEL)
396 0 : pTxtNd->SetAttrOutlineLevel( nLevel );
397 :
398 : }
399 :
400 0 : n++;
401 : // Undo ???
402 : }
403 0 : if (GetIDocumentUndoRedo().DoesUndo())
404 : {
405 0 : GetIDocumentUndoRedo().EndUndo(UNDO_OUTLINE_LR, NULL);
406 : }
407 :
408 0 : ChkCondColls();
409 0 : SetModified();
410 :
411 0 : return true;
412 : }
413 :
414 : // Move up/down
415 0 : bool SwDoc::MoveOutlinePara( const SwPaM& rPam, short nOffset )
416 : {
417 : // Do not move to special sections in the nodes array
418 0 : const SwPosition& rStt = *rPam.Start(),
419 0 : & rEnd = &rStt == rPam.GetPoint() ? *rPam.GetMark()
420 0 : : *rPam.GetPoint();
421 0 : if( GetNodes().GetOutLineNds().empty() || !nOffset ||
422 0 : (rStt.nNode.GetIndex() < GetNodes().GetEndOfExtras().GetIndex()) ||
423 0 : (rEnd.nNode.GetIndex() < GetNodes().GetEndOfExtras().GetIndex()))
424 : {
425 0 : return false;
426 : }
427 :
428 0 : sal_uInt16 nAktPos = 0;
429 0 : SwNodeIndex aSttRg( rStt.nNode ), aEndRg( rEnd.nNode );
430 :
431 0 : int nOutLineLevel = MAXLEVEL;
432 0 : SwNode* pSrch = &aSttRg.GetNode();
433 :
434 0 : if( pSrch->IsTxtNode())
435 0 : nOutLineLevel = static_cast<sal_uInt8>(((SwTxtNode*)pSrch)->GetAttrOutlineLevel()-1);
436 0 : SwNode* pEndSrch = &aEndRg.GetNode();
437 0 : if( !GetNodes().GetOutLineNds().Seek_Entry( pSrch, &nAktPos ) )
438 : {
439 0 : if( !nAktPos )
440 0 : return false; // Promoting or demoting before the first outline => no.
441 0 : if( --nAktPos )
442 0 : aSttRg = *GetNodes().GetOutLineNds()[ nAktPos ];
443 0 : else if( 0 > nOffset )
444 0 : return false; // Promoting at the top of document?!
445 : else
446 0 : aSttRg = *GetNodes().GetEndOfContent().StartOfSectionNode();
447 : }
448 0 : sal_uInt16 nTmpPos = 0;
449 : // If the given range ends at an outlined text node we have to decide if it has to be a part of
450 : // the moving range or not. Normally it will be a sub outline of our chapter
451 : // and has to be moved, too. But if the chapter ends with a table(or a section end),
452 : // the next text node will be choosen and this could be the next outline of the same level.
453 : // The criteria has to be the outline level: sub level => incorporate, same/higher level => no.
454 0 : if( GetNodes().GetOutLineNds().Seek_Entry( pEndSrch, &nTmpPos ) )
455 : {
456 0 : if( !pEndSrch->IsTxtNode() || pEndSrch == pSrch ||
457 0 : nOutLineLevel < ((SwTxtNode*)pEndSrch)->GetAttrOutlineLevel()-1 )
458 0 : ++nTmpPos; // For sub outlines only!
459 : }
460 :
461 0 : aEndRg = nTmpPos < GetNodes().GetOutLineNds().size()
462 0 : ? *GetNodes().GetOutLineNds()[ nTmpPos ]
463 0 : : GetNodes().GetEndOfContent();
464 0 : if( nOffset >= 0 )
465 0 : nAktPos = nTmpPos;
466 0 : if( aEndRg == aSttRg )
467 : {
468 : OSL_FAIL( "Moving outlines: Surprising selection" );
469 0 : ++aEndRg;
470 : }
471 :
472 : const SwNode* pNd;
473 : // The following code corrects the range to handle sections (start/end nodes)
474 : // The range will be extended if the least node before the range is a start node
475 : // which ends inside the range => The complete section will be moved.
476 : // The range will be shrinked if the last position is a start node.
477 : // The range will be shrinked if the last node is an end node which starts before the range.
478 0 : aSttRg--;
479 0 : while( aSttRg.GetNode().IsStartNode() )
480 : {
481 0 : pNd = aSttRg.GetNode().EndOfSectionNode();
482 0 : if( pNd->GetIndex() >= aEndRg.GetIndex() )
483 0 : break;
484 0 : aSttRg--;
485 : }
486 0 : ++aSttRg;
487 :
488 0 : aEndRg--;
489 0 : while( aEndRg.GetNode().IsStartNode() )
490 0 : aEndRg--;
491 :
492 0 : while( aEndRg.GetNode().IsEndNode() )
493 : {
494 0 : pNd = aEndRg.GetNode().StartOfSectionNode();
495 0 : if( pNd->GetIndex() >= aSttRg.GetIndex() )
496 0 : break;
497 0 : aEndRg--;
498 : }
499 0 : ++aEndRg;
500 :
501 : // calculation of the new position
502 0 : if( nOffset < 0 && nAktPos < sal_uInt16(-nOffset) )
503 0 : pNd = GetNodes().GetEndOfContent().StartOfSectionNode();
504 0 : else if( nAktPos + nOffset >= (sal_uInt16)GetNodes().GetOutLineNds().size() )
505 0 : pNd = &GetNodes().GetEndOfContent();
506 : else
507 0 : pNd = GetNodes().GetOutLineNds()[ nAktPos + nOffset ];
508 :
509 0 : sal_uLong nNewPos = pNd->GetIndex();
510 :
511 : // And now a correction of the insert position if necessary...
512 0 : SwNodeIndex aInsertPos( *pNd, -1 );
513 0 : while( aInsertPos.GetNode().IsStartNode() )
514 : {
515 : // Just before the insert position starts a section:
516 : // when I'm moving forward I do not want to enter the section,
517 : // when I'm moving backward I want to stay in the section if I'm already a part of,
518 : // I want to stay outside if I was outside before.
519 0 : if( nOffset < 0 )
520 : {
521 0 : pNd = aInsertPos.GetNode().EndOfSectionNode();
522 0 : if( pNd->GetIndex() >= aEndRg.GetIndex() )
523 0 : break;
524 : }
525 0 : aInsertPos--;
526 0 : --nNewPos;
527 : }
528 :
529 0 : if( nOffset >= 0 )
530 : {
531 : // When just before the insert position a section ends, it is okay when I'm moving backward
532 : // because I want to stay outside the section.
533 : // When moving forward I've to check if I started inside or outside the section
534 : // because I don't want to enter of leave such a section
535 0 : while( aInsertPos.GetNode().IsEndNode() )
536 : {
537 0 : pNd = aInsertPos.GetNode().StartOfSectionNode();
538 0 : if( pNd->GetIndex() >= aSttRg.GetIndex() )
539 0 : break;
540 0 : aInsertPos--;
541 0 : --nNewPos;
542 : }
543 : }
544 : // We do not want to move into tables (at the moment)
545 0 : ++aInsertPos;
546 0 : pNd = &aInsertPos.GetNode();
547 0 : if( pNd->IsTableNode() )
548 0 : pNd = pNd->StartOfSectionNode();
549 0 : if( pNd->FindTableNode() )
550 0 : return false;
551 :
552 : OSL_ENSURE( aSttRg.GetIndex() > nNewPos || nNewPos >= aEndRg.GetIndex(),
553 : "Position lies within Move range" );
554 :
555 : // If a Position inside the special nodes array sections was calculated,
556 : // set it to document start instead.
557 : // Sections or Tables at the document start will be pushed backwards.
558 0 : nNewPos = std::max( nNewPos, GetNodes().GetEndOfExtras().GetIndex() + 2 );
559 :
560 0 : long nOffs = nNewPos - ( 0 < nOffset ? aEndRg.GetIndex() : aSttRg.GetIndex());
561 0 : SwPaM aPam( aSttRg, aEndRg, 0, -1 );
562 0 : return MoveParagraph( aPam, nOffs, true );
563 : }
564 :
565 0 : static sal_uInt16 lcl_FindOutlineName( const SwNodes& rNds, const OUString& rName,
566 : bool bExact )
567 : {
568 0 : sal_uInt16 nSavePos = USHRT_MAX;
569 0 : const SwOutlineNodes& rOutlNds = rNds.GetOutLineNds();
570 0 : for( sal_uInt16 n = 0; n < rOutlNds.size(); ++n )
571 : {
572 0 : SwTxtNode* pTxtNd = rOutlNds[ n ]->GetTxtNode();
573 0 : OUString sTxt( pTxtNd->GetExpandTxt() );
574 0 : if (sTxt == rName)
575 : {
576 : // Found "exact", set Pos to the Node
577 0 : nSavePos = n;
578 0 : break;
579 : }
580 0 : else if( !bExact && USHRT_MAX == nSavePos && sTxt.startsWith(rName) )
581 : {
582 : // maybe we just found the text's first part
583 0 : nSavePos = n;
584 : }
585 0 : }
586 :
587 0 : return nSavePos;
588 : }
589 :
590 0 : static sal_uInt16 lcl_FindOutlineNum( const SwNodes& rNds, OUString& rName )
591 : {
592 : // Valid numbers are (always just offsets!):
593 : // ([Number]+\.)+ (as a regular expression!)
594 : // (Number follwed by a period, with 5 repetitions)
595 : // i.e.: "1.1.", "1.", "1.1.1."
596 0 : sal_Int32 nPos = 0;
597 0 : OUString sNum = rName.getToken( 0, '.', nPos );
598 0 : if( -1 == nPos )
599 0 : return USHRT_MAX; // invalid number!
600 :
601 : sal_uInt16 nLevelVal[ MAXLEVEL ]; // numbers of all levels
602 0 : memset( nLevelVal, 0, MAXLEVEL * sizeof( nLevelVal[0] ));
603 0 : sal_uInt8 nLevel = 0;
604 0 : OUString sName( rName );
605 :
606 0 : while( -1 != nPos )
607 : {
608 0 : sal_uInt16 nVal = 0;
609 : sal_Unicode c;
610 0 : for( sal_uInt16 n = 0; n < sNum.getLength(); ++n )
611 0 : if( '0' <= ( c = sNum[ n ]) && c <= '9' )
612 : {
613 0 : nVal *= 10; nVal += c - '0';
614 : }
615 0 : else if( nLevel )
616 0 : break; // "almost" valid number
617 : else
618 0 : return USHRT_MAX; // invalid number!
619 :
620 0 : if( MAXLEVEL > nLevel )
621 0 : nLevelVal[ nLevel++ ] = nVal;
622 :
623 0 : sName = sName.copy( nPos );
624 0 : nPos = 0;
625 0 : sNum = sName.getToken( 0, '.', nPos );
626 : // #i4533# without this check all parts delimited by a dot are treated as outline numbers
627 0 : if(!comphelper::string::isdigitAsciiString(sNum))
628 0 : nPos = -1;
629 : }
630 0 : rName = sName; // that's the follow-up text
631 :
632 : // read all levels, so search the document for this outline
633 0 : const SwOutlineNodes& rOutlNds = rNds.GetOutLineNds();
634 : // Without OutlineNodes searching doesn't pay off
635 : // and we save a crash
636 0 : if( rOutlNds.empty() )
637 0 : return USHRT_MAX;
638 : SwTxtNode* pNd;
639 0 : nPos = 0;
640 : // search in the existing outline nodes for the required outline num array
641 0 : for( ; nPos < (sal_Int32) rOutlNds.size(); ++nPos )
642 : {
643 0 : pNd = rOutlNds[ nPos ]->GetTxtNode();
644 0 : const int nLvl = pNd->GetAttrOutlineLevel()-1;
645 0 : if( nLvl == nLevel - 1)
646 : {
647 : // #i51089#, #i68289#
648 : // Assure, that text node has the correct numbering level. Otherwise,
649 : // its number vector will not fit to the searched level.
650 0 : if ( pNd->GetNum() &&
651 0 : pNd->GetActualListLevel() == ( nLevel - 1 ) )
652 : {
653 0 : const SwNodeNum & rNdNum = *(pNd->GetNum());
654 0 : SwNumberTree::tNumberVector aLevelVal = rNdNum.GetNumberVector();
655 : // now compare with the one searched for
656 0 : bool bEqual = true;
657 0 : for( sal_uInt8 n = 0; (n < nLevel) && bEqual; ++n )
658 : {
659 0 : bEqual = aLevelVal[n] == nLevelVal[n];
660 : }
661 0 : if(bEqual)
662 : {
663 0 : break;
664 0 : }
665 : }
666 : else
667 : {
668 : // A text node, which has an outline paragraph style applied and
669 : // has as hard attribute 'no numbering' set, has an outline level,
670 : // but no numbering tree node. Thus, consider this situation in
671 : // the assertion condition.
672 : OSL_ENSURE( !pNd->GetNumRule(),
673 : "<lcl_FindOutlineNum(..)> - text node with outline level and numbering rule, but without numbering tree node. This is a serious defect -> inform OD" );
674 : }
675 : }
676 : }
677 0 : if( nPos >= (sal_Int32) rOutlNds.size() )
678 0 : nPos = USHRT_MAX;
679 0 : return nPos;
680 : }
681 :
682 : // Add this bullet point:
683 :
684 : // A Name can contain a Number and/or the Text.
685 :
686 : // First, we try to find the correct Entry via the Number.
687 : // If it exists, we compare the Text, to see if it's the right one.
688 : // If that's not the case, we search again via the Text. If it is
689 : // found, we got the right entry. Or else we use the one found by
690 : // searching for the Number.
691 : // If we don't have a Number, we search via the Text only.
692 0 : bool SwDoc::GotoOutline( SwPosition& rPos, const OUString& rName ) const
693 : {
694 0 : if( !rName.isEmpty() )
695 : {
696 0 : const SwOutlineNodes& rOutlNds = GetNodes().GetOutLineNds();
697 :
698 : // 1. step: via the Number:
699 0 : OUString sName( rName );
700 0 : sal_uInt16 nFndPos = ::lcl_FindOutlineNum( GetNodes(), sName );
701 0 : if( USHRT_MAX != nFndPos )
702 : {
703 0 : SwTxtNode* pNd = rOutlNds[ nFndPos ]->GetTxtNode();
704 0 : OUString sExpandedText = pNd->GetExpandTxt();
705 : //#i4533# leading numbers followed by a dot have been remove while
706 : //searching for the outline position
707 : //to compensate this they must be removed from the paragraphs text content, too
708 0 : sal_Int32 nPos = 0;
709 0 : OUString sTempNum;
710 0 : while(!sExpandedText.isEmpty() && !(sTempNum = sExpandedText.getToken(0, '.', nPos)).isEmpty() &&
711 0 : -1 != nPos &&
712 0 : comphelper::string::isdigitAsciiString(sTempNum))
713 : {
714 0 : sExpandedText = sExpandedText.copy(nPos);
715 0 : nPos = 0;
716 : }
717 :
718 0 : if( sExpandedText != sName )
719 : {
720 0 : sal_uInt16 nTmp = ::lcl_FindOutlineName( GetNodes(), sName, true );
721 0 : if( USHRT_MAX != nTmp ) // found via the Name
722 : {
723 0 : nFndPos = nTmp;
724 0 : pNd = rOutlNds[ nFndPos ]->GetTxtNode();
725 : }
726 : }
727 0 : rPos.nNode = *pNd;
728 0 : rPos.nContent.Assign( pNd, 0 );
729 0 : return true;
730 : }
731 :
732 0 : nFndPos = ::lcl_FindOutlineName( GetNodes(), rName, false );
733 0 : if( USHRT_MAX != nFndPos )
734 : {
735 0 : SwTxtNode* pNd = rOutlNds[ nFndPos ]->GetTxtNode();
736 0 : rPos.nNode = *pNd;
737 0 : rPos.nContent.Assign( pNd, 0 );
738 0 : return true;
739 : }
740 :
741 : // #i68289# additional search on hyperlink URL without its outline numbering part
742 0 : if ( sName != rName )
743 : {
744 0 : nFndPos = ::lcl_FindOutlineName( GetNodes(), sName, false );
745 0 : if( USHRT_MAX != nFndPos )
746 : {
747 0 : SwTxtNode* pNd = rOutlNds[ nFndPos ]->GetTxtNode();
748 0 : rPos.nNode = *pNd;
749 0 : rPos.nContent.Assign( pNd, 0 );
750 0 : return true;
751 : }
752 0 : }
753 : }
754 0 : return false;
755 : }
756 :
757 0 : static void lcl_ChgNumRule( SwDoc& rDoc, const SwNumRule& rRule )
758 : {
759 0 : SwNumRule* pOld = rDoc.FindNumRulePtr( rRule.GetName() );
760 : OSL_ENSURE( pOld, "we cannot proceed without the old NumRule" );
761 :
762 0 : sal_uInt16 nChgFmtLevel = 0;
763 0 : sal_uInt16 nMask = 1;
764 :
765 0 : for ( sal_uInt8 n = 0; n < MAXLEVEL; ++n, nMask <<= 1 )
766 : {
767 0 : const SwNumFmt& rOldFmt = pOld->Get( n ), &rNewFmt = rRule.Get( n );
768 :
769 0 : if ( rOldFmt != rNewFmt )
770 : {
771 0 : nChgFmtLevel |= nMask;
772 : }
773 0 : else if ( SVX_NUM_NUMBER_NONE > rNewFmt.GetNumberingType()
774 0 : && 1 < rNewFmt.GetIncludeUpperLevels()
775 0 : && 0 != ( nChgFmtLevel & GetUpperLvlChg( n, rNewFmt.GetIncludeUpperLevels(), nMask ) ) )
776 : {
777 0 : nChgFmtLevel |= nMask;
778 : }
779 : }
780 :
781 0 : if( !nChgFmtLevel ) // Nothing has been changed?
782 : {
783 0 : const bool bInvalidateNumRule( pOld->IsContinusNum() != rRule.IsContinusNum() );
784 0 : pOld->CheckCharFmts( &rDoc );
785 0 : pOld->SetContinusNum( rRule.IsContinusNum() );
786 :
787 0 : if ( bInvalidateNumRule )
788 : {
789 0 : pOld->SetInvalidRule(sal_True);
790 : }
791 :
792 0 : return ;
793 : }
794 :
795 0 : SwNumRule::tTxtNodeList aTxtNodeList;
796 0 : pOld->GetTxtNodeList( aTxtNodeList );
797 0 : sal_uInt8 nLvl( 0 );
798 0 : for ( SwNumRule::tTxtNodeList::iterator aIter = aTxtNodeList.begin();
799 0 : aIter != aTxtNodeList.end(); ++aIter )
800 : {
801 0 : SwTxtNode* pTxtNd = *aIter;
802 0 : nLvl = static_cast<sal_uInt8>(pTxtNd->GetActualListLevel());
803 :
804 0 : if( nLvl < MAXLEVEL )
805 : {
806 0 : if( nChgFmtLevel & ( 1 << nLvl ))
807 : {
808 0 : pTxtNd->NumRuleChgd();
809 : }
810 : }
811 : }
812 :
813 0 : for ( sal_uInt8 n = 0; n < MAXLEVEL; ++n )
814 0 : if ( nChgFmtLevel & ( 1 << n ) )
815 0 : pOld->Set( n, rRule.GetNumFmt( n ) );
816 :
817 0 : pOld->CheckCharFmts( &rDoc );
818 0 : pOld->SetInvalidRule( sal_True );
819 0 : pOld->SetContinusNum( rRule.IsContinusNum() );
820 :
821 0 : rDoc.UpdateNumRule();
822 : }
823 :
824 0 : void SwDoc::SetNumRule( const SwPaM& rPam,
825 : const SwNumRule& rRule,
826 : const bool bCreateNewList,
827 : const OUString& sContinuedListId,
828 : bool bSetItem,
829 : const bool bResetIndentAttrs )
830 : {
831 0 : SwUndoInsNum * pUndo = NULL;
832 0 : if (GetIDocumentUndoRedo().DoesUndo())
833 : {
834 : // Start/End for attributes!
835 0 : GetIDocumentUndoRedo().StartUndo( UNDO_INSNUM, NULL );
836 0 : pUndo = new SwUndoInsNum( rPam, rRule );
837 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
838 : }
839 :
840 0 : SwNumRule* pNewOrChangedNumRule = FindNumRulePtr( rRule.GetName() );
841 0 : bool bNewNumRuleCreated = false;
842 0 : if ( pNewOrChangedNumRule == NULL )
843 : {
844 : // create new numbering rule based on given one
845 0 : pNewOrChangedNumRule = ( *mpNumRuleTbl )[MakeNumRule( rRule.GetName(), &rRule )];
846 0 : bNewNumRuleCreated = true;
847 : }
848 0 : else if ( rRule != *pNewOrChangedNumRule )
849 : {
850 : // change existing numbering rule
851 0 : if( pUndo != NULL )
852 : {
853 0 : pUndo->SaveOldNumRule( *pNewOrChangedNumRule );
854 : }
855 0 : ::lcl_ChgNumRule( *this, rRule );
856 0 : if( pUndo != NULL )
857 : {
858 0 : pUndo->SetLRSpaceEndPos();
859 : }
860 : }
861 :
862 0 : if ( bSetItem )
863 : {
864 0 : OUString sListId;
865 0 : if ( bCreateNewList )
866 : {
867 0 : if ( bNewNumRuleCreated )
868 : {
869 : // apply list id of list, which has been created for the new list style
870 0 : sListId = pNewOrChangedNumRule->GetDefaultListId();
871 : }
872 : else
873 : {
874 : // create new list and apply its list id
875 0 : const SwList* pNewList = createList( OUString(), pNewOrChangedNumRule->GetName() );
876 : OSL_ENSURE( pNewList,
877 : "<SwDoc::SetNumRule(..)> - could not create new list. Serious defect -> please inform OD." );
878 0 : sListId = pNewList->GetListId();
879 : }
880 : }
881 0 : else if ( !sContinuedListId.isEmpty() )
882 : {
883 : // apply given list id
884 0 : sListId = sContinuedListId;
885 : }
886 0 : if (!sListId.isEmpty())
887 : {
888 0 : InsertPoolItem( rPam, SfxStringItem( RES_PARATR_LIST_ID, sListId ), 0 );
889 0 : }
890 : }
891 :
892 0 : if ( !rPam.HasMark() )
893 : {
894 0 : SwTxtNode * pTxtNd = rPam.GetPoint()->nNode.GetNode().GetTxtNode();
895 : // robust code: consider case that the PaM doesn't denote a text node - e.g. it denotes a graphic node
896 0 : if ( pTxtNd != NULL )
897 : {
898 0 : SwNumRule * pRule = pTxtNd->GetNumRule();
899 :
900 0 : if (pRule && pRule->GetName() == pNewOrChangedNumRule->GetName())
901 : {
902 0 : bSetItem = false;
903 0 : if ( !pTxtNd->IsInList() )
904 : {
905 0 : pTxtNd->AddToList();
906 : }
907 : }
908 : // Only clear numbering attribute at text node, if at paragraph
909 : // style the new numbering rule is found.
910 0 : else if ( !pRule )
911 : {
912 0 : SwTxtFmtColl* pColl = pTxtNd->GetTxtColl();
913 0 : if ( pColl )
914 : {
915 0 : SwNumRule* pCollRule = FindNumRulePtr(pColl->GetNumRule().GetValue());
916 0 : if ( pCollRule && pCollRule->GetName() == pNewOrChangedNumRule->GetName() )
917 : {
918 0 : pTxtNd->ResetAttr( RES_PARATR_NUMRULE );
919 0 : bSetItem = false;
920 : }
921 : }
922 : }
923 : }
924 : }
925 :
926 0 : if ( bSetItem )
927 : {
928 0 : InsertPoolItem( rPam, SwNumRuleItem( pNewOrChangedNumRule->GetName() ), 0 );
929 : }
930 :
931 0 : if ( bResetIndentAttrs
932 0 : && pNewOrChangedNumRule->Get( 0 ).GetPositionAndSpaceMode() == SvxNumberFormat::LABEL_ALIGNMENT )
933 : {
934 0 : ::lcl_ResetIndentAttrs(this, rPam, RES_LR_SPACE);
935 : }
936 :
937 0 : if (GetIDocumentUndoRedo().DoesUndo())
938 : {
939 0 : GetIDocumentUndoRedo().EndUndo( UNDO_INSNUM, NULL );
940 : }
941 :
942 0 : SetModified();
943 0 : }
944 :
945 :
946 0 : void SwDoc::SetCounted(const SwPaM & rPam, bool bCounted)
947 : {
948 0 : if ( bCounted )
949 : {
950 0 : ::lcl_ResetIndentAttrs(this, rPam, RES_PARATR_LIST_ISCOUNTED);
951 : }
952 : else
953 : {
954 0 : InsertPoolItem( rPam, SfxBoolItem( RES_PARATR_LIST_ISCOUNTED, false ), 0 );
955 : }
956 0 : }
957 :
958 0 : void SwDoc::SetNumRuleStart( const SwPosition& rPos, sal_Bool bFlag )
959 : {
960 0 : SwTxtNode* pTxtNd = rPos.nNode.GetNode().GetTxtNode();
961 :
962 0 : if (pTxtNd)
963 : {
964 0 : const SwNumRule* pRule = pTxtNd->GetNumRule();
965 0 : if( pRule && !bFlag != !pTxtNd->IsListRestart())
966 : {
967 0 : if (GetIDocumentUndoRedo().DoesUndo())
968 : {
969 0 : SwUndo *const pUndo( new SwUndoNumRuleStart(rPos, bFlag) );
970 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
971 : }
972 :
973 0 : pTxtNd->SetListRestart(bFlag ? true : false);
974 :
975 0 : SetModified();
976 : }
977 : }
978 0 : }
979 :
980 0 : void SwDoc::SetNodeNumStart( const SwPosition& rPos, sal_uInt16 nStt )
981 : {
982 0 : SwTxtNode* pTxtNd = rPos.nNode.GetNode().GetTxtNode();
983 :
984 0 : if (pTxtNd)
985 : {
986 0 : if ( !pTxtNd->HasAttrListRestartValue() ||
987 0 : pTxtNd->GetAttrListRestartValue() != nStt )
988 : {
989 0 : if (GetIDocumentUndoRedo().DoesUndo())
990 : {
991 0 : SwUndo *const pUndo( new SwUndoNumRuleStart(rPos, nStt) );
992 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
993 : }
994 0 : pTxtNd->SetAttrListRestartValue( nStt );
995 :
996 0 : SetModified();
997 : }
998 : }
999 0 : }
1000 :
1001 : // We can only delete if the Rule is unused!
1002 0 : bool SwDoc::DelNumRule( const OUString& rName, bool bBroadcast )
1003 : {
1004 0 : sal_uInt16 nPos = FindNumRule( rName );
1005 :
1006 0 : if (nPos == USHRT_MAX)
1007 0 : return false;
1008 :
1009 0 : if ( (*mpNumRuleTbl)[ nPos ] == GetOutlineNumRule() )
1010 : {
1011 : OSL_FAIL( "<SwDoc::DelNumRule(..)> - No deletion of outline list style. This is serious defect - please inform OD" );
1012 0 : return false;
1013 : }
1014 :
1015 0 : if( !IsUsed( *(*mpNumRuleTbl)[ nPos ] ))
1016 : {
1017 0 : if (GetIDocumentUndoRedo().DoesUndo())
1018 : {
1019 : SwUndo * pUndo =
1020 0 : new SwUndoNumruleDelete(*(*mpNumRuleTbl)[nPos], this);
1021 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
1022 : }
1023 :
1024 0 : if (bBroadcast)
1025 : BroadcastStyleOperation(rName, SFX_STYLE_FAMILY_PSEUDO,
1026 0 : SFX_STYLESHEET_ERASED);
1027 :
1028 0 : deleteListForListStyle( rName );
1029 : {
1030 : // delete further list, which have the deleted list style as default list style
1031 0 : std::vector< SwList* > aListsForDeletion;
1032 0 : tHashMapForLists::iterator aListIter = maLists.begin();
1033 0 : while ( aListIter != maLists.end() )
1034 : {
1035 0 : SwList* pList = (*aListIter).second;
1036 0 : if ( pList->GetDefaultListStyleName() == rName )
1037 : {
1038 0 : aListsForDeletion.push_back( pList );
1039 : }
1040 :
1041 0 : ++aListIter;
1042 : }
1043 0 : while ( !aListsForDeletion.empty() )
1044 : {
1045 0 : SwList* pList = aListsForDeletion.back();
1046 0 : aListsForDeletion.pop_back();
1047 0 : deleteList( pList->GetListId() );
1048 0 : }
1049 : }
1050 : // #i34097# DeleteAndDestroy deletes rName if
1051 : // rName is directly taken from the numrule.
1052 0 : const OUString aTmpName( rName );
1053 0 : delete (*mpNumRuleTbl)[ nPos ];
1054 0 : mpNumRuleTbl->erase( mpNumRuleTbl->begin() + nPos );
1055 0 : maNumRuleMap.erase(aTmpName);
1056 :
1057 0 : SetModified();
1058 0 : return true;
1059 : }
1060 0 : return false;
1061 : }
1062 :
1063 0 : void SwDoc::ChgNumRuleFmts( const SwNumRule& rRule )
1064 : {
1065 0 : SwNumRule* pRule = FindNumRulePtr( rRule.GetName() );
1066 0 : if( pRule )
1067 : {
1068 0 : SwUndoInsNum* pUndo = 0;
1069 0 : if (GetIDocumentUndoRedo().DoesUndo())
1070 : {
1071 0 : pUndo = new SwUndoInsNum( *pRule, rRule );
1072 0 : pUndo->GetHistory();
1073 0 : GetIDocumentUndoRedo().AppendUndo( pUndo );
1074 : }
1075 0 : ::lcl_ChgNumRule( *this, rRule );
1076 :
1077 0 : if( pUndo )
1078 0 : pUndo->SetLRSpaceEndPos();
1079 :
1080 0 : SetModified();
1081 : }
1082 0 : }
1083 :
1084 0 : bool SwDoc::RenameNumRule(const OUString & rOldName, const OUString & rNewName,
1085 : bool bBroadcast)
1086 : {
1087 0 : bool bResult = false;
1088 0 : SwNumRule * pNumRule = FindNumRulePtr(rOldName);
1089 :
1090 0 : if (pNumRule)
1091 : {
1092 0 : if (GetIDocumentUndoRedo().DoesUndo())
1093 : {
1094 0 : SwUndo * pUndo = new SwUndoNumruleRename(rOldName, rNewName, this);
1095 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
1096 : }
1097 :
1098 0 : SwNumRule::tTxtNodeList aTxtNodeList;
1099 0 : pNumRule->GetTxtNodeList( aTxtNodeList );
1100 :
1101 0 : pNumRule->SetName( rNewName, *this );
1102 :
1103 0 : SwNumRuleItem aItem(rNewName);
1104 :
1105 0 : for ( SwNumRule::tTxtNodeList::iterator aIter = aTxtNodeList.begin();
1106 0 : aIter != aTxtNodeList.end(); ++aIter )
1107 : {
1108 0 : SwTxtNode * pTxtNd = *aIter;
1109 0 : pTxtNd->SetAttr(aItem);
1110 : }
1111 :
1112 0 : bResult = true;
1113 :
1114 0 : if (bBroadcast)
1115 : BroadcastStyleOperation(rOldName, SFX_STYLE_FAMILY_PSEUDO,
1116 0 : SFX_STYLESHEET_MODIFIED);
1117 : }
1118 :
1119 0 : return bResult;
1120 : }
1121 :
1122 0 : void SwDoc::StopNumRuleAnimations( OutputDevice* pOut )
1123 : {
1124 0 : for( sal_uInt16 n = GetNumRuleTbl().size(); n; )
1125 : {
1126 0 : SwNumRule::tTxtNodeList aTxtNodeList;
1127 0 : GetNumRuleTbl()[ --n ]->GetTxtNodeList( aTxtNodeList );
1128 0 : for ( SwNumRule::tTxtNodeList::iterator aTxtNodeIter = aTxtNodeList.begin();
1129 0 : aTxtNodeIter != aTxtNodeList.end(); ++aTxtNodeIter )
1130 : {
1131 0 : SwTxtNode* pTNd = *aTxtNodeIter;
1132 0 : SwIterator<SwTxtFrm,SwTxtNode> aIter(*pTNd);
1133 0 : for(SwTxtFrm* pFrm = aIter.First(); pFrm; pFrm = aIter.Next() )
1134 0 : if( pFrm->HasAnimation() )
1135 0 : pFrm->StopAnimation( pOut );
1136 0 : }
1137 0 : }
1138 0 : }
1139 :
1140 0 : bool SwDoc::ReplaceNumRule( const SwPosition& rPos,
1141 : const OUString& rOldRule, const OUString& rNewRule )
1142 : {
1143 0 : bool bRet = false;
1144 0 : SwNumRule *pOldRule = FindNumRulePtr( rOldRule ),
1145 0 : *pNewRule = FindNumRulePtr( rNewRule );
1146 0 : if( pOldRule && pNewRule && pOldRule != pNewRule )
1147 : {
1148 0 : SwUndoInsNum* pUndo = 0;
1149 0 : if (GetIDocumentUndoRedo().DoesUndo())
1150 : {
1151 : // Start/End for attributes!
1152 0 : GetIDocumentUndoRedo().StartUndo( UNDO_START, NULL );
1153 0 : pUndo = new SwUndoInsNum( rPos, *pNewRule, rOldRule );
1154 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
1155 : }
1156 :
1157 0 : SwNumRule::tTxtNodeList aTxtNodeList;
1158 0 : pOldRule->GetTxtNodeList( aTxtNodeList );
1159 0 : if ( aTxtNodeList.size() > 0 )
1160 : {
1161 0 : SwRegHistory aRegH( pUndo ? pUndo->GetHistory() : 0 );
1162 0 : sal_uInt16 nChgFmtLevel = 0;
1163 0 : for( sal_uInt8 n = 0; n < MAXLEVEL; ++n )
1164 : {
1165 0 : const SwNumFmt& rOldFmt = pOldRule->Get( n ),
1166 0 : & rNewFmt = pNewRule->Get( n );
1167 :
1168 0 : if( rOldFmt.GetAbsLSpace() != rNewFmt.GetAbsLSpace() ||
1169 0 : rOldFmt.GetFirstLineOffset() != rNewFmt.GetFirstLineOffset() )
1170 0 : nChgFmtLevel |= ( 1 << n );
1171 : }
1172 :
1173 0 : const SwTxtNode* pGivenTxtNode = rPos.nNode.GetNode().GetTxtNode();
1174 0 : SwNumRuleItem aRule( rNewRule );
1175 0 : for ( SwNumRule::tTxtNodeList::iterator aIter = aTxtNodeList.begin();
1176 0 : aIter != aTxtNodeList.end(); ++aIter )
1177 : {
1178 0 : SwTxtNode* pTxtNd = *aIter;
1179 :
1180 0 : if ( pGivenTxtNode &&
1181 0 : pGivenTxtNode->GetListId() == pTxtNd->GetListId() )
1182 : {
1183 0 : aRegH.RegisterInModify( pTxtNd, *pTxtNd );
1184 :
1185 0 : pTxtNd->SetAttr( aRule );
1186 0 : pTxtNd->NumRuleChgd();
1187 : }
1188 : }
1189 0 : GetIDocumentUndoRedo().EndUndo( UNDO_END, NULL );
1190 0 : SetModified();
1191 :
1192 0 : bRet = true;
1193 0 : }
1194 : }
1195 :
1196 0 : return bRet;
1197 : }
1198 :
1199 : namespace
1200 : {
1201 0 : struct ListStyleData
1202 : {
1203 : SwNumRule* pReplaceNumRule;
1204 : bool bCreateNewList;
1205 : OUString sListId;
1206 :
1207 0 : ListStyleData()
1208 : : pReplaceNumRule( 0 ),
1209 : bCreateNewList( false ),
1210 0 : sListId()
1211 0 : {}
1212 : };
1213 : }
1214 :
1215 0 : void SwDoc::MakeUniqueNumRules(const SwPaM & rPaM)
1216 : {
1217 : OSL_ENSURE( rPaM.GetDoc() == this, "need same doc" );
1218 :
1219 0 : ::std::map<SwNumRule *, ListStyleData> aMyNumRuleMap;
1220 :
1221 0 : bool bFirst = true;
1222 :
1223 0 : const sal_uLong nStt = rPaM.Start()->nNode.GetIndex();
1224 0 : const sal_uLong nEnd = rPaM.End()->nNode.GetIndex();
1225 0 : for (sal_uLong n = nStt; n <= nEnd; n++)
1226 : {
1227 0 : SwTxtNode * pCNd = GetNodes()[n]->GetTxtNode();
1228 :
1229 0 : if (pCNd)
1230 : {
1231 0 : SwNumRule * pRule = pCNd->GetNumRule();
1232 :
1233 0 : if (pRule && pRule->IsAutoRule() && ! pRule->IsOutlineRule())
1234 : {
1235 0 : ListStyleData aListStyleData = aMyNumRuleMap[pRule];
1236 :
1237 0 : if ( aListStyleData.pReplaceNumRule == 0 )
1238 : {
1239 0 : if (bFirst)
1240 : {
1241 0 : SwPosition aPos(*pCNd);
1242 : aListStyleData.pReplaceNumRule =
1243 : const_cast<SwNumRule *>
1244 0 : (SearchNumRule( aPos, false, pCNd->HasNumber(),
1245 : false, 0,
1246 0 : aListStyleData.sListId, true ));
1247 : }
1248 :
1249 0 : if ( aListStyleData.pReplaceNumRule == 0 )
1250 : {
1251 0 : aListStyleData.pReplaceNumRule = new SwNumRule(*pRule);
1252 0 : aListStyleData.pReplaceNumRule->SetName( GetUniqueNumRuleName(), *this );
1253 0 : aListStyleData.bCreateNewList = true;
1254 : }
1255 :
1256 0 : aMyNumRuleMap[pRule] = aListStyleData;
1257 : }
1258 :
1259 0 : SwPaM aPam(*pCNd);
1260 :
1261 : SetNumRule( aPam,
1262 : *aListStyleData.pReplaceNumRule,
1263 : aListStyleData.bCreateNewList,
1264 0 : aListStyleData.sListId );
1265 0 : if ( aListStyleData.bCreateNewList )
1266 : {
1267 0 : aListStyleData.bCreateNewList = false;
1268 0 : aListStyleData.sListId = pCNd->GetListId();
1269 0 : aMyNumRuleMap[pRule] = aListStyleData;
1270 : }
1271 :
1272 0 : bFirst = false;
1273 : }
1274 : }
1275 0 : }
1276 0 : }
1277 :
1278 0 : bool SwDoc::NoNum( const SwPaM& rPam )
1279 : {
1280 :
1281 0 : bool bRet = SplitNode( *rPam.GetPoint(), false );
1282 : // Do we actually use Numbering at all?
1283 0 : if( bRet )
1284 : {
1285 : // Set NoNum and Upate
1286 0 : const SwNodeIndex& rIdx = rPam.GetPoint()->nNode;
1287 0 : SwTxtNode* pNd = rIdx.GetNode().GetTxtNode();
1288 0 : const SwNumRule* pRule = pNd->GetNumRule();
1289 0 : if( pRule )
1290 : {
1291 0 : pNd->SetCountedInList(false);
1292 :
1293 0 : SetModified();
1294 : }
1295 : else
1296 0 : bRet = false; // no Numbering or just always sal_True?
1297 : }
1298 0 : return bRet;
1299 : }
1300 :
1301 0 : void SwDoc::DelNumRules( const SwPaM& rPam )
1302 : {
1303 0 : sal_uLong nStt = rPam.GetPoint()->nNode.GetIndex(),
1304 0 : nEnd = rPam.GetMark()->nNode.GetIndex();
1305 0 : if( nStt > nEnd )
1306 : {
1307 0 : sal_uLong nTmp = nStt; nStt = nEnd; nEnd = nTmp;
1308 : }
1309 :
1310 : SwUndoDelNum* pUndo;
1311 0 : if (GetIDocumentUndoRedo().DoesUndo())
1312 : {
1313 0 : pUndo = new SwUndoDelNum( rPam );
1314 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
1315 : }
1316 : else
1317 0 : pUndo = 0;
1318 :
1319 0 : SwRegHistory aRegH( pUndo ? pUndo->GetHistory() : 0 );
1320 :
1321 0 : SwNumRuleItem aEmptyRule( aEmptyOUStr );
1322 0 : const SwNode* pOutlNd = 0;
1323 0 : for( ; nStt <= nEnd; ++nStt )
1324 : {
1325 0 : SwTxtNode* pTNd = GetNodes()[ nStt ]->GetTxtNode();
1326 0 : SwNumRule* pNumRuleOfTxtNode = pTNd ? pTNd->GetNumRule() : 0;
1327 0 : if ( pTNd && pNumRuleOfTxtNode )
1328 : {
1329 : // recognize changes of attribute for undo
1330 0 : aRegH.RegisterInModify( pTNd, *pTNd );
1331 :
1332 0 : if( pUndo )
1333 0 : pUndo->AddNode( *pTNd, sal_False );
1334 :
1335 : // directly set list style attribute is reset, otherwise empty
1336 : // list style is applied
1337 0 : const SfxItemSet* pAttrSet = pTNd->GetpSwAttrSet();
1338 0 : if ( pAttrSet &&
1339 0 : pAttrSet->GetItemState( RES_PARATR_NUMRULE, false ) == SFX_ITEM_SET )
1340 0 : pTNd->ResetAttr( RES_PARATR_NUMRULE );
1341 : else
1342 0 : pTNd->SetAttr( aEmptyRule );
1343 :
1344 0 : pTNd->ResetAttr( RES_PARATR_LIST_ID );
1345 0 : pTNd->ResetAttr( RES_PARATR_LIST_LEVEL );
1346 0 : pTNd->ResetAttr( RES_PARATR_LIST_ISRESTART );
1347 0 : pTNd->ResetAttr( RES_PARATR_LIST_RESTARTVALUE );
1348 0 : pTNd->ResetAttr( RES_PARATR_LIST_ISCOUNTED );
1349 :
1350 0 : if( RES_CONDTXTFMTCOLL == pTNd->GetFmtColl()->Which() )
1351 : {
1352 0 : pTNd->ChkCondColl();
1353 : }
1354 0 : else if( !pOutlNd &&
1355 0 : static_cast<SwTxtFmtColl*>(pTNd->GetFmtColl())->IsAssignedToListLevelOfOutlineStyle() )
1356 : {
1357 0 : pOutlNd = pTNd;
1358 : }
1359 : }
1360 : }
1361 :
1362 : // Finally, update all
1363 0 : UpdateNumRule();
1364 :
1365 0 : if( pOutlNd )
1366 0 : GetNodes().UpdtOutlineIdx( *pOutlNd );
1367 0 : }
1368 :
1369 0 : void SwDoc::InvalidateNumRules()
1370 : {
1371 0 : for (sal_uInt16 n = 0; n < mpNumRuleTbl->size(); ++n)
1372 0 : (*mpNumRuleTbl)[n]->SetInvalidRule(sal_True);
1373 0 : }
1374 :
1375 : // To the next/preceding Bullet at the same Level
1376 0 : static bool lcl_IsNumOk( sal_uInt8 nSrchNum, sal_uInt8& rLower, sal_uInt8& rUpper,
1377 : bool bOverUpper, sal_uInt8 nNumber )
1378 : {
1379 : OSL_ENSURE( nNumber < MAXLEVEL,
1380 : "<lcl_IsNumOk(..)> - misusage of method" );
1381 :
1382 0 : bool bRet = false;
1383 : {
1384 0 : if( bOverUpper ? nSrchNum == nNumber : nSrchNum >= nNumber )
1385 0 : bRet = true;
1386 0 : else if( nNumber > rLower )
1387 0 : rLower = nNumber;
1388 0 : else if( nNumber < rUpper )
1389 0 : rUpper = nNumber;
1390 : }
1391 0 : return bRet;
1392 : }
1393 :
1394 0 : static bool lcl_IsValidPrevNextNumNode( const SwNodeIndex& rIdx )
1395 : {
1396 0 : bool bRet = false;
1397 0 : const SwNode& rNd = rIdx.GetNode();
1398 0 : switch( rNd.GetNodeType() )
1399 : {
1400 : case ND_ENDNODE:
1401 0 : bRet = SwTableBoxStartNode == rNd.StartOfSectionNode()->GetStartNodeType() ||
1402 0 : rNd.StartOfSectionNode()->IsSectionNode();
1403 0 : break;
1404 :
1405 : case ND_STARTNODE:
1406 0 : bRet = SwTableBoxStartNode == ((SwStartNode&)rNd).GetStartNodeType();
1407 0 : break;
1408 :
1409 : case ND_SECTIONNODE: // that one's valid, so proceed
1410 0 : bRet = true;
1411 0 : break;
1412 : }
1413 0 : return bRet;
1414 : }
1415 :
1416 0 : static bool lcl_GotoNextPrevNum( SwPosition& rPos, bool bNext,
1417 : bool bOverUpper, sal_uInt8* pUpper, sal_uInt8* pLower )
1418 : {
1419 0 : const SwTxtNode* pNd = rPos.nNode.GetNode().GetTxtNode();
1420 0 : if( !pNd || 0 == pNd->GetNumRule() )
1421 0 : return false;
1422 :
1423 0 : sal_uInt8 nSrchNum = static_cast<sal_uInt8>(pNd->GetActualListLevel());
1424 :
1425 0 : SwNodeIndex aIdx( rPos.nNode );
1426 0 : if( ! pNd->IsCountedInList() )
1427 : {
1428 : // If NO_NUMLEVEL is switched on, we search the preceding Node with Numbering
1429 0 : bool bError = false;
1430 0 : do {
1431 0 : aIdx--;
1432 0 : if( aIdx.GetNode().IsTxtNode() )
1433 : {
1434 0 : pNd = aIdx.GetNode().GetTxtNode();
1435 0 : const SwNumRule* pRule = pNd->GetNumRule();
1436 :
1437 : sal_uInt8 nTmpNum;
1438 :
1439 0 : if( pRule )
1440 : {
1441 0 : nTmpNum = static_cast<sal_uInt8>(pNd->GetActualListLevel());
1442 0 : if( !( ! pNd->IsCountedInList() &&
1443 0 : (nTmpNum >= nSrchNum )) )
1444 0 : break; // found it!
1445 : }
1446 : else
1447 0 : bError = true;
1448 : }
1449 : else
1450 0 : bError = !lcl_IsValidPrevNextNumNode( aIdx );
1451 :
1452 0 : } while( !bError );
1453 0 : if( bError )
1454 0 : return false;
1455 : }
1456 :
1457 0 : sal_uInt8 nLower = nSrchNum, nUpper = nSrchNum;
1458 0 : bool bRet = false;
1459 :
1460 : const SwTxtNode* pLast;
1461 0 : if( bNext )
1462 0 : aIdx++, pLast = pNd;
1463 : else
1464 0 : aIdx--, pLast = 0;
1465 :
1466 0 : while( bNext ? ( aIdx.GetIndex() < aIdx.GetNodes().Count() - 1 )
1467 0 : : aIdx.GetIndex() != 0 )
1468 : {
1469 0 : if( aIdx.GetNode().IsTxtNode() )
1470 : {
1471 0 : pNd = aIdx.GetNode().GetTxtNode();
1472 0 : const SwNumRule* pRule = pNd->GetNumRule();
1473 0 : if( pRule )
1474 : {
1475 0 : if( ::lcl_IsNumOk( nSrchNum, nLower, nUpper, bOverUpper,
1476 0 : static_cast<sal_uInt8>(pNd->GetActualListLevel()) ))
1477 : {
1478 0 : rPos.nNode = aIdx;
1479 0 : rPos.nContent.Assign( (SwTxtNode*)pNd, 0 );
1480 0 : bRet = true;
1481 0 : break;
1482 : }
1483 : else
1484 0 : pLast = pNd;
1485 : }
1486 : else
1487 0 : break;
1488 : }
1489 0 : else if( !lcl_IsValidPrevNextNumNode( aIdx ))
1490 0 : break;
1491 :
1492 0 : if( bNext )
1493 0 : ++aIdx;
1494 : else
1495 0 : aIdx--;
1496 : }
1497 :
1498 0 : if( !bRet && !bOverUpper && pLast ) // do not iterate over higher numbers, but still to the end
1499 : {
1500 0 : if( bNext )
1501 : {
1502 0 : rPos.nNode = aIdx;
1503 0 : if( aIdx.GetNode().IsCntntNode() )
1504 0 : rPos.nContent.Assign( aIdx.GetNode().GetCntntNode(), 0 );
1505 : }
1506 : else
1507 : {
1508 0 : rPos.nNode.Assign( *pLast );
1509 0 : rPos.nContent.Assign( (SwTxtNode*)pLast, 0 );
1510 : }
1511 0 : bRet = true;
1512 : }
1513 :
1514 0 : if( bRet )
1515 : {
1516 0 : if( pUpper )
1517 0 : *pUpper = nUpper;
1518 0 : if( pLower )
1519 0 : *pLower = nLower;
1520 : }
1521 0 : return bRet;
1522 : }
1523 :
1524 0 : bool SwDoc::GotoNextNum( SwPosition& rPos, bool bOverUpper,
1525 : sal_uInt8* pUpper, sal_uInt8* pLower )
1526 : {
1527 0 : return ::lcl_GotoNextPrevNum( rPos, true, bOverUpper, pUpper, pLower );
1528 : }
1529 :
1530 0 : const SwNumRule * SwDoc::SearchNumRule(const SwPosition & rPos,
1531 : const bool bForward,
1532 : const bool bNum,
1533 : const bool bOutline,
1534 : int nNonEmptyAllowed,
1535 : OUString& sListId,
1536 : const bool bInvestigateStartNode)
1537 : {
1538 0 : const SwNumRule * pResult = NULL;
1539 0 : SwTxtNode * pTxtNd = rPos.nNode.GetNode().GetTxtNode();
1540 0 : SwNode * pStartFromNode = pTxtNd;
1541 :
1542 0 : if (pTxtNd)
1543 : {
1544 0 : SwNodeIndex aIdx(rPos.nNode);
1545 :
1546 : // - the start node has also been investigated, if requested.
1547 0 : const SwNode * pNode = NULL;
1548 0 : do
1549 : {
1550 0 : if ( !bInvestigateStartNode )
1551 : {
1552 0 : if (bForward)
1553 0 : ++aIdx;
1554 : else
1555 0 : aIdx--;
1556 : }
1557 :
1558 0 : if (aIdx.GetNode().IsTxtNode())
1559 : {
1560 0 : pTxtNd = aIdx.GetNode().GetTxtNode();
1561 :
1562 0 : const SwNumRule * pNumRule = pTxtNd->GetNumRule();
1563 0 : if (pNumRule)
1564 : {
1565 0 : if ( ( pNumRule->IsOutlineRule() == ( bOutline ? sal_True : sal_False ) ) &&
1566 0 : ( ( bNum && pNumRule->Get(0).IsEnumeration()) ||
1567 0 : ( !bNum && pNumRule->Get(0).IsItemize() ) ) ) // #i22362#, #i29560#
1568 : {
1569 0 : pResult = pTxtNd->GetNumRule();
1570 : // provide also the list id, to which the text node belongs.
1571 0 : sListId = pTxtNd->GetListId();
1572 : }
1573 :
1574 0 : break;
1575 : }
1576 0 : else if (pTxtNd->Len() > 0 || NULL != pTxtNd->GetNumRule())
1577 : {
1578 0 : if (nNonEmptyAllowed == 0)
1579 0 : break;
1580 :
1581 0 : nNonEmptyAllowed--;
1582 :
1583 0 : if (nNonEmptyAllowed < 0)
1584 0 : nNonEmptyAllowed = -1;
1585 : }
1586 : }
1587 :
1588 0 : if ( bInvestigateStartNode )
1589 : {
1590 0 : if (bForward)
1591 0 : ++aIdx;
1592 : else
1593 0 : aIdx--;
1594 : }
1595 :
1596 0 : pNode = &aIdx.GetNode();
1597 : }
1598 0 : while (!(pNode == GetNodes().DocumentSectionStartNode(pStartFromNode) ||
1599 0 : pNode == GetNodes().DocumentSectionEndNode(pStartFromNode)));
1600 : }
1601 :
1602 0 : return pResult;
1603 : }
1604 :
1605 0 : bool SwDoc::GotoPrevNum( SwPosition& rPos, bool bOverUpper,
1606 : sal_uInt8* pUpper, sal_uInt8* pLower )
1607 : {
1608 0 : return ::lcl_GotoNextPrevNum( rPos, false, bOverUpper, pUpper, pLower );
1609 : }
1610 :
1611 0 : bool SwDoc::NumUpDown( const SwPaM& rPam, bool bDown )
1612 : {
1613 0 : sal_uLong nStt = rPam.GetPoint()->nNode.GetIndex(),
1614 0 : nEnd = rPam.GetMark()->nNode.GetIndex();
1615 0 : if( nStt > nEnd )
1616 : {
1617 0 : sal_uLong nTmp = nStt; nStt = nEnd; nEnd = nTmp;
1618 : }
1619 :
1620 : // -> outline nodes are promoted or demoted differently
1621 0 : bool bOnlyOutline = true;
1622 0 : bool bOnlyNonOutline = true;
1623 0 : for (sal_uLong n = nStt; n <= nEnd; n++)
1624 : {
1625 0 : SwTxtNode * pTxtNd = GetNodes()[n]->GetTxtNode();
1626 :
1627 0 : if (pTxtNd)
1628 : {
1629 0 : SwNumRule * pRule = pTxtNd->GetNumRule();
1630 :
1631 0 : if (pRule)
1632 : {
1633 0 : if (pRule->IsOutlineRule())
1634 0 : bOnlyNonOutline = false;
1635 : else
1636 0 : bOnlyOutline = false;
1637 : }
1638 : }
1639 : }
1640 :
1641 0 : bool bRet = true;
1642 0 : sal_Int8 nDiff = bDown ? 1 : -1;
1643 :
1644 0 : if (bOnlyOutline)
1645 0 : bRet = OutlineUpDown(rPam, nDiff);
1646 0 : else if (bOnlyNonOutline)
1647 : {
1648 : /* #i24560#
1649 : Only promote or demote if all selected paragraphs are
1650 : promotable resp. demotable.
1651 : */
1652 0 : for (sal_uLong nTmp = nStt; nTmp <= nEnd; ++nTmp)
1653 : {
1654 0 : SwTxtNode* pTNd = GetNodes()[ nTmp ]->GetTxtNode();
1655 :
1656 : // Make code robust: consider case that the node doesn't denote a
1657 : // text node.
1658 0 : if ( pTNd )
1659 : {
1660 0 : SwNumRule * pRule = pTNd->GetNumRule();
1661 :
1662 0 : if (pRule)
1663 : {
1664 0 : sal_uInt8 nLevel = static_cast<sal_uInt8>(pTNd->GetActualListLevel());
1665 0 : if( (-1 == nDiff && 0 >= nLevel) ||
1666 0 : (1 == nDiff && MAXLEVEL - 1 <= nLevel))
1667 0 : bRet = false;
1668 : }
1669 : }
1670 : }
1671 :
1672 0 : if( bRet )
1673 : {
1674 0 : if (GetIDocumentUndoRedo().DoesUndo())
1675 : {
1676 0 : SwUndo *const pUndo( new SwUndoNumUpDown(rPam, nDiff) );
1677 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
1678 : }
1679 :
1680 0 : for(sal_uLong nTmp = nStt; nTmp <= nEnd; ++nTmp )
1681 : {
1682 0 : SwTxtNode* pTNd = GetNodes()[ nTmp ]->GetTxtNode();
1683 :
1684 0 : if( pTNd)
1685 : {
1686 0 : SwNumRule * pRule = pTNd->GetNumRule();
1687 :
1688 0 : if (pRule)
1689 : {
1690 0 : sal_uInt8 nLevel = static_cast<sal_uInt8>(pTNd->GetActualListLevel());
1691 0 : nLevel = nLevel + nDiff;
1692 :
1693 0 : pTNd->SetAttrListLevel(nLevel);
1694 : }
1695 : }
1696 : }
1697 :
1698 0 : ChkCondColls();
1699 0 : SetModified();
1700 : }
1701 : }
1702 :
1703 0 : return bRet;
1704 : }
1705 :
1706 0 : bool SwDoc::MoveParagraph( const SwPaM& rPam, long nOffset, bool bIsOutlMv )
1707 : {
1708 0 : const SwPosition *pStt = rPam.Start(), *pEnd = rPam.End();
1709 :
1710 0 : sal_uLong nStIdx = pStt->nNode.GetIndex();
1711 0 : sal_uLong nEndIdx = pEnd->nNode.GetIndex();
1712 :
1713 : // Here are some sophisticated checks whether the wished PaM will be moved or not.
1714 : // For moving outlines (bIsOutlMv) I've already done some checks, so here are two different
1715 : // checks...
1716 : SwNode *pTmp1;
1717 : SwNode *pTmp2;
1718 0 : if( bIsOutlMv )
1719 : {
1720 : // For moving chapters (outline) the following reason will deny the move:
1721 : // if a start node is inside the moved range and its end node outside or vice versa.
1722 : // If a start node is the first moved paragraph, its end node has to be within the moved
1723 : // range, too (e.g. as last node).
1724 : // If an end node is the last node of the moved range, its start node has to be a part of
1725 : // the moved section, too.
1726 0 : pTmp1 = GetNodes()[ nStIdx ];
1727 0 : if( pTmp1->IsStartNode() )
1728 : { // First is a start node
1729 0 : pTmp2 = pTmp1->EndOfSectionNode();
1730 0 : if( pTmp2->GetIndex() > nEndIdx )
1731 0 : return false; // Its end node is behind the moved range
1732 : }
1733 0 : pTmp1 = pTmp1->StartOfSectionNode()->EndOfSectionNode();
1734 0 : if( pTmp1->GetIndex() <= nEndIdx )
1735 0 : return false; // End node inside but start node before moved range => no.
1736 0 : pTmp1 = GetNodes()[ nEndIdx ];
1737 0 : if( pTmp1->IsEndNode() )
1738 : { // The last one is an end node
1739 0 : pTmp1 = pTmp1->StartOfSectionNode();
1740 0 : if( pTmp1->GetIndex() < nStIdx )
1741 0 : return false; // Its start node is before the moved range.
1742 : }
1743 0 : pTmp1 = pTmp1->StartOfSectionNode();
1744 0 : if( pTmp1->GetIndex() >= nStIdx )
1745 0 : return false; // A start node which ends behind the moved range => no.
1746 : }
1747 :
1748 : sal_uLong nInStIdx, nInEndIdx;
1749 0 : long nOffs = nOffset;
1750 0 : if( nOffset > 0 )
1751 : {
1752 0 : nInEndIdx = nEndIdx;
1753 0 : nEndIdx += nOffset;
1754 0 : ++nOffs;
1755 : }
1756 : else
1757 : {
1758 : // Impossible to move to negative index
1759 0 : if( sal_uLong(std::abs( nOffset )) > nStIdx)
1760 0 : return false;
1761 :
1762 0 : nInEndIdx = nStIdx - 1;
1763 0 : nStIdx += nOffset;
1764 : }
1765 0 : nInStIdx = nInEndIdx + 1;
1766 : // The following paragraphs shall be swapped:
1767 : // Swap [ nStIdx, nInEndIdx ] with [ nInStIdx, nEndIdx ]
1768 :
1769 0 : if( nEndIdx >= GetNodes().GetEndOfContent().GetIndex() )
1770 0 : return false;
1771 :
1772 0 : if( !bIsOutlMv )
1773 : { // And here the restrictions for moving paragraphs other than chapters (outlines)
1774 : // The plan is to exchange [nStIdx,nInEndIdx] and [nStartIdx,nEndIdx]
1775 : // It will checked if the both "start" nodes as well as the both "end" notes belongs to
1776 : // the same start-end-section. This is more restrictive than the conditions checked above.
1777 : // E.g. a paragraph will not escape from a section or be inserted to another section.
1778 0 : pTmp1 = GetNodes()[ nStIdx ]->StartOfSectionNode();
1779 0 : pTmp2 = GetNodes()[ nInStIdx ]->StartOfSectionNode();
1780 0 : if( pTmp1 != pTmp2 )
1781 0 : return false; // "start" nodes in different sections
1782 0 : pTmp1 = GetNodes()[ nEndIdx ];
1783 0 : bool bIsEndNode = pTmp1->IsEndNode();
1784 0 : if( !pTmp1->IsStartNode() )
1785 : {
1786 0 : pTmp1 = pTmp1->StartOfSectionNode();
1787 0 : if( bIsEndNode ) // For end nodes the first start node is of course inside the range,
1788 0 : pTmp1 = pTmp1->StartOfSectionNode(); // I've to check the start node of the start node.
1789 : }
1790 0 : pTmp1 = pTmp1->EndOfSectionNode();
1791 0 : pTmp2 = GetNodes()[ nInEndIdx ];
1792 0 : if( !pTmp2->IsStartNode() )
1793 : {
1794 0 : bIsEndNode = pTmp2->IsEndNode();
1795 0 : pTmp2 = pTmp2->StartOfSectionNode();
1796 0 : if( bIsEndNode )
1797 0 : pTmp2 = pTmp2->StartOfSectionNode();
1798 : }
1799 0 : pTmp2 = pTmp2->EndOfSectionNode();
1800 0 : if( pTmp1 != pTmp2 )
1801 0 : return false; // The "end" notes are in different sections
1802 : }
1803 :
1804 : // Test for Redlining - Can the Selection be moved at all, actually?
1805 0 : if( !IsIgnoreRedline() )
1806 : {
1807 0 : sal_uInt16 nRedlPos = GetRedlinePos( pStt->nNode.GetNode(), nsRedlineType_t::REDLINE_DELETE );
1808 0 : if( USHRT_MAX != nRedlPos )
1809 : {
1810 0 : SwPosition aStPos( *pStt ), aEndPos( *pEnd );
1811 0 : aStPos.nContent = 0;
1812 0 : SwCntntNode* pCNd = pEnd->nNode.GetNode().GetCntntNode();
1813 0 : aEndPos.nContent = pCNd ? pCNd->Len() : 1;
1814 0 : bool bCheckDel = true;
1815 :
1816 : // There is a some Redline Delete Object for the range
1817 0 : for( ; nRedlPos < GetRedlineTbl().size(); ++nRedlPos )
1818 : {
1819 0 : const SwRangeRedline* pTmp = GetRedlineTbl()[ nRedlPos ];
1820 0 : if( !bCheckDel || nsRedlineType_t::REDLINE_DELETE == pTmp->GetType() )
1821 : {
1822 0 : const SwPosition *pRStt = pTmp->Start(), *pREnd = pTmp->End();
1823 0 : switch( ComparePosition( *pRStt, *pREnd, aStPos, aEndPos ))
1824 : {
1825 : case POS_COLLIDE_START:
1826 : case POS_BEHIND: // Pos1 comes after Pos2
1827 0 : nRedlPos = GetRedlineTbl().size();
1828 0 : break;
1829 :
1830 : case POS_COLLIDE_END:
1831 : case POS_BEFORE: // Pos1 comes before Pos2
1832 0 : break;
1833 : case POS_INSIDE: // Pos1 is completely inside Pos2
1834 : // that's valid, but check all following for overlapping
1835 0 : bCheckDel = false;
1836 0 : break;
1837 :
1838 : case POS_OUTSIDE: // Pos2 is completely inside Pos1
1839 : case POS_EQUAL: // Pos1 is equal to Pos2
1840 : case POS_OVERLAP_BEFORE: // Pos1 overlaps Pos2 in the beginning
1841 : case POS_OVERLAP_BEHIND: // Pos1 overlaps Pos2 at the end
1842 0 : return false;
1843 : }
1844 : }
1845 0 : }
1846 : }
1847 : }
1848 :
1849 : {
1850 : // Send DataChanged before moving. We then can detect
1851 : // which objects are still in the range.
1852 : // After the move they could come before/after the
1853 : // Position.
1854 0 : SwDataChanged aTmp( rPam );
1855 : }
1856 :
1857 0 : SwNodeIndex aIdx( nOffset > 0 ? pEnd->nNode : pStt->nNode, nOffs );
1858 0 : SwNodeRange aMvRg( pStt->nNode, 0, pEnd->nNode, +1 );
1859 :
1860 0 : SwRangeRedline* pOwnRedl = 0;
1861 0 : if( IsRedlineOn() )
1862 : {
1863 : // If the range is completely in the own Redline, we can move it!
1864 0 : sal_uInt16 nRedlPos = GetRedlinePos( pStt->nNode.GetNode(), nsRedlineType_t::REDLINE_INSERT );
1865 0 : if( USHRT_MAX != nRedlPos )
1866 : {
1867 0 : SwRangeRedline* pTmp = GetRedlineTbl()[ nRedlPos ];
1868 0 : const SwPosition *pRStt = pTmp->Start(), *pREnd = pTmp->End();
1869 0 : SwRangeRedline aTmpRedl( nsRedlineType_t::REDLINE_INSERT, rPam );
1870 0 : const SwCntntNode* pCEndNd = pEnd->nNode.GetNode().GetCntntNode();
1871 : // Is completely in the range and is the own Redline too?
1872 0 : if( aTmpRedl.IsOwnRedline( *pTmp ) &&
1873 0 : (pRStt->nNode < pStt->nNode ||
1874 0 : (pRStt->nNode == pStt->nNode && !pRStt->nContent.GetIndex()) ) &&
1875 0 : (pEnd->nNode < pREnd->nNode ||
1876 0 : (pEnd->nNode == pREnd->nNode &&
1877 0 : pCEndNd ? pREnd->nContent.GetIndex() == pCEndNd->Len()
1878 0 : : !pREnd->nContent.GetIndex() )) )
1879 : {
1880 0 : pOwnRedl = pTmp;
1881 0 : if( nRedlPos + 1 < (sal_uInt16)GetRedlineTbl().size() )
1882 : {
1883 0 : pTmp = GetRedlineTbl()[ nRedlPos+1 ];
1884 0 : if( *pTmp->Start() == *pREnd )
1885 : // then don't!
1886 0 : pOwnRedl = 0;
1887 : }
1888 :
1889 0 : if( pOwnRedl &&
1890 0 : !( pRStt->nNode <= aIdx && aIdx <= pREnd->nNode ))
1891 : {
1892 : // it's not in itself, so don't move it
1893 0 : pOwnRedl = 0;
1894 : }
1895 0 : }
1896 : }
1897 :
1898 0 : if( !pOwnRedl )
1899 : {
1900 0 : GetIDocumentUndoRedo().StartUndo( UNDO_START, NULL );
1901 :
1902 : // First the Insert, then the Delete
1903 0 : SwPosition aInsPos( aIdx );
1904 0 : aInsPos.nContent.Assign( aIdx.GetNode().GetCntntNode(), 0 );
1905 :
1906 0 : SwPaM aPam( pStt->nNode, aMvRg.aEnd );
1907 :
1908 0 : SwPaM& rOrigPam = (SwPaM&)rPam;
1909 0 : rOrigPam.DeleteMark();
1910 0 : rOrigPam.GetPoint()->nNode = aIdx.GetIndex() - 1;
1911 :
1912 0 : bool bDelLastPara = !aInsPos.nNode.GetNode().IsCntntNode();
1913 :
1914 : /* When copying to a non-content node Copy will
1915 : insert a paragraph before that node and insert before
1916 : that inserted node. Copy creates an SwUndoInserts that
1917 : does not cover the extra paragraph. Thus we insert the
1918 : extra paragraph ourselves, _with_ correct undo
1919 : information. */
1920 0 : if (bDelLastPara)
1921 : {
1922 : /* aInsPos points to the non-content node. Move it to
1923 : the previous content node. */
1924 0 : SwPaM aInsPam(aInsPos);
1925 0 : const bool bMoved = aInsPam.Move(fnMoveBackward);
1926 : OSL_ENSURE(bMoved, "No content node found!");
1927 :
1928 0 : if (bMoved)
1929 : {
1930 : /* Append the new node after the content node
1931 : found. The new position to insert the moved
1932 : paragraph at is before the inserted
1933 : paragraph. */
1934 0 : AppendTxtNode(*aInsPam.GetPoint());
1935 0 : aInsPos = *aInsPam.GetPoint();
1936 0 : }
1937 : }
1938 :
1939 0 : CopyRange( aPam, aInsPos, false );
1940 0 : if( bDelLastPara )
1941 : {
1942 : // We need to remove the last empty Node again
1943 0 : aIdx = aInsPos.nNode;
1944 0 : SwCntntNode* pCNd = GetNodes().GoPrevious( &aInsPos.nNode );
1945 0 : aInsPos.nContent.Assign( pCNd, pCNd ? pCNd->Len() : 0 );
1946 :
1947 : // All, that are in the to-be-deleted Node, need to be
1948 : // moved to the next Node
1949 : SwPosition* pPos;
1950 0 : for( sal_uInt16 n = 0; n < GetRedlineTbl().size(); ++n )
1951 : {
1952 0 : SwRangeRedline* pTmp = GetRedlineTbl()[ n ];
1953 0 : if( ( pPos = &pTmp->GetBound(true))->nNode == aIdx )
1954 : {
1955 0 : pPos->nNode++;
1956 0 : pPos->nContent.Assign( pPos->nNode.GetNode().GetCntntNode(),0);
1957 : }
1958 0 : if( ( pPos = &pTmp->GetBound(false))->nNode == aIdx )
1959 : {
1960 0 : pPos->nNode++;
1961 0 : pPos->nContent.Assign( pPos->nNode.GetNode().GetCntntNode(),0);
1962 : }
1963 : }
1964 0 : CorrRel( aIdx, aInsPos, 0, sal_False );
1965 :
1966 0 : pCNd->JoinNext();
1967 : }
1968 :
1969 0 : rOrigPam.GetPoint()->nNode++;
1970 0 : rOrigPam.GetPoint()->nContent.Assign( rOrigPam.GetCntntNode(), 0 );
1971 :
1972 0 : RedlineMode_t eOld = GetRedlineMode();
1973 0 : checkRedlining(eOld);
1974 0 : if (GetIDocumentUndoRedo().DoesUndo())
1975 : {
1976 : // Still NEEDS to be optimized (even after 14 years)
1977 : SetRedlineMode(
1978 0 : (RedlineMode_t)(nsRedlineMode_t::REDLINE_ON | nsRedlineMode_t::REDLINE_SHOW_INSERT | nsRedlineMode_t::REDLINE_SHOW_DELETE));
1979 0 : SwUndo *const pUndo(new SwUndoRedlineDelete(aPam, UNDO_DELETE));
1980 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
1981 : }
1982 :
1983 0 : SwRangeRedline* pNewRedline = new SwRangeRedline( nsRedlineType_t::REDLINE_DELETE, aPam );
1984 :
1985 : // prevent assertion from aPam's target being deleted
1986 : // (Alternatively, one could just let aPam go out of scope, but
1987 : // that requires touching a lot of code.)
1988 0 : aPam.GetBound(true).nContent.Assign( NULL, 0 );
1989 0 : aPam.GetBound(false).nContent.Assign( NULL, 0 );
1990 :
1991 0 : AppendRedline( pNewRedline, true );
1992 :
1993 : // Still NEEDS to be optimized!
1994 0 : SetRedlineMode( eOld );
1995 0 : GetIDocumentUndoRedo().EndUndo( UNDO_END, NULL );
1996 0 : SetModified();
1997 :
1998 0 : return true;
1999 : }
2000 : }
2001 :
2002 0 : if( !pOwnRedl && !IsIgnoreRedline() && !GetRedlineTbl().empty() )
2003 : {
2004 0 : SwPaM aTemp(aIdx);
2005 0 : SplitRedline(aTemp);
2006 : }
2007 :
2008 0 : sal_uLong nRedlSttNd(0), nRedlEndNd(0);
2009 0 : if( pOwnRedl )
2010 : {
2011 0 : const SwPosition *pRStt = pOwnRedl->Start(), *pREnd = pOwnRedl->End();
2012 0 : nRedlSttNd = pRStt->nNode.GetIndex();
2013 0 : nRedlEndNd = pREnd->nNode.GetIndex();
2014 : }
2015 :
2016 0 : SwUndoMoveNum* pUndo = 0;
2017 0 : sal_uLong nMoved = 0;
2018 0 : if (GetIDocumentUndoRedo().DoesUndo())
2019 : {
2020 0 : pUndo = new SwUndoMoveNum( rPam, nOffset, bIsOutlMv );
2021 0 : nMoved = rPam.End()->nNode.GetIndex() - rPam.Start()->nNode.GetIndex() + 1;
2022 : }
2023 :
2024 0 : MoveNodeRange( aMvRg, aIdx, DOC_MOVEREDLINES );
2025 :
2026 0 : if( pUndo )
2027 : {
2028 : // i57907: Under circumstances (sections at the end of a chapter)
2029 : // the rPam.Start() is not moved to the new position.
2030 : // But aIdx should be at the new end position and as long as the
2031 : // number of moved paragraphs is nMoved, I know, where the new
2032 : // position is.
2033 0 : pUndo->SetStartNode( aIdx.GetIndex() - nMoved );
2034 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
2035 : }
2036 :
2037 0 : if( pOwnRedl )
2038 : {
2039 0 : SwPosition *pRStt = pOwnRedl->Start(), *pREnd = pOwnRedl->End();
2040 0 : if( pRStt->nNode.GetIndex() != nRedlSttNd )
2041 : {
2042 0 : pRStt->nNode = nRedlSttNd;
2043 0 : pRStt->nContent.Assign( pRStt->nNode.GetNode().GetCntntNode(),0);
2044 : }
2045 0 : if( pREnd->nNode.GetIndex() != nRedlEndNd )
2046 : {
2047 0 : pREnd->nNode = nRedlEndNd;
2048 0 : SwCntntNode* pCNd = pREnd->nNode.GetNode().GetCntntNode();
2049 0 : pREnd->nContent.Assign( pCNd, pCNd ? pCNd->Len() : 0 );
2050 : }
2051 : }
2052 :
2053 0 : SetModified();
2054 0 : return true;
2055 : }
2056 :
2057 0 : bool SwDoc::NumOrNoNum( const SwNodeIndex& rIdx, sal_Bool bDel )
2058 : {
2059 0 : bool bResult = false;
2060 0 : SwTxtNode * pTxtNd = rIdx.GetNode().GetTxtNode();
2061 :
2062 0 : if (pTxtNd && pTxtNd->GetNumRule() != NULL &&
2063 0 : (pTxtNd->HasNumber() || pTxtNd->HasBullet()))
2064 : {
2065 0 : if ( !pTxtNd->IsCountedInList() == !bDel)
2066 : {
2067 0 : sal_Bool bOldNum = bDel;
2068 0 : sal_Bool bNewNum = bDel ? sal_False : sal_True;
2069 0 : pTxtNd->SetCountedInList(bNewNum ? true : false);
2070 :
2071 0 : SetModified();
2072 :
2073 0 : bResult = true;
2074 :
2075 0 : if (GetIDocumentUndoRedo().DoesUndo())
2076 : {
2077 : SwUndoNumOrNoNum * pUndo =
2078 0 : new SwUndoNumOrNoNum(rIdx, bOldNum, bNewNum);
2079 :
2080 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
2081 : }
2082 : }
2083 0 : else if (bDel && pTxtNd->GetNumRule(false) &&
2084 0 : pTxtNd->GetActualListLevel() >= 0 &&
2085 0 : pTxtNd->GetActualListLevel() < MAXLEVEL)
2086 : {
2087 0 : SwPaM aPam(*pTxtNd);
2088 0 : DelNumRules(aPam);
2089 :
2090 0 : bResult = true;
2091 : }
2092 : }
2093 :
2094 0 : return bResult;
2095 : }
2096 :
2097 0 : SwNumRule* SwDoc::GetNumRuleAtPos( const SwPosition& rPos ) const
2098 : {
2099 0 : SwNumRule* pRet = NULL;
2100 0 : SwTxtNode* pTNd = rPos.nNode.GetNode().GetTxtNode();
2101 :
2102 0 : if ( pTNd != NULL )
2103 : {
2104 0 : pRet = pTNd->GetNumRule();
2105 : }
2106 :
2107 0 : return pRet;
2108 : }
2109 :
2110 0 : sal_uInt16 SwDoc::FindNumRule( const OUString& rName ) const
2111 : {
2112 0 : for( sal_uInt16 n = mpNumRuleTbl->size(); n; )
2113 0 : if( (*mpNumRuleTbl)[ --n ]->GetName() == rName )
2114 0 : return n;
2115 :
2116 0 : return USHRT_MAX;
2117 : }
2118 :
2119 0 : SwNumRule* SwDoc::FindNumRulePtr( const OUString& rName ) const
2120 : {
2121 0 : SwNumRule * pResult = 0;
2122 :
2123 0 : pResult = maNumRuleMap[rName];
2124 :
2125 0 : if ( !pResult )
2126 : {
2127 0 : for (sal_uInt16 n = 0; n < mpNumRuleTbl->size(); ++n)
2128 : {
2129 0 : if ((*mpNumRuleTbl)[n]->GetName() == rName)
2130 : {
2131 0 : pResult = (*mpNumRuleTbl)[n];
2132 :
2133 0 : break;
2134 : }
2135 : }
2136 : }
2137 :
2138 0 : return pResult;
2139 : }
2140 :
2141 0 : void SwDoc::AddNumRule(SwNumRule * pRule)
2142 : {
2143 0 : if ((SAL_MAX_UINT16 - 1) <= mpNumRuleTbl->size())
2144 : {
2145 : OSL_ENSURE(false, "SwDoc::AddNumRule: table full.");
2146 0 : abort(); // this should never happen on real documents
2147 : }
2148 0 : mpNumRuleTbl->push_back(pRule);
2149 0 : maNumRuleMap[pRule->GetName()] = pRule;
2150 0 : pRule->SetNumRuleMap(&maNumRuleMap);
2151 :
2152 0 : createListForListStyle( pRule->GetName() );
2153 0 : }
2154 :
2155 0 : sal_uInt16 SwDoc::MakeNumRule( const OUString &rName,
2156 : const SwNumRule* pCpy,
2157 : bool bBroadcast,
2158 : const SvxNumberFormat::SvxNumPositionAndSpaceMode eDefaultNumberFormatPositionAndSpaceMode )
2159 : {
2160 : SwNumRule* pNew;
2161 0 : if( pCpy )
2162 : {
2163 0 : pNew = new SwNumRule( *pCpy );
2164 :
2165 0 : pNew->SetName( GetUniqueNumRuleName( &rName ), *this );
2166 :
2167 0 : if( pNew->GetName() != rName )
2168 : {
2169 0 : pNew->SetPoolFmtId( USHRT_MAX );
2170 0 : pNew->SetPoolHelpId( USHRT_MAX );
2171 0 : pNew->SetPoolHlpFileId( UCHAR_MAX );
2172 0 : pNew->SetDefaultListId( OUString() );
2173 : }
2174 0 : pNew->CheckCharFmts( this );
2175 : }
2176 : else
2177 : {
2178 : pNew = new SwNumRule( GetUniqueNumRuleName( &rName ),
2179 0 : eDefaultNumberFormatPositionAndSpaceMode );
2180 : }
2181 :
2182 0 : sal_uInt16 nRet = mpNumRuleTbl->size();
2183 :
2184 0 : AddNumRule(pNew);
2185 :
2186 0 : if (GetIDocumentUndoRedo().DoesUndo())
2187 : {
2188 0 : SwUndo * pUndo = new SwUndoNumruleCreate(pNew, this);
2189 0 : GetIDocumentUndoRedo().AppendUndo(pUndo);
2190 : }
2191 :
2192 0 : if (bBroadcast)
2193 : BroadcastStyleOperation(pNew->GetName(), SFX_STYLE_FAMILY_PSEUDO,
2194 0 : SFX_STYLESHEET_CREATED);
2195 :
2196 0 : return nRet;
2197 : }
2198 :
2199 0 : OUString SwDoc::GetUniqueNumRuleName( const OUString* pChkStr, bool bAutoNum ) const
2200 : {
2201 0 : OUString aName;
2202 0 : if( bAutoNum )
2203 : {
2204 0 : static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != NULL);
2205 :
2206 0 : if (bHack)
2207 : {
2208 : static sal_Int64 nIdCounter = SAL_CONST_INT64(8000000000);
2209 0 : aName = OUString::number(nIdCounter++);
2210 : }
2211 : else
2212 : {
2213 0 : static rtlRandomPool s_RandomPool( rtl_random_createPool() );
2214 : sal_Int64 n;
2215 0 : rtl_random_getBytes( s_RandomPool, &n, sizeof(n) );
2216 0 : aName = OUString::number( (n < 0 ? -n : n) );
2217 : }
2218 0 : if( pChkStr && pChkStr->isEmpty() )
2219 0 : pChkStr = 0;
2220 : }
2221 0 : else if( pChkStr && !pChkStr->isEmpty() )
2222 0 : aName = *pChkStr;
2223 : else
2224 : {
2225 0 : pChkStr = 0;
2226 0 : aName = SW_RESSTR( STR_NUMRULE_DEFNAME );
2227 : }
2228 :
2229 0 : sal_uInt16 nNum(0), nTmp, nFlagSize = ( mpNumRuleTbl->size() / 8 ) +2;
2230 0 : sal_uInt8* pSetFlags = new sal_uInt8[ nFlagSize ];
2231 0 : memset( pSetFlags, 0, nFlagSize );
2232 :
2233 0 : sal_Int32 nNmLen = aName.getLength();
2234 0 : if( !bAutoNum && pChkStr )
2235 : {
2236 0 : while( nNmLen-- && '0' <= aName[nNmLen] && aName[nNmLen] <= '9' )
2237 : ; //nop
2238 :
2239 0 : if( ++nNmLen < aName.getLength() )
2240 : {
2241 0 : aName = aName.copy(0, nNmLen );
2242 0 : pChkStr = 0;
2243 : }
2244 : }
2245 :
2246 : const SwNumRule* pNumRule;
2247 : sal_uInt16 n;
2248 :
2249 0 : for( n = 0; n < mpNumRuleTbl->size(); ++n )
2250 0 : if( 0 != ( pNumRule = (*mpNumRuleTbl)[ n ] ) )
2251 : {
2252 0 : const OUString sNm = pNumRule->GetName();
2253 0 : if( sNm.startsWith( aName ) )
2254 : {
2255 : // Determine Number and set the Flag
2256 0 : nNum = (sal_uInt16)sNm.copy( nNmLen ).toInt32();
2257 0 : if( nNum-- && nNum < mpNumRuleTbl->size() )
2258 0 : pSetFlags[ nNum / 8 ] |= (0x01 << ( nNum & 0x07 ));
2259 : }
2260 0 : if( pChkStr && *pChkStr==sNm )
2261 0 : pChkStr = 0;
2262 : }
2263 :
2264 0 : if( !pChkStr )
2265 : {
2266 : // All Numbers have been flagged accordingly, so identify the right Number
2267 0 : nNum = mpNumRuleTbl->size();
2268 0 : for( n = 0; n < nFlagSize; ++n )
2269 0 : if( 0xff != ( nTmp = pSetFlags[ n ] ))
2270 : {
2271 : // identify the Number
2272 0 : nNum = n * 8;
2273 0 : while( nTmp & 1 )
2274 0 : ++nNum, nTmp >>= 1;
2275 0 : break;
2276 : }
2277 : }
2278 0 : delete [] pSetFlags;
2279 0 : if( pChkStr && !pChkStr->isEmpty() )
2280 0 : return *pChkStr;
2281 0 : return aName + OUString::number( ++nNum );
2282 : }
2283 :
2284 0 : void SwDoc::UpdateNumRule()
2285 : {
2286 0 : const SwNumRuleTbl& rNmTbl = GetNumRuleTbl();
2287 0 : for( sal_uInt16 n = 0; n < rNmTbl.size(); ++n )
2288 0 : if( rNmTbl[ n ]->IsInvalidRule() )
2289 0 : rNmTbl[ n ]->Validate();
2290 0 : }
2291 :
2292 0 : void SwDoc::MarkListLevel( const OUString& sListId,
2293 : const int nListLevel,
2294 : const bool bValue )
2295 : {
2296 0 : SwList* pList = getListByName( sListId );
2297 :
2298 0 : if ( pList )
2299 : {
2300 0 : MarkListLevel( *pList, nListLevel, bValue );
2301 : }
2302 0 : }
2303 :
2304 0 : void SwDoc::MarkListLevel( SwList& rList,
2305 : const int nListLevel,
2306 : const bool bValue )
2307 : {
2308 : // Set new marked list level and notify all affected nodes of the changed mark.
2309 0 : rList.MarkListLevel( nListLevel, bValue );
2310 0 : }
2311 :
2312 0 : bool SwDoc::IsFirstOfNumRuleAtPos( const SwPosition & rPos )
2313 : {
2314 0 : bool bResult = false;
2315 :
2316 0 : const SwTxtNode* pTxtNode = rPos.nNode.GetNode().GetTxtNode();
2317 0 : if ( pTxtNode != NULL )
2318 : {
2319 0 : bResult = pTxtNode->IsFirstOfNumRule();
2320 : }
2321 :
2322 0 : return bResult;
2323 : }
2324 :
2325 : // implementation for interface <IDocumentListItems>
2326 0 : bool SwDoc::lessThanNodeNum::operator()( const SwNodeNum* pNodeNumOne,
2327 : const SwNodeNum* pNodeNumTwo ) const
2328 : {
2329 0 : return pNodeNumOne->LessThan( *pNodeNumTwo );
2330 : }
2331 :
2332 0 : void SwDoc::addListItem( const SwNodeNum& rNodeNum )
2333 : {
2334 0 : if ( mpListItemsList == 0 )
2335 : {
2336 0 : return;
2337 : }
2338 :
2339 : const bool bAlreadyInserted(
2340 0 : mpListItemsList->find( &rNodeNum ) != mpListItemsList->end() );
2341 : OSL_ENSURE( !bAlreadyInserted,
2342 : "<SwDoc::InsertListItem(..)> - <SwNodeNum> instance already registered as numbered item!" );
2343 0 : if ( !bAlreadyInserted )
2344 : {
2345 0 : mpListItemsList->insert( &rNodeNum );
2346 : }
2347 : }
2348 :
2349 0 : void SwDoc::removeListItem( const SwNodeNum& rNodeNum )
2350 : {
2351 0 : if ( mpListItemsList == 0 )
2352 : {
2353 0 : return;
2354 : }
2355 :
2356 0 : const tImplSortedNodeNumList::size_type nDeleted = mpListItemsList->erase( &rNodeNum );
2357 : if ( nDeleted > 1 )
2358 : {
2359 : OSL_FAIL( "<SwDoc::RemoveListItem(..)> - <SwNodeNum> was registered more than once as numbered item!" );
2360 : }
2361 : }
2362 :
2363 0 : OUString SwDoc::getListItemText( const SwNodeNum& rNodeNum,
2364 : const bool bWithNumber,
2365 : const bool bWithSpacesForLevel ) const
2366 : {
2367 0 : return rNodeNum.GetTxtNode()
2368 : ? rNodeNum.GetTxtNode()->GetExpandTxt( 0, -1, bWithNumber,
2369 : bWithNumber, bWithSpacesForLevel )
2370 0 : : OUString();
2371 : }
2372 :
2373 0 : void SwDoc::getListItems( tSortedNodeNumList& orNodeNumList ) const
2374 : {
2375 0 : orNodeNumList.clear();
2376 0 : orNodeNumList.reserve( mpListItemsList->size() );
2377 :
2378 0 : tImplSortedNodeNumList::iterator aIter;
2379 0 : tImplSortedNodeNumList::iterator aEndIter = mpListItemsList->end();
2380 0 : for ( aIter = mpListItemsList->begin(); aIter != aEndIter; ++aIter )
2381 : {
2382 0 : orNodeNumList.push_back( (*aIter) );
2383 : }
2384 0 : }
2385 :
2386 0 : void SwDoc::getNumItems( tSortedNodeNumList& orNodeNumList ) const
2387 : {
2388 0 : orNodeNumList.clear();
2389 0 : orNodeNumList.reserve( mpListItemsList->size() );
2390 :
2391 0 : tImplSortedNodeNumList::iterator aIter;
2392 0 : tImplSortedNodeNumList::iterator aEndIter = mpListItemsList->end();
2393 0 : for ( aIter = mpListItemsList->begin(); aIter != aEndIter; ++aIter )
2394 : {
2395 0 : const SwNodeNum* pNodeNum = (*aIter);
2396 0 : if ( pNodeNum->IsCounted() &&
2397 0 : pNodeNum->GetTxtNode() && pNodeNum->GetTxtNode()->HasNumber() )
2398 : {
2399 0 : orNodeNumList.push_back( pNodeNum );
2400 : }
2401 : }
2402 0 : }
2403 :
2404 : // implementation for interface <IDocumentOutlineNodes>
2405 0 : sal_Int32 SwDoc::getOutlineNodesCount() const
2406 : {
2407 0 : return GetNodes().GetOutLineNds().size();
2408 : }
2409 :
2410 0 : int SwDoc::getOutlineLevel( const sal_Int32 nIdx ) const
2411 : {
2412 0 : return GetNodes().GetOutLineNds()[ static_cast<sal_uInt16>(nIdx) ]->
2413 0 : GetTxtNode()->GetAttrOutlineLevel()-1;
2414 : }
2415 :
2416 0 : OUString SwDoc::getOutlineText( const sal_Int32 nIdx,
2417 : const bool bWithNumber,
2418 : const bool bWithSpacesForLevel,
2419 : const bool bWithFtn ) const
2420 : {
2421 0 : return GetNodes().GetOutLineNds()[ static_cast<sal_uInt16>(nIdx) ]->
2422 : GetTxtNode()->GetExpandTxt( 0, -1, bWithNumber,
2423 0 : bWithNumber, bWithSpacesForLevel, bWithFtn );
2424 : }
2425 :
2426 0 : SwTxtNode* SwDoc::getOutlineNode( const sal_Int32 nIdx ) const
2427 : {
2428 0 : return GetNodes().GetOutLineNds()[ static_cast<sal_uInt16>(nIdx) ]->GetTxtNode();
2429 : }
2430 :
2431 0 : void SwDoc::getOutlineNodes( IDocumentOutlineNodes::tSortedOutlineNodeList& orOutlineNodeList ) const
2432 : {
2433 0 : orOutlineNodeList.clear();
2434 0 : orOutlineNodeList.reserve( getOutlineNodesCount() );
2435 :
2436 0 : const sal_uInt16 nOutlCount( static_cast<sal_uInt16>(getOutlineNodesCount()) );
2437 0 : for ( sal_uInt16 i = 0; i < nOutlCount; ++i )
2438 : {
2439 : orOutlineNodeList.push_back(
2440 0 : GetNodes().GetOutLineNds()[i]->GetTxtNode() );
2441 : }
2442 0 : }
2443 :
2444 : // implementation of interface IDocumentListsAccess
2445 0 : SwList* SwDoc::createList( const OUString& rListId,
2446 : const OUString& sDefaultListStyleName )
2447 : {
2448 0 : OUString sListId = rListId;
2449 0 : if ( sListId.isEmpty() )
2450 : {
2451 0 : sListId = listfunc::CreateUniqueListId( *this );
2452 : }
2453 :
2454 0 : if ( getListByName( sListId ) )
2455 : {
2456 : OSL_FAIL( "<SwDoc::createList(..)> - provided list id already used. Serious defect -> please inform OD." );
2457 0 : return 0;
2458 : }
2459 :
2460 0 : SwNumRule* pDefaultNumRuleForNewList = FindNumRulePtr( sDefaultListStyleName );
2461 0 : if ( !pDefaultNumRuleForNewList )
2462 : {
2463 : OSL_FAIL( "<SwDoc::createList(..)> - for provided default list style name no list style is found. Serious defect -> please inform OD." );
2464 0 : return 0;
2465 : }
2466 :
2467 0 : SwList* pNewList = new SwList( sListId, *pDefaultNumRuleForNewList, GetNodes() );
2468 0 : maLists[sListId] = pNewList;
2469 :
2470 0 : return pNewList;
2471 : }
2472 :
2473 0 : void SwDoc::deleteList( const OUString& sListId )
2474 : {
2475 0 : SwList* pList = getListByName( sListId );
2476 0 : if ( pList )
2477 : {
2478 0 : maLists.erase( sListId );
2479 0 : delete pList;
2480 : }
2481 0 : }
2482 :
2483 0 : SwList* SwDoc::getListByName( const OUString& sListId ) const
2484 : {
2485 0 : SwList* pList = 0;
2486 :
2487 : boost::unordered_map< OUString, SwList*, OUStringHash >::const_iterator
2488 0 : aListIter = maLists.find( sListId );
2489 0 : if ( aListIter != maLists.end() )
2490 : {
2491 0 : pList = (*aListIter).second;
2492 : }
2493 :
2494 0 : return pList;
2495 : }
2496 :
2497 0 : SwList* SwDoc::createListForListStyle( const OUString& sListStyleName )
2498 : {
2499 0 : if ( sListStyleName.isEmpty() )
2500 : {
2501 : OSL_FAIL( "<SwDoc::createListForListStyle(..)> - no list style name provided. Serious defect -> please inform OD." );
2502 0 : return 0;
2503 : }
2504 :
2505 0 : if ( getListForListStyle( sListStyleName ) )
2506 : {
2507 : OSL_FAIL( "<SwDoc::createListForListStyle(..)> - a list for the provided list style name already exists. Serious defect -> please inform OD." );
2508 0 : return 0;
2509 : }
2510 :
2511 0 : SwNumRule* pNumRule = FindNumRulePtr( sListStyleName );
2512 0 : if ( !pNumRule )
2513 : {
2514 : OSL_FAIL( "<SwDoc::createListForListStyle(..)> - for provided list style name no list style is found. Serious defect -> please inform OD." );
2515 0 : return 0;
2516 : }
2517 :
2518 0 : OUString sListId( pNumRule->GetDefaultListId() ); // can be empty String
2519 0 : if ( getListByName( sListId ) )
2520 : {
2521 0 : sListId = OUString();
2522 : }
2523 0 : SwList* pNewList = createList( sListId, sListStyleName );
2524 0 : maListStyleLists[sListStyleName] = pNewList;
2525 0 : pNumRule->SetDefaultListId( pNewList->GetListId() );
2526 :
2527 0 : return pNewList;
2528 : }
2529 :
2530 0 : SwList* SwDoc::getListForListStyle( const OUString& sListStyleName ) const
2531 : {
2532 0 : SwList* pList = 0;
2533 :
2534 : boost::unordered_map< OUString, SwList*, OUStringHash >::const_iterator
2535 0 : aListIter = maListStyleLists.find( sListStyleName );
2536 0 : if ( aListIter != maListStyleLists.end() )
2537 : {
2538 0 : pList = (*aListIter).second;
2539 : }
2540 :
2541 0 : return pList;
2542 : }
2543 :
2544 0 : void SwDoc::deleteListForListStyle( const OUString& sListStyleName )
2545 : {
2546 0 : OUString sListId;
2547 : {
2548 0 : SwList* pList = getListForListStyle( sListStyleName );
2549 : OSL_ENSURE( pList,
2550 : "<SwDoc::deleteListForListStyle(..)> - misusage of method: no list found for given list style name" );
2551 0 : if ( pList )
2552 : {
2553 0 : sListId = pList->GetListId();
2554 : }
2555 : }
2556 0 : if ( !sListId.isEmpty() )
2557 : {
2558 0 : maListStyleLists.erase( sListStyleName );
2559 0 : deleteList( sListId );
2560 0 : }
2561 0 : }
2562 :
2563 0 : void SwDoc::trackChangeOfListStyleName( const OUString& sListStyleName,
2564 : const OUString& sNewListStyleName )
2565 : {
2566 0 : SwList* pList = getListForListStyle( sListStyleName );
2567 : OSL_ENSURE( pList,
2568 : "<SwDoc::changeOfListStyleName(..)> - misusage of method: no list found for given list style name" );
2569 :
2570 0 : if ( pList != 0 )
2571 : {
2572 0 : maListStyleLists.erase( sListStyleName );
2573 0 : maListStyleLists[sNewListStyleName] = pList;
2574 : }
2575 0 : }
2576 :
2577 : namespace listfunc
2578 : {
2579 0 : const OUString MakeListIdUnique( const SwDoc& rDoc,
2580 : const OUString& aSuggestedUniqueListId )
2581 : {
2582 0 : long nHitCount = 0;
2583 0 : OUString aTmpStr = aSuggestedUniqueListId;
2584 0 : while ( rDoc.getListByName( aTmpStr ) )
2585 : {
2586 0 : ++nHitCount;
2587 0 : aTmpStr = aSuggestedUniqueListId;
2588 0 : aTmpStr += OUString::number( nHitCount );
2589 : }
2590 :
2591 0 : return aTmpStr;
2592 : }
2593 0 : const OUString CreateUniqueListId( const SwDoc& rDoc )
2594 : {
2595 0 : static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != NULL);
2596 :
2597 0 : if (bHack)
2598 : {
2599 : static sal_Int64 nIdCounter = SAL_CONST_INT64(7000000000);
2600 0 : return MakeListIdUnique( rDoc, OUString( "list" + OUString::number(nIdCounter++) ) );
2601 : }
2602 : else
2603 : {
2604 : // #i92478#
2605 0 : OUString aNewListId( "list" );
2606 : // #o12311627#
2607 0 : static rtlRandomPool s_RandomPool( rtl_random_createPool() );
2608 : sal_Int64 n;
2609 0 : rtl_random_getBytes( s_RandomPool, &n, sizeof(n) );
2610 0 : aNewListId += OUString::number( (n < 0 ? -n : n) );
2611 :
2612 0 : return MakeListIdUnique( rDoc, aNewListId );
2613 : }
2614 : }
2615 : }
2616 :
2617 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|