1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 using namespace ::ooo::vba
;
31 using namespace ::com::sun::star
;
33 /// @throws uno::RuntimeException
34 static uno::Sequence
< style::TabStop
> lcl_getTabStops( const uno::Reference
< beans::XPropertySet
>& xParaProps
)
36 uno::Sequence
< style::TabStop
> aSeq
;
37 xParaProps
->getPropertyValue("ParaTabStops") >>= aSeq
;
41 /// @throws uno::RuntimeException
42 static void lcl_setTabStops( const uno::Reference
< beans::XPropertySet
>& xParaProps
, const uno::Sequence
< style::TabStop
>& aSeq
)
44 xParaProps
->setPropertyValue("ParaTabStops", uno::Any( aSeq
) );
49 class TabStopsEnumWrapper
: public EnumerationHelper_BASE
51 uno::Reference
< container::XIndexAccess
> mxIndexAccess
;
55 explicit TabStopsEnumWrapper( uno::Reference
< container::XIndexAccess
> xIndexAccess
) : mxIndexAccess(std::move( xIndexAccess
)), m_nIndex( 0 )
58 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
60 return ( m_nIndex
< mxIndexAccess
->getCount() );
63 virtual uno::Any SAL_CALL
nextElement( ) override
65 if( m_nIndex
< mxIndexAccess
->getCount() )
67 return mxIndexAccess
->getByIndex( m_nIndex
++ );
69 throw container::NoSuchElementException();
73 class TabStopCollectionHelper
: public ::cppu::WeakImplHelper
< container::XIndexAccess
,
74 container::XEnumerationAccess
>
77 uno::Reference
< XHelperInterface
> mxParent
;
78 uno::Reference
< uno::XComponentContext
> mxContext
;
82 /// @throws css::uno::RuntimeException
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())
87 virtual sal_Int32 SAL_CALL
getCount( ) override
91 virtual uno::Any SAL_CALL
getByIndex( sal_Int32 Index
) override
93 if ( Index
< 0 || Index
>= getCount() )
94 throw css::lang::IndexOutOfBoundsException();
96 return uno::Any( uno::Reference
< word::XTabStop
>( new SwVbaTabStop( mxParent
, mxContext
) ) );
98 virtual uno::Type SAL_CALL
getElementType( ) override
100 return cppu::UnoType
<word::XTabStop
>::get();
102 virtual sal_Bool SAL_CALL
hasElements( ) override
106 // XEnumerationAccess
107 virtual uno::Reference
< container::XEnumeration
> SAL_CALL
createEnumeration( ) override
109 return new TabStopsEnumWrapper( this );
115 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
)
119 uno::Reference
< word::XTabStop
> SAL_CALL
SwVbaTabStops::Add( float Position
, const uno::Any
& Alignment
, const uno::Any
& Leader
)
121 sal_Int32 nPosition
= Millimeter::getInHundredthsOfOneMillimeter( Position
);
123 style::TabAlign nAlign
= style::TabAlign_LEFT
;
124 if( Alignment
.hasValue() )
126 sal_Int32 wdAlign
= word::WdTabAlignment::wdAlignTabLeft
;
127 Alignment
>>= wdAlign
;
130 case word::WdTabAlignment::wdAlignTabLeft
:
132 nAlign
= style::TabAlign_LEFT
;
135 case word::WdTabAlignment::wdAlignTabRight
:
137 nAlign
= style::TabAlign_RIGHT
;
140 case word::WdTabAlignment::wdAlignTabCenter
:
142 nAlign
= style::TabAlign_CENTER
;
145 case word::WdTabAlignment::wdAlignTabDecimal
:
147 nAlign
= style::TabAlign_DECIMAL
;
150 case word::WdTabAlignment::wdAlignTabBar
:
151 case word::WdTabAlignment::wdAlignTabList
:
153 DebugHelper::basicexception( ERRCODE_BASIC_NOT_IMPLEMENTED
, {} );
163 sal_Unicode cLeader
= ' '; // default is space
164 if( Leader
.hasValue() )
166 sal_Int32 wdLeader
= word::WdTabLeader::wdTabLeaderSpaces
;
170 case word::WdTabLeader::wdTabLeaderSpaces
:
175 case word::WdTabLeader::wdTabLeaderMiddleDot
:
177 cLeader
= 183; // U+00B7 MIDDLE DOT
180 case word::WdTabLeader::wdTabLeaderDots
:
185 case word::WdTabLeader::wdTabLeaderDashes
:
186 case word::WdTabLeader::wdTabLeaderHeavy
:
187 case word::WdTabLeader::wdTabLeaderLines
:
200 aTab
.Position
= nPosition
;
201 aTab
.Alignment
= nAlign
;
202 aTab
.DecimalChar
= '.'; // default value
203 aTab
.FillChar
= cLeader
;
205 uno::Sequence
< style::TabStop
> aOldTabs
= lcl_getTabStops( mxParaProps
);
206 auto [begin
, end
] = asNonConstRange(aOldTabs
);
208 style::TabStop
* pOldTab
= std::find_if(begin
, end
,
209 [nPosition
](const style::TabStop
& rTab
) { return rTab
.Position
== nPosition
; });
210 bool bOverWriter
= pOldTab
!= end
;
214 lcl_setTabStops( mxParaProps
, aOldTabs
);
218 sal_Int32 nTabs
= aOldTabs
.getLength();
219 uno::Sequence
< style::TabStop
> aNewTabs( nTabs
+ 1 );
221 auto it
= aNewTabs
.getArray();
223 std::copy(begin
, end
, std::next(it
));
224 lcl_setTabStops( mxParaProps
, aNewTabs
);
227 return uno::Reference
< word::XTabStop
>( new SwVbaTabStop( this, mxContext
) );
230 void SAL_CALL
SwVbaTabStops::ClearAll()
232 uno::Sequence
< style::TabStop
> aSeq
;
233 lcl_setTabStops( mxParaProps
, aSeq
);
236 // XEnumerationAccess
238 SwVbaTabStops::getElementType()
240 return cppu::UnoType
<word::XTabStop
>::get();
242 uno::Reference
< container::XEnumeration
>
243 SwVbaTabStops::createEnumeration()
245 return new TabStopsEnumWrapper( m_xIndexAccess
);
249 SwVbaTabStops::createCollectionObject( const css::uno::Any
& aSource
)
255 SwVbaTabStops::getServiceImplName()
257 return "SwVbaTabStops";
260 css::uno::Sequence
<OUString
>
261 SwVbaTabStops::getServiceNames()
263 static uno::Sequence
< OUString
> const sNames
265 "ooo.vba.word.TabStops"
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */