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