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 <convuno.hxx>
21 #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 <ooo/vba/excel/XlPaperSize.hpp>
34 #include <basic/sberrors.hxx>
35 #include <filter/msfilter/util.hxx>
37 using namespace ::com::sun::star
;
38 using namespace ::ooo::vba
;
43 ScVbaPageSetup::ScVbaPageSetup(const uno::Reference
< XHelperInterface
>& xParent
,
44 const uno::Reference
< uno::XComponentContext
>& xContext
,
45 const uno::Reference
< sheet::XSpreadsheet
>& xSheet
,
46 const uno::Reference
< frame::XModel
>& xModel
):
47 ScVbaPageSetup_BASE( xParent
, xContext
), mxSheet( xSheet
), mbIsLandscape( false )
49 // query for current page style
50 mxModel
.set( xModel
, uno::UNO_SET_THROW
);
51 uno::Reference
< beans::XPropertySet
> xSheetProps( mxSheet
, uno::UNO_QUERY_THROW
);
52 uno::Any aValue
= xSheetProps
->getPropertyValue("PageStyle");
54 aValue
>>= aStyleName
;
56 uno::Reference
< style::XStyleFamiliesSupplier
> xStyleFamiliesSup( mxModel
, uno::UNO_QUERY_THROW
);
57 uno::Reference
< container::XNameAccess
> xStyleFamilies
= xStyleFamiliesSup
->getStyleFamilies();
58 uno::Reference
< container::XNameAccess
> xPageStyle( xStyleFamilies
->getByName("PageStyles"), uno::UNO_QUERY_THROW
);
59 mxPageProps
.set( xPageStyle
->getByName(aStyleName
), uno::UNO_QUERY_THROW
);
60 mnOrientLandscape
= excel::XlPageOrientation::xlLandscape
;
61 mnOrientPortrait
= excel::XlPageOrientation::xlPortrait
;
62 mxPageProps
->getPropertyValue("IsLandscape") >>= mbIsLandscape
;
65 OUString SAL_CALL
ScVbaPageSetup::getPrintArea()
68 uno::Reference
< sheet::XPrintAreas
> xPrintAreas( mxSheet
, uno::UNO_QUERY_THROW
);
69 const uno::Sequence
< table::CellRangeAddress
> aSeq
= xPrintAreas
->getPrintAreas();
70 if( aSeq
.hasElements() )
72 ScRangeList aRangeList
;
73 for( const auto& rRange
: aSeq
)
76 ScUnoConversion::FillScRange( aRange
, rRange
);
77 aRangeList
.push_back( aRange
);
79 ScDocument
& rDoc
= excel::getDocShell( mxModel
)->GetDocument();
80 aRangeList
.Format( aPrintArea
, ScRefFlags::RANGE_ABS
, rDoc
, formula::FormulaGrammar::CONV_XL_A1
, ',' );
86 void SAL_CALL
ScVbaPageSetup::setPrintArea( const OUString
& rAreas
)
88 uno::Reference
< sheet::XPrintAreas
> xPrintAreas( mxSheet
, uno::UNO_QUERY_THROW
);
89 if( rAreas
.isEmpty() ||
90 rAreas
.equalsIgnoreAsciiCase( "FALSE" ) )
92 // print the whole sheet
93 uno::Sequence
< table::CellRangeAddress
> aSeq
;
94 xPrintAreas
->setPrintAreas( aSeq
);
98 ScRangeList aCellRanges
;
100 if( getScRangeListForAddress( rAreas
, excel::getDocShell( mxModel
) , aRange
, aCellRanges
) )
102 uno::Sequence
< table::CellRangeAddress
> aSeq( aCellRanges
.size() );
103 for ( size_t i
= 0, nRanges
= aCellRanges
.size(); i
< nRanges
; ++i
)
105 ScRange
& rRange
= aCellRanges
[ i
];
106 table::CellRangeAddress aRangeAddress
;
107 ScUnoConversion::FillApiRange( aRangeAddress
, rRange
);
108 aSeq
[ i
++ ] = aRangeAddress
;
110 xPrintAreas
->setPrintAreas( aSeq
);
115 double SAL_CALL
ScVbaPageSetup::getHeaderMargin()
117 return VbaPageSetupBase::getHeaderMargin();
120 void SAL_CALL
ScVbaPageSetup::setHeaderMargin( double margin
)
122 VbaPageSetupBase::setHeaderMargin( margin
);
125 double SAL_CALL
ScVbaPageSetup::getFooterMargin()
127 return VbaPageSetupBase::getFooterMargin();
130 void SAL_CALL
ScVbaPageSetup::setFooterMargin( double margin
)
132 VbaPageSetupBase::setFooterMargin( margin
);
135 uno::Any SAL_CALL
ScVbaPageSetup::getFitToPagesTall()
137 return mxPageProps
->getPropertyValue("ScaleToPagesY");
140 void SAL_CALL
ScVbaPageSetup::setFitToPagesTall( const uno::Any
& fitToPagesTall
)
144 sal_uInt16 scaleToPageY
= 0;
146 if( fitToPagesTall
.getValueTypeClass() != uno::TypeClass_BOOLEAN
|| (fitToPagesTall
>>= aValue
))
148 fitToPagesTall
>>= scaleToPageY
;
151 mxPageProps
->setPropertyValue("ScaleToPagesY", uno::makeAny( scaleToPageY
));
153 catch( uno::Exception
& )
158 uno::Any SAL_CALL
ScVbaPageSetup::getFitToPagesWide()
160 return mxPageProps
->getPropertyValue("ScaleToPagesX");
163 void SAL_CALL
ScVbaPageSetup::setFitToPagesWide( const uno::Any
& fitToPagesWide
)
167 sal_uInt16 scaleToPageX
= 0;
169 if( fitToPagesWide
.getValueTypeClass() != uno::TypeClass_BOOLEAN
|| (fitToPagesWide
>>= aValue
))
171 fitToPagesWide
>>= scaleToPageX
;
174 mxPageProps
->setPropertyValue("ScaleToPagesX", uno::makeAny( scaleToPageX
));
176 catch( uno::Exception
& )
181 uno::Any SAL_CALL
ScVbaPageSetup::getZoom()
183 return mxPageProps
->getPropertyValue("PageScale");
186 void SAL_CALL
ScVbaPageSetup::setZoom( const uno::Any
& zoom
)
188 sal_uInt16 pageScale
= 0;
191 if( zoom
.getValueTypeClass() == uno::TypeClass_BOOLEAN
)
197 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_PARAMETER
);
203 if(( pageScale
< ZOOM_IN
)||( pageScale
> ZOOM_MAX
))
205 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_PARAMETER
);
209 // these only exist in S08
210 sal_uInt16 nScale
= 0;
211 mxPageProps
->setPropertyValue("ScaleToPages", uno::makeAny( nScale
));
212 mxPageProps
->setPropertyValue("ScaleToPagesX", uno::makeAny( nScale
));
213 mxPageProps
->setPropertyValue("ScaleToPagesY", uno::makeAny( nScale
));
215 catch (const beans::UnknownPropertyException
&)
219 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_PARAMETER
);
222 catch (const uno::Exception
&)
226 mxPageProps
->setPropertyValue("PageScale", uno::makeAny( pageScale
));
229 OUString SAL_CALL
ScVbaPageSetup::getLeftHeader()
234 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
235 uno::Reference
< text::XText
> xText
= xHeaderContent
->getLeftText();
236 leftHeader
= xText
->getString();
238 catch( uno::Exception
& )
245 void SAL_CALL
ScVbaPageSetup::setLeftHeader( const OUString
& leftHeader
)
249 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
250 uno::Reference
< text::XText
> xText
= xHeaderContent
->getLeftText();
251 xText
->setString( leftHeader
);
252 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
254 catch( uno::Exception
& )
259 OUString SAL_CALL
ScVbaPageSetup::getCenterHeader()
261 OUString centerHeader
;
264 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
265 uno::Reference
< text::XText
> xText
= xHeaderContent
->getCenterText();
266 centerHeader
= xText
->getString();
268 catch( uno::Exception
& )
275 void SAL_CALL
ScVbaPageSetup::setCenterHeader( const OUString
& centerHeader
)
279 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
280 uno::Reference
< text::XText
> xText
= xHeaderContent
->getCenterText();
281 xText
->setString( centerHeader
);
282 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
284 catch( uno::Exception
& )
289 OUString SAL_CALL
ScVbaPageSetup::getRightHeader()
291 OUString rightHeader
;
294 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
295 uno::Reference
< text::XText
> xText
= xHeaderContent
->getRightText();
296 rightHeader
= xText
->getString();
298 catch( uno::Exception
& )
305 void SAL_CALL
ScVbaPageSetup::setRightHeader( const OUString
& rightHeader
)
309 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
310 uno::Reference
< text::XText
> xText
= xHeaderContent
->getRightText();
311 xText
->setString( rightHeader
);
312 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
314 catch( uno::Exception
& )
319 OUString SAL_CALL
ScVbaPageSetup::getLeftFooter()
324 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
325 uno::Reference
< text::XText
> xText
= xFooterContent
->getLeftText();
326 leftFooter
= xText
->getString();
328 catch( uno::Exception
& )
335 void SAL_CALL
ScVbaPageSetup::setLeftFooter( const OUString
& leftFooter
)
339 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
340 uno::Reference
< text::XText
> xText
= xFooterContent
->getLeftText();
341 xText
->setString( leftFooter
);
342 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
344 catch( uno::Exception
& )
349 OUString SAL_CALL
ScVbaPageSetup::getCenterFooter()
351 OUString centerFooter
;
354 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
355 uno::Reference
< text::XText
> xText
= xFooterContent
->getCenterText();
356 centerFooter
= xText
->getString();
358 catch( uno::Exception
& )
365 void SAL_CALL
ScVbaPageSetup::setCenterFooter( const OUString
& centerFooter
)
369 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
370 uno::Reference
< text::XText
> xText
= xFooterContent
->getCenterText();
371 xText
->setString( centerFooter
);
372 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
374 catch( uno::Exception
& )
380 OUString SAL_CALL
ScVbaPageSetup::getRightFooter()
382 OUString rightFooter
;
385 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
386 uno::Reference
< text::XText
> xText
= xFooterContent
->getRightText();
387 rightFooter
= xText
->getString();
389 catch( uno::Exception
& )
396 void SAL_CALL
ScVbaPageSetup::setRightFooter( const OUString
& rightFooter
)
400 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
401 uno::Reference
< text::XText
> xText
= xFooterContent
->getRightText();
402 xText
->setString( rightFooter
);
403 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
405 catch( uno::Exception
& )
410 sal_Int32 SAL_CALL
ScVbaPageSetup::getOrder()
412 sal_Int32 order
= excel::XlOrder::xlDownThenOver
;
415 uno::Any aValue
= mxPageProps
->getPropertyValue("PrintDownFirst");
416 bool bPrintDownFirst
= false;
417 aValue
>>= bPrintDownFirst
;
418 if( !bPrintDownFirst
)
419 order
= excel::XlOrder::xlOverThenDown
;
421 catch( uno::Exception
& )
428 void SAL_CALL
ScVbaPageSetup::setOrder(sal_Int32 order
)
433 case excel::XlOrder::xlDownThenOver
:
435 case excel::XlOrder::xlOverThenDown
:
439 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_PARAMETER
);
444 mxPageProps
->setPropertyValue("PrintDownFirst", uno::makeAny( bOrder
));
446 catch (const uno::Exception
&)
451 sal_Int32 SAL_CALL
ScVbaPageSetup::getFirstPageNumber()
453 sal_Int16 number
= 0;
456 uno::Any aValue
= mxPageProps
->getPropertyValue("FirstPageNumber");
459 catch( uno::Exception
& )
465 number
= excel::Constants::xlAutomatic
;
471 void SAL_CALL
ScVbaPageSetup::setFirstPageNumber( sal_Int32 firstPageNumber
)
473 if( firstPageNumber
== excel::Constants::xlAutomatic
)
479 aValue
<<= static_cast<sal_Int16
>(firstPageNumber
);
480 mxPageProps
->setPropertyValue("FirstPageNumber", aValue
);
482 catch (const uno::Exception
&)
487 sal_Bool SAL_CALL
ScVbaPageSetup::getCenterVertically()
489 bool centerVertically
= false;
492 uno::Any aValue
= mxPageProps
->getPropertyValue("CenterVertically");
493 aValue
>>= centerVertically
;
495 catch (const uno::Exception
&)
498 return centerVertically
;
501 void SAL_CALL
ScVbaPageSetup::setCenterVertically( sal_Bool centerVertically
)
505 mxPageProps
->setPropertyValue("CenterVertically", uno::makeAny( centerVertically
));
507 catch (const uno::Exception
&)
512 sal_Bool SAL_CALL
ScVbaPageSetup::getCenterHorizontally()
514 bool centerHorizontally
= false;
517 uno::Any aValue
= mxPageProps
->getPropertyValue("CenterHorizontally");
518 aValue
>>= centerHorizontally
;
520 catch (const uno::Exception
&)
523 return centerHorizontally
;
526 void SAL_CALL
ScVbaPageSetup::setCenterHorizontally( sal_Bool centerHorizontally
)
530 mxPageProps
->setPropertyValue("CenterHorizontally", uno::makeAny( centerHorizontally
));
532 catch (const uno::Exception
&)
537 sal_Bool SAL_CALL
ScVbaPageSetup::getPrintHeadings()
539 bool printHeadings
= false;
542 uno::Any aValue
= mxPageProps
->getPropertyValue("PrintHeaders");
543 aValue
>>= printHeadings
;
545 catch (const uno::Exception
&)
548 return printHeadings
;
551 void SAL_CALL
ScVbaPageSetup::setPrintHeadings( sal_Bool printHeadings
)
555 mxPageProps
->setPropertyValue("PrintHeaders", uno::makeAny( printHeadings
));
557 catch( uno::Exception
& )
562 sal_Bool SAL_CALL
ScVbaPageSetup::getPrintGridlines()
567 void SAL_CALL
ScVbaPageSetup::setPrintGridlines( sal_Bool
/*_printgridlines*/ )
571 OUString SAL_CALL
ScVbaPageSetup::getPrintTitleRows()
575 void SAL_CALL
ScVbaPageSetup::setPrintTitleRows( const OUString
& /*_printtitlerows*/ )
578 OUString SAL_CALL
ScVbaPageSetup::getPrintTitleColumns()
583 void SAL_CALL
ScVbaPageSetup::setPrintTitleColumns( const OUString
& /*_printtitlecolumns*/ )
587 sal_Int32 SAL_CALL
ScVbaPageSetup::getPaperSize()
589 awt::Size aSize
; // current papersize
590 mxPageProps
->getPropertyValue( "Size" ) >>= aSize
;
592 ::std::swap( aSize
.Width
, aSize
.Height
);
594 sal_Int32 nPaperSizeIndex
= msfilter::util::PaperSizeConv::getMSPaperSizeIndex( aSize
);
595 if ( nPaperSizeIndex
== 0 )
596 nPaperSizeIndex
= excel::XlPaperSize::xlPaperUser
;
597 return nPaperSizeIndex
;
600 void SAL_CALL
ScVbaPageSetup::setPaperSize( sal_Int32 papersize
)
602 if ( papersize
!= excel::XlPaperSize::xlPaperUser
)
604 awt::Size aPaperSize
;
605 const msfilter::util::ApiPaperSize
& rConvertedSize
= msfilter::util::PaperSizeConv::getApiSizeForMSPaperSizeIndex( papersize
);
606 aPaperSize
.Height
= rConvertedSize
.mnHeight
;
607 aPaperSize
.Width
= rConvertedSize
.mnWidth
;
609 ::std::swap( aPaperSize
.Width
, aPaperSize
.Height
);
610 mxPageProps
->setPropertyValue( "Size", uno::makeAny( aPaperSize
) );
615 ScVbaPageSetup::getServiceImplName()
617 return "ScVbaPageSetup";
620 uno::Sequence
< OUString
>
621 ScVbaPageSetup::getServiceNames()
623 static uno::Sequence
< OUString
> const aServiceNames
625 "ooo.vba.excel.PageSetup"
627 return aServiceNames
;
630 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */