xref: /aoo42x/main/editeng/source/uno/unofdesc.cxx (revision cdf0e10c)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_editeng.hxx"
30 
31 #include <editeng/eeitem.hxx>
32 #include <com/sun/star/uno/Any.hxx>
33 
34 #ifndef _TOOLKIT_HELPRE_VCLUNOHELPER_HXX_
35 #include <toolkit/helper/vclunohelper.hxx>
36 #endif
37 #include <editeng/fontitem.hxx>
38 #include <editeng/fhgtitem.hxx>
39 #include <editeng/postitem.hxx>
40 #include <editeng/udlnitem.hxx>
41 #include <editeng/wghtitem.hxx>
42 #include <editeng/crsditem.hxx>
43 #include <editeng/wrlmitem.hxx>
44 #include <editeng/memberids.hrc>
45 #include <svl/itempool.hxx>
46 
47 #include <editeng/unofdesc.hxx>
48 
49 using namespace ::rtl;
50 using namespace ::com::sun::star;
51 
52 
53 void SvxUnoFontDescriptor::ConvertToFont( const awt::FontDescriptor& rDesc, Font& rFont )
54 {
55 	rFont.SetName( rDesc.Name );
56 	rFont.SetStyleName( rDesc.StyleName );
57 	rFont.SetSize( Size( rDesc.Width, rDesc.Height ) );
58 	rFont.SetFamily( (FontFamily)rDesc.Family );
59 	rFont.SetCharSet( (CharSet)rDesc.CharSet );
60 	rFont.SetPitch( (FontPitch)rDesc.Pitch );
61 	rFont.SetOrientation( (short)(rDesc.Orientation*10) );
62 	rFont.SetKerning( rDesc.Kerning );
63 	rFont.SetWeight( VCLUnoHelper::ConvertFontWeight(rDesc.Weight) );
64 	rFont.SetItalic( (FontItalic)rDesc.Slant );
65 	rFont.SetUnderline( (FontUnderline)rDesc.Underline );
66 	rFont.SetStrikeout( (FontStrikeout)rDesc.Strikeout );
67 	rFont.SetWordLineMode( rDesc.WordLineMode );
68 }
69 
70 void SvxUnoFontDescriptor::ConvertFromFont( const Font& rFont, awt::FontDescriptor& rDesc )
71 {
72 	rDesc.Name = rFont.GetName();
73 	rDesc.StyleName = rFont.GetStyleName();
74 	rDesc.Width = sal::static_int_cast< sal_Int16 >(rFont.GetSize().Width());
75 	rDesc.Height = sal::static_int_cast< sal_Int16 >(rFont.GetSize().Height());
76 	rDesc.Family = sal::static_int_cast< sal_Int16 >(rFont.GetFamily());
77 	rDesc.CharSet = rFont.GetCharSet();
78 	rDesc.Pitch = sal::static_int_cast< sal_Int16 >(rFont.GetPitch());
79 	rDesc.Orientation = static_cast< float >(rFont.GetOrientation() / 10);
80 	rDesc.Kerning = rFont.IsKerning();
81 	rDesc.Weight = VCLUnoHelper::ConvertFontWeight( rFont.GetWeight() );
82 	rDesc.Slant = (awt::FontSlant)rFont.GetItalic();
83 	rDesc.Underline = sal::static_int_cast< sal_Int16 >(rFont.GetUnderline());
84 	rDesc.Strikeout = sal::static_int_cast< sal_Int16 >(rFont.GetStrikeout());
85 	rDesc.WordLineMode = rFont.IsWordLineMode();
86 }
87 
88 void SvxUnoFontDescriptor::FillItemSet( const awt::FontDescriptor& rDesc, SfxItemSet& rSet )
89 {
90 	uno::Any aTemp;
91 
92 	{
93 		SvxFontItem aFontItem( EE_CHAR_FONTINFO );
94         aFontItem.SetFamilyName( rDesc.Name);
95         aFontItem.SetStyleName( rDesc.StyleName);
96         aFontItem.SetFamily( (FontFamily)rDesc.Family);
97         aFontItem.SetCharSet( rDesc.CharSet );
98         aFontItem.SetPitch( (FontPitch)rDesc.Pitch);
99 		rSet.Put(aFontItem);
100 	}
101 
102 	{
103 		SvxFontHeightItem aFontHeightItem( 0, 100, EE_CHAR_FONTHEIGHT );
104 		aTemp <<= (float)rDesc.Height;
105 		((SfxPoolItem*)&aFontHeightItem)->PutValue( aTemp, MID_FONTHEIGHT|CONVERT_TWIPS );
106 		rSet.Put(aFontHeightItem);
107 	}
108 
109 	{
110 		SvxPostureItem aPostureItem( (FontItalic)0, EE_CHAR_ITALIC );
111 		aTemp <<= rDesc.Slant;
112 		((SfxPoolItem*)&aPostureItem)->PutValue( aTemp, MID_POSTURE );
113 		rSet.Put(aPostureItem);
114 	}
115 
116 	{
117 		SvxUnderlineItem aUnderlineItem( (FontUnderline)0, EE_CHAR_UNDERLINE );
118 		aTemp <<= (sal_Int16)rDesc.Underline;
119 		((SfxPoolItem*)&aUnderlineItem)->PutValue( aTemp, MID_TL_STYLE );
120 		rSet.Put( aUnderlineItem );
121 	}
122 
123 	{
124 		SvxWeightItem aWeightItem( (FontWeight)0, EE_CHAR_WEIGHT );
125 		aTemp <<= rDesc.Weight;
126 		((SfxPoolItem*)&aWeightItem)->PutValue( aTemp, MID_WEIGHT );
127 		rSet.Put( aWeightItem );
128 	}
129 
130 	{
131 		SvxCrossedOutItem aCrossedOutItem( (FontStrikeout)0, EE_CHAR_STRIKEOUT );
132 		aTemp <<= rDesc.Strikeout;
133 		((SfxPoolItem*)&aCrossedOutItem)->PutValue( aTemp, MID_CROSS_OUT );
134 		rSet.Put( aCrossedOutItem );
135 	}
136 
137 	{
138 		SvxWordLineModeItem aWLMItem( rDesc.WordLineMode, EE_CHAR_WLM );
139 		rSet.Put( aWLMItem );
140 	}
141 }
142 
143 void SvxUnoFontDescriptor::FillFromItemSet( const SfxItemSet& rSet, awt::FontDescriptor& rDesc )
144 {
145 	const SfxPoolItem* pItem = NULL;
146 	{
147 		SvxFontItem* pFontItem = (SvxFontItem*)&rSet.Get( EE_CHAR_FONTINFO, sal_True );
148 		rDesc.Name		= pFontItem->GetFamilyName();
149 		rDesc.StyleName	= pFontItem->GetStyleName();
150 		rDesc.Family	= sal::static_int_cast< sal_Int16 >(
151             pFontItem->GetFamily());
152 		rDesc.CharSet	= pFontItem->GetCharSet();
153 		rDesc.Pitch		= sal::static_int_cast< sal_Int16 >(
154             pFontItem->GetPitch());
155 	}
156 	{
157 		pItem = &rSet.Get( EE_CHAR_FONTHEIGHT, sal_True );
158 		uno::Any aHeight;
159 		if( pItem->QueryValue( aHeight, MID_FONTHEIGHT ) )
160 			aHeight >>= rDesc.Height;
161 	}
162 	{
163 		pItem = &rSet.Get( EE_CHAR_ITALIC, sal_True );
164 		uno::Any aFontSlant;
165 		if(pItem->QueryValue( aFontSlant, MID_POSTURE ))
166 			aFontSlant >>= rDesc.Slant;
167 	}
168 	{
169 		pItem = &rSet.Get( EE_CHAR_UNDERLINE, sal_True );
170 		uno::Any aUnderline;
171 		if(pItem->QueryValue( aUnderline, MID_TL_STYLE ))
172 			aUnderline >>= rDesc.Underline;
173 	}
174 	{
175 		pItem = &rSet.Get( EE_CHAR_WEIGHT, sal_True );
176 		uno::Any aWeight;
177 		if(pItem->QueryValue( aWeight, MID_WEIGHT ))
178 			aWeight >>= rDesc.Weight;
179 	}
180 	{
181 		pItem = &rSet.Get( EE_CHAR_STRIKEOUT, sal_True );
182 		uno::Any aStrikeOut;
183 		if(pItem->QueryValue( aStrikeOut, MID_CROSS_OUT ))
184 			aStrikeOut >>= rDesc.Strikeout;
185 	}
186 	{
187 		SvxWordLineModeItem* pWLMItem = (SvxWordLineModeItem*)&rSet.Get( EE_CHAR_WLM, sal_True );
188 		rDesc.WordLineMode = pWLMItem->GetValue();
189 	}
190 }
191 
192 #define CheckState( state ) \
193 	switch( state ) \
194 	{ \
195 	case SFX_ITEM_DONTCARE: \
196 	case SFX_ITEM_DISABLED: \
197 		return beans::PropertyState_AMBIGUOUS_VALUE; \
198 	case SFX_ITEM_READONLY: \
199 	case SFX_ITEM_SET: \
200 		return beans::PropertyState_DIRECT_VALUE; \
201 	}
202 
203 beans::PropertyState SvxUnoFontDescriptor::getPropertyState( const SfxItemSet& rSet )
204 {
205 	CheckState(rSet.GetItemState( EE_CHAR_FONTINFO, sal_False ));
206 	CheckState(rSet.GetItemState( EE_CHAR_FONTHEIGHT, sal_False ));
207 	CheckState(rSet.GetItemState( EE_CHAR_ITALIC, sal_False ));
208 	CheckState(rSet.GetItemState( EE_CHAR_UNDERLINE, sal_False ));
209 	CheckState(rSet.GetItemState( EE_CHAR_WEIGHT, sal_False ));
210 	CheckState(rSet.GetItemState( EE_CHAR_STRIKEOUT, sal_False ));
211 	CheckState(rSet.GetItemState( EE_CHAR_WLM, sal_False ));
212 
213 	return beans::PropertyState_DEFAULT_VALUE;
214 }
215 
216 void SvxUnoFontDescriptor::setPropertyToDefault( SfxItemSet& rSet )
217 {
218 	rSet.InvalidateItem( EE_CHAR_FONTINFO );
219 	rSet.InvalidateItem( EE_CHAR_FONTHEIGHT );
220 	rSet.InvalidateItem( EE_CHAR_ITALIC );
221 	rSet.InvalidateItem( EE_CHAR_UNDERLINE );
222 	rSet.InvalidateItem( EE_CHAR_WEIGHT );
223 	rSet.InvalidateItem( EE_CHAR_STRIKEOUT );
224 	rSet.InvalidateItem( EE_CHAR_WLM );
225 }
226 
227 uno::Any SvxUnoFontDescriptor::getPropertyDefault( SfxItemPool* pPool )
228 {
229 	SfxItemSet aSet( *pPool, EE_CHAR_FONTINFO, EE_CHAR_FONTINFO,
230 							 EE_CHAR_FONTHEIGHT, EE_CHAR_FONTHEIGHT,
231 							 EE_CHAR_ITALIC, EE_CHAR_ITALIC,
232 							 EE_CHAR_UNDERLINE, EE_CHAR_UNDERLINE,
233 							 EE_CHAR_WEIGHT, EE_CHAR_WEIGHT,
234 							 EE_CHAR_STRIKEOUT, EE_CHAR_STRIKEOUT,
235 							 EE_CHAR_WLM, EE_CHAR_WLM, 0 );
236 
237 	uno::Any aAny;
238 
239 	if(!pPool->IsWhich(EE_CHAR_FONTINFO)||
240 		!pPool->IsWhich(EE_CHAR_FONTHEIGHT)||
241 		!pPool->IsWhich(EE_CHAR_ITALIC)||
242 		!pPool->IsWhich(EE_CHAR_UNDERLINE)||
243 		!pPool->IsWhich(EE_CHAR_WEIGHT)||
244 		!pPool->IsWhich(EE_CHAR_STRIKEOUT)||
245 		!pPool->IsWhich(EE_CHAR_WLM))
246 		return aAny;
247 
248 	aSet.Put(pPool->GetDefaultItem(EE_CHAR_FONTINFO));
249 	aSet.Put(pPool->GetDefaultItem(EE_CHAR_FONTHEIGHT));
250 	aSet.Put(pPool->GetDefaultItem(EE_CHAR_ITALIC));
251 	aSet.Put(pPool->GetDefaultItem(EE_CHAR_UNDERLINE));
252 	aSet.Put(pPool->GetDefaultItem(EE_CHAR_WEIGHT));
253 	aSet.Put(pPool->GetDefaultItem(EE_CHAR_STRIKEOUT));
254 	aSet.Put(pPool->GetDefaultItem(EE_CHAR_WLM));
255 
256 	awt::FontDescriptor aDesc;
257 
258 	FillFromItemSet( aSet, aDesc );
259 
260 	aAny <<= aDesc;
261 
262 	return aAny;
263 }
264 
265 
266 
267