1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xipage.cxx,v $
10 * $Revision: 1.18.90.3 $
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_sc.hxx"
34 #include <svtools/itemset.hxx>
35 #include <vcl/graph.hxx>
36 #include "scitems.hxx"
37 #include <svtools/eitem.hxx>
38 #include <svtools/intitem.hxx>
39 #include <svx/pageitem.hxx>
40 #include <svx/sizeitem.hxx>
41 #include <svx/lrspitem.hxx>
42 #include <svx/ulspitem.hxx>
43 #include <svx/brshitem.hxx>
44 #include "document.hxx"
45 #include "stlsheet.hxx"
47 #include "xistream.hxx"
48 #include "xihelper.hxx"
49 #include "xiescher.hxx"
51 // Page settings ==============================================================
53 XclImpPageSettings::XclImpPageSettings( const XclImpRoot
& rRoot
) :
59 void XclImpPageSettings::Initialize()
65 void XclImpPageSettings::ReadSetup( XclImpStream
& rStrm
)
67 DBG_ASSERT_BIFF( GetBiff() >= EXC_BIFF4
);
68 if( GetBiff() < EXC_BIFF4
)
73 rStrm
>> maData
.mnPaperSize
>> maData
.mnScaling
>> maData
.mnStartPage
74 >> maData
.mnFitToWidth
>> maData
.mnFitToHeight
>> nFlags
;
76 mbValidPaper
= maData
.mbValid
= !::get_flag( nFlags
, EXC_SETUP_INVALID
);
77 maData
.mbPrintInRows
= ::get_flag( nFlags
, EXC_SETUP_INROWS
);
78 maData
.mbPortrait
= ::get_flag( nFlags
, EXC_SETUP_PORTRAIT
);
79 maData
.mbBlackWhite
= ::get_flag( nFlags
, EXC_SETUP_BLACKWHITE
);
80 maData
.mbManualStart
= true;
82 // new in BIFF5 - BIFF8
83 if( GetBiff() >= EXC_BIFF5
)
85 rStrm
>> maData
.mnHorPrintRes
>> maData
.mnVerPrintRes
86 >> maData
.mfHeaderMargin
>> maData
.mfFooterMargin
>> maData
.mnCopies
;
88 maData
.mbDraftQuality
= ::get_flag( nFlags
, EXC_SETUP_DRAFT
);
89 maData
.mbPrintNotes
= ::get_flag( nFlags
, EXC_SETUP_PRINTNOTES
);
90 maData
.mbManualStart
= ::get_flag( nFlags
, EXC_SETUP_STARTPAGE
);
94 void XclImpPageSettings::ReadMargin( XclImpStream
& rStrm
)
96 switch( rStrm
.GetRecId() )
98 case EXC_ID_LEFTMARGIN
: rStrm
>> maData
.mfLeftMargin
; break;
99 case EXC_ID_RIGHTMARGIN
: rStrm
>> maData
.mfRightMargin
; break;
100 case EXC_ID_TOPMARGIN
: rStrm
>> maData
.mfTopMargin
; break;
101 case EXC_ID_BOTTOMMARGIN
: rStrm
>> maData
.mfBottomMargin
; break;
102 default: DBG_ERRORFILE( "XclImpPageSettings::ReadMargin - unknown record" );
106 void XclImpPageSettings::ReadCenter( XclImpStream
& rStrm
)
108 DBG_ASSERT_BIFF( GetBiff() >= EXC_BIFF3
); // read it anyway
109 bool bCenter
= (rStrm
.ReaduInt16() != 0);
110 switch( rStrm
.GetRecId() )
112 case EXC_ID_HCENTER
: maData
.mbHorCenter
= bCenter
; break;
113 case EXC_ID_VCENTER
: maData
.mbVerCenter
= bCenter
; break;
114 default: DBG_ERRORFILE( "XclImpPageSettings::ReadCenter - unknown record" );
118 void XclImpPageSettings::ReadHeaderFooter( XclImpStream
& rStrm
)
121 if( rStrm
.GetRecLeft() )
122 aString
= (GetBiff() <= EXC_BIFF5
) ? rStrm
.ReadByteString( false ) : rStrm
.ReadUniString();
124 switch( rStrm
.GetRecId() )
126 case EXC_ID_HEADER
: maData
.maHeader
= aString
; break;
127 case EXC_ID_FOOTER
: maData
.maFooter
= aString
; break;
128 default: DBG_ERRORFILE( "XclImpPageSettings::ReadHeaderFooter - unknown record" );
132 void XclImpPageSettings::ReadPageBreaks( XclImpStream
& rStrm
)
134 ScfUInt16Vec
* pVec
= 0;
135 switch( rStrm
.GetRecId() )
137 case EXC_ID_HORPAGEBREAKS
: pVec
= &maData
.maHorPageBreaks
; break;
138 case EXC_ID_VERPAGEBREAKS
: pVec
= &maData
.maVerPageBreaks
; break;
139 default: DBG_ERRORFILE( "XclImpPageSettings::ReadPageBreaks - unknown record" );
144 bool bIgnore
= GetBiff() == EXC_BIFF8
; // ignore start/end columns or rows in BIFF8
146 sal_uInt16 nCount
, nBreak
;
149 pVec
->reserve( nCount
);
155 pVec
->push_back( nBreak
);
162 void XclImpPageSettings::ReadPrintHeaders( XclImpStream
& rStrm
)
164 maData
.mbPrintHeadings
= (rStrm
.ReaduInt16() != 0);
167 void XclImpPageSettings::ReadPrintGridLines( XclImpStream
& rStrm
)
169 maData
.mbPrintGrid
= (rStrm
.ReaduInt16() != 0);
172 void XclImpPageSettings::ReadImgData( XclImpStream
& rStrm
)
174 Graphic aGraphic
= XclImpObjectManager::ReadImgData( rStrm
);
175 if( aGraphic
.GetType() != GRAPHIC_NONE
)
176 maData
.mxBrushItem
.reset( new SvxBrushItem( aGraphic
, GPOS_TILED
, ATTR_BACKGROUND
) );
179 void XclImpPageSettings::SetPaperSize( sal_uInt16 nXclPaperSize
, bool bPortrait
)
181 maData
.mnPaperSize
= nXclPaperSize
;
182 maData
.mbPortrait
= bPortrait
;
186 // ----------------------------------------------------------------------------
190 void lclPutMarginItem( SfxItemSet
& rItemSet
, sal_uInt16 nRecId
, double fMarginInch
)
192 sal_uInt16 nMarginTwips
= XclTools::GetTwipsFromInch( fMarginInch
);
195 case EXC_ID_TOPMARGIN
:
196 case EXC_ID_BOTTOMMARGIN
:
198 SvxULSpaceItem
aItem( GETITEM( rItemSet
, SvxULSpaceItem
, ATTR_ULSPACE
) );
199 if( nRecId
== EXC_ID_TOPMARGIN
)
200 aItem
.SetUpperValue( nMarginTwips
);
202 aItem
.SetLowerValue( nMarginTwips
);
203 rItemSet
.Put( aItem
);
206 case EXC_ID_LEFTMARGIN
:
207 case EXC_ID_RIGHTMARGIN
:
209 SvxLRSpaceItem
aItem( GETITEM( rItemSet
, SvxLRSpaceItem
, ATTR_LRSPACE
) );
210 if( nRecId
== EXC_ID_LEFTMARGIN
)
211 aItem
.SetLeftValue( nMarginTwips
);
213 aItem
.SetRightValue( nMarginTwips
);
214 rItemSet
.Put( aItem
);
218 DBG_ERRORFILE( "XclImpPageSettings::SetMarginItem - unknown record id" );
224 void XclImpPageSettings::Finalize()
226 ScDocument
& rDoc
= GetDoc();
227 SCTAB nScTab
= GetCurrScTab();
229 // *** create page style sheet ***
231 String
aStyleName( RTL_CONSTASCII_USTRINGPARAM( "PageStyle_" ) );
233 if( GetDoc().GetName( nScTab
, aTableName
) )
234 aStyleName
.Append( aTableName
);
236 aStyleName
.Append( String::CreateFromInt32( nScTab
+ 1 ) );
238 ScStyleSheet
& rStyleSheet
= ScfTools::MakePageStyleSheet( GetStyleSheetPool(), aStyleName
, false );
239 SfxItemSet
& rItemSet
= rStyleSheet
.GetItemSet();
241 // *** page settings ***
243 ScfTools::PutItem( rItemSet
, SfxBoolItem( ATTR_PAGE_TOPDOWN
, !maData
.mbPrintInRows
), true );
244 ScfTools::PutItem( rItemSet
, SfxBoolItem( ATTR_PAGE_HORCENTER
, maData
.mbHorCenter
), true );
245 ScfTools::PutItem( rItemSet
, SfxBoolItem( ATTR_PAGE_VERCENTER
, maData
.mbVerCenter
), true );
246 ScfTools::PutItem( rItemSet
, SfxBoolItem( ATTR_PAGE_HEADERS
, maData
.mbPrintHeadings
), true );
247 ScfTools::PutItem( rItemSet
, SfxBoolItem( ATTR_PAGE_GRID
, maData
.mbPrintGrid
), true );
248 ScfTools::PutItem( rItemSet
, SfxBoolItem( ATTR_PAGE_NOTES
, maData
.mbPrintNotes
), true );
250 sal_uInt16 nStartPage
= maData
.mbManualStart
? maData
.mnStartPage
: 0;
251 ScfTools::PutItem( rItemSet
, SfxUInt16Item( ATTR_PAGE_FIRSTPAGENO
, nStartPage
), true );
253 if( maData
.mxBrushItem
.get() )
254 rItemSet
.Put( *maData
.mxBrushItem
);
258 SvxPageItem
aPageItem( GETITEM( rItemSet
, SvxPageItem
, ATTR_PAGE
) );
259 aPageItem
.SetLandscape( !maData
.mbPortrait
);
260 rItemSet
.Put( aPageItem
);
261 ScfTools::PutItem( rItemSet
, SvxSizeItem( ATTR_PAGE_SIZE
, maData
.GetScPaperSize() ), true );
264 if( maData
.mbFitToPages
)
265 rItemSet
.Put( ScPageScaleToItem( maData
.mnFitToWidth
, maData
.mnFitToHeight
) );
266 else if( maData
.mbValid
)
267 rItemSet
.Put( SfxUInt16Item( ATTR_PAGE_SCALE
, maData
.mnScaling
) );
269 // *** margin preparations ***
271 double fLeftMargin
= maData
.mfLeftMargin
;
272 double fRightMargin
= maData
.mfRightMargin
;
273 double fTopMargin
= maData
.mfTopMargin
;
274 double fBottomMargin
= maData
.mfBottomMargin
;
275 // distances between header/footer and page area
276 double fHeaderHeight
= 0.0;
277 double fHeaderDist
= 0.0;
278 double fFooterHeight
= 0.0;
279 double fFooterDist
= 0.0;
280 // in Calc, "header/footer left/right margin" is X distance between header/footer and page margin
281 double fHdrLeftMargin
= maData
.mfHdrLeftMargin
- maData
.mfLeftMargin
;
282 double fHdrRightMargin
= maData
.mfHdrRightMargin
- maData
.mfRightMargin
;
283 double fFtrLeftMargin
= maData
.mfFtrLeftMargin
- maData
.mfLeftMargin
;
284 double fFtrRightMargin
= maData
.mfFtrRightMargin
- maData
.mfRightMargin
;
286 // *** header and footer ***
288 XclImpHFConverter
aHFConv( GetRoot() );
291 bool bHasHeader
= (maData
.maHeader
.Len() != 0);
292 SvxSetItem
aHdrSetItem( GETITEM( rItemSet
, SvxSetItem
, ATTR_PAGE_HEADERSET
) );
293 SfxItemSet
& rHdrItemSet
= aHdrSetItem
.GetItemSet();
294 rHdrItemSet
.Put( SfxBoolItem( ATTR_PAGE_ON
, bHasHeader
) );
297 aHFConv
.ParseString( maData
.maHeader
);
298 aHFConv
.FillToItemSet( rItemSet
, ATTR_PAGE_HEADERLEFT
);
299 aHFConv
.FillToItemSet( rItemSet
, ATTR_PAGE_HEADERRIGHT
);
300 // #i23296# In Calc, "top margin" is distance to header
301 fTopMargin
= maData
.mfHeaderMargin
;
302 // Calc uses distance between header and sheet data area
303 fHeaderHeight
= XclTools::GetInchFromTwips( aHFConv
.GetTotalHeight() );
304 fHeaderDist
= maData
.mfTopMargin
- maData
.mfHeaderMargin
- fHeaderHeight
;
306 if( fHeaderDist
< 0.0 )
308 /* #i23296# Header overlays sheet data:
309 -> set fixed header height to get correct sheet data position. */
310 ScfTools::PutItem( rHdrItemSet
, SfxBoolItem( ATTR_PAGE_DYNAMIC
, false ), true );
311 // shrink header height
312 long nHdrHeight
= XclTools::GetTwipsFromInch( fHeaderHeight
+ fHeaderDist
);
313 ScfTools::PutItem( rHdrItemSet
, SvxSizeItem( ATTR_PAGE_SIZE
, Size( 0, nHdrHeight
) ), true );
314 lclPutMarginItem( rHdrItemSet
, EXC_ID_BOTTOMMARGIN
, 0.0 );
318 // use dynamic header height
319 ScfTools::PutItem( rHdrItemSet
, SfxBoolItem( ATTR_PAGE_DYNAMIC
, true ), true );
320 lclPutMarginItem( rHdrItemSet
, EXC_ID_BOTTOMMARGIN
, fHeaderDist
);
322 lclPutMarginItem( rHdrItemSet
, EXC_ID_LEFTMARGIN
, fHdrLeftMargin
);
323 lclPutMarginItem( rHdrItemSet
, EXC_ID_RIGHTMARGIN
, fHdrRightMargin
);
324 rItemSet
.Put( aHdrSetItem
);
327 bool bHasFooter
= (maData
.maFooter
.Len() != 0);
328 SvxSetItem
aFtrSetItem( GETITEM( rItemSet
, SvxSetItem
, ATTR_PAGE_FOOTERSET
) );
329 SfxItemSet
& rFtrItemSet
= aFtrSetItem
.GetItemSet();
330 rFtrItemSet
.Put( SfxBoolItem( ATTR_PAGE_ON
, bHasFooter
) );
333 aHFConv
.ParseString( maData
.maFooter
);
334 aHFConv
.FillToItemSet( rItemSet
, ATTR_PAGE_FOOTERLEFT
);
335 aHFConv
.FillToItemSet( rItemSet
, ATTR_PAGE_FOOTERRIGHT
);
336 // #i23296# In Calc, "bottom margin" is distance to footer
337 fBottomMargin
= maData
.mfFooterMargin
;
338 // Calc uses distance between footer and sheet data area
339 fFooterHeight
= XclTools::GetInchFromTwips( aHFConv
.GetTotalHeight() );
340 fFooterDist
= maData
.mfBottomMargin
- maData
.mfFooterMargin
- fFooterHeight
;
342 if( fFooterDist
< 0.0 )
344 /* #i23296# Footer overlays sheet data:
345 -> set fixed footer height to get correct sheet data end position. */
346 ScfTools::PutItem( rFtrItemSet
, SfxBoolItem( ATTR_PAGE_DYNAMIC
, false ), true );
347 // shrink footer height
348 long nFtrHeight
= XclTools::GetTwipsFromInch( fFooterHeight
+ fFooterDist
);
349 ScfTools::PutItem( rFtrItemSet
, SvxSizeItem( ATTR_PAGE_SIZE
, Size( 0, nFtrHeight
) ), true );
350 lclPutMarginItem( rFtrItemSet
, EXC_ID_TOPMARGIN
, 0.0 );
354 // use dynamic footer height
355 ScfTools::PutItem( rFtrItemSet
, SfxBoolItem( ATTR_PAGE_DYNAMIC
, true ), true );
356 lclPutMarginItem( rFtrItemSet
, EXC_ID_TOPMARGIN
, fFooterDist
);
358 lclPutMarginItem( rFtrItemSet
, EXC_ID_LEFTMARGIN
, fFtrLeftMargin
);
359 lclPutMarginItem( rFtrItemSet
, EXC_ID_RIGHTMARGIN
, fFtrRightMargin
);
360 rItemSet
.Put( aFtrSetItem
);
362 // *** set final margins ***
364 lclPutMarginItem( rItemSet
, EXC_ID_LEFTMARGIN
, fLeftMargin
);
365 lclPutMarginItem( rItemSet
, EXC_ID_RIGHTMARGIN
, fRightMargin
);
366 lclPutMarginItem( rItemSet
, EXC_ID_TOPMARGIN
, fTopMargin
);
367 lclPutMarginItem( rItemSet
, EXC_ID_BOTTOMMARGIN
, fBottomMargin
);
369 // *** put style sheet into document ***
371 rDoc
.SetPageStyle( nScTab
, rStyleSheet
.GetName() );
373 // *** page breaks ***
375 ScfUInt16Vec::const_iterator aIt
, aEnd
;
377 for( aIt
= maData
.maHorPageBreaks
.begin(), aEnd
= maData
.maHorPageBreaks
.end(); aIt
!= aEnd
; ++aIt
)
379 SCROW nScRow
= static_cast< SCROW
>( *aIt
);
380 if( nScRow
<= MAXROW
)
381 rDoc
.SetRowBreak(nScRow
, nScTab
, false, true);
384 for( aIt
= maData
.maVerPageBreaks
.begin(), aEnd
= maData
.maVerPageBreaks
.end(); aIt
!= aEnd
; ++aIt
)
386 SCCOL nScCol
= static_cast< SCCOL
>( *aIt
);
387 if( nScCol
<= MAXCOL
)
388 rDoc
.SetColBreak(nScCol
, nScTab
, false, true);
392 // ============================================================================