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