update dev300-m58
[ooovba.git] / sc / source / ui / vba / vbainterior.cxx
blob6a2ce4da9bf870242055b5272d41144131ea6d33
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbainterior.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/table/XCell.hpp>
32 #include <com/sun/star/table/XColumnRowRange.hpp>
33 #include <com/sun/star/beans/XIntrospection.hpp>
34 #include <com/sun/star/beans/XIntrospectionAccess.hpp>
35 #include <com/sun/star/reflection/XIdlMethod.hpp>
36 #include <com/sun/star/beans/MethodConcept.hpp>
37 #include <com/sun/star/beans/NamedValue.hpp>
38 #include <com/sun/star/xml/AttributeData.hpp>
40 #include <ooo/vba/excel/XlColorIndex.hpp>
41 #include <ooo/vba/excel/XlPattern.hpp>
43 #include <comphelper/processfactory.hxx>
44 #include <cppuhelper/queryinterface.hxx>
46 #include <svx/xtable.hxx>
48 #include "vbainterior.hxx"
49 #include "vbapalette.hxx"
51 #define STATIC_TABLE_SIZE( array ) (sizeof(array)/sizeof(*(array)))
52 #define COLORMAST 0xFFFFFF
53 const sal_uInt16 EXC_COLOR_WINDOWBACK = 65;
54 typedef std::map< sal_Int32, sal_Int32 > PatternMap;
55 typedef std::pair< sal_Int32, sal_Int32 > PatternPair;
56 using namespace ::com::sun::star;
57 using namespace ::ooo::vba;
58 using namespace ::ooo::vba::excel::XlPattern;
59 static const rtl::OUString BACKCOLOR( RTL_CONSTASCII_USTRINGPARAM( "CellBackColor" ) );
60 static const rtl::OUString PATTERN( RTL_CONSTASCII_USTRINGPARAM( "Pattern" ) );
61 static const rtl::OUString PATTERNCOLOR( RTL_CONSTASCII_USTRINGPARAM( "PatternColor" ) );
63 PatternMap lcl_getPatternMap()
65 PatternMap aPatternMap;
66 aPatternMap.insert( PatternPair( xlPatternAutomatic, 0 ) );
67 aPatternMap.insert( PatternPair( xlPatternChecker, 9 ) );
68 aPatternMap.insert( PatternPair( xlPatternCrissCross, 16 ) );
69 aPatternMap.insert( PatternPair( xlPatternDown, 7 ) );
70 aPatternMap.insert( PatternPair( xlPatternGray16, 17 ) );
71 aPatternMap.insert( PatternPair( xlPatternGray25, 4 ) );
72 aPatternMap.insert( PatternPair( xlPatternGray50, 2 ) );
73 aPatternMap.insert( PatternPair( xlPatternGray75, 3 ) );
74 aPatternMap.insert( PatternPair( xlPatternGray8, 18 ) );
75 aPatternMap.insert( PatternPair( xlPatternGrid, 15 ) );
76 aPatternMap.insert( PatternPair( xlPatternHorizontal, 5 ) );
77 aPatternMap.insert( PatternPair( xlPatternLightDown, 13 ) );
78 aPatternMap.insert( PatternPair( xlPatternLightHorizontal, 11 ) );
79 aPatternMap.insert( PatternPair( xlPatternLightUp, 14 ) );
80 aPatternMap.insert( PatternPair( xlPatternLightVertical, 12 ) );
81 aPatternMap.insert( PatternPair( xlPatternNone, 0 ) );
82 aPatternMap.insert( PatternPair( xlPatternSemiGray75, 10 ) );
83 aPatternMap.insert( PatternPair( xlPatternSolid, 0 ) );
84 aPatternMap.insert( PatternPair( xlPatternUp, 8 ) );
85 aPatternMap.insert( PatternPair( xlPatternVertical, 6 ) );
86 return aPatternMap;
89 static PatternMap aPatternMap( lcl_getPatternMap() );
91 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 )
93 // auto color
94 //m_aPattColor.SetColor( (sal_uInt32)0xFFFFFFFF );
95 m_aPattColor.SetColor( (sal_uInt32)0x0 );
96 m_nPattern = 0L;
97 if ( !m_xProps.is() )
98 throw lang::IllegalArgumentException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "properties") ), uno::Reference< uno::XInterface >(), 2 );
101 uno::Any
102 ScVbaInterior::getColor() throw (uno::RuntimeException)
104 Color aBackColor( GetBackColor() );
105 return uno::makeAny( OORGBToXLRGB( aBackColor.GetColor() ) );
108 void
109 ScVbaInterior::setColor( const uno::Any& _color ) throw (uno::RuntimeException)
111 sal_Int32 nColor = 0;
112 if( _color >>= nColor )
114 SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( XLRGBToOORGB( nColor ) ) );
115 //m_xProps->setPropertyValue( BACKCOLOR , XLRGBToOORGB(_color));
116 SetMixedColor();
120 void
121 ScVbaInterior::SetMixedColor()
123 // pattern
124 uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
125 if( aPattern.hasValue() )
127 m_nPattern = GetAttributeData( aPattern );
129 sal_Int32 nPattern = aPatternMap[ m_nPattern ];
130 // pattern color
131 uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
132 if( aPatternColor.hasValue() )
134 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
135 m_aPattColor.SetColor( nPatternColor );
137 sal_Int32 nPatternColor = m_aPattColor.GetColor();
138 // back color
139 Color aBackColor( GetBackColor() );
140 // set mixed color
141 Color aMixedColor;
142 if( nPattern > 0 )
143 aMixedColor = GetPatternColor( Color(nPatternColor), aBackColor, (sal_uInt32)nPattern );
144 else
145 aMixedColor = GetPatternColor( aBackColor, aBackColor, (sal_uInt32)nPattern );
146 sal_Int32 nMixedColor = aMixedColor.GetColor() & COLORMAST;
147 m_xProps->setPropertyValue( BACKCOLOR , uno::makeAny( nMixedColor ) );
150 uno::Reference< container::XIndexAccess >
151 ScVbaInterior::getPalette()
153 if ( !m_pScDoc )
154 throw uno::RuntimeException();
155 SfxObjectShell* pShell = m_pScDoc->GetDocumentShell();
156 ScVbaPalette aPalette( pShell );
157 return aPalette.getPalette();
160 void SAL_CALL
161 ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex ) throw (css::uno::RuntimeException)
163 sal_Int32 nIndex = 0;
164 _colorindex >>= nIndex;
166 // hackly for excel::XlColorIndex::xlColorIndexNone
167 if( nIndex == excel::XlColorIndex::xlColorIndexNone )
169 m_xProps->setPropertyValue( BACKCOLOR, uno::makeAny( sal_Int32( -1 ) ) );
171 else
173 // setColor expects colors in XL RGB values
174 // #FIXME this is daft we convert OO RGB val to XL RGB val and
175 // then back again to OO RGB value
176 setColor( OORGBToXLRGB( GetIndexColor( nIndex ) ) );
179 uno::Any
180 ScVbaInterior::GetIndexColor( const sal_Int32& nColorIndex )
182 sal_Int32 nIndex = nColorIndex;
183 // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
184 // handled properly here
185 if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) || ( nIndex == excel::XlColorIndex::xlColorIndexNone ) )
186 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 )
187 --nIndex; // OOo indices are zero bases
188 uno::Reference< container::XIndexAccess > xIndex = getPalette();
189 return xIndex->getByIndex( nIndex );
192 sal_Int32
193 ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
195 uno::Reference< container::XIndexAccess > xIndex = getPalette();
196 sal_Int32 nElems = xIndex->getCount();
197 sal_Int32 nIndex = -1;
198 for ( sal_Int32 count=0; count<nElems; ++count )
200 sal_Int32 nPaletteColor = 0;
201 xIndex->getByIndex( count ) >>= nPaletteColor;
202 if ( nPaletteColor == nColor )
204 nIndex = count + 1; // 1 based
205 break;
208 return nIndex;
211 uno::Any SAL_CALL
212 ScVbaInterior::getColorIndex() throw ( css::uno::RuntimeException )
214 sal_Int32 nColor = 0;
215 // hackly for excel::XlColorIndex::xlColorIndexNone
216 uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
217 if( ( aColor >>= nColor ) && ( nColor == -1 ) )
219 nColor = excel::XlColorIndex::xlColorIndexNone;
220 return uno::makeAny( nColor );
223 // getColor returns Xl ColorValue, need to convert it to OO val
224 // as the palette deals with OO RGB values
225 // #FIXME this is daft in getColor we convert OO RGB val to XL RGB val
226 // and then back again to OO RGB value
227 XLRGBToOORGB( getColor() ) >>= nColor;
229 return uno::makeAny( GetColorIndex( nColor ) );
231 Color
232 ScVbaInterior::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt32 nXclPattern )
234 // 0x00 == 0% transparence (full rPattColor)
235 // 0x80 == 100% transparence (full rBackColor)
236 static const sal_uInt8 pnRatioTable[] =
238 0x80, 0x00, 0x40, 0x20, 0x60, 0x40, 0x40, 0x40, // 00 - 07
239 0x40, 0x40, 0x20, 0x60, 0x60, 0x60, 0x60, 0x48, // 08 - 15
240 0x50, 0x70, 0x78 // 16 - 18
242 return ( nXclPattern < STATIC_TABLE_SIZE( pnRatioTable ) ) ?
243 GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
245 Color
246 ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
248 return Color(
249 nTrans,
250 GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
251 GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
252 GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
254 sal_uInt8
255 ScVbaInterior::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
257 sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
258 return static_cast< sal_uInt8 >( nTemp );
260 uno::Reference< container::XNameContainer >
261 ScVbaInterior::GetAttributeContainer()
263 return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue( rtl::OUString::createFromAscii( "UserDefinedAttributes" ) ), uno::UNO_QUERY_THROW );
265 sal_Int32
266 ScVbaInterior::GetAttributeData( uno::Any aValue )
268 xml::AttributeData aDataValue;
269 if( aValue >>= aDataValue )
271 return aDataValue.Value.toInt32();
273 return sal_Int32( 0 );
275 uno::Any
276 ScVbaInterior::SetAttributeData( sal_Int32 nValue )
278 xml::AttributeData aAttributeData;
279 //aAttributeData.Namespace = rtl::OUString::createFromAscii( "ooo.vba.excel.CellPatten");
280 aAttributeData.Type = rtl::OUString::createFromAscii( "sal_Int32" );
281 aAttributeData.Value = rtl::OUString::valueOf( nValue );
282 return uno::makeAny( aAttributeData );
284 uno::Any
285 ScVbaInterior::GetUserDefinedAttributes( const rtl::OUString& sName )
287 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
288 if( xNameContainer->hasByName( sName ) )
290 return xNameContainer->getByName( sName );
292 return uno::Any();
294 void
295 ScVbaInterior::SetUserDefinedAttributes( const rtl::OUString& sName, const uno::Any& aValue )
297 if( aValue.hasValue() )
299 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
300 if( xNameContainer->hasByName( sName ) )
301 xNameContainer->removeByName( sName );
302 xNameContainer->insertByName( sName, aValue );
303 m_xProps->setPropertyValue( rtl::OUString::createFromAscii( "UserDefinedAttributes" ), uno::makeAny( xNameContainer ) );
306 // OOo do not support below API
307 uno::Any SAL_CALL
308 ScVbaInterior::getPattern() throw (uno::RuntimeException)
310 // XlPattern
311 uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
312 if( aPattern.hasValue() )
313 return uno::makeAny( GetAttributeData( aPattern ) );
314 return uno::makeAny( excel::XlPattern::xlPatternNone );
316 void SAL_CALL
317 ScVbaInterior::setPattern( const uno::Any& _pattern ) throw (uno::RuntimeException)
319 if( _pattern >>= m_nPattern )
321 SetUserDefinedAttributes( PATTERN, SetAttributeData( m_nPattern ) );
322 SetMixedColor();
324 else
325 throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid Pattern index" ), uno::Reference< uno::XInterface >() );
327 Color
328 ScVbaInterior::GetBackColor()
330 sal_Int32 nColor = 0;
331 Color aBackColor;
332 uno::Any aColor = GetUserDefinedAttributes( BACKCOLOR );
333 if( aColor.hasValue() )
335 nColor = GetAttributeData( aColor );
336 aBackColor.SetColor( nColor );
338 else
340 uno::Any aAny;
341 aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
342 if( aAny >>= nColor )
344 nColor = XLRGBToOORGB( nColor );
345 aBackColor.SetColor( nColor );
346 SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( nColor ) );
349 return aBackColor;
351 uno::Any SAL_CALL
352 ScVbaInterior::getPatternColor() throw (uno::RuntimeException)
354 // 0 is the default color. no filled.
355 uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
356 if( aPatternColor.hasValue() )
358 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
359 return uno::makeAny( OORGBToXLRGB( nPatternColor ) );
361 return uno::makeAny( sal_Int32( 0 ) );
363 void SAL_CALL
364 ScVbaInterior::setPatternColor( const uno::Any& _patterncolor ) throw (uno::RuntimeException)
366 sal_Int32 nPattColor = 0;
367 if( _patterncolor >>= nPattColor )
369 SetUserDefinedAttributes( PATTERNCOLOR, SetAttributeData( XLRGBToOORGB( nPattColor ) ) );
370 SetMixedColor();
372 else
373 throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid Pattern Color" ), uno::Reference< uno::XInterface >() );
375 uno::Any SAL_CALL
376 ScVbaInterior::getPatternColorIndex() throw (uno::RuntimeException)
378 sal_Int32 nColor = 0;
379 XLRGBToOORGB( getPatternColor() ) >>= nColor;
381 return uno::makeAny( GetIndexColor( nColor ) );
383 void SAL_CALL
384 ScVbaInterior::setPatternColorIndex( const uno::Any& _patterncolorindex ) throw (uno::RuntimeException)
386 sal_Int32 nColorIndex = 0;
387 if( _patterncolorindex >>= nColorIndex )
389 if( nColorIndex == 0 )
390 return;
391 sal_Int32 nPattColor = 0;
392 GetIndexColor( nColorIndex ) >>= nPattColor;
393 setPatternColor( uno::makeAny( OORGBToXLRGB( nPattColor ) ) );
395 else
396 throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid Pattern Color" ), uno::Reference< uno::XInterface >() );
399 rtl::OUString&
400 ScVbaInterior::getServiceImplName()
402 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaInterior") );
403 return sImplName;
406 uno::Sequence< rtl::OUString >
407 ScVbaInterior::getServiceNames()
409 static uno::Sequence< rtl::OUString > aServiceNames;
410 if ( aServiceNames.getLength() == 0 )
412 aServiceNames.realloc( 1 );
413 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Interior" ) );
415 return aServiceNames;