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 "vbarows.hxx"
20 : #include "vbarow.hxx"
21 : #include <com/sun/star/text/HoriOrientation.hpp>
22 : #include <com/sun/star/table/XCellRange.hpp>
23 : #include <ooo/vba/word/WdRowAlignment.hpp>
24 : #include <ooo/vba/word/WdConstants.hpp>
25 : #include <ooo/vba/word/WdRulerStyle.hpp>
26 : #include "wordvbahelper.hxx"
27 : #include "vbacolumns.hxx"
28 : #include "vbatablehelper.hxx"
29 :
30 : using namespace ::ooo::vba;
31 : using namespace ::com::sun::star;
32 :
33 0 : class RowsEnumWrapper : public EnumerationHelper_BASE
34 : {
35 : uno::WeakReference< XHelperInterface > mxParent;
36 : uno::Reference< uno::XComponentContext > mxContext;
37 : uno::Reference< text::XTextTable > mxTextTable;
38 : uno::Reference< container::XIndexAccess > mxIndexAccess;
39 : sal_Int32 nIndex;
40 :
41 : public:
42 0 : RowsEnumWrapper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< text::XTextTable >& xTextTable ) : mxParent( xParent ), mxContext( xContext ), mxTextTable( xTextTable ), nIndex( 0 )
43 : {
44 0 : mxIndexAccess.set( mxTextTable->getRows(), uno::UNO_QUERY );
45 0 : }
46 0 : virtual sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
47 : {
48 0 : return ( nIndex < mxIndexAccess->getCount() );
49 : }
50 :
51 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
52 : {
53 0 : if( nIndex < mxIndexAccess->getCount() )
54 : {
55 0 : return uno::makeAny( uno::Reference< word::XRow > ( new SwVbaRow( mxParent, mxContext, mxTextTable, nIndex++ ) ) );
56 : }
57 0 : throw container::NoSuchElementException();
58 : }
59 : };
60 :
61 0 : SwVbaRows::SwVbaRows( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< text::XTextTable >& xTextTable, const uno::Reference< table::XTableRows >& xTableRows ) throw (uno::RuntimeException) : SwVbaRows_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( xTableRows, uno::UNO_QUERY_THROW ) ), mxTextTable( xTextTable ), mxTableRows( xTableRows )
62 : {
63 0 : mnStartRowIndex = 0;
64 0 : mnEndRowIndex = m_xIndexAccess->getCount() - 1;
65 0 : }
66 :
67 0 : SwVbaRows::SwVbaRows( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< text::XTextTable >& xTextTable, const uno::Reference< table::XTableRows >& xTableRows, sal_Int32 nStarIndex, sal_Int32 nEndIndex ) throw (uno::RuntimeException) : SwVbaRows_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( xTableRows, uno::UNO_QUERY_THROW ) ), mxTextTable( xTextTable ), mxTableRows( xTableRows ), mnStartRowIndex( nStarIndex ), mnEndRowIndex( nEndIndex )
68 : {
69 0 : if( mnEndRowIndex < mnStartRowIndex )
70 0 : throw uno::RuntimeException();
71 0 : }
72 :
73 : /**
74 : * get the alignment of the rows: SO format com.sun.star.text.HoriOrientation
75 : * is mapped to WdRowAlignment in Word
76 : * @return the alignment
77 : */
78 0 : ::sal_Int32 SAL_CALL SwVbaRows::getAlignment() throw (uno::RuntimeException, std::exception)
79 : {
80 0 : sal_Int16 nAlignment = text::HoriOrientation::LEFT;
81 0 : uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
82 0 : xTableProps->getPropertyValue("HoriOrient") >>= nAlignment;
83 0 : sal_Int32 nRet = 0;
84 0 : switch( nAlignment )
85 : {
86 : case text::HoriOrientation::CENTER:
87 : {
88 0 : nRet = word::WdRowAlignment::wdAlignRowCenter;
89 0 : break;
90 : }
91 : case text::HoriOrientation::RIGHT:
92 : {
93 0 : nRet = word::WdRowAlignment::wdAlignRowRight;
94 0 : break;
95 : }
96 : default:
97 : {
98 0 : nRet = word::WdRowAlignment::wdAlignRowLeft;
99 : }
100 : }
101 0 : return nRet;
102 : }
103 :
104 0 : void SAL_CALL SwVbaRows::setAlignment( ::sal_Int32 _alignment ) throw (uno::RuntimeException, std::exception)
105 : {
106 0 : sal_Int16 nAlignment = text::HoriOrientation::LEFT;
107 0 : switch( _alignment )
108 : {
109 : case word::WdRowAlignment::wdAlignRowCenter:
110 : {
111 0 : nAlignment = text::HoriOrientation::CENTER;
112 0 : break;
113 : }
114 : case word::WdRowAlignment::wdAlignRowRight:
115 : {
116 0 : nAlignment = text::HoriOrientation::RIGHT;
117 0 : break;
118 : }
119 : default:
120 : {
121 0 : nAlignment = text::HoriOrientation::LEFT;
122 : }
123 : }
124 0 : uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
125 0 : xTableProps->setPropertyValue("HoriOrient", uno::makeAny( nAlignment ) );
126 0 : }
127 :
128 0 : uno::Any SAL_CALL SwVbaRows::getAllowBreakAcrossPages() throw (uno::RuntimeException, std::exception)
129 : {
130 0 : sal_Bool bAllowBreak = sal_False;
131 0 : uno::Reference< container::XIndexAccess > xRowsAccess( mxTableRows, uno::UNO_QUERY_THROW );
132 0 : for( sal_Int32 index = mnStartRowIndex; index <= mnEndRowIndex; ++index )
133 : {
134 0 : uno::Reference< beans::XPropertySet > xRowProps( xRowsAccess->getByIndex( index ), uno::UNO_QUERY_THROW );
135 0 : sal_Bool bSplit = sal_False;
136 0 : xRowProps->getPropertyValue("IsSplitAllowed") >>= bSplit;
137 0 : if( index == 0 )
138 : {
139 0 : bAllowBreak = bSplit;
140 : }
141 0 : if( bSplit != bAllowBreak )
142 : {
143 0 : sal_Int32 nRet = word::WdConstants::wdUndefined;
144 0 : return uno::makeAny( nRet );
145 : }
146 0 : }
147 0 : return uno::makeAny( bAllowBreak );
148 : }
149 :
150 0 : void SAL_CALL SwVbaRows::setAllowBreakAcrossPages( const uno::Any& _allowbreakacrosspages ) throw (uno::RuntimeException, std::exception)
151 : {
152 0 : sal_Bool bAllowBreak = sal_False;
153 0 : _allowbreakacrosspages >>= bAllowBreak;
154 0 : uno::Reference< container::XIndexAccess > xRowsAccess( mxTableRows, uno::UNO_QUERY_THROW );
155 0 : for( sal_Int32 index = mnStartRowIndex; index <= mnEndRowIndex; ++index )
156 : {
157 0 : uno::Reference< beans::XPropertySet > xRowProps( xRowsAccess->getByIndex( index ), uno::UNO_QUERY_THROW );
158 0 : xRowProps->setPropertyValue("IsSplitAllowed", uno::makeAny( bAllowBreak ) );
159 0 : }
160 0 : }
161 :
162 0 : float SAL_CALL SwVbaRows::getSpaceBetweenColumns() throw (uno::RuntimeException, std::exception)
163 : {
164 : // just get the first spacing of the first cell
165 0 : uno::Reference< table::XCellRange > xCellRange( mxTextTable, uno::UNO_QUERY_THROW );
166 0 : uno::Reference< beans::XPropertySet > xCellProps( xCellRange->getCellByPosition( 0, mnStartRowIndex ), uno::UNO_QUERY_THROW );
167 0 : sal_Int32 nLeftBorderDistance = 0;
168 0 : sal_Int32 nRightBorderDistance = 0;
169 0 : xCellProps->getPropertyValue("LeftBorderDistance") >>= nLeftBorderDistance;
170 0 : xCellProps->getPropertyValue("RightBorderDistance") >>= nRightBorderDistance;
171 0 : return static_cast< float >( Millimeter::getInPoints( nLeftBorderDistance + nRightBorderDistance ) );
172 : }
173 :
174 0 : void SAL_CALL SwVbaRows::setSpaceBetweenColumns( float _spacebetweencolumns ) throw (uno::RuntimeException, std::exception)
175 : {
176 0 : sal_Int32 nSpace = Millimeter::getInHundredthsOfOneMillimeter( _spacebetweencolumns ) / 2;
177 0 : uno::Reference< container::XIndexAccess > xColumnAccess( mxTextTable->getColumns(), uno::UNO_QUERY_THROW );
178 0 : uno::Reference< table::XCellRange > xCellRange( mxTextTable, uno::UNO_QUERY_THROW );
179 0 : SwVbaTableHelper aTableHelper( mxTextTable );
180 0 : for( sal_Int32 row = mnStartRowIndex; row <= mnEndRowIndex; ++row )
181 : {
182 0 : sal_Int32 nColumns = aTableHelper.getTabColumnsCount( row );
183 0 : for( sal_Int32 column = 0; column < nColumns; ++column )
184 : {
185 0 : uno::Reference< beans::XPropertySet > xCellProps( xCellRange->getCellByPosition( column, row ), uno::UNO_QUERY_THROW );
186 0 : xCellProps->setPropertyValue("LeftBorderDistance", uno::makeAny( nSpace ) );
187 0 : xCellProps->setPropertyValue("RightBorderDistance", uno::makeAny( nSpace ) );
188 0 : }
189 0 : }
190 0 : }
191 :
192 0 : void SAL_CALL SwVbaRows::Delete( ) throw (uno::RuntimeException, std::exception)
193 : {
194 0 : mxTableRows->removeByIndex( mnStartRowIndex, getCount() );
195 0 : }
196 :
197 0 : void SAL_CALL SwVbaRows::SetLeftIndent( float LeftIndent, ::sal_Int32 RulerStyle ) throw (uno::RuntimeException, std::exception)
198 : {
199 0 : uno::Reference< word::XColumns > xColumns( new SwVbaColumns( getParent(), mxContext, mxTextTable, mxTextTable->getColumns() ) );
200 0 : sal_Int32 nIndent = (sal_Int32)( LeftIndent );
201 0 : switch( RulerStyle )
202 : {
203 : case word::WdRulerStyle::wdAdjustFirstColumn:
204 : {
205 0 : setIndentWithAdjustFirstColumn( xColumns, nIndent );
206 0 : break;
207 : }
208 : case word::WdRulerStyle::wdAdjustNone:
209 : {
210 0 : setIndentWithAdjustNone( nIndent );
211 0 : break;
212 : }
213 : case word::WdRulerStyle::wdAdjustProportional:
214 : {
215 0 : setIndentWithAdjustProportional( xColumns, nIndent );
216 0 : break;
217 : }
218 : case word::WdRulerStyle::wdAdjustSameWidth:
219 : {
220 0 : setIndentWithAdjustSameWidth( xColumns, nIndent );
221 0 : break;
222 : }
223 : default:
224 : {
225 0 : DebugHelper::exception(SbERR_BAD_ARGUMENT, OUString());
226 : }
227 0 : }
228 0 : }
229 :
230 0 : void SwVbaRows::setIndentWithAdjustNone( sal_Int32 indent ) throw (uno::RuntimeException)
231 : {
232 0 : uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
233 0 : sal_Int32 nMargin = 0;
234 0 : xTableProps->getPropertyValue("LeftMargin") >>= nMargin;
235 0 : nMargin += indent;
236 0 : xTableProps->setPropertyValue("LeftMargin", uno::makeAny( nMargin ) );
237 0 : }
238 :
239 0 : void SwVbaRows::setIndentWithAdjustFirstColumn( const uno::Reference< word::XColumns >& xColumns, sal_Int32 indent ) throw (uno::RuntimeException)
240 : {
241 0 : sal_Int32 nIndex = 1;
242 0 : uno::Reference< XCollection > xCol( xColumns, uno::UNO_QUERY_THROW );
243 0 : uno::Reference< word::XColumn > xColumn( xCol->Item( uno::makeAny( nIndex ), uno::Any() ), uno::UNO_QUERY_THROW );
244 0 : sal_Int32 nWidth = xColumn->getWidth();
245 0 : nWidth -= indent;
246 0 : xColumn->setWidth( nWidth );
247 0 : setIndentWithAdjustNone( indent );
248 0 : }
249 :
250 0 : void SwVbaRows::setIndentWithAdjustProportional(
251 : const uno::Reference< word::XColumns >& xColumns,
252 : sal_Int32 indent
253 : ) throw (uno::RuntimeException)
254 : {
255 : // calculate the new width and get the proportion between old and new
256 0 : uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
257 0 : sal_Int32 nWidth = 0;
258 0 : xTableProps->getPropertyValue("Width") >>= nWidth;
259 0 : sal_Int32 nNewWidth = nWidth - indent;
260 0 : if ((nNewWidth <= 0) || (nWidth <= 0))
261 : {
262 : throw uno::RuntimeException(
263 : OUString("Pb with width, in SwVbaRows::setIndentWithAdjustProportional "
264 : "(nNewWidth <= 0) || (nWidth <= 0)"),
265 : uno::Reference< uno::XInterface >()
266 0 : );
267 : }
268 0 : double propFactor = (double)nNewWidth/(double)nWidth;
269 :
270 : // get all columns, calculate and set the new width of the columns
271 0 : uno::Reference< XCollection > xCol( xColumns, uno::UNO_QUERY_THROW );
272 0 : sal_Int32 nColCount = xCol->getCount();
273 0 : for( sal_Int32 i = 0; i < nColCount; i++ )
274 : {
275 0 : uno::Reference< word::XColumn > xColumn( xCol->Item( uno::makeAny( i ), uno::Any() ), uno::UNO_QUERY_THROW );
276 0 : sal_Int32 nColWidth = xColumn->getWidth();
277 0 : sal_Int32 nNewColWidth = ( sal_Int32 )( propFactor * nColWidth );
278 0 : xColumn->setWidth( nNewColWidth );
279 0 : }
280 :
281 : // set the width and position of the table
282 0 : setIndentWithAdjustNone( indent );
283 0 : xTableProps->setPropertyValue("Width", uno::makeAny( nNewWidth ) );
284 0 : }
285 :
286 0 : void SwVbaRows::setIndentWithAdjustSameWidth( const uno::Reference< word::XColumns >& xColumns, sal_Int32 indent ) throw (uno::RuntimeException)
287 : {
288 : // calculate the new width and get the width of all columns
289 0 : uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
290 0 : sal_Int32 nWidth = 0;
291 0 : xTableProps->getPropertyValue("Width") >>= nWidth;
292 0 : sal_Int32 nNewWidth = nWidth - indent;
293 :
294 : // get all columns, calculate and set the new width of the columns
295 0 : uno::Reference< XCollection > xCol( xColumns, uno::UNO_QUERY_THROW );
296 0 : sal_Int32 nColCount = xCol->getCount();
297 0 : sal_Int32 nNewColWidth = (sal_Int32)( double( nNewWidth )/nColCount );
298 0 : for( sal_Int32 i = 0; i < nColCount; i++ )
299 : {
300 0 : uno::Reference< word::XColumn > xColumn( xCol->Item( uno::makeAny( i ), uno::Any() ), uno::UNO_QUERY_THROW );
301 0 : xColumn->setWidth( nNewColWidth );
302 0 : }
303 :
304 : // set the width and position of the table
305 0 : setIndentWithAdjustNone( indent );
306 0 : xTableProps->setPropertyValue("Width", uno::makeAny( nNewWidth ) );
307 0 : }
308 :
309 0 : void SAL_CALL SwVbaRows::Select( ) throw (uno::RuntimeException, std::exception)
310 : {
311 0 : SwVbaRow::SelectRow( getCurrentWordDoc(mxContext), mxTextTable, mnStartRowIndex, mnEndRowIndex );
312 0 : }
313 :
314 0 : ::sal_Int32 SAL_CALL SwVbaRows::getCount() throw (uno::RuntimeException)
315 : {
316 0 : return ( mnEndRowIndex - mnStartRowIndex + 1 );
317 : }
318 :
319 0 : uno::Any SAL_CALL SwVbaRows::Item( const uno::Any& Index1, const uno::Any& /*not processed in this base class*/ ) throw (uno::RuntimeException)
320 : {
321 0 : sal_Int32 nIndex = 0;
322 0 : if( ( Index1 >>= nIndex ) )
323 : {
324 0 : if( nIndex <= 0 || nIndex > getCount() )
325 : {
326 0 : throw lang::IndexOutOfBoundsException("Index out of bounds", uno::Reference< uno::XInterface >() );
327 : }
328 0 : return uno::makeAny( uno::Reference< word::XRow >( new SwVbaRow( this, mxContext, mxTextTable, nIndex - 1 ) ) );
329 : }
330 0 : throw uno::RuntimeException("Index out of bounds", uno::Reference< uno::XInterface >() );
331 : }
332 :
333 : // XEnumerationAccess
334 : uno::Type
335 0 : SwVbaRows::getElementType() throw (uno::RuntimeException)
336 : {
337 0 : return cppu::UnoType<word::XRow>::get();
338 : }
339 : uno::Reference< container::XEnumeration >
340 0 : SwVbaRows::createEnumeration() throw (uno::RuntimeException)
341 : {
342 0 : return new RowsEnumWrapper( this, mxContext, mxTextTable );
343 : }
344 :
345 : uno::Any
346 0 : SwVbaRows::createCollectionObject( const uno::Any& aSource )
347 : {
348 0 : return aSource;
349 : }
350 :
351 : OUString
352 0 : SwVbaRows::getServiceImplName()
353 : {
354 0 : return OUString("SwVbaRows");
355 : }
356 :
357 : uno::Sequence<OUString>
358 0 : SwVbaRows::getServiceNames()
359 : {
360 0 : static uno::Sequence< OUString > sNames;
361 0 : if ( sNames.getLength() == 0 )
362 : {
363 0 : sNames.realloc( 1 );
364 0 : sNames[0] = "ooo.vba.word.Rows";
365 : }
366 0 : return sNames;
367 : }
368 :
369 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|