1*e6ed5fbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e6ed5fbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e6ed5fbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e6ed5fbcSAndrew Rist  * distributed with this work for additional information
6*e6ed5fbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e6ed5fbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e6ed5fbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*e6ed5fbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e6ed5fbcSAndrew Rist  *
11*e6ed5fbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e6ed5fbcSAndrew Rist  *
13*e6ed5fbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e6ed5fbcSAndrew Rist  * software distributed under the License is distributed on an
15*e6ed5fbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e6ed5fbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*e6ed5fbcSAndrew Rist  * specific language governing permissions and limitations
18*e6ed5fbcSAndrew Rist  * under the License.
19*e6ed5fbcSAndrew Rist  *
20*e6ed5fbcSAndrew Rist  *************************************************************/
21*e6ed5fbcSAndrew Rist 
22*e6ed5fbcSAndrew Rist 
23cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
24cdf0e10cSrcweir #include <ooo/vba/msforms/XLineFormat.hpp>
25cdf0e10cSrcweir #include "vbacolorformat.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir using namespace ooo::vba;
28cdf0e10cSrcweir using namespace com::sun::star;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir sal_Int32
getColorIndex(sal_Int32 nIndex)31cdf0e10cSrcweir MsoColorIndizes::getColorIndex( sal_Int32 nIndex )
32cdf0e10cSrcweir {
33cdf0e10cSrcweir     const static sal_Int32 COLORINDIZES[56] =
34cdf0e10cSrcweir     {   HAPICOLOR_BLACK, HAPICOLOR_WITHE, HAPICOLOR_RED, HAPICOLOR_BRIGHTGREEN, HAPICOLOR_BLUE, HAPICOLOR_YELLOW, HAPICOLOR_PINK,
35cdf0e10cSrcweir         HAPICOLOR_TURQUOISE, HAPICOLOR_DARKRED, HAPICOLOR_GREEN, HAPICOLOR_DARKBLUE, HAPICOLOR_DARKYELLOW, HAPICOLOR_VIOLET,
36cdf0e10cSrcweir         HAPICOLOR_TEAL, HAPICOLOR_GRAY_25_PERCENT, HAPICOLOR_GRAY_50_PERCENT, HAPICOLOR_PERIWINCKLE, HAPICOLOR_PLUM,
37cdf0e10cSrcweir         HAPICOLOR_IVORY, HAPICOLOR_LIGHTTURQUOISE, HAPICOLOR_DARKPRUPLE, HAPICOLOR_CORAL, HAPICOLOR_OCEANBLUE, HAPICOLOR_ICEBLUE,
38cdf0e10cSrcweir         HAPICOLOR_GREEN, HAPICOLOR_PINK, HAPICOLOR_YELLOW, HAPICOLOR_TURQUOISE, HAPICOLOR_VIOLET, HAPICOLOR_DARKRED, HAPICOLOR_TEAL,
39cdf0e10cSrcweir         HAPICOLOR_BLUE, HAPICOLOR_SKYBLUE, HAPICOLOR_LIGHTTURQUOISE, HAPICOLOR_LIGHTGREEN, HAPICOLOR_LIGHTYELLOW, HAPICOLOR_PALEBLUE,
40cdf0e10cSrcweir         HAPICOLOR_ROSE, HAPICOLOR_LAVENDER, HAPICOLOR_TAN, HAPICOLOR_LIGHTBLUE, HAPICOLOR_AQUA, HAPICOLOR_LIME, HAPICOLOR_GOLD,
41cdf0e10cSrcweir         HAPICOLOR_LIGHTORANGE, HAPICOLOR_ORANGE, HAPICOLOR_BLUEGRAY, HAPICOLOR_GRAY_40_PERCENT, HAPICOLOR_DARKTEAL,
42cdf0e10cSrcweir         HAPICOLOR_SEAGREEN, HAPICOLOR_NONAME, HAPICOLOR_OLIVEGREEN, HAPICOLOR_BROWN, HAPICOLOR_PLUM, HAPICOLOR_INDIGO,
43cdf0e10cSrcweir         HAPICOLOR_GRAY_80_PERCENT
44cdf0e10cSrcweir     };
45cdf0e10cSrcweir     return COLORINDIZES[nIndex];
46cdf0e10cSrcweir }
ScVbaColorFormat(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<XHelperInterface> xInternalParent,const uno::Reference<drawing::XShape> xShape,const sal_Int16 nColorFormatType)47cdf0e10cSrcweir ScVbaColorFormat::ScVbaColorFormat( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< XHelperInterface > xInternalParent, const uno::Reference< drawing::XShape > xShape, const sal_Int16 nColorFormatType ) : ScVbaColorFormat_BASE( xParent, xContext ), m_xInternalParent( xInternalParent ), m_xShape( xShape ), m_nColorFormatType( nColorFormatType )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     m_xPropertySet.set( xShape, uno::UNO_QUERY_THROW );
50cdf0e10cSrcweir     m_nFillFormatBackColor = 0;
51cdf0e10cSrcweir     try
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         uno::Reference< ov::msforms::XFillFormat > xFillFormat( xInternalParent, uno::UNO_QUERY_THROW );
54cdf0e10cSrcweir         m_pFillFormat = ( ScVbaFillFormat* )( xFillFormat.get() );
55cdf0e10cSrcweir     }catch ( uno::RuntimeException  e )
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir         m_pFillFormat = NULL;
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir void
setColorFormat(sal_Int16 _ntype)62cdf0e10cSrcweir ScVbaColorFormat::setColorFormat( sal_Int16 _ntype )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     m_nColorFormatType = _ntype;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir // Attribute
68cdf0e10cSrcweir sal_Int32 SAL_CALL
getRGB()69cdf0e10cSrcweir ScVbaColorFormat::getRGB() throw (uno::RuntimeException)
70cdf0e10cSrcweir {
71cdf0e10cSrcweir     sal_Int32 nRGB = 0;
72cdf0e10cSrcweir     switch( m_nColorFormatType )
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir     case ColorFormatType::LINEFORMAT_FORECOLOR:
75cdf0e10cSrcweir         m_xPropertySet->getPropertyValue( rtl::OUString::createFromAscii("LineColor") ) >>= nRGB;
76cdf0e10cSrcweir         break;
77cdf0e10cSrcweir     case ColorFormatType::LINEFORMAT_BACKCOLOR:
78cdf0e10cSrcweir         //TODO BackColor not supported
79cdf0e10cSrcweir         // m_xPropertySet->setPropertyValue( rtl::OUString::createFromAscii("Color"), uno::makeAny( nRGB ) );
80cdf0e10cSrcweir         break;
81cdf0e10cSrcweir     case ColorFormatType::FILLFORMAT_FORECOLOR:
82cdf0e10cSrcweir         m_xPropertySet->getPropertyValue( rtl::OUString::createFromAscii("FillColor") ) >>= nRGB;
83cdf0e10cSrcweir         break;
84cdf0e10cSrcweir     case ColorFormatType::FILLFORMAT_BACKCOLOR:
85cdf0e10cSrcweir         nRGB = m_nFillFormatBackColor;
86cdf0e10cSrcweir         break;
87cdf0e10cSrcweir     default:
88cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii("Second parameter of ColorFormat is wrong."), uno::Reference< uno::XInterface >() );
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir     nRGB = OORGBToXLRGB( nRGB );
91cdf0e10cSrcweir     return nRGB;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir void SAL_CALL
setRGB(sal_Int32 _rgb)95cdf0e10cSrcweir ScVbaColorFormat::setRGB( sal_Int32 _rgb ) throw (uno::RuntimeException)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir     sal_Int32 nRGB = XLRGBToOORGB( _rgb );
98cdf0e10cSrcweir     switch( m_nColorFormatType )
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir     case ColorFormatType::LINEFORMAT_FORECOLOR:
101cdf0e10cSrcweir         m_xPropertySet->setPropertyValue( rtl::OUString::createFromAscii( "LineColor" ), uno::makeAny( nRGB ) );
102cdf0e10cSrcweir         break;
103cdf0e10cSrcweir     case ColorFormatType::LINEFORMAT_BACKCOLOR:
104cdf0e10cSrcweir         // TODO BackColor not supported
105cdf0e10cSrcweir         break;
106cdf0e10cSrcweir     case ColorFormatType::FILLFORMAT_FORECOLOR:
107cdf0e10cSrcweir         m_xPropertySet->setPropertyValue( rtl::OUString::createFromAscii( "FillColor" ), uno::makeAny( nRGB ) );
108cdf0e10cSrcweir         if( m_pFillFormat )
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             m_pFillFormat->setForeColorAndInternalStyle(nRGB);
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir         break;
113cdf0e10cSrcweir     case ColorFormatType::FILLFORMAT_BACKCOLOR:
114cdf0e10cSrcweir         m_nFillFormatBackColor = nRGB;
115cdf0e10cSrcweir         if( m_pFillFormat )
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             m_pFillFormat->setForeColorAndInternalStyle(nRGB);
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir         break;
120cdf0e10cSrcweir     default:
121cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii("Second parameter of ColorFormat is wrong."), uno::Reference< uno::XInterface >() );
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir sal_Int32 SAL_CALL
getSchemeColor()126cdf0e10cSrcweir ScVbaColorFormat::getSchemeColor() throw (uno::RuntimeException)
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     sal_Int32 nColor = getRGB();
129cdf0e10cSrcweir     // #TODO I guess the number of elements is determined by the correct scheme
130cdf0e10cSrcweir     // the implementation here seems to be a rehash of color index ( which seems to be a
131cdf0e10cSrcweir     // different thing ) - I would guess we need to know/import etc. the correct color scheme
132cdf0e10cSrcweir     // or at least find out a little more
133cdf0e10cSrcweir     sal_Int32 i = 0;
134cdf0e10cSrcweir     for( ; i < 56; i++ )
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         if( nColor == MsoColorIndizes::getColorIndex(i) )
137cdf0e10cSrcweir 	   break;
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     if( i == 56 ) // this is most likely an error condition
141cdf0e10cSrcweir         --i;
142cdf0e10cSrcweir     return i;
143cdf0e10cSrcweir     // #TODO figure out what craziness is this,
144cdf0e10cSrcweir     // the 56 colors seems incorrect, as in default XL ( 2003 ) there are 80 colors
145cdf0e10cSrcweir /*
146cdf0e10cSrcweir     if( i == 56 )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         i = -2;
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     return ( i + 2 );
152cdf0e10cSrcweir */
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir void SAL_CALL
setSchemeColor(sal_Int32 _schemecolor)156cdf0e10cSrcweir ScVbaColorFormat::setSchemeColor( sal_Int32 _schemecolor ) throw (uno::RuntimeException)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     // the table is 0 based
159cdf0e10cSrcweir     sal_Int32 nColor = MsoColorIndizes::getColorIndex( _schemecolor );
160cdf0e10cSrcweir     // nColor is already xl RGB
161cdf0e10cSrcweir     setRGB( nColor );
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165cdf0e10cSrcweir rtl::OUString&
getServiceImplName()166cdf0e10cSrcweir ScVbaColorFormat::getServiceImplName()
167cdf0e10cSrcweir {
168cdf0e10cSrcweir     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaColorFormat") );
169cdf0e10cSrcweir     return sImplName;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir uno::Sequence< rtl::OUString >
getServiceNames()173cdf0e10cSrcweir ScVbaColorFormat::getServiceNames()
174cdf0e10cSrcweir {
175cdf0e10cSrcweir     static uno::Sequence< rtl::OUString > aServiceNames;
176cdf0e10cSrcweir     if ( aServiceNames.getLength() == 0 )
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         aServiceNames.realloc( 1 );
179cdf0e10cSrcweir         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.msforms.ColorFormat" ) );
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir     return aServiceNames;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184