2010-10-12 15:59:00 +02:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2007-04-25 15:07:44 +00:00
/*************************************************************************
*
2008-04-11 00:03:23 +00:00
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER .
2007-04-25 15:07:44 +00:00
*
2010-02-12 15:01:35 +01:00
* Copyright 2000 , 2010 Oracle and / or its affiliates .
2007-04-25 15:07:44 +00:00
*
2008-04-11 00:03:23 +00:00
* OpenOffice . org - a multi - platform office productivity suite
2007-04-25 15:07:44 +00:00
*
2008-04-11 00:03:23 +00:00
* This file is part of OpenOffice . org .
2007-04-25 15:07:44 +00:00
*
2008-04-11 00:03:23 +00:00
* OpenOffice . org is free software : you can redistribute it and / or modify
* it under the terms of the GNU Lesser General Public License version 3
* only , as published by the Free Software Foundation .
2007-04-25 15:07:44 +00:00
*
2008-04-11 00:03:23 +00:00
* OpenOffice . org is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Lesser General Public License version 3 for more details
* ( a copy is included in the LICENSE file that accompanied this code ) .
2007-04-25 15:07:44 +00:00
*
2008-04-11 00:03:23 +00:00
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice . org . If not , see
* < http : //www.openoffice.org/license.html>
* for a copy of the LGPLv3 License .
2007-04-25 15:07:44 +00:00
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# 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>
2009-02-13 13:03:24 +00:00
# include <com/sun/star/beans/NamedValue.hpp>
# include <com/sun/star/xml/AttributeData.hpp>
2007-04-25 15:07:44 +00:00
2009-02-13 13:03:24 +00:00
# include <ooo/vba/excel/XlColorIndex.hpp>
# include <ooo/vba/excel/XlPattern.hpp>
2007-04-25 15:07:44 +00:00
# include <comphelper/processfactory.hxx>
# include <cppuhelper/queryinterface.hxx>
2010-06-15 20:02:53 +02:00
# include <map>
2010-12-03 13:59:12 +00:00
# include <sal/macros.h>
2007-04-25 15:07:44 +00:00
# include <svx/xtable.hxx>
# include "vbainterior.hxx"
# include "vbapalette.hxx"
2010-06-15 20:02:53 +02:00
# include "document.hxx"
2009-02-13 13:03:24 +00:00
# define COLORMAST 0xFFFFFF
const sal_uInt16 EXC_COLOR_WINDOWBACK = 65 ;
typedef std : : map < sal_Int32 , sal_Int32 > PatternMap ;
typedef std : : pair < sal_Int32 , sal_Int32 > PatternPair ;
2007-04-25 15:07:44 +00:00
using namespace : : com : : sun : : star ;
2009-02-13 13:03:24 +00:00
using namespace : : ooo : : vba ;
using namespace : : ooo : : vba : : excel : : XlPattern ;
2007-04-25 15:07:44 +00:00
static const rtl : : OUString BACKCOLOR ( RTL_CONSTASCII_USTRINGPARAM ( " CellBackColor " ) ) ;
2009-02-13 13:03:24 +00:00
static const rtl : : OUString PATTERN ( RTL_CONSTASCII_USTRINGPARAM ( " Pattern " ) ) ;
static const rtl : : OUString PATTERNCOLOR ( RTL_CONSTASCII_USTRINGPARAM ( " PatternColor " ) ) ;
2007-04-25 15:07:44 +00:00
2009-02-13 13:03:24 +00:00
PatternMap lcl_getPatternMap ( )
2007-04-25 15:07:44 +00:00
{
2009-02-13 13:03:24 +00:00
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 ) throw ( lang : : IllegalArgumentException ) : ScVbaInterior_BASE ( xParent , xContext ) , m_xProps ( xProps ) , m_pScDoc ( pScDoc )
{
// auto color
m_aPattColor . SetColor ( ( sal_uInt32 ) 0x0 ) ;
m_nPattern = 0L ;
2007-04-25 15:07:44 +00:00
if ( ! m_xProps . is ( ) )
throw lang : : IllegalArgumentException ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " properties " ) ) , uno : : Reference < uno : : XInterface > ( ) , 2 ) ;
}
uno : : Any
ScVbaInterior : : getColor ( ) throw ( uno : : RuntimeException )
{
2009-02-13 13:03:24 +00:00
Color aBackColor ( GetBackColor ( ) ) ;
return uno : : makeAny ( OORGBToXLRGB ( aBackColor . GetColor ( ) ) ) ;
2007-04-25 15:07:44 +00:00
}
void
ScVbaInterior : : setColor ( const uno : : Any & _color ) throw ( uno : : RuntimeException )
{
2009-02-13 13:03:24 +00:00
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 ) ) ;
2007-04-25 15:07:44 +00:00
}
uno : : Reference < container : : XIndexAccess >
ScVbaInterior : : getPalette ( )
{
2007-12-07 09:54:12 +00:00
if ( ! m_pScDoc )
throw uno : : RuntimeException ( ) ;
2007-04-25 15:07:44 +00:00
SfxObjectShell * pShell = m_pScDoc - > GetDocumentShell ( ) ;
ScVbaPalette aPalette ( pShell ) ;
return aPalette . getPalette ( ) ;
}
void SAL_CALL
ScVbaInterior : : setColorIndex ( const css : : uno : : Any & _colorindex ) throw ( css : : uno : : RuntimeException )
{
sal_Int32 nIndex = 0 ;
_colorindex > > = nIndex ;
2009-02-13 13:03:24 +00:00
2009-09-18 15:24:22 +00:00
// 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 ) ) ) ;
}
2009-02-13 13:03:24 +00:00
}
uno : : Any
ScVbaInterior : : GetIndexColor ( const sal_Int32 & nColorIndex )
{
sal_Int32 nIndex = nColorIndex ;
2007-04-25 15:07:44 +00:00
// #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 ( ) ;
2009-02-13 13:03:24 +00:00
return xIndex - > getByIndex ( nIndex ) ;
2007-04-25 15:07:44 +00:00
}
2009-02-13 13:03:24 +00:00
sal_Int32
ScVbaInterior : : GetColorIndex ( const sal_Int32 nColor )
2007-04-25 15:07:44 +00:00
{
uno : : Reference < container : : XIndexAccess > xIndex = getPalette ( ) ;
sal_Int32 nElems = xIndex - > getCount ( ) ;
sal_Int32 nIndex = - 1 ;
for ( sal_Int32 count = 0 ; count < nElems ; + + count )
{
2007-08-30 09:04:53 +00:00
sal_Int32 nPaletteColor = 0 ;
2007-04-25 15:07:44 +00:00
xIndex - > getByIndex ( count ) > > = nPaletteColor ;
if ( nPaletteColor = = nColor )
{
nIndex = count + 1 ; // 1 based
break ;
}
}
2009-02-13 13:03:24 +00:00
return nIndex ;
}
uno : : Any SAL_CALL
ScVbaInterior : : getColorIndex ( ) throw ( css : : uno : : RuntimeException )
{
sal_Int32 nColor = 0 ;
2009-09-18 15:24:22 +00:00
// 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 ) ;
}
2009-02-13 13:03:24 +00:00
// 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 ;
2009-09-18 15:24:22 +00:00
return uno : : makeAny ( GetColorIndex ( nColor ) ) ;
2009-02-13 13:03:24 +00:00
}
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
} ;
2010-12-03 13:59:12 +00:00
return ( nXclPattern < SAL_N_ELEMENTS ( pnRatioTable ) ) ?
2009-02-13 13:03:24 +00:00
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
2010-12-20 19:22:50 -05:00
ScVbaInterior : : GetMixedColorComp ( sal_uInt8 nFore , sal_uInt8 nBack , sal_uInt8 nTrans ) const
2009-02-13 13:03:24 +00:00
{
sal_uInt32 nTemp = ( ( static_cast < sal_Int32 > ( nBack ) - nFore ) * nTrans ) / 0x80 + nFore ;
return static_cast < sal_uInt8 > ( nTemp ) ;
}
uno : : Reference < container : : XNameContainer >
ScVbaInterior : : GetAttributeContainer ( )
{
2010-11-02 20:52:20 +00:00
return uno : : Reference < container : : XNameContainer > ( m_xProps - > getPropertyValue ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " UserDefinedAttributes " ) ) ) , uno : : UNO_QUERY_THROW ) ;
2009-02-13 13:03:24 +00:00
}
sal_Int32
ScVbaInterior : : GetAttributeData ( uno : : Any 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 ;
2010-11-02 20:52:20 +00:00
aAttributeData . Type = rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " sal_Int32 " ) ) ;
2009-02-13 13:03:24 +00:00
aAttributeData . Value = rtl : : OUString : : valueOf ( nValue ) ;
return uno : : makeAny ( aAttributeData ) ;
}
uno : : Any
ScVbaInterior : : GetUserDefinedAttributes ( const rtl : : 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 rtl : : 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 ) ;
2010-11-02 20:52:20 +00:00
m_xProps - > setPropertyValue ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " UserDefinedAttributes " ) ) , uno : : makeAny ( xNameContainer ) ) ;
2009-02-13 13:03:24 +00:00
}
}
// OOo do not support below API
uno : : Any SAL_CALL
ScVbaInterior : : getPattern ( ) throw ( uno : : RuntimeException )
{
// 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 ) throw ( uno : : RuntimeException )
{
if ( _pattern > > = m_nPattern )
{
SetUserDefinedAttributes ( PATTERN , SetAttributeData ( m_nPattern ) ) ;
SetMixedColor ( ) ;
}
else
2010-11-02 20:52:20 +00:00
throw uno : : RuntimeException ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " Invalid Pattern index " ) ) , uno : : Reference < uno : : XInterface > ( ) ) ;
2009-02-13 13:03:24 +00:00
}
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 ( ) throw ( uno : : RuntimeException )
{
// 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 ) throw ( uno : : RuntimeException )
{
sal_Int32 nPattColor = 0 ;
if ( _patterncolor > > = nPattColor )
{
SetUserDefinedAttributes ( PATTERNCOLOR , SetAttributeData ( XLRGBToOORGB ( nPattColor ) ) ) ;
SetMixedColor ( ) ;
}
else
2010-11-02 20:52:20 +00:00
throw uno : : RuntimeException ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " Invalid Pattern Color " ) ) , uno : : Reference < uno : : XInterface > ( ) ) ;
2009-02-13 13:03:24 +00:00
}
uno : : Any SAL_CALL
ScVbaInterior : : getPatternColorIndex ( ) throw ( uno : : RuntimeException )
{
sal_Int32 nColor = 0 ;
XLRGBToOORGB ( getPatternColor ( ) ) > > = nColor ;
return uno : : makeAny ( GetIndexColor ( nColor ) ) ;
}
void SAL_CALL
ScVbaInterior : : setPatternColorIndex ( const uno : : Any & _patterncolorindex ) throw ( uno : : RuntimeException )
{
sal_Int32 nColorIndex = 0 ;
if ( _patterncolorindex > > = nColorIndex )
{
if ( nColorIndex = = 0 )
return ;
sal_Int32 nPattColor = 0 ;
GetIndexColor ( nColorIndex ) > > = nPattColor ;
setPatternColor ( uno : : makeAny ( OORGBToXLRGB ( nPattColor ) ) ) ;
}
else
2010-11-02 20:52:20 +00:00
throw uno : : RuntimeException ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " Invalid Pattern Color " ) ) , uno : : Reference < uno : : XInterface > ( ) ) ;
2007-04-25 15:07:44 +00:00
}
2007-12-07 09:54:12 +00:00
rtl : : OUString &
ScVbaInterior : : getServiceImplName ( )
{
static rtl : : OUString sImplName ( RTL_CONSTASCII_USTRINGPARAM ( " ScVbaInterior " ) ) ;
return sImplName ;
}
uno : : Sequence < rtl : : OUString >
ScVbaInterior : : getServiceNames ( )
{
static uno : : Sequence < rtl : : OUString > aServiceNames ;
if ( aServiceNames . getLength ( ) = = 0 )
{
aServiceNames . realloc ( 1 ) ;
2009-02-13 13:03:24 +00:00
aServiceNames [ 0 ] = rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " ooo.vba.excel.Interior " ) ) ;
2007-12-07 09:54:12 +00:00
}
return aServiceNames ;
}
2007-04-25 15:07:44 +00:00
2010-10-12 15:59:00 +02:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */