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