1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "vbarows.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>
29 #include "vbacolumns.hxx"
30 #include "vbatablehelper.hxx"
32 using namespace ::ooo::vba
;
33 using namespace ::com::sun::star
;
37 class RowsEnumWrapper
: public EnumerationHelper_BASE
39 uno::WeakReference
< XHelperInterface
> mxParent
;
40 uno::Reference
< uno::XComponentContext
> mxContext
;
41 uno::Reference
< text::XTextTable
> mxTextTable
;
42 uno::Reference
< container::XIndexAccess
> mxIndexAccess
;
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 )
48 mxIndexAccess
= mxTextTable
->getRows();
50 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
52 return ( m_nIndex
< mxIndexAccess
->getCount() );
55 virtual uno::Any SAL_CALL
nextElement( ) override
57 if( m_nIndex
< mxIndexAccess
->getCount() )
59 return uno::Any( uno::Reference
< word::XRow
> ( new SwVbaRow( mxParent
, mxContext
, mxTextTable
, m_nIndex
++ ) ) );
61 throw container::NoSuchElementException();
67 SwVbaRows::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
)
70 mnEndRowIndex
= m_xIndexAccess
->getCount() - 1;
73 SwVbaRows::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
)
75 if( mnEndRowIndex
< mnStartRowIndex
)
76 throw uno::RuntimeException();
80 * get the alignment of the rows: SO format com.sun.star.text.HoriOrientation
81 * is mapped to WdRowAlignment in Word
82 * @return the alignment
84 ::sal_Int32 SAL_CALL
SwVbaRows::getAlignment()
86 sal_Int16 nAlignment
= text::HoriOrientation::LEFT
;
87 uno::Reference
< beans::XPropertySet
> xTableProps( mxTextTable
, uno::UNO_QUERY_THROW
);
88 xTableProps
->getPropertyValue("HoriOrient") >>= nAlignment
;
92 case text::HoriOrientation::CENTER
:
94 nRet
= word::WdRowAlignment::wdAlignRowCenter
;
97 case text::HoriOrientation::RIGHT
:
99 nRet
= word::WdRowAlignment::wdAlignRowRight
;
104 nRet
= word::WdRowAlignment::wdAlignRowLeft
;
110 void SAL_CALL
SwVbaRows::setAlignment( ::sal_Int32 _alignment
)
112 sal_Int16 nAlignment
= text::HoriOrientation::LEFT
;
115 case word::WdRowAlignment::wdAlignRowCenter
:
117 nAlignment
= text::HoriOrientation::CENTER
;
120 case word::WdRowAlignment::wdAlignRowRight
:
122 nAlignment
= text::HoriOrientation::RIGHT
;
127 nAlignment
= text::HoriOrientation::LEFT
;
130 uno::Reference
< beans::XPropertySet
> xTableProps( mxTextTable
, uno::UNO_QUERY_THROW
);
131 xTableProps
->setPropertyValue("HoriOrient", uno::Any( nAlignment
) );
134 uno::Any SAL_CALL
SwVbaRows::getAllowBreakAcrossPages()
136 bool bAllowBreak
= false;
137 uno::Reference
< container::XIndexAccess
> xRowsAccess( mxTableRows
, uno::UNO_QUERY_THROW
);
138 for( sal_Int32 index
= mnStartRowIndex
; index
<= mnEndRowIndex
; ++index
)
140 uno::Reference
< beans::XPropertySet
> xRowProps( xRowsAccess
->getByIndex( index
), uno::UNO_QUERY_THROW
);
142 xRowProps
->getPropertyValue("IsSplitAllowed") >>= bSplit
;
145 bAllowBreak
= bSplit
;
147 if( bSplit
!= bAllowBreak
)
149 return uno::Any( sal_Int32(word::WdConstants::wdUndefined
) );
152 return uno::Any( bAllowBreak
);
155 void SAL_CALL
SwVbaRows::setAllowBreakAcrossPages( const uno::Any
& _allowbreakacrosspages
)
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
)
162 uno::Reference
< beans::XPropertySet
> xRowProps( xRowsAccess
->getByIndex( index
), uno::UNO_QUERY_THROW
);
163 xRowProps
->setPropertyValue("IsSplitAllowed", uno::Any( bAllowBreak
) );
167 float SAL_CALL
SwVbaRows::getSpaceBetweenColumns()
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
) );
179 void SAL_CALL
SwVbaRows::setSpaceBetweenColumns( float _spacebetweencolumns
)
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
)
187 sal_Int32 nColumns
= aTableHelper
.getTabColumnsCount( row
);
188 for( sal_Int32 column
= 0; column
< nColumns
; ++column
)
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
) );
197 void SAL_CALL
SwVbaRows::Delete( )
199 mxTableRows
->removeByIndex( mnStartRowIndex
, getCount() );
202 void SAL_CALL
SwVbaRows::SetLeftIndent( float LeftIndent
, ::sal_Int32 RulerStyle
)
204 uno::Reference
< word::XColumns
> xColumns( new SwVbaColumns( getParent(), mxContext
, mxTextTable
, mxTextTable
->getColumns() ) );
205 sal_Int32 nIndent
= static_cast<sal_Int32
>(LeftIndent
);
208 case word::WdRulerStyle::wdAdjustFirstColumn
:
210 setIndentWithAdjustFirstColumn( xColumns
, nIndent
);
213 case word::WdRulerStyle::wdAdjustNone
:
215 setIndentWithAdjustNone( nIndent
);
218 case word::WdRulerStyle::wdAdjustProportional
:
220 setIndentWithAdjustProportional( xColumns
, nIndent
);
223 case word::WdRulerStyle::wdAdjustSameWidth
:
225 setIndentWithAdjustSameWidth( xColumns
, nIndent
);
230 DebugHelper::runtimeexception(ERRCODE_BASIC_BAD_ARGUMENT
);
235 void SwVbaRows::setIndentWithAdjustNone( sal_Int32 indent
)
237 uno::Reference
< beans::XPropertySet
> xTableProps( mxTextTable
, uno::UNO_QUERY_THROW
);
238 sal_Int32 nMargin
= 0;
239 xTableProps
->getPropertyValue("LeftMargin") >>= nMargin
;
241 xTableProps
->setPropertyValue("LeftMargin", uno::Any( nMargin
) );
244 void SwVbaRows::setIndentWithAdjustFirstColumn( const uno::Reference
< word::XColumns
>& xColumns
, sal_Int32 indent
)
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();
250 xColumn
->setWidth( nWidth
);
251 setIndentWithAdjustNone( indent
);
254 void SwVbaRows::setIndentWithAdjustProportional(
255 const uno::Reference
< word::XColumns
>& xColumns
,
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))
266 throw uno::RuntimeException(
267 "Pb with width, in SwVbaRows::setIndentWithAdjustProportional "
268 "(nNewWidth <= 0) || (nWidth <= 0)"
271 double propFactor
= static_cast<double>(nNewWidth
)/static_cast<double>(nWidth
);
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
++ )
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
);
284 // set the width and position of the table
285 setIndentWithAdjustNone( indent
);
286 xTableProps
->setPropertyValue("Width", uno::Any( nNewWidth
) );
289 void SwVbaRows::setIndentWithAdjustSameWidth( const uno::Reference
< word::XColumns
>& xColumns
, sal_Int32 indent
)
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
;
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
++ )
303 uno::Reference
< word::XColumn
> xColumn( xCol
->Item( uno::Any( i
), uno::Any() ), uno::UNO_QUERY_THROW
);
304 xColumn
->setWidth( nNewColWidth
);
307 // set the width and position of the table
308 setIndentWithAdjustNone( indent
);
309 xTableProps
->setPropertyValue("Width", uno::Any( nNewWidth
) );
312 void SAL_CALL
SwVbaRows::Select( )
314 SwVbaRow::SelectRow( getCurrentWordDoc(mxContext
), mxTextTable
, mnStartRowIndex
, mnEndRowIndex
);
317 ::sal_Int32 SAL_CALL
SwVbaRows::getCount()
319 return ( mnEndRowIndex
- mnStartRowIndex
+ 1 );
322 uno::Any SAL_CALL
SwVbaRows::Item( const uno::Any
& Index1
, const uno::Any
& /*not processed in this base class*/ )
324 sal_Int32 nIndex
= 0;
325 if( Index1
>>= nIndex
)
327 if( nIndex
<= 0 || nIndex
> getCount() )
329 throw lang::IndexOutOfBoundsException("Index out of bounds" );
331 return uno::Any( uno::Reference
< word::XRow
>( new SwVbaRow( this, mxContext
, mxTextTable
, nIndex
- 1 ) ) );
333 throw uno::RuntimeException("Index out of bounds" );
336 // XEnumerationAccess
338 SwVbaRows::getElementType()
340 return cppu::UnoType
<word::XRow
>::get();
342 uno::Reference
< container::XEnumeration
>
343 SwVbaRows::createEnumeration()
345 return new RowsEnumWrapper( this, mxContext
, mxTextTable
);
349 SwVbaRows::createCollectionObject( const uno::Any
& aSource
)
355 SwVbaRows::getServiceImplName()
360 uno::Sequence
<OUString
>
361 SwVbaRows::getServiceNames()
363 static uno::Sequence
< OUString
> const sNames
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */