1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <vbanewfont.hxx>
29 #include <com/sun/star/awt/FontWeight.hpp>
30 #include <com/sun/star/awt/FontSlant.hpp>
31 #include <com/sun/star/awt/FontStrikeout.hpp>
32 #include <com/sun/star/awt/FontUnderline.hpp>
33 
34 using namespace ::com::sun::star;
35 using namespace ::ooo::vba;
36 
37 // ============================================================================
38 
39 VbaNewFont::VbaNewFont(
40         const uno::Reference< XHelperInterface >& rxParent,
41         const uno::Reference< uno::XComponentContext >& rxContext,
42         const uno::Reference< beans::XPropertySet >& rxModelProps ) throw (uno::RuntimeException) :
43     VbaNewFont_BASE( rxParent, rxContext ),
44     mxProps( rxModelProps, uno::UNO_SET_THROW )
45 {
46 }
47 
48 // XNewFont attributes
49 
50 ::rtl::OUString SAL_CALL VbaNewFont::getName() throw (uno::RuntimeException)
51 {
52     uno::Any aAny = mxProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontName" ) ) );
53     return aAny.get< ::rtl::OUString >();
54 }
55 
56 void SAL_CALL VbaNewFont::setName( const ::rtl::OUString& rName ) throw (uno::RuntimeException)
57 {
58     mxProps->setPropertyValue(
59         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontName" ) ),
60         uno::Any( rName ) );
61 }
62 
63 double SAL_CALL VbaNewFont::getSize() throw (uno::RuntimeException)
64 {
65     uno::Any aAny = mxProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontHeight" ) ) );
66     return aAny.get< float >();
67 }
68 
69 void SAL_CALL VbaNewFont::setSize( double fSize ) throw (uno::RuntimeException)
70 {
71     mxProps->setPropertyValue(
72         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontHeight" ) ),
73         uno::Any( static_cast< float >( fSize ) ) );
74 }
75 
76 sal_Int16 SAL_CALL VbaNewFont::getCharset() throw (uno::RuntimeException)
77 {
78     uno::Any aAny = mxProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontCharset" ) ) );
79     return rtl_getBestWindowsCharsetFromTextEncoding( static_cast< rtl_TextEncoding >( aAny.get< sal_Int16 >() ) );
80 }
81 
82 void SAL_CALL VbaNewFont::setCharset( sal_Int16 nCharset ) throw (uno::RuntimeException)
83 {
84     rtl_TextEncoding eFontEnc = RTL_TEXTENCODING_DONTKNOW;
85     if( (0 <= nCharset) && (nCharset <= SAL_MAX_UINT8) )
86         eFontEnc = rtl_getTextEncodingFromWindowsCharset( static_cast< sal_uInt8 >( nCharset ) );
87     if( eFontEnc == RTL_TEXTENCODING_DONTKNOW )
88         throw uno::RuntimeException();
89     mxProps->setPropertyValue(
90         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontCharset" ) ),
91         uno::Any( static_cast< sal_Int16 >( eFontEnc ) ) );
92 }
93 
94 sal_Int16 SAL_CALL VbaNewFont::getWeight() throw (uno::RuntimeException)
95 {
96     return getBold() ? 700 : 400;
97 }
98 
99 void SAL_CALL VbaNewFont::setWeight( sal_Int16 nWeight ) throw (uno::RuntimeException)
100 {
101     setBold( nWeight >= 700 );
102 }
103 
104 sal_Bool SAL_CALL VbaNewFont::getBold() throw (uno::RuntimeException)
105 {
106     uno::Any aAny = mxProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontWeight" ) ) );
107     return aAny.get< float >() > awt::FontWeight::NORMAL;
108 }
109 
110 void SAL_CALL VbaNewFont::setBold( sal_Bool bBold ) throw (uno::RuntimeException)
111 {
112     mxProps->setPropertyValue(
113         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontWeight" ) ),
114         uno::Any( bBold ? awt::FontWeight::BOLD : awt::FontWeight::NORMAL ) );
115 }
116 
117 sal_Bool SAL_CALL VbaNewFont::getItalic() throw (uno::RuntimeException)
118 {
119     uno::Any aAny = mxProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontSlant" ) ) );
120     return aAny.get< awt::FontSlant >() != awt::FontSlant_NONE;
121 }
122 
123 void SAL_CALL VbaNewFont::setItalic( sal_Bool bItalic ) throw (uno::RuntimeException)
124 {
125     mxProps->setPropertyValue(
126         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontSlant" ) ),
127         uno::Any( bItalic ? awt::FontSlant_ITALIC : awt::FontSlant_NONE ) );
128 }
129 
130 sal_Bool SAL_CALL VbaNewFont::getUnderline() throw (uno::RuntimeException)
131 {
132     uno::Any aAny = mxProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontUnderline" ) ) );
133     return aAny.get< sal_Int16 >() != awt::FontUnderline::NONE;
134 }
135 
136 void SAL_CALL VbaNewFont::setUnderline( sal_Bool bUnderline ) throw (uno::RuntimeException)
137 {
138     mxProps->setPropertyValue(
139         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontUnderline" ) ),
140         uno::Any( bUnderline ? awt::FontUnderline::SINGLE : awt::FontUnderline::NONE ) );
141 }
142 
143 sal_Bool SAL_CALL VbaNewFont::getStrikethrough() throw (uno::RuntimeException)
144 {
145     uno::Any aAny = mxProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontStrikeout" ) ) );
146     return aAny.get< sal_Int16 >() != awt::FontStrikeout::NONE;
147 }
148 
149 void SAL_CALL VbaNewFont::setStrikethrough( sal_Bool bStrikethrough ) throw (uno::RuntimeException)
150 {
151     mxProps->setPropertyValue(
152         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontStrikeout" ) ),
153         uno::Any( bStrikethrough ? awt::FontStrikeout::SINGLE : awt::FontStrikeout::NONE ) );
154 }
155 
156 // XHelperInterface
157 
158 VBAHELPER_IMPL_XHELPERINTERFACE( VbaNewFont, "ooo.vba.msforms.NewFont" )
159 
160 // ============================================================================
161