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