summaryrefslogtreecommitdiff
path: root/sc/source/ui/vba/vbainterior.cxx
blob: 91bfe2fae30e82fa3e5586275aa5212845ccb0f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/table/XCell.hpp>
#include <com/sun/star/table/XColumnRowRange.hpp>
#include <com/sun/star/beans/XIntrospection.hpp>
#include <com/sun/star/beans/XIntrospectionAccess.hpp>
#include <com/sun/star/reflection/XIdlMethod.hpp>
#include <com/sun/star/beans/MethodConcept.hpp>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/xml/AttributeData.hpp>

#include <ooo/vba/excel/XlColorIndex.hpp>
#include <ooo/vba/excel/XlPattern.hpp>

#include <comphelper/processfactory.hxx>
#include <cppuhelper/queryinterface.hxx>

#include <map>

#include <sal/macros.h>
#include <svx/xtable.hxx>

#include "vbainterior.hxx"
#include "vbapalette.hxx"
#include <document.hxx>

#define COLORMAST 0xFFFFFF
typedef std::map< sal_Int32, sal_Int32 >  PatternMap;
typedef std::pair< sal_Int32, sal_Int32 > PatternPair;
using namespace ::com::sun::star;
using namespace ::ooo::vba;
using namespace ::ooo::vba::excel::XlPattern;
static const char BACKCOLOR[] = "CellBackColor";
static const char PATTERN[] = "Pattern";
static const char PATTERNCOLOR[] = "PatternColor";

static PatternMap lcl_getPatternMap()
{
    PatternMap aPatternMap;
    aPatternMap.insert( PatternPair( xlPatternAutomatic, 0 ) );
    aPatternMap.insert( PatternPair( xlPatternChecker, 9 ) );
    aPatternMap.insert( PatternPair( xlPatternCrissCross, 16 ) );
    aPatternMap.insert( PatternPair( xlPatternDown, 7 ) );
    aPatternMap.insert( PatternPair( xlPatternGray16, 17 ) );
    aPatternMap.insert( PatternPair( xlPatternGray25, 4 ) );
    aPatternMap.insert( PatternPair( xlPatternGray50, 2 ) );
    aPatternMap.insert( PatternPair( xlPatternGray75, 3 ) );
    aPatternMap.insert( PatternPair( xlPatternGray8, 18 ) );
    aPatternMap.insert( PatternPair( xlPatternGrid, 15 ) );
    aPatternMap.insert( PatternPair( xlPatternHorizontal, 5 ) );
    aPatternMap.insert( PatternPair( xlPatternLightDown, 13 ) );
    aPatternMap.insert( PatternPair( xlPatternLightHorizontal, 11 ) );
    aPatternMap.insert( PatternPair( xlPatternLightUp, 14 ) );
    aPatternMap.insert( PatternPair( xlPatternLightVertical, 12 ) );
    aPatternMap.insert( PatternPair( xlPatternNone, 0 ) );
    aPatternMap.insert( PatternPair( xlPatternSemiGray75, 10 ) );
    aPatternMap.insert( PatternPair( xlPatternSolid, 0 ) );
    aPatternMap.insert( PatternPair( xlPatternUp, 8 ) );
    aPatternMap.insert( PatternPair( xlPatternVertical, 6 ) );
    return aPatternMap;
}

static PatternMap aPatternMap( lcl_getPatternMap() );

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 )
{
    // auto color
    m_aPattColor.SetColor( (sal_uInt32)0x0 );
    m_nPattern = 0;
    if ( !m_xProps.is() )
        throw lang::IllegalArgumentException("properties", uno::Reference< uno::XInterface >(), 2 );
}

uno::Any
ScVbaInterior::getColor()
{
    Color aBackColor( GetBackColor() );
    return uno::makeAny( OORGBToXLRGB( aBackColor.GetColor() ) );
}

void
ScVbaInterior::setColor( const uno::Any& _color  )
{
    sal_Int32 nColor = 0;
    if( _color >>= nColor )
    {
        SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( XLRGBToOORGB( nColor ) ) );
        SetMixedColor();
    }
}

void
ScVbaInterior::SetMixedColor()
{
    // pattern
    uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
    if( aPattern.hasValue() )
    {
        m_nPattern = GetAttributeData( aPattern );
    }
    sal_Int32 nPattern = aPatternMap[ m_nPattern ];
    // pattern color
    uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
    if( aPatternColor.hasValue() )
    {
        sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
        m_aPattColor.SetColor( nPatternColor );
    }
    sal_Int32 nPatternColor = m_aPattColor.GetColor();
    // back color
    Color aBackColor( GetBackColor() );
    // set mixed color
    Color aMixedColor;
    if( nPattern > 0 )
        aMixedColor = GetPatternColor( Color(nPatternColor), aBackColor, (sal_uInt32)nPattern );
    else
        aMixedColor = GetPatternColor( aBackColor, aBackColor, (sal_uInt32)nPattern );
    sal_Int32 nMixedColor = aMixedColor.GetColor() & COLORMAST;
    m_xProps->setPropertyValue( BACKCOLOR , uno::makeAny( nMixedColor ) );
}

uno::Reference< container::XIndexAccess >
ScVbaInterior::getPalette()
{
    if ( !m_pScDoc )
        throw uno::RuntimeException();
    SfxObjectShell* pShell = m_pScDoc->GetDocumentShell();
    ScVbaPalette aPalette( pShell );
    return aPalette.getPalette();
}

void SAL_CALL
ScVbaInterior::setColorIndex( const css::uno::Any& _colorindex )
{
    sal_Int32 nIndex = 0;
    _colorindex >>= nIndex;

    // hackly for excel::XlColorIndex::xlColorIndexNone
    if( nIndex == excel::XlColorIndex::xlColorIndexNone )
    {
        m_xProps->setPropertyValue( BACKCOLOR, uno::makeAny( sal_Int32( -1 ) ) );
    }
    else
    {
        // setColor expects colors in XL RGB values
        // #FIXME this is daft we convert OO RGB val to XL RGB val and
        // then back again to OO RGB value
        setColor( OORGBToXLRGB( GetIndexColor( nIndex ) ) );
    }
}
uno::Any
ScVbaInterior::GetIndexColor( sal_Int32 nColorIndex )
{
    sal_Int32 nIndex = nColorIndex;
    // #FIXME  xlColorIndexAutomatic & xlColorIndexNone are not really
    // handled properly here
    if ( !nIndex || ( nIndex == excel::XlColorIndex::xlColorIndexAutomatic ) || ( nIndex == excel::XlColorIndex::xlColorIndexNone )  )
        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 )
    --nIndex; // OOo indices are zero bases
    uno::Reference< container::XIndexAccess > xIndex = getPalette();
    return xIndex->getByIndex( nIndex );
}

sal_Int32
ScVbaInterior::GetColorIndex( const sal_Int32 nColor )
{
    uno::Reference< container::XIndexAccess > xIndex = getPalette();
    sal_Int32 nElems = xIndex->getCount();
    sal_Int32 nIndex = -1;
    for ( sal_Int32 count=0; count<nElems; ++count )
           {
        sal_Int32 nPaletteColor = 0;
        xIndex->getByIndex( count ) >>= nPaletteColor;
        if ( nPaletteColor == nColor )
        {
            nIndex = count + 1; // 1 based
            break;
        }
    }
    return nIndex;
}

uno::Any SAL_CALL
ScVbaInterior::getColorIndex()
{
    sal_Int32 nColor = 0;
    // hackly for excel::XlColorIndex::xlColorIndexNone
    uno::Any aColor = m_xProps->getPropertyValue( BACKCOLOR );
    if( ( aColor >>= nColor ) && ( nColor == -1 ) )
    {
        nColor = excel::XlColorIndex::xlColorIndexNone;
        return uno::makeAny( nColor );
    }

    // getColor returns Xl ColorValue, need to convert it to OO val
    // as the palette deals with OO RGB values
    // #FIXME this is daft in getColor we convert OO RGB val to XL RGB val
    // and then back again to OO RGB value
    XLRGBToOORGB( getColor() ) >>= nColor;

    return uno::makeAny( GetColorIndex( nColor ) );
}
Color
ScVbaInterior::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt32 nXclPattern )
{
    // 0x00 == 0% transparence (full rPattColor)
    // 0x80 == 100% transparence (full rBackColor)
    static const sal_uInt8 pnRatioTable[] =
    {
        0x80, 0x00, 0x40, 0x20, 0x60, 0x40, 0x40, 0x40,     // 00 - 07
        0x40, 0x40, 0x20, 0x60, 0x60, 0x60, 0x60, 0x48,     // 08 - 15
        0x50, 0x70, 0x78                                    // 16 - 18
    };
    return ( nXclPattern < SAL_N_ELEMENTS( pnRatioTable ) ) ?
        GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
}
Color
ScVbaInterior::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans )
{
    return Color(
        nTrans,
        GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ),
        GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ),
        GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ));
}
sal_uInt8
ScVbaInterior::GetMixedColorComp(  sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans )
{
    sal_uInt32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore;
    return static_cast< sal_uInt8 >( nTemp );
}
uno::Reference< container::XNameContainer >
ScVbaInterior::GetAttributeContainer()
{
    return uno::Reference < container::XNameContainer > ( m_xProps->getPropertyValue("UserDefinedAttributes"), uno::UNO_QUERY_THROW );
}
sal_Int32
ScVbaInterior::GetAttributeData( uno::Any const & aValue )
{
    xml::AttributeData aDataValue;
    if( aValue >>= aDataValue )
    {
        return aDataValue.Value.toInt32();
    }
    return sal_Int32( 0 );
}
uno::Any
ScVbaInterior::SetAttributeData( sal_Int32 nValue )
{
    xml::AttributeData aAttributeData;
    aAttributeData.Type = "sal_Int32";
    aAttributeData.Value = OUString::number( nValue );
    return uno::makeAny( aAttributeData );
}
uno::Any
ScVbaInterior::GetUserDefinedAttributes( const OUString& sName )
{
    uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
    if( xNameContainer->hasByName( sName ) )
    {
        return xNameContainer->getByName( sName );
    }
    return uno::Any();
}
void
ScVbaInterior::SetUserDefinedAttributes( const OUString& sName, const uno::Any& aValue )
{
    if( aValue.hasValue() )
    {
        uno::Reference< container::XNameContainer > xNameContainer( GetAttributeContainer(), uno::UNO_QUERY_THROW );
        if( xNameContainer->hasByName( sName ) )
            xNameContainer->removeByName( sName );
        xNameContainer->insertByName( sName, aValue );
        m_xProps->setPropertyValue("UserDefinedAttributes", uno::makeAny( xNameContainer ) );
    }
}
// OOo do not support below API
uno::Any SAL_CALL
ScVbaInterior::getPattern()
{
    // XlPattern
    uno::Any aPattern = GetUserDefinedAttributes( PATTERN );
    if( aPattern.hasValue() )
        return uno::makeAny( GetAttributeData( aPattern ) );
    return uno::makeAny( excel::XlPattern::xlPatternNone );
}
void SAL_CALL
ScVbaInterior::setPattern( const uno::Any& _pattern )
{
    if( !(_pattern >>= m_nPattern) )
        throw uno::RuntimeException("Invalid Pattern index" );

    SetUserDefinedAttributes( PATTERN, SetAttributeData( m_nPattern ) );
    SetMixedColor();

}
Color
ScVbaInterior::GetBackColor()
{
    sal_Int32 nColor = 0;
    Color aBackColor;
    uno::Any aColor = GetUserDefinedAttributes( BACKCOLOR );
    if( aColor.hasValue() )
    {
        nColor = GetAttributeData( aColor );
        aBackColor.SetColor( nColor );
    }
    else
    {
        uno::Any aAny;
        aAny = OORGBToXLRGB( m_xProps->getPropertyValue( BACKCOLOR ) );
        if( aAny >>= nColor )
        {
            nColor = XLRGBToOORGB( nColor );
            aBackColor.SetColor( nColor );
            SetUserDefinedAttributes( BACKCOLOR, SetAttributeData( nColor ) );
        }
    }
    return aBackColor;
}
uno::Any SAL_CALL
ScVbaInterior::getPatternColor()
{
    // 0 is the default color. no filled.
    uno::Any aPatternColor = GetUserDefinedAttributes( PATTERNCOLOR );
    if( aPatternColor.hasValue() )
    {
        sal_uInt32 nPatternColor = GetAttributeData( aPatternColor );
        return uno::makeAny( OORGBToXLRGB( nPatternColor ) );
    }
    return uno::makeAny( sal_Int32( 0 ) );
}
void SAL_CALL
ScVbaInterior::setPatternColor( const uno::Any& _patterncolor )
{
    sal_Int32 nPattColor = 0;
    if( !(_patterncolor >>= nPattColor) )
        throw uno::RuntimeException("Invalid Pattern Color" );

    SetUserDefinedAttributes( PATTERNCOLOR, SetAttributeData( XLRGBToOORGB( nPattColor ) ) );
    SetMixedColor();

}
uno::Any SAL_CALL
ScVbaInterior::getPatternColorIndex()
{
    sal_Int32 nColor = 0;
    XLRGBToOORGB( getPatternColor() ) >>= nColor;

    return uno::makeAny( GetColorIndex( nColor ) );
}
void SAL_CALL
ScVbaInterior::setPatternColorIndex( const uno::Any& _patterncolorindex )
{
    sal_Int32 nColorIndex = 0;
    if( !(_patterncolorindex >>= nColorIndex) )
        throw uno::RuntimeException("Invalid Pattern Color" );

    if( nColorIndex == 0 )
        return;
    sal_Int32 nPattColor = 0;
    GetIndexColor( nColorIndex ) >>= nPattColor;
    setPatternColor( uno::makeAny( OORGBToXLRGB( nPattColor ) ) );

}

uno::Any SAL_CALL ScVbaInterior::getThemeColor()
{
    // Just a stub for now.
    return uno::makeAny(static_cast<sal_Int32>(0));
}

void SAL_CALL ScVbaInterior::setThemeColor(const uno::Any& /*rAny*/)
{
    // Just a stub for now.
}

uno::Any SAL_CALL ScVbaInterior::getTintAndShade()
{
    // Just a stub for now.
    return uno::makeAny(static_cast<double>(0));
}

void SAL_CALL ScVbaInterior::setTintAndShade(const uno::Any& /*rAny*/)
{
    // Just a stub for now.
}

uno::Any SAL_CALL ScVbaInterior::getPatternTintAndShade()
{
    // Just a stub for now.
    return uno::makeAny(static_cast<double>(0));
}

void SAL_CALL ScVbaInterior::setPatternTintAndShade(const uno::Any& /*rAny*/)
{
    // Just a stub for now.
}

OUString
ScVbaInterior::getServiceImplName()
{
    return OUString("ScVbaInterior");
}

uno::Sequence< OUString >
ScVbaInterior::getServiceNames()
{
    static uno::Sequence< OUString > aServiceNames;
    if ( aServiceNames.getLength() == 0 )
    {
        aServiceNames.realloc( 1 );
        aServiceNames[ 0 ] = "ooo.vba.excel.Interior";
    }
    return aServiceNames;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */