1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 #include "vbapagesetup.hxx"
20 #include <com/sun/star/frame/XModel.hpp>
21 #include <com/sun/star/text/XText.hpp>
22 #include <com/sun/star/text/XPageCursor.hpp>
23 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <ooo/vba/word/WdSectionStart.hpp>
26 #include <ooo/vba/word/WdOrientation.hpp>
27 #include "wordvbahelper.hxx"
28 
29 using namespace ::com::sun::star;
30 using namespace ::ooo::vba;
31 
SwVbaPageSetup(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel,const uno::Reference<beans::XPropertySet> & xProps)32 SwVbaPageSetup::SwVbaPageSetup(const uno::Reference< XHelperInterface >& xParent,
33                 const uno::Reference< uno::XComponentContext >& xContext,
34                 const uno::Reference< frame::XModel >& xModel,
35                 const uno::Reference< beans::XPropertySet >& xProps ):
36            SwVbaPageSetup_BASE( xParent, xContext )
37 {
38     mxModel.set( xModel, uno::UNO_SET_THROW );
39     mxPageProps.set( xProps, uno::UNO_SET_THROW );
40     mnOrientPortrait = word::WdOrientation::wdOrientPortrait;
41     mnOrientLandscape = word::WdOrientation::wdOrientLandscape;
42 }
43 
getGutter()44 double SAL_CALL SwVbaPageSetup::getGutter()
45 {
46     // not support in Writer
47     return 0;
48 }
49 
setGutter(double _gutter)50 void SAL_CALL SwVbaPageSetup::setGutter( double _gutter )
51 {
52     // default add gutter into left margin
53     if( _gutter != 0 )
54     {
55         double margin = VbaPageSetupBase::getLeftMargin() + _gutter;
56         VbaPageSetupBase::setLeftMargin( margin );
57     }
58 }
59 
getHeaderDistance()60 double SAL_CALL SwVbaPageSetup::getHeaderDistance()
61 {
62     bool isHeaderOn = false;
63     mxPageProps->getPropertyValue("HeaderIsOn") >>= isHeaderOn;
64     if( !isHeaderOn )
65         mxPageProps->setPropertyValue("HeaderIsOn", uno::makeAny( true ) );
66     return VbaPageSetupBase::getHeaderMargin();
67 }
68 
69     /**
70      * changes the value of TopMargin to the value of new MS-Word-HeaderDistance. Subtracts the difference
71      * between old TopMargin and the new headerDistance from the value of HeaderSpacing (which defines the
72      * space between the header and the body of the text). calculates the new HeaderHeight (= height of the
73      * header + headerBodyDistance).
74      *
75      * @param: headerDistance is the value that is set in MS Word for the distance from the top of the page
76      *          to the header
77      */
setHeaderDistance(double _headerdistance)78 void SAL_CALL SwVbaPageSetup::setHeaderDistance( double _headerdistance )
79 {
80     sal_Int32 newHeaderDistance = Millimeter::getInHundredthsOfOneMillimeter( _headerdistance );
81     bool isHeaderOn = false;
82     sal_Int32 currentTopMargin = 0;
83     sal_Int32 currentSpacing = 0;
84     sal_Int32 currentHeaderHeight = 0;
85 
86     mxPageProps->getPropertyValue("HeaderIsOn") >>= isHeaderOn;
87     if( !isHeaderOn )
88         mxPageProps->setPropertyValue("HeaderIsOn", uno::makeAny( true ) );
89 
90     mxPageProps->getPropertyValue("TopMargin") >>= currentTopMargin;
91     mxPageProps->getPropertyValue("HeaderBodyDistance") >>= currentSpacing;
92     mxPageProps->getPropertyValue("HeaderHeight") >>= currentHeaderHeight;
93 
94     sal_Int32 newSpacing = currentSpacing - ( newHeaderDistance - currentTopMargin );
95     sal_Int32 height = currentHeaderHeight - currentSpacing;
96     sal_Int32 newHeaderHeight = newSpacing + height;
97 
98     mxPageProps->setPropertyValue("TopMargin", uno::makeAny( newHeaderDistance ) );
99     mxPageProps->setPropertyValue("HeaderBodyDistance", uno::makeAny( newSpacing ) );
100     mxPageProps->setPropertyValue("HeaderHeight", uno::makeAny( newHeaderHeight ) );
101 }
102 
getFooterDistance()103 double SAL_CALL SwVbaPageSetup::getFooterDistance()
104 {
105     bool isFooterOn = false;
106     mxPageProps->getPropertyValue("FooterIsOn") >>= isFooterOn;
107     if( !isFooterOn )
108         mxPageProps->setPropertyValue("FooterIsOn", uno::makeAny( true ) );
109     return VbaPageSetupBase::getFooterMargin();
110 }
111 
setFooterDistance(double _footerdistance)112 void SAL_CALL SwVbaPageSetup::setFooterDistance( double _footerdistance )
113 {
114     sal_Int32 newFooterDistance = Millimeter::getInHundredthsOfOneMillimeter( _footerdistance );
115     bool isFooterOn = false;
116     sal_Int32 currentBottomMargin = 0;
117     sal_Int32 currentSpacing = 0;
118     sal_Int32 currentFooterHeight = 0;
119 
120     mxPageProps->getPropertyValue("FooterIsOn") >>= isFooterOn;
121     if( !isFooterOn )
122         mxPageProps->setPropertyValue("FooterIsOn", uno::makeAny( true ) );
123 
124     mxPageProps->getPropertyValue("BottomMargin") >>= currentBottomMargin;
125     mxPageProps->getPropertyValue("FooterBodyDistance") >>= currentSpacing;
126     mxPageProps->getPropertyValue("FooterHeight") >>= currentFooterHeight;
127 
128     sal_Int32 newSpacing = currentSpacing - ( newFooterDistance - currentBottomMargin );
129     sal_Int32 height = currentFooterHeight - currentSpacing;
130     sal_Int32 newFooterHeight = newSpacing + height;
131 
132     mxPageProps->setPropertyValue("BottomMargin", uno::makeAny( newFooterDistance ) );
133     mxPageProps->setPropertyValue("FooterBodyDistance", uno::makeAny( newSpacing ) );
134     mxPageProps->setPropertyValue("FooterHeight", uno::makeAny( newFooterHeight ) );
135 }
136 
getDifferentFirstPageHeaderFooter()137 sal_Bool SAL_CALL SwVbaPageSetup::getDifferentFirstPageHeaderFooter()
138 {
139     OUString pageStyle = getStyleOfFirstPage();
140     if ( pageStyle == "First Page" )
141         return true;
142 
143     return false;
144 }
145 
setDifferentFirstPageHeaderFooter(sal_Bool status)146 void SAL_CALL SwVbaPageSetup::setDifferentFirstPageHeaderFooter( sal_Bool status )
147 {
148     if( status == getDifferentFirstPageHeaderFooter() )
149         return;
150 
151     OUString newStyle;
152     if( status )
153         newStyle = "First Page";
154     else
155         newStyle = "Standard";
156 
157     uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( mxModel ), uno::UNO_QUERY_THROW );
158     sal_Int32 nTopMargin = 0;
159     xStyleProps->getPropertyValue("TopMargin") >>= nTopMargin;
160     sal_Int32 nBottomMargin = 0;
161     xStyleProps->getPropertyValue("BottomMargin") >>= nBottomMargin;
162     sal_Int32 nLeftMargin = 0;
163     xStyleProps->getPropertyValue("LeftMargin") >>= nLeftMargin;
164     sal_Int32 nRightMargin = 0;
165     xStyleProps->getPropertyValue("RightMargin") >>= nRightMargin;
166     sal_Int32 nHeaderHeight = 0;
167     xStyleProps->getPropertyValue("HeaderHeight") >>= nHeaderHeight;
168     sal_Int32 nFooterHeight = 0;
169     xStyleProps->getPropertyValue("FooterHeight") >>= nFooterHeight;
170 
171     bool isHeaderOn = false;
172     xStyleProps->getPropertyValue("HeaderIsOn") >>= isHeaderOn;
173     if( isHeaderOn )
174     {
175         nTopMargin += nHeaderHeight;
176         nBottomMargin += nFooterHeight;
177         xStyleProps->setPropertyValue("HeaderIsOn", uno::makeAny( false ) );
178         xStyleProps->setPropertyValue("FooterIsOn", uno::makeAny( false ) );
179     }
180     uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
181     if( xPageCursor->getPage() != 1 )
182     {
183         xPageCursor->jumpToFirstPage();
184     }
185 
186     uno::Reference< beans::XPropertySet > xCursorProps( xPageCursor, uno::UNO_QUERY_THROW );
187     uno::Reference< beans::XPropertySet > xTableProps( xCursorProps->getPropertyValue("TextTable"), uno::UNO_QUERY );
188     if( xTableProps.is() )
189     {
190         xTableProps->setPropertyValue("PageDescName", uno::makeAny( newStyle ) );
191     }
192     else
193     {
194         xCursorProps->setPropertyValue("PageDescName", uno::makeAny( newStyle ) );
195     }
196 
197     uno::Reference< beans::XPropertySet > xFirstPageProps( word::getCurrentPageStyle( mxModel ), uno::UNO_QUERY_THROW );
198     xFirstPageProps->setPropertyValue("TopMargin", uno::makeAny( nTopMargin ) );
199     xFirstPageProps->setPropertyValue("BottomMargin", uno::makeAny( nBottomMargin ) );
200     xFirstPageProps->setPropertyValue("LeftMargin", uno::makeAny( nLeftMargin ) );
201     xFirstPageProps->setPropertyValue("RightMargin", uno::makeAny( nRightMargin ) );
202 }
203 
getStyleOfFirstPage() const204 OUString SwVbaPageSetup::getStyleOfFirstPage() const
205 {
206     OUString styleFirstPage;
207     uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
208     if( xPageCursor->getPage() != 1 )
209     {
210         xPageCursor->jumpToFirstPage();
211     }
212 
213     uno::Reference< beans::XPropertySet > xCursorProps( xPageCursor, uno::UNO_QUERY_THROW );
214     uno::Reference< beans::XPropertySet > xTableProps( xCursorProps->getPropertyValue("TextTable"), uno::UNO_QUERY );
215     if( xTableProps.is() )
216     {
217         xTableProps->getPropertyValue("PageDescName") >>= styleFirstPage;
218     }
219     else
220     {
221         xCursorProps->getPropertyValue("PageDescName") >>= styleFirstPage;
222     }
223     return styleFirstPage;
224 }
225 
getSectionStart()226 ::sal_Int32 SAL_CALL SwVbaPageSetup::getSectionStart()
227 {
228     // FIXME:
229     sal_Int32 wdSectionStart = word::WdSectionStart::wdSectionNewPage;
230     uno::Reference< container::XNamed > xNamed( mxPageProps, uno::UNO_QUERY_THROW );
231     OUString sStyleName = xNamed->getName();
232     if ( sStyleName == "Left Page" )
233         wdSectionStart = word::WdSectionStart::wdSectionEvenPage;
234     else if ( sStyleName == "Right Page" )
235         wdSectionStart = word::WdSectionStart::wdSectionOddPage;
236     else
237         wdSectionStart = word::WdSectionStart::wdSectionNewPage;
238     return wdSectionStart;
239 }
240 
setSectionStart(::sal_Int32)241 void SAL_CALL SwVbaPageSetup::setSectionStart( ::sal_Int32 /*_sectionstart*/ )
242 {
243     // fail to find corresponding feature in Writer
244     // #FIXME:
245 }
246 
247 OUString
getServiceImplName()248 SwVbaPageSetup::getServiceImplName()
249 {
250     return "SwVbaPageSetup";
251 }
252 
253 uno::Sequence< OUString >
getServiceNames()254 SwVbaPageSetup::getServiceNames()
255 {
256     static uno::Sequence< OUString > const aServiceNames
257     {
258         "ooo.vba.word.PageSetup"
259     };
260     return aServiceNames;
261 }
262 
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
264