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/XProperty.hpp>
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/container/XIndexAccess.hpp>
22 #include <com/sun/star/awt/FontWeight.hpp>
23 #include <com/sun/star/awt/FontUnderline.hpp>
24 #include <com/sun/star/awt/FontStrikeout.hpp>
25 #include <com/sun/star/awt/FontSlant.hpp>
26 #include <com/sun/star/text/XSimpleText.hpp>
27 #include <vbahelper/vbafontbase.hxx>
28 
29 using namespace ::ooo::vba;
30 using namespace ::com::sun::star;
31 
32 
33 // form controls use other property name as the remaining OOo API
34 #define VBAFONTBASE_PROPNAME( ascii_normal, ascii_control ) \
35     mbFormControl ? OUString( ascii_control ) : OUString( ascii_normal )
36 
VbaFontBase(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<css::container::XIndexAccess> & xPalette,const uno::Reference<beans::XPropertySet> & xPropertySet,bool bFormControl)37 VbaFontBase::VbaFontBase(
38         const uno::Reference< XHelperInterface >& xParent,
39         const uno::Reference< uno::XComponentContext >& xContext,
40         const uno::Reference< css::container::XIndexAccess >& xPalette,
41         const uno::Reference< beans::XPropertySet >& xPropertySet,
42         bool bFormControl ) :
43     VbaFontBase_BASE( xParent, xContext ),
44     mxFont( xPropertySet, uno::UNO_SET_THROW ),
45     mxPalette( xPalette, uno::UNO_SET_THROW ),
46     mbFormControl( bFormControl )
47 {
48 }
49 
~VbaFontBase()50 VbaFontBase::~VbaFontBase()
51 {
52 }
53 
54 void SAL_CALL
setSuperscript(const uno::Any & aValue)55 VbaFontBase::setSuperscript( const uno::Any& aValue )
56 {
57     // not supported in form controls
58     if( mbFormControl )
59         return;
60 
61     bool bValue = false;
62     aValue >>= bValue;
63     sal_Int16 nValue = NORMAL;
64     sal_Int8 nValue2 = NORMALHEIGHT;
65 
66     if( bValue )
67     {
68         nValue = SUPERSCRIPT;
69         nValue2 = SUPERSCRIPTHEIGHT;
70     }
71     mxFont->setPropertyValue( "CharEscapement" , uno::Any(nValue) );
72     mxFont->setPropertyValue( "CharEscapementHeight" , uno::Any(nValue2) );
73 }
74 
75 uno::Any SAL_CALL
getSuperscript()76 VbaFontBase::getSuperscript()
77 {
78     short nValue = NORMAL;
79     // not supported in form controls
80     if( !mbFormControl )
81        mxFont->getPropertyValue( "CharEscapement" ) >>= nValue;
82     return uno::makeAny( nValue == SUPERSCRIPT );
83 }
84 
85 void SAL_CALL
setSubscript(const uno::Any & aValue)86 VbaFontBase::setSubscript( const uno::Any& aValue )
87 {
88     // not supported in form controls
89     if( mbFormControl )
90         return;
91 
92     bool bValue = false;
93     aValue >>= bValue;
94     sal_Int16 nValue = NORMAL;
95     sal_Int8 nValue2 = NORMALHEIGHT;
96 
97     if( bValue )
98     {
99         nValue= SUBSCRIPT;
100         nValue2 = SUBSCRIPTHEIGHT;
101     }
102 
103     mxFont->setPropertyValue( "CharEscapementHeight" , uno::Any(nValue2) );
104     mxFont->setPropertyValue( "CharEscapement" , uno::Any(nValue) );
105 
106 }
107 
108 uno::Any SAL_CALL
getSubscript()109 VbaFontBase::getSubscript()
110 {
111     short nValue = NORMAL;
112     // not supported in form controls
113     if( !mbFormControl )
114        mxFont->getPropertyValue( "CharEscapement" ) >>= nValue;
115     return uno::makeAny( nValue == SUBSCRIPT );
116 }
117 
118 void SAL_CALL
setSize(const uno::Any & aValue)119 VbaFontBase::setSize( const uno::Any& aValue )
120 {
121     // form controls need a sal_Int16 containing points, other APIs need a float
122     uno::Any aVal( aValue );
123     if( mbFormControl )
124     {
125         float fVal = 0.0;
126         aVal >>= fVal;
127         aVal <<= static_cast< sal_Int16 >( fVal );
128     }
129     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharHeight", "FontHeight" ), aVal );
130 }
131 
132 uno::Any SAL_CALL
getSize()133 VbaFontBase::getSize()
134 {
135     return mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharHeight", "FontHeight" ) );
136 }
137 
138 void SAL_CALL
setColorIndex(const uno::Any & _colorindex)139 VbaFontBase::setColorIndex( const uno::Any& _colorindex )
140 {
141     sal_Int32 nIndex = 0;
142     _colorindex >>= nIndex;
143 
144     --nIndex; // OOo indices are zero bases
145 
146     // setColor expects colors in XL RGB values
147     // #FIXME this is daft we convert OO RGB val to XL RGB val and
148     // then back again to OO RGB value
149     setColor( OORGBToXLRGB(mxPalette->getByIndex( nIndex )) );
150 }
151 
152 
153 uno::Any SAL_CALL
getColorIndex()154 VbaFontBase::getColorIndex()
155 {
156     sal_Int32 nColor = 0;
157 
158     XLRGBToOORGB( getColor() ) >>= nColor;
159     sal_Int32 nElems = mxPalette->getCount();
160     sal_Int32 nIndex = -1;
161     for ( sal_Int32 count=0; count<nElems; ++count )
162            {
163         sal_Int32 nPaletteColor = 0;
164         mxPalette->getByIndex( count ) >>= nPaletteColor;
165         if ( nPaletteColor == nColor )
166         {
167             nIndex = count + 1; // 1 based
168             break;
169         }
170     }
171     return uno::makeAny( nIndex );
172 }
173 
174 void SAL_CALL
setBold(const uno::Any & aValue)175 VbaFontBase::setBold( const uno::Any& aValue )
176 {
177     bool bValue = false;
178     aValue >>= bValue;
179     double fBoldValue = awt::FontWeight::NORMAL;
180     if( bValue )
181         fBoldValue = awt::FontWeight::BOLD;
182     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharWeight", "FontWeight" ), uno::Any( fBoldValue ) );
183 
184 }
185 
186 uno::Any SAL_CALL
getBold()187 VbaFontBase::getBold()
188 {
189     double fValue = 0.0;
190     mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharWeight", "FontWeight" ) ) >>= fValue;
191     return uno::makeAny( fValue == awt::FontWeight::BOLD );
192 }
193 
194 void SAL_CALL
setStrikethrough(const uno::Any & aValue)195 VbaFontBase::setStrikethrough( const uno::Any& aValue )
196 {
197     bool bValue = false;
198     aValue >>= bValue;
199     short nValue = awt::FontStrikeout::NONE;
200     if( bValue )
201         nValue = awt::FontStrikeout::SINGLE;
202     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharStrikeout", "FontStrikeout" ), uno::Any( nValue ) );
203 }
204 
205 uno::Any SAL_CALL
getStrikethrough()206 VbaFontBase::getStrikethrough()
207 {
208     short nValue = 0;
209     mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharStrikeout", "FontStrikeout" ) ) >>= nValue;
210     return uno::Any( nValue == awt::FontStrikeout::SINGLE );
211 }
212 
213 void  SAL_CALL
setShadow(const uno::Any & aValue)214 VbaFontBase::setShadow( const uno::Any& aValue )
215 {
216     if( !mbFormControl )
217        mxFont->setPropertyValue( "CharShadowed" , aValue );
218 }
219 
220 uno::Any SAL_CALL
getShadow()221 VbaFontBase::getShadow()
222 {
223     return mbFormControl ? uno::Any( false ) : mxFont->getPropertyValue( "CharShadowed" );
224 }
225 
226 void  SAL_CALL
setItalic(const uno::Any & aValue)227 VbaFontBase::setItalic( const uno::Any& aValue )
228 {
229     bool bValue = false;
230     aValue >>= bValue;
231     awt::FontSlant nValue = awt::FontSlant_NONE;
232     if( bValue )
233         nValue = awt::FontSlant_ITALIC;
234     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharPosture", "FontSlant" ), uno::Any( static_cast<short>(nValue) ) );
235 }
236 
237 uno::Any SAL_CALL
getItalic()238 VbaFontBase::getItalic()
239 {
240     awt::FontSlant aFS;
241     mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharPosture", "FontSlant" ) ) >>= aFS;
242     return uno::makeAny( aFS == awt::FontSlant_ITALIC );
243 }
244 
245 void  SAL_CALL
setName(const uno::Any & aValue)246 VbaFontBase::setName( const uno::Any& aValue )
247 {
248     OUString sString;
249     aValue >>= sString;
250     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharFontName", "FontName" ), aValue );
251 }
252 
253 uno::Any SAL_CALL
getName()254 VbaFontBase::getName()
255 {
256     return mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharFontName", "FontName" ) );
257 }
258 
259 uno::Any
getColor()260 VbaFontBase::getColor()
261 {
262     uno::Any aAny = OORGBToXLRGB( mxFont->getPropertyValue( VBAFONTBASE_PROPNAME( "CharColor", "TextColor" ) ) );
263     return aAny;
264 }
265 
266 void
setColor(const uno::Any & _color)267 VbaFontBase::setColor( const uno::Any& _color  )
268 {
269     mxFont->setPropertyValue( VBAFONTBASE_PROPNAME( "CharColor", "TextColor" ), XLRGBToOORGB(_color) );
270 }
271 
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
273