1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbapagesetup.hxx"
31 #include <com/sun/star/text/XText.hpp>
32 #include <com/sun/star/text/XPageCursor.hpp>
33 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
34 #include <com/sun/star/container/XNameAccess.hpp>
35 #include <ooo/vba/word/WdSectionStart.hpp>
36 #include <ooo/vba/word/WdOrientation.hpp>
37 #include "wordvbahelper.hxx"
39 using namespace ::com::sun::star
;
40 using namespace ::ooo::vba
;
42 SwVbaPageSetup::SwVbaPageSetup(const uno::Reference
< XHelperInterface
>& xParent
,
43 const uno::Reference
< uno::XComponentContext
>& xContext
,
44 const uno::Reference
< frame::XModel
>& xModel
,
45 const uno::Reference
< beans::XPropertySet
>& xProps
) throw (uno::RuntimeException
):
46 SwVbaPageSetup_BASE( xParent
, xContext
)
48 mxModel
.set( xModel
, uno::UNO_QUERY_THROW
);
49 mxPageProps
.set( xProps
, uno::UNO_QUERY_THROW
);
50 mnOrientPortrait
= word::WdOrientation::wdOrientPortrait
;
51 mnOrientLandscape
= word::WdOrientation::wdOrientLandscape
;
54 double SAL_CALL
SwVbaPageSetup::getGutter() throw (uno::RuntimeException
)
56 // not support in Writer
60 void SAL_CALL
SwVbaPageSetup::setGutter( double _gutter
) throw (uno::RuntimeException
)
62 // default add gutter into left margin
65 double margin
= VbaPageSetupBase::getLeftMargin() + _gutter
;
66 VbaPageSetupBase::setLeftMargin( margin
);
70 double SAL_CALL
SwVbaPageSetup::getHeaderDistance() throw (uno::RuntimeException
)
72 sal_Bool isHeaderOn
= sal_False
;
73 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn"))) >>= isHeaderOn
;
75 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn")), uno::makeAny( sal_True
) );
76 return VbaPageSetupBase::getHeaderMargin();
80 * changes the value of TopMargin to the value of new MS-Word-HeaderDistance. Subtracts the difference
81 * between old TopMargin and the new headerDistance from the value of HeaderSpacing (which defines the
82 * space between the header and the body of the text). calculates the new HeaderHeight (= height of the
83 * header + headerBodyDistance).
85 * @param: headerDistance is the value that is set in MS Word for the distance from the top of the page
88 void SAL_CALL
SwVbaPageSetup::setHeaderDistance( double _headerdistance
) throw (uno::RuntimeException
)
90 sal_Int32 newHeaderDistance
= Millimeter::getInHundredthsOfOneMillimeter( _headerdistance
);
91 sal_Bool isHeaderOn
= sal_False
;
92 sal_Int32 aktTopMargin
= 0;
93 sal_Int32 aktSpacing
= 0;
94 sal_Int32 aktHeaderHeight
= 0;
96 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn"))) >>= isHeaderOn
;
98 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn")), uno::makeAny( sal_True
) );
100 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin"))) >>= aktTopMargin
;
101 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderBodyDistance"))) >>= aktSpacing
;
102 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderHeight"))) >>= aktHeaderHeight
;
104 sal_Int32 newSpacing
= aktSpacing
- ( newHeaderDistance
- aktTopMargin
);
105 sal_Int32 height
= aktHeaderHeight
- aktSpacing
;
106 sal_Int32 newHeaderHeight
= newSpacing
+ height
;
108 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin")), uno::makeAny( newHeaderDistance
) );
109 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderBodyDistance")), uno::makeAny( newSpacing
) );
110 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderHeight")), uno::makeAny( newHeaderHeight
) );
113 double SAL_CALL
SwVbaPageSetup::getFooterDistance() throw (uno::RuntimeException
)
115 sal_Bool isFooterOn
= sal_False
;
116 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn"))) >>= isFooterOn
;
118 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn")), uno::makeAny( sal_True
) );
119 return VbaPageSetupBase::getFooterMargin();
122 void SAL_CALL
SwVbaPageSetup::setFooterDistance( double _footerdistance
) throw (uno::RuntimeException
)
124 sal_Int32 newFooterDistance
= Millimeter::getInHundredthsOfOneMillimeter( _footerdistance
);
125 sal_Bool isFooterOn
= sal_False
;
126 sal_Int32 aktBottomMargin
= 0;
127 sal_Int32 aktSpacing
= 0;
128 sal_Int32 aktFooterHeight
= 0;
130 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn"))) >>= isFooterOn
;
132 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn")), uno::makeAny( sal_True
) );
134 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BottomMargin"))) >>= aktBottomMargin
;
135 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterBodyDistance"))) >>= aktSpacing
;
136 mxPageProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterHeight"))) >>= aktFooterHeight
;
138 sal_Int32 newSpacing
= aktSpacing
- ( newFooterDistance
- aktBottomMargin
);
139 sal_Int32 height
= aktFooterHeight
- aktSpacing
;
140 sal_Int32 newFooterHeight
= newSpacing
+ height
;
142 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BottomMargin")), uno::makeAny( newFooterDistance
) );
143 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterBodyDistance")), uno::makeAny( newSpacing
) );
144 mxPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterHeight")), uno::makeAny( newFooterHeight
) );
147 sal_Bool SAL_CALL
SwVbaPageSetup::getDifferentFirstPageHeaderFooter() throw (uno::RuntimeException
)
149 rtl::OUString pageStyle
= getStyleOfFirstPage();
150 if( pageStyle
.equalsAscii( "First Page" ) )
156 void SAL_CALL
SwVbaPageSetup::setDifferentFirstPageHeaderFooter( sal_Bool status
) throw (uno::RuntimeException
)
158 if( status
== getDifferentFirstPageHeaderFooter() )
161 rtl::OUString newStyle
;
163 newStyle
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("First Page") );
165 newStyle
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Standard") );
167 uno::Reference
< beans::XPropertySet
> xStyleProps( word::getCurrentPageStyle( mxModel
), uno::UNO_QUERY_THROW
);
168 sal_Int32 nTopMargin
= 0;
169 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin"))) >>= nTopMargin
;
170 sal_Int32 nBottomMargin
= 0;
171 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BottomMargin"))) >>= nBottomMargin
;
172 sal_Int32 nLeftMargin
= 0;
173 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("LeftMargin"))) >>= nLeftMargin
;
174 sal_Int32 nRightMargin
= 0;
175 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("RightMargin"))) >>= nRightMargin
;
176 sal_Int32 nHeaderHeight
= 0;
177 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderHeight"))) >>= nHeaderHeight
;
178 sal_Int32 nFooterHeight
= 0;
179 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterHeight"))) >>= nFooterHeight
;
181 sal_Bool isHeaderOn
= sal_False
;
182 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn"))) >>= isHeaderOn
;
185 nTopMargin
+= nHeaderHeight
;
186 nBottomMargin
+= nFooterHeight
;
187 xStyleProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn")), uno::makeAny( sal_False
) );
188 xStyleProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn")), uno::makeAny( sal_False
) );
190 uno::Reference
< text::XPageCursor
> xPageCursor( word::getXTextViewCursor( mxModel
), uno::UNO_QUERY_THROW
);
191 if( xPageCursor
->getPage() != 1 )
193 xPageCursor
->jumpToFirstPage();
196 uno::Reference
< beans::XPropertySet
> xCursorProps( xPageCursor
, uno::UNO_QUERY_THROW
);
197 uno::Reference
< beans::XPropertySet
> xTableProps( xCursorProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TextTable") ) ), uno::UNO_QUERY
);
198 if( xTableProps
.is() )
200 xTableProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageDescName") ), uno::makeAny( newStyle
) );
204 xCursorProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageDescName") ), uno::makeAny( newStyle
) );
207 uno::Reference
< beans::XPropertySet
> xFirstPageProps( word::getCurrentPageStyle( mxModel
), uno::UNO_QUERY_THROW
);
208 xFirstPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TopMargin") ), uno::makeAny( nTopMargin
) );
209 xFirstPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BottomMargin") ), uno::makeAny( nBottomMargin
) );
210 xFirstPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("LeftMargin") ), uno::makeAny( nLeftMargin
) );
211 xFirstPageProps
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("RightMargin") ), uno::makeAny( nRightMargin
) );
214 rtl::OUString
SwVbaPageSetup::getStyleOfFirstPage() throw (uno::RuntimeException
)
216 rtl::OUString styleFirstPage
;
217 uno::Reference
< text::XPageCursor
> xPageCursor( word::getXTextViewCursor( mxModel
), uno::UNO_QUERY_THROW
);
218 if( xPageCursor
->getPage() != 1 )
220 xPageCursor
->jumpToFirstPage();
223 uno::Reference
< beans::XPropertySet
> xCursorProps( xPageCursor
, uno::UNO_QUERY_THROW
);
224 uno::Reference
< beans::XPropertySet
> xTableProps( xCursorProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("TextTable") ) ), uno::UNO_QUERY
);
225 if( xTableProps
.is() )
227 xTableProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageDescName") ) ) >>= styleFirstPage
;
231 xCursorProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageDescName") ) ) >>= styleFirstPage
;
233 return styleFirstPage
;
236 ::sal_Int32 SAL_CALL
SwVbaPageSetup::getSectionStart() throw (uno::RuntimeException
)
239 sal_Int32 wdSectionStart
= word::WdSectionStart::wdSectionNewPage
;
240 uno::Reference
< container::XNamed
> xNamed( mxPageProps
, uno::UNO_QUERY_THROW
);
241 rtl::OUString sStyleName
= xNamed
->getName();
242 //mxPageProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Name") ) ) >>= sStyleName;
243 if( sStyleName
.equalsAscii("Left Page") )
244 wdSectionStart
= word::WdSectionStart::wdSectionEvenPage
;
245 else if( sStyleName
.equalsAscii("Right Page") )
246 wdSectionStart
= word::WdSectionStart::wdSectionOddPage
;
248 wdSectionStart
= word::WdSectionStart::wdSectionNewPage
;
249 return wdSectionStart
;
252 void SAL_CALL
SwVbaPageSetup::setSectionStart( ::sal_Int32
/*_sectionstart*/ ) throw (uno::RuntimeException
)
254 // fail to find corresponding feature in Writer
259 SwVbaPageSetup::getServiceImplName()
261 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaPageSetup") );
265 uno::Sequence
< rtl::OUString
>
266 SwVbaPageSetup::getServiceNames()
268 static uno::Sequence
< rtl::OUString
> aServiceNames
;
269 if ( aServiceNames
.getLength() == 0 )
271 aServiceNames
.realloc( 1 );
272 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.PageSetup" ) );
274 return aServiceNames
;