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