LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/ui/vba - vbainterior.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 138 194 71.1 %
Date: 2013-07-09 Functions: 21 29 72.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10