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 : static const char BACKCOLOR[] = "CellBackColor";
51 : static const char PATTERN[] = "Pattern";
52 : static const char PATTERNCOLOR[] = "PatternColor";
53 :
54 3 : static PatternMap lcl_getPatternMap()
55 : {
56 3 : PatternMap aPatternMap;
57 3 : aPatternMap.insert( PatternPair( xlPatternAutomatic, 0 ) );
58 3 : aPatternMap.insert( PatternPair( xlPatternChecker, 9 ) );
59 3 : aPatternMap.insert( PatternPair( xlPatternCrissCross, 16 ) );
60 3 : aPatternMap.insert( PatternPair( xlPatternDown, 7 ) );
61 3 : aPatternMap.insert( PatternPair( xlPatternGray16, 17 ) );
62 3 : aPatternMap.insert( PatternPair( xlPatternGray25, 4 ) );
63 3 : aPatternMap.insert( PatternPair( xlPatternGray50, 2 ) );
64 3 : aPatternMap.insert( PatternPair( xlPatternGray75, 3 ) );
65 3 : aPatternMap.insert( PatternPair( xlPatternGray8, 18 ) );
66 3 : aPatternMap.insert( PatternPair( xlPatternGrid, 15 ) );
67 3 : aPatternMap.insert( PatternPair( xlPatternHorizontal, 5 ) );
68 3 : aPatternMap.insert( PatternPair( xlPatternLightDown, 13 ) );
69 3 : aPatternMap.insert( PatternPair( xlPatternLightHorizontal, 11 ) );
70 3 : aPatternMap.insert( PatternPair( xlPatternLightUp, 14 ) );
71 3 : aPatternMap.insert( PatternPair( xlPatternLightVertical, 12 ) );
72 3 : aPatternMap.insert( PatternPair( xlPatternNone, 0 ) );
73 3 : aPatternMap.insert( PatternPair( xlPatternSemiGray75, 10 ) );
74 3 : aPatternMap.insert( PatternPair( xlPatternSolid, 0 ) );
75 3 : aPatternMap.insert( PatternPair( xlPatternUp, 8 ) );
76 3 : aPatternMap.insert( PatternPair( xlPatternVertical, 6 ) );
77 3 : return aPatternMap;
78 : }
79 :
80 3 : static PatternMap aPatternMap( lcl_getPatternMap() );
81 :
82 9 : 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 9 : m_aPattColor.SetColor( (sal_uInt32)0x0 );
86 9 : m_nPattern = 0L;
87 9 : if ( !m_xProps.is() )
88 0 : throw lang::IllegalArgumentException("properties", uno::Reference< uno::XInterface >(), 2 );
89 9 : }
90 :
91 : uno::Any
92 11 : ScVbaInterior::getColor() throw (uno::RuntimeException, std::exception)
93 : {
94 11 : Color aBackColor( GetBackColor() );
95 11 : return uno::makeAny( OORGBToXLRGB( aBackColor.GetColor() ) );
96 : }
97 :
98 : void
99 4 : ScVbaInterior::setColor( const uno::Any& _color ) throw (uno::RuntimeException, std::exception)
100 : {
101 4 : sal_Int32 nColor = 0;
102 4 : if( _color >>= nColor )
103 : {
104 4 : SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( XLRGBToOORGB( nColor ) ) );
105 4 : SetMixedColor();
106 : }
107 4 : }
108 :
109 : void
110 4 : ScVbaInterior::SetMixedColor()
111 : {
112 : // pattern
113 4 : uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
114 4 : if( aPattern.hasValue() )
115 : {
116 0 : m_nPattern = GetAttributeData( aPattern );
117 : }
118 4 : sal_Int32 nPattern = aPatternMap[ m_nPattern ];
119 : // pattern color
120 8 : uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
121 4 : if( aPatternColor.hasValue() )
122 : {
123 0 : sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
124 0 : m_aPattColor.SetColor( nPatternColor );
125 : }
126 4 : sal_Int32 nPatternColor = m_aPattColor.GetColor();
127 : // back color
128 4 : Color aBackColor( GetBackColor() );
129 : // set mixed color
130 4 : Color aMixedColor;
131 4 : if( nPattern > 0 )
132 0 : aMixedColor = GetPatternColor( Color(nPatternColor), aBackColor, (sal_uInt32)nPattern );
133 : else
134 4 : aMixedColor = GetPatternColor( aBackColor, aBackColor, (sal_uInt32)nPattern );
135 4 : sal_Int32 nMixedColor = aMixedColor.GetColor() & COLORMAST;
136 8 : m_xProps->setPropertyValue( BACKCOLOR , uno::makeAny( nMixedColor ) );
137 4 : }
138 :
139 : uno::Reference< container::XIndexAccess >
140 8 : ScVbaInterior::getPalette()
141 : {
142 8 : if ( !m_pScDoc )
143 0 : throw uno::RuntimeException();
144 8 : SfxObjectShell* pShell = m_pScDoc->GetDocumentShell();
145 8 : ScVbaPalette aPalette( pShell );
146 8 : return aPalette.getPalette();
147 : }
148 :
149 : void SAL_CALL
150 4 : ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex ) throw (css::uno::RuntimeException, std::exception)
151 : {
152 4 : sal_Int32 nIndex = 0;
153 4 : _colorindex >>= nIndex;
154 :
155 : // hackly for excel::XlColorIndex::xlColorIndexNone
156 4 : 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 4 : setColor( OORGBToXLRGB( GetIndexColor( nIndex ) ) );
166 : }
167 4 : }
168 : uno::Any
169 4 : ScVbaInterior::GetIndexColor( const sal_Int32& nColorIndex )
170 : {
171 4 : sal_Int32 nIndex = nColorIndex;
172 : // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
173 : // handled properly here
174 4 : 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 4 : --nIndex; // OOo indices are zero bases
177 4 : uno::Reference< container::XIndexAccess > xIndex = getPalette();
178 4 : return xIndex->getByIndex( nIndex );
179 : }
180 :
181 : sal_Int32
182 4 : ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
183 : {
184 4 : uno::Reference< container::XIndexAccess > xIndex = getPalette();
185 4 : sal_Int32 nElems = xIndex->getCount();
186 4 : sal_Int32 nIndex = -1;
187 11 : for ( sal_Int32 count=0; count<nElems; ++count )
188 : {
189 11 : sal_Int32 nPaletteColor = 0;
190 11 : xIndex->getByIndex( count ) >>= nPaletteColor;
191 11 : if ( nPaletteColor == nColor )
192 : {
193 4 : nIndex = count + 1; // 1 based
194 4 : break;
195 : }
196 : }
197 4 : return nIndex;
198 : }
199 :
200 : uno::Any SAL_CALL
201 4 : ScVbaInterior::getColorIndex() throw ( css::uno::RuntimeException, std::exception )
202 : {
203 4 : sal_Int32 nColor = 0;
204 : // hackly for excel::XlColorIndex::xlColorIndexNone
205 4 : uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
206 4 : 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 4 : XLRGBToOORGB( getColor() ) >>= nColor;
217 :
218 4 : return uno::makeAny( GetColorIndex( nColor ) );
219 : }
220 : Color
221 4 : 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 4 : return ( nXclPattern < SAL_N_ELEMENTS( pnRatioTable ) ) ?
232 4 : GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
233 : }
234 : Color
235 4 : ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
236 : {
237 : return Color(
238 : nTrans,
239 4 : GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
240 4 : GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
241 12 : GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
242 : }
243 : sal_uInt8
244 12 : ScVbaInterior::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
245 : {
246 12 : sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
247 12 : return static_cast< sal_uInt8 >( nTemp );
248 : }
249 : uno::Reference< container::XNameContainer >
250 28 : ScVbaInterior::GetAttributeContainer()
251 : {
252 28 : return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue("UserDefinedAttributes"), uno::UNO_QUERY_THROW );
253 : }
254 : sal_Int32
255 14 : ScVbaInterior::GetAttributeData( uno::Any aValue )
256 : {
257 14 : xml::AttributeData aDataValue;
258 14 : if( aValue >>= aDataValue )
259 : {
260 14 : return aDataValue.Value.toInt32();
261 : }
262 0 : return sal_Int32( 0 );
263 : }
264 : uno::Any
265 5 : ScVbaInterior::SetAttributeData( sal_Int32 nValue )
266 : {
267 5 : xml::AttributeData aAttributeData;
268 5 : aAttributeData.Type = "sal_Int32";
269 5 : aAttributeData.Value = OUString::number( nValue );
270 5 : return uno::makeAny( aAttributeData );
271 : }
272 : uno::Any
273 23 : ScVbaInterior::GetUserDefinedAttributes( const OUString& sName )
274 : {
275 23 : uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
276 23 : if( xNameContainer->hasByName( sName ) )
277 : {
278 14 : return xNameContainer->getByName( sName );
279 : }
280 9 : return uno::Any();
281 : }
282 : void
283 5 : ScVbaInterior::SetUserDefinedAttributes( const OUString& sName, const uno::Any& aValue )
284 : {
285 5 : if( aValue.hasValue() )
286 : {
287 5 : uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
288 5 : if( xNameContainer->hasByName( sName ) )
289 4 : xNameContainer->removeByName( sName );
290 5 : xNameContainer->insertByName( sName, aValue );
291 5 : m_xProps->setPropertyValue("UserDefinedAttributes", uno::makeAny( xNameContainer ) );
292 : }
293 5 : }
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 15 : ScVbaInterior::GetBackColor()
317 : {
318 15 : sal_Int32 nColor = 0;
319 15 : Color aBackColor;
320 15 : uno::Any aColor = GetUserDefinedAttributes( BACKCOLOR );
321 15 : if( aColor.hasValue() )
322 : {
323 14 : nColor = GetAttributeData( aColor );
324 14 : aBackColor.SetColor( nColor );
325 : }
326 : else
327 : {
328 1 : uno::Any aAny;
329 1 : aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
330 1 : if( aAny >>= nColor )
331 : {
332 1 : nColor = XLRGBToOORGB( nColor );
333 1 : aBackColor.SetColor( nColor );
334 1 : SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( nColor ) );
335 1 : }
336 : }
337 15 : 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 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 9 : }
404 :
405 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|