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