LibreOffice Module sw (master)  1
vbatabstops.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 "vbatabstops.hxx"
20 #include "vbatabstop.hxx"
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/style/TabAlign.hpp>
23 #include <com/sun/star/style/TabStop.hpp>
24 #include <ooo/vba/word/WdTabLeader.hpp>
25 #include <ooo/vba/word/WdTabAlignment.hpp>
26 #include <basic/sberrors.hxx>
27 #include <cppuhelper/implbase.hxx>
28 
29 using namespace ::ooo::vba;
30 using namespace ::com::sun::star;
31 
33 static uno::Sequence< style::TabStop > lcl_getTabStops( const uno::Reference< beans::XPropertySet >& xParaProps )
34 {
35  uno::Sequence< style::TabStop > aSeq;
36  xParaProps->getPropertyValue("ParaTabStops") >>= aSeq;
37  return aSeq;
38 }
39 
41 static void lcl_setTabStops( const uno::Reference< beans::XPropertySet >& xParaProps, const uno::Sequence< style::TabStop >& aSeq )
42 {
43  xParaProps->setPropertyValue("ParaTabStops", uno::makeAny( aSeq ) );
44 }
45 
47 {
48  uno::Reference< container::XIndexAccess > mxIndexAccess;
49  sal_Int32 nIndex;
50 
51 public:
52  explicit TabStopsEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : mxIndexAccess( xIndexAccess ), nIndex( 0 )
53  {
54  }
55  virtual sal_Bool SAL_CALL hasMoreElements( ) override
56  {
57  return ( nIndex < mxIndexAccess->getCount() );
58  }
59 
60  virtual uno::Any SAL_CALL nextElement( ) override
61  {
62  if( nIndex < mxIndexAccess->getCount() )
63  {
64  return mxIndexAccess->getByIndex( nIndex++ );
65  }
66  throw container::NoSuchElementException();
67  }
68 };
69 
70 class TabStopCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
71  container::XEnumerationAccess >
72 {
73 private:
74  uno::Reference< XHelperInterface > mxParent;
75  uno::Reference< uno::XComponentContext > mxContext;
76  sal_Int32 mnTabStops;
77 
78 public:
80  TabStopCollectionHelper( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext > & xContext, const css::uno::Reference< css::beans::XPropertySet >& xParaProps ): mxParent( xParent ), mxContext( xContext )
81  {
82  mnTabStops = lcl_getTabStops( xParaProps ).getLength();
83  }
84 
85  virtual sal_Int32 SAL_CALL getCount( ) override
86  {
87  return mnTabStops;
88  }
89  virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override
90  {
92  throw css::lang::IndexOutOfBoundsException();
93 
94  return uno::makeAny( uno::Reference< word::XTabStop >( new SwVbaTabStop( mxParent, mxContext ) ) );
95  }
96  virtual uno::Type SAL_CALL getElementType( ) override
97  {
99  }
100  virtual sal_Bool SAL_CALL hasElements( ) override
101  {
102  return true;
103  }
104  // XEnumerationAccess
105  virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
106  {
107  return new TabStopsEnumWrapper( this );
108  }
109 };
110 
111 SwVbaTabStops::SwVbaTabStops( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< beans::XPropertySet >& xParaProps ) : SwVbaTabStops_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new TabStopCollectionHelper( xParent, xContext, xParaProps ) ) ), mxParaProps( xParaProps )
112 {
113 }
114 
115 uno::Reference< word::XTabStop > SAL_CALL SwVbaTabStops::Add( float Position, const uno::Any& Alignment, const uno::Any& Leader )
116 {
117  sal_Int32 nPosition = Millimeter::getInHundredthsOfOneMillimeter( Position );
118 
119  style::TabAlign nAlign = style::TabAlign_LEFT;
120  if( Alignment.hasValue() )
121  {
122  sal_Int32 wdAlign = word::WdTabAlignment::wdAlignTabLeft;
123  Alignment >>= wdAlign;
124  switch( wdAlign )
125  {
126  case word::WdTabAlignment::wdAlignTabLeft:
127  {
128  nAlign = style::TabAlign_LEFT;
129  break;
130  }
131  case word::WdTabAlignment::wdAlignTabRight:
132  {
133  nAlign = style::TabAlign_RIGHT;
134  break;
135  }
136  case word::WdTabAlignment::wdAlignTabCenter:
137  {
138  nAlign = style::TabAlign_CENTER;
139  break;
140  }
141  case word::WdTabAlignment::wdAlignTabDecimal:
142  {
143  nAlign = style::TabAlign_DECIMAL;
144  break;
145  }
146  case word::WdTabAlignment::wdAlignTabBar:
147  case word::WdTabAlignment::wdAlignTabList:
148  {
149  DebugHelper::basicexception( ERRCODE_BASIC_NOT_IMPLEMENTED, OUString() );
150  break;
151  }
152  default:
153  {
154  //left
155  }
156  }
157  }
158 
159  sal_Unicode cLeader = ' '; // default is space
160  if( Leader.hasValue() )
161  {
162  sal_Int32 wdLeader = word::WdTabLeader::wdTabLeaderSpaces;
163  Leader >>= wdLeader;
164  switch( wdLeader )
165  {
166  case word::WdTabLeader::wdTabLeaderSpaces:
167  {
168  cLeader = ' ';
169  break;
170  }
171  case word::WdTabLeader::wdTabLeaderMiddleDot:
172  {
173  cLeader = 183; // U+00B7 MIDDLE DOT
174  break;
175  }
176  case word::WdTabLeader::wdTabLeaderDots:
177  {
178  cLeader = '.';
179  break;
180  }
181  case word::WdTabLeader::wdTabLeaderDashes:
182  case word::WdTabLeader::wdTabLeaderHeavy:
183  case word::WdTabLeader::wdTabLeaderLines:
184  {
185  cLeader = '_';
186  break;
187  }
188  default:
189  {
190  //left
191  }
192  }
193  }
194 
195  style::TabStop aTab;
196  aTab.Position = nPosition;
197  aTab.Alignment = nAlign;
198  aTab.DecimalChar = '.'; // default value
199  aTab.FillChar = cLeader;
200 
201  uno::Sequence< style::TabStop > aOldTabs = lcl_getTabStops( mxParaProps );
202 
203  style::TabStop* pOldTab = std::find_if(aOldTabs.begin(), aOldTabs.end(),
204  [nPosition](const style::TabStop& rTab) { return rTab.Position == nPosition; });
205  bool bOverWriter = pOldTab != aOldTabs.end();
206  if( bOverWriter )
207  {
208  *pOldTab = aTab;
209  lcl_setTabStops( mxParaProps, aOldTabs );
210  }
211  else
212  {
213  sal_Int32 nTabs = aOldTabs.getLength();
214  uno::Sequence< style::TabStop > aNewTabs( nTabs + 1 );
215 
216  aNewTabs[0] = aTab;
217  std::copy(aOldTabs.begin(), aOldTabs.end(), std::next(aNewTabs.begin()));
218  lcl_setTabStops( mxParaProps, aNewTabs );
219  }
220 
221  return uno::Reference< word::XTabStop >( new SwVbaTabStop( this, mxContext ) );
222 }
223 
224 void SAL_CALL SwVbaTabStops::ClearAll()
225 {
226  uno::Sequence< style::TabStop > aSeq;
227  lcl_setTabStops( mxParaProps, aSeq );
228 }
229 
230 // XEnumerationAccess
231 uno::Type
233 {
235 }
236 uno::Reference< container::XEnumeration >
238 {
239  return new TabStopsEnumWrapper( m_xIndexAccess );
240 }
241 
242 uno::Any
243 SwVbaTabStops::createCollectionObject( const css::uno::Any& aSource )
244 {
245  return aSource;
246 }
247 
248 OUString
250 {
251  return "SwVbaTabStops";
252 }
253 
254 css::uno::Sequence<OUString>
256 {
257  static uno::Sequence< OUString > const sNames
258  {
259  "ooo.vba.word.TabStops"
260  };
261  return sNames;
262 }
263 
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration() override
TabStopsEnumWrapper(const uno::Reference< container::XIndexAccess > &xIndexAccess)
Definition: vbatabstops.cxx:52
bool hasValue()
TabStopCollectionHelper(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::beans::XPropertySet > &xParaProps)
Definition: vbatabstops.cxx:80
virtual sal_Bool SAL_CALL hasElements() override
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
uno::Reference< uno::XComponentContext > mxContext
Definition: vbatabstops.cxx:75
uno::Reference< XHelperInterface > mxParent
Definition: vbatabstops.cxx:74
virtual sal_Int32 SAL_CALL getCount() override
Definition: vbatabstops.cxx:85
Reference
#define ERRCODE_BASIC_NOT_IMPLEMENTED
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
virtual uno::Any SAL_CALL nextElement() override
Definition: vbatabstops.cxx:60
sal_uInt16 sal_Unicode
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
virtual css::uno::Type SAL_CALL getElementType() override
SwVbaTabStops(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::beans::XPropertySet > &xParaProps)
virtual css::uno::Reference< ::ooo::vba::word::XTabStop > SAL_CALL Add(float Position, const css::uno::Any &Alignment, const css::uno::Any &Leader) override
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: vbatabstops.cxx:55
virtual OUString getServiceImplName() override
unsigned char sal_Bool
css::uno::Type const & get()
virtual uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
Definition: vbatabstops.cxx:89
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual uno::Type SAL_CALL getElementType() override
Definition: vbatabstops.cxx:96
css::uno::Reference< css::beans::XPropertySet > mxParaProps
Definition: vbatabstops.hxx:31
static void lcl_setTabStops(const uno::Reference< beans::XPropertySet > &xParaProps, const uno::Sequence< style::TabStop > &aSeq)
Definition: vbatabstops.cxx:41
virtual void SAL_CALL ClearAll() override
uno::Reference< container::XIndexAccess > mxIndexAccess
Definition: vbatabstops.cxx:48
static uno::Sequence< style::TabStop > lcl_getTabStops(const uno::Reference< beans::XPropertySet > &xParaProps)
Definition: vbatabstops.cxx:33