LibreOffice Module sc (master) 1
vbainterior.cxx
Go to the documentation of this file.
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/xml/AttributeData.hpp>
21
22#include <ooo/vba/excel/XlColorIndex.hpp>
23#include <ooo/vba/excel/XlPattern.hpp>
24
25#include <map>
26
27#include <sal/macros.h>
28
29#include "vbainterior.hxx"
30#include "vbapalette.hxx"
31#include <document.hxx>
32#include <utility>
33
34using namespace ::com::sun::star;
35using namespace ::ooo::vba;
36using namespace ::ooo::vba::excel::XlPattern;
37
38constexpr OUStringLiteral BACKCOLOR = u"CellBackColor";
39constexpr OUStringLiteral PATTERN = u"Pattern";
40constexpr OUStringLiteral PATTERNCOLOR = u"PatternColor";
41
42static std::map< sal_Int32, sal_Int32 > aPatternMap {
43 { xlPatternAutomatic, 0 },
44 { xlPatternChecker, 9 },
45 { xlPatternCrissCross, 16 },
46 { xlPatternDown, 7 },
47 { xlPatternGray16, 17 },
48 { xlPatternGray25, 4 },
49 { xlPatternGray50, 2 },
50 { xlPatternGray75, 3 },
51 { xlPatternGray8, 18 },
52 { xlPatternGrid, 15 },
53 { xlPatternHorizontal, 5 },
54 { xlPatternLightDown, 13 },
55 { xlPatternLightHorizontal, 11 },
56 { xlPatternLightUp, 14 },
57 { xlPatternLightVertical, 12 },
58 { xlPatternNone, 0 },
59 { xlPatternSemiGray75, 10 },
60 { xlPatternSolid, 0 },
61 { xlPatternUp, 8 },
62 { xlPatternVertical, 6 }
63};
64
65ScVbaInterior::ScVbaInterior( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, uno::Reference< beans::XPropertySet > xProps, ScDocument* pScDoc ) : ScVbaInterior_BASE( xParent, xContext ), m_xProps(std::move(xProps)), m_pScDoc( pScDoc )
66{
67 // auto color
69 m_nPattern = 0;
70 if ( !m_xProps.is() )
71 throw lang::IllegalArgumentException("properties", uno::Reference< uno::XInterface >(), 2 );
72}
73
76{
77 return uno::Any( OORGBToXLRGB( GetBackColor() ) );
78}
79
80void
82{
83 sal_Int32 nColor = 0;
84 if( _color >>= nColor )
85 {
88 }
89}
90
91void
93{
94 // pattern
96 if( aPattern.hasValue() )
97 {
98 m_nPattern = GetAttributeData( aPattern );
99 }
100 sal_Int32 nPattern = aPatternMap[ m_nPattern ];
101 // pattern color
103 if( aPatternColor.hasValue() )
104 {
105 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
106 m_aPattColor = Color(ColorTransparency, nPatternColor);
107 }
108 Color nPatternColor = m_aPattColor;
109 // back color
110 Color aBackColor( GetBackColor() );
111 // set mixed color
112 Color aMixedColor;
113 if( nPattern > 0 )
114 aMixedColor = GetPatternColor( nPatternColor, aBackColor, static_cast<sal_uInt32>(nPattern) );
115 else
116 aMixedColor = GetPatternColor( aBackColor, aBackColor, static_cast<sal_uInt32>(nPattern) );
117 Color nMixedColor = aMixedColor.GetRGBColor();
118 m_xProps->setPropertyValue( BACKCOLOR , uno::Any( nMixedColor ) );
119}
120
121uno::Reference< container::XIndexAccess >
123{
124 if ( !m_pScDoc )
125 throw uno::RuntimeException();
127 ScVbaPalette aPalette( pShell );
128 return aPalette.getPalette();
129}
130
131void SAL_CALL
132ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex )
133{
134 sal_Int32 nIndex = 0;
135 _colorindex >>= nIndex;
136
137 // hackly for excel::XlColorIndex::xlColorIndexNone
138 if( nIndex == excel::XlColorIndex::xlColorIndexNone )
139 {
140 m_xProps->setPropertyValue( BACKCOLOR, uno::Any( sal_Int32( -1 ) ) );
141 }
142 else
143 {
144 // setColor expects colors in XL RGB values
145 // #FIXME this is daft we convert OO RGB val to XL RGB val and
146 // then back again to OO RGB value
148 }
149}
151ScVbaInterior::GetIndexColor( sal_Int32 nColorIndex )
152{
153 sal_Int32 nIndex = nColorIndex;
154 // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
155 // handled properly here
156 if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) || ( nIndex == excel::XlColorIndex::xlColorIndexNone ) )
157 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 )
158 --nIndex; // OOo indices are zero bases
159 uno::Reference< container::XIndexAccess > xIndex = getPalette();
160 return xIndex->getByIndex( nIndex );
161}
162
163sal_Int32
164ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
165{
166 uno::Reference< container::XIndexAccess > xIndex = getPalette();
167 sal_Int32 nElems = xIndex->getCount();
168 sal_Int32 nIndex = -1;
169 for ( sal_Int32 count=0; count<nElems; ++count )
170 {
171 sal_Int32 nPaletteColor = 0;
172 xIndex->getByIndex( count ) >>= nPaletteColor;
173 if ( nPaletteColor == nColor )
174 {
175 nIndex = count + 1; // 1 based
176 break;
177 }
178 }
179 return nIndex;
180}
181
182uno::Any SAL_CALL
184{
185 sal_Int32 nColor = 0;
186 // hackly for excel::XlColorIndex::xlColorIndexNone
187 uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
188 if( ( aColor >>= nColor ) && ( nColor == -1 ) )
189 {
190 nColor = excel::XlColorIndex::xlColorIndexNone;
191 return uno::Any( nColor );
192 }
193
194 // getColor returns Xl ColorValue, need to convert it to OO val
195 // as the palette deals with OO RGB values
196 // #FIXME this is daft in getColor we convert OO RGB val to XL RGB val
197 // and then back again to OO RGB value
198 XLRGBToOORGB( getColor() ) >>= nColor;
199
200 return uno::Any( GetColorIndex( nColor ) );
201}
202Color
203ScVbaInterior::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt32 nXclPattern )
204{
205 // 0x00 == 0% transparence (full rPattColor)
206 // 0x80 == 100% transparence (full rBackColor)
207 static const sal_uInt8 pnRatioTable[] =
208 {
209 0x80, 0x00, 0x40, 0x20, 0x60, 0x40, 0x40, 0x40, // 00 - 07
210 0x40, 0x40, 0x20, 0x60, 0x60, 0x60, 0x60, 0x48, // 08 - 15
211 0x50, 0x70, 0x78 // 16 - 18
212 };
213 return ( nXclPattern < SAL_N_ELEMENTS( pnRatioTable ) ) ?
214 GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
215}
216Color
217ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
218{
219 return Color(
220 ColorTransparency, nTrans,
221 GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
222 GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
223 GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
224}
227{
228 sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
229 return static_cast< sal_uInt8 >( nTemp );
230}
231uno::Reference< container::XNameContainer >
233{
234 return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue("UserDefinedAttributes"), uno::UNO_QUERY_THROW );
235}
236sal_Int32
238{
239 xml::AttributeData aDataValue;
240 if( aValue >>= aDataValue )
241 {
242 return aDataValue.Value.toInt32();
243 }
244 return 0;
245}
248{
249 xml::AttributeData aAttributeData;
250 aAttributeData.Type = "sal_Int32";
251 aAttributeData.Value = OUString::number( nValue );
252 return uno::Any( aAttributeData );
253}
256{
257 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_SET_THROW );
258 if( xNameContainer->hasByName( sName ) )
259 {
260 return xNameContainer->getByName( sName );
261 }
262 return uno::Any();
263}
264void
265ScVbaInterior::SetUserDefinedAttributes( const OUString& sName, const uno::Any& aValue )
266{
267 if( aValue.hasValue() )
268 {
269 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_SET_THROW );
270 if( xNameContainer->hasByName( sName ) )
271 xNameContainer->removeByName( sName );
272 xNameContainer->insertByName( sName, aValue );
273 m_xProps->setPropertyValue("UserDefinedAttributes", uno::Any( xNameContainer ) );
274 }
275}
276// OOo do not support below API
277uno::Any SAL_CALL
279{
280 // XlPattern
282 if( aPattern.hasValue() )
283 return uno::Any( GetAttributeData( aPattern ) );
284 return uno::Any( excel::XlPattern::xlPatternNone );
285}
286void SAL_CALL
288{
289 if( !(_pattern >>= m_nPattern) )
290 throw uno::RuntimeException("Invalid Pattern index" );
291
294
295}
296Color
298{
299 sal_Int32 nColor = 0;
300 Color aBackColor;
302 if( aColor.hasValue() )
303 {
304 nColor = GetAttributeData( aColor );
305 aBackColor = Color(ColorTransparency, nColor);
306 }
307 else
308 {
309 uno::Any aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
310 if( aAny >>= nColor )
311 {
312 nColor = XLRGBToOORGB( nColor );
313 aBackColor = Color(ColorTransparency, nColor);
315 }
316 }
317 return aBackColor;
318}
319uno::Any SAL_CALL
321{
322 // 0 is the default color. no filled.
324 if( aPatternColor.hasValue() )
325 {
326 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
327 return uno::Any( OORGBToXLRGB( Color(ColorTransparency, nPatternColor) ) );
328 }
329 return uno::Any( sal_Int32( 0 ) );
330}
331void SAL_CALL
333{
334 sal_Int32 nPattColor = 0;
335 if( !(_patterncolor >>= nPattColor) )
336 throw uno::RuntimeException("Invalid Pattern Color" );
337
340
341}
342uno::Any SAL_CALL
344{
345 sal_Int32 nColor = 0;
346 XLRGBToOORGB( getPatternColor() ) >>= nColor;
347
348 return uno::Any( GetColorIndex( nColor ) );
349}
350void SAL_CALL
352{
353 sal_Int32 nColorIndex = 0;
354 if( !(_patterncolorindex >>= nColorIndex) )
355 throw uno::RuntimeException("Invalid Pattern Color" );
356
357 if( nColorIndex == 0 )
358 return;
359 Color nPattColor;
360 GetIndexColor( nColorIndex ) >>= nPattColor;
361 setPatternColor( uno::Any( OORGBToXLRGB( nPattColor ) ) );
362
363}
364
366{
367 // Just a stub for now.
368 return uno::Any(static_cast<sal_Int32>(0));
369}
370
371void SAL_CALL ScVbaInterior::setThemeColor(const uno::Any& /*rAny*/)
372{
373 // Just a stub for now.
374}
375
377{
378 // Just a stub for now.
379 return uno::Any(static_cast<double>(0));
380}
381
382void SAL_CALL ScVbaInterior::setTintAndShade(const uno::Any& /*rAny*/)
383{
384 // Just a stub for now.
385}
386
388{
389 // Just a stub for now.
390 return uno::Any(static_cast<double>(0));
391}
392
394{
395 // Just a stub for now.
396}
397
398OUString
400{
401 return "ScVbaInterior";
402}
403
404uno::Sequence< OUString >
406{
407 static uno::Sequence< OUString > const aServiceNames
408 {
409 "ooo.vba.excel.Interior"
410 };
411 return aServiceNames;
412}
413
414/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XPropertySet > m_xProps
Color GetRGBColor() const
sal_uInt8 GetBlue() const
sal_uInt8 GetRed() const
sal_uInt8 GetGreen() const
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1081
virtual void SAL_CALL setColorIndex(const css::uno::Any &_colorindex) override
sal_Int32 GetColorIndex(const sal_Int32 nColor)
Color GetBackColor()
static css::uno::Any SetAttributeData(sal_Int32 nValue)
css::uno::Reference< css::container::XIndexAccess > getPalette() const
Color m_aPattColor
Definition: vbainterior.hxx:39
virtual css::uno::Any SAL_CALL getPatternColorIndex() override
ScDocument * m_pScDoc
Definition: vbainterior.hxx:38
sal_Int32 m_nPattern
Definition: vbainterior.hxx:40
css::uno::Any GetIndexColor(sal_Int32 nColorIndex)
static sal_uInt8 GetMixedColorComp(sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans)
virtual css::uno::Any SAL_CALL getPatternColor() override
void SetMixedColor()
Definition: vbainterior.cxx:92
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Any SAL_CALL getPattern() override
void SAL_CALL setTintAndShade(const css::uno::Any &rAny) override
virtual void SAL_CALL setPattern(const css::uno::Any &_pattern) override
ScVbaInterior(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, css::uno::Reference< css::beans::XPropertySet > xProps, ScDocument *pScDoc=nullptr)
Definition: vbainterior.cxx:65
static sal_Int32 GetAttributeData(css::uno::Any const &aValue)
css::uno::Reference< css::beans::XPropertySet > m_xProps
Definition: vbainterior.hxx:37
virtual OUString getServiceImplName() override
virtual void SAL_CALL setColor(const css::uno::Any &_color) override
Definition: vbainterior.cxx:81
void SetUserDefinedAttributes(const OUString &sName, const css::uno::Any &aValue)
virtual css::uno::Any SAL_CALL getColorIndex() override
virtual void SAL_CALL setPatternColorIndex(const css::uno::Any &_patterncolorindex) override
css::uno::Any SAL_CALL getThemeColor() override
static Color GetPatternColor(const Color &rPattColor, const Color &rBackColor, sal_uInt32 nXclPattern)
static Color GetMixedColor(const Color &rFore, const Color &rBack, sal_uInt8 nTrans)
virtual void SAL_CALL setPatternColor(const css::uno::Any &_patterncolor) override
void SAL_CALL setThemeColor(const css::uno::Any &rAny) override
css::uno::Any SAL_CALL getTintAndShade() override
css::uno::Reference< css::container::XNameContainer > GetAttributeContainer()
css::uno::Any GetUserDefinedAttributes(const OUString &sName)
css::uno::Any SAL_CALL getPatternTintAndShade() override
void SAL_CALL setPatternTintAndShade(const css::uno::Any &rAny) override
virtual css::uno::Any SAL_CALL getColor() override
Definition: vbainterior.cxx:75
css::uno::Reference< css::container::XIndexAccess > getPalette() const
Definition: vbapalette.cxx:100
ColorTransparency
float u
Sequence< OUString > aServiceNames
sal_Int16 nValue
sal_Int32 nIndex
#define SAL_N_ELEMENTS(arr)
const char * sName
sal_Int32 XLRGBToOORGB(sal_Int32 nCol)
sal_Int32 OORGBToXLRGB(sal_Int32 nCol)
bool hasValue()
unsigned char sal_uInt8
static std::map< sal_Int32, sal_Int32 > aPatternMap
Definition: vbainterior.cxx:42
constexpr OUStringLiteral BACKCOLOR
Definition: vbainterior.cxx:38
constexpr OUStringLiteral PATTERN
Definition: vbainterior.cxx:39
constexpr OUStringLiteral PATTERNCOLOR
Definition: vbainterior.cxx:40