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 <com/sun/star/beans/XPropertySet.hpp>
20 : #include <com/sun/star/table/XCell.hpp>
21 : #include <com/sun/star/table/XColumnRowRange.hpp>
22 : #include <com/sun/star/beans/XIntrospection.hpp>
23 : #include <com/sun/star/beans/XIntrospectionAccess.hpp>
24 : #include <com/sun/star/reflection/XIdlMethod.hpp>
25 : #include <com/sun/star/beans/MethodConcept.hpp>
26 : #include <com/sun/star/beans/NamedValue.hpp>
27 : #include <com/sun/star/xml/AttributeData.hpp>
28 :
29 : #include <ooo/vba/excel/XlColorIndex.hpp>
30 : #include <ooo/vba/excel/XlPattern.hpp>
31 :
32 : #include <comphelper/processfactory.hxx>
33 : #include <cppuhelper/queryinterface.hxx>
34 :
35 : #include <map>
36 :
37 : #include <sal/macros.h>
38 : #include <svx/xtable.hxx>
39 :
40 : #include "vbainterior.hxx"
41 : #include "vbapalette.hxx"
42 : #include "document.hxx"
43 :
44 : #define COLORMAST 0xFFFFFF
45 : typedef std::map< sal_Int32, sal_Int32 > PatternMap;
46 : typedef std::pair< sal_Int32, sal_Int32 > PatternPair;
47 : using namespace ::com::sun::star;
48 : using namespace ::ooo::vba;
49 : using namespace ::ooo::vba::excel::XlPattern;
50 4 : static const OUString BACKCOLOR( "CellBackColor" );
51 4 : static const OUString PATTERN( "Pattern" );
52 4 : static const OUString PATTERNCOLOR( "PatternColor" );
53 :
54 4 : static PatternMap lcl_getPatternMap()
55 : {
56 4 : PatternMap aPatternMap;
57 4 : aPatternMap.insert( PatternPair( xlPatternAutomatic, 0 ) );
58 4 : aPatternMap.insert( PatternPair( xlPatternChecker, 9 ) );
59 4 : aPatternMap.insert( PatternPair( xlPatternCrissCross, 16 ) );
60 4 : aPatternMap.insert( PatternPair( xlPatternDown, 7 ) );
61 4 : aPatternMap.insert( PatternPair( xlPatternGray16, 17 ) );
62 4 : aPatternMap.insert( PatternPair( xlPatternGray25, 4 ) );
63 4 : aPatternMap.insert( PatternPair( xlPatternGray50, 2 ) );
64 4 : aPatternMap.insert( PatternPair( xlPatternGray75, 3 ) );
65 4 : aPatternMap.insert( PatternPair( xlPatternGray8, 18 ) );
66 4 : aPatternMap.insert( PatternPair( xlPatternGrid, 15 ) );
67 4 : aPatternMap.insert( PatternPair( xlPatternHorizontal, 5 ) );
68 4 : aPatternMap.insert( PatternPair( xlPatternLightDown, 13 ) );
69 4 : aPatternMap.insert( PatternPair( xlPatternLightHorizontal, 11 ) );
70 4 : aPatternMap.insert( PatternPair( xlPatternLightUp, 14 ) );
71 4 : aPatternMap.insert( PatternPair( xlPatternLightVertical, 12 ) );
72 4 : aPatternMap.insert( PatternPair( xlPatternNone, 0 ) );
73 4 : aPatternMap.insert( PatternPair( xlPatternSemiGray75, 10 ) );
74 4 : aPatternMap.insert( PatternPair( xlPatternSolid, 0 ) );
75 4 : aPatternMap.insert( PatternPair( xlPatternUp, 8 ) );
76 4 : aPatternMap.insert( PatternPair( xlPatternVertical, 6 ) );
77 4 : return aPatternMap;
78 : }
79 :
80 4 : static PatternMap aPatternMap( lcl_getPatternMap() );
81 :
82 18 : ScVbaInterior::ScVbaInterior( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< beans::XPropertySet >& xProps, ScDocument* pScDoc ) throw ( lang::IllegalArgumentException) : ScVbaInterior_BASE( xParent, xContext ), m_xProps(xProps), m_pScDoc( pScDoc )
83 : {
84 : // auto color
85 18 : m_aPattColor.SetColor( (sal_uInt32)0x0 );
86 18 : m_nPattern = 0L;
87 18 : if ( !m_xProps.is() )
88 0 : throw lang::IllegalArgumentException("properties", uno::Reference< uno::XInterface >(), 2 );
89 18 : }
90 :
91 : uno::Any
92 22 : ScVbaInterior::getColor() throw (uno::RuntimeException, std::exception)
93 : {
94 22 : Color aBackColor( GetBackColor() );
95 22 : return uno::makeAny( OORGBToXLRGB( aBackColor.GetColor() ) );
96 : }
97 :
98 : void
99 8 : ScVbaInterior::setColor( const uno::Any& _color ) throw (uno::RuntimeException, std::exception)
100 : {
101 8 : sal_Int32 nColor = 0;
102 8 : if( _color >>= nColor )
103 : {
104 8 : SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( XLRGBToOORGB( nColor ) ) );
105 8 : SetMixedColor();
106 : }
107 8 : }
108 :
109 : void
110 8 : ScVbaInterior::SetMixedColor()
111 : {
112 : // pattern
113 8 : uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
114 8 : if( aPattern.hasValue() )
115 : {
116 0 : m_nPattern = GetAttributeData( aPattern );
117 : }
118 8 : sal_Int32 nPattern = aPatternMap[ m_nPattern ];
119 : // pattern color
120 16 : uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
121 8 : if( aPatternColor.hasValue() )
122 : {
123 0 : sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
124 0 : m_aPattColor.SetColor( nPatternColor );
125 : }
126 8 : sal_Int32 nPatternColor = m_aPattColor.GetColor();
127 : // back color
128 8 : Color aBackColor( GetBackColor() );
129 : // set mixed color
130 8 : Color aMixedColor;
131 8 : if( nPattern > 0 )
132 0 : aMixedColor = GetPatternColor( Color(nPatternColor), aBackColor, (sal_uInt32)nPattern );
133 : else
134 8 : aMixedColor = GetPatternColor( aBackColor, aBackColor, (sal_uInt32)nPattern );
135 8 : sal_Int32 nMixedColor = aMixedColor.GetColor() & COLORMAST;
136 16 : m_xProps->setPropertyValue( BACKCOLOR , uno::makeAny( nMixedColor ) );
137 8 : }
138 :
139 : uno::Reference< container::XIndexAccess >
140 16 : ScVbaInterior::getPalette()
141 : {
142 16 : if ( !m_pScDoc )
143 0 : throw uno::RuntimeException();
144 16 : SfxObjectShell* pShell = m_pScDoc->GetDocumentShell();
145 16 : ScVbaPalette aPalette( pShell );
146 16 : return aPalette.getPalette();
147 : }
148 :
149 : void SAL_CALL
150 8 : ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex ) throw (css::uno::RuntimeException, std::exception)
151 : {
152 8 : sal_Int32 nIndex = 0;
153 8 : _colorindex >>= nIndex;
154 :
155 : // hackly for excel::XlColorIndex::xlColorIndexNone
156 8 : if( nIndex == excel::XlColorIndex::xlColorIndexNone )
157 : {
158 0 : m_xProps->setPropertyValue( BACKCOLOR, uno::makeAny( sal_Int32( -1 ) ) );
159 : }
160 : else
161 : {
162 : // setColor expects colors in XL RGB values
163 : // #FIXME this is daft we convert OO RGB val to XL RGB val and
164 : // then back again to OO RGB value
165 8 : setColor( OORGBToXLRGB( GetIndexColor( nIndex ) ) );
166 : }
167 8 : }
168 : uno::Any
169 8 : ScVbaInterior::GetIndexColor( const sal_Int32& nColorIndex )
170 : {
171 8 : sal_Int32 nIndex = nColorIndex;
172 : // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
173 : // handled properly here
174 8 : if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) || ( nIndex == excel::XlColorIndex::xlColorIndexNone ) )
175 0 : nIndex = 2; // default is white ( this maybe will probably break, e.g. we may at some stage need to know what this interior is, a cell or something else and then pick a default colour based on that )
176 8 : --nIndex; // OOo indices are zero bases
177 8 : uno::Reference< container::XIndexAccess > xIndex = getPalette();
178 8 : return xIndex->getByIndex( nIndex );
179 : }
180 :
181 : sal_Int32
182 8 : ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
183 : {
184 8 : uno::Reference< container::XIndexAccess > xIndex = getPalette();
185 8 : sal_Int32 nElems = xIndex->getCount();
186 8 : sal_Int32 nIndex = -1;
187 22 : for ( sal_Int32 count=0; count<nElems; ++count )
188 : {
189 22 : sal_Int32 nPaletteColor = 0;
190 22 : xIndex->getByIndex( count ) >>= nPaletteColor;
191 22 : if ( nPaletteColor == nColor )
192 : {
193 8 : nIndex = count + 1; // 1 based
194 8 : break;
195 : }
196 : }
197 8 : return nIndex;
198 : }
199 :
200 : uno::Any SAL_CALL
201 8 : ScVbaInterior::getColorIndex() throw ( css::uno::RuntimeException, std::exception )
202 : {
203 8 : sal_Int32 nColor = 0;
204 : // hackly for excel::XlColorIndex::xlColorIndexNone
205 8 : uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
206 8 : if( ( aColor >>= nColor ) && ( nColor == -1 ) )
207 : {
208 0 : nColor = excel::XlColorIndex::xlColorIndexNone;
209 0 : return uno::makeAny( nColor );
210 : }
211 :
212 : // getColor returns Xl ColorValue, need to convert it to OO val
213 : // as the palette deals with OO RGB values
214 : // #FIXME this is daft in getColor we convert OO RGB val to XL RGB val
215 : // and then back again to OO RGB value
216 8 : XLRGBToOORGB( getColor() ) >>= nColor;
217 :
218 8 : return uno::makeAny( GetColorIndex( nColor ) );
219 : }
220 : Color
221 8 : ScVbaInterior::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt32 nXclPattern )
222 : {
223 : // 0x00 == 0% transparence (full rPattColor)
224 : // 0x80 == 100% transparence (full rBackColor)
225 : static const sal_uInt8 pnRatioTable[] =
226 : {
227 : 0x80, 0x00, 0x40, 0x20, 0x60, 0x40, 0x40, 0x40, // 00 - 07
228 : 0x40, 0x40, 0x20, 0x60, 0x60, 0x60, 0x60, 0x48, // 08 - 15
229 : 0x50, 0x70, 0x78 // 16 - 18
230 : };
231 8 : return ( nXclPattern < SAL_N_ELEMENTS( pnRatioTable ) ) ?
232 8 : GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
233 : }
234 : Color
235 8 : ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
236 : {
237 : return Color(
238 : nTrans,
239 8 : GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
240 8 : GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
241 24 : GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
242 : }
243 : sal_uInt8
244 24 : ScVbaInterior::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans ) const
245 : {
246 24 : sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
247 24 : return static_cast< sal_uInt8 >( nTemp );
248 : }
249 : uno::Reference< container::XNameContainer >
250 56 : ScVbaInterior::GetAttributeContainer()
251 : {
252 56 : return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue("UserDefinedAttributes"), uno::UNO_QUERY_THROW );
253 : }
254 : sal_Int32
255 28 : ScVbaInterior::GetAttributeData( uno::Any aValue )
256 : {
257 28 : xml::AttributeData aDataValue;
258 28 : if( aValue >>= aDataValue )
259 : {
260 28 : return aDataValue.Value.toInt32();
261 : }
262 0 : return sal_Int32( 0 );
263 : }
264 : uno::Any
265 10 : ScVbaInterior::SetAttributeData( sal_Int32 nValue )
266 : {
267 10 : xml::AttributeData aAttributeData;
268 10 : aAttributeData.Type = OUString( "sal_Int32" );
269 10 : aAttributeData.Value = OUString::number( nValue );
270 10 : return uno::makeAny( aAttributeData );
271 : }
272 : uno::Any
273 46 : ScVbaInterior::GetUserDefinedAttributes( const OUString& sName )
274 : {
275 46 : uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
276 46 : if( xNameContainer->hasByName( sName ) )
277 : {
278 28 : return xNameContainer->getByName( sName );
279 : }
280 18 : return uno::Any();
281 : }
282 : void
283 10 : ScVbaInterior::SetUserDefinedAttributes( const OUString& sName, const uno::Any& aValue )
284 : {
285 10 : if( aValue.hasValue() )
286 : {
287 10 : uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
288 10 : if( xNameContainer->hasByName( sName ) )
289 8 : xNameContainer->removeByName( sName );
290 10 : xNameContainer->insertByName( sName, aValue );
291 10 : m_xProps->setPropertyValue("UserDefinedAttributes", uno::makeAny( xNameContainer ) );
292 : }
293 10 : }
294 : // OOo do not support below API
295 : uno::Any SAL_CALL
296 0 : ScVbaInterior::getPattern() throw (uno::RuntimeException, std::exception)
297 : {
298 : // XlPattern
299 0 : uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
300 0 : if( aPattern.hasValue() )
301 0 : return uno::makeAny( GetAttributeData( aPattern ) );
302 0 : return uno::makeAny( excel::XlPattern::xlPatternNone );
303 : }
304 : void SAL_CALL
305 0 : ScVbaInterior::setPattern( const uno::Any& _pattern ) throw (uno::RuntimeException, std::exception)
306 : {
307 0 : if( _pattern >>= m_nPattern )
308 : {
309 0 : SetUserDefinedAttributes( PATTERN, SetAttributeData( m_nPattern ) );
310 0 : SetMixedColor();
311 : }
312 : else
313 0 : throw uno::RuntimeException("Invalid Pattern index" );
314 0 : }
315 : Color
316 30 : ScVbaInterior::GetBackColor()
317 : {
318 30 : sal_Int32 nColor = 0;
319 30 : Color aBackColor;
320 30 : uno::Any aColor = GetUserDefinedAttributes( BACKCOLOR );
321 30 : if( aColor.hasValue() )
322 : {
323 28 : nColor = GetAttributeData( aColor );
324 28 : aBackColor.SetColor( nColor );
325 : }
326 : else
327 : {
328 2 : uno::Any aAny;
329 2 : aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
330 2 : if( aAny >>= nColor )
331 : {
332 2 : nColor = XLRGBToOORGB( nColor );
333 2 : aBackColor.SetColor( nColor );
334 2 : SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( nColor ) );
335 2 : }
336 : }
337 30 : return aBackColor;
338 : }
339 : uno::Any SAL_CALL
340 0 : ScVbaInterior::getPatternColor() throw (uno::RuntimeException, std::exception)
341 : {
342 : // 0 is the default color. no filled.
343 0 : uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
344 0 : if( aPatternColor.hasValue() )
345 : {
346 0 : sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
347 0 : return uno::makeAny( OORGBToXLRGB( nPatternColor ) );
348 : }
349 0 : return uno::makeAny( sal_Int32( 0 ) );
350 : }
351 : void SAL_CALL
352 0 : ScVbaInterior::setPatternColor( const uno::Any& _patterncolor ) throw (uno::RuntimeException, std::exception)
353 : {
354 0 : sal_Int32 nPattColor = 0;
355 0 : if( _patterncolor >>= nPattColor )
356 : {
357 0 : SetUserDefinedAttributes( PATTERNCOLOR, SetAttributeData( XLRGBToOORGB( nPattColor ) ) );
358 0 : SetMixedColor();
359 : }
360 : else
361 0 : throw uno::RuntimeException("Invalid Pattern Color" );
362 0 : }
363 : uno::Any SAL_CALL
364 0 : ScVbaInterior::getPatternColorIndex() throw (uno::RuntimeException, std::exception)
365 : {
366 0 : sal_Int32 nColor = 0;
367 0 : XLRGBToOORGB( getPatternColor() ) >>= nColor;
368 :
369 0 : return uno::makeAny( GetIndexColor( nColor ) );
370 : }
371 : void SAL_CALL
372 0 : ScVbaInterior::setPatternColorIndex( const uno::Any& _patterncolorindex ) throw (uno::RuntimeException, std::exception)
373 : {
374 0 : sal_Int32 nColorIndex = 0;
375 0 : if( _patterncolorindex >>= nColorIndex )
376 : {
377 0 : if( nColorIndex == 0 )
378 0 : return;
379 0 : sal_Int32 nPattColor = 0;
380 0 : GetIndexColor( nColorIndex ) >>= nPattColor;
381 0 : setPatternColor( uno::makeAny( OORGBToXLRGB( nPattColor ) ) );
382 : }
383 : else
384 0 : throw uno::RuntimeException("Invalid Pattern Color" );
385 : }
386 :
387 : OUString
388 0 : ScVbaInterior::getServiceImplName()
389 : {
390 0 : return OUString("ScVbaInterior");
391 : }
392 :
393 : uno::Sequence< OUString >
394 0 : ScVbaInterior::getServiceNames()
395 : {
396 0 : static uno::Sequence< OUString > aServiceNames;
397 0 : if ( aServiceNames.getLength() == 0 )
398 : {
399 0 : aServiceNames.realloc( 1 );
400 0 : aServiceNames[ 0 ] = "ooo.vba.excel.Interior";
401 : }
402 0 : return aServiceNames;
403 12 : }
404 :
405 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|