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