xref: /aoo42x/main/sc/source/ui/vba/vbainterior.cxx (revision b3f79822)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/table/XCell.hpp>
25 #include <com/sun/star/table/XColumnRowRange.hpp>
26 #include <com/sun/star/beans/XIntrospection.hpp>
27 #include <com/sun/star/beans/XIntrospectionAccess.hpp>
28 #include <com/sun/star/reflection/XIdlMethod.hpp>
29 #include <com/sun/star/beans/MethodConcept.hpp>
30 #include <com/sun/star/beans/NamedValue.hpp>
31 #include <com/sun/star/xml/AttributeData.hpp>
32 
33 #include <ooo/vba/excel/XlColorIndex.hpp>
34 #include <ooo/vba/excel/XlPattern.hpp>
35 
36 #include <comphelper/processfactory.hxx>
37 #include <cppuhelper/queryinterface.hxx>
38 
39 #include <map>
40 
41 #include <svx/xtable.hxx>
42 
43 #include "vbainterior.hxx"
44 #include "vbapalette.hxx"
45 #include "document.hxx"
46 
47 #define STATIC_TABLE_SIZE( array )  (sizeof(array)/sizeof(*(array)))
48 #define COLORMAST 0xFFFFFF
49 const sal_uInt16 EXC_COLOR_WINDOWBACK = 65;
50 typedef std::map< sal_Int32, sal_Int32 >  PatternMap;
51 typedef std::pair< sal_Int32, sal_Int32 > PatternPair;
52 using namespace ::com::sun::star;
53 using namespace ::ooo::vba;
54 using namespace ::ooo::vba::excel::XlPattern;
55 static const rtl::OUString BACKCOLOR( RTL_CONSTASCII_USTRINGPARAM( "CellBackColor" ) );
56 static const rtl::OUString PATTERN( RTL_CONSTASCII_USTRINGPARAM( "Pattern" ) );
57 static const rtl::OUString PATTERNCOLOR( RTL_CONSTASCII_USTRINGPARAM( "PatternColor" ) );
58 
lcl_getPatternMap()59 PatternMap lcl_getPatternMap()
60 {
61     PatternMap aPatternMap;
62     aPatternMap.insert( PatternPair( xlPatternAutomatic, 0 ) );
63     aPatternMap.insert( PatternPair( xlPatternChecker, 9 ) );
64     aPatternMap.insert( PatternPair( xlPatternCrissCross, 16 ) );
65     aPatternMap.insert( PatternPair( xlPatternDown, 7 ) );
66     aPatternMap.insert( PatternPair( xlPatternGray16, 17 ) );
67     aPatternMap.insert( PatternPair( xlPatternGray25, 4 ) );
68     aPatternMap.insert( PatternPair( xlPatternGray50, 2 ) );
69     aPatternMap.insert( PatternPair( xlPatternGray75, 3 ) );
70     aPatternMap.insert( PatternPair( xlPatternGray8, 18 ) );
71     aPatternMap.insert( PatternPair( xlPatternGrid, 15 ) );
72     aPatternMap.insert( PatternPair( xlPatternHorizontal, 5 ) );
73     aPatternMap.insert( PatternPair( xlPatternLightDown, 13 ) );
74     aPatternMap.insert( PatternPair( xlPatternLightHorizontal, 11 ) );
75     aPatternMap.insert( PatternPair( xlPatternLightUp, 14 ) );
76     aPatternMap.insert( PatternPair( xlPatternLightVertical, 12 ) );
77     aPatternMap.insert( PatternPair( xlPatternNone, 0 ) );
78     aPatternMap.insert( PatternPair( xlPatternSemiGray75, 10 ) );
79     aPatternMap.insert( PatternPair( xlPatternSolid, 0 ) );
80     aPatternMap.insert( PatternPair( xlPatternUp, 8 ) );
81     aPatternMap.insert( PatternPair( xlPatternVertical, 6 ) );
82     return aPatternMap;
83 }
84 
85 static PatternMap aPatternMap( lcl_getPatternMap() );
86 
ScVbaInterior(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<beans::XPropertySet> & xProps,ScDocument * pScDoc)87 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 )
88 {
89     // auto color
90     //m_aPattColor.SetColor( (sal_uInt32)0xFFFFFFFF );
91     m_aPattColor.SetColor( (sal_uInt32)0x0 );
92     m_nPattern = 0L;
93 	if ( !m_xProps.is() )
94 		throw lang::IllegalArgumentException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "properties") ), uno::Reference< uno::XInterface >(), 2 );
95 }
96 
97 uno::Any
getColor()98 ScVbaInterior::getColor() throw (uno::RuntimeException)
99 {
100     Color aBackColor( GetBackColor() );
101 	return uno::makeAny( OORGBToXLRGB( aBackColor.GetColor() ) );
102 }
103 
104 void
setColor(const uno::Any & _color)105 ScVbaInterior::setColor( const uno::Any& _color  ) throw (uno::RuntimeException)
106 {
107     sal_Int32 nColor = 0;
108     if( _color >>= nColor )
109     {
110         SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( XLRGBToOORGB( nColor ) ) );
111 	    //m_xProps->setPropertyValue( BACKCOLOR , XLRGBToOORGB(_color));
112         SetMixedColor();
113     }
114 }
115 
116 void
SetMixedColor()117 ScVbaInterior::SetMixedColor()
118 {
119     // pattern
120     uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
121     if( aPattern.hasValue() )
122     {
123         m_nPattern = GetAttributeData( aPattern );
124     }
125     sal_Int32 nPattern = aPatternMap[ m_nPattern ];
126     // pattern color
127     uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
128     if( aPatternColor.hasValue() )
129     {
130         sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
131         m_aPattColor.SetColor( nPatternColor );
132     }
133     sal_Int32 nPatternColor = m_aPattColor.GetColor();
134     // back color
135     Color aBackColor( GetBackColor() );
136     // set mixed color
137     Color aMixedColor;
138     if( nPattern > 0 )
139         aMixedColor = GetPatternColor( Color(nPatternColor), aBackColor, (sal_uInt32)nPattern );
140     else
141         aMixedColor = GetPatternColor( aBackColor, aBackColor, (sal_uInt32)nPattern );
142     sal_Int32 nMixedColor = aMixedColor.GetColor() & COLORMAST;
143 	m_xProps->setPropertyValue( BACKCOLOR , uno::makeAny( nMixedColor ) );
144 }
145 
146 uno::Reference< container::XIndexAccess >
getPalette()147 ScVbaInterior::getPalette()
148 {
149 	if ( !m_pScDoc )
150 		throw uno::RuntimeException();
151 	SfxObjectShell* pShell = m_pScDoc->GetDocumentShell();
152 	ScVbaPalette aPalette( pShell );
153 	return aPalette.getPalette();
154 }
155 
156 void SAL_CALL
setColorIndex(const css::uno::Any & _colorindex)157 ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex ) throw (css::uno::RuntimeException)
158 {
159 	sal_Int32 nIndex = 0;
160 	_colorindex >>= nIndex;
161 
162     // hackly for excel::XlColorIndex::xlColorIndexNone
163     if( nIndex == excel::XlColorIndex::xlColorIndexNone )
164     {
165         m_xProps->setPropertyValue( BACKCOLOR, uno::makeAny( sal_Int32( -1 ) ) );
166     }
167     else
168     {
169 	    // setColor expects colors in XL RGB values
170     	// #FIXME this is daft we convert OO RGB val to XL RGB val and
171 	    // then back again to OO RGB value
172     	setColor( OORGBToXLRGB( GetIndexColor( nIndex ) ) );
173     }
174 }
175 uno::Any
GetIndexColor(const sal_Int32 & nColorIndex)176 ScVbaInterior::GetIndexColor( const sal_Int32& nColorIndex )
177 {
178 	sal_Int32 nIndex = nColorIndex;
179 	// #FIXME  xlColorIndexAutomatic & xlColorIndexNone are not really
180 	// handled properly here
181 	if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) || ( nIndex == excel::XlColorIndex::xlColorIndexNone )  )
182 		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 )
183 	--nIndex; // OOo indices are zero bases
184 	uno::Reference< container::XIndexAccess > xIndex = getPalette();
185     return xIndex->getByIndex( nIndex );
186 }
187 
188 sal_Int32
GetColorIndex(const sal_Int32 nColor)189 ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
190 {
191 	uno::Reference< container::XIndexAccess > xIndex = getPalette();
192 	sal_Int32 nElems = xIndex->getCount();
193 	sal_Int32 nIndex = -1;
194 	for ( sal_Int32 count=0; count<nElems; ++count )
195        	{
196 		sal_Int32 nPaletteColor = 0;
197 		xIndex->getByIndex( count ) >>= nPaletteColor;
198 		if ( nPaletteColor == nColor )
199 		{
200 			nIndex = count + 1; // 1 based
201 			break;
202 		}
203 	}
204     return nIndex;
205 }
206 
207 uno::Any SAL_CALL
getColorIndex()208 ScVbaInterior::getColorIndex() throw ( css::uno::RuntimeException )
209 {
210 	sal_Int32 nColor = 0;
211     // hackly for excel::XlColorIndex::xlColorIndexNone
212     uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
213     if( ( aColor >>= nColor ) && ( nColor == -1 ) )
214     {
215         nColor = excel::XlColorIndex::xlColorIndexNone;
216         return uno::makeAny( nColor );
217     }
218 
219 	// getColor returns Xl ColorValue, need to convert it to OO val
220 	// as the palette deals with OO RGB values
221 	// #FIXME this is daft in getColor we convert OO RGB val to XL RGB val
222 	// and then back again to OO RGB value
223 	XLRGBToOORGB( getColor() ) >>= nColor;
224 
225 	return uno::makeAny( GetColorIndex( nColor ) );
226 }
227 Color
GetPatternColor(const Color & rPattColor,const Color & rBackColor,sal_uInt32 nXclPattern)228 ScVbaInterior::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt32 nXclPattern )
229 {
230     // 0x00 == 0% transparence (full rPattColor)
231     // 0x80 == 100% transparence (full rBackColor)
232     static const sal_uInt8 pnRatioTable[] =
233     {
234         0x80, 0x00, 0x40, 0x20, 0x60, 0x40, 0x40, 0x40,     // 00 - 07
235         0x40, 0x40, 0x20, 0x60, 0x60, 0x60, 0x60, 0x48,     // 08 - 15
236         0x50, 0x70, 0x78                                    // 16 - 18
237     };
238     return ( nXclPattern < STATIC_TABLE_SIZE( pnRatioTable ) ) ?
239         GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
240 }
241 Color
GetMixedColor(const Color & rFore,const Color & rBack,sal_uInt8 nTrans)242 ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
243 {
244     return Color(
245         nTrans,
246         GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
247         GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
248         GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
249 }
250 sal_uInt8
GetMixedColorComp(sal_uInt8 nFore,sal_uInt8 nBack,sal_uInt8 nTrans)251 ScVbaInterior::GetMixedColorComp(  sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
252 {
253     sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
254     return static_cast< sal_uInt8 >( nTemp );
255 }
256 uno::Reference< container::XNameContainer >
GetAttributeContainer()257 ScVbaInterior::GetAttributeContainer()
258 {
259     return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue( rtl::OUString::createFromAscii( "UserDefinedAttributes" ) ), uno::UNO_QUERY_THROW );
260 }
261 sal_Int32
GetAttributeData(uno::Any aValue)262 ScVbaInterior::GetAttributeData( uno::Any aValue )
263 {
264     xml::AttributeData aDataValue;
265     if( aValue >>= aDataValue )
266     {
267         return aDataValue.Value.toInt32();
268     }
269     return sal_Int32( 0 );
270 }
271 uno::Any
SetAttributeData(sal_Int32 nValue)272 ScVbaInterior::SetAttributeData( sal_Int32 nValue )
273 {
274     xml::AttributeData aAttributeData;
275     //aAttributeData.Namespace = rtl::OUString::createFromAscii( "ooo.vba.excel.CellPatten");
276     aAttributeData.Type = rtl::OUString::createFromAscii( "sal_Int32" );
277     aAttributeData.Value = rtl::OUString::valueOf( nValue );
278     return uno::makeAny( aAttributeData );
279 }
280 uno::Any
GetUserDefinedAttributes(const rtl::OUString & sName)281 ScVbaInterior::GetUserDefinedAttributes( const rtl::OUString& sName )
282 {
283     uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
284     if( xNameContainer->hasByName( sName ) )
285     {
286         return xNameContainer->getByName( sName );
287     }
288     return uno::Any();
289 }
290 void
SetUserDefinedAttributes(const rtl::OUString & sName,const uno::Any & aValue)291 ScVbaInterior::SetUserDefinedAttributes( const rtl::OUString& sName, const uno::Any& aValue )
292 {
293     if( aValue.hasValue() )
294     {
295         uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
296         if( xNameContainer->hasByName( sName ) )
297             xNameContainer->removeByName( sName );
298         xNameContainer->insertByName( sName, aValue );
299         m_xProps->setPropertyValue( rtl::OUString::createFromAscii( "UserDefinedAttributes" ), uno::makeAny( xNameContainer ) );
300     }
301 }
302 // OOo do not support below API
303 uno::Any SAL_CALL
getPattern()304 ScVbaInterior::getPattern() throw (uno::RuntimeException)
305 {
306     // XlPattern
307     uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
308     if( aPattern.hasValue() )
309         return uno::makeAny( GetAttributeData( aPattern ) );
310     return uno::makeAny( excel::XlPattern::xlPatternNone );
311 }
312 void SAL_CALL
setPattern(const uno::Any & _pattern)313 ScVbaInterior::setPattern( const uno::Any& _pattern ) throw (uno::RuntimeException)
314 {
315     if( _pattern >>= m_nPattern )
316     {
317         SetUserDefinedAttributes( PATTERN, SetAttributeData( m_nPattern ) );
318         SetMixedColor();
319     }
320     else
321         throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid Pattern index" ), uno::Reference< uno::XInterface >() );
322 }
323 Color
GetBackColor()324 ScVbaInterior::GetBackColor()
325 {
326     sal_Int32 nColor = 0;
327     Color aBackColor;
328     uno::Any aColor = GetUserDefinedAttributes( BACKCOLOR );
329     if( aColor.hasValue() )
330     {
331         nColor = GetAttributeData( aColor );
332         aBackColor.SetColor( nColor );
333     }
334     else
335     {
336 	    uno::Any aAny;
337 	    aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
338         if( aAny >>= nColor )
339         {
340             nColor = XLRGBToOORGB( nColor );
341             aBackColor.SetColor( nColor );
342             SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( nColor ) );
343         }
344     }
345     return aBackColor;
346 }
347 uno::Any SAL_CALL
getPatternColor()348 ScVbaInterior::getPatternColor() throw (uno::RuntimeException)
349 {
350     // 0 is the default color. no filled.
351     uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
352     if( aPatternColor.hasValue() )
353     {
354         sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
355         return uno::makeAny( OORGBToXLRGB( nPatternColor ) );
356     }
357     return uno::makeAny( sal_Int32( 0 ) );
358 }
359 void SAL_CALL
setPatternColor(const uno::Any & _patterncolor)360 ScVbaInterior::setPatternColor( const uno::Any& _patterncolor ) throw (uno::RuntimeException)
361 {
362     sal_Int32 nPattColor = 0;
363     if( _patterncolor >>= nPattColor )
364     {
365         SetUserDefinedAttributes( PATTERNCOLOR, SetAttributeData( XLRGBToOORGB( nPattColor ) ) );
366         SetMixedColor();
367     }
368     else
369         throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid Pattern Color" ), uno::Reference< uno::XInterface >() );
370 }
371 uno::Any SAL_CALL
getPatternColorIndex()372 ScVbaInterior::getPatternColorIndex() throw (uno::RuntimeException)
373 {
374 	sal_Int32 nColor = 0;
375 	XLRGBToOORGB( getPatternColor() ) >>= nColor;
376 
377 	return uno::makeAny( GetIndexColor( nColor ) );
378 }
379 void SAL_CALL
setPatternColorIndex(const uno::Any & _patterncolorindex)380 ScVbaInterior::setPatternColorIndex( const uno::Any& _patterncolorindex ) throw (uno::RuntimeException)
381 {
382     sal_Int32 nColorIndex = 0;
383     if( _patterncolorindex >>= nColorIndex )
384     {
385         if( nColorIndex == 0 )
386             return;
387         sal_Int32 nPattColor = 0;
388         GetIndexColor( nColorIndex ) >>= nPattColor;
389         setPatternColor( uno::makeAny( OORGBToXLRGB( nPattColor ) ) );
390     }
391     else
392         throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid Pattern Color" ), uno::Reference< uno::XInterface >() );
393 }
394 
395 rtl::OUString&
getServiceImplName()396 ScVbaInterior::getServiceImplName()
397 {
398 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaInterior") );
399 	return sImplName;
400 }
401 
402 uno::Sequence< rtl::OUString >
getServiceNames()403 ScVbaInterior::getServiceNames()
404 {
405 	static uno::Sequence< rtl::OUString > aServiceNames;
406 	if ( aServiceNames.getLength() == 0 )
407 	{
408 		aServiceNames.realloc( 1 );
409 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Interior" ) );
410 	}
411 	return aServiceNames;
412 }
413 
414