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