merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / layout / laycache.cxx
blobaff3c4e07ea6137aa49049edc860a55e98564b50
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: laycache.cxx,v $
10 * $Revision: 1.31 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <hintids.hxx>
36 #include <svx/brkitem.hxx>
37 #include <tools/stream.hxx>
38 #include <doc.hxx>
39 #include <docstat.hxx>
40 #include <docary.hxx>
41 #include <fmtpdsc.hxx>
42 #include <laycache.hxx>
43 #include <layhelp.hxx>
44 #include <pagefrm.hxx>
45 #include <rootfrm.hxx>
46 #include <txtfrm.hxx>
47 #include <ndtxt.hxx>
48 #include <swtable.hxx>
49 #include <tabfrm.hxx>
50 #include <rowfrm.hxx>
51 #include <colfrm.hxx>
52 #include <bodyfrm.hxx>
53 #include <ndindex.hxx>
54 #include <sectfrm.hxx>
55 #include <frmfmt.hxx>
56 #include <fmtcntnt.hxx>
57 #include <pagedesc.hxx>
58 #include <frmtool.hxx>
59 #include <dflyobj.hxx>
60 #include <dcontact.hxx>
61 #include <flyfrm.hxx>
62 // OD 2004-05-24 #i28701#
63 #include <sortedobjs.hxx>
64 // --> OD 2006-03-22 #b6375613#
65 #include <pam.hxx>
66 #ifndef _DOCSH_HXX
67 #include <docsh.hxx>
68 #endif
69 #include <com/sun/star/document/XDocumentInfoSupplier.hpp>
70 #include <com/sun/star/beans/XPropertySet.hpp>
72 using namespace ::com::sun::star;
73 // <--
75 #include <set>
77 SV_IMPL_PTRARR( SwPageFlyCache, SwFlyCachePtr )
79 /*-----------------28.5.2001 10:06------------------
80 * Reading and writing of the layout cache.
81 * The layout cache is not necessary, but it improves
82 * the performance and reduces the text flow during
83 * the formatting.
84 * The layout cache contains the index of the paragraphs/tables
85 * at the top of every page, so it's possible to create
86 * the right count of pages and to distribute the document content
87 * to this pages before the formatting starts.
88 *--------------------------------------------------*/
90 void SwLayoutCache::Read( SvStream &rStream )
92 if( !pImpl )
94 pImpl = new SwLayCacheImpl;
95 if( !pImpl->Read( rStream ) )
97 delete pImpl;
98 pImpl = 0;
103 //-----------------------------------------------------------------------------
105 void SwLayCacheImpl::Insert( USHORT nType, ULONG nIndex, xub_StrLen nOffset )
107 aType.Insert( nType, aType.Count() );
108 SvULongs::Insert( nIndex, SvULongs::Count() );
109 aOffset.Insert( nOffset, aOffset.Count() );
112 BOOL SwLayCacheImpl::Read( SvStream& rStream )
114 SwLayCacheIoImpl aIo( rStream, FALSE );
115 if( aIo.GetMajorVersion() > SW_LAYCACHE_IO_VERSION_MAJOR )
116 return FALSE;
118 // Due to an evil bug in the layout cache (#102759#), we cannot trust the
119 // sizes of fly frames which have been written using the "old" layout cache.
120 // This flag should indicate that we do not want to trust the width and
121 // height of fly frames
122 bUseFlyCache = aIo.GetMinorVersion() >= 1;
124 BYTE cFlags;
125 UINT32 nIndex, nOffset;
127 aIo.OpenRec( SW_LAYCACHE_IO_REC_PAGES );
128 aIo.OpenFlagRec();
129 aIo.CloseFlagRec();
130 while( aIo.BytesLeft() && !aIo.HasError() )
132 switch( aIo.Peek() )
134 case SW_LAYCACHE_IO_REC_PARA:
135 aIo.OpenRec( SW_LAYCACHE_IO_REC_PARA );
136 cFlags = aIo.OpenFlagRec();
137 aIo.GetStream() >> nIndex;
138 if( (cFlags & 0x01) != 0 )
139 aIo.GetStream() >> nOffset;
140 else
141 nOffset = STRING_LEN;
142 aIo.CloseFlagRec();
143 Insert( SW_LAYCACHE_IO_REC_PARA, nIndex, (xub_StrLen)nOffset );
144 aIo.CloseRec( SW_LAYCACHE_IO_REC_PARA );
145 break;
146 case SW_LAYCACHE_IO_REC_TABLE:
147 aIo.OpenRec( SW_LAYCACHE_IO_REC_TABLE );
148 aIo.OpenFlagRec();
149 aIo.GetStream() >> nIndex
150 >> nOffset;
151 Insert( SW_LAYCACHE_IO_REC_TABLE, nIndex, (xub_StrLen)nOffset );
152 aIo.CloseFlagRec();
153 aIo.CloseRec( SW_LAYCACHE_IO_REC_TABLE );
154 break;
155 case SW_LAYCACHE_IO_REC_FLY:
157 aIo.OpenRec( SW_LAYCACHE_IO_REC_FLY );
158 aIo.OpenFlagRec();
159 aIo.CloseFlagRec();
160 long nX, nY, nW, nH;
161 USHORT nPgNum;
162 aIo.GetStream() >> nPgNum >> nIndex
163 >> nX >> nY >> nW >> nH;
164 SwFlyCache* pFly = new SwFlyCache( nPgNum, nIndex, nX, nY, nW, nH );
165 aFlyCache.Insert( pFly, aFlyCache.Count() );
166 aIo.CloseRec( SW_LAYCACHE_IO_REC_FLY );
167 break;
169 default:
170 aIo.SkipRec();
171 break;
174 aIo.CloseRec( SW_LAYCACHE_IO_REC_PAGES );
176 return !aIo.HasError();
179 /*-----------------28.5.2001 10:19------------------
180 * SwLayoutCache::Write(..)
181 * writes the index (more precise: the difference between
182 * the index and the first index of the document content)
183 * of the first paragraph/table at the top of every page.
184 * If at the top of a page is the rest of a paragraph/table
185 * from the bottom of the previous page, the character/row
186 * number is stored, too.
187 * The position, size and page number of the text frames
188 * are stored, too
189 * --------------------------------------------------*/
191 void SwLayoutCache::Write( SvStream &rStream, const SwDoc& rDoc )
193 if( rDoc.GetRootFrm() ) // the layout itself ..
195 SwLayCacheIoImpl aIo( rStream, TRUE );
196 // We want to save the relative index, so we need the index
197 // of the first content
198 ULONG nStartOfContent = rDoc.GetNodes().GetEndOfContent().
199 StartOfSectionNode()->GetIndex();
200 // The first page..
201 SwPageFrm* pPage = (SwPageFrm*)rDoc.GetRootFrm()->Lower();
203 aIo.OpenRec( SW_LAYCACHE_IO_REC_PAGES );
204 aIo.OpenFlagRec( 0, 0 );
205 aIo.CloseFlagRec();
206 while( pPage )
208 if( pPage->GetPrev() )
210 SwLayoutFrm* pLay = pPage->FindBodyCont();
211 SwFrm* pTmp = pLay ? pLay->ContainsAny() : NULL;
212 // We are only interested in paragraph or table frames,
213 // a section frames contains paragraphs/tables.
214 if( pTmp && pTmp->IsSctFrm() )
215 pTmp = ((SwSectionFrm*)pTmp)->ContainsAny();
217 if( pTmp ) // any content
219 if( pTmp->IsTxtFrm() )
221 ULONG nNdIdx = ((SwTxtFrm*)pTmp)->GetNode()->GetIndex();
222 if( nNdIdx > nStartOfContent )
224 /* Open Paragraph Record */
225 aIo.OpenRec( SW_LAYCACHE_IO_REC_PARA );
226 BOOL bFollow = ((SwTxtFrm*)pTmp)->IsFollow();
227 aIo.OpenFlagRec( bFollow ? 0x01 : 0x00,
228 bFollow ? 8 : 4 );
229 nNdIdx -= nStartOfContent;
230 aIo.GetStream() << static_cast<sal_uInt32>(nNdIdx);
231 if( bFollow )
232 aIo.GetStream() << static_cast<sal_uInt32>(((SwTxtFrm*)pTmp)->GetOfst());
233 aIo.CloseFlagRec();
234 /* Close Paragraph Record */
235 aIo.CloseRec( SW_LAYCACHE_IO_REC_PARA );
238 else if( pTmp->IsTabFrm() )
240 SwTabFrm* pTab = (SwTabFrm*)pTmp;
241 ULONG nOfst = STRING_LEN;
242 if( pTab->IsFollow() )
244 // If the table is a follow, we have to look for the
245 // master and to count all rows to get the row number
246 nOfst = 0;
247 if( pTab->IsFollow() )
248 pTab = pTab->FindMaster( true );
249 while( pTab != pTmp )
251 SwFrm* pSub = pTab->Lower();
252 while( pSub )
254 ++nOfst;
255 pSub = pSub->GetNext();
257 pTab = pTab->GetFollow();
258 ASSERT( pTab, "Table follow without master" );
263 ULONG nNdIdx =
264 pTab->GetTable()->GetTableNode()->GetIndex();
265 if( nNdIdx > nStartOfContent )
267 /* Open Table Record */
268 aIo.OpenRec( SW_LAYCACHE_IO_REC_TABLE );
269 aIo.OpenFlagRec( 0, 8 );
270 nNdIdx -= nStartOfContent;
271 aIo.GetStream() << static_cast<sal_uInt32>(nNdIdx)
272 << static_cast<sal_uInt32>(nOfst);
273 aIo.CloseFlagRec();
274 /* Close Table Record */
275 aIo.CloseRec( SW_LAYCACHE_IO_REC_TABLE );
277 // If the table has a follow on the next page,
278 // we know already the row number and store this
279 // immediately.
280 if( pTab->GetFollow() )
282 if( nOfst == STRING_LEN )
283 nOfst = 0;
286 SwFrm* pSub = pTab->Lower();
287 while( pSub )
289 ++nOfst;
290 pSub = pSub->GetNext();
292 pTab = pTab->GetFollow();
293 SwPageFrm *pTabPage = pTab->FindPageFrm();
294 if( pTabPage != pPage )
296 ASSERT( pPage->GetPhyPageNum() <
297 pTabPage->GetPhyPageNum(),
298 "Looping Tableframes" );
299 pPage = pTabPage;
300 break;
302 } while ( pTab->GetFollow() );
304 else
305 break;
306 } while( pTab );
310 if( pPage->GetSortedObjs() )
312 SwSortedObjs &rObjs = *pPage->GetSortedObjs();
313 for ( USHORT i = 0; i < rObjs.Count(); ++i )
315 SwAnchoredObject* pAnchoredObj = rObjs[i];
316 if ( pAnchoredObj->ISA(SwFlyFrm) )
318 SwFlyFrm *pFly = static_cast<SwFlyFrm*>(pAnchoredObj);
319 if( pFly->Frm().Left() != WEIT_WECH &&
320 !pFly->GetAnchorFrm()->FindFooterOrHeader() )
322 const SwContact *pC =
323 ::GetUserCall(pAnchoredObj->GetDrawObj());
324 if( pC )
326 sal_uInt32 nOrdNum = pAnchoredObj->GetDrawObj()->GetOrdNum();
327 USHORT nPageNum = pPage->GetPhyPageNum();
328 /* Open Fly Record */
329 aIo.OpenRec( SW_LAYCACHE_IO_REC_FLY );
330 aIo.OpenFlagRec( 0, 0 );
331 aIo.CloseFlagRec();
332 SwRect &rRct = pFly->Frm();
333 sal_Int32 nX = rRct.Left() - pPage->Frm().Left();
334 sal_Int32 nY = rRct.Top() - pPage->Frm().Top();
335 aIo.GetStream() << nPageNum << nOrdNum
336 << nX << nY << rRct.Width()
337 << rRct.Height();
338 /* Close Fly Record */
339 aIo.CloseRec( SW_LAYCACHE_IO_REC_FLY );
345 pPage = (SwPageFrm*)pPage->GetNext();
347 aIo.CloseRec( SW_LAYCACHE_IO_REC_PAGES );
351 #ifndef PRODUCT
352 sal_Bool SwLayoutCache::CompareLayout( const SwDoc& rDoc ) const
354 sal_Bool bRet = sal_True;
355 if( pImpl && rDoc.GetRootFrm() )
357 USHORT nIndex = 0;
358 ULONG nStartOfContent = rDoc.GetNodes().GetEndOfContent().
359 StartOfSectionNode()->GetIndex();
360 SwPageFrm* pPage = (SwPageFrm*)rDoc.GetRootFrm()->Lower();
361 if( pPage )
362 pPage = (SwPageFrm*)pPage->GetNext();
363 while( pPage )
365 if( nIndex >= pImpl->Count() )
367 if( bRet )
368 bRet = sal_False;
369 break;
371 SwLayoutFrm* pLay = pPage->FindBodyCont();
372 SwFrm* pTmp = pLay ? pLay->ContainsAny() : NULL;
373 if( pTmp && pTmp->IsSctFrm() )
374 pTmp = ((SwSectionFrm*)pTmp)->ContainsAny();
375 if( pTmp )
377 if( pTmp->IsTxtFrm() )
379 ULONG nNdIdx = ((SwTxtFrm*)pTmp)->GetNode()->GetIndex();
380 if( nNdIdx > nStartOfContent )
382 BOOL bFollow = ((SwTxtFrm*)pTmp)->IsFollow();
383 nNdIdx -= nStartOfContent;
384 if( pImpl->GetBreakIndex( nIndex ) != nNdIdx ||
385 SW_LAYCACHE_IO_REC_PARA !=
386 pImpl->GetBreakType( nIndex ) ||
387 ( bFollow ? ((SwTxtFrm*)pTmp)->GetOfst()
388 : STRING_LEN ) != pImpl->GetBreakOfst( nIndex ) )
390 if( bRet )
391 bRet = sal_False;
393 ++nIndex;
396 else if( pTmp->IsTabFrm() )
398 SwTabFrm* pTab = (SwTabFrm*)pTmp;
399 ULONG nOfst = STRING_LEN;
400 if( pTab->IsFollow() )
402 nOfst = 0;
403 if( pTab->IsFollow() )
404 pTab = pTab->FindMaster( true );
405 while( pTab != pTmp )
407 SwFrm* pSub = pTab->Lower();
408 while( pSub )
410 ++nOfst;
411 pSub = pSub->GetNext();
413 pTab = pTab->GetFollow();
418 ULONG nNdIdx =
419 pTab->GetTable()->GetTableNode()->GetIndex();
420 if( nNdIdx > nStartOfContent )
422 nNdIdx -= nStartOfContent;
423 if( pImpl->GetBreakIndex( nIndex ) != nNdIdx ||
424 SW_LAYCACHE_IO_REC_TABLE !=
425 pImpl->GetBreakType( nIndex ) ||
426 nOfst != pImpl->GetBreakOfst( nIndex ) )
428 if( bRet )
429 bRet = sal_False;
431 ++nIndex;
433 if( pTab->GetFollow() )
435 if( nOfst == STRING_LEN )
436 nOfst = 0;
439 SwFrm* pSub = pTab->Lower();
440 while( pSub )
442 ++nOfst;
443 pSub = pSub->GetNext();
445 pTab = pTab->GetFollow();
446 SwPageFrm *pTabPage = pTab->FindPageFrm();
447 if( pTabPage != pPage )
449 pPage = pTabPage;
450 break;
452 } while ( pTab->GetFollow() );
454 else
455 break;
456 } while( pTab );
459 pPage = (SwPageFrm*)pPage->GetNext();
462 return bRet;
464 #endif
466 void SwLayoutCache::ClearImpl()
468 if( !IsLocked() )
470 delete pImpl;
471 pImpl = 0;
476 SwLayoutCache::~SwLayoutCache()
478 ASSERT( !nLockCount, "Deleting a locked SwLayoutCache!?" );
479 delete pImpl;
482 /*-----------------28.5.2001 10:47------------------
483 * SwActualSection,
484 * a help class to create not nested section frames
485 * for nested sections.
486 * --------------------------------------------------*/
488 SwActualSection::SwActualSection( SwActualSection *pUp,
489 SwSectionFrm *pSect,
490 SwSectionNode *pNd ) :
491 pUpper( pUp ),
492 pSectFrm( pSect ),
493 pSectNode( pNd )
495 if ( !pSectNode )
497 const SwNodeIndex *pIndex = pSect->GetFmt()->GetCntnt().GetCntntIdx();
498 pSectNode = pSect->GetFmt()->GetDoc()->GetNodes()[*pIndex]->
499 FindSectionNode();
503 /*-----------------28.5.2001 11:09------------------
504 * SwLayHelper
505 * is the helper class, which utilizes the layout cache information
506 * to distribute the document content to the rigth pages.
507 * It's used by the _InsertCnt(..)-function.
508 * If there's no layout cache, the distibution to the pages is more
509 * a guess, but a guess with statistical background.
510 * --------------------------------------------------*/
512 SwLayHelper::SwLayHelper( SwDoc *pD, SwFrm* &rpF, SwFrm* &rpP, SwPageFrm* &rpPg,
513 SwLayoutFrm* &rpL, SwActualSection* &rpA, BOOL &rB,
514 ULONG nNodeIndex, BOOL bCache )
515 : rpFrm( rpF ), rpPrv( rpP ), rpPage( rpPg ), rpLay( rpL ),
516 rpActualSection( rpA ), rbBreakAfter(rB), pDoc(pD), nMaxParaPerPage( 25 ),
517 nParagraphCnt( bCache ? 0 : USHRT_MAX ), bFirst( bCache )
519 pImpl = pDoc->GetLayoutCache() ? pDoc->GetLayoutCache()->LockImpl() : NULL;
520 if( pImpl )
522 nMaxParaPerPage = 1000;
523 nStartOfContent = pDoc->GetNodes().GetEndOfContent().StartOfSectionNode()
524 ->GetIndex();
525 nNodeIndex -= nStartOfContent;
526 nIndex = 0;
527 nFlyIdx = 0;
528 while( nIndex < pImpl->Count() && (*pImpl)[ nIndex ] < nNodeIndex )
529 ++nIndex;
530 if( nIndex >= pImpl->Count() )
532 pDoc->GetLayoutCache()->UnlockImpl();
533 pImpl = NULL;
536 else
538 nIndex = USHRT_MAX;
539 nStartOfContent = ULONG_MAX;
543 SwLayHelper::~SwLayHelper()
545 if( pImpl )
547 ASSERT( pDoc && pDoc->GetLayoutCache(), "Missing layoutcache" );
548 pDoc->GetLayoutCache()->UnlockImpl();
552 /*-----------------23.5.2001 16:40------------------
553 * SwLayHelper::CalcPageCount() does not really calculate the page count,
554 * it returns the page count value from the layout cache, if available,
555 * otherwise it estimates the page count.
556 * --------------------------------------------------*/
558 ULONG SwLayHelper::CalcPageCount()
560 ULONG nPgCount;
561 SwLayCacheImpl *pCache = pDoc->GetLayoutCache() ?
562 pDoc->GetLayoutCache()->LockImpl() : NULL;
563 if( pCache )
565 nPgCount = pCache->Count() + 1;
566 pDoc->GetLayoutCache()->UnlockImpl();
568 else
570 nPgCount = pDoc->GetDocStat().nPage;
571 if ( nPgCount <= 10 ) // no page insertion for less than 10 pages
572 nPgCount = 0;
573 ULONG nNdCount = pDoc->GetDocStat().nPara;
574 if ( nNdCount <= 1 )
576 //Estimates the number of paragraphs.
577 ULONG nTmp = pDoc->GetNodes().GetEndOfContent().GetIndex() -
578 pDoc->GetNodes().GetEndOfExtras().GetIndex();
579 //Tables have a little overhead..
580 nTmp -= pDoc->GetTblFrmFmts()->Count() * 25;
581 //Fly frames, too ..
582 nTmp -= (pDoc->GetNodes().GetEndOfAutotext().GetIndex() -
583 pDoc->GetNodes().GetEndOfInserts().GetIndex()) / 3 * 5;
584 if ( nTmp > 0 )
585 nNdCount = nTmp;
587 if ( nNdCount > 100 ) // no estimation below this value
589 if ( nPgCount > 0 )
590 nMaxParaPerPage = nNdCount / nPgCount;
591 else
593 nMaxParaPerPage = Max( ULONG(20),
594 ULONG(20 + nNdCount / 1000 * 3) );
595 #ifdef PM2
596 const ULONG nMax = 49;
597 #else
598 const ULONG nMax = 53;
599 #endif
600 nMaxParaPerPage = Min( nMaxParaPerPage, nMax );
601 nPgCount = nNdCount / nMaxParaPerPage;
603 if ( nNdCount < 1000 )
604 nPgCount = 0;// no progress bar for small documents
605 if ( pDoc->get(IDocumentSettingAccess::BROWSE_MODE) )
606 nMaxParaPerPage *= 6;
609 return nPgCount;
612 /*-----------------23.5.2001 16:44------------------
613 * SwLayHelper::CheckInsertPage()
614 * inserts a page and return TRUE, if
615 * - the break after flag is set
616 * - the actual content wants a break before
617 * - the maximum count of paragraph/rows is reached
619 * The break after flag is set, if the actual content
620 * wants a break after.
621 * --------------------------------------------------*/
623 BOOL SwLayHelper::CheckInsertPage()
625 BOOL bEnd = 0 == rpPage->GetNext();
626 const SwAttrSet* pAttr = rpFrm->GetAttrSet();
627 const SvxFmtBreakItem& rBrk = pAttr->GetBreak();
628 const SwFmtPageDesc& rDesc = pAttr->GetPageDesc();
629 // --> FME 2004-10-26 #118195# Do not evaluate page description if frame
630 // is a follow frame!
631 const SwPageDesc* pDesc = rpFrm->IsFlowFrm() &&
632 SwFlowFrm::CastFlowFrm( rpFrm )->IsFollow() ?
634 rDesc.GetPageDesc();
635 // <--
637 BOOL bBrk = nParagraphCnt > nMaxParaPerPage || rbBreakAfter;
638 rbBreakAfter = rBrk.GetBreak() == SVX_BREAK_PAGE_AFTER ||
639 rBrk.GetBreak() == SVX_BREAK_PAGE_BOTH;
640 if ( !bBrk )
641 bBrk = rBrk.GetBreak() == SVX_BREAK_PAGE_BEFORE ||
642 rBrk.GetBreak() == SVX_BREAK_PAGE_BOTH;
644 if ( bBrk || pDesc )
646 USHORT nPgNum = 0;
647 if ( !pDesc )
648 pDesc = rpPage->GetPageDesc()->GetFollow();
649 else
651 if ( 0 != (nPgNum = rDesc.GetNumOffset()) )
652 ((SwRootFrm*)rpPage->GetUpper())->SetVirtPageNum(TRUE);
654 BOOL bNextPageOdd = !rpPage->OnRightPage();
655 BOOL bInsertEmpty = FALSE;
656 if( nPgNum && bNextPageOdd != ( ( nPgNum % 2 ) != 0 ) )
658 bNextPageOdd = !bNextPageOdd;
659 bInsertEmpty = TRUE;
661 ::InsertNewPage( (SwPageDesc&)*pDesc, rpPage->GetUpper(),
662 bNextPageOdd, bInsertEmpty, FALSE, rpPage->GetNext() );
663 if ( bEnd )
665 ASSERT( rpPage->GetNext(), "Keine neue Seite?" );
667 { rpPage = (SwPageFrm*)rpPage->GetNext();
668 } while ( rpPage->GetNext() );
670 else
672 ASSERT( rpPage->GetNext(), "Keine neue Seite?" );
673 rpPage = (SwPageFrm*)rpPage->GetNext();
674 if ( rpPage->IsEmptyPage() )
676 ASSERT( rpPage->GetNext(), "Keine neue Seite?" );
677 rpPage = (SwPageFrm*)rpPage->GetNext();
680 rpLay = rpPage->FindBodyCont();
681 while( rpLay->Lower() )
682 rpLay = (SwLayoutFrm*)rpLay->Lower();
683 return TRUE;
685 return FALSE;
688 // --> OD 2006-03-22 #b6375613#
689 bool lcl_HasTextFrmAnchoredObjs( SwTxtFrm* p_pTxtFrm )
691 bool bHasTextFrmAnchoredObjs( false );
693 const SwSpzFrmFmts* pSpzFrmFmts = p_pTxtFrm->GetTxtNode()->GetDoc()->GetSpzFrmFmts();
694 for ( USHORT i = 0; i < pSpzFrmFmts->Count(); ++i )
696 SwFrmFmt *pFmt = (SwFrmFmt*)(*pSpzFrmFmts)[i];
697 const SwFmtAnchor &rAnch = pFmt->GetAnchor();
698 if ( rAnch.GetCntntAnchor() &&
699 ( rAnch.GetAnchorId() == FLY_AT_CNTNT ||
700 rAnch.GetAnchorId() == FLY_AUTO_CNTNT ) &&
701 rAnch.GetCntntAnchor()->nNode.GetIndex() ==
702 p_pTxtFrm->GetTxtNode()->GetIndex() )
704 bHasTextFrmAnchoredObjs = true;
705 break;
709 return bHasTextFrmAnchoredObjs;
712 void lcl_ApplyWorkaroundForB6375613( SwFrm* p_pFirstFrmOnNewPage )
714 SwTxtFrm* pFirstTextFrmOnNewPage = dynamic_cast<SwTxtFrm*>(p_pFirstFrmOnNewPage);
716 if ( pFirstTextFrmOnNewPage &&
717 !pFirstTextFrmOnNewPage->IsFollow() &&
718 pFirstTextFrmOnNewPage->GetTxt().Len() == 0 &&
719 lcl_HasTextFrmAnchoredObjs( pFirstTextFrmOnNewPage ) )
721 // apply page break before at this text frame to assure, that it doesn't flow backward.
722 const SvxBreak eBreak =
723 pFirstTextFrmOnNewPage->GetAttrSet()->GetBreak().GetBreak();
724 if ( eBreak == SVX_BREAK_NONE )
726 pFirstTextFrmOnNewPage->GetTxtNode()->LockModify();
727 SwDoc* pDoc( pFirstTextFrmOnNewPage->GetTxtNode()->GetDoc() );
728 IDocumentContentOperations* pIDCO = pFirstTextFrmOnNewPage->GetTxtNode()->getIDocumentContentOperations();
729 const SwPaM aTmpPaM( *(pFirstTextFrmOnNewPage->GetTxtNode()) );
730 pIDCO->InsertPoolItem( aTmpPaM,
731 SvxFmtBreakItem( SVX_BREAK_PAGE_BEFORE, RES_BREAK ), 0 );
732 pFirstTextFrmOnNewPage->GetTxtNode()->UnlockModify();
734 uno::Reference< document::XDocumentInfoSupplier > xDoc(
735 pDoc->GetDocShell()->GetBaseModel(),
736 uno::UNO_QUERY);
737 uno::Reference< beans::XPropertySet > xDocInfo(
738 xDoc->getDocumentInfo(),
739 uno::UNO_QUERY );
742 xDocInfo->setPropertyValue( rtl::OUString::createFromAscii("WorkaroundForB6375613Applied"), uno::makeAny( true ) );
744 catch( uno::Exception& )
750 // <--
752 /*-----------------28.5.2001 11:31------------------
753 * SwLayHelper::CheckInsert
754 * is the entry point for the _InsertCnt-function.
755 * The document content index is checked either it is
756 * in the layout cache either it's time to insert a page
757 * cause the maximal estimation of content per page is reached.
758 * A really big table or long paragraph may contains more than
759 * one page, in this case the needed count of pages will inserted.
760 * --------------------------------------------------*/
762 BOOL SwLayHelper::CheckInsert( ULONG nNodeIndex )
764 BOOL bRet = FALSE;
765 BOOL bLongTab = FALSE;
766 ULONG nMaxRowPerPage( 0 );
767 nNodeIndex -= nStartOfContent;
768 USHORT nRows( 0 );
769 if( rpFrm->IsTabFrm() )
771 //Inside a table counts every row as a paragraph
772 SwFrm *pLow = ((SwTabFrm*)rpFrm)->Lower();
773 nRows = 0;
776 ++nRows;
777 pLow = pLow->GetNext();
778 } while ( pLow );
779 nParagraphCnt += nRows;
780 if( !pImpl && nParagraphCnt > nMaxParaPerPage + 10 )
782 // OD 09.04.2003 #108698# - improve heuristics:
783 // Assume that a table, which has more than three times the quantity
784 // of maximal paragraphs per page rows, consists of rows, which have
785 // the height of a normal paragraph. Thus, allow as much rows per page
786 // as much paragraphs are allowed.
787 if ( nRows > ( 3*nMaxParaPerPage ) )
789 nMaxRowPerPage = nMaxParaPerPage;
791 else
793 SwFrm *pTmp = ((SwTabFrm*)rpFrm)->Lower();
794 if( pTmp->GetNext() )
795 pTmp = pTmp->GetNext();
796 pTmp = ((SwRowFrm*)pTmp)->Lower();
797 USHORT nCnt = 0;
800 ++nCnt;
801 pTmp = pTmp->GetNext();
802 } while( pTmp );
803 nMaxRowPerPage = Max( ULONG(2), nMaxParaPerPage / nCnt );
805 bLongTab = TRUE;
808 else
809 ++nParagraphCnt;
810 if( bFirst && pImpl && nIndex < pImpl->Count() &&
811 pImpl->GetBreakIndex( nIndex ) == nNodeIndex &&
812 ( pImpl->GetBreakOfst( nIndex ) < STRING_LEN ||
813 ( ++nIndex < pImpl->Count() &&
814 pImpl->GetBreakIndex( nIndex ) == nNodeIndex ) ) )
815 bFirst = FALSE;
816 #if OSL_DEBUG_LEVEL > 1
817 ULONG nBreakIndex = ( pImpl && nIndex < pImpl->Count() ) ?
818 pImpl->GetBreakIndex(nIndex) : 0xffff;
819 (void)nBreakIndex;
820 #endif
821 // OD 09.04.2003 #108698# - always split a big tables.
822 if ( !bFirst ||
823 ( rpFrm->IsTabFrm() && bLongTab )
826 ULONG nRowCount = 0;
829 if( pImpl || bLongTab )
831 #if OSL_DEBUG_LEVEL > 1
832 ULONG nBrkIndex = ( pImpl && nIndex < pImpl->Count() ) ?
833 pImpl->GetBreakIndex(nIndex) : 0xffff;
834 (void)nBrkIndex;
835 #endif
836 xub_StrLen nOfst = STRING_LEN;
837 USHORT nType = SW_LAYCACHE_IO_REC_PAGES;
838 if( bLongTab )
840 rbBreakAfter = sal_True;
841 nOfst = static_cast<xub_StrLen>(nRowCount + nMaxRowPerPage);
843 else
845 while( nIndex < pImpl->Count() &&
846 pImpl->GetBreakIndex(nIndex) < nNodeIndex)
847 ++nIndex;
848 if( nIndex < pImpl->Count() &&
849 pImpl->GetBreakIndex(nIndex) == nNodeIndex )
851 nType = pImpl->GetBreakType( nIndex );
852 nOfst = pImpl->GetBreakOfst( nIndex++ );
853 rbBreakAfter = sal_True;
857 if( nOfst < STRING_LEN )
859 sal_Bool bSplit = sal_False;
860 USHORT nRepeat( 0 );
861 if( !bLongTab && rpFrm->IsTxtFrm() &&
862 SW_LAYCACHE_IO_REC_PARA == nType &&
863 nOfst<((SwTxtFrm*)rpFrm)->GetTxtNode()->GetTxt().Len() )
864 bSplit = sal_True;
865 else if( rpFrm->IsTabFrm() && nRowCount < nOfst &&
866 ( bLongTab || SW_LAYCACHE_IO_REC_TABLE == nType ) )
868 nRepeat = ((SwTabFrm*)rpFrm)->
869 GetTable()->GetRowsToRepeat();
870 bSplit = nOfst < nRows && nRowCount + nRepeat < nOfst;
871 bLongTab = bLongTab && bSplit;
873 if( bSplit )
875 rpFrm->InsertBehind( rpLay, rpPrv );
876 rpFrm->Frm().Pos() = rpLay->Frm().Pos();
877 rpFrm->Frm().Pos().Y() += 1;
878 rpPrv = rpFrm;
879 if( rpFrm->IsTabFrm() )
881 SwTabFrm* pTab = (SwTabFrm*)rpFrm;
882 // --> OD 2004-09-23 #i33629#, #i29955#
883 ::RegistFlys( pTab->FindPageFrm(), pTab );
884 // <--
885 SwFrm *pRow = pTab->Lower();
886 SwTabFrm *pFoll = new SwTabFrm( *pTab );
888 SwFrm *pPrv;
889 if( nRepeat > 0 )
891 bDontCreateObjects = TRUE; //frmtool
893 // Insert new headlines:
894 USHORT nRowIdx = 0;
895 SwRowFrm* pHeadline = 0;
896 while( nRowIdx < nRepeat )
898 ASSERT( pTab->GetTable()->GetTabLines()[ nRowIdx ], "Table ohne Zeilen?" );
899 pHeadline =
900 new SwRowFrm( *pTab->GetTable()->GetTabLines()[ nRowIdx ] );
901 pHeadline->SetRepeatedHeadline( true );
902 pHeadline->InsertBefore( pFoll, 0 );
903 pHeadline->RegistFlys();
905 ++nRowIdx;
908 bDontCreateObjects = FALSE;
909 pPrv = pHeadline;
910 nRows = nRows + nRepeat;
912 else
913 pPrv = 0;
914 while( pRow && nRowCount < nOfst )
916 pRow = pRow->GetNext();
917 ++nRowCount;
919 while ( pRow )
921 SwFrm* pNxt = pRow->GetNext();
922 pRow->Remove();
923 pRow->InsertBehind( pFoll, pPrv );
924 pPrv = pRow;
925 pRow = pNxt;
927 rpFrm = pFoll;
929 else
931 SwTxtFrm *pNew = new SwTxtFrm( ((SwTxtFrm*)rpFrm)->
932 GetTxtNode() );
933 pNew->_SetIsFollow( sal_True );
934 pNew->ManipOfst( nOfst );
935 pNew->SetFollow( ((SwTxtFrm*)rpFrm)->GetFollow() );
936 ((SwTxtFrm*)rpFrm)->SetFollow( pNew );
937 rpFrm = pNew;
943 SwPageFrm* pLastPage = rpPage;
944 if( CheckInsertPage() )
946 // --> OD 2006-03-21 #b6375613#
947 if ( pDoc->ApplyWorkaroundForB6375613() )
949 lcl_ApplyWorkaroundForB6375613( rpFrm );
951 // <--
952 _CheckFlyCache( pLastPage );
953 if( rpPrv && rpPrv->IsTxtFrm() && !rpPrv->GetValidSizeFlag() )
954 rpPrv->Frm().Height( rpPrv->GetUpper()->Prt().Height() );
956 bRet = TRUE;
957 rpPrv = 0;
958 nParagraphCnt = 0;
960 if ( rpActualSection )
962 //Hatte der SectionFrm ueberhaupt Inhalt? Wenn
963 //nicht kann er gleich umgehaengt werden.
964 SwSectionFrm *pSct;
965 bool bInit = false;
966 if ( !rpActualSection->GetSectionFrm()->ContainsCntnt())
968 pSct = rpActualSection->GetSectionFrm();
969 pSct->Remove();
971 else
973 pSct = new SwSectionFrm(
974 *rpActualSection->GetSectionFrm(), FALSE );
975 rpActualSection->GetSectionFrm()->SimpleFormat();
976 bInit = true;
978 rpActualSection->SetSectionFrm( pSct );
979 pSct->InsertBehind( rpLay, 0 );
980 if( bInit )
981 pSct->Init();
982 pSct->Frm().Pos() = rpLay->Frm().Pos();
983 pSct->Frm().Pos().Y() += 1; //wg. Benachrichtigungen.
985 rpLay = pSct;
986 if ( rpLay->Lower() && rpLay->Lower()->IsLayoutFrm() )
987 rpLay = rpLay->GetNextLayoutLeaf();
990 } while( bLongTab || ( pImpl && nIndex < pImpl->Count() &&
991 (*pImpl)[ nIndex ] == nNodeIndex ) );
993 bFirst = FALSE;
994 return bRet;
997 struct SdrObjectCompare
999 bool operator()( const SdrObject* pF1, const SdrObject* pF2 ) const
1001 return pF1->GetOrdNum() < pF2->GetOrdNum();
1005 struct FlyCacheCompare
1007 bool operator()( const SwFlyCache* pC1, const SwFlyCache* pC2 ) const
1009 return pC1->nOrdNum < pC2->nOrdNum;
1013 /*-----------------28.6.2001 14:40------------------
1014 * SwLayHelper::_CheckFlyCache(..)
1015 * If a new page is inserted, the last page is analysed.
1016 * If there are text frames with default position, the fly cache
1017 * is checked, if these frames are stored in the cache.
1018 * --------------------------------------------------*/
1020 void SwLayHelper::_CheckFlyCache( SwPageFrm* pPage )
1022 if( !pImpl || !pPage )
1023 return;
1024 USHORT nFlyCount = pImpl->GetFlyCount();
1025 // Any text frames at the page, fly cache avaiable?
1026 if( pPage->GetSortedObjs() && nFlyIdx < nFlyCount )
1028 SwSortedObjs &rObjs = *pPage->GetSortedObjs();
1029 USHORT nPgNum = pPage->GetPhyPageNum();
1034 // NOTE: This code assumes that all objects have already been
1035 // inserted into the drawing layout, so that the cached objects
1036 // can be identified by their ordnum. Unfortunately this function
1037 // is called with page n if page n+1 has been inserted. Thus
1038 // not all the objects have been inserted and the ordnums cannot
1039 // be used to identify the objects.
1042 for ( USHORT i = 0; i < rObjs.Count(); ++i ) // check objects
1044 SdrObject *pO = rObjs[i];
1045 if ( pO->ISA(SwVirtFlyDrawObj) ) // a text frame?
1047 SwFlyFrm *pFly = ((SwVirtFlyDrawObj*)pO)->GetFlyFrm();
1048 if( pFly->Frm().Left() == WEIT_WECH && pFly->GetAnchor() &&
1049 !pFly->GetAnchor()->FindFooterOrHeader() )
1050 { // Only frame with default position and not in header/footer
1051 const SwContact *pC = (SwContact*)GetUserCall(pO);
1052 if( pC )
1054 ULONG nOrdNum = pO->GetOrdNum(); // the Id
1055 SwFlyCache* pFlyC;
1056 while( nFlyIdx < nFlyCount && ( pFlyC = pImpl->
1057 GetFlyCache(nFlyIdx) )->nPageNum < nPgNum)
1058 ++nFlyIdx;
1059 if( nFlyIdx < nFlyCount &&
1060 pFlyC->nPageNum == nPgNum )
1062 USHORT nIdx = nFlyIdx;
1063 while( nIdx < nFlyCount && ( pFlyC = pImpl->
1064 GetFlyCache( nIdx ) )->nPageNum == nPgNum &&
1065 pFlyC->nOrdNum != nOrdNum )
1066 ++nIdx;
1067 if( nIdx < nFlyCount && pFlyC->nPageNum == nPgNum &&
1068 pFlyC->nOrdNum == nOrdNum )
1069 { // we get the stored information
1070 pFly->Frm().Pos().X() = pFlyC->Left() +
1071 pPage->Frm().Left();
1072 pFly->Frm().Pos().Y() = pFlyC->Top() +
1073 pPage->Frm().Top();
1074 pFly->Frm().Width( pFlyC->Width() );
1075 pFly->Frm().Height( pFlyC->Height() );
1085 // NOTE: Here we do not use the absolute ordnums but
1086 // relative ordnums for the objects on this page.
1088 // skip fly frames from pages before the current page
1089 SwFlyCache* pFlyC;
1090 while( nFlyIdx < nFlyCount && ( pFlyC = pImpl->
1091 GetFlyCache(nFlyIdx) )->nPageNum < nPgNum)
1092 ++nFlyIdx;
1094 // sort cached objects on this page by ordnum
1095 std::set< const SwFlyCache*, FlyCacheCompare > aFlyCacheSet;
1096 USHORT nIdx = nFlyIdx;
1098 while( nIdx < nFlyCount && ( pFlyC = pImpl->
1099 GetFlyCache( nIdx ) )->nPageNum == nPgNum )
1101 aFlyCacheSet.insert( pFlyC );
1102 ++nIdx;
1105 // sort objects on this page by ordnum
1106 std::set< const SdrObject*, SdrObjectCompare > aFlySet;
1107 for ( USHORT i = 0; i < rObjs.Count(); ++i )
1109 SwAnchoredObject* pAnchoredObj = rObjs[i];
1110 if ( pAnchoredObj->ISA(SwFlyFrm) ) // a text frame?
1112 SwFlyFrm *pFly = static_cast<SwFlyFrm*>(pAnchoredObj);
1113 if( pFly->GetAnchorFrm() &&
1114 !pFly->GetAnchorFrm()->FindFooterOrHeader() )
1116 const SwContact *pC = ::GetUserCall( pAnchoredObj->GetDrawObj() );
1117 if( pC )
1119 aFlySet.insert( pAnchoredObj->GetDrawObj() );
1125 if ( aFlyCacheSet.size() == aFlySet.size() )
1127 std::set< const SwFlyCache*, FlyCacheCompare >::iterator aFlyCacheSetIt =
1128 aFlyCacheSet.begin();
1129 std::set< const SdrObject*, SdrObjectCompare >::iterator aFlySetIt =
1130 aFlySet.begin();
1132 while ( aFlyCacheSetIt != aFlyCacheSet.end() )
1134 const SwFlyCache* pFlyCache = *aFlyCacheSetIt;
1135 SwFlyFrm* pFly = ((SwVirtFlyDrawObj*)*aFlySetIt)->GetFlyFrm();
1137 if ( pFly->Frm().Left() == WEIT_WECH )
1139 // we get the stored information
1140 pFly->Frm().Pos().X() = pFlyCache->Left() +
1141 pPage->Frm().Left();
1142 pFly->Frm().Pos().Y() = pFlyCache->Top() +
1143 pPage->Frm().Top();
1144 if ( pImpl->IsUseFlyCache() )
1146 pFly->Frm().Width( pFlyCache->Width() );
1147 pFly->Frm().Height( pFlyCache->Height() );
1151 ++aFlyCacheSetIt;
1152 ++aFlySetIt;
1158 /*-----------------28.6.2001 14:48------------------
1159 * SwLayHelper::CheckPageFlyCache(..)
1160 * looks for the given text frame in the fly cache and sets
1161 * the position and size, if possible.
1162 * The fly cache is sorted by pages and we start searching with the given page.
1163 * If we found the page number in the fly cache, we set
1164 * the rpPage parameter to the right page, if possible.
1165 * --------------------------------------------------*/
1167 BOOL SwLayHelper::CheckPageFlyCache( SwPageFrm* &rpPage, SwFlyFrm* pFly )
1169 if( !pFly->GetAnchorFrm() || !pFly->GetVirtDrawObj() ||
1170 pFly->GetAnchorFrm()->FindFooterOrHeader() )
1171 return FALSE;
1172 BOOL bRet = FALSE;
1173 SwDoc* pDoc = rpPage->GetFmt()->GetDoc();
1174 SwLayCacheImpl *pCache = pDoc->GetLayoutCache() ?
1175 pDoc->GetLayoutCache()->LockImpl() : NULL;
1176 if( pCache )
1178 USHORT nPgNum = rpPage->GetPhyPageNum();
1179 USHORT nIdx = 0;
1180 USHORT nCnt = pCache->GetFlyCount();
1181 ULONG nOrdNum = pFly->GetVirtDrawObj()->GetOrdNum();
1182 SwFlyCache* pFlyC = 0;
1184 // skip fly frames from pages before the current page
1185 while( nIdx < nCnt &&
1186 nPgNum > (pFlyC = pCache->GetFlyCache( nIdx ))->nPageNum )
1187 ++nIdx;
1189 while( nIdx < nCnt &&
1190 nOrdNum != (pFlyC = pCache->GetFlyCache( nIdx ))->nOrdNum )
1191 ++nIdx;
1192 if( nIdx < nCnt )
1194 SwPageFrm *pPage = rpPage;
1195 while( pPage && pPage->GetPhyPageNum() < pFlyC->nPageNum )
1196 pPage = (SwPageFrm*)pPage->GetNext();
1197 // --> OD 2005-02-22 #i43266# - if the found page is an empty page,
1198 // take the previous one (take next one, if previous one doesn't exists)
1199 if ( pPage && pPage->IsEmptyPage() )
1201 pPage = static_cast<SwPageFrm*>( pPage->GetPrev()
1202 ? pPage->GetPrev()
1203 : pPage->GetNext() );
1205 // <--
1206 if( pPage )
1208 rpPage = pPage;
1209 pFly->Frm().Pos().X() = pFlyC->Left() + pPage->Frm().Left();
1210 pFly->Frm().Pos().Y() = pFlyC->Top() + pPage->Frm().Top();
1211 if ( pCache->IsUseFlyCache() )
1213 pFly->Frm().Width( pFlyC->Width() );
1214 pFly->Frm().Height( pFlyC->Height() );
1216 bRet = TRUE;
1219 pDoc->GetLayoutCache()->UnlockImpl();
1221 return bRet;
1224 // -----------------------------------------------------------------------------
1226 SwLayCacheIoImpl::SwLayCacheIoImpl( SvStream& rStrm, BOOL bWrtMd ) :
1227 pStream( &rStrm ),
1228 nMajorVersion(SW_LAYCACHE_IO_VERSION_MAJOR),
1229 nMinorVersion(SW_LAYCACHE_IO_VERSION_MINOR),
1230 bWriteMode( bWrtMd ),
1231 bError( FALSE )
1233 if( bWriteMode )
1234 *pStream << nMajorVersion
1235 << nMinorVersion;
1237 else
1238 *pStream >> nMajorVersion
1239 >> nMinorVersion;
1242 BOOL SwLayCacheIoImpl::OpenRec( BYTE cType )
1244 BOOL bRes = TRUE;
1245 UINT16 nLvl = aRecTypes.Count();
1246 ASSERT( nLvl == aRecSizes.Count(), "OpenRec: Level" );
1247 UINT32 nPos = pStream->Tell();
1248 if( bWriteMode )
1250 aRecTypes.Insert( cType, nLvl );
1251 aRecSizes.Insert( nPos, nLvl );
1252 *pStream << (UINT32) 0;
1254 else
1256 UINT32 nVal;
1257 *pStream >> nVal;
1258 BYTE cRecTyp = (BYTE)nVal;
1259 aRecTypes.Insert( cRecTyp, nLvl );
1260 sal_uInt32 nSize = nVal >> 8;
1261 aRecSizes.Insert( nPos + nSize, nLvl );
1262 if( !nVal || cRecTyp != cType ||
1263 pStream->GetErrorCode() != SVSTREAM_OK || pStream->IsEof() )
1265 ASSERT( nVal, "OpenRec: Record-Header is 0" );
1266 ASSERT( cRecTyp == cType,
1267 "OpenRec: Wrong Record Type" );
1268 aRecTypes[nLvl] = 0;
1269 aRecSizes[nLvl] = pStream->Tell();
1270 bRes = sal_False;
1271 bError = TRUE;
1274 return bRes;
1277 // Close record
1279 BOOL SwLayCacheIoImpl::CloseRec( BYTE )
1281 BOOL bRes = TRUE;
1282 UINT16 nLvl = aRecTypes.Count();
1283 ASSERT( nLvl == aRecSizes.Count(), "CloseRec: wrong Level" );
1284 ASSERT( nLvl, "CloseRec: no levels" );
1285 if( nLvl )
1287 nLvl--;
1288 UINT32 nPos = pStream->Tell();
1289 if( bWriteMode )
1291 UINT32 nBgn = aRecSizes[nLvl];
1292 pStream->Seek( nBgn );
1293 UINT32 nSize = nPos - nBgn;
1294 UINT32 nVal = ( nSize << 8 ) | aRecTypes[nLvl];
1295 *pStream << nVal;
1296 pStream->Seek( nPos );
1297 if( pStream->GetError() != SVSTREAM_OK )
1298 bRes = FALSE;
1300 else
1302 UINT32 n = aRecSizes[nLvl];
1303 ASSERT( n >= nPos, "CloseRec: to much data read" );
1304 if( n != nPos )
1306 pStream->Seek( n );
1307 if( n < nPos )
1308 bRes = FALSE;
1310 if( pStream->GetErrorCode() != SVSTREAM_OK )
1311 bRes = FALSE;
1314 aRecTypes.Remove( nLvl, 1 );
1315 aRecSizes.Remove( nLvl, 1 );
1318 if( !bRes )
1319 bError = TRUE;
1321 return bRes;
1324 UINT32 SwLayCacheIoImpl::BytesLeft()
1326 UINT16 nLvl = aRecSizes.Count();
1327 UINT32 n = 0;
1328 if( !bError && nLvl )
1330 UINT32 nEndPos = aRecSizes[ nLvl-1 ];
1331 UINT32 nPos = pStream->Tell();
1332 if( nEndPos > nPos )
1333 n = nEndPos - nPos;
1336 return n;
1339 BYTE SwLayCacheIoImpl::Peek()
1341 BYTE c = 0;
1342 if( !bError )
1344 UINT32 nPos = pStream->Tell();
1345 *pStream >> c;
1346 pStream->Seek( nPos );
1347 if( pStream->GetErrorCode() != SVSTREAM_OK )
1349 c = 0;
1350 bError = TRUE;
1353 return c;
1356 void SwLayCacheIoImpl::SkipRec()
1358 BYTE c = Peek();
1359 OpenRec( c );
1360 pStream->Seek( aRecSizes[aRecSizes.Count()-1] );
1361 CloseRec( c );
1364 BYTE SwLayCacheIoImpl::OpenFlagRec()
1366 ASSERT( !bWriteMode, "OpenFlagRec illegal in write mode" );
1367 BYTE cFlags;
1368 *pStream >> cFlags;
1369 nFlagRecEnd = pStream->Tell() + ( cFlags & 0x0F );
1370 return (cFlags >> 4);
1373 void SwLayCacheIoImpl::OpenFlagRec( BYTE nFlags, BYTE nLen )
1375 ASSERT( bWriteMode, "OpenFlagRec illegal in read mode" );
1376 ASSERT( (nFlags & 0xF0) == 0, "illegal flags set" );
1377 ASSERT( nLen < 16, "wrong flag record length" );
1378 BYTE cFlags = (nFlags << 4) + nLen;
1379 *pStream << cFlags;
1380 nFlagRecEnd = pStream->Tell() + nLen;
1383 void SwLayCacheIoImpl::CloseFlagRec()
1385 if( bWriteMode )
1387 ASSERT( pStream->Tell() == nFlagRecEnd, "Wrong amount of data written" );
1389 else
1391 ASSERT( pStream->Tell() <= nFlagRecEnd, "To many data read" );
1392 if( pStream->Tell() != nFlagRecEnd )
1393 pStream->Seek( nFlagRecEnd );