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