tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / vba / vbainterior.cxx
blobb83b80097c4cc5d09ba394fadcf6a1c187c40d33
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/xml/AttributeData.hpp>
22 #include <ooo/vba/excel/XlColorIndex.hpp>
23 #include <ooo/vba/excel/XlPattern.hpp>
25 #include <map>
27 #include <sal/macros.h>
29 #include "vbainterior.hxx"
30 #include "vbapalette.hxx"
31 #include <document.hxx>
32 #include <docsh.hxx>
33 #include <utility>
34 #include <frozen/bits/defines.h>
35 #include <frozen/bits/elsa_std.h>
36 #include <frozen/map.h>
38 using namespace ::com::sun::star;
39 using namespace ::ooo::vba;
40 using namespace ::ooo::vba::excel::XlPattern;
42 constexpr OUString BACKCOLOR = u"CellBackColor"_ustr;
43 constexpr OUString PATTERN = u"Pattern"_ustr;
44 constexpr OUString PATTERNCOLOR = u"PatternColor"_ustr;
46 constexpr auto aPatternMap = frozen::make_map<sal_Int32, sal_Int32>({
47 { xlPatternAutomatic, 0 },
48 { xlPatternChecker, 9 },
49 { xlPatternCrissCross, 16 },
50 { xlPatternDown, 7 },
51 { xlPatternGray16, 17 },
52 { xlPatternGray25, 4 },
53 { xlPatternGray50, 2 },
54 { xlPatternGray75, 3 },
55 { xlPatternGray8, 18 },
56 { xlPatternGrid, 15 },
57 { xlPatternHorizontal, 5 },
58 { xlPatternLightDown, 13 },
59 { xlPatternLightHorizontal, 11 },
60 { xlPatternLightUp, 14 },
61 { xlPatternLightVertical, 12 },
62 { xlPatternNone, 0 },
63 { xlPatternSemiGray75, 10 },
64 { xlPatternSolid, 0 },
65 { xlPatternUp, 8 },
66 { xlPatternVertical, 6 }
67 });
69 ScVbaInterior::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 )
71 // auto color
72 m_aPattColor = Color(0);
73 m_nPattern = 0;
74 if ( !m_xProps.is() )
75 throw lang::IllegalArgumentException(u"properties"_ustr, uno::Reference< uno::XInterface >(), 2 );
78 uno::Any
79 ScVbaInterior::getColor()
81 return uno::Any( OORGBToXLRGB( GetBackColor() ) );
84 void
85 ScVbaInterior::setColor( const uno::Any& _color )
87 sal_Int32 nColor = 0;
88 if( _color >>= nColor )
90 SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( XLRGBToOORGB( nColor ) ) );
91 SetMixedColor();
95 void
96 ScVbaInterior::SetMixedColor()
98 // pattern
99 uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
100 if( aPattern.hasValue() )
102 m_nPattern = GetAttributeData( aPattern );
104 sal_Int32 nPattern = 0;
105 auto it = aPatternMap.find( m_nPattern );
106 if (it != aPatternMap.end())
107 nPattern = it->second;
108 // pattern color
109 uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
110 if( aPatternColor.hasValue() )
112 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
113 m_aPattColor = Color(ColorTransparency, nPatternColor);
115 Color nPatternColor = m_aPattColor;
116 // back color
117 Color aBackColor( GetBackColor() );
118 // set mixed color
119 Color aMixedColor;
120 if( nPattern > 0 )
121 aMixedColor = GetPatternColor( nPatternColor, aBackColor, static_cast<sal_uInt32>(nPattern) );
122 else
123 aMixedColor = GetPatternColor( aBackColor, aBackColor, static_cast<sal_uInt32>(nPattern) );
124 Color nMixedColor = aMixedColor.GetRGBColor();
125 m_xProps->setPropertyValue( BACKCOLOR , uno::Any( nMixedColor ) );
128 uno::Reference< container::XIndexAccess >
129 ScVbaInterior::getPalette() const
131 if ( !m_pScDoc )
132 throw uno::RuntimeException();
133 ScDocShell* pShell = m_pScDoc->GetDocumentShell();
134 ScVbaPalette aPalette( pShell );
135 return aPalette.getPalette();
138 void SAL_CALL
139 ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex )
141 sal_Int32 nIndex = 0;
142 _colorindex >>= nIndex;
144 // hackly for excel::XlColorIndex::xlColorIndexNone
145 if( nIndex == excel::XlColorIndex::xlColorIndexNone )
147 m_xProps->setPropertyValue( BACKCOLOR, uno::Any( sal_Int32( -1 ) ) );
149 else
151 // setColor expects colors in XL RGB values
152 // #FIXME this is daft we convert OO RGB val to XL RGB val and
153 // then back again to OO RGB value
154 setColor( OORGBToXLRGB( GetIndexColor( nIndex ) ) );
157 uno::Any
158 ScVbaInterior::GetIndexColor( sal_Int32 nColorIndex )
160 sal_Int32 nIndex = nColorIndex;
161 // #FIXME xlColorIndexAutomatic & xlColorIndexNone are not really
162 // handled properly here
163 if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) || ( nIndex == excel::XlColorIndex::xlColorIndexNone ) )
164 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 )
165 --nIndex; // OOo indices are zero bases
166 uno::Reference< container::XIndexAccess > xIndex = getPalette();
167 return xIndex->getByIndex( nIndex );
170 sal_Int32
171 ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
173 uno::Reference< container::XIndexAccess > xIndex = getPalette();
174 sal_Int32 nElems = xIndex->getCount();
175 sal_Int32 nIndex = -1;
176 for ( sal_Int32 count=0; count<nElems; ++count )
178 sal_Int32 nPaletteColor = 0;
179 xIndex->getByIndex( count ) >>= nPaletteColor;
180 if ( nPaletteColor == nColor )
182 nIndex = count + 1; // 1 based
183 break;
186 return nIndex;
189 uno::Any SAL_CALL
190 ScVbaInterior::getColorIndex()
192 sal_Int32 nColor = 0;
193 // hackly for excel::XlColorIndex::xlColorIndexNone
194 uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
195 if( ( aColor >>= nColor ) && ( nColor == -1 ) )
197 nColor = excel::XlColorIndex::xlColorIndexNone;
198 return uno::Any( nColor );
201 // getColor returns Xl ColorValue, need to convert it to OO val
202 // as the palette deals with OO RGB values
203 // #FIXME this is daft in getColor we convert OO RGB val to XL RGB val
204 // and then back again to OO RGB value
205 XLRGBToOORGB( getColor() ) >>= nColor;
207 return uno::Any( GetColorIndex( nColor ) );
209 Color
210 ScVbaInterior::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt32 nXclPattern )
212 // 0x00 == 0% transparence (full rPattColor)
213 // 0x80 == 100% transparence (full rBackColor)
214 static const sal_uInt8 pnRatioTable[] =
216 0x80, 0x00, 0x40, 0x20, 0x60, 0x40, 0x40, 0x40, // 00 - 07
217 0x40, 0x40, 0x20, 0x60, 0x60, 0x60, 0x60, 0x48, // 08 - 15
218 0x50, 0x70, 0x78 // 16 - 18
220 return ( nXclPattern < SAL_N_ELEMENTS( pnRatioTable ) ) ?
221 GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
223 Color
224 ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
226 return Color(
227 ColorTransparency, nTrans,
228 GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
229 GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
230 GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
232 sal_uInt8
233 ScVbaInterior::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
235 sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
236 return static_cast< sal_uInt8 >( nTemp );
238 uno::Reference< container::XNameContainer >
239 ScVbaInterior::GetAttributeContainer()
241 return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue(u"UserDefinedAttributes"_ustr), uno::UNO_QUERY_THROW );
243 sal_Int32
244 ScVbaInterior::GetAttributeData( uno::Any const & aValue )
246 xml::AttributeData aDataValue;
247 if( aValue >>= aDataValue )
249 return aDataValue.Value.toInt32();
251 return 0;
253 uno::Any
254 ScVbaInterior::SetAttributeData( sal_Int32 nValue )
256 xml::AttributeData aAttributeData;
257 aAttributeData.Type = "sal_Int32";
258 aAttributeData.Value = OUString::number( nValue );
259 return uno::Any( aAttributeData );
261 uno::Any
262 ScVbaInterior::GetUserDefinedAttributes( const OUString& sName )
264 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_SET_THROW );
265 if( xNameContainer->hasByName( sName ) )
267 return xNameContainer->getByName( sName );
269 return uno::Any();
271 void
272 ScVbaInterior::SetUserDefinedAttributes( const OUString& sName, const uno::Any& aValue )
274 if( aValue.hasValue() )
276 uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_SET_THROW );
277 if( xNameContainer->hasByName( sName ) )
278 xNameContainer->removeByName( sName );
279 xNameContainer->insertByName( sName, aValue );
280 m_xProps->setPropertyValue(u"UserDefinedAttributes"_ustr, uno::Any( xNameContainer ) );
283 // OOo do not support below API
284 uno::Any SAL_CALL
285 ScVbaInterior::getPattern()
287 // XlPattern
288 uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
289 if( aPattern.hasValue() )
290 return uno::Any( GetAttributeData( aPattern ) );
291 return uno::Any( excel::XlPattern::xlPatternNone );
293 void SAL_CALL
294 ScVbaInterior::setPattern( const uno::Any& _pattern )
296 if( !(_pattern >>= m_nPattern) )
297 throw uno::RuntimeException(u"Invalid Pattern index"_ustr );
299 SetUserDefinedAttributes( PATTERN, SetAttributeData( m_nPattern ) );
300 SetMixedColor();
303 Color
304 ScVbaInterior::GetBackColor()
306 sal_Int32 nColor = 0;
307 Color aBackColor;
308 uno::Any aColor = GetUserDefinedAttributes( BACKCOLOR );
309 if( aColor.hasValue() )
311 nColor = GetAttributeData( aColor );
312 aBackColor = Color(ColorTransparency, nColor);
314 else
316 uno::Any aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
317 if( aAny >>= nColor )
319 nColor = XLRGBToOORGB( nColor );
320 aBackColor = Color(ColorTransparency, nColor);
321 SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( nColor ) );
324 return aBackColor;
326 uno::Any SAL_CALL
327 ScVbaInterior::getPatternColor()
329 // 0 is the default color. no filled.
330 uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
331 if( aPatternColor.hasValue() )
333 sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
334 return uno::Any( OORGBToXLRGB( Color(ColorTransparency, nPatternColor) ) );
336 return uno::Any( sal_Int32( 0 ) );
338 void SAL_CALL
339 ScVbaInterior::setPatternColor( const uno::Any& _patterncolor )
341 sal_Int32 nPattColor = 0;
342 if( !(_patterncolor >>= nPattColor) )
343 throw uno::RuntimeException(u"Invalid Pattern Color"_ustr );
345 SetUserDefinedAttributes( PATTERNCOLOR, SetAttributeData( XLRGBToOORGB( nPattColor ) ) );
346 SetMixedColor();
349 uno::Any SAL_CALL
350 ScVbaInterior::getPatternColorIndex()
352 sal_Int32 nColor = 0;
353 XLRGBToOORGB( getPatternColor() ) >>= nColor;
355 return uno::Any( GetColorIndex( nColor ) );
357 void SAL_CALL
358 ScVbaInterior::setPatternColorIndex( const uno::Any& _patterncolorindex )
360 sal_Int32 nColorIndex = 0;
361 if( !(_patterncolorindex >>= nColorIndex) )
362 throw uno::RuntimeException(u"Invalid Pattern Color"_ustr );
364 if( nColorIndex == 0 )
365 return;
366 Color nPattColor;
367 GetIndexColor( nColorIndex ) >>= nPattColor;
368 setPatternColor( uno::Any( OORGBToXLRGB( nPattColor ) ) );
372 uno::Any SAL_CALL ScVbaInterior::getThemeColor()
374 // Just a stub for now.
375 return uno::Any(static_cast<sal_Int32>(0));
378 void SAL_CALL ScVbaInterior::setThemeColor(const uno::Any& /*rAny*/)
380 // Just a stub for now.
383 uno::Any SAL_CALL ScVbaInterior::getTintAndShade()
385 // Just a stub for now.
386 return uno::Any(static_cast<double>(0));
389 void SAL_CALL ScVbaInterior::setTintAndShade(const uno::Any& /*rAny*/)
391 // Just a stub for now.
394 uno::Any SAL_CALL ScVbaInterior::getPatternTintAndShade()
396 // Just a stub for now.
397 return uno::Any(static_cast<double>(0));
400 void SAL_CALL ScVbaInterior::setPatternTintAndShade(const uno::Any& /*rAny*/)
402 // Just a stub for now.
405 OUString
406 ScVbaInterior::getServiceImplName()
408 return u"ScVbaInterior"_ustr;
411 uno::Sequence< OUString >
412 ScVbaInterior::getServiceNames()
414 static uno::Sequence< OUString > const aServiceNames
416 u"ooo.vba.excel.Interior"_ustr
418 return aServiceNames;
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */