LibreOffice Module sw (master)  1
vbaborders.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 "vbaborders.hxx"
20 #include <ooo/vba/word/XBorder.hpp>
21 #include <ooo/vba/word/WdBorderType.hpp>
22 #include <ooo/vba/word/WdLineStyle.hpp>
23 #include <sal/macros.h>
24 #include <cppuhelper/implbase.hxx>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/table/TableBorder.hpp>
27 #include <com/sun/star/table/ShadowFormat.hpp>
28 #include <com/sun/star/table/ShadowLocation.hpp>
29 #include "vbapalette.hxx"
30 
31 using namespace ::com::sun::star;
32 using namespace ::ooo::vba;
33 
34 typedef ::cppu::WeakImplHelper<container::XIndexAccess > RangeBorders_Base;
36 
37 // #TODO sort these indexes to match the order in which Word iterates over the
38 // borders, the enumeration will match the order in this list
39 static const sal_Int16 supportedIndexTable[] = { word::WdBorderType::wdBorderBottom, word::WdBorderType::wdBorderDiagonalDown, word::WdBorderType::wdBorderDiagonalUp, word::WdBorderType::wdBorderHorizontal, word::WdBorderType::wdBorderLeft, word::WdBorderType::wdBorderRight, word::WdBorderType::wdBorderTop, word::WdBorderType::wdBorderVertical };
40 
41 // Equiv widths in 1/100 mm
42 const static sal_Int32 OOLineHairline = 2;
43 
45 {
46 private:
47  uno::Reference< beans::XPropertySet > m_xProps;
48  sal_Int32 const m_LineType;
49  void setBorderLine( table::BorderLine const & rBorderLine )
50  {
51  table::TableBorder aTableBorder;
52  m_xProps->getPropertyValue( "TableBorder" ) >>= aTableBorder;
53 
54  switch ( m_LineType )
55  {
56  case word::WdBorderType::wdBorderLeft:
57  aTableBorder.IsLeftLineValid = true;
58  aTableBorder.LeftLine= rBorderLine;
59  break;
60  case word::WdBorderType::wdBorderTop:
61  aTableBorder.IsTopLineValid = true;
62  aTableBorder.TopLine = rBorderLine;
63  break;
64 
65  case word::WdBorderType::wdBorderBottom:
66  aTableBorder.IsBottomLineValid = true;
67  aTableBorder.BottomLine = rBorderLine;
68  break;
69  case word::WdBorderType::wdBorderRight:
70  aTableBorder.IsRightLineValid = true;
71  aTableBorder.RightLine = rBorderLine;
72  break;
73  case word::WdBorderType::wdBorderVertical:
74  aTableBorder.IsVerticalLineValid = true;
75  aTableBorder.VerticalLine = rBorderLine;
76  break;
77  case word::WdBorderType::wdBorderHorizontal:
78  aTableBorder.IsHorizontalLineValid = true;
79  aTableBorder.HorizontalLine = rBorderLine;
80  break;
81  case word::WdBorderType::wdBorderDiagonalDown:
82  case word::WdBorderType::wdBorderDiagonalUp:
83  // #TODO have to ignore at the moment, would be
84  // nice to investigate what we can do here
85  break;
86  default:
87  return;
88  }
89  m_xProps->setPropertyValue( "TableBorder", uno::makeAny(aTableBorder) );
90  }
91 
92  bool getBorderLine( table::BorderLine& rBorderLine )
93  {
94  table::TableBorder aTableBorder;
95  m_xProps->getPropertyValue( "TableBorder" ) >>= aTableBorder;
96  switch ( m_LineType )
97  {
98  case word::WdBorderType::wdBorderLeft:
99  if ( aTableBorder.IsLeftLineValid )
100  rBorderLine = aTableBorder.LeftLine;
101  break;
102  case word::WdBorderType::wdBorderTop:
103  if ( aTableBorder.IsTopLineValid )
104  rBorderLine = aTableBorder.TopLine;
105  break;
106  case word::WdBorderType::wdBorderBottom:
107  if ( aTableBorder.IsBottomLineValid )
108  rBorderLine = aTableBorder.BottomLine;
109  break;
110  case word::WdBorderType::wdBorderRight:
111  if ( aTableBorder.IsRightLineValid )
112  rBorderLine = aTableBorder.RightLine;
113  break;
114  case word::WdBorderType::wdBorderVertical:
115  if ( aTableBorder.IsVerticalLineValid )
116  rBorderLine = aTableBorder.VerticalLine;
117  break;
118  case word::WdBorderType::wdBorderHorizontal:
119  if ( aTableBorder.IsHorizontalLineValid )
120  rBorderLine = aTableBorder.HorizontalLine;
121  break;
122 
123  case word::WdBorderType::wdBorderDiagonalDown:
124  case word::WdBorderType::wdBorderDiagonalUp:
125  // #TODO have to ignore at the moment, would be
126  // nice to investigate what we can do here
127  break;
128  default:
129  return false;
130  }
131  return true;
132  }
133 
134 protected:
135  virtual OUString getServiceImplName() override
136  {
137  return "SwVbaBorder";
138  }
139 
140  virtual css::uno::Sequence<OUString> getServiceNames() override
141  {
142  static uno::Sequence< OUString > const aServiceNames
143  {
144  "ooo.vba.word.Border"
145  };
146  return aServiceNames;
147  }
148 public:
149  SwVbaBorder( const uno::Reference< beans::XPropertySet > & xProps, const uno::Reference< uno::XComponentContext >& xContext, sal_Int32 lineType ) : SwVbaBorder_Base( uno::Reference< XHelperInterface >( xProps, uno::UNO_QUERY ), xContext ), m_xProps( xProps ), m_LineType( lineType ) {}
150 
151  uno::Any SAL_CALL getLineStyle() override
152  {
153  sal_Int32 nLineStyle = word::WdLineStyle::wdLineStyleNone;
154  table::BorderLine aBorderLine;
155  if ( getBorderLine( aBorderLine ) )
156  {
157  if( aBorderLine.InnerLineWidth !=0 && aBorderLine.OuterLineWidth !=0 )
158  {
159  nLineStyle = word::WdLineStyle::wdLineStyleDouble;
160  }
161  else if( aBorderLine.InnerLineWidth !=0 || aBorderLine.OuterLineWidth !=0 )
162  {
163  nLineStyle = word::WdLineStyle::wdLineStyleSingle;
164  }
165  else
166  {
167  nLineStyle = word::WdLineStyle::wdLineStyleNone;
168  }
169  }
170  return uno::makeAny( nLineStyle );
171  }
172  void SAL_CALL setLineStyle( const uno::Any& _linestyle ) override
173  {
174  // Urk no choice but to silently ignore we don't support this attribute
175  // #TODO would be nice to support the word line styles
176  sal_Int32 nLineStyle = 0;
177  _linestyle >>= nLineStyle;
178  table::BorderLine aBorderLine;
179  if ( !getBorderLine( aBorderLine ) )
180  throw uno::RuntimeException("Method failed" );
181 
182  switch ( nLineStyle )
183  {
184  case word::WdLineStyle::wdLineStyleNone:
185  {
186  aBorderLine.InnerLineWidth = 0;
187  aBorderLine.OuterLineWidth = 0;
188  break;
189  }
190  case word::WdLineStyle::wdLineStyleDashDot:
191  case word::WdLineStyle::wdLineStyleDashDotDot:
192  case word::WdLineStyle::wdLineStyleDashDotStroked:
193  case word::WdLineStyle::wdLineStyleDashLargeGap:
194  case word::WdLineStyle::wdLineStyleDashSmallGap:
195  case word::WdLineStyle::wdLineStyleDot:
196  case word::WdLineStyle::wdLineStyleDouble:
197  case word::WdLineStyle::wdLineStyleDoubleWavy:
198  case word::WdLineStyle::wdLineStyleEmboss3D:
199  case word::WdLineStyle::wdLineStyleEngrave3D:
200  case word::WdLineStyle::wdLineStyleInset:
201  case word::WdLineStyle::wdLineStyleOutset:
202  case word::WdLineStyle::wdLineStyleSingle:
203  case word::WdLineStyle::wdLineStyleSingleWavy:
204  case word::WdLineStyle::wdLineStyleThickThinLargeGap:
205  case word::WdLineStyle::wdLineStyleThickThinMedGap:
206  case word::WdLineStyle::wdLineStyleThickThinSmallGap:
207  case word::WdLineStyle::wdLineStyleThinThickLargeGap:
208  case word::WdLineStyle::wdLineStyleThinThickMedGap:
209  case word::WdLineStyle::wdLineStyleThinThickSmallGap:
210  case word::WdLineStyle::wdLineStyleThinThickThinLargeGap:
211  case word::WdLineStyle::wdLineStyleThinThickThinMedGap:
212  case word::WdLineStyle::wdLineStyleThinThickThinSmallGap:
213  case word::WdLineStyle::wdLineStyleTriple:
214  {
215  aBorderLine.InnerLineWidth = 0;
216  aBorderLine.OuterLineWidth = OOLineHairline;
217  break;
218  }
219  default:
220  throw uno::RuntimeException("Bad param" );
221  }
222  setBorderLine( aBorderLine );
223 
224  }
225 };
226 
227 class RangeBorders : public RangeBorders_Base
228 {
229 private:
230  uno::Reference< table::XCellRange > m_xRange;
231  uno::Reference< uno::XComponentContext > m_xContext;
232  VbaPalette const m_Palette;
233  sal_Int32 getTableIndex( sal_Int32 nConst )
234  {
235  // okay return position of the index in the table
236  sal_Int32 nIndexes = getCount();
237  sal_Int32 realIndex = 0;
238  const sal_Int16* pTableEntry = supportedIndexTable;
239  for ( ; realIndex < nIndexes; ++realIndex, ++pTableEntry )
240  {
241  if ( *pTableEntry == nConst )
242  return realIndex;
243  }
244  return getCount(); // error condition
245  }
246 public:
247  RangeBorders( const uno::Reference< table::XCellRange >& xRange, const uno::Reference< uno::XComponentContext > & xContext, VbaPalette const & rPalette ) : m_xRange( xRange ), m_xContext( xContext ), m_Palette( rPalette )
248  {
249  }
250  // XIndexAccess
251  virtual ::sal_Int32 SAL_CALL getCount( ) override
252  {
254  }
255  virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
256  {
257 
258  sal_Int32 nIndex = getTableIndex( Index );
259  if ( nIndex >= 0 && nIndex < getCount() )
260  {
261  uno::Reference< beans::XPropertySet > xProps( m_xRange, uno::UNO_QUERY_THROW );
262  return uno::makeAny( uno::Reference< word::XBorder >( new SwVbaBorder( xProps, m_xContext, supportedIndexTable[ nIndex ] )) );
263  }
264  throw lang::IndexOutOfBoundsException();
265  }
266  virtual uno::Type SAL_CALL getElementType( ) override
267  {
269  }
270  virtual sal_Bool SAL_CALL hasElements( ) override
271  {
272  return true;
273  }
274 };
275 
276 static uno::Reference< container::XIndexAccess >
277 rangeToBorderIndexAccess( const uno::Reference< table::XCellRange >& xRange, const uno::Reference< uno::XComponentContext > & xContext, VbaPalette const & rPalette )
278 {
279  return new RangeBorders( xRange, xContext, rPalette );
280 }
281 
283 {
284  uno::Reference<container::XIndexAccess > m_xIndexAccess;
285  sal_Int32 nIndex;
286 public:
287  explicit RangeBorderEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
288  virtual sal_Bool SAL_CALL hasMoreElements( ) override
289  {
290  return ( nIndex < m_xIndexAccess->getCount() );
291  }
292 
293  virtual uno::Any SAL_CALL nextElement( ) override
294  {
295  if ( nIndex < m_xIndexAccess->getCount() )
296  return m_xIndexAccess->getByIndex( nIndex++ );
297  throw container::NoSuchElementException();
298  }
299 };
300 
301 // for Table borders
302 SwVbaBorders::SwVbaBorders( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< table::XCellRange >& xRange, VbaPalette const & rPalette ): SwVbaBorders_BASE( xParent, xContext, rangeToBorderIndexAccess( xRange ,xContext, rPalette ) )
303 {
304  m_xProps.set( xRange, uno::UNO_QUERY_THROW );
305 }
306 
307 uno::Reference< container::XEnumeration >
309 {
310  return new RangeBorderEnumWrapper( m_xIndexAccess );
311 }
312 
313 uno::Any
314 SwVbaBorders::createCollectionObject( const css::uno::Any& aSource )
315 {
316  return aSource; // it's already a Border object
317 }
318 
319 uno::Type
321 {
323 }
324 
325 uno::Any
326 SwVbaBorders::getItemByIntIndex( const sal_Int32 nIndex )
327 {
328  return createCollectionObject( m_xIndexAccess->getByIndex( nIndex ) );
329 }
330 
332 {
333  // always return False for table border in MS Word
334  return false;
335 }
336 
337 void SAL_CALL SwVbaBorders::setShadow( sal_Bool /*_shadow*/ )
338 {
339  // not support in Table border in Word
340  // TODO:
341 }
342 
343 OUString
345 {
346  return "SwVbaBorders";
347 }
348 
349 uno::Sequence< OUString >
351 {
352  static uno::Sequence< OUString > const aServiceNames
353  {
354  "ooo.vba.word.Borders"
355  };
356  return aServiceNames;
357 }
358 
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno::Reference< container::XIndexAccess > m_xIndexAccess
Definition: vbaborders.cxx:284
virtual uno::Type SAL_CALL getElementType() override
RangeBorders(const uno::Reference< table::XCellRange > &xRange, const uno::Reference< uno::XComponentContext > &xContext, const ScVbaPalette &rPalette)
virtual OUString getServiceImplName() override
Definition: vbaborders.cxx:135
uno::Reference< table::XCellRange > m_xRange
Definition: vbaborders.cxx:230
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbaborders.cxx:308
virtual OUString getServiceImplName() override
Definition: vbaborders.cxx:344
sal_Int32 getTableIndex(sal_Int32 nConst)
css::uno::Reference< css::beans::XPropertySet > m_xProps
Definition: vbaborders.hxx:34
Reference
ScVbaPalette m_Palette
static const sal_Int32 OOLineHairline
Definition: vbaborders.cxx:42
InheritedHelperInterfaceWeakImpl< word::XBorder > SwVbaBorder_Base
Definition: vbaborders.cxx:35
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
uno::Any SAL_CALL getLineStyle() override
Definition: vbaborders.cxx:151
RangeBorderEnumWrapper(const uno::Reference< container::XIndexAccess > &xIndexAccess)
sal_Int32 const m_LineType
Definition: vbaborders.cxx:48
uno::Reference< beans::XPropertySet > m_xProps
Definition: vbaborders.cxx:47
#define SAL_N_ELEMENTS(arr)
virtual uno::Any SAL_CALL nextElement() override
virtual void SAL_CALL setShadow(sal_Bool _shadow) override
Definition: vbaborders.cxx:337
unsigned char sal_Bool
virtual sal_Bool SAL_CALL getShadow() override
Definition: vbaborders.cxx:331
virtual ::sal_Int32 SAL_CALL getCount() override
css::uno::Type const & get()
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbaborders.cxx:350
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbaborders.cxx:320
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
Definition: vbaborders.cxx:314
virtual css::uno::Any getItemByIntIndex(const sal_Int32 nIndex) override
Definition: vbaborders.cxx:326
static const sal_Int16 supportedIndexTable[]
Definition: vbaborders.cxx:39
SwVbaBorder(const uno::Reference< beans::XPropertySet > &xProps, const uno::Reference< uno::XComponentContext > &xContext, sal_Int32 lineType)
Definition: vbaborders.cxx:149
bool getBorderLine(table::BorderLine &rBorderLine)
Definition: vbaborders.cxx:92
virtual sal_Bool SAL_CALL hasElements() override
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbaborders.cxx:140
static uno::Reference< container::XIndexAccess > rangeToBorderIndexAccess(const uno::Reference< table::XCellRange > &xRange, const uno::Reference< uno::XComponentContext > &xContext, VbaPalette const &rPalette)
Definition: vbaborders.cxx:277
virtual uno::Any SAL_CALL getByIndex(::sal_Int32 Index) override
void SAL_CALL setLineStyle(const uno::Any &_linestyle) override
Definition: vbaborders.cxx:172
::cppu::WeakImplHelper< container::XIndexAccess > RangeBorders_Base
Definition: vbaborders.cxx:34
void setBorderLine(table::BorderLine const &rBorderLine)
Definition: vbaborders.cxx:49
uno::Reference< uno::XComponentContext > m_xContext
Definition: vbaborders.cxx:231
virtual sal_Bool SAL_CALL hasMoreElements() override
SwVbaBorders(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::table::XCellRange > &xRange, VbaPalette const &rPalette)
Definition: vbaborders.cxx:302