bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / vba / vbainterior.cxx
blob3782a100b7ee6687421f29118eaf2820cef0db6f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
29 #include <ooo/vba/excel/XlColorIndex.hpp>
30 #include <ooo/vba/excel/XlPattern.hpp>
32 #include <comphelper/processfactory.hxx>
33 #include <cppuhelper/queryinterface.hxx>
35 #include <map>
37 #include <sal/macros.h>
38 #include <svx/xtable.hxx>
40 #include "vbainterior.hxx"
41 #include "vbapalette.hxx"
42 #include "document.hxx"
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 static const OUString BACKCOLOR( "CellBackColor" );
52 static const OUString PATTERN( "Pattern" );
53 static const OUString PATTERNCOLOR( "PatternColor" );
55 static PatternMap lcl_getPatternMap()
57 PatternMap aPatternMap;
58 aPatternMap.insert( PatternPair( xlPatternAutomatic, 0 ) );
59 aPatternMap.insert( PatternPair( xlPatternChecker, 9 ) );
60 aPatternMap.insert( PatternPair( xlPatternCrissCross, 16 ) );
61 aPatternMap.insert( PatternPair( xlPatternDown, 7 ) );
62 aPatternMap.insert( PatternPair( xlPatternGray16, 17 ) );
63 aPatternMap.insert( PatternPair( xlPatternGray25, 4 ) );
64 aPatternMap.insert( PatternPair( xlPatternGray50, 2 ) );
65 aPatternMap.insert( PatternPair( xlPatternGray75, 3 ) );
66 aPatternMap.insert( PatternPair( xlPatternGray8, 18 ) );
67 aPatternMap.insert( PatternPair( xlPatternGrid, 15 ) );
68 aPatternMap.insert( PatternPair( xlPatternHorizontal, 5 ) );
69 aPatternMap.insert( PatternPair( xlPatternLightDown, 13 ) );
70 aPatternMap.insert( PatternPair( xlPatternLightHorizontal, 11 ) );
71 aPatternMap.insert( PatternPair( xlPatternLightUp, 14 ) );
72 aPatternMap.insert( PatternPair( xlPatternLightVertical, 12 ) );
73 aPatternMap.insert( PatternPair( xlPatternNone, 0 ) );
74 aPatternMap.insert( PatternPair( xlPatternSemiGray75, 10 ) );
75 aPatternMap.insert( PatternPair( xlPatternSolid, 0 ) );
76 aPatternMap.insert( PatternPair( xlPatternUp, 8 ) );
77 aPatternMap.insert( PatternPair( xlPatternVertical, 6 ) );
78 return aPatternMap;
81 static PatternMap aPatternMap( lcl_getPatternMap() );
83 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 )
85 // auto color
86 m_aPattColor.SetColor( (sal_uInt32)0x0 );
87 m_nPattern = 0L;
88 if ( !m_xProps.is() )
89 throw lang::IllegalArgumentException( OUString( "properties"), uno::Reference< uno::XInterface >(), 2 );
92 uno::Any
93 ScVbaInterior::getColor() throw (uno::RuntimeException)
95 Color aBackColor( GetBackColor() );
96 return uno::makeAny( OORGBToXLRGB( aBackColor.GetColor() ) );
99 void
100 ScVbaInterior::setColor( const uno::Any& _color ) throw (uno::RuntimeException)
102 sal_Int32 nColor = 0;
103 if( _color >>= nColor )
105 SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( XLRGBToOORGB( nColor ) ) );
106 SetMixedColor();
110 void
111 ScVbaInterior::SetMixedColor()
113 // pattern
114 uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
115 if( aPattern.hasValue() )
117 m_nPattern = GetAttributeData( aPattern );
119 sal_Int32 nPattern = aPatternMap[ m_nPattern ];
120 // pattern color
121 uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
122 if( aPatternColor.hasValue() )
124 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
125 m_aPattColor.SetColor( nPatternColor );
127 sal_Int32 nPatternColor = m_aPattColor.GetColor();
128 // back color
129 Color aBackColor( GetBackColor() );
130 // set mixed color
131 Color aMixedColor;
132 if( nPattern > 0 )
133 aMixedColor = GetPatternColor( Color(nPatternColor), aBackColor, (sal_uInt32)nPattern );
134 else
135 aMixedColor = GetPatternColor( aBackColor, aBackColor, (sal_uInt32)nPattern );
136 sal_Int32 nMixedColor = aMixedColor.GetColor() & COLORMAST;
137 m_xProps->setPropertyValue( BACKCOLOR , uno::makeAny( nMixedColor ) );
140 uno::Reference< container::XIndexAccess >
141 ScVbaInterior::getPalette()
143 if ( !m_pScDoc )
144 throw uno::RuntimeException();
145 SfxObjectShell* pShell = m_pScDoc->GetDocumentShell();
146 ScVbaPalette aPalette( pShell );
147 return aPalette.getPalette();
150 void SAL_CALL
151 ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex ) throw (css::uno::RuntimeException)
153 sal_Int32 nIndex = 0;
154 _colorindex >>= nIndex;
156 // hackly for excel::XlColorIndex::xlColorIndexNone
157 if( nIndex == excel::XlColorIndex::xlColorIndexNone )
159 m_xProps->setPropertyValue( BACKCOLOR, uno::makeAny( sal_Int32( -1 ) ) );
161 else
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 setColor( OORGBToXLRGB( GetIndexColor( nIndex ) ) );
169 uno::Any
170 ScVbaInterior::GetIndexColor( const sal_Int32& nColorIndex )
172 sal_Int32 nIndex = nColorIndex;
173 // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
174 // handled properly here
175 if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) || ( nIndex == excel::XlColorIndex::xlColorIndexNone ) )
176 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 --nIndex; // OOo indices are zero bases
178 uno::Reference< container::XIndexAccess > xIndex = getPalette();
179 return xIndex->getByIndex( nIndex );
182 sal_Int32
183 ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
185 uno::Reference< container::XIndexAccess > xIndex = getPalette();
186 sal_Int32 nElems = xIndex->getCount();
187 sal_Int32 nIndex = -1;
188 for ( sal_Int32 count=0; count<nElems; ++count )
190 sal_Int32 nPaletteColor = 0;
191 xIndex->getByIndex( count ) >>= nPaletteColor;
192 if ( nPaletteColor == nColor )
194 nIndex = count + 1; // 1 based
195 break;
198 return nIndex;
201 uno::Any SAL_CALL
202 ScVbaInterior::getColorIndex() throw ( css::uno::RuntimeException )
204 sal_Int32 nColor = 0;
205 // hackly for excel::XlColorIndex::xlColorIndexNone
206 uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
207 if( ( aColor >>= nColor ) && ( nColor == -1 ) )
209 nColor = excel::XlColorIndex::xlColorIndexNone;
210 return uno::makeAny( nColor );
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 XLRGBToOORGB( getColor() ) >>= nColor;
219 return uno::makeAny( GetColorIndex( nColor ) );
221 Color
222 ScVbaInterior::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt32 nXclPattern )
224 // 0x00 == 0% transparence (full rPattColor)
225 // 0x80 == 100% transparence (full rBackColor)
226 static const sal_uInt8 pnRatioTable[] =
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
232 return ( nXclPattern < SAL_N_ELEMENTS( pnRatioTable ) ) ?
233 GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
235 Color
236 ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
238 return Color(
239 nTrans,
240 GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
241 GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
242 GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
244 sal_uInt8
245 ScVbaInterior::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans ) const
247 sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
248 return static_cast< sal_uInt8 >( nTemp );
250 uno::Reference< container::XNameContainer >
251 ScVbaInterior::GetAttributeContainer()
253 return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue( OUString( "UserDefinedAttributes" ) ), uno::UNO_QUERY_THROW );
255 sal_Int32
256 ScVbaInterior::GetAttributeData( uno::Any aValue )
258 xml::AttributeData aDataValue;
259 if( aValue >>= aDataValue )
261 return aDataValue.Value.toInt32();
263 return sal_Int32( 0 );
265 uno::Any
266 ScVbaInterior::SetAttributeData( sal_Int32 nValue )
268 xml::AttributeData aAttributeData;
269 aAttributeData.Type = OUString( "sal_Int32" );
270 aAttributeData.Value = OUString::valueOf( nValue );
271 return uno::makeAny( aAttributeData );
273 uno::Any
274 ScVbaInterior::GetUserDefinedAttributes( const OUString& sName )
276 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
277 if( xNameContainer->hasByName( sName ) )
279 return xNameContainer->getByName( sName );
281 return uno::Any();
283 void
284 ScVbaInterior::SetUserDefinedAttributes( const OUString& sName, const uno::Any& aValue )
286 if( aValue.hasValue() )
288 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
289 if( xNameContainer->hasByName( sName ) )
290 xNameContainer->removeByName( sName );
291 xNameContainer->insertByName( sName, aValue );
292 m_xProps->setPropertyValue( OUString( "UserDefinedAttributes" ), uno::makeAny( xNameContainer ) );
295 // OOo do not support below API
296 uno::Any SAL_CALL
297 ScVbaInterior::getPattern() throw (uno::RuntimeException)
299 // XlPattern
300 uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
301 if( aPattern.hasValue() )
302 return uno::makeAny( GetAttributeData( aPattern ) );
303 return uno::makeAny( excel::XlPattern::xlPatternNone );
305 void SAL_CALL
306 ScVbaInterior::setPattern( const uno::Any& _pattern ) throw (uno::RuntimeException)
308 if( _pattern >>= m_nPattern )
310 SetUserDefinedAttributes( PATTERN, SetAttributeData( m_nPattern ) );
311 SetMixedColor();
313 else
314 throw uno::RuntimeException( OUString( "Invalid Pattern index" ), uno::Reference< uno::XInterface >() );
316 Color
317 ScVbaInterior::GetBackColor()
319 sal_Int32 nColor = 0;
320 Color aBackColor;
321 uno::Any aColor = GetUserDefinedAttributes( BACKCOLOR );
322 if( aColor.hasValue() )
324 nColor = GetAttributeData( aColor );
325 aBackColor.SetColor( nColor );
327 else
329 uno::Any aAny;
330 aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
331 if( aAny >>= nColor )
333 nColor = XLRGBToOORGB( nColor );
334 aBackColor.SetColor( nColor );
335 SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( nColor ) );
338 return aBackColor;
340 uno::Any SAL_CALL
341 ScVbaInterior::getPatternColor() throw (uno::RuntimeException)
343 // 0 is the default color. no filled.
344 uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
345 if( aPatternColor.hasValue() )
347 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
348 return uno::makeAny( OORGBToXLRGB( nPatternColor ) );
350 return uno::makeAny( sal_Int32( 0 ) );
352 void SAL_CALL
353 ScVbaInterior::setPatternColor( const uno::Any& _patterncolor ) throw (uno::RuntimeException)
355 sal_Int32 nPattColor = 0;
356 if( _patterncolor >>= nPattColor )
358 SetUserDefinedAttributes( PATTERNCOLOR, SetAttributeData( XLRGBToOORGB( nPattColor ) ) );
359 SetMixedColor();
361 else
362 throw uno::RuntimeException( OUString( "Invalid Pattern Color" ), uno::Reference< uno::XInterface >() );
364 uno::Any SAL_CALL
365 ScVbaInterior::getPatternColorIndex() throw (uno::RuntimeException)
367 sal_Int32 nColor = 0;
368 XLRGBToOORGB( getPatternColor() ) >>= nColor;
370 return uno::makeAny( GetIndexColor( nColor ) );
372 void SAL_CALL
373 ScVbaInterior::setPatternColorIndex( const uno::Any& _patterncolorindex ) throw (uno::RuntimeException)
375 sal_Int32 nColorIndex = 0;
376 if( _patterncolorindex >>= nColorIndex )
378 if( nColorIndex == 0 )
379 return;
380 sal_Int32 nPattColor = 0;
381 GetIndexColor( nColorIndex ) >>= nPattColor;
382 setPatternColor( uno::makeAny( OORGBToXLRGB( nPattColor ) ) );
384 else
385 throw uno::RuntimeException( OUString( "Invalid Pattern Color" ), uno::Reference< uno::XInterface >() );
388 OUString
389 ScVbaInterior::getServiceImplName()
391 return OUString("ScVbaInterior");
394 uno::Sequence< OUString >
395 ScVbaInterior::getServiceNames()
397 static uno::Sequence< OUString > aServiceNames;
398 if ( aServiceNames.getLength() == 0 )
400 aServiceNames.realloc( 1 );
401 aServiceNames[ 0 ] = OUString("ooo.vba.excel.Interior" );
403 return aServiceNames;
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */