xref: /AOO41X/main/sw/source/core/docnode/ndsect.cxx (revision 707fc0d4d52eb4f69d89a98ffec6918ca5de6326)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <svl/smplhint.hxx>
28 #include <hintids.hxx>
29 #include <sfx2/linkmgr.hxx>
30 #include <svl/itemiter.hxx>
31 #include <tools/resid.hxx>
32 #include <fmtcntnt.hxx>
33 #include <fmtanchr.hxx>
34 #include <txtftn.hxx>
35 #include <fmtclds.hxx>
36 #include <doc.hxx>
37 #include <IDocumentUndoRedo.hxx>
38 #include <rootfrm.hxx>
39 #include <pam.hxx>
40 #include <ndtxt.hxx>
41 #include <section.hxx>
42 #include <UndoSection.hxx>
43 #include <UndoDelete.hxx>
44 #include <swundo.hxx>
45 #include <calc.hxx>
46 #include <swtable.hxx>
47 #include <swserv.hxx>
48 #include <frmfmt.hxx>
49 #include <frmtool.hxx>
50 #include <ftnidx.hxx>
51 #include <docary.hxx>
52 #include <redline.hxx>
53 #include <sectfrm.hxx>
54 #include <pagefrm.hxx>
55 #include <cntfrm.hxx>
56 #include <node2lay.hxx>
57 #include <doctxm.hxx>
58 #include <fmtftntx.hxx>
59 #include <comcore.hrc>
60 // --> OD 2005-12-01 #i27138#
61 #include <viewsh.hxx>
62 #include <txtfrm.hxx>
63 // <--
64 
65 
66 // OD 04.11.2003 #i21457# - new implementation of local method <lcl_IsInSameTblBox(..)>.
67 // Method now determines the previous/next on its own. Thus, it can be controlled,
68 // for which previous/next is checked, if it's visible.
69 bool lcl_IsInSameTblBox( SwNodes& _rNds,
70                          const SwNode& _rNd,
71                          const bool _bPrev )
72 {
73     const SwTableNode* pTblNd = _rNd.FindTableNode();
74     if ( !pTblNd )
75     {
76         return true;
77     }
78 
79     // determine index to be checked. Its assumed that a previous/next exist.
80     SwNodeIndex aChkIdx( _rNd );
81     {
82         // determine index of previous/next - skip hidden ones, which are
83         // inside the table.
84         // If found one is before/after table, this one isn't in the same
85         // table box as <_rNd>.
86         bool bFound = false;
87         do
88         {
89             if ( _bPrev
90                  ? !_rNds.GoPrevSection( &aChkIdx, sal_False, sal_False )
91                  : !_rNds.GoNextSection( &aChkIdx, sal_False, sal_False ) )
92             {
93                 ASSERT( false, "<lcl_IsInSameTblBox(..)> - no previous/next!" );
94                 return false;
95             }
96             else
97             {
98                 if ( aChkIdx < pTblNd->GetIndex() ||
99                      aChkIdx > pTblNd->EndOfSectionNode()->GetIndex() )
100                 {
101                     return false;
102                 }
103                 else
104                 {
105                     // check, if found one isn't inside a hidden section, which
106                     // is also inside the table.
107                     SwSectionNode* pSectNd = aChkIdx.GetNode().FindSectionNode();
108                     if ( !pSectNd ||
109                          pSectNd->GetIndex() < pTblNd->GetIndex() ||
110                          !pSectNd->GetSection().IsHiddenFlag() )
111                     {
112                         bFound = true;
113                     }
114                 }
115             }
116         } while ( !bFound );
117     }
118 
119     // dann suche den StartNode der Box
120     const SwTableSortBoxes& rSortBoxes = pTblNd->GetTable().GetTabSortBoxes();
121     sal_uLong nIdx = _rNd.GetIndex();
122     for( sal_uInt16 n = 0; n < rSortBoxes.Count(); ++n )
123     {
124         const SwStartNode* pNd = rSortBoxes[ n ]->GetSttNd();
125         if ( pNd->GetIndex() < nIdx && nIdx < pNd->EndOfSectionIndex() )
126         {
127             // dann muss der andere Index in derselben Section liegen
128             nIdx = aChkIdx.GetIndex();
129             return pNd->GetIndex() < nIdx && nIdx < pNd->EndOfSectionIndex();
130         }
131     }
132 
133     return true;
134 }
135 
136 void lcl_CheckEmptyLayFrm( SwNodes& rNds, SwSectionData& rSectionData,
137                         const SwNode& rStt, const SwNode& rEnd )
138 {
139     SwNodeIndex aIdx( rStt );
140     if( !rNds.GoPrevSection( &aIdx, sal_True, sal_False ) ||
141         !CheckNodesRange( rStt, aIdx, sal_True ) ||
142         // OD 04.11.2003 #i21457#
143         !lcl_IsInSameTblBox( rNds, rStt, true ))
144     {
145         aIdx = rEnd;
146         if( !rNds.GoNextSection( &aIdx, sal_True, sal_False ) ||
147             !CheckNodesRange( rEnd, aIdx, sal_True ) ||
148             // OD 04.11.2003 #i21457#
149             !lcl_IsInSameTblBox( rNds, rEnd, false ))
150         {
151             rSectionData.SetHidden( false );
152         }
153     }
154 }
155 
156 SwSection *
157 SwDoc::InsertSwSection(SwPaM const& rRange, SwSectionData & rNewData,
158                        SwTOXBase const*const pTOXBase,
159                        SfxItemSet const*const pAttr, bool const bUpdate)
160 {
161     const SwNode* pPrvNd = 0;
162     sal_uInt16 nRegionRet = 0;
163     if( rRange.HasMark() &&
164         0 == ( nRegionRet = IsInsRegionAvailable( rRange, &pPrvNd ) ))
165     {
166         ASSERT( !this, "Selection ueber verschiedene Sections" );
167         return 0;
168     }
169 
170     // Teste ob das gesamte Dokument versteckt werden soll,
171     // koennen wir zur Zeit nicht !!!!
172     if (rNewData.IsHidden() && rRange.HasMark())
173     {
174         const SwPosition *pStt = rRange.Start(), *pEnd = rRange.End();
175         if( !pStt->nContent.GetIndex() &&
176             pEnd->nNode.GetNode().GetCntntNode()->Len() ==
177             pEnd->nContent.GetIndex() )
178         {
179             ::lcl_CheckEmptyLayFrm( GetNodes(),
180                                     rNewData,
181                                     pStt->nNode.GetNode(),
182                                     pEnd->nNode.GetNode() );
183         }
184     }
185 
186     SwUndoInsSection* pUndoInsSect = 0;
187     bool const bUndo(GetIDocumentUndoRedo().DoesUndo());
188     if (bUndo)
189     {
190         pUndoInsSect = new SwUndoInsSection(rRange, rNewData, pAttr, pTOXBase);
191         GetIDocumentUndoRedo().AppendUndo( pUndoInsSect );
192         GetIDocumentUndoRedo().DoUndo(false);
193     }
194 
195     SwSectionFmt* const pFmt = MakeSectionFmt( 0 );
196     if ( pAttr )
197     {
198         pFmt->SetFmtAttr( *pAttr );
199     }
200 
201     SwSectionNode* pNewSectNode = 0;
202 
203     RedlineMode_t eOld = GetRedlineMode();
204     SetRedlineMode_intern( (RedlineMode_t)((eOld & ~nsRedlineMode_t::REDLINE_SHOW_MASK) | nsRedlineMode_t::REDLINE_IGNORE));
205 
206     if( rRange.HasMark() )
207     {
208         SwPosition *pSttPos = (SwPosition*)rRange.Start(),
209                     *pEndPos = (SwPosition*)rRange.End();
210         if( pPrvNd && 3 == nRegionRet )
211         {
212             ASSERT( pPrvNd, "der SectionNode fehlt" );
213             SwNodeIndex aStt( pSttPos->nNode ), aEnd( pEndPos->nNode, +1 );
214             while( pPrvNd != aStt.GetNode().StartOfSectionNode() )
215                 aStt--;
216             while( pPrvNd != aEnd.GetNode().StartOfSectionNode() )
217                 aEnd++;
218 
219             --aEnd;     // im InsertSection ist Ende inclusive
220             pNewSectNode = GetNodes().InsertTextSection(
221                         aStt, *pFmt, rNewData, pTOXBase, & aEnd);
222         }
223         else
224         {
225             if( pUndoInsSect )
226             {
227                 if( !( pPrvNd && 1 == nRegionRet ) &&
228                     pSttPos->nContent.GetIndex() )
229                 {
230                     SwTxtNode* const pTNd =
231                         pSttPos->nNode.GetNode().GetTxtNode();
232                     if (pTNd)
233                     {
234                         pUndoInsSect->SaveSplitNode( pTNd, sal_True );
235                     }
236                 }
237 
238                 if ( !( pPrvNd && 2 == nRegionRet ) )
239                 {
240                     SwTxtNode *const pTNd =
241                         pEndPos->nNode.GetNode().GetTxtNode();
242                     if (pTNd &&
243                         (pTNd->GetTxt().Len() != pEndPos->nContent.GetIndex()))
244                     {
245                         pUndoInsSect->SaveSplitNode( pTNd, sal_False );
246                     }
247                 }
248             }
249 
250             const SwCntntNode* pCNd;
251             if( pPrvNd && 1 == nRegionRet )
252             {
253                 pSttPos->nNode.Assign( *pPrvNd );
254                 pSttPos->nContent.Assign( pSttPos->nNode.GetNode().GetCntntNode(), 0 );
255             }
256             else if( pSttPos->nContent.GetIndex() )
257             {
258                 SplitNode( *pSttPos, false );
259             }
260 
261             if( pPrvNd && 2 == nRegionRet )
262             {
263                 pEndPos->nNode.Assign( *pPrvNd );
264                 pEndPos->nContent.Assign( pEndPos->nNode.GetNode().GetCntntNode(), 0 );
265             }
266             else
267             {
268                 pCNd = pEndPos->nNode.GetNode().GetCntntNode();
269                 if( pCNd && pCNd->Len() != pEndPos->nContent.GetIndex() )
270                 {
271                     xub_StrLen nCntnt = pSttPos->nContent.GetIndex();
272                     SplitNode( *pEndPos, false );
273 
274                     SwTxtNode* pTNd;
275                     if( pEndPos->nNode.GetIndex() == pSttPos->nNode.GetIndex() )
276                     {
277                         pSttPos->nNode--;
278                         pEndPos->nNode--;
279                         pTNd = pSttPos->nNode.GetNode().GetTxtNode();
280                         pSttPos->nContent.Assign( pTNd, nCntnt );
281                     }
282                     else
283                     {
284                         // wieder ans Ende vom vorherigen setzen
285                         pEndPos->nNode--;
286                         pTNd = pEndPos->nNode.GetNode().GetTxtNode();
287                     }
288                     if( pTNd ) nCntnt = pTNd->GetTxt().Len(); else nCntnt = 0;
289                     pEndPos->nContent.Assign( pTNd, nCntnt );
290                 }
291             }
292             pNewSectNode = GetNodes().InsertTextSection(
293                 pSttPos->nNode, *pFmt, rNewData, pTOXBase, &pEndPos->nNode);
294         }
295     }
296     else
297     {
298         const SwPosition* pPos = rRange.GetPoint();
299         const SwCntntNode* pCNd = pPos->nNode.GetNode().GetCntntNode();
300         if( !pPos->nContent.GetIndex() )
301         {
302             pNewSectNode = GetNodes().InsertTextSection(
303                 pPos->nNode, *pFmt, rNewData, pTOXBase, 0, true);
304         }
305         else if( pPos->nContent.GetIndex() == pCNd->Len() )
306         {
307             pNewSectNode = GetNodes().InsertTextSection(
308                 pPos->nNode, *pFmt, rNewData, pTOXBase, 0, false);
309         }
310         else
311         {
312             if( pUndoInsSect && pCNd->IsTxtNode() )
313             {
314                 pUndoInsSect->SaveSplitNode( (SwTxtNode*)pCNd, sal_True );
315             }
316             SplitNode( *pPos, false );
317             pNewSectNode = GetNodes().InsertTextSection(
318                 pPos->nNode, *pFmt, rNewData, pTOXBase, 0, true);
319         }
320     }
321 
322 //FEATURE::CONDCOLL
323     pNewSectNode->CheckSectionCondColl();
324 //FEATURE::CONDCOLL
325 
326     SetRedlineMode_intern( eOld );
327 
328     if( IsRedlineOn() || (!IsIgnoreRedline() && pRedlineTbl->Count() ))
329     {
330         SwPaM aPam( *pNewSectNode->EndOfSectionNode(), *pNewSectNode, 1 );
331         if( IsRedlineOn() )
332         {
333             AppendRedline( new SwRedline( nsRedlineType_t::REDLINE_INSERT, aPam ), true);
334         }
335         else
336         {
337             SplitRedline( aPam );
338         }
339     }
340 
341     // ist eine Condition gesetzt
342     if (rNewData.IsHidden() && rNewData.GetCondition().Len())
343     {
344         // dann berechne bis zu dieser Position
345         SwCalc aCalc( *this );
346         if( ! IsInReading() )
347         {
348             FldsToCalc( aCalc, pNewSectNode->GetIndex(), USHRT_MAX );
349         }
350         SwSection& rNewSect = pNewSectNode->GetSection();
351         rNewSect.SetCondHidden( aCalc.Calculate( rNewSect.GetCondition() ).GetBool() );
352     }
353 
354     sal_Bool bUpdateFtn = sal_False;
355     if( GetFtnIdxs().Count() && pAttr )
356     {
357         sal_uInt16 nVal = ((SwFmtFtnAtTxtEnd&)pAttr->Get(
358                                             RES_FTN_AT_TXTEND )).GetValue();
359         if( ( FTNEND_ATTXTEND_OWNNUMSEQ == nVal ||
360               FTNEND_ATTXTEND_OWNNUMANDFMT == nVal ) ||
361             ( FTNEND_ATTXTEND_OWNNUMSEQ == ( nVal = ((SwFmtEndAtTxtEnd&)
362                             pAttr->Get( RES_END_AT_TXTEND )).GetValue() ) ||
363               FTNEND_ATTXTEND_OWNNUMANDFMT == nVal ))
364         {
365             bUpdateFtn = sal_True;
366         }
367     }
368 
369     if( pUndoInsSect )
370     {
371         pUndoInsSect->SetSectNdPos( pNewSectNode->GetIndex() );
372         pUndoInsSect->SetUpdtFtnFlag( bUpdateFtn );
373         GetIDocumentUndoRedo().DoUndo(bUndo);
374     }
375 
376     if (rNewData.IsLinkType())
377     {
378         pNewSectNode->GetSection().CreateLink( bUpdate ? CREATE_UPDATE : CREATE_CONNECT );
379     }
380 
381     if( bUpdateFtn )
382     {
383         GetFtnIdxs().UpdateFtn( SwNodeIndex( *pNewSectNode ));
384     }
385 
386     SetModified();
387     return &pNewSectNode->GetSection();
388 }
389 
390 sal_uInt16 SwDoc::IsInsRegionAvailable( const SwPaM& rRange,
391                                 const SwNode** ppSttNd ) const
392 {
393     sal_uInt16 nRet = 1;
394     if( rRange.HasMark() )
395     {
396         // teste ob es sich um eine gueltige Selektion handelt
397         const SwPosition* pStt = rRange.Start(),
398                         * pEnd = rRange.End();
399 
400         const SwCntntNode* pCNd = pEnd->nNode.GetNode().GetCntntNode();
401         const SwNode* pNd = &pStt->nNode.GetNode();
402         const SwSectionNode* pSectNd = pNd->FindSectionNode();
403         const SwSectionNode* pEndSectNd = pCNd ? pCNd->FindSectionNode() : 0;
404         if( pSectNd && pEndSectNd && pSectNd != pEndSectNd )
405         {
406             // versuche eine umschliessende Section zu erzeugen
407             // Aber, nur wenn der Start am Sectionanfang und das Ende am
408             // Section Ende liegt!
409             nRet = 0;
410             if( !pStt->nContent.GetIndex() && pSectNd->GetIndex()
411                 == pStt->nNode.GetIndex() - 1 && pEnd->nContent.GetIndex() ==
412                 pCNd->Len() )
413             {
414                 SwNodeIndex aIdx( pStt->nNode, -1 );
415                 sal_uLong nCmp = pEnd->nNode.GetIndex();
416                 const SwStartNode* pPrvNd;
417                 const SwEndNode* pNxtNd;
418                 while( 0 != ( pPrvNd = (pNd = &aIdx.GetNode())->GetSectionNode() ) &&
419                     !( aIdx.GetIndex() < nCmp &&
420                         nCmp < pPrvNd->EndOfSectionIndex() ) )
421                 {
422                     aIdx--;
423                 }
424                 if( !pPrvNd )
425                     pPrvNd = pNd->IsStartNode() ? (SwStartNode*)pNd
426                                                 : pNd->StartOfSectionNode();
427 
428                 aIdx = pEnd->nNode.GetIndex() + 1;
429                 nCmp = pStt->nNode.GetIndex();
430                 while( 0 != ( pNxtNd = (pNd = &aIdx.GetNode())->GetEndNode() ) &&
431                     pNxtNd->StartOfSectionNode()->IsSectionNode() &&
432                     !( pNxtNd->StartOfSectionIndex() < nCmp &&
433                         nCmp < aIdx.GetIndex() ) )
434                 {
435                     aIdx++;
436                 }
437                 if( !pNxtNd )
438                     pNxtNd = pNd->EndOfSectionNode();
439 
440                 if( pPrvNd && pNxtNd && pPrvNd == pNxtNd->StartOfSectionNode() )
441                 {
442                     nRet = 3;
443 
444                     if( ppSttNd )
445                         *ppSttNd = pPrvNd;
446                 }
447             }
448         }
449         else if( !pSectNd && pEndSectNd )
450         {
451             // versuche eine umschliessende Section zu erzeugen
452             // Aber, nur wenn das Ende am Section Ende liegt!
453             nRet = 0;
454             if( pEnd->nContent.GetIndex() == pCNd->Len() )
455             {
456                 SwNodeIndex aIdx( pEnd->nNode, 1 );
457                 if( aIdx.GetNode().IsEndNode() &&
458                         0 != aIdx.GetNode().FindSectionNode() )
459                 {
460                     do {
461                         aIdx++;
462                     } while( aIdx.GetNode().IsEndNode() &&
463                                 0 != aIdx.GetNode().FindSectionNode() );
464 //                  if( !aIdx.GetNode().IsEndNode() )
465                     {
466                         nRet = 2;
467                         if( ppSttNd )
468                         {
469                             aIdx--;
470                             *ppSttNd = &aIdx.GetNode();
471                         }
472                     }
473                 }
474             }
475         }
476         else if( pSectNd && !pEndSectNd )
477         {
478             // versuche eine umschliessende Section zu erzeugen
479             // Aber, nur wenn der Start am Section Anfang liegt!
480             nRet = 0;
481             if( !pStt->nContent.GetIndex() )
482             {
483                 SwNodeIndex aIdx( pStt->nNode, -1 );
484                 if( aIdx.GetNode().IsSectionNode() )
485                 {
486                     do {
487                         aIdx--;
488                     } while( aIdx.GetNode().IsSectionNode() );
489                     if( !aIdx.GetNode().IsSectionNode() )
490                     {
491                         nRet = 1;
492                         if( ppSttNd )
493                         {
494                             aIdx++;
495                             *ppSttNd = &aIdx.GetNode();
496                         }
497                     }
498                 }
499             }
500         }
501     }
502     return nRet;
503 }
504 
505 SwSection* SwDoc::GetCurrSection( const SwPosition& rPos ) const
506 {
507     const SwSectionNode* pSectNd = rPos.nNode.GetNode().FindSectionNode();
508     if( pSectNd )
509         return (SwSection*)&pSectNd->GetSection();
510     return 0;
511 }
512 
513 SwSectionFmt* SwDoc::MakeSectionFmt( SwSectionFmt *pDerivedFrom )
514 {
515     if( !pDerivedFrom )
516         pDerivedFrom = (SwSectionFmt*)pDfltFrmFmt;
517     SwSectionFmt* pNew = new SwSectionFmt( pDerivedFrom, this );
518     pSectionFmtTbl->Insert( pNew, pSectionFmtTbl->Count() );
519     return pNew;
520 }
521 
522 void SwDoc::DelSectionFmt( SwSectionFmt *pFmt, sal_Bool bDelNodes )
523 {
524     sal_uInt16 nPos = pSectionFmtTbl->GetPos( pFmt );
525 
526     GetIDocumentUndoRedo().StartUndo(UNDO_DELSECTION, NULL);
527 
528     if( USHRT_MAX != nPos )
529     {
530         const SwNodeIndex* pIdx = pFmt->GetCntnt( sal_False ).GetCntntIdx();
531         const SfxPoolItem* pFtnEndAtTxtEnd;
532         if( SFX_ITEM_SET != pFmt->GetItemState(
533                             RES_FTN_AT_TXTEND, sal_True, &pFtnEndAtTxtEnd ) ||
534             SFX_ITEM_SET != pFmt->GetItemState(
535                             RES_END_AT_TXTEND, sal_True, &pFtnEndAtTxtEnd ))
536             pFtnEndAtTxtEnd = 0;
537 
538         const SwSectionNode* pSectNd;
539 
540         if( GetIDocumentUndoRedo().DoesUndo() )
541         {
542             if( bDelNodes && pIdx && &GetNodes() == &pIdx->GetNodes() &&
543                 0 != (pSectNd = pIdx->GetNode().GetSectionNode() ))
544             {
545                 SwNodeIndex aUpdIdx( *pIdx );
546                 SwPaM aPaM( *pSectNd->EndOfSectionNode(), *pSectNd );
547                 GetIDocumentUndoRedo().AppendUndo( new SwUndoDelete( aPaM ));
548                 if( pFtnEndAtTxtEnd )
549                     GetFtnIdxs().UpdateFtn( aUpdIdx );
550                 SetModified();
551                 //#126178# start/end undo have to be pairs!
552                 GetIDocumentUndoRedo().EndUndo(UNDO_DELSECTION, NULL);
553                 return ;
554             }
555             GetIDocumentUndoRedo().AppendUndo( MakeUndoDelSection( *pFmt ) );
556         }
557         else if( bDelNodes && pIdx && &GetNodes() == &pIdx->GetNodes() &&
558                 0 != (pSectNd = pIdx->GetNode().GetSectionNode() ))
559         {
560             SwNodeIndex aUpdIdx( *pIdx );
561             DeleteSection( (SwNode*)pSectNd );
562             if( pFtnEndAtTxtEnd )
563                 GetFtnIdxs().UpdateFtn( aUpdIdx );
564             SetModified();
565             //#126178# start/end undo have to be pairs!
566             GetIDocumentUndoRedo().EndUndo(UNDO_DELSECTION, NULL);
567             return ;
568         }
569 
570         {
571             SwPtrMsgPoolItem aMsgHint( RES_REMOVE_UNO_OBJECT, pFmt );
572             pFmt->ModifyNotification( &aMsgHint, &aMsgHint );
573         }
574 
575         // A ClearRedo could result in a rekursive call of this function and delete some section
576         // formats => the position iside the SectionFmtTbl could have changed
577         nPos = pSectionFmtTbl->GetPos( pFmt );
578 
579         // ACHTUNG: erst aus dem Array entfernen und dann loeschen.
580         //          Der Section-DTOR versucht selbst noch sein Format
581         //          zu loeschen!
582         pSectionFmtTbl->Remove( nPos );
583 //FEATURE::CONDCOLL
584         sal_uLong nCnt = 0, nSttNd = 0;
585         if( pIdx && &GetNodes() == &pIdx->GetNodes() &&
586             0 != (pSectNd = pIdx->GetNode().GetSectionNode() ))
587         {
588             nSttNd = pSectNd->GetIndex();
589             nCnt = pSectNd->EndOfSectionIndex() - nSttNd - 1;
590         }
591 //FEATURE::CONDCOLL
592 
593         delete pFmt;
594 
595         if( nSttNd && pFtnEndAtTxtEnd )
596         {
597             SwNodeIndex aUpdIdx( GetNodes(), nSttNd );
598             GetFtnIdxs().UpdateFtn( aUpdIdx );
599         }
600 
601 //FEATURE::CONDCOLL
602         SwCntntNode* pCNd;
603         for( ; nCnt--; ++nSttNd )
604             if( 0 != (pCNd = GetNodes()[ nSttNd ]->GetCntntNode() ) &&
605                 RES_CONDTXTFMTCOLL == pCNd->GetFmtColl()->Which() )
606                 pCNd->ChkCondColl();
607 //FEATURE::CONDCOLL
608     }
609 
610     GetIDocumentUndoRedo().EndUndo(UNDO_DELSECTION, NULL);
611 
612     SetModified();
613 }
614 
615 void SwDoc::UpdateSection(sal_uInt16 const nPos, SwSectionData & rNewData,
616         SfxItemSet const*const pAttr, bool const bPreventLinkUpdate)
617 {
618     SwSectionFmt* pFmt = (*pSectionFmtTbl)[ nPos ];
619     SwSection* pSection = pFmt->GetSection();
620     /// OD 04.10.2002 #102894#
621     /// remember hidden condition flag of SwSection before changes
622     bool bOldCondHidden = pSection->IsCondHidden() ? true : false;
623 
624     if (pSection->DataEquals(rNewData))
625     {
626         // die Attribute ueberpruefen
627         sal_Bool bOnlyAttrChg = sal_False;
628         if( pAttr && pAttr->Count() )
629         {
630             SfxItemIter aIter( *pAttr );
631             sal_uInt16 nWhich = aIter.GetCurItem()->Which();
632             while( sal_True )
633             {
634                 if( pFmt->GetFmtAttr( nWhich ) != *aIter.GetCurItem() )
635                 {
636                     bOnlyAttrChg = sal_True;
637                     break;
638                 }
639 
640                 if( aIter.IsAtEnd() )
641                     break;
642                 nWhich = aIter.NextItem()->Which();
643             }
644         }
645 
646         if( bOnlyAttrChg )
647         {
648             if (GetIDocumentUndoRedo().DoesUndo())
649             {
650                 GetIDocumentUndoRedo().AppendUndo(
651                     MakeUndoUpdateSection( *pFmt, true ) );
652             }
653             // #i32968# Inserting columns in the section causes MakeFrmFmt
654             // to put two  objects of type SwUndoFrmFmt on the undo stack.
655             // We don't want them.
656             ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
657             pFmt->SetFmtAttr( *pAttr );
658             SetModified();
659         }
660         return;
661     }
662 
663     // Teste ob eine gesamte Content-Section (Dokument/TabellenBox/Fly)
664     // versteckt werden soll, koennen wir zur Zeit nicht !!!!
665     const SwNodeIndex* pIdx = 0;
666     {
667         if (rNewData.IsHidden())
668         {
669             pIdx = pFmt->GetCntnt().GetCntntIdx();
670             if (pIdx)
671             {
672                 const SwSectionNode* pSectNd =
673                     pIdx->GetNode().GetSectionNode();
674                 if (pSectNd)
675                 {
676                     ::lcl_CheckEmptyLayFrm( GetNodes(), rNewData,
677                                 *pSectNd, *pSectNd->EndOfSectionNode() );
678                 }
679             }
680         }
681     }
682 
683     if (GetIDocumentUndoRedo().DoesUndo())
684     {
685         GetIDocumentUndoRedo().AppendUndo(MakeUndoUpdateSection(*pFmt, false));
686     }
687     // #i32968# Inserting columns in the section causes MakeFrmFmt to put two
688     // objects of type SwUndoFrmFmt on the undo stack. We don't want them.
689     ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
690 
691     // #56167# Der LinkFileName koennte auch nur aus Separatoren bestehen
692     String sCompareString = sfx2::cTokenSeperator;
693     sCompareString += sfx2::cTokenSeperator;
694     const bool bUpdate =
695            (!pSection->IsLinkType() && rNewData.IsLinkType())
696         || (    rNewData.GetLinkFileName().Len()
697             &&  (rNewData.GetLinkFileName() != sCompareString)
698             &&  (rNewData.GetLinkFileName() != pSection->GetLinkFileName()));
699 
700     String sSectName( rNewData.GetSectionName() );
701     if (sSectName != pSection->GetSectionName())
702         GetUniqueSectionName( &sSectName );
703     else
704         sSectName.Erase();
705 
706     /// OD 04.10.2002 #102894# - NOTE
707     /// In SwSection::operator=(..) class member bCondHiddenFlag is always set to sal_True.
708     /// IMHO this have to be changed, but I can't estimate the consequences:
709     /// Either it is set to sal_True using corresponding method <SwSection.SetCondHidden(..)>,
710     /// or it is set to the value of SwSection which is assigned to it.
711     /// Discussion with AMA results that the adjustment to the assignment operator
712     /// could be very risky -> see notes in bug #102894#.
713     pSection->SetSectionData(rNewData);
714 
715     if( pAttr )
716         pSection->GetFmt()->SetFmtAttr( *pAttr );
717 
718     if( sSectName.Len() )
719     {
720         pSection->SetSectionName( sSectName );
721     }
722 
723     // ist eine Condition gesetzt
724     if( pSection->IsHidden() && pSection->GetCondition().Len() )
725     {
726         // dann berechne bis zu dieser Position
727         SwCalc aCalc( *this );
728         if( !pIdx )
729             pIdx = pFmt->GetCntnt().GetCntntIdx();
730         FldsToCalc( aCalc, pIdx->GetIndex(), USHRT_MAX );
731         /// OD 04.10.2002 #102894#
732         /// Because on using SwSection::operator=() to set up <pSection>
733         /// with <rNewData> and the above given note, the hidden condition flag
734         /// has to be set to sal_False, if hidden condition flag of <pFmt->GetSection()>
735         /// (SwSection before the changes) is sal_False (already saved in <bOldCondHidden>)
736         /// and new calculated condition is sal_True.
737         /// This is necessary, because otherwise the <SetCondHidden> would have
738         /// no effect.
739         bool bCalculatedCondHidden =
740                 aCalc.Calculate( pSection->GetCondition() ).GetBool() ? true : false;
741         if ( bCalculatedCondHidden && !bOldCondHidden )
742         {
743             pSection->SetCondHidden( false );
744         }
745         pSection->SetCondHidden( bCalculatedCondHidden );
746     }
747 
748     if( bUpdate )
749         pSection->CreateLink( bPreventLinkUpdate ? CREATE_CONNECT : CREATE_UPDATE );
750     else if( !pSection->IsLinkType() && pSection->IsConnected() )
751     {
752         pSection->Disconnect();
753         GetLinkManager().Remove( &pSection->GetBaseLink() );
754     }
755 
756     SetModified();
757 }
758 
759 /* -----------------19.02.99 09:31-------------------
760  * LockFrms wurde im InsertSection genutzt, um zu verhindern, dass
761  * SectionFrms durch das DelFrms zerstoert werden. Dies ist durch
762  * den Destroy-Listen-Mechanismus ueberfluessig geworden.
763  * Falls diese Methode doch noch einmal reanimiert wird, bietet es
764  * sich vielleicht an, beim Entlocken die SectionFrms auf Inhalt zu
765  * pruefen und dann ggf. zur Zerstoerung anzumelden.
766  * --------------------------------------------------*/
767 
768 // und dann waren da noch die Fussnoten:
769 void lcl_DeleteFtn( SwSectionNode *pNd, sal_uLong nStt, sal_uLong nEnd )
770 {
771     SwFtnIdxs& rFtnArr = pNd->GetDoc()->GetFtnIdxs();
772     if( rFtnArr.Count() )
773     {
774         sal_uInt16 nPos;
775         rFtnArr.SeekEntry( SwNodeIndex( *pNd ), &nPos );
776         SwTxtFtn* pSrch;
777 
778         // loesche erstmal alle, die dahinter stehen
779         while( nPos < rFtnArr.Count() &&
780             _SwTxtFtn_GetIndex( (pSrch = rFtnArr[ nPos ]) ) <= nEnd )
781         {
782             // Werden die Nodes nicht geloescht mussen sie bei den Seiten
783             // abmeldet (Frms loeschen) werden, denn sonst bleiben sie
784             // stehen (Undo loescht sie nicht!)
785             pSrch->DelFrms(0);
786             ++nPos;
787         }
788 
789         while( nPos-- &&
790             _SwTxtFtn_GetIndex( (pSrch = rFtnArr[ nPos ]) ) >= nStt )
791         {
792             // Werden die Nodes nicht geloescht mussen sie bei den Seiten
793             // abmeldet (Frms loeschen) werden, denn sonst bleiben sie
794             // stehen (Undo loescht sie nicht!)
795             pSrch->DelFrms(0);
796         }
797     }
798 }
799 
800 static inline bool lcl_IsTOXSection(SwSectionData const& rSectionData)
801 {
802     return (TOX_CONTENT_SECTION == rSectionData.GetType())
803         || (TOX_HEADER_SECTION  == rSectionData.GetType());
804 }
805 
806 SwSectionNode* SwNodes::InsertTextSection(SwNodeIndex const& rNdIdx,
807                                 SwSectionFmt& rSectionFmt,
808                                 SwSectionData const& rSectionData,
809                                 SwTOXBase const*const pTOXBase,
810                                 SwNodeIndex const*const pEnde,
811                                 bool const bInsAtStart, bool const bCreateFrms)
812 {
813     SwNodeIndex aInsPos( rNdIdx );
814     if( !pEnde )        // kein Bereich also neue Section davor/hinter anlegen
815     {
816         // #i26762#
817         ASSERT(!pEnde || rNdIdx <= *pEnde,
818                "Section start and end in wrong order!");
819 
820         if( bInsAtStart )
821         {
822             if (!lcl_IsTOXSection(rSectionData))
823             {
824                 do {
825                     aInsPos--;
826                 } while( aInsPos.GetNode().IsSectionNode() );
827                 aInsPos++;
828             }
829         }
830         else
831         {
832             SwNode* pNd;
833             aInsPos++;
834             if (!lcl_IsTOXSection(rSectionData))
835             {
836                 while( aInsPos.GetIndex() < Count() - 1 &&
837                         ( pNd = &aInsPos.GetNode())->IsEndNode() &&
838                         pNd->StartOfSectionNode()->IsSectionNode())
839                 {
840                     aInsPos++;
841                 }
842             }
843         }
844     }
845 
846     SwSectionNode *const pSectNd =
847             new SwSectionNode(aInsPos, rSectionFmt, pTOXBase);
848     if( pEnde )
849     {
850         // Sonderfall fuer die Reader/Writer
851         if( &pEnde->GetNode() != &GetEndOfContent() )
852             aInsPos = pEnde->GetIndex()+1;
853         // #i58710: We created a RTF document with a section break inside a table cell
854         // We are not able to handle a section start inside a table and the section end outside.
855         const SwNode* pLastNode = pSectNd->StartOfSectionNode()->EndOfSectionNode();
856         if( aInsPos > pLastNode->GetIndex() )
857             aInsPos = pLastNode->GetIndex();
858         // Another way round: if the section starts outside a table but the end is inside...
859         // aInsPos is at the moment the Position where my EndNode will be inserted
860         const SwStartNode* pStartNode = aInsPos.GetNode().StartOfSectionNode();
861         // This StartNode should be in front of me, but if not, I wanna survive
862         sal_uLong nMyIndex = pSectNd->GetIndex();
863         if( pStartNode->GetIndex() > nMyIndex ) // Suspicious!
864         {
865             const SwNode* pTemp;
866             do
867             {
868                 pTemp = pStartNode; // pTemp is a suspicious one
869                 pStartNode = pStartNode->StartOfSectionNode();
870             }
871             while( pStartNode->GetIndex() > nMyIndex );
872             pTemp = pTemp->EndOfSectionNode();
873             // If it starts behind me but ends behind my end...
874             if( pTemp->GetIndex() >= aInsPos.GetIndex() )
875                 aInsPos = pTemp->GetIndex()+1; // ...I have to correct my end position
876         }
877 
878     }
879     else
880     {
881         SwTxtNode* pCpyTNd = rNdIdx.GetNode().GetTxtNode();
882         if( pCpyTNd )
883         {
884             SwTxtNode* pTNd = new SwTxtNode( aInsPos, pCpyTNd->GetTxtColl() );
885             if( pCpyTNd->HasSwAttrSet() )
886             {
887                 // Task 70955 - move PageDesc/Break to the first Node of the
888                 //              section
889                 const SfxItemSet& rSet = *pCpyTNd->GetpSwAttrSet();
890                 if( SFX_ITEM_SET == rSet.GetItemState( RES_BREAK ) ||
891                     SFX_ITEM_SET == rSet.GetItemState( RES_PAGEDESC ))
892                 {
893                     SfxItemSet aSet( rSet );
894                     if( bInsAtStart )
895                         pCpyTNd->ResetAttr( RES_PAGEDESC, RES_BREAK );
896                     else
897                     {
898                         aSet.ClearItem( RES_PAGEDESC );
899                         aSet.ClearItem( RES_BREAK );
900                     }
901                     pTNd->SetAttr( aSet );
902                 }
903                 else
904                     pTNd->SetAttr( rSet );
905             }
906             // den Frame anlegen nicht vergessen !!
907             pCpyTNd->MakeFrms( *pTNd );
908         }
909         else
910             new SwTxtNode( aInsPos, (SwTxtFmtColl*)GetDoc()->GetDfltTxtFmtColl() );
911     }
912     new SwEndNode( aInsPos, *pSectNd );
913 
914     pSectNd->GetSection().SetSectionData(rSectionData);
915     SwSectionFmt* pSectFmt = pSectNd->GetSection().GetFmt();
916 
917     // Hier bietet sich als Optimierung an, vorhandene Frames nicht zu
918     // zerstoeren und wieder neu anzulegen, sondern nur umzuhaengen.
919     sal_Bool bInsFrm = bCreateFrms && !pSectNd->GetSection().IsHidden() &&
920                    GetDoc()->GetCurrentViewShell(); //swmod 071108//swmod 071225
921     SwNode2Layout *pNode2Layout = NULL;
922     if( bInsFrm )
923     {
924         SwNodeIndex aTmp( *pSectNd );
925         if( !pSectNd->GetNodes().FindPrvNxtFrmNode( aTmp, pSectNd->EndOfSectionNode() ) )
926             // dann sammel mal alle Uppers ein
927             pNode2Layout = new SwNode2Layout( *pSectNd );
928     }
929 
930     // jetzt noch bei allen im Bereich den richtigen StartNode setzen
931     sal_uLong nEnde = pSectNd->EndOfSectionIndex();
932     sal_uLong nStart = pSectNd->GetIndex()+1;
933     sal_uLong nSkipIdx = ULONG_MAX;
934     for( sal_uLong n = nStart; n < nEnde; ++n )
935     {
936         SwNode* pNd = (*this)[n];
937 
938         //JP 30.04.99: Bug 65644 - alle in der NodeSection liegenden
939         //              Sections unter die neue haengen
940         if( ULONG_MAX == nSkipIdx )
941             pNd->pStartOfSection = pSectNd;
942         else if( n >= nSkipIdx )
943             nSkipIdx = ULONG_MAX;
944 
945         if( pNd->IsStartNode() )
946         {
947             // die Verschachtelung der Formate herstellen!
948             if( pNd->IsSectionNode() )
949             {
950                 ((SwSectionNode*)pNd)->GetSection().GetFmt()->
951                                     SetDerivedFrom( pSectFmt );
952                 ((SwSectionNode*)pNd)->DelFrms();
953                 n = pNd->EndOfSectionIndex();
954             }
955             else
956             {
957                 if( pNd->IsTableNode() )
958                     ((SwTableNode*)pNd)->DelFrms();
959 
960                 if( ULONG_MAX == nSkipIdx )
961                     nSkipIdx = pNd->EndOfSectionIndex();
962             }
963         }
964         else if( pNd->IsCntntNode() )
965             ((SwCntntNode*)pNd)->DelFrms();
966     }
967 
968     lcl_DeleteFtn( pSectNd, nStart, nEnde );
969 
970     if( bInsFrm )
971     {
972         if( pNode2Layout )
973         {
974             sal_uLong nIdx = pSectNd->GetIndex();
975             pNode2Layout->RestoreUpperFrms( pSectNd->GetNodes(), nIdx, nIdx + 1 );
976             delete pNode2Layout;
977         }
978         else
979             pSectNd->MakeFrms( &aInsPos );
980     }
981 
982     return pSectNd;
983 }
984 
985 SwSectionNode* SwNode::FindSectionNode()
986 {
987     if( IsSectionNode() )
988         return GetSectionNode();
989     SwStartNode* pTmp = pStartOfSection;
990     while( !pTmp->IsSectionNode() && pTmp->GetIndex() )
991 #if defined( ALPHA ) && defined( UNX )
992         pTmp = ((SwNode*)pTmp)->pStartOfSection;
993 #else
994         pTmp = pTmp->pStartOfSection;
995 #endif
996     return pTmp->GetSectionNode();
997 }
998 
999 
1000 //---------
1001 // SwSectionNode
1002 //---------
1003 
1004 // ugly hack to make m_pSection const
1005 static SwSectionFmt &
1006 lcl_initParent(SwSectionNode & rThis, SwSectionFmt & rFmt)
1007 {
1008     SwSectionNode *const pParent =
1009         rThis.StartOfSectionNode()->FindSectionNode();
1010     if( pParent )
1011     {
1012         // das Format beim richtigen Parent anmelden.
1013         rFmt.SetDerivedFrom( pParent->GetSection().GetFmt() );
1014     }
1015     return rFmt;
1016 }
1017 
1018 SwSectionNode::SwSectionNode(SwNodeIndex const& rIdx,
1019         SwSectionFmt & rFmt, SwTOXBase const*const pTOXBase)
1020     : SwStartNode( rIdx, ND_SECTIONNODE )
1021     , m_pSection( (pTOXBase)
1022         ? new SwTOXBaseSection(*pTOXBase, lcl_initParent(*this, rFmt))
1023         : new SwSection( CONTENT_SECTION, rFmt.GetName(),
1024                 lcl_initParent(*this, rFmt) ) )
1025 {
1026     // jetzt noch die Verbindung von Format zum Node setzen
1027     // Modify unterdruecken, interresiert keinen
1028     rFmt.LockModify();
1029     rFmt.SetFmtAttr( SwFmtCntnt( this ) );
1030     rFmt.UnlockModify();
1031 }
1032 
1033 #if OSL_DEBUG_LEVEL > 1
1034 //Hier werden ueberfluessige SectionFrms entfernt
1035 SwFrm* SwClearDummies( SwFrm* pFrm )
1036 {
1037     SwFrm* pTmp = pFrm;
1038     while( pTmp )
1039     {
1040         ASSERT( !pTmp->GetUpper(), "SwClearDummies: No Upper allowed!" );
1041         if( pTmp->IsSctFrm() )
1042         {
1043             SwSectionFrm* pSectFrm = (SwSectionFrm*)pFrm;
1044             pTmp = pTmp->GetNext();
1045             if( !pSectFrm->GetLower() )
1046             {
1047                 if( pSectFrm->GetPrev() )
1048                     pSectFrm->GetPrev()->pNext = pTmp;
1049                 else
1050                     pFrm = pTmp;
1051                 if( pTmp )
1052                     pTmp->pPrev = pSectFrm->GetPrev();
1053                 delete pSectFrm;
1054             }
1055         }
1056         else
1057             pTmp = pTmp->GetNext();
1058     }
1059     return pFrm;
1060 }
1061 #endif
1062 
1063 SwSectionNode::~SwSectionNode()
1064 {
1065     // mba: test if iteration works as clients will be removed in callback
1066     // --> OD #i117863#
1067     // use hint which allows to specify, if the content shall be saved or not
1068     m_pSection->GetFmt()->CallSwClientNotify( SwSectionFrmMoveAndDeleteHint( sal_True ) );
1069     // <--
1070     SwSectionFmt* pFmt = m_pSection->GetFmt();
1071     if( pFmt )
1072     {
1073         // das Attribut entfernen, weil die Section ihr Format loescht
1074         // und falls das Cntnt-Attribut vorhanden ist, die Section aufhebt.
1075         pFmt->LockModify();
1076         pFmt->ResetFmtAttr( RES_CNTNT );
1077         pFmt->UnlockModify();
1078     }
1079 }
1080 
1081 
1082 SwFrm *SwSectionNode::MakeFrm( SwFrm *pSib )
1083 {
1084     m_pSection->m_Data.SetHiddenFlag(false);
1085     return new SwSectionFrm( *m_pSection, pSib );
1086 }
1087 
1088 //Methode erzeugt fuer den vorhergehenden Node alle Ansichten vom
1089 //Dokument. Die erzeugten Contentframes werden in das entsprechende
1090 //Layout gehaengt.
1091 void SwSectionNode::MakeFrms(const SwNodeIndex & rIdx )
1092 {
1093     // also nehme meinen nachfolgenden oder vorhergehenden ContentFrame:
1094     SwNodes& rNds = GetNodes();
1095     if( rNds.IsDocNodes() && rNds.GetDoc()->GetCurrentViewShell() ) //swmod 071108//swmod 071225
1096     {
1097         if( GetSection().IsHidden() || IsCntntHidden() )
1098         {
1099             SwNodeIndex aIdx( *EndOfSectionNode() );
1100             SwCntntNode* pCNd = rNds.GoNextSection( &aIdx, sal_True, sal_False );
1101             if( !pCNd )
1102             {
1103                 aIdx = *this;
1104                 if( 0 == ( pCNd = rNds.GoPrevSection( &aIdx, sal_True, sal_False )) )
1105                     return ;
1106             }
1107             pCNd = aIdx.GetNode().GetCntntNode();
1108             pCNd->MakeFrms( (SwCntntNode&)rIdx.GetNode() );
1109         }
1110         else
1111         {
1112             SwNode2Layout aNode2Layout( *this, rIdx.GetIndex() );
1113             SwFrm *pFrm, *pNew;
1114             while( 0 != (pFrm = aNode2Layout.NextFrm()) )
1115             {
1116                 ASSERT( pFrm->IsSctFrm(), "Depend von Section keine Section." );
1117                 pNew = rIdx.GetNode().GetCntntNode()->MakeFrm( pFrm );
1118 
1119                 SwSectionNode* pS = rIdx.GetNode().FindSectionNode();
1120                 // --> OD 2008-06-23 #156927#
1121                 // Assure that node is not inside a table, which is inside the
1122                 // found section.
1123                 if ( pS )
1124                 {
1125                     SwTableNode* pTableNode = rIdx.GetNode().FindTableNode();
1126                     if ( pTableNode &&
1127                          pTableNode->GetIndex() > pS->GetIndex() )
1128                     {
1129                         pS = 0;
1130                     }
1131                 }
1132                 // <--
1133                 // if the node is in a section, the sectionframe now
1134                 // has to be created..
1135                 // OD 14.11.2002 #104684# - boolean to control <Init()> of a new
1136                 // section frame.
1137                 bool bInitNewSect = false;
1138                 if( pS )
1139                 {
1140                     SwSectionFrm *pSct = new SwSectionFrm( pS->GetSection(), pFrm );
1141                     // OD 14.11.2002 #104684# - prepare <Init()> of new section frame.
1142                     bInitNewSect = true;
1143                     SwLayoutFrm* pUp = pSct;
1144                     while( pUp->Lower() )  // for columned sections
1145                     {
1146                         ASSERT( pUp->Lower()->IsLayoutFrm(),"Who's in there?" );
1147                         pUp = (SwLayoutFrm*)pUp->Lower();
1148                     }
1149                     pNew->Paste( pUp, NULL );
1150                     // --> OD 2005-12-01 #i27138#
1151                     // notify accessibility paragraphs objects about changed
1152                     // CONTENT_FLOWS_FROM/_TO relation.
1153                     // Relation CONTENT_FLOWS_FROM for next paragraph will change
1154                     // and relation CONTENT_FLOWS_TO for previous paragraph will change.
1155                     if ( pNew->IsTxtFrm() )
1156                     {
1157                         ViewShell* pViewShell( pNew->getRootFrm()->GetCurrShell() );
1158                         if ( pViewShell && pViewShell->GetLayout() &&
1159                              pViewShell->GetLayout()->IsAnyShellAccessible() )
1160                         {
1161                             pViewShell->InvalidateAccessibleParaFlowRelation(
1162                                 dynamic_cast<SwTxtFrm*>(pNew->FindNextCnt( true )),
1163                                 dynamic_cast<SwTxtFrm*>(pNew->FindPrevCnt( true )) );
1164                         }
1165                     }
1166                     // <--
1167                     pNew = pSct;
1168                 }
1169 
1170                 // wird ein Node vorher oder nachher mit Frames versehen
1171                 if ( rIdx < GetIndex() )
1172                     // der neue liegt vor mir
1173                     pNew->Paste( pFrm->GetUpper(), pFrm );
1174                 else
1175                     // der neue liegt hinter mir
1176                     pNew->Paste( pFrm->GetUpper(), pFrm->GetNext() );
1177                 // --> OD 2005-12-01 #i27138#
1178                 // notify accessibility paragraphs objects about changed
1179                 // CONTENT_FLOWS_FROM/_TO relation.
1180                 // Relation CONTENT_FLOWS_FROM for next paragraph will change
1181                 // and relation CONTENT_FLOWS_TO for previous paragraph will change.
1182                 if ( pNew->IsTxtFrm() )
1183                 {
1184                     ViewShell* pViewShell( pNew->getRootFrm()->GetCurrShell() );
1185                     if ( pViewShell && pViewShell->GetLayout() &&
1186                          pViewShell->GetLayout()->IsAnyShellAccessible() )
1187                     {
1188                         pViewShell->InvalidateAccessibleParaFlowRelation(
1189                             dynamic_cast<SwTxtFrm*>(pNew->FindNextCnt( true )),
1190                             dynamic_cast<SwTxtFrm*>(pNew->FindPrevCnt( true )) );
1191                     }
1192                 }
1193                 // <--
1194                 if ( bInitNewSect )
1195                     static_cast<SwSectionFrm*>(pNew)->Init();
1196             }
1197         }
1198     }
1199 }
1200 
1201 //Fuer jedes vorkommen im Layout einen SectionFrm anlegen und vor den
1202 //entsprechenden CntntFrm pasten.
1203 
1204 void SwSectionNode::MakeFrms( SwNodeIndex* pIdxBehind, SwNodeIndex* pEndIdx )
1205 {
1206     ASSERT( pIdxBehind, "kein Index" );
1207     SwNodes& rNds = GetNodes();
1208     SwDoc* pDoc = rNds.GetDoc();
1209 
1210     *pIdxBehind = *this;
1211 
1212     m_pSection->m_Data.SetHiddenFlag(true);
1213 
1214     if( rNds.IsDocNodes() )
1215     {
1216         SwNodeIndex *pEnd = pEndIdx ? pEndIdx :
1217                             new SwNodeIndex( *EndOfSectionNode(), 1 );
1218         ::MakeFrms( pDoc, *pIdxBehind, *pEnd );
1219         if( !pEndIdx )
1220             delete pEnd;
1221     }
1222 
1223 }
1224 
1225 void SwSectionNode::DelFrms()
1226 {
1227     sal_uLong nStt = GetIndex()+1, nEnd = EndOfSectionIndex();
1228     if( nStt >= nEnd )
1229     {
1230         // unser Flag muessen wir noch aktualisieren
1231         // pSection->bHiddenFlag = sal_True;
1232         return ;
1233     }
1234 
1235     SwNodes& rNds = GetNodes();
1236     m_pSection->GetFmt()->DelFrms();
1237 
1238     // unser Flag muessen wir noch aktualisieren
1239     m_pSection->m_Data.SetHiddenFlag(true);
1240 
1241     // Bug 30582: falls der Bereich in Fly oder TabellenBox ist, dann
1242     //              kann er nur "gehiddet" werden, wenn weiterer Content
1243     //              vorhanden ist, der "Frames" haelt. Sonst hat der
1244     //              Fly/TblBox-Frame keinen Lower !!!
1245     {
1246         SwNodeIndex aIdx( *this );
1247         if( !rNds.GoPrevSection( &aIdx, sal_True, sal_False ) ||
1248             !CheckNodesRange( *this, aIdx, sal_True ) ||
1249             // OD 04.11.2003 #i21457#
1250             !lcl_IsInSameTblBox( rNds, *this, true ))
1251         {
1252             aIdx = *EndOfSectionNode();
1253             if( !rNds.GoNextSection( &aIdx, sal_True, sal_False ) ||
1254                 !CheckNodesRange( *EndOfSectionNode(), aIdx, sal_True ) ||
1255                 // OD 04.11.2003 #i21457#
1256                 !lcl_IsInSameTblBox( rNds, *EndOfSectionNode(), false ))
1257             {
1258                 m_pSection->m_Data.SetHiddenFlag(false);
1259             }
1260         }
1261     }
1262 }
1263 
1264 SwSectionNode* SwSectionNode::MakeCopy( SwDoc* pDoc, const SwNodeIndex& rIdx ) const
1265 {
1266     // in welchen Array steht ich denn: Nodes, UndoNodes ??
1267     const SwNodes& rNds = GetNodes();
1268 
1269     // das SectionFrmFmt kopieren
1270     SwSectionFmt* pSectFmt = pDoc->MakeSectionFmt( 0 );
1271     pSectFmt->CopyAttrs( *GetSection().GetFmt() );
1272 
1273     ::std::auto_ptr<SwTOXBase> pTOXBase;
1274     if (TOX_CONTENT_SECTION == GetSection().GetType())
1275     {
1276         ASSERT( GetSection().ISA( SwTOXBaseSection ), "no TOXBaseSection!" );
1277         SwTOXBaseSection const& rTBS(
1278             dynamic_cast<SwTOXBaseSection const&>(GetSection()));
1279         pTOXBase.reset( new SwTOXBase(rTBS, pDoc) );
1280     }
1281 
1282     SwSectionNode *const pSectNd =
1283         new SwSectionNode(rIdx, *pSectFmt, pTOXBase.get());
1284     SwEndNode* pEndNd = new SwEndNode( rIdx, *pSectNd );
1285     SwNodeIndex aInsPos( *pEndNd );
1286 
1287     // Werte uebertragen
1288     SwSection *const pNewSect = pSectNd->m_pSection.get();
1289 
1290     if (TOX_CONTENT_SECTION != GetSection().GetType())
1291     {
1292         // beim Move den Namen beibehalten
1293         if( rNds.GetDoc() == pDoc && pDoc->IsCopyIsMove() )
1294         {
1295             pNewSect->SetSectionName( GetSection().GetSectionName() );
1296         }
1297         else
1298         {
1299             pNewSect->SetSectionName(
1300                 pDoc->GetUniqueSectionName( &GetSection().GetSectionName() ));
1301         }
1302     }
1303 
1304 
1305     pNewSect->SetType( GetSection().GetType() );
1306     pNewSect->SetCondition( GetSection().GetCondition() );
1307     pNewSect->SetLinkFileName( GetSection().GetLinkFileName() );
1308     if( !pNewSect->IsHiddenFlag() && GetSection().IsHidden() )
1309         pNewSect->SetHidden( sal_True );
1310     if( !pNewSect->IsProtectFlag() && GetSection().IsProtect() )
1311         pNewSect->SetProtect( sal_True );
1312     // --> FME 2004-06-22 #114856# edit in readonly sections
1313     if( !pNewSect->IsEditInReadonlyFlag() && GetSection().IsEditInReadonly() )
1314         pNewSect->SetEditInReadonly( sal_True );
1315     // <--
1316 
1317     SwNodeRange aRg( *this, +1, *EndOfSectionNode() );  // (wo stehe in denn nun ??)
1318     rNds._Copy( aRg, aInsPos, sal_False );
1319 
1320     // loesche alle Frames vom kopierten Bereich, diese werden beim
1321     // erzeugen des SectionFrames angelegt !
1322     pSectNd->DelFrms();
1323 
1324     // dann kopiere auch noch die Links/Server
1325     if( pNewSect->IsLinkType() )        // den Link eintragen
1326         pNewSect->CreateLink( pDoc->GetCurrentViewShell() ? CREATE_CONNECT  //swmod 071108//swmod 071225
1327                                                  : CREATE_NONE );
1328 
1329     // falls als Server aus dem Undo kopiert wird, wieder eintragen
1330     if (m_pSection->IsServer()
1331         && pDoc->GetIDocumentUndoRedo().IsUndoNodes(rNds))
1332     {
1333         pNewSect->SetRefObject( m_pSection->GetObject() );
1334         pDoc->GetLinkManager().InsertServer( pNewSect->GetObject() );
1335     }
1336 
1337     // METADATA: copy xml:id; must be done after insertion of node
1338     pSectFmt->RegisterAsCopyOf(*GetSection().GetFmt());
1339 
1340     return pSectNd;
1341 }
1342 
1343 sal_Bool SwSectionNode::IsCntntHidden() const
1344 {
1345     ASSERT( !m_pSection->IsHidden(),
1346             "That's simple: Hidden Section => Hidden Content" );
1347     SwNodeIndex aTmp( *this, 1 );
1348     sal_uLong nEnd = EndOfSectionIndex();
1349     while( aTmp < nEnd )
1350     {
1351         if( aTmp.GetNode().IsSectionNode() )
1352         {
1353             const SwSection& rSect = ((SwSectionNode&)aTmp.GetNode()).GetSection();
1354             if( rSect.IsHiddenFlag() )
1355                 // dann diese Section ueberspringen
1356                 aTmp = *aTmp.GetNode().EndOfSectionNode();
1357         }
1358         else
1359         {
1360             if( aTmp.GetNode().IsCntntNode() || aTmp.GetNode().IsTableNode() )
1361                 return sal_False; // Nicht versteckter Inhalt wurde gefunden
1362             ASSERT( aTmp.GetNode().IsEndNode(), "EndNode expected" );
1363         }
1364         aTmp++;
1365     }
1366     return sal_True; // Alles versteckt
1367 }
1368 
1369 
1370 void SwSectionNode::NodesArrChgd()
1371 {
1372     SwSectionFmt *const pFmt = m_pSection->GetFmt();
1373     if( pFmt )
1374     {
1375         SwNodes& rNds = GetNodes();
1376         SwDoc* pDoc = pFmt->GetDoc();
1377 
1378         if( !rNds.IsDocNodes() )
1379         {
1380             SwPtrMsgPoolItem aMsgHint( RES_REMOVE_UNO_OBJECT, pFmt );
1381             pFmt->ModifyNotification( &aMsgHint, &aMsgHint );
1382         }
1383 
1384         pFmt->LockModify();
1385         pFmt->SetFmtAttr( SwFmtCntnt( this ));
1386         pFmt->UnlockModify();
1387 
1388         SwSectionNode* pSectNd = StartOfSectionNode()->FindSectionNode();
1389         // set the correct parent from the new section
1390         pFmt->SetDerivedFrom( pSectNd ? pSectNd->GetSection().GetFmt()
1391                                       : pDoc->GetDfltFrmFmt() );
1392 
1393         // jetzt noch bei allen im Bereich den richtigen StartNode setzen
1394         sal_uLong nStart = GetIndex()+1, nEnde = EndOfSectionIndex();
1395         for( sal_uLong n = nStart; n < nEnde; ++n )
1396             // die Verschachtelung der Formate herstellen!
1397             if( 0 != ( pSectNd = rNds[ n ]->GetSectionNode() ) )
1398             {
1399                 pSectNd->GetSection().GetFmt()->SetDerivedFrom( pFmt );
1400                 n = pSectNd->EndOfSectionIndex();
1401             }
1402 
1403         // verschieben vom Nodes- ins UndoNodes-Array?
1404         if( rNds.IsDocNodes() )
1405         {
1406             ASSERT( pDoc == GetDoc(),
1407                     "verschieben in unterschiedliche Documente?" );
1408             if( m_pSection->IsLinkType() )      // den Link austragen
1409                 m_pSection->CreateLink( pDoc->GetCurrentViewShell() ? CREATE_CONNECT    //swmod 071108
1410                                                          : CREATE_NONE );//swmod 071225
1411             if (m_pSection->IsServer())
1412             {
1413                 pDoc->GetLinkManager().InsertServer( m_pSection->GetObject() );
1414             }
1415         }
1416         else
1417         {
1418             if (CONTENT_SECTION != m_pSection->GetType())
1419             {
1420                 pDoc->GetLinkManager().Remove( &m_pSection->GetBaseLink() );
1421             }
1422 
1423             if (m_pSection->IsServer())
1424             {
1425                 pDoc->GetLinkManager().RemoveServer( m_pSection->GetObject() );
1426             }
1427         }
1428     }
1429 }
1430 
1431 
1432 String SwDoc::GetUniqueSectionName( const String* pChkStr ) const
1433 {
1434     ResId aId( STR_REGION_DEFNAME, *pSwResMgr );
1435     String aName( aId );
1436     xub_StrLen nNmLen = aName.Len();
1437 
1438     sal_uInt16 nNum = 0;
1439     sal_uInt16 nTmp, nFlagSize = ( pSectionFmtTbl->Count() / 8 ) +2;
1440     sal_uInt8* pSetFlags = new sal_uInt8[ nFlagSize ];
1441     memset( pSetFlags, 0, nFlagSize );
1442 
1443     const SwSectionNode* pSectNd;
1444     sal_uInt16 n;
1445 
1446     for( n = 0; n < pSectionFmtTbl->Count(); ++n )
1447         if( 0 != ( pSectNd = (*pSectionFmtTbl)[ n ]->GetSectionNode( sal_False ) ))
1448         {
1449             const String& rNm = pSectNd->GetSection().GetSectionName();
1450             if( rNm.Match( aName ) == nNmLen )
1451             {
1452                 // Nummer bestimmen und das Flag setzen
1453                 nNum = static_cast<sal_uInt16>(rNm.Copy( nNmLen ).ToInt32());
1454                 if( nNum-- && nNum < pSectionFmtTbl->Count() )
1455                     pSetFlags[ nNum / 8 ] |= (0x01 << ( nNum & 0x07 ));
1456             }
1457             if( pChkStr && pChkStr->Equals( rNm ) )
1458                 pChkStr = 0;
1459         }
1460 
1461     if( !pChkStr )
1462     {
1463         // alle Nummern entsprechend geflag, also bestimme die richtige Nummer
1464         nNum = pSectionFmtTbl->Count();
1465         for( n = 0; n < nFlagSize; ++n )
1466             if( 0xff != ( nTmp = pSetFlags[ n ] ))
1467             {
1468                 // also die Nummer bestimmen
1469                 nNum = n * 8;
1470                 while( nTmp & 1 )
1471                     ++nNum, nTmp >>= 1;
1472                 break;
1473             }
1474 
1475     }
1476     delete [] pSetFlags;
1477     if( pChkStr )
1478         return *pChkStr;
1479     return aName += String::CreateFromInt32( ++nNum );
1480 }
1481 
1482 
1483