xref: /aoo42x/main/sw/source/ui/vba/vbapagesetup.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "vbapagesetup.hxx"
24 #include <com/sun/star/text/XText.hpp>
25 #include <com/sun/star/text/XPageCursor.hpp>
26 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
27 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <ooo/vba/word/WdSectionStart.hpp>
29 #include <ooo/vba/word/WdOrientation.hpp>
30 #include "wordvbahelper.hxx"
31 
32 using namespace ::com::sun::star;
33 using namespace ::ooo::vba;
34 
SwVbaPageSetup(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel,const uno::Reference<beans::XPropertySet> & xProps)35 SwVbaPageSetup::SwVbaPageSetup(const uno::Reference< XHelperInterface >& xParent,
36 				const uno::Reference< uno::XComponentContext >& xContext,
37 				const uno::Reference< frame::XModel >& xModel,
38                 const uno::Reference< beans::XPropertySet >& xProps ) throw (uno::RuntimeException):
39 	   	SwVbaPageSetup_BASE( xParent, xContext )
40 {
41     mxModel.set( xModel, uno::UNO_QUERY_THROW );
42     mxPageProps.set( xProps, uno::UNO_QUERY_THROW );
43     mnOrientPortrait = word::WdOrientation::wdOrientPortrait;
44     mnOrientLandscape = word::WdOrientation::wdOrientLandscape;
45 }
46 
getGutter()47 double SAL_CALL SwVbaPageSetup::getGutter() throw (uno::RuntimeException)
48 {
49     // not support in Writer
50     return 0;
51 }
52 
setGutter(double _gutter)53 void SAL_CALL SwVbaPageSetup::setGutter( double _gutter ) throw (uno::RuntimeException)
54 {
55     // default add gutter into left margin
56     if( _gutter != 0 )
57     {
58         double margin = VbaPageSetupBase::getLeftMargin() + _gutter;
59         VbaPageSetupBase::setLeftMargin( margin );
60     }
61 }
62 
getHeaderDistance()63 double SAL_CALL SwVbaPageSetup::getHeaderDistance() throw (uno::RuntimeException)
64 {
65     sal_Bool isHeaderOn = sal_False;
66     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn"))) >>= isHeaderOn;
67     if( !isHeaderOn )
68         mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn")), uno::makeAny( sal_True ) );
69     return VbaPageSetupBase::getHeaderMargin();
70 }
71 
72     /**
73      * changes the value of TopMargin to the value of new MS-Word-HeaderDistance. Subtracts the difference
74      * between old TopMargin and the new headerDistance from the value of HeaderSpacing (which defines the
75      * space between the header and the body of the text). calculates the new HeaderHeight (= height of the
76      * header + headerBodyDistance).
77      *
78      * @param: headerDistance is the value that is set in MS Word for the distance from the top of the page
79      *          to the header
80      */
setHeaderDistance(double _headerdistance)81 void SAL_CALL SwVbaPageSetup::setHeaderDistance( double _headerdistance ) throw (uno::RuntimeException)
82 {
83     sal_Int32 newHeaderDistance = Millimeter::getInHundredthsOfOneMillimeter( _headerdistance );
84     sal_Bool isHeaderOn = sal_False;
85     sal_Int32 aktTopMargin = 0;
86     sal_Int32 aktSpacing = 0;
87     sal_Int32 aktHeaderHeight = 0;
88 
89     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn"))) >>= isHeaderOn;
90     if( !isHeaderOn )
91         mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn")), uno::makeAny( sal_True ) );
92 
93     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin"))) >>= aktTopMargin;
94     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderBodyDistance"))) >>= aktSpacing;
95     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderHeight"))) >>= aktHeaderHeight;
96 
97     sal_Int32 newSpacing = aktSpacing - ( newHeaderDistance - aktTopMargin );
98     sal_Int32 height = aktHeaderHeight - aktSpacing;
99     sal_Int32 newHeaderHeight = newSpacing + height;
100 
101     mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin")), uno::makeAny( newHeaderDistance ) );
102     mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderBodyDistance")), uno::makeAny( newSpacing ) );
103     mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderHeight")), uno::makeAny( newHeaderHeight ) );
104 }
105 
getFooterDistance()106 double SAL_CALL SwVbaPageSetup::getFooterDistance() throw (uno::RuntimeException)
107 {
108     sal_Bool isFooterOn = sal_False;
109     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn"))) >>= isFooterOn;
110     if( !isFooterOn )
111         mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn")), uno::makeAny( sal_True ) );
112     return VbaPageSetupBase::getFooterMargin();
113 }
114 
setFooterDistance(double _footerdistance)115 void SAL_CALL SwVbaPageSetup::setFooterDistance( double _footerdistance ) throw (uno::RuntimeException)
116 {
117     sal_Int32 newFooterDistance = Millimeter::getInHundredthsOfOneMillimeter( _footerdistance );
118     sal_Bool isFooterOn = sal_False;
119     sal_Int32 aktBottomMargin = 0;
120     sal_Int32 aktSpacing = 0;
121     sal_Int32 aktFooterHeight = 0;
122 
123     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn"))) >>= isFooterOn;
124     if( !isFooterOn )
125         mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn")), uno::makeAny( sal_True ) );
126 
127     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BottomMargin"))) >>= aktBottomMargin;
128     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterBodyDistance"))) >>= aktSpacing;
129     mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterHeight"))) >>= aktFooterHeight;
130 
131     sal_Int32 newSpacing = aktSpacing - ( newFooterDistance - aktBottomMargin );
132     sal_Int32 height = aktFooterHeight - aktSpacing;
133     sal_Int32 newFooterHeight = newSpacing + height;
134 
135     mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BottomMargin")), uno::makeAny( newFooterDistance ) );
136     mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterBodyDistance")), uno::makeAny( newSpacing ) );
137     mxPageProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterHeight")), uno::makeAny( newFooterHeight ) );
138 }
139 
getDifferentFirstPageHeaderFooter()140 sal_Bool SAL_CALL SwVbaPageSetup::getDifferentFirstPageHeaderFooter() throw (uno::RuntimeException)
141 {
142     rtl::OUString pageStyle = getStyleOfFirstPage();
143     if( pageStyle.equalsAscii( "First Page" ) )
144         return sal_True;
145 
146     return sal_False;
147 }
148 
setDifferentFirstPageHeaderFooter(sal_Bool status)149 void SAL_CALL SwVbaPageSetup::setDifferentFirstPageHeaderFooter( sal_Bool status ) throw (uno::RuntimeException)
150 {
151     if( status == getDifferentFirstPageHeaderFooter() )
152         return;
153 
154     rtl::OUString newStyle;
155     if( status )
156         newStyle = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("First Page") );
157     else
158         newStyle = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Standard") );
159 
160     uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( mxModel ), uno::UNO_QUERY_THROW );
161     sal_Int32 nTopMargin = 0;
162     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin"))) >>= nTopMargin;
163     sal_Int32 nBottomMargin = 0;
164     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BottomMargin"))) >>= nBottomMargin;
165     sal_Int32 nLeftMargin = 0;
166     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("LeftMargin"))) >>= nLeftMargin;
167     sal_Int32 nRightMargin = 0;
168     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("RightMargin"))) >>= nRightMargin;
169     sal_Int32 nHeaderHeight = 0;
170     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderHeight"))) >>= nHeaderHeight;
171     sal_Int32 nFooterHeight = 0;
172     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterHeight"))) >>= nFooterHeight;
173 
174     sal_Bool isHeaderOn = sal_False;
175     xStyleProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn"))) >>= isHeaderOn;
176     if( isHeaderOn )
177     {
178         nTopMargin += nHeaderHeight;
179         nBottomMargin += nFooterHeight;
180         xStyleProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn")), uno::makeAny( sal_False ) );
181         xStyleProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn")), uno::makeAny( sal_False ) );
182     }
183     uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
184     if( xPageCursor->getPage() != 1 )
185     {
186         xPageCursor->jumpToFirstPage();
187     }
188 
189     uno::Reference< beans::XPropertySet > xCursorProps( xPageCursor, uno::UNO_QUERY_THROW );
190     uno::Reference< beans::XPropertySet > xTableProps( xCursorProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TextTable") ) ), uno::UNO_QUERY );
191     if( xTableProps.is() )
192     {
193         xTableProps->setPropertyValue(  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageDescName") ), uno::makeAny( newStyle ) );
194     }
195     else
196     {
197         xCursorProps->setPropertyValue(  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageDescName") ), uno::makeAny( newStyle ) );
198     }
199 
200     uno::Reference< beans::XPropertySet > xFirstPageProps( word::getCurrentPageStyle( mxModel ), uno::UNO_QUERY_THROW );
201     xFirstPageProps->setPropertyValue(  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin") ), uno::makeAny( nTopMargin ) );
202     xFirstPageProps->setPropertyValue(  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BottomMargin") ), uno::makeAny( nBottomMargin ) );
203     xFirstPageProps->setPropertyValue(  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("LeftMargin") ), uno::makeAny( nLeftMargin ) );
204     xFirstPageProps->setPropertyValue(  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("RightMargin") ), uno::makeAny( nRightMargin ) );
205 }
206 
getStyleOfFirstPage()207 rtl::OUString SwVbaPageSetup::getStyleOfFirstPage() throw (uno::RuntimeException)
208 {
209     rtl::OUString styleFirstPage;
210     uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
211     if( xPageCursor->getPage() != 1 )
212     {
213         xPageCursor->jumpToFirstPage();
214     }
215 
216     uno::Reference< beans::XPropertySet > xCursorProps( xPageCursor, uno::UNO_QUERY_THROW );
217     uno::Reference< beans::XPropertySet > xTableProps( xCursorProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TextTable") ) ), uno::UNO_QUERY );
218     if( xTableProps.is() )
219     {
220         xTableProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageDescName") ) ) >>= styleFirstPage;
221     }
222     else
223     {
224         xCursorProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageDescName") ) ) >>= styleFirstPage;
225     }
226     return styleFirstPage;
227 }
228 
getSectionStart()229 ::sal_Int32 SAL_CALL SwVbaPageSetup::getSectionStart() throw (uno::RuntimeException)
230 {
231     // FIXME:
232     sal_Int32 wdSectionStart = word::WdSectionStart::wdSectionNewPage;
233     uno::Reference< container::XNamed > xNamed( mxPageProps, uno::UNO_QUERY_THROW );
234     rtl::OUString sStyleName = xNamed->getName();
235     //mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Name") ) ) >>= sStyleName;
236     if( sStyleName.equalsAscii("Left Page") )
237         wdSectionStart = word::WdSectionStart::wdSectionEvenPage;
238     else if( sStyleName.equalsAscii("Right Page") )
239         wdSectionStart = word::WdSectionStart::wdSectionOddPage;
240     else
241         wdSectionStart = word::WdSectionStart::wdSectionNewPage;
242     return wdSectionStart;
243 }
244 
setSectionStart(::sal_Int32)245 void SAL_CALL SwVbaPageSetup::setSectionStart( ::sal_Int32 /*_sectionstart*/ ) throw (uno::RuntimeException)
246 {
247     // fail to find corresponding feature in Writer
248     // #FIXME:
249 }
250 
251 rtl::OUString&
getServiceImplName()252 SwVbaPageSetup::getServiceImplName()
253 {
254 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaPageSetup") );
255 	return sImplName;
256 }
257 
258 uno::Sequence< rtl::OUString >
getServiceNames()259 SwVbaPageSetup::getServiceNames()
260 {
261 	static uno::Sequence< rtl::OUString > aServiceNames;
262 	if ( aServiceNames.getLength() == 0 )
263 	{
264 		aServiceNames.realloc( 1 );
265 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.PageSetup" ) );
266 	}
267 	return aServiceNames;
268 }
269