xref: /aoo41x/main/sw/source/core/docnode/ndsect.cxx (revision 015b7d31)
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.
lcl_IsInSameTblBox(SwNodes & _rNds,const SwNode & _rNd,const bool _bPrev)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 
lcl_CheckEmptyLayFrm(SwNodes & rNds,SwSectionData & rSectionData,const SwNode & rStt,const SwNode & rEnd)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 *
InsertSwSection(SwPaM const & rRange,SwSectionData & rNewData,SwTOXBase const * const pTOXBase,SfxItemSet const * const pAttr,bool const bUpdate)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 
IsInsRegionAvailable(const SwPaM & rRange,const SwNode ** ppSttNd) const390 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 			// try to create a section enclosing it if the end is at the section end
452 			nRet = 0;
453 			if( pEnd->nContent.GetIndex() == pCNd->Len() )
454 			{
455 				SwNodeIndex aIdx( pEnd->nNode, 1 );
456 				if( aIdx.GetNode().IsEndNode() &&
457 						0 != aIdx.GetNode().FindSectionNode() )
458 				{
459 					do {
460 						aIdx++;
461 					} while( aIdx.GetNode().IsEndNode() &&
462 								0 != aIdx.GetNode().FindSectionNode() );
463 //					if( !aIdx.GetNode().IsEndNode() )
464 					{
465 						nRet = 2;
466 						if( ppSttNd )
467 						{
468 							aIdx--;
469 							*ppSttNd = &aIdx.GetNode();
470 						}
471 					}
472 				}
473 			}
474 		}
475 		else if( pSectNd && !pEndSectNd )
476 		{
477 			// try to create a section enclosing it if the start is at the section start
478 			nRet = 0;
479 			if( !pStt->nContent.GetIndex() )
480 			{
481 				SwNodeIndex aIdx( pStt->nNode, -1 );
482 				if( aIdx.GetNode().IsSectionNode() )
483 				{
484 					do {
485 						aIdx--;
486 					} while( aIdx.GetNode().IsSectionNode() );
487 					if( !aIdx.GetNode().IsSectionNode() )
488 					{
489 						nRet = 1;
490 						if( ppSttNd )
491 						{
492 							aIdx++;
493 							*ppSttNd = &aIdx.GetNode();
494 						}
495 					}
496 				}
497 			}
498 		}
499 	}
500 	return nRet;
501 }
502 
GetCurrSection(const SwPosition & rPos) const503 SwSection* SwDoc::GetCurrSection( const SwPosition& rPos ) const
504 {
505 	const SwSectionNode* pSectNd = rPos.nNode.GetNode().FindSectionNode();
506 	if( pSectNd )
507 		return (SwSection*)&pSectNd->GetSection();
508 	return 0;
509 }
510 
MakeSectionFmt(SwSectionFmt * pDerivedFrom)511 SwSectionFmt* SwDoc::MakeSectionFmt( SwSectionFmt *pDerivedFrom )
512 {
513 	if( !pDerivedFrom )
514 		pDerivedFrom = (SwSectionFmt*)pDfltFrmFmt;
515 	SwSectionFmt* pNew = new SwSectionFmt( pDerivedFrom, this );
516 	pSectionFmtTbl->Insert( pNew, pSectionFmtTbl->Count() );
517 	return pNew;
518 }
519 
DelSectionFmt(SwSectionFmt * pFmt,sal_Bool bDelNodes)520 void SwDoc::DelSectionFmt( SwSectionFmt *pFmt, sal_Bool bDelNodes )
521 {
522 	sal_uInt16 nPos = pSectionFmtTbl->GetPos( pFmt );
523 
524     GetIDocumentUndoRedo().StartUndo(UNDO_DELSECTION, NULL);
525 
526 	if( USHRT_MAX != nPos )
527 	{
528 		const SwNodeIndex* pIdx = pFmt->GetCntnt( sal_False ).GetCntntIdx();
529 		const SfxPoolItem* pFtnEndAtTxtEnd;
530 		if( SFX_ITEM_SET != pFmt->GetItemState(
531 							RES_FTN_AT_TXTEND, sal_True, &pFtnEndAtTxtEnd ) ||
532 			SFX_ITEM_SET != pFmt->GetItemState(
533 							RES_END_AT_TXTEND, sal_True, &pFtnEndAtTxtEnd ))
534 			pFtnEndAtTxtEnd = 0;
535 
536 		const SwSectionNode* pSectNd;
537 
538         if( GetIDocumentUndoRedo().DoesUndo() )
539         {
540 			if( bDelNodes && pIdx && &GetNodes() == &pIdx->GetNodes() &&
541 				0 != (pSectNd = pIdx->GetNode().GetSectionNode() ))
542 			{
543 				SwNodeIndex aUpdIdx( *pIdx );
544 				SwPaM aPaM( *pSectNd->EndOfSectionNode(), *pSectNd );
545                 GetIDocumentUndoRedo().AppendUndo( new SwUndoDelete( aPaM ));
546 				if( pFtnEndAtTxtEnd )
547 					GetFtnIdxs().UpdateFtn( aUpdIdx );
548 				SetModified();
549                 //#126178# start/end undo have to be pairs!
550                 GetIDocumentUndoRedo().EndUndo(UNDO_DELSECTION, NULL);
551                 return ;
552 			}
553             GetIDocumentUndoRedo().AppendUndo( MakeUndoDelSection( *pFmt ) );
554 		}
555 		else if( bDelNodes && pIdx && &GetNodes() == &pIdx->GetNodes() &&
556 				0 != (pSectNd = pIdx->GetNode().GetSectionNode() ))
557 		{
558 			SwNodeIndex aUpdIdx( *pIdx );
559 			DeleteSection( (SwNode*)pSectNd );
560 			if( pFtnEndAtTxtEnd )
561 				GetFtnIdxs().UpdateFtn( aUpdIdx );
562 			SetModified();
563             //#126178# start/end undo have to be pairs!
564             GetIDocumentUndoRedo().EndUndo(UNDO_DELSECTION, NULL);
565             return ;
566 		}
567 
568 		{
569 			SwPtrMsgPoolItem aMsgHint( RES_REMOVE_UNO_OBJECT, pFmt );
570 			pFmt->ModifyNotification( &aMsgHint, &aMsgHint );
571 		}
572 
573         // A ClearRedo could result in a rekursive call of this function and delete some section
574         // formats => the position iside the SectionFmtTbl could have changed
575         nPos = pSectionFmtTbl->GetPos( pFmt );
576 
577 		// ACHTUNG: erst aus dem Array entfernen und dann loeschen.
578 		//			Der Section-DTOR versucht selbst noch sein Format
579 		//			zu loeschen!
580 		pSectionFmtTbl->Remove( nPos );
581 //FEATURE::CONDCOLL
582 		sal_uLong nCnt = 0, nSttNd = 0;
583 		if( pIdx && &GetNodes() == &pIdx->GetNodes() &&
584 			0 != (pSectNd = pIdx->GetNode().GetSectionNode() ))
585 		{
586 			nSttNd = pSectNd->GetIndex();
587 			nCnt = pSectNd->EndOfSectionIndex() - nSttNd - 1;
588 		}
589 //FEATURE::CONDCOLL
590 
591 		delete pFmt;
592 
593 		if( nSttNd && pFtnEndAtTxtEnd )
594 		{
595 			SwNodeIndex aUpdIdx( GetNodes(), nSttNd );
596 			GetFtnIdxs().UpdateFtn( aUpdIdx );
597 		}
598 
599 //FEATURE::CONDCOLL
600 		SwCntntNode* pCNd;
601 		for( ; nCnt--; ++nSttNd )
602 			if( 0 != (pCNd = GetNodes()[ nSttNd ]->GetCntntNode() ) &&
603 				RES_CONDTXTFMTCOLL == pCNd->GetFmtColl()->Which() )
604 				pCNd->ChkCondColl();
605 //FEATURE::CONDCOLL
606 	}
607 
608     GetIDocumentUndoRedo().EndUndo(UNDO_DELSECTION, NULL);
609 
610 	SetModified();
611 }
612 
UpdateSection(sal_uInt16 const nPos,SwSectionData & rNewData,SfxItemSet const * const pAttr,bool const bPreventLinkUpdate)613 void SwDoc::UpdateSection(sal_uInt16 const nPos, SwSectionData & rNewData,
614         SfxItemSet const*const pAttr, bool const bPreventLinkUpdate)
615 {
616 	SwSectionFmt* pFmt = (*pSectionFmtTbl)[ nPos ];
617 	SwSection* pSection = pFmt->GetSection();
618     /// OD 04.10.2002 #102894#
619     /// remember hidden condition flag of SwSection before changes
620     bool bOldCondHidden = pSection->IsCondHidden() ? true : false;
621 
622     if (pSection->DataEquals(rNewData))
623 	{
624 		// die Attribute ueberpruefen
625 		sal_Bool bOnlyAttrChg = sal_False;
626 		if( pAttr && pAttr->Count() )
627 		{
628 			SfxItemIter aIter( *pAttr );
629 			sal_uInt16 nWhich = aIter.GetCurItem()->Which();
630 			while( sal_True )
631 			{
632                 if( pFmt->GetFmtAttr( nWhich ) != *aIter.GetCurItem() )
633 				{
634 					bOnlyAttrChg = sal_True;
635 					break;
636 				}
637 
638 				if( aIter.IsAtEnd() )
639 					break;
640 				nWhich = aIter.NextItem()->Which();
641 			}
642 		}
643 
644 		if( bOnlyAttrChg )
645 		{
646             if (GetIDocumentUndoRedo().DoesUndo())
647             {
648                 GetIDocumentUndoRedo().AppendUndo(
649                     MakeUndoUpdateSection( *pFmt, true ) );
650             }
651             // #i32968# Inserting columns in the section causes MakeFrmFmt
652             // to put two  objects of type SwUndoFrmFmt on the undo stack.
653             // We don't want them.
654             ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
655             pFmt->SetFmtAttr( *pAttr );
656 			SetModified();
657         }
658 		return;
659 	}
660 
661 	// Teste ob eine gesamte Content-Section (Dokument/TabellenBox/Fly)
662 	// versteckt werden soll, koennen wir zur Zeit nicht !!!!
663 	const SwNodeIndex* pIdx = 0;
664 	{
665         if (rNewData.IsHidden())
666 		{
667             pIdx = pFmt->GetCntnt().GetCntntIdx();
668             if (pIdx)
669             {
670                 const SwSectionNode* pSectNd =
671                     pIdx->GetNode().GetSectionNode();
672                 if (pSectNd)
673                 {
674                     ::lcl_CheckEmptyLayFrm( GetNodes(), rNewData,
675 								*pSectNd, *pSectNd->EndOfSectionNode() );
676                 }
677             }
678 		}
679 	}
680 
681     if (GetIDocumentUndoRedo().DoesUndo())
682     {
683         GetIDocumentUndoRedo().AppendUndo(MakeUndoUpdateSection(*pFmt, false));
684     }
685     // #i32968# Inserting columns in the section causes MakeFrmFmt to put two
686     // objects of type SwUndoFrmFmt on the undo stack. We don't want them.
687     ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
688 
689 	// #56167# Der LinkFileName koennte auch nur aus Separatoren bestehen
690     String sCompareString = sfx2::cTokenSeperator;
691     sCompareString += sfx2::cTokenSeperator;
692     const bool bUpdate =
693            (!pSection->IsLinkType() && rNewData.IsLinkType())
694         || (    rNewData.GetLinkFileName().Len()
695             &&  (rNewData.GetLinkFileName() != sCompareString)
696             &&  (rNewData.GetLinkFileName() != pSection->GetLinkFileName()));
697 
698     String sSectName( rNewData.GetSectionName() );
699     if (sSectName != pSection->GetSectionName())
700 		GetUniqueSectionName( &sSectName );
701 	else
702 		sSectName.Erase();
703 
704     /// OD 04.10.2002 #102894# - NOTE
705     /// In SwSection::operator=(..) class member bCondHiddenFlag is always set to sal_True.
706     /// IMHO this have to be changed, but I can't estimate the consequences:
707     /// Either it is set to sal_True using corresponding method <SwSection.SetCondHidden(..)>,
708     /// or it is set to the value of SwSection which is assigned to it.
709     /// Discussion with AMA results that the adjustment to the assignment operator
710     /// could be very risky -> see notes in bug #102894#.
711     pSection->SetSectionData(rNewData);
712 
713 	if( pAttr )
714         pSection->GetFmt()->SetFmtAttr( *pAttr );
715 
716 	if( sSectName.Len() )
717     {
718         pSection->SetSectionName( sSectName );
719     }
720 
721 	// ist eine Condition gesetzt
722 	if( pSection->IsHidden() && pSection->GetCondition().Len() )
723 	{
724         // dann berechne bis zu dieser Position
725 		SwCalc aCalc( *this );
726 		if( !pIdx )
727 			pIdx = pFmt->GetCntnt().GetCntntIdx();
728 		FldsToCalc( aCalc, pIdx->GetIndex(), USHRT_MAX );
729         /// OD 04.10.2002 #102894#
730         /// Because on using SwSection::operator=() to set up <pSection>
731         /// with <rNewData> and the above given note, the hidden condition flag
732         /// has to be set to sal_False, if hidden condition flag of <pFmt->GetSection()>
733         /// (SwSection before the changes) is sal_False (already saved in <bOldCondHidden>)
734         /// and new calculated condition is sal_True.
735         /// This is necessary, because otherwise the <SetCondHidden> would have
736         /// no effect.
737         bool bCalculatedCondHidden =
738                 aCalc.Calculate( pSection->GetCondition() ).GetBool() ? true : false;
739         if ( bCalculatedCondHidden && !bOldCondHidden )
740         {
741             pSection->SetCondHidden( false );
742         }
743         pSection->SetCondHidden( bCalculatedCondHidden );
744 	}
745 
746 	if( bUpdate )
747 		pSection->CreateLink( bPreventLinkUpdate ? CREATE_CONNECT : CREATE_UPDATE );
748 	else if( !pSection->IsLinkType() && pSection->IsConnected() )
749 	{
750 		pSection->Disconnect();
751 		GetLinkManager().Remove( &pSection->GetBaseLink() );
752 	}
753 
754 	SetModified();
755 }
756 
757 /* -----------------19.02.99 09:31-------------------
758  * LockFrms wurde im InsertSection genutzt, um zu verhindern, dass
759  * SectionFrms durch das DelFrms zerstoert werden. Dies ist durch
760  * den Destroy-Listen-Mechanismus ueberfluessig geworden.
761  * Falls diese Methode doch noch einmal reanimiert wird, bietet es
762  * sich vielleicht an, beim Entlocken die SectionFrms auf Inhalt zu
763  * pruefen und dann ggf. zur Zerstoerung anzumelden.
764  * --------------------------------------------------*/
765 
766 // und dann waren da noch die Fussnoten:
lcl_DeleteFtn(SwSectionNode * pNd,sal_uLong nStt,sal_uLong nEnd)767 void lcl_DeleteFtn( SwSectionNode *pNd, sal_uLong nStt, sal_uLong nEnd )
768 {
769 	SwFtnIdxs& rFtnArr = pNd->GetDoc()->GetFtnIdxs();
770 	if( rFtnArr.Count() )
771 	{
772 		sal_uInt16 nPos;
773 		rFtnArr.SeekEntry( SwNodeIndex( *pNd ), &nPos );
774 		SwTxtFtn* pSrch;
775 
776 		// loesche erstmal alle, die dahinter stehen
777 		while( nPos < rFtnArr.Count() &&
778 			_SwTxtFtn_GetIndex( (pSrch = rFtnArr[ nPos ]) ) <= nEnd )
779 		{
780 			// Werden die Nodes nicht geloescht mussen sie bei den Seiten
781 			// abmeldet (Frms loeschen) werden, denn sonst bleiben sie
782 			// stehen (Undo loescht sie nicht!)
783 			pSrch->DelFrms(0);
784 			++nPos;
785 		}
786 
787 		while( nPos-- &&
788 			_SwTxtFtn_GetIndex( (pSrch = rFtnArr[ nPos ]) ) >= nStt )
789 		{
790 			// Werden die Nodes nicht geloescht mussen sie bei den Seiten
791 			// abmeldet (Frms loeschen) werden, denn sonst bleiben sie
792 			// stehen (Undo loescht sie nicht!)
793 			pSrch->DelFrms(0);
794 		}
795 	}
796 }
797 
lcl_IsTOXSection(SwSectionData const & rSectionData)798 static inline bool lcl_IsTOXSection(SwSectionData const& rSectionData)
799 {
800     return (TOX_CONTENT_SECTION == rSectionData.GetType())
801         || (TOX_HEADER_SECTION  == rSectionData.GetType());
802 }
803 
InsertTextSection(SwNodeIndex const & rNdIdx,SwSectionFmt & rSectionFmt,SwSectionData const & rSectionData,SwTOXBase const * const pTOXBase,SwNodeIndex const * const pEnde,bool const bInsAtStart,bool const bCreateFrms)804 SwSectionNode* SwNodes::InsertTextSection(SwNodeIndex const& rNdIdx,
805 								SwSectionFmt& rSectionFmt,
806                                 SwSectionData const& rSectionData,
807                                 SwTOXBase const*const pTOXBase,
808                                 SwNodeIndex const*const pEnde,
809                                 bool const bInsAtStart, bool const bCreateFrms)
810 {
811 	SwNodeIndex aInsPos( rNdIdx );
812 	if( !pEnde )		// kein Bereich also neue Section davor/hinter anlegen
813 	{
814         // #i26762#
815         ASSERT(!pEnde || rNdIdx <= *pEnde,
816                "Section start and end in wrong order!");
817 
818 		if( bInsAtStart )
819 		{
820             if (!lcl_IsTOXSection(rSectionData))
821 			{
822 				do {
823 					aInsPos--;
824 				} while( aInsPos.GetNode().IsSectionNode() );
825 				aInsPos++;
826 			}
827 		}
828 		else
829 		{
830 			SwNode* pNd;
831 			aInsPos++;
832             if (!lcl_IsTOXSection(rSectionData))
833             {
834 				while( aInsPos.GetIndex() < Count() - 1 &&
835 						( pNd = &aInsPos.GetNode())->IsEndNode() &&
836                         pNd->StartOfSectionNode()->IsSectionNode())
837                 {
838 					aInsPos++;
839                 }
840             }
841 		}
842 	}
843 
844     SwSectionNode *const pSectNd =
845             new SwSectionNode(aInsPos, rSectionFmt, pTOXBase);
846 	if( pEnde )
847 	{
848 		// Sonderfall fuer die Reader/Writer
849 		if( &pEnde->GetNode() != &GetEndOfContent() )
850 			aInsPos = pEnde->GetIndex()+1;
851         // #i58710: We created a RTF document with a section break inside a table cell
852         // We are not able to handle a section start inside a table and the section end outside.
853         const SwNode* pLastNode = pSectNd->StartOfSectionNode()->EndOfSectionNode();
854         if( aInsPos > pLastNode->GetIndex() )
855             aInsPos = pLastNode->GetIndex();
856         // Another way round: if the section starts outside a table but the end is inside...
857         // aInsPos is at the moment the Position where my EndNode will be inserted
858         const SwStartNode* pStartNode = aInsPos.GetNode().StartOfSectionNode();
859         // This StartNode should be in front of me, but if not, I wanna survive
860         sal_uLong nMyIndex = pSectNd->GetIndex();
861         if( pStartNode->GetIndex() > nMyIndex ) // Suspicious!
862         {
863             const SwNode* pTemp;
864             do
865             {
866                 pTemp = pStartNode; // pTemp is a suspicious one
867                 pStartNode = pStartNode->StartOfSectionNode();
868             }
869             while( pStartNode->GetIndex() > nMyIndex );
870             pTemp = pTemp->EndOfSectionNode();
871             // If it starts behind me but ends behind my end...
872             if( pTemp->GetIndex() >= aInsPos.GetIndex() )
873                 aInsPos = pTemp->GetIndex()+1; // ...I have to correct my end position
874         }
875 
876 	}
877 	else
878 	{
879 		SwTxtNode* pCpyTNd = rNdIdx.GetNode().GetTxtNode();
880 		if( pCpyTNd )
881 		{
882 			SwTxtNode* pTNd = new SwTxtNode( aInsPos, pCpyTNd->GetTxtColl() );
883             if( pCpyTNd->HasSwAttrSet() )
884 			{
885 				// Task 70955 - move PageDesc/Break to the first Node of the
886 				//				section
887 				const SfxItemSet& rSet = *pCpyTNd->GetpSwAttrSet();
888 				if( SFX_ITEM_SET == rSet.GetItemState( RES_BREAK ) ||
889 					SFX_ITEM_SET == rSet.GetItemState( RES_PAGEDESC ))
890 				{
891 					SfxItemSet aSet( rSet );
892 					if( bInsAtStart )
893 						pCpyTNd->ResetAttr( RES_PAGEDESC, RES_BREAK );
894 					else
895 					{
896 						aSet.ClearItem( RES_PAGEDESC );
897 						aSet.ClearItem( RES_BREAK );
898 					}
899                     pTNd->SetAttr( aSet );
900 				}
901 				else
902                     pTNd->SetAttr( rSet );
903 			}
904 			// den Frame anlegen nicht vergessen !!
905 			pCpyTNd->MakeFrms( *pTNd );
906 		}
907 		else
908 			new SwTxtNode( aInsPos, (SwTxtFmtColl*)GetDoc()->GetDfltTxtFmtColl() );
909 	}
910 	new SwEndNode( aInsPos, *pSectNd );
911 
912     pSectNd->GetSection().SetSectionData(rSectionData);
913 	SwSectionFmt* pSectFmt = pSectNd->GetSection().GetFmt();
914 
915 	// Hier bietet sich als Optimierung an, vorhandene Frames nicht zu
916 	// zerstoeren und wieder neu anzulegen, sondern nur umzuhaengen.
917 	sal_Bool bInsFrm = bCreateFrms && !pSectNd->GetSection().IsHidden() &&
918 				   GetDoc()->GetCurrentViewShell();	//swmod 071108//swmod 071225
919 	SwNode2Layout *pNode2Layout = NULL;
920 	if( bInsFrm )
921 	{
922 		SwNodeIndex aTmp( *pSectNd );
923 		if( !pSectNd->GetNodes().FindPrvNxtFrmNode( aTmp, pSectNd->EndOfSectionNode() ) )
924 			// dann sammel mal alle Uppers ein
925 			pNode2Layout = new SwNode2Layout( *pSectNd );
926 	}
927 
928 	// jetzt noch bei allen im Bereich den richtigen StartNode setzen
929 	sal_uLong nEnde = pSectNd->EndOfSectionIndex();
930 	sal_uLong nStart = pSectNd->GetIndex()+1;
931 	sal_uLong nSkipIdx = ULONG_MAX;
932 	for( sal_uLong n = nStart; n < nEnde; ++n )
933 	{
934 		SwNode* pNd = (*this)[n];
935 
936 		//JP 30.04.99: Bug 65644 - alle in der NodeSection liegenden
937 		//				Sections unter die neue haengen
938 		if( ULONG_MAX == nSkipIdx )
939 			pNd->pStartOfSection = pSectNd;
940 		else if( n >= nSkipIdx )
941 			nSkipIdx = ULONG_MAX;
942 
943 		if( pNd->IsStartNode() )
944 		{
945 			// die Verschachtelung der Formate herstellen!
946 			if( pNd->IsSectionNode() )
947 			{
948 				((SwSectionNode*)pNd)->GetSection().GetFmt()->
949 									SetDerivedFrom( pSectFmt );
950 				((SwSectionNode*)pNd)->DelFrms();
951 				n = pNd->EndOfSectionIndex();
952 			}
953 			else
954 			{
955 				if( pNd->IsTableNode() )
956 					((SwTableNode*)pNd)->DelFrms();
957 
958 				if( ULONG_MAX == nSkipIdx )
959 					nSkipIdx = pNd->EndOfSectionIndex();
960 			}
961 		}
962 		else if( pNd->IsCntntNode() )
963 			((SwCntntNode*)pNd)->DelFrms();
964 	}
965 
966 	lcl_DeleteFtn( pSectNd, nStart, nEnde );
967 
968 	if( bInsFrm )
969 	{
970 		if( pNode2Layout )
971 		{
972 			sal_uLong nIdx = pSectNd->GetIndex();
973 			pNode2Layout->RestoreUpperFrms( pSectNd->GetNodes(), nIdx, nIdx + 1 );
974 			delete pNode2Layout;
975 		}
976 		else
977 			pSectNd->MakeFrms( &aInsPos );
978 	}
979 
980 	return pSectNd;
981 }
982 
FindSectionNode()983 SwSectionNode* SwNode::FindSectionNode()
984 {
985 	if( IsSectionNode() )
986 		return GetSectionNode();
987 	SwStartNode* pTmp = pStartOfSection;
988 	while( !pTmp->IsSectionNode() && pTmp->GetIndex() )
989 #if defined( ALPHA ) && defined( UNX )
990 		pTmp = ((SwNode*)pTmp)->pStartOfSection;
991 #else
992 		pTmp = pTmp->pStartOfSection;
993 #endif
994 	return pTmp->GetSectionNode();
995 }
996 
997 
998 //---------
999 // SwSectionNode
1000 //---------
1001 
1002 // ugly hack to make m_pSection const
1003 static SwSectionFmt &
lcl_initParent(SwSectionNode & rThis,SwSectionFmt & rFmt)1004 lcl_initParent(SwSectionNode & rThis, SwSectionFmt & rFmt)
1005 {
1006     SwSectionNode *const pParent =
1007         rThis.StartOfSectionNode()->FindSectionNode();
1008 	if( pParent )
1009 	{
1010 		// das Format beim richtigen Parent anmelden.
1011 		rFmt.SetDerivedFrom( pParent->GetSection().GetFmt() );
1012 	}
1013     return rFmt;
1014 }
1015 
SwSectionNode(SwNodeIndex const & rIdx,SwSectionFmt & rFmt,SwTOXBase const * const pTOXBase)1016 SwSectionNode::SwSectionNode(SwNodeIndex const& rIdx,
1017         SwSectionFmt & rFmt, SwTOXBase const*const pTOXBase)
1018 	: SwStartNode( rIdx, ND_SECTIONNODE )
1019     , m_pSection( (pTOXBase)
1020         ? new SwTOXBaseSection(*pTOXBase, lcl_initParent(*this, rFmt))
1021         : new SwSection( CONTENT_SECTION, rFmt.GetName(),
1022                 lcl_initParent(*this, rFmt) ) )
1023 {
1024 	// jetzt noch die Verbindung von Format zum Node setzen
1025 	// Modify unterdruecken, interresiert keinen
1026 	rFmt.LockModify();
1027     rFmt.SetFmtAttr( SwFmtCntnt( this ) );
1028 	rFmt.UnlockModify();
1029 }
1030 
1031 #if OSL_DEBUG_LEVEL > 1
1032 //Hier werden ueberfluessige SectionFrms entfernt
SwClearDummies(SwFrm * pFrm)1033 SwFrm* SwClearDummies( SwFrm* pFrm )
1034 {
1035 	SwFrm* pTmp = pFrm;
1036 	while( pTmp )
1037 	{
1038 		ASSERT( !pTmp->GetUpper(), "SwClearDummies: No Upper allowed!" );
1039 		if( pTmp->IsSctFrm() )
1040 		{
1041 			SwSectionFrm* pSectFrm = (SwSectionFrm*)pFrm;
1042 			pTmp = pTmp->GetNext();
1043 			if( !pSectFrm->GetLower() )
1044 			{
1045 				if( pSectFrm->GetPrev() )
1046 					pSectFrm->GetPrev()->pNext = pTmp;
1047 				else
1048 					pFrm = pTmp;
1049 				if( pTmp )
1050 					pTmp->pPrev = pSectFrm->GetPrev();
1051 				delete pSectFrm;
1052 			}
1053 		}
1054 		else
1055 			pTmp = pTmp->GetNext();
1056 	}
1057 	return pFrm;
1058 }
1059 #endif
1060 
~SwSectionNode()1061 SwSectionNode::~SwSectionNode()
1062 {
1063     // mba: test if iteration works as clients will be removed in callback
1064     // --> OD #i117863#
1065     // use hint which allows to specify, if the content shall be saved or not
1066     m_pSection->GetFmt()->CallSwClientNotify( SwSectionFrmMoveAndDeleteHint( sal_True ) );
1067     // <--
1068     SwSectionFmt* pFmt = m_pSection->GetFmt();
1069 	if( pFmt )
1070 	{
1071 		// das Attribut entfernen, weil die Section ihr Format loescht
1072 		// und falls das Cntnt-Attribut vorhanden ist, die Section aufhebt.
1073 		pFmt->LockModify();
1074         pFmt->ResetFmtAttr( RES_CNTNT );
1075 		pFmt->UnlockModify();
1076 	}
1077 }
1078 
1079 
MakeFrm(SwFrm * pSib)1080 SwFrm *SwSectionNode::MakeFrm( SwFrm *pSib )
1081 {
1082     m_pSection->m_Data.SetHiddenFlag(false);
1083     return new SwSectionFrm( *m_pSection, pSib );
1084 }
1085 
1086 //Methode erzeugt fuer den vorhergehenden Node alle Ansichten vom
1087 //Dokument. Die erzeugten Contentframes werden in das entsprechende
1088 //Layout gehaengt.
MakeFrms(const SwNodeIndex & rIdx)1089 void SwSectionNode::MakeFrms(const SwNodeIndex & rIdx )
1090 {
1091 	// also nehme meinen nachfolgenden oder vorhergehenden ContentFrame:
1092 	SwNodes& rNds = GetNodes();
1093 	if( rNds.IsDocNodes() && rNds.GetDoc()->GetCurrentViewShell() )	//swmod 071108//swmod 071225
1094 	{
1095 		if( GetSection().IsHidden() || IsCntntHidden() )
1096 		{
1097 			SwNodeIndex aIdx( *EndOfSectionNode() );
1098 			SwCntntNode* pCNd = rNds.GoNextSection( &aIdx, sal_True, sal_False );
1099 			if( !pCNd )
1100 			{
1101 				aIdx = *this;
1102 				if( 0 == ( pCNd = rNds.GoPrevSection( &aIdx, sal_True, sal_False )) )
1103 					return ;
1104 			}
1105             pCNd = aIdx.GetNode().GetCntntNode();
1106 			pCNd->MakeFrms( (SwCntntNode&)rIdx.GetNode() );
1107 		}
1108 		else
1109 		{
1110 			SwNode2Layout aNode2Layout( *this, rIdx.GetIndex() );
1111 			SwFrm *pFrm, *pNew;
1112 			while( 0 != (pFrm = aNode2Layout.NextFrm()) )
1113 			{
1114 				ASSERT( pFrm->IsSctFrm(), "Depend von Section keine Section." );
1115 				pNew = rIdx.GetNode().GetCntntNode()->MakeFrm( pFrm );
1116 
1117                 SwSectionNode* pS = rIdx.GetNode().FindSectionNode();
1118                 // --> OD 2008-06-23 #156927#
1119                 // Assure that node is not inside a table, which is inside the
1120                 // found section.
1121                 if ( pS )
1122                 {
1123                     SwTableNode* pTableNode = rIdx.GetNode().FindTableNode();
1124                     if ( pTableNode &&
1125                          pTableNode->GetIndex() > pS->GetIndex() )
1126                     {
1127                         pS = 0;
1128                     }
1129                 }
1130                 // <--
1131                 // if the node is in a section, the sectionframe now
1132 				// has to be created..
1133                 // OD 14.11.2002 #104684# - boolean to control <Init()> of a new
1134                 // section frame.
1135                 bool bInitNewSect = false;
1136                 if( pS )
1137 				{
1138                     SwSectionFrm *pSct = new SwSectionFrm( pS->GetSection(), pFrm );
1139                     // OD 14.11.2002 #104684# - prepare <Init()> of new section frame.
1140                     bInitNewSect = true;
1141 					SwLayoutFrm* pUp = pSct;
1142 					while( pUp->Lower() )  // for columned sections
1143 					{
1144 						ASSERT( pUp->Lower()->IsLayoutFrm(),"Who's in there?" );
1145 						pUp = (SwLayoutFrm*)pUp->Lower();
1146 					}
1147 					pNew->Paste( pUp, NULL );
1148                     // --> OD 2005-12-01 #i27138#
1149                     // notify accessibility paragraphs objects about changed
1150                     // CONTENT_FLOWS_FROM/_TO relation.
1151                     // Relation CONTENT_FLOWS_FROM for next paragraph will change
1152                     // and relation CONTENT_FLOWS_TO for previous paragraph will change.
1153                     if ( pNew->IsTxtFrm() )
1154                     {
1155                         ViewShell* pViewShell( pNew->getRootFrm()->GetCurrShell() );
1156                         if ( pViewShell && pViewShell->GetLayout() &&
1157                              pViewShell->GetLayout()->IsAnyShellAccessible() )
1158                         {
1159                             pViewShell->InvalidateAccessibleParaFlowRelation(
1160                                 dynamic_cast<SwTxtFrm*>(pNew->FindNextCnt( true )),
1161                                 dynamic_cast<SwTxtFrm*>(pNew->FindPrevCnt( true )) );
1162                         }
1163                     }
1164                     // <--
1165                     pNew = pSct;
1166 				}
1167 
1168 				// wird ein Node vorher oder nachher mit Frames versehen
1169 				if ( rIdx < GetIndex() )
1170 					// der neue liegt vor mir
1171 					pNew->Paste( pFrm->GetUpper(), pFrm );
1172 				else
1173 					// der neue liegt hinter mir
1174 					pNew->Paste( pFrm->GetUpper(), pFrm->GetNext() );
1175                 // --> OD 2005-12-01 #i27138#
1176                 // notify accessibility paragraphs objects about changed
1177                 // CONTENT_FLOWS_FROM/_TO relation.
1178                 // Relation CONTENT_FLOWS_FROM for next paragraph will change
1179                 // and relation CONTENT_FLOWS_TO for previous paragraph will change.
1180                 if ( pNew->IsTxtFrm() )
1181                 {
1182                     ViewShell* pViewShell( pNew->getRootFrm()->GetCurrShell() );
1183                     if ( pViewShell && pViewShell->GetLayout() &&
1184                          pViewShell->GetLayout()->IsAnyShellAccessible() )
1185                     {
1186                         pViewShell->InvalidateAccessibleParaFlowRelation(
1187                             dynamic_cast<SwTxtFrm*>(pNew->FindNextCnt( true )),
1188                             dynamic_cast<SwTxtFrm*>(pNew->FindPrevCnt( true )) );
1189                     }
1190                 }
1191                 // <--
1192                 if ( bInitNewSect )
1193                     static_cast<SwSectionFrm*>(pNew)->Init();
1194 			}
1195 		}
1196 	}
1197 }
1198 
1199 //Fuer jedes vorkommen im Layout einen SectionFrm anlegen und vor den
1200 //entsprechenden CntntFrm pasten.
1201 
MakeFrms(SwNodeIndex * pIdxBehind,SwNodeIndex * pEndIdx)1202 void SwSectionNode::MakeFrms( SwNodeIndex* pIdxBehind, SwNodeIndex* pEndIdx )
1203 {
1204 	ASSERT( pIdxBehind, "kein Index" );
1205 	SwNodes& rNds = GetNodes();
1206 	SwDoc* pDoc = rNds.GetDoc();
1207 
1208 	*pIdxBehind = *this;
1209 
1210     m_pSection->m_Data.SetHiddenFlag(true);
1211 
1212 	if( rNds.IsDocNodes() )
1213 	{
1214 		SwNodeIndex *pEnd = pEndIdx ? pEndIdx :
1215 							new SwNodeIndex( *EndOfSectionNode(), 1 );
1216 		::MakeFrms( pDoc, *pIdxBehind, *pEnd );
1217 		if( !pEndIdx )
1218 			delete pEnd;
1219 	}
1220 
1221 }
1222 
DelFrms()1223 void SwSectionNode::DelFrms()
1224 {
1225 	sal_uLong nStt = GetIndex()+1, nEnd = EndOfSectionIndex();
1226 	if( nStt >= nEnd )
1227 	{
1228 		// unser Flag muessen wir noch aktualisieren
1229 		// pSection->bHiddenFlag = sal_True;
1230 		return ;
1231 	}
1232 
1233 	SwNodes& rNds = GetNodes();
1234     m_pSection->GetFmt()->DelFrms();
1235 
1236 	// unser Flag muessen wir noch aktualisieren
1237     m_pSection->m_Data.SetHiddenFlag(true);
1238 
1239 	// Bug 30582: falls der Bereich in Fly oder TabellenBox ist, dann
1240 	//				kann er nur "gehiddet" werden, wenn weiterer Content
1241 	//				vorhanden ist, der "Frames" haelt. Sonst hat der
1242 	//				Fly/TblBox-Frame keinen Lower !!!
1243 	{
1244 		SwNodeIndex aIdx( *this );
1245 		if( !rNds.GoPrevSection( &aIdx, sal_True, sal_False ) ||
1246 			!CheckNodesRange( *this, aIdx, sal_True ) ||
1247             // OD 04.11.2003 #i21457#
1248             !lcl_IsInSameTblBox( rNds, *this, true ))
1249 		{
1250 			aIdx = *EndOfSectionNode();
1251 			if( !rNds.GoNextSection( &aIdx, sal_True, sal_False ) ||
1252 				!CheckNodesRange( *EndOfSectionNode(), aIdx, sal_True ) ||
1253                 // OD 04.11.2003 #i21457#
1254                 !lcl_IsInSameTblBox( rNds, *EndOfSectionNode(), false ))
1255             {
1256                 m_pSection->m_Data.SetHiddenFlag(false);
1257             }
1258         }
1259     }
1260 }
1261 
MakeCopy(SwDoc * pDoc,const SwNodeIndex & rIdx) const1262 SwSectionNode* SwSectionNode::MakeCopy( SwDoc* pDoc, const SwNodeIndex& rIdx ) const
1263 {
1264 	// in welchen Array steht ich denn: Nodes, UndoNodes ??
1265 	const SwNodes& rNds = GetNodes();
1266 
1267 	// das SectionFrmFmt kopieren
1268 	SwSectionFmt* pSectFmt = pDoc->MakeSectionFmt( 0 );
1269 	pSectFmt->CopyAttrs( *GetSection().GetFmt() );
1270 
1271     ::std::auto_ptr<SwTOXBase> pTOXBase;
1272     if (TOX_CONTENT_SECTION == GetSection().GetType())
1273     {
1274         ASSERT( GetSection().ISA( SwTOXBaseSection ), "no TOXBaseSection!" );
1275         SwTOXBaseSection const& rTBS(
1276             dynamic_cast<SwTOXBaseSection const&>(GetSection()));
1277         pTOXBase.reset( new SwTOXBase(rTBS, pDoc) );
1278     }
1279 
1280     SwSectionNode *const pSectNd =
1281         new SwSectionNode(rIdx, *pSectFmt, pTOXBase.get());
1282 	SwEndNode* pEndNd = new SwEndNode( rIdx, *pSectNd );
1283 	SwNodeIndex aInsPos( *pEndNd );
1284 
1285 	// Werte uebertragen
1286     SwSection *const pNewSect = pSectNd->m_pSection.get();
1287 
1288     if (TOX_CONTENT_SECTION != GetSection().GetType())
1289     {
1290 		// beim Move den Namen beibehalten
1291 		if( rNds.GetDoc() == pDoc && pDoc->IsCopyIsMove() )
1292         {
1293             pNewSect->SetSectionName( GetSection().GetSectionName() );
1294         }
1295 		else
1296         {
1297             pNewSect->SetSectionName(
1298                 pDoc->GetUniqueSectionName( &GetSection().GetSectionName() ));
1299         }
1300     }
1301 
1302 
1303 	pNewSect->SetType( GetSection().GetType() );
1304 	pNewSect->SetCondition( GetSection().GetCondition() );
1305 	pNewSect->SetLinkFileName( GetSection().GetLinkFileName() );
1306 	if( !pNewSect->IsHiddenFlag() && GetSection().IsHidden() )
1307 		pNewSect->SetHidden( sal_True );
1308 	if( !pNewSect->IsProtectFlag() && GetSection().IsProtect() )
1309         pNewSect->SetProtect( sal_True );
1310     // --> FME 2004-06-22 #114856# edit in readonly sections
1311     if( !pNewSect->IsEditInReadonlyFlag() && GetSection().IsEditInReadonly() )
1312         pNewSect->SetEditInReadonly( sal_True );
1313     // <--
1314 
1315 	SwNodeRange aRg( *this, +1, *EndOfSectionNode() );	// (wo stehe in denn nun ??)
1316 	rNds._Copy( aRg, aInsPos, sal_False );
1317 
1318 	// loesche alle Frames vom kopierten Bereich, diese werden beim
1319 	// erzeugen des SectionFrames angelegt !
1320 	pSectNd->DelFrms();
1321 
1322 	// dann kopiere auch noch die Links/Server
1323 	if( pNewSect->IsLinkType() )		// den Link eintragen
1324 		pNewSect->CreateLink( pDoc->GetCurrentViewShell() ? CREATE_CONNECT	//swmod 071108//swmod 071225
1325 												 : CREATE_NONE );
1326 
1327 	// falls als Server aus dem Undo kopiert wird, wieder eintragen
1328     if (m_pSection->IsServer()
1329         && pDoc->GetIDocumentUndoRedo().IsUndoNodes(rNds))
1330     {
1331         pNewSect->SetRefObject( m_pSection->GetObject() );
1332 		pDoc->GetLinkManager().InsertServer( pNewSect->GetObject() );
1333 	}
1334 
1335     // METADATA: copy xml:id; must be done after insertion of node
1336     pSectFmt->RegisterAsCopyOf(*GetSection().GetFmt());
1337 
1338 	return pSectNd;
1339 }
1340 
IsCntntHidden() const1341 sal_Bool SwSectionNode::IsCntntHidden() const
1342 {
1343     ASSERT( !m_pSection->IsHidden(),
1344             "That's simple: Hidden Section => Hidden Content" );
1345 	SwNodeIndex aTmp( *this, 1 );
1346 	sal_uLong nEnd = EndOfSectionIndex();
1347 	while( aTmp < nEnd )
1348 	{
1349 		if( aTmp.GetNode().IsSectionNode() )
1350 		{
1351 			const SwSection& rSect = ((SwSectionNode&)aTmp.GetNode()).GetSection();
1352 			if( rSect.IsHiddenFlag() )
1353 				// dann diese Section ueberspringen
1354 				aTmp = *aTmp.GetNode().EndOfSectionNode();
1355 		}
1356 		else
1357 		{
1358 			if( aTmp.GetNode().IsCntntNode() || aTmp.GetNode().IsTableNode() )
1359 				return sal_False; // Nicht versteckter Inhalt wurde gefunden
1360 			ASSERT( aTmp.GetNode().IsEndNode(), "EndNode expected" );
1361 		}
1362 		aTmp++;
1363 	}
1364 	return sal_True; // Alles versteckt
1365 }
1366 
1367 
NodesArrChgd()1368 void SwSectionNode::NodesArrChgd()
1369 {
1370     SwSectionFmt *const pFmt = m_pSection->GetFmt();
1371 	if( pFmt )
1372 	{
1373 		SwNodes& rNds = GetNodes();
1374 		SwDoc* pDoc = pFmt->GetDoc();
1375 
1376 		if( !rNds.IsDocNodes() )
1377 		{
1378 			SwPtrMsgPoolItem aMsgHint( RES_REMOVE_UNO_OBJECT, pFmt );
1379 			pFmt->ModifyNotification( &aMsgHint, &aMsgHint );
1380 		}
1381 
1382 		pFmt->LockModify();
1383         pFmt->SetFmtAttr( SwFmtCntnt( this ));
1384 		pFmt->UnlockModify();
1385 
1386         SwSectionNode* pSectNd = StartOfSectionNode()->FindSectionNode();
1387 		// set the correct parent from the new section
1388 		pFmt->SetDerivedFrom( pSectNd ? pSectNd->GetSection().GetFmt()
1389 									  : pDoc->GetDfltFrmFmt() );
1390 
1391 		// jetzt noch bei allen im Bereich den richtigen StartNode setzen
1392 		sal_uLong nStart = GetIndex()+1, nEnde = EndOfSectionIndex();
1393 		for( sal_uLong n = nStart; n < nEnde; ++n )
1394 			// die Verschachtelung der Formate herstellen!
1395 			if( 0 != ( pSectNd = rNds[ n ]->GetSectionNode() ) )
1396 			{
1397 				pSectNd->GetSection().GetFmt()->SetDerivedFrom( pFmt );
1398 				n = pSectNd->EndOfSectionIndex();
1399 			}
1400 
1401 		// verschieben vom Nodes- ins UndoNodes-Array?
1402 		if( rNds.IsDocNodes() )
1403 		{
1404 			ASSERT( pDoc == GetDoc(),
1405 					"verschieben in unterschiedliche Documente?" );
1406 			if( m_pSection->IsLinkType() )		// den Link austragen
1407 				m_pSection->CreateLink( pDoc->GetCurrentViewShell() ? CREATE_CONNECT	//swmod 071108
1408 														 : CREATE_NONE );//swmod 071225
1409             if (m_pSection->IsServer())
1410             {
1411                 pDoc->GetLinkManager().InsertServer( m_pSection->GetObject() );
1412             }
1413         }
1414         else
1415         {
1416             if (CONTENT_SECTION != m_pSection->GetType())
1417             {
1418                 pDoc->GetLinkManager().Remove( &m_pSection->GetBaseLink() );
1419             }
1420 
1421             if (m_pSection->IsServer())
1422             {
1423                 pDoc->GetLinkManager().RemoveServer( m_pSection->GetObject() );
1424             }
1425         }
1426     }
1427 }
1428 
1429 
GetUniqueSectionName(const String * pChkStr) const1430 String SwDoc::GetUniqueSectionName( const String* pChkStr ) const
1431 {
1432 	ResId aId( STR_REGION_DEFNAME, *pSwResMgr );
1433 	String aName( aId );
1434 	xub_StrLen nNmLen = aName.Len();
1435 
1436     sal_uInt16 nNum = 0;
1437     sal_uInt16 nTmp, nFlagSize = ( pSectionFmtTbl->Count() / 8 ) +2;
1438 	sal_uInt8* pSetFlags = new sal_uInt8[ nFlagSize ];
1439 	memset( pSetFlags, 0, nFlagSize );
1440 
1441 	const SwSectionNode* pSectNd;
1442 	sal_uInt16 n;
1443 
1444 	for( n = 0; n < pSectionFmtTbl->Count(); ++n )
1445 		if( 0 != ( pSectNd = (*pSectionFmtTbl)[ n ]->GetSectionNode( sal_False ) ))
1446 		{
1447             const String& rNm = pSectNd->GetSection().GetSectionName();
1448 			if( rNm.Match( aName ) == nNmLen )
1449 			{
1450 				// Nummer bestimmen und das Flag setzen
1451                 nNum = static_cast<sal_uInt16>(rNm.Copy( nNmLen ).ToInt32());
1452 				if( nNum-- && nNum < pSectionFmtTbl->Count() )
1453 					pSetFlags[ nNum / 8 ] |= (0x01 << ( nNum & 0x07 ));
1454 			}
1455 			if( pChkStr && pChkStr->Equals( rNm ) )
1456 				pChkStr = 0;
1457 		}
1458 
1459 	if( !pChkStr )
1460 	{
1461 		// alle Nummern entsprechend geflag, also bestimme die richtige Nummer
1462 		nNum = pSectionFmtTbl->Count();
1463 		for( n = 0; n < nFlagSize; ++n )
1464 			if( 0xff != ( nTmp = pSetFlags[ n ] ))
1465 			{
1466 				// also die Nummer bestimmen
1467 				nNum = n * 8;
1468 				while( nTmp & 1 )
1469 					++nNum, nTmp >>= 1;
1470 				break;
1471 			}
1472 
1473 	}
1474 	delete [] pSetFlags;
1475 	if( pChkStr )
1476 		return *pChkStr;
1477 	return aName += String::CreateFromInt32( ++nNum );
1478 }
1479 
1480 
1481