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 <com/sun/star/sheet/XPrintAreas.hpp>
25 #include <com/sun/star/sheet/XHeaderFooterContent.hpp>
26 #include <com/sun/star/text/XText.hpp>
27 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <ooo/vba/excel/XlPageOrientation.hpp>
30 #include <ooo/vba/excel/XlOrder.hpp>
31 #include <ooo/vba/excel/Constants.hpp>
32 #include <i18nutil/paper.hxx>
33 #include <sal/macros.h>
35 #include <filter/msfilter/util.hxx>
37 using namespace ::com::sun::star
;
38 using namespace ::ooo::vba
;
43 bool getScRangeListForAddress( const OUString
& sName
, ScDocShell
* pDocSh
, ScRange
& refRange
, ScRangeList
& aCellRanges
, formula::FormulaGrammar::AddressConvention aConv
= formula::FormulaGrammar::CONV_XL_A1
) throw ( uno::RuntimeException
);
45 ScVbaPageSetup::ScVbaPageSetup(const uno::Reference
< XHelperInterface
>& xParent
,
46 const uno::Reference
< uno::XComponentContext
>& xContext
,
47 const uno::Reference
< sheet::XSpreadsheet
>& xSheet
,
48 const uno::Reference
< frame::XModel
>& xModel
) throw (uno::RuntimeException
):
49 ScVbaPageSetup_BASE( xParent
, xContext
), mxSheet( xSheet
), mbIsLandscape( false )
51 // query for current page style
52 mxModel
.set( xModel
, uno::UNO_QUERY_THROW
);
53 uno::Reference
< beans::XPropertySet
> xSheetProps( mxSheet
, uno::UNO_QUERY_THROW
);
54 uno::Any aValue
= xSheetProps
->getPropertyValue("PageStyle");
56 aValue
>>= aStyleName
;
58 uno::Reference
< style::XStyleFamiliesSupplier
> xStyleFamiliesSup( mxModel
, uno::UNO_QUERY_THROW
);
59 uno::Reference
< container::XNameAccess
> xStyleFamilies
= xStyleFamiliesSup
->getStyleFamilies();
60 uno::Reference
< container::XNameAccess
> xPageStyle( xStyleFamilies
->getByName("PageStyles"), uno::UNO_QUERY_THROW
);
61 mxPageProps
.set( xPageStyle
->getByName(aStyleName
), uno::UNO_QUERY_THROW
);
62 mnOrientLandscape
= excel::XlPageOrientation::xlLandscape
;
63 mnOrientPortrait
= excel::XlPageOrientation::xlPortrait
;
64 mxPageProps
->getPropertyValue("IsLandscape") >>= mbIsLandscape
;
67 OUString SAL_CALL
ScVbaPageSetup::getPrintArea() throw (css::uno::RuntimeException
)
70 uno::Reference
< sheet::XPrintAreas
> xPrintAreas( mxSheet
, uno::UNO_QUERY_THROW
);
71 uno::Sequence
< table::CellRangeAddress
> aSeq
= xPrintAreas
->getPrintAreas();
72 sal_Int32 nCount
= aSeq
.getLength();
75 ScAddress::Details
aDetails( formula::FormulaGrammar::CONV_XL_A1
, 0, 0 );
76 sal_uInt16 nFlags
= SCA_VALID
;
77 nFlags
|= ( SCA_TAB_ABSOLUTE
| SCA_COL_ABSOLUTE
| SCA_ROW_ABSOLUTE
| SCA_TAB2_ABSOLUTE
| SCA_COL2_ABSOLUTE
| SCA_ROW2_ABSOLUTE
);
78 ScRangeList aRangeList
;
79 for( sal_Int32 i
=0; i
<nCount
; i
++ )
82 ScUnoConversion::FillScRange( aRange
, aSeq
[i
] );
83 aRangeList
.Append( aRange
);
85 ScDocument
* pDoc
= excel::getDocShell( mxModel
)->GetDocument();
86 aRangeList
.Format( aPrintArea
, nFlags
, pDoc
, formula::FormulaGrammar::CONV_XL_A1
, ',' );
92 void SAL_CALL
ScVbaPageSetup::setPrintArea( const OUString
& rAreas
) throw (css::uno::RuntimeException
)
94 uno::Reference
< sheet::XPrintAreas
> xPrintAreas( mxSheet
, uno::UNO_QUERY_THROW
);
95 if( rAreas
.isEmpty() ||
96 rAreas
.equalsIgnoreAsciiCase ( OUString("FALSE") ) )
98 // print the whole sheet
99 uno::Sequence
< table::CellRangeAddress
> aSeq
;
100 xPrintAreas
->setPrintAreas( aSeq
);
104 ScRangeList aCellRanges
;
106 if( getScRangeListForAddress( rAreas
, excel::getDocShell( mxModel
) , aRange
, aCellRanges
) )
108 uno::Sequence
< table::CellRangeAddress
> aSeq( aCellRanges
.size() );
109 for ( size_t i
= 0, nRanges
= aCellRanges
.size(); i
< nRanges
; ++i
)
111 ScRange
* pRange
= aCellRanges
[ i
];
112 table::CellRangeAddress aRangeAddress
;
113 ScUnoConversion::FillApiRange( aRangeAddress
, *pRange
);
114 aSeq
[ i
++ ] = aRangeAddress
;
116 xPrintAreas
->setPrintAreas( aSeq
);
121 double SAL_CALL
ScVbaPageSetup::getHeaderMargin() throw (css::uno::RuntimeException
)
123 return VbaPageSetupBase::getHeaderMargin();
126 void SAL_CALL
ScVbaPageSetup::setHeaderMargin( double margin
) throw (css::uno::RuntimeException
)
128 VbaPageSetupBase::setHeaderMargin( margin
);
131 double SAL_CALL
ScVbaPageSetup::getFooterMargin() throw (css::uno::RuntimeException
)
133 return VbaPageSetupBase::getFooterMargin();
136 void SAL_CALL
ScVbaPageSetup::setFooterMargin( double margin
) throw (css::uno::RuntimeException
)
138 VbaPageSetupBase::setFooterMargin( margin
);
141 uno::Any SAL_CALL
ScVbaPageSetup::getFitToPagesTall() throw (css::uno::RuntimeException
)
143 return mxPageProps
->getPropertyValue("ScaleToPagesY");
146 void SAL_CALL
ScVbaPageSetup::setFitToPagesTall( const uno::Any
& fitToPagesTall
) throw (css::uno::RuntimeException
)
148 sal_uInt16 scaleToPageY
= 0;
152 if( fitToPagesTall
.getValueTypeClass() != uno::TypeClass_BOOLEAN
|| (fitToPagesTall
>>= aValue
))
154 fitToPagesTall
>>= scaleToPageY
;
157 mxPageProps
->setPropertyValue("ScaleToPagesY", uno::makeAny( scaleToPageY
));
159 catch( uno::Exception
& )
164 uno::Any SAL_CALL
ScVbaPageSetup::getFitToPagesWide() throw (css::uno::RuntimeException
)
166 return mxPageProps
->getPropertyValue("ScaleToPagesX");
169 void SAL_CALL
ScVbaPageSetup::setFitToPagesWide( const uno::Any
& fitToPagesWide
) throw (css::uno::RuntimeException
)
171 sal_uInt16 scaleToPageX
= 0;
174 sal_Bool aValue
= false;
175 if( fitToPagesWide
.getValueTypeClass() != uno::TypeClass_BOOLEAN
|| (fitToPagesWide
>>= aValue
))
177 fitToPagesWide
>>= scaleToPageX
;
180 mxPageProps
->setPropertyValue("ScaleToPagesX", uno::makeAny( scaleToPageX
));
182 catch( uno::Exception
& )
187 uno::Any SAL_CALL
ScVbaPageSetup::getZoom() throw (css::uno::RuntimeException
)
189 return mxPageProps
->getPropertyValue("PageScale");
192 void SAL_CALL
ScVbaPageSetup::setZoom( const uno::Any
& zoom
) throw (css::uno::RuntimeException
)
194 sal_uInt16 pageScale
= 0;
197 if( zoom
.getValueTypeClass() == uno::TypeClass_BOOLEAN
)
199 sal_Bool aValue
= false;
203 DebugHelper::exception(SbERR_BAD_PARAMETER
, OUString() );
209 if(( pageScale
< ZOOM_IN
)||( pageScale
> ZOOM_MAX
))
211 DebugHelper::exception(SbERR_BAD_PARAMETER
, OUString() );
215 // these only exist in S08
216 sal_uInt16 nScale
= 0;
217 mxPageProps
->setPropertyValue("ScaleToPages", uno::makeAny( nScale
));
218 mxPageProps
->setPropertyValue("ScaleToPagesX", uno::makeAny( nScale
));
219 mxPageProps
->setPropertyValue("ScaleToPagesY", uno::makeAny( nScale
));
221 catch( beans::UnknownPropertyException
& )
225 DebugHelper::exception(SbERR_BAD_PARAMETER
, OUString() );
228 catch( uno::Exception
& )
232 mxPageProps
->setPropertyValue("PageScale", uno::makeAny( pageScale
));
235 OUString SAL_CALL
ScVbaPageSetup::getLeftHeader() throw (css::uno::RuntimeException
)
240 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
241 if( xHeaderContent
.is() )
243 uno::Reference
< text::XText
> xText
= xHeaderContent
->getLeftText();
244 leftHeader
= xText
->getString();
247 catch( uno::Exception
& )
254 void SAL_CALL
ScVbaPageSetup::setLeftHeader( const OUString
& leftHeader
) throw (css::uno::RuntimeException
)
258 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
259 if( xHeaderContent
.is() )
261 uno::Reference
< text::XText
> xText
= xHeaderContent
->getLeftText();
262 xText
->setString( leftHeader
);
263 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
266 catch( uno::Exception
& )
271 OUString SAL_CALL
ScVbaPageSetup::getCenterHeader() throw (css::uno::RuntimeException
)
273 OUString centerHeader
;
276 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
277 if( xHeaderContent
.is() )
279 uno::Reference
< text::XText
> xText
= xHeaderContent
->getCenterText();
280 centerHeader
= xText
->getString();
283 catch( uno::Exception
& )
290 void SAL_CALL
ScVbaPageSetup::setCenterHeader( const OUString
& centerHeader
) throw (css::uno::RuntimeException
)
294 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
295 if( xHeaderContent
.is() )
297 uno::Reference
< text::XText
> xText
= xHeaderContent
->getCenterText();
298 xText
->setString( centerHeader
);
299 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
302 catch( uno::Exception
& )
307 OUString SAL_CALL
ScVbaPageSetup::getRightHeader() throw (css::uno::RuntimeException
)
309 OUString rightHeader
;
312 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
313 if( xHeaderContent
.is() )
315 uno::Reference
< text::XText
> xText
= xHeaderContent
->getRightText();
316 rightHeader
= xText
->getString();
319 catch( uno::Exception
& )
326 void SAL_CALL
ScVbaPageSetup::setRightHeader( const OUString
& rightHeader
) throw (css::uno::RuntimeException
)
330 uno::Reference
<sheet::XHeaderFooterContent
> xHeaderContent( mxPageProps
->getPropertyValue("RightPageHeaderContent"), uno::UNO_QUERY_THROW
);
331 if( xHeaderContent
.is() )
333 uno::Reference
< text::XText
> xText
= xHeaderContent
->getRightText();
334 xText
->setString( rightHeader
);
335 mxPageProps
->setPropertyValue("RightPageHeaderContent", uno::makeAny(xHeaderContent
) );
338 catch( uno::Exception
& )
343 OUString SAL_CALL
ScVbaPageSetup::getLeftFooter() throw (css::uno::RuntimeException
)
348 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
349 if( xFooterContent
.is() )
351 uno::Reference
< text::XText
> xText
= xFooterContent
->getLeftText();
352 leftFooter
= xText
->getString();
355 catch( uno::Exception
& )
362 void SAL_CALL
ScVbaPageSetup::setLeftFooter( const OUString
& leftFooter
) throw (css::uno::RuntimeException
)
366 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
367 if( xFooterContent
.is() )
369 uno::Reference
< text::XText
> xText
= xFooterContent
->getLeftText();
370 xText
->setString( leftFooter
);
371 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
374 catch( uno::Exception
& )
379 OUString SAL_CALL
ScVbaPageSetup::getCenterFooter() throw (css::uno::RuntimeException
)
381 OUString centerFooter
;
384 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
385 if( xFooterContent
.is() )
387 uno::Reference
< text::XText
> xText
= xFooterContent
->getCenterText();
388 centerFooter
= xText
->getString();
391 catch( uno::Exception
& )
398 void SAL_CALL
ScVbaPageSetup::setCenterFooter( const OUString
& centerFooter
) throw (css::uno::RuntimeException
)
402 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
403 if( xFooterContent
.is() )
405 uno::Reference
< text::XText
> xText
= xFooterContent
->getCenterText();
406 xText
->setString( centerFooter
);
407 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
410 catch( uno::Exception
& )
416 OUString SAL_CALL
ScVbaPageSetup::getRightFooter() throw (css::uno::RuntimeException
)
418 OUString rightFooter
;
421 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
422 if( xFooterContent
.is() )
424 uno::Reference
< text::XText
> xText
= xFooterContent
->getRightText();
425 rightFooter
= xText
->getString();
428 catch( uno::Exception
& )
435 void SAL_CALL
ScVbaPageSetup::setRightFooter( const OUString
& rightFooter
) throw (css::uno::RuntimeException
)
439 uno::Reference
<sheet::XHeaderFooterContent
> xFooterContent( mxPageProps
->getPropertyValue("RightPageFooterContent"), uno::UNO_QUERY_THROW
);
440 if( xFooterContent
.is() )
442 uno::Reference
< text::XText
> xText
= xFooterContent
->getRightText();
443 xText
->setString( rightFooter
);
444 mxPageProps
->setPropertyValue("RightPageFooterContent", uno::makeAny(xFooterContent
) );
447 catch( uno::Exception
& )
452 sal_Int32 SAL_CALL
ScVbaPageSetup::getOrder() throw (css::uno::RuntimeException
)
454 sal_Int32 order
= excel::XlOrder::xlDownThenOver
;
457 uno::Any aValue
= mxPageProps
->getPropertyValue("PrintDownFirst");
458 sal_Bool bPrintDownFirst
= false;
459 aValue
>>= bPrintDownFirst
;
460 if( !bPrintDownFirst
)
461 order
= excel::XlOrder::xlOverThenDown
;
463 catch( uno::Exception
& )
470 void SAL_CALL
ScVbaPageSetup::setOrder( sal_Int32 order
) throw (css::uno::RuntimeException
)
472 sal_Bool bOrder
= sal_True
;
475 case excel::XlOrder::xlDownThenOver
:
477 case excel::XlOrder::xlOverThenDown
:
481 DebugHelper::exception(SbERR_BAD_PARAMETER
, OUString() );
486 mxPageProps
->setPropertyValue("PrintDownFirst", uno::makeAny( bOrder
));
488 catch( uno::Exception
& )
493 sal_Int32 SAL_CALL
ScVbaPageSetup::getFirstPageNumber() throw (css::uno::RuntimeException
)
495 sal_Int16 number
= 0;
498 uno::Any aValue
= mxPageProps
->getPropertyValue("FirstPageNumber");
501 catch( uno::Exception
& )
507 number
= excel::Constants::xlAutomatic
;
513 void SAL_CALL
ScVbaPageSetup::setFirstPageNumber( sal_Int32 firstPageNumber
) throw (css::uno::RuntimeException
)
515 if( firstPageNumber
== excel::Constants::xlAutomatic
)
521 aValue
<<= (sal_Int16
)firstPageNumber
;
522 mxPageProps
->setPropertyValue("FirstPageNumber", aValue
);
524 catch( uno::Exception
& )
529 sal_Bool SAL_CALL
ScVbaPageSetup::getCenterVertically() throw (css::uno::RuntimeException
)
531 sal_Bool centerVertically
= false;
534 uno::Any aValue
= mxPageProps
->getPropertyValue("CenterVertically");
535 aValue
>>= centerVertically
;
537 catch( uno::Exception
& )
540 return centerVertically
;
543 void SAL_CALL
ScVbaPageSetup::setCenterVertically( sal_Bool centerVertically
) throw (css::uno::RuntimeException
)
547 mxPageProps
->setPropertyValue("CenterVertically", uno::makeAny( centerVertically
));
549 catch( uno::Exception
& )
554 sal_Bool SAL_CALL
ScVbaPageSetup::getCenterHorizontally() throw (css::uno::RuntimeException
)
556 sal_Bool centerHorizontally
= false;
559 uno::Any aValue
= mxPageProps
->getPropertyValue("CenterHorizontally");
560 aValue
>>= centerHorizontally
;
562 catch( uno::Exception
& )
565 return centerHorizontally
;
568 void SAL_CALL
ScVbaPageSetup::setCenterHorizontally( sal_Bool centerHorizontally
) throw (css::uno::RuntimeException
)
572 mxPageProps
->setPropertyValue("CenterHorizontally", uno::makeAny( centerHorizontally
));
574 catch( uno::Exception
& )
579 sal_Bool SAL_CALL
ScVbaPageSetup::getPrintHeadings() throw (css::uno::RuntimeException
)
581 sal_Bool printHeadings
= false;
584 uno::Any aValue
= mxPageProps
->getPropertyValue("PrintHeaders");
585 aValue
>>= printHeadings
;
587 catch( uno::Exception
& )
590 return printHeadings
;
593 void SAL_CALL
ScVbaPageSetup::setPrintHeadings( sal_Bool printHeadings
) throw (css::uno::RuntimeException
)
597 mxPageProps
->setPropertyValue("PrintHeaders", uno::makeAny( printHeadings
));
599 catch( uno::Exception
& )
604 ::sal_Bool SAL_CALL
ScVbaPageSetup::getPrintGridlines() throw (uno::RuntimeException
)
609 void SAL_CALL
ScVbaPageSetup::setPrintGridlines( ::sal_Bool
/*_printgridlines*/ ) throw (uno::RuntimeException
)
613 OUString SAL_CALL
ScVbaPageSetup::getPrintTitleRows() throw (uno::RuntimeException
)
617 void SAL_CALL
ScVbaPageSetup::setPrintTitleRows( const OUString
& /*_printtitlerows*/ ) throw (css::uno::RuntimeException
)
620 OUString SAL_CALL
ScVbaPageSetup::getPrintTitleColumns() throw (uno::RuntimeException
)
625 void SAL_CALL
ScVbaPageSetup::setPrintTitleColumns( const OUString
& /*_printtitlecolumns*/ ) throw (uno::RuntimeException
)
629 sal_Int32 SAL_CALL
ScVbaPageSetup::getPaperSize() throw (uno::RuntimeException
)
631 awt::Size aSize
; // current papersize
632 mxPageProps
->getPropertyValue( "Size" ) >>= aSize
;
634 ::std::swap( aSize
.Width
, aSize
.Height
);
636 sal_Int32 nPaperSizeIndex
= msfilter::util::PaperSizeConv::getMSPaperSizeIndex( aSize
);
637 if ( nPaperSizeIndex
== 0 )
638 nPaperSizeIndex
= excel::XlPaperSize::xlPaperUser
;
639 return nPaperSizeIndex
;
642 void SAL_CALL
ScVbaPageSetup::setPaperSize( sal_Int32 papersize
) throw (uno::RuntimeException
)
644 if ( papersize
!= excel::XlPaperSize::xlPaperUser
)
646 awt::Size aPaperSize
;
647 const msfilter::util::ApiPaperSize
& rConvertedSize
= msfilter::util::PaperSizeConv::getApiSizeForMSPaperSizeIndex( papersize
);
648 aPaperSize
.Height
= rConvertedSize
.mnHeight
;
649 aPaperSize
.Width
= rConvertedSize
.mnWidth
;
651 ::std::swap( aPaperSize
.Width
, aPaperSize
.Height
);
652 mxPageProps
->setPropertyValue( "Size", uno::makeAny( aPaperSize
) );
657 ScVbaPageSetup::getServiceImplName()
659 return OUString("ScVbaPageSetup");
662 uno::Sequence
< OUString
>
663 ScVbaPageSetup::getServiceNames()
665 static uno::Sequence
< OUString
> aServiceNames
;
666 if ( aServiceNames
.getLength() == 0 )
668 aServiceNames
.realloc( 1 );
669 aServiceNames
[ 0 ] = "ooo.vba.excel.PageSetup";
671 return aServiceNames
;
674 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */