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