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