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