fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / vba / vbainterior.cxx
blobaa2fe734748b142eaef93cb5084057acebdf2ec9
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 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";
54 static PatternMap lcl_getPatternMap()
56 PatternMap aPatternMap;
57 aPatternMap.insert( PatternPair( xlPatternAutomatic, 0 ) );
58 aPatternMap.insert( PatternPair( xlPatternChecker, 9 ) );
59 aPatternMap.insert( PatternPair( xlPatternCrissCross, 16 ) );
60 aPatternMap.insert( PatternPair( xlPatternDown, 7 ) );
61 aPatternMap.insert( PatternPair( xlPatternGray16, 17 ) );
62 aPatternMap.insert( PatternPair( xlPatternGray25, 4 ) );
63 aPatternMap.insert( PatternPair( xlPatternGray50, 2 ) );
64 aPatternMap.insert( PatternPair( xlPatternGray75, 3 ) );
65 aPatternMap.insert( PatternPair( xlPatternGray8, 18 ) );
66 aPatternMap.insert( PatternPair( xlPatternGrid, 15 ) );
67 aPatternMap.insert( PatternPair( xlPatternHorizontal, 5 ) );
68 aPatternMap.insert( PatternPair( xlPatternLightDown, 13 ) );
69 aPatternMap.insert( PatternPair( xlPatternLightHorizontal, 11 ) );
70 aPatternMap.insert( PatternPair( xlPatternLightUp, 14 ) );
71 aPatternMap.insert( PatternPair( xlPatternLightVertical, 12 ) );
72 aPatternMap.insert( PatternPair( xlPatternNone, 0 ) );
73 aPatternMap.insert( PatternPair( xlPatternSemiGray75, 10 ) );
74 aPatternMap.insert( PatternPair( xlPatternSolid, 0 ) );
75 aPatternMap.insert( PatternPair( xlPatternUp, 8 ) );
76 aPatternMap.insert( PatternPair( xlPatternVertical, 6 ) );
77 return aPatternMap;
80 static PatternMap aPatternMap( lcl_getPatternMap() );
82 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 // auto color
85 m_aPattColor.SetColor( (sal_uInt32)0x0 );
86 m_nPattern = 0L;
87 if ( !m_xProps.is() )
88 throw lang::IllegalArgumentException("properties", uno::Reference< uno::XInterface >(), 2 );
91 uno::Any
92 ScVbaInterior::getColor() throw (uno::RuntimeException, std::exception)
94 Color aBackColor( GetBackColor() );
95 return uno::makeAny( OORGBToXLRGB( aBackColor.GetColor() ) );
98 void
99 ScVbaInterior::setColor( const uno::Any& _color ) throw (uno::RuntimeException, std::exception)
101 sal_Int32 nColor = 0;
102 if( _color >>= nColor )
104 SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( XLRGBToOORGB( nColor ) ) );
105 SetMixedColor();
109 void
110 ScVbaInterior::SetMixedColor()
112 // pattern
113 uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
114 if( aPattern.hasValue() )
116 m_nPattern = GetAttributeData( aPattern );
118 sal_Int32 nPattern = aPatternMap[ m_nPattern ];
119 // pattern color
120 uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
121 if( aPatternColor.hasValue() )
123 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
124 m_aPattColor.SetColor( nPatternColor );
126 sal_Int32 nPatternColor = m_aPattColor.GetColor();
127 // back color
128 Color aBackColor( GetBackColor() );
129 // set mixed color
130 Color aMixedColor;
131 if( nPattern > 0 )
132 aMixedColor = GetPatternColor( Color(nPatternColor), aBackColor, (sal_uInt32)nPattern );
133 else
134 aMixedColor = GetPatternColor( aBackColor, aBackColor, (sal_uInt32)nPattern );
135 sal_Int32 nMixedColor = aMixedColor.GetColor() & COLORMAST;
136 m_xProps->setPropertyValue( BACKCOLOR , uno::makeAny( nMixedColor ) );
139 uno::Reference< container::XIndexAccess >
140 ScVbaInterior::getPalette()
142 if ( !m_pScDoc )
143 throw uno::RuntimeException();
144 SfxObjectShell* pShell = m_pScDoc->GetDocumentShell();
145 ScVbaPalette aPalette( pShell );
146 return aPalette.getPalette();
149 void SAL_CALL
150 ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex ) throw (css::uno::RuntimeException, std::exception)
152 sal_Int32 nIndex = 0;
153 _colorindex >>= nIndex;
155 // hackly for excel::XlColorIndex::xlColorIndexNone
156 if( nIndex == excel::XlColorIndex::xlColorIndexNone )
158 m_xProps->setPropertyValue( BACKCOLOR, uno::makeAny( sal_Int32( -1 ) ) );
160 else
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 setColor( OORGBToXLRGB( GetIndexColor( nIndex ) ) );
168 uno::Any
169 ScVbaInterior::GetIndexColor( const sal_Int32& nColorIndex )
171 sal_Int32 nIndex = nColorIndex;
172 // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
173 // handled properly here
174 if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) || ( nIndex == excel::XlColorIndex::xlColorIndexNone ) )
175 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 --nIndex; // OOo indices are zero bases
177 uno::Reference< container::XIndexAccess > xIndex = getPalette();
178 return xIndex->getByIndex( nIndex );
181 sal_Int32
182 ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
184 uno::Reference< container::XIndexAccess > xIndex = getPalette();
185 sal_Int32 nElems = xIndex->getCount();
186 sal_Int32 nIndex = -1;
187 for ( sal_Int32 count=0; count<nElems; ++count )
189 sal_Int32 nPaletteColor = 0;
190 xIndex->getByIndex( count ) >>= nPaletteColor;
191 if ( nPaletteColor == nColor )
193 nIndex = count + 1; // 1 based
194 break;
197 return nIndex;
200 uno::Any SAL_CALL
201 ScVbaInterior::getColorIndex() throw ( css::uno::RuntimeException, std::exception )
203 sal_Int32 nColor = 0;
204 // hackly for excel::XlColorIndex::xlColorIndexNone
205 uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
206 if( ( aColor >>= nColor ) && ( nColor == -1 ) )
208 nColor = excel::XlColorIndex::xlColorIndexNone;
209 return uno::makeAny( nColor );
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 XLRGBToOORGB( getColor() ) >>= nColor;
218 return uno::makeAny( GetColorIndex( nColor ) );
220 Color
221 ScVbaInterior::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt32 nXclPattern )
223 // 0x00 == 0% transparence (full rPattColor)
224 // 0x80 == 100% transparence (full rBackColor)
225 static const sal_uInt8 pnRatioTable[] =
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
231 return ( nXclPattern < SAL_N_ELEMENTS( pnRatioTable ) ) ?
232 GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
234 Color
235 ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
237 return Color(
238 nTrans,
239 GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
240 GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
241 GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
243 sal_uInt8
244 ScVbaInterior::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
246 sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
247 return static_cast< sal_uInt8 >( nTemp );
249 uno::Reference< container::XNameContainer >
250 ScVbaInterior::GetAttributeContainer()
252 return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue("UserDefinedAttributes"), uno::UNO_QUERY_THROW );
254 sal_Int32
255 ScVbaInterior::GetAttributeData( uno::Any aValue )
257 xml::AttributeData aDataValue;
258 if( aValue >>= aDataValue )
260 return aDataValue.Value.toInt32();
262 return sal_Int32( 0 );
264 uno::Any
265 ScVbaInterior::SetAttributeData( sal_Int32 nValue )
267 xml::AttributeData aAttributeData;
268 aAttributeData.Type = "sal_Int32";
269 aAttributeData.Value = OUString::number( nValue );
270 return uno::makeAny( aAttributeData );
272 uno::Any
273 ScVbaInterior::GetUserDefinedAttributes( const OUString& sName )
275 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
276 if( xNameContainer->hasByName( sName ) )
278 return xNameContainer->getByName( sName );
280 return uno::Any();
282 void
283 ScVbaInterior::SetUserDefinedAttributes( const OUString& sName, const uno::Any& aValue )
285 if( aValue.hasValue() )
287 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
288 if( xNameContainer->hasByName( sName ) )
289 xNameContainer->removeByName( sName );
290 xNameContainer->insertByName( sName, aValue );
291 m_xProps->setPropertyValue("UserDefinedAttributes", uno::makeAny( xNameContainer ) );
294 // OOo do not support below API
295 uno::Any SAL_CALL
296 ScVbaInterior::getPattern() throw (uno::RuntimeException, std::exception)
298 // XlPattern
299 uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
300 if( aPattern.hasValue() )
301 return uno::makeAny( GetAttributeData( aPattern ) );
302 return uno::makeAny( excel::XlPattern::xlPatternNone );
304 void SAL_CALL
305 ScVbaInterior::setPattern( const uno::Any& _pattern ) throw (uno::RuntimeException, std::exception)
307 if( _pattern >>= m_nPattern )
309 SetUserDefinedAttributes( PATTERN, SetAttributeData( m_nPattern ) );
310 SetMixedColor();
312 else
313 throw uno::RuntimeException("Invalid Pattern index" );
315 Color
316 ScVbaInterior::GetBackColor()
318 sal_Int32 nColor = 0;
319 Color aBackColor;
320 uno::Any aColor = GetUserDefinedAttributes( BACKCOLOR );
321 if( aColor.hasValue() )
323 nColor = GetAttributeData( aColor );
324 aBackColor.SetColor( nColor );
326 else
328 uno::Any aAny;
329 aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
330 if( aAny >>= nColor )
332 nColor = XLRGBToOORGB( nColor );
333 aBackColor.SetColor( nColor );
334 SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( nColor ) );
337 return aBackColor;
339 uno::Any SAL_CALL
340 ScVbaInterior::getPatternColor() throw (uno::RuntimeException, std::exception)
342 // 0 is the default color. no filled.
343 uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
344 if( aPatternColor.hasValue() )
346 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
347 return uno::makeAny( OORGBToXLRGB( nPatternColor ) );
349 return uno::makeAny( sal_Int32( 0 ) );
351 void SAL_CALL
352 ScVbaInterior::setPatternColor( const uno::Any& _patterncolor ) throw (uno::RuntimeException, std::exception)
354 sal_Int32 nPattColor = 0;
355 if( _patterncolor >>= nPattColor )
357 SetUserDefinedAttributes( PATTERNCOLOR, SetAttributeData( XLRGBToOORGB( nPattColor ) ) );
358 SetMixedColor();
360 else
361 throw uno::RuntimeException("Invalid Pattern Color" );
363 uno::Any SAL_CALL
364 ScVbaInterior::getPatternColorIndex() throw (uno::RuntimeException, std::exception)
366 sal_Int32 nColor = 0;
367 XLRGBToOORGB( getPatternColor() ) >>= nColor;
369 return GetIndexColor( nColor );
371 void SAL_CALL
372 ScVbaInterior::setPatternColorIndex( const uno::Any& _patterncolorindex ) throw (uno::RuntimeException, std::exception)
374 sal_Int32 nColorIndex = 0;
375 if( _patterncolorindex >>= nColorIndex )
377 if( nColorIndex == 0 )
378 return;
379 sal_Int32 nPattColor = 0;
380 GetIndexColor( nColorIndex ) >>= nPattColor;
381 setPatternColor( uno::makeAny( OORGBToXLRGB( nPattColor ) ) );
383 else
384 throw uno::RuntimeException("Invalid Pattern Color" );
387 OUString
388 ScVbaInterior::getServiceImplName()
390 return OUString("ScVbaInterior");
393 uno::Sequence< OUString >
394 ScVbaInterior::getServiceNames()
396 static uno::Sequence< OUString > aServiceNames;
397 if ( aServiceNames.getLength() == 0 )
399 aServiceNames.realloc( 1 );
400 aServiceNames[ 0 ] = "ooo.vba.excel.Interior";
402 return aServiceNames;
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */