tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / vba / vbapagesetup.cxx
blob009570e32dca2f32ad81d8cc954f49fe9c77d6ca
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <docsh.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>
36 #include <utility>
38 using namespace ::com::sun::star;
39 using namespace ::ooo::vba;
41 #define ZOOM_IN 10
42 #define ZOOM_MAX 400
44 ScVbaPageSetup::ScVbaPageSetup(const uno::Reference< XHelperInterface >& xParent,
45 const uno::Reference< uno::XComponentContext >& xContext,
46 uno::Reference< sheet::XSpreadsheet > xSheet,
47 const uno::Reference< frame::XModel >& xModel):
48 ScVbaPageSetup_BASE( xParent, xContext ), mxSheet(std::move( xSheet )), mbIsLandscape( false )
50 // query for current page style
51 mxModel.set( xModel, uno::UNO_SET_THROW );
52 uno::Reference< beans::XPropertySet > xSheetProps( mxSheet, uno::UNO_QUERY_THROW );
53 uno::Any aValue = xSheetProps->getPropertyValue(u"PageStyle"_ustr);
54 OUString aStyleName;
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(u"PageStyles"_ustr), 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(u"IsLandscape"_ustr) >>= mbIsLandscape;
66 OUString SAL_CALL ScVbaPageSetup::getPrintArea()
68 OUString aPrintArea;
69 uno::Reference< sheet::XPrintAreas > xPrintAreas( mxSheet, uno::UNO_QUERY_THROW );
70 const uno::Sequence< table::CellRangeAddress > aSeq = xPrintAreas->getPrintAreas();
71 if( aSeq.hasElements() )
73 ScRangeList aRangeList;
74 for( const auto& rRange : aSeq )
76 ScRange aRange;
77 ScUnoConversion::FillScRange( aRange, rRange );
78 aRangeList.push_back( aRange );
80 if ( ScDocShell* pShell = excel::getDocShell( mxModel ))
82 ScDocument& rDoc = pShell->GetDocument();
83 aRangeList.Format( aPrintArea, ScRefFlags::RANGE_ABS, rDoc, formula::FormulaGrammar::CONV_XL_A1, ',' );
87 return aPrintArea;
90 void SAL_CALL ScVbaPageSetup::setPrintArea( const OUString& rAreas )
92 uno::Reference< sheet::XPrintAreas > xPrintAreas( mxSheet, uno::UNO_QUERY_THROW );
93 if( rAreas.isEmpty() ||
94 rAreas.equalsIgnoreAsciiCase( "FALSE" ) )
96 // print the whole sheet
97 uno::Sequence< table::CellRangeAddress > aSeq;
98 xPrintAreas->setPrintAreas( aSeq );
100 else
102 ScRangeList aCellRanges;
103 ScRange aRange;
104 if( getScRangeListForAddress( rAreas, excel::getDocShell( mxModel ) , aRange, aCellRanges ) )
106 uno::Sequence< table::CellRangeAddress > aSeq( aCellRanges.size() );
107 auto aSeqRange = asNonConstRange(aSeq);
108 for ( size_t i = 0, nRanges = aCellRanges.size(); i < nRanges; ++i )
110 ScRange & rRange = aCellRanges[ i ];
111 table::CellRangeAddress aRangeAddress;
112 ScUnoConversion::FillApiRange( aRangeAddress, rRange );
113 aSeqRange[ i++ ] = aRangeAddress;
115 xPrintAreas->setPrintAreas( aSeq );
120 double SAL_CALL ScVbaPageSetup::getHeaderMargin()
122 return VbaPageSetupBase::getHeaderMargin();
125 void SAL_CALL ScVbaPageSetup::setHeaderMargin( double margin )
127 VbaPageSetupBase::setHeaderMargin( margin );
130 double SAL_CALL ScVbaPageSetup::getFooterMargin()
132 return VbaPageSetupBase::getFooterMargin();
135 void SAL_CALL ScVbaPageSetup::setFooterMargin( double margin )
137 VbaPageSetupBase::setFooterMargin( margin );
140 uno::Any SAL_CALL ScVbaPageSetup::getFitToPagesTall()
142 return mxPageProps->getPropertyValue(u"ScaleToPagesY"_ustr);
145 void SAL_CALL ScVbaPageSetup::setFitToPagesTall( const uno::Any& fitToPagesTall)
149 sal_uInt16 scaleToPageY = 0;
150 bool aValue;
151 if( fitToPagesTall.getValueTypeClass() != uno::TypeClass_BOOLEAN || (fitToPagesTall >>= aValue))
153 fitToPagesTall >>= scaleToPageY;
156 mxPageProps->setPropertyValue(u"ScaleToPagesY"_ustr, uno::Any( scaleToPageY ));
158 catch( uno::Exception& )
163 uno::Any SAL_CALL ScVbaPageSetup::getFitToPagesWide()
165 return mxPageProps->getPropertyValue(u"ScaleToPagesX"_ustr);
168 void SAL_CALL ScVbaPageSetup::setFitToPagesWide( const uno::Any& fitToPagesWide)
172 sal_uInt16 scaleToPageX = 0;
173 bool aValue = false;
174 if( fitToPagesWide.getValueTypeClass() != uno::TypeClass_BOOLEAN || (fitToPagesWide >>= aValue))
176 fitToPagesWide >>= scaleToPageX;
179 mxPageProps->setPropertyValue(u"ScaleToPagesX"_ustr, uno::Any( scaleToPageX ));
181 catch( uno::Exception& )
186 uno::Any SAL_CALL ScVbaPageSetup::getZoom()
188 return mxPageProps->getPropertyValue(u"PageScale"_ustr);
191 void SAL_CALL ScVbaPageSetup::setZoom( const uno::Any& zoom)
193 sal_uInt16 pageScale = 0;
196 if( zoom.getValueTypeClass() == uno::TypeClass_BOOLEAN )
198 bool aValue = false;
199 zoom >>= aValue;
200 if( aValue )
202 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_PARAMETER);
205 else
207 zoom >>= pageScale;
208 if(( pageScale < ZOOM_IN )||( pageScale > ZOOM_MAX ))
210 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_PARAMETER);
214 // these only exist in S08
215 sal_uInt16 nScale = 0;
216 mxPageProps->setPropertyValue(u"ScaleToPages"_ustr, uno::Any( nScale ));
217 mxPageProps->setPropertyValue(u"ScaleToPagesX"_ustr, uno::Any( nScale ));
218 mxPageProps->setPropertyValue(u"ScaleToPagesY"_ustr, uno::Any( nScale ));
220 catch (const beans::UnknownPropertyException&)
222 if( pageScale == 0 )
224 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_PARAMETER);
227 catch (const uno::Exception&)
231 mxPageProps->setPropertyValue(u"PageScale"_ustr, uno::Any( pageScale ));
234 OUString SAL_CALL ScVbaPageSetup::getLeftHeader()
236 OUString leftHeader;
239 uno::Reference<sheet::XHeaderFooterContent> xHeaderContent( mxPageProps->getPropertyValue(u"RightPageHeaderContent"_ustr), uno::UNO_QUERY_THROW);
240 uno::Reference< text::XText > xText = xHeaderContent->getLeftText();
241 leftHeader = xText->getString();
243 catch( uno::Exception& )
247 return leftHeader;
250 void SAL_CALL ScVbaPageSetup::setLeftHeader( const OUString& leftHeader)
254 uno::Reference<sheet::XHeaderFooterContent> xHeaderContent( mxPageProps->getPropertyValue(u"RightPageHeaderContent"_ustr), uno::UNO_QUERY_THROW);
255 uno::Reference< text::XText > xText = xHeaderContent->getLeftText();
256 xText->setString( leftHeader );
257 mxPageProps->setPropertyValue(u"RightPageHeaderContent"_ustr, uno::Any(xHeaderContent) );
259 catch( uno::Exception& )
264 OUString SAL_CALL ScVbaPageSetup::getCenterHeader()
266 OUString centerHeader;
269 uno::Reference<sheet::XHeaderFooterContent> xHeaderContent( mxPageProps->getPropertyValue(u"RightPageHeaderContent"_ustr), uno::UNO_QUERY_THROW);
270 uno::Reference< text::XText > xText = xHeaderContent->getCenterText();
271 centerHeader = xText->getString();
273 catch( uno::Exception& )
277 return centerHeader;
280 void SAL_CALL ScVbaPageSetup::setCenterHeader( const OUString& centerHeader)
284 uno::Reference<sheet::XHeaderFooterContent> xHeaderContent( mxPageProps->getPropertyValue(u"RightPageHeaderContent"_ustr), uno::UNO_QUERY_THROW);
285 uno::Reference< text::XText > xText = xHeaderContent->getCenterText();
286 xText->setString( centerHeader );
287 mxPageProps->setPropertyValue(u"RightPageHeaderContent"_ustr, uno::Any(xHeaderContent) );
289 catch( uno::Exception& )
294 OUString SAL_CALL ScVbaPageSetup::getRightHeader()
296 OUString rightHeader;
299 uno::Reference<sheet::XHeaderFooterContent> xHeaderContent( mxPageProps->getPropertyValue(u"RightPageHeaderContent"_ustr), uno::UNO_QUERY_THROW);
300 uno::Reference< text::XText > xText = xHeaderContent->getRightText();
301 rightHeader = xText->getString();
303 catch( uno::Exception& )
307 return rightHeader;
310 void SAL_CALL ScVbaPageSetup::setRightHeader( const OUString& rightHeader)
314 uno::Reference<sheet::XHeaderFooterContent> xHeaderContent( mxPageProps->getPropertyValue(u"RightPageHeaderContent"_ustr), uno::UNO_QUERY_THROW);
315 uno::Reference< text::XText > xText = xHeaderContent->getRightText();
316 xText->setString( rightHeader );
317 mxPageProps->setPropertyValue(u"RightPageHeaderContent"_ustr, uno::Any(xHeaderContent) );
319 catch( uno::Exception& )
324 OUString SAL_CALL ScVbaPageSetup::getLeftFooter()
326 OUString leftFooter;
329 uno::Reference<sheet::XHeaderFooterContent> xFooterContent( mxPageProps->getPropertyValue(u"RightPageFooterContent"_ustr), uno::UNO_QUERY_THROW);
330 uno::Reference< text::XText > xText = xFooterContent->getLeftText();
331 leftFooter = xText->getString();
333 catch( uno::Exception& )
337 return leftFooter;
340 void SAL_CALL ScVbaPageSetup::setLeftFooter( const OUString& leftFooter)
344 uno::Reference<sheet::XHeaderFooterContent> xFooterContent( mxPageProps->getPropertyValue(u"RightPageFooterContent"_ustr), uno::UNO_QUERY_THROW);
345 uno::Reference< text::XText > xText = xFooterContent->getLeftText();
346 xText->setString( leftFooter );
347 mxPageProps->setPropertyValue(u"RightPageFooterContent"_ustr, uno::Any(xFooterContent) );
349 catch( uno::Exception& )
354 OUString SAL_CALL ScVbaPageSetup::getCenterFooter()
356 OUString centerFooter;
359 uno::Reference<sheet::XHeaderFooterContent> xFooterContent( mxPageProps->getPropertyValue(u"RightPageFooterContent"_ustr), uno::UNO_QUERY_THROW);
360 uno::Reference< text::XText > xText = xFooterContent->getCenterText();
361 centerFooter = xText->getString();
363 catch( uno::Exception& )
367 return centerFooter;
370 void SAL_CALL ScVbaPageSetup::setCenterFooter( const OUString& centerFooter)
374 uno::Reference<sheet::XHeaderFooterContent> xFooterContent( mxPageProps->getPropertyValue(u"RightPageFooterContent"_ustr), uno::UNO_QUERY_THROW);
375 uno::Reference< text::XText > xText = xFooterContent->getCenterText();
376 xText->setString( centerFooter );
377 mxPageProps->setPropertyValue(u"RightPageFooterContent"_ustr, uno::Any(xFooterContent) );
379 catch( uno::Exception& )
385 OUString SAL_CALL ScVbaPageSetup::getRightFooter()
387 OUString rightFooter;
390 uno::Reference<sheet::XHeaderFooterContent> xFooterContent( mxPageProps->getPropertyValue(u"RightPageFooterContent"_ustr), uno::UNO_QUERY_THROW);
391 uno::Reference< text::XText > xText = xFooterContent->getRightText();
392 rightFooter = xText->getString();
394 catch( uno::Exception& )
398 return rightFooter;
401 void SAL_CALL ScVbaPageSetup::setRightFooter( const OUString& rightFooter)
405 uno::Reference<sheet::XHeaderFooterContent> xFooterContent( mxPageProps->getPropertyValue(u"RightPageFooterContent"_ustr), uno::UNO_QUERY_THROW);
406 uno::Reference< text::XText > xText = xFooterContent->getRightText();
407 xText->setString( rightFooter );
408 mxPageProps->setPropertyValue(u"RightPageFooterContent"_ustr, uno::Any(xFooterContent) );
410 catch( uno::Exception& )
415 sal_Int32 SAL_CALL ScVbaPageSetup::getOrder()
417 sal_Int32 order = excel::XlOrder::xlDownThenOver;
420 uno::Any aValue = mxPageProps->getPropertyValue(u"PrintDownFirst"_ustr);
421 bool bPrintDownFirst = false;
422 aValue >>= bPrintDownFirst;
423 if( !bPrintDownFirst )
424 order = excel::XlOrder::xlOverThenDown;
426 catch( uno::Exception& )
430 return order;
433 void SAL_CALL ScVbaPageSetup::setOrder(sal_Int32 order)
435 bool bOrder = true;
436 switch( order )
438 case excel::XlOrder::xlDownThenOver:
439 break;
440 case excel::XlOrder::xlOverThenDown:
441 bOrder = false;
442 break;
443 default:
444 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_PARAMETER);
449 mxPageProps->setPropertyValue(u"PrintDownFirst"_ustr, uno::Any( bOrder ));
451 catch (const uno::Exception&)
456 sal_Int32 SAL_CALL ScVbaPageSetup::getFirstPageNumber()
458 sal_Int16 number = 0;
461 uno::Any aValue = mxPageProps->getPropertyValue(u"FirstPageNumber"_ustr);
462 aValue >>= number;
464 catch( uno::Exception& )
468 if( number ==0 )
470 number = excel::Constants::xlAutomatic;
473 return number;
476 void SAL_CALL ScVbaPageSetup::setFirstPageNumber( sal_Int32 firstPageNumber)
478 if( firstPageNumber == excel::Constants::xlAutomatic )
479 firstPageNumber = 0;
483 uno::Any aValue;
484 aValue <<= static_cast<sal_Int16>(firstPageNumber);
485 mxPageProps->setPropertyValue(u"FirstPageNumber"_ustr, aValue );
487 catch (const uno::Exception&)
492 sal_Bool SAL_CALL ScVbaPageSetup::getCenterVertically()
494 bool centerVertically = false;
497 uno::Any aValue = mxPageProps->getPropertyValue(u"CenterVertically"_ustr);
498 aValue >>= centerVertically;
500 catch (const uno::Exception&)
503 return centerVertically;
506 void SAL_CALL ScVbaPageSetup::setCenterVertically( sal_Bool centerVertically)
510 mxPageProps->setPropertyValue(u"CenterVertically"_ustr, uno::Any( centerVertically ));
512 catch (const uno::Exception&)
517 sal_Bool SAL_CALL ScVbaPageSetup::getCenterHorizontally()
519 bool centerHorizontally = false;
522 uno::Any aValue = mxPageProps->getPropertyValue(u"CenterHorizontally"_ustr);
523 aValue >>= centerHorizontally;
525 catch (const uno::Exception&)
528 return centerHorizontally;
531 void SAL_CALL ScVbaPageSetup::setCenterHorizontally( sal_Bool centerHorizontally)
535 mxPageProps->setPropertyValue(u"CenterHorizontally"_ustr, uno::Any( centerHorizontally ));
537 catch (const uno::Exception&)
542 sal_Bool SAL_CALL ScVbaPageSetup::getPrintHeadings()
544 bool printHeadings = false;
547 uno::Any aValue = mxPageProps->getPropertyValue(u"PrintHeaders"_ustr);
548 aValue >>= printHeadings;
550 catch (const uno::Exception&)
553 return printHeadings;
556 void SAL_CALL ScVbaPageSetup::setPrintHeadings( sal_Bool printHeadings)
560 mxPageProps->setPropertyValue(u"PrintHeaders"_ustr, uno::Any( printHeadings ));
562 catch( uno::Exception& )
567 sal_Bool SAL_CALL ScVbaPageSetup::getPrintGridlines()
569 return false;
572 void SAL_CALL ScVbaPageSetup::setPrintGridlines( sal_Bool /*_printgridlines*/ )
576 OUString SAL_CALL ScVbaPageSetup::getPrintTitleRows()
578 return OUString();
580 void SAL_CALL ScVbaPageSetup::setPrintTitleRows( const OUString& /*_printtitlerows*/ )
583 OUString SAL_CALL ScVbaPageSetup::getPrintTitleColumns()
585 return OUString();
588 void SAL_CALL ScVbaPageSetup::setPrintTitleColumns( const OUString& /*_printtitlecolumns*/ )
592 sal_Int32 SAL_CALL ScVbaPageSetup::getPaperSize()
594 awt::Size aSize; // current papersize
595 mxPageProps->getPropertyValue( u"Size"_ustr ) >>= aSize;
596 if ( mbIsLandscape )
597 ::std::swap( aSize.Width, aSize.Height );
599 sal_Int32 nPaperSizeIndex = msfilter::util::PaperSizeConv::getMSPaperSizeIndex( aSize );
600 if ( nPaperSizeIndex == 0 )
601 nPaperSizeIndex = excel::XlPaperSize::xlPaperUser;
602 return nPaperSizeIndex;
605 void SAL_CALL ScVbaPageSetup::setPaperSize( sal_Int32 papersize )
607 if ( papersize != excel::XlPaperSize::xlPaperUser )
609 awt::Size aPaperSize;
610 const msfilter::util::ApiPaperSize& rConvertedSize = msfilter::util::PaperSizeConv::getApiSizeForMSPaperSizeIndex( papersize );
611 aPaperSize.Height = rConvertedSize.mnHeight;
612 aPaperSize.Width = rConvertedSize.mnWidth;
613 if ( mbIsLandscape )
614 ::std::swap( aPaperSize.Width, aPaperSize.Height );
615 mxPageProps->setPropertyValue( u"Size"_ustr, uno::Any( aPaperSize ) );
619 OUString
620 ScVbaPageSetup::getServiceImplName()
622 return u"ScVbaPageSetup"_ustr;
625 uno::Sequence< OUString >
626 ScVbaPageSetup::getServiceNames()
628 static uno::Sequence< OUString > const aServiceNames
630 u"ooo.vba.excel.PageSetup"_ustr
632 return aServiceNames;
635 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */