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