1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "vbapagesetup.hxx"
20 #include "cellsuno.hxx"
21 #include "convuno.hxx"
22 #include "rangelst.hxx"
23 #include "excelvbahelper.hxx"
24 #include "vbarange.hxx"
25 #include <com/sun/star/sheet/XPrintAreas.hpp>
26 #include <com/sun/star/sheet/XHeaderFooterContent.hpp>
27 #include <com/sun/star/text/XText.hpp>
28 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 #include <ooo/vba/excel/XlPageOrientation.hpp>
31 #include <ooo/vba/excel/XlOrder.hpp>
32 #include <ooo/vba/excel/Constants.hpp>
33 #include <i18nutil/paper.hxx>
34 #include <sal/macros.h>
36 #include <filter/msfilter/util.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::ooo::vba
;
44 ScVbaPageSetup::ScVbaPageSetup(const uno::Reference
< XHelperInterface
>& xParent
,
45 const uno::Reference
< uno::XComponentContext
>& xContext
,
46 const uno::Reference
< sheet::XSpreadsheet
>& xSheet
,
47 const uno::Reference
< frame::XModel
>& xModel
) throw (uno::RuntimeException
):
48 ScVbaPageSetup_BASE( xParent
, xContext
), mxSheet( xSheet
), mbIsLandscape( false )
50 // query for current page style
51 mxModel
.set( xModel
, uno::UNO_QUERY_THROW
);
52 uno::Reference
< beans::XPropertySet
> xSheetProps( mxSheet
, uno::UNO_QUERY_THROW
);
53 uno::Any aValue
= xSheetProps
->getPropertyValue("PageStyle");
55 aValue
>>= aStyleName
;
57 uno::Reference
< style::XStyleFamiliesSupplier
> xStyleFamiliesSup( mxModel
, uno::UNO_QUERY_THROW
);
58 uno::Reference
< container::XNameAccess
> xStyleFamilies
= xStyleFamiliesSup
->getStyleFamilies();
59 uno::Reference
< container::XNameAccess
> xPageStyle( xStyleFamilies
->getByName("PageStyles"), uno::UNO_QUERY_THROW
);
60 mxPageProps
.set( xPageStyle
->getByName(aStyleName
), uno::UNO_QUERY_THROW
);
61 mnOrientLandscape
= excel::XlPageOrientation::xlLandscape
;
62 mnOrientPortrait
= excel::XlPageOrientation::xlPortrait
;
63 mxPageProps
->getPropertyValue("IsLandscape") >>= mbIsLandscape
;
66 OUString SAL_CALL
ScVbaPageSetup::getPrintArea() throw (css::uno::RuntimeException
, std::exception
)
69 uno::Reference
< sheet::XPrintAreas
> xPrintAreas( mxSheet
, uno::UNO_QUERY_THROW
);
70 uno::Sequence
< table::CellRangeAddress
> aSeq
= xPrintAreas
->getPrintAreas();
71 sal_Int32 nCount
= aSeq
.getLength();
74 ScAddress::Details
aDetails( formula::FormulaGrammar::CONV_XL_A1
, 0, 0 );
75 sal_uInt16 nFlags
= SCA_VALID
;
76 nFlags
|= ( SCA_TAB_ABSOLUTE
| SCA_COL_ABSOLUTE
| SCA_ROW_ABSOLUTE
| SCA_TAB2_ABSOLUTE
| SCA_COL2_ABSOLUTE
| SCA_ROW2_ABSOLUTE
);
77 ScRangeList aRangeList
;
78 for( sal_Int32 i
=0; i
<nCount
; i
++ )
81 ScUnoConversion::FillScRange( aRange
, aSeq
[i
] );
82 aRangeList
.Append( aRange
);
84 ScDocument
& rDoc
= excel::getDocShell( mxModel
)->GetDocument();
85 aRangeList
.Format( aPrintArea
, nFlags
, &rDoc
, formula::FormulaGrammar::CONV_XL_A1
, ',' );
91 void SAL_CALL
ScVbaPageSetup::setPrintArea( const OUString
& rAreas
) throw (css::uno::RuntimeException
, std::exception
)
93 uno::Reference
< sheet::XPrintAreas
> xPrintAreas( mxSheet
, uno::UNO_QUERY_THROW
);
94 if( rAreas
.isEmpty() ||
95 rAreas
.equalsIgnoreAsciiCase( "FALSE" ) )
97 // print the whole sheet
98 uno::Sequence
< table::CellRangeAddress
> aSeq
;
99 xPrintAreas
->setPrintAreas( aSeq
);
103 ScRangeList aCellRanges
;
105 if( getScRangeListForAddress( rAreas
, excel::getDocShell( mxModel
) , aRange
, aCellRanges
) )
107 uno::Sequence
< table::CellRangeAddress
> aSeq( aCellRanges
.size() );
108 for ( size_t i
= 0, nRanges
= aCellRanges
.size(); i
< nRanges
; ++i
)
110 ScRange
* pRange
= aCellRanges
[ i
];
111 table::CellRangeAddress aRangeAddress
;
112 ScUnoConversion::FillApiRange( aRangeAddress
, *pRange
);
113 aSeq
[ i
++ ] = aRangeAddress
;
115 xPrintAreas
->setPrintAreas( aSeq
);
120 double SAL_CALL
ScVbaPageSetup::getHeaderMargin() throw (css::uno::RuntimeException
)
122 return VbaPageSetupBase::getHeaderMargin();
125 void SAL_CALL
ScVbaPageSetup::setHeaderMargin( double margin
) throw (css::uno::RuntimeException
)
127 VbaPageSetupBase::setHeaderMargin( margin
);
130 double SAL_CALL
ScVbaPageSetup::getFooterMargin() throw (css::uno::RuntimeException
)
132 return VbaPageSetupBase::getFooterMargin();
135 void SAL_CALL
ScVbaPageSetup::setFooterMargin( double margin
) throw (css::uno::RuntimeException
)
137 VbaPageSetupBase::setFooterMargin( margin
);
140 uno::Any SAL_CALL
ScVbaPageSetup::getFitToPagesTall() throw (css::uno::RuntimeException
, std::exception
)
142 return mxPageProps
->getPropertyValue("ScaleToPagesY");
145 void SAL_CALL
ScVbaPageSetup::setFitToPagesTall( const uno::Any
& fitToPagesTall
) throw (css::uno::RuntimeException
, std::exception
)
147 sal_uInt16 scaleToPageY
= 0;
151 if( fitToPagesTall
.getValueTypeClass() != uno::TypeClass_BOOLEAN
|| (fitToPagesTall
>>= aValue
))
153 fitToPagesTall
>>= scaleToPageY
;
156 mxPageProps
->setPropertyValue("ScaleToPagesY", uno::makeAny( scaleToPageY
));
158 catch( uno::Exception
& )
163 uno::Any SAL_CALL
ScVbaPageSetup::getFitToPagesWide() throw (css::uno::RuntimeException
, std::exception
)
165 return mxPageProps
->getPropertyValue("ScaleToPagesX");
168 void SAL_CALL
ScVbaPageSetup::setFitToPagesWide( const uno::Any
& fitToPagesWide
) throw (css::uno::RuntimeException
, std::exception
)
170 sal_uInt16 scaleToPageX
= 0;
174 if( fitToPagesWide
.getValueTypeClass() != uno::TypeClass_BOOLEAN
|| (fitToPagesWide
>>= aValue
))
176 fitToPagesWide
>>= scaleToPageX
;
179 mxPageProps
->setPropertyValue("ScaleToPagesX", uno::makeAny( scaleToPageX
));
181 catch( uno::Exception
& )
186 uno::Any SAL_CALL
ScVbaPageSetup::getZoom() throw (css::uno::RuntimeException
, std::exception
)
188 return mxPageProps
->getPropertyValue("PageScale");
191 void SAL_CALL
ScVbaPageSetup::setZoom( const uno::Any
& zoom
) throw (css::uno::RuntimeException
, std::exception
)
193 sal_uInt16 pageScale
= 0;
196 if( zoom
.getValueTypeClass() == uno::TypeClass_BOOLEAN
)
202 DebugHelper::runtimeexception(SbERR_BAD_PARAMETER
, OUString() );
208 if(( pageScale
< ZOOM_IN
)||( pageScale
> ZOOM_MAX
))
210 DebugHelper::runtimeexception(SbERR_BAD_PARAMETER
, OUString() );
214 // these only exist in S08
215 sal_uInt16 nScale
= 0;
216 mxPageProps
->setPropertyValue("ScaleToPages", uno::makeAny( nScale
));
217 mxPageProps
->setPropertyValue("ScaleToPagesX", uno::makeAny( nScale
));
218 mxPageProps
->setPropertyValue("ScaleToPagesY", uno::makeAny( nScale
));
220 catch (const beans::UnknownPropertyException
&)
224 DebugHelper::runtimeexception(SbERR_BAD_PARAMETER
, OUString() );
227 catch (const uno::Exception
&)
231 mxPageProps
->setPropertyValue("PageScale", uno::makeAny( pageScale
));
234 OUString SAL_CALL
ScVbaPageSetup::getLeftHeader() throw (css::uno::RuntimeException
, std::exception
)
239 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
240 if( xHeaderContent
.is() )
242 uno::Reference
< text::XText
> xText
= xHeaderContent
->getLeftText();
243 leftHeader
= xText
->getString();
246 catch( uno::Exception
& )
253 void SAL_CALL
ScVbaPageSetup::setLeftHeader( const OUString
& leftHeader
) throw (css::uno::RuntimeException
, std::exception
)
257 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
258 if( xHeaderContent
.is() )
260 uno::Reference
< text::XText
> xText
= xHeaderContent
->getLeftText();
261 xText
->setString( leftHeader
);
262 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
265 catch( uno::Exception
& )
270 OUString SAL_CALL
ScVbaPageSetup::getCenterHeader() throw (css::uno::RuntimeException
, std::exception
)
272 OUString centerHeader
;
275 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
276 if( xHeaderContent
.is() )
278 uno::Reference
< text::XText
> xText
= xHeaderContent
->getCenterText();
279 centerHeader
= xText
->getString();
282 catch( uno::Exception
& )
289 void SAL_CALL
ScVbaPageSetup::setCenterHeader( const OUString
& centerHeader
) throw (css::uno::RuntimeException
, std::exception
)
293 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
294 if( xHeaderContent
.is() )
296 uno::Reference
< text::XText
> xText
= xHeaderContent
->getCenterText();
297 xText
->setString( centerHeader
);
298 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
301 catch( uno::Exception
& )
306 OUString SAL_CALL
ScVbaPageSetup::getRightHeader() throw (css::uno::RuntimeException
, std::exception
)
308 OUString rightHeader
;
311 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
312 if( xHeaderContent
.is() )
314 uno::Reference
< text::XText
> xText
= xHeaderContent
->getRightText();
315 rightHeader
= xText
->getString();
318 catch( uno::Exception
& )
325 void SAL_CALL
ScVbaPageSetup::setRightHeader( const OUString
& rightHeader
) throw (css::uno::RuntimeException
, std::exception
)
329 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
330 if( xHeaderContent
.is() )
332 uno::Reference
< text::XText
> xText
= xHeaderContent
->getRightText();
333 xText
->setString( rightHeader
);
334 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
337 catch( uno::Exception
& )
342 OUString SAL_CALL
ScVbaPageSetup::getLeftFooter() throw (css::uno::RuntimeException
, std::exception
)
347 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
348 if( xFooterContent
.is() )
350 uno::Reference
< text::XText
> xText
= xFooterContent
->getLeftText();
351 leftFooter
= xText
->getString();
354 catch( uno::Exception
& )
361 void SAL_CALL
ScVbaPageSetup::setLeftFooter( const OUString
& leftFooter
) throw (css::uno::RuntimeException
, std::exception
)
365 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
366 if( xFooterContent
.is() )
368 uno::Reference
< text::XText
> xText
= xFooterContent
->getLeftText();
369 xText
->setString( leftFooter
);
370 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
373 catch( uno::Exception
& )
378 OUString SAL_CALL
ScVbaPageSetup::getCenterFooter() throw (css::uno::RuntimeException
, std::exception
)
380 OUString centerFooter
;
383 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
384 if( xFooterContent
.is() )
386 uno::Reference
< text::XText
> xText
= xFooterContent
->getCenterText();
387 centerFooter
= xText
->getString();
390 catch( uno::Exception
& )
397 void SAL_CALL
ScVbaPageSetup::setCenterFooter( const OUString
& centerFooter
) throw (css::uno::RuntimeException
, std::exception
)
401 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
402 if( xFooterContent
.is() )
404 uno::Reference
< text::XText
> xText
= xFooterContent
->getCenterText();
405 xText
->setString( centerFooter
);
406 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
409 catch( uno::Exception
& )
415 OUString SAL_CALL
ScVbaPageSetup::getRightFooter() throw (css::uno::RuntimeException
, std::exception
)
417 OUString rightFooter
;
420 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
421 if( xFooterContent
.is() )
423 uno::Reference
< text::XText
> xText
= xFooterContent
->getRightText();
424 rightFooter
= xText
->getString();
427 catch( uno::Exception
& )
434 void SAL_CALL
ScVbaPageSetup::setRightFooter( const OUString
& rightFooter
) throw (css::uno::RuntimeException
, std::exception
)
438 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
439 if( xFooterContent
.is() )
441 uno::Reference
< text::XText
> xText
= xFooterContent
->getRightText();
442 xText
->setString( rightFooter
);
443 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
446 catch( uno::Exception
& )
451 sal_Int32 SAL_CALL
ScVbaPageSetup::getOrder() throw (css::uno::RuntimeException
, std::exception
)
453 sal_Int32 order
= excel::XlOrder::xlDownThenOver
;
456 uno::Any aValue
= mxPageProps
->getPropertyValue("PrintDownFirst");
457 bool bPrintDownFirst
= false;
458 aValue
>>= bPrintDownFirst
;
459 if( !bPrintDownFirst
)
460 order
= excel::XlOrder::xlOverThenDown
;
462 catch( uno::Exception
& )
469 void SAL_CALL
ScVbaPageSetup::setOrder(sal_Int32 order
) throw (css::uno::RuntimeException
, std::exception
)
474 case excel::XlOrder::xlDownThenOver
:
476 case excel::XlOrder::xlOverThenDown
:
480 DebugHelper::runtimeexception(SbERR_BAD_PARAMETER
, OUString() );
485 mxPageProps
->setPropertyValue("PrintDownFirst", uno::makeAny( bOrder
));
487 catch (const uno::Exception
&)
492 sal_Int32 SAL_CALL
ScVbaPageSetup::getFirstPageNumber() throw (css::uno::RuntimeException
, std::exception
)
494 sal_Int16 number
= 0;
497 uno::Any aValue
= mxPageProps
->getPropertyValue("FirstPageNumber");
500 catch( uno::Exception
& )
506 number
= excel::Constants::xlAutomatic
;
512 void SAL_CALL
ScVbaPageSetup::setFirstPageNumber( sal_Int32 firstPageNumber
) throw (css::uno::RuntimeException
, std::exception
)
514 if( firstPageNumber
== excel::Constants::xlAutomatic
)
520 aValue
<<= (sal_Int16
)firstPageNumber
;
521 mxPageProps
->setPropertyValue("FirstPageNumber", aValue
);
523 catch (const uno::Exception
&)
528 sal_Bool SAL_CALL
ScVbaPageSetup::getCenterVertically() throw (css::uno::RuntimeException
, std::exception
)
530 bool centerVertically
= false;
533 uno::Any aValue
= mxPageProps
->getPropertyValue("CenterVertically");
534 aValue
>>= centerVertically
;
536 catch (const uno::Exception
&)
539 return centerVertically
;
542 void SAL_CALL
ScVbaPageSetup::setCenterVertically( sal_Bool centerVertically
) throw (css::uno::RuntimeException
, std::exception
)
546 mxPageProps
->setPropertyValue("CenterVertically", uno::makeAny( centerVertically
));
548 catch (const uno::Exception
&)
553 sal_Bool SAL_CALL
ScVbaPageSetup::getCenterHorizontally() throw (css::uno::RuntimeException
, std::exception
)
555 bool centerHorizontally
= false;
558 uno::Any aValue
= mxPageProps
->getPropertyValue("CenterHorizontally");
559 aValue
>>= centerHorizontally
;
561 catch (const uno::Exception
&)
564 return centerHorizontally
;
567 void SAL_CALL
ScVbaPageSetup::setCenterHorizontally( sal_Bool centerHorizontally
) throw (css::uno::RuntimeException
, std::exception
)
571 mxPageProps
->setPropertyValue("CenterHorizontally", uno::makeAny( centerHorizontally
));
573 catch (const uno::Exception
&)
578 sal_Bool SAL_CALL
ScVbaPageSetup::getPrintHeadings() throw (css::uno::RuntimeException
, std::exception
)
580 bool printHeadings
= false;
583 uno::Any aValue
= mxPageProps
->getPropertyValue("PrintHeaders");
584 aValue
>>= printHeadings
;
586 catch (const uno::Exception
&)
589 return printHeadings
;
592 void SAL_CALL
ScVbaPageSetup::setPrintHeadings( sal_Bool printHeadings
) throw (css::uno::RuntimeException
, std::exception
)
596 mxPageProps
->setPropertyValue("PrintHeaders", uno::makeAny( printHeadings
));
598 catch( uno::Exception
& )
603 sal_Bool SAL_CALL
ScVbaPageSetup::getPrintGridlines() throw (uno::RuntimeException
, std::exception
)
608 void SAL_CALL
ScVbaPageSetup::setPrintGridlines( sal_Bool
/*_printgridlines*/ ) throw (uno::RuntimeException
, std::exception
)
612 OUString SAL_CALL
ScVbaPageSetup::getPrintTitleRows() throw (uno::RuntimeException
, std::exception
)
616 void SAL_CALL
ScVbaPageSetup::setPrintTitleRows( const OUString
& /*_printtitlerows*/ ) throw (css::uno::RuntimeException
, std::exception
)
619 OUString SAL_CALL
ScVbaPageSetup::getPrintTitleColumns() throw (uno::RuntimeException
, std::exception
)
624 void SAL_CALL
ScVbaPageSetup::setPrintTitleColumns( const OUString
& /*_printtitlecolumns*/ ) throw (uno::RuntimeException
, std::exception
)
628 sal_Int32 SAL_CALL
ScVbaPageSetup::getPaperSize() throw (uno::RuntimeException
, std::exception
)
630 awt::Size aSize
; // current papersize
631 mxPageProps
->getPropertyValue( "Size" ) >>= aSize
;
633 ::std::swap( aSize
.Width
, aSize
.Height
);
635 sal_Int32 nPaperSizeIndex
= msfilter::util::PaperSizeConv::getMSPaperSizeIndex( aSize
);
636 if ( nPaperSizeIndex
== 0 )
637 nPaperSizeIndex
= excel::XlPaperSize::xlPaperUser
;
638 return nPaperSizeIndex
;
641 void SAL_CALL
ScVbaPageSetup::setPaperSize( sal_Int32 papersize
) throw (uno::RuntimeException
, std::exception
)
643 if ( papersize
!= excel::XlPaperSize::xlPaperUser
)
645 awt::Size aPaperSize
;
646 const msfilter::util::ApiPaperSize
& rConvertedSize
= msfilter::util::PaperSizeConv::getApiSizeForMSPaperSizeIndex( papersize
);
647 aPaperSize
.Height
= rConvertedSize
.mnHeight
;
648 aPaperSize
.Width
= rConvertedSize
.mnWidth
;
650 ::std::swap( aPaperSize
.Width
, aPaperSize
.Height
);
651 mxPageProps
->setPropertyValue( "Size", uno::makeAny( aPaperSize
) );
656 ScVbaPageSetup::getServiceImplName()
658 return OUString("ScVbaPageSetup");
661 uno::Sequence
< OUString
>
662 ScVbaPageSetup::getServiceNames()
664 static uno::Sequence
< OUString
> aServiceNames
;
665 if ( aServiceNames
.getLength() == 0 )
667 aServiceNames
.realloc( 1 );
668 aServiceNames
[ 0 ] = "ooo.vba.excel.PageSetup";
670 return aServiceNames
;
673 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */