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_forms.hxx"
30 #include "formcontrolfont.hxx"
31 #ifndef _FRM_PROPERTY_HRC_
32 #include "property.hrc"
33 #endif
34 #include "property.hxx"
35 #include <tools/debug.hxx>
36 #include <comphelper/property.hxx>
37 #include <comphelper/types.hxx>
38 #include <tools/color.hxx>
39 #include <toolkit/helper/emptyfontdescriptor.hxx>
40 #include <com/sun/star/awt/FontRelief.hpp>
41 #include <com/sun/star/awt/FontEmphasisMark.hpp>
42 
43 //.........................................................................
44 namespace frm
45 {
46 //.........................................................................
47 
48     using namespace ::comphelper;
49     using namespace ::com::sun::star::uno;
50     using namespace ::com::sun::star::awt;
51     using namespace ::com::sun::star::lang;
52     using namespace ::com::sun::star::beans;
53 
54     //------------------------------------------------------------------------------
55     namespace
56     {
57         Any lcl_extractFontDescriptorAggregate( sal_Int32 _nHandle, const FontDescriptor& _rFont )
58         {
59             Any aValue;
60             switch ( _nHandle )
61             {
62 		    case PROPERTY_ID_FONT_NAME:
63 			    aValue <<= _rFont.Name;
64 			    break;
65 
66             case PROPERTY_ID_FONT_STYLENAME:
67 			    aValue <<= _rFont.StyleName;
68 			    break;
69 
70             case PROPERTY_ID_FONT_FAMILY:
71 			    aValue <<= (sal_Int16)_rFont.Family;
72 			    break;
73 
74             case PROPERTY_ID_FONT_CHARSET:
75 			    aValue <<= (sal_Int16)_rFont.CharSet;
76 			    break;
77 
78             case PROPERTY_ID_FONT_CHARWIDTH:
79 			    aValue <<= _rFont.CharacterWidth;
80                 break;
81 
82             case PROPERTY_ID_FONT_KERNING:
83 			    aValue <<= _rFont.Kerning;
84                 break;
85 
86             case PROPERTY_ID_FONT_ORIENTATION:
87 			    aValue <<= _rFont.Orientation;
88                 break;
89 
90             case PROPERTY_ID_FONT_PITCH:
91 			    aValue <<= _rFont.Pitch;
92                 break;
93 
94             case PROPERTY_ID_FONT_TYPE:
95 			    aValue <<= _rFont.Type;
96                 break;
97 
98             case PROPERTY_ID_FONT_WIDTH:
99 			    aValue <<= _rFont.Width;
100                 break;
101 
102             case PROPERTY_ID_FONT_HEIGHT:
103 			    aValue <<= (float)( _rFont.Height );
104 			    break;
105 
106             case PROPERTY_ID_FONT_WEIGHT:
107 			    aValue <<= (float)_rFont.Weight;
108 			    break;
109 
110             case PROPERTY_ID_FONT_SLANT:
111 			    aValue = makeAny(_rFont.Slant);
112 			    break;
113 
114             case PROPERTY_ID_FONT_UNDERLINE:
115 			    aValue <<= (sal_Int16)_rFont.Underline;
116 			    break;
117 
118             case PROPERTY_ID_FONT_STRIKEOUT:
119 			    aValue <<= (sal_Int16)_rFont.Strikeout;
120 			    break;
121 
122             case PROPERTY_ID_FONT_WORDLINEMODE:
123 			    aValue = makeAny( (sal_Bool)_rFont.WordLineMode );
124 			    break;
125 
126             default:
127                 OSL_ENSURE( sal_False, "lcl_extractFontDescriptorAggregate: invalid handle!" );
128                 break;
129             }
130             return aValue;
131         }
132     }
133 
134     //=====================================================================
135     //= FontControlModel
136     //=====================================================================
137     //---------------------------------------------------------------------
138     FontControlModel::FontControlModel( bool _bToolkitCompatibleDefaults )
139         :m_nFontRelief( FontRelief::NONE )
140         ,m_nFontEmphasis( FontEmphasisMark::NONE )
141         ,m_bToolkitCompatibleDefaults( _bToolkitCompatibleDefaults )
142     {
143     }
144 
145     //---------------------------------------------------------------------
146     FontControlModel::FontControlModel( const FontControlModel* _pOriginal )
147     {
148         m_aFont = _pOriginal->m_aFont;
149 	    m_nFontRelief = _pOriginal->m_nFontRelief;
150 	    m_nFontEmphasis = _pOriginal->m_nFontEmphasis;
151         m_aTextLineColor = _pOriginal->m_aTextLineColor;
152         m_aTextColor = _pOriginal->m_aTextColor;
153         m_bToolkitCompatibleDefaults = _pOriginal->m_bToolkitCompatibleDefaults;
154     }
155 
156     //---------------------------------------------------------------------
157     bool FontControlModel::isFontRelatedProperty( sal_Int32 _nPropertyHandle ) const
158     {
159         return isFontAggregateProperty( _nPropertyHandle )
160             || ( _nPropertyHandle == PROPERTY_ID_FONT )
161             || ( _nPropertyHandle == PROPERTY_ID_FONTEMPHASISMARK )
162             || ( _nPropertyHandle == PROPERTY_ID_FONTRELIEF )
163             || ( _nPropertyHandle == PROPERTY_ID_TEXTLINECOLOR )
164             || ( _nPropertyHandle == PROPERTY_ID_TEXTCOLOR );
165     }
166 
167     //---------------------------------------------------------------------
168     bool FontControlModel::isFontAggregateProperty( sal_Int32 _nPropertyHandle ) const
169     {
170         return ( _nPropertyHandle == PROPERTY_ID_FONT_CHARWIDTH )
171             || ( _nPropertyHandle == PROPERTY_ID_FONT_ORIENTATION )
172             || ( _nPropertyHandle == PROPERTY_ID_FONT_WIDTH )
173             || ( _nPropertyHandle == PROPERTY_ID_FONT_NAME )
174 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_STYLENAME )
175 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_FAMILY )
176 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_CHARSET )
177 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_HEIGHT )
178 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_WEIGHT )
179 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_SLANT )
180 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_UNDERLINE )
181 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_STRIKEOUT )
182 		    || ( _nPropertyHandle == PROPERTY_ID_FONT_WORDLINEMODE )
183             || ( _nPropertyHandle == PROPERTY_ID_FONT_PITCH )
184             || ( _nPropertyHandle == PROPERTY_ID_FONT_KERNING )
185             || ( _nPropertyHandle == PROPERTY_ID_FONT_TYPE );
186     }
187 
188     //---------------------------------------------------------------------
189     sal_Int32 FontControlModel::getTextColor( ) const
190     {
191         sal_Int32 nColor = COL_TRANSPARENT;
192         m_aTextColor >>= nColor;
193         return nColor;
194     }
195 
196     //---------------------------------------------------------------------
197     sal_Int32 FontControlModel::getTextLineColor( ) const
198     {
199         sal_Int32 nColor = COL_TRANSPARENT;
200         m_aTextLineColor >>= nColor;
201         return nColor;
202     }
203 
204     //------------------------------------------------------------------------------
205     void FontControlModel::describeFontRelatedProperties( Sequence< Property >& /* [out] */ _rProps ) const
206     {
207         sal_Int32 nPos = _rProps.getLength();
208         _rProps.realloc( nPos + 21 );
209         Property* pProperties = _rProps.getArray();
210 
211         DECL_PROP2      ( FONT,               FontDescriptor,   BOUND, MAYBEDEFAULT );
212         DECL_PROP2      ( FONTEMPHASISMARK,   sal_Int16,        BOUND, MAYBEDEFAULT );
213         DECL_PROP2      ( FONTRELIEF,         sal_Int16,        BOUND, MAYBEDEFAULT );
214         DECL_PROP3      ( TEXTCOLOR,          sal_Int32,        BOUND, MAYBEDEFAULT, MAYBEVOID );
215         DECL_PROP3      ( TEXTLINECOLOR,      sal_Int32,        BOUND, MAYBEDEFAULT, MAYBEVOID );
216 
217         DECL_PROP1      ( FONT_CHARWIDTH,     float,            MAYBEDEFAULT );
218         DECL_BOOL_PROP1 ( FONT_KERNING,                         MAYBEDEFAULT );
219         DECL_PROP1      ( FONT_ORIENTATION,   float,            MAYBEDEFAULT );
220         DECL_PROP1      ( FONT_PITCH,         sal_Int16,        MAYBEDEFAULT );
221         DECL_PROP1      ( FONT_TYPE,          sal_Int16,        MAYBEDEFAULT );
222         DECL_PROP1      ( FONT_WIDTH,         sal_Int16,        MAYBEDEFAULT );
223         DECL_PROP1      ( FONT_NAME,          ::rtl::OUString,  MAYBEDEFAULT );
224         DECL_PROP1      ( FONT_STYLENAME,     ::rtl::OUString,  MAYBEDEFAULT );
225         DECL_PROP1      ( FONT_FAMILY,        sal_Int16,        MAYBEDEFAULT );
226         DECL_PROP1      ( FONT_CHARSET,       sal_Int16,        MAYBEDEFAULT );
227         DECL_PROP1      ( FONT_HEIGHT,        float,            MAYBEDEFAULT );
228         DECL_PROP1      ( FONT_WEIGHT,        float,            MAYBEDEFAULT );
229         DECL_PROP1      ( FONT_SLANT,         sal_Int16,        MAYBEDEFAULT );
230         DECL_PROP1      ( FONT_UNDERLINE,     sal_Int16,        MAYBEDEFAULT );
231         DECL_PROP1      ( FONT_STRIKEOUT,     sal_Int16,        MAYBEDEFAULT );
232         DECL_BOOL_PROP1 ( FONT_WORDLINEMODE,                    MAYBEDEFAULT );
233     }
234 
235     //---------------------------------------------------------------------
236     void FontControlModel::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
237     {
238         switch( _nHandle )
239         {
240         case PROPERTY_ID_TEXTCOLOR:
241 			_rValue = m_aTextColor;
242 			break;
243 
244 		case PROPERTY_ID_FONTEMPHASISMARK:
245 			_rValue <<= m_nFontEmphasis;
246 			break;
247 
248         case PROPERTY_ID_FONTRELIEF:
249 			_rValue <<= m_nFontRelief;
250 			break;
251 
252 		case PROPERTY_ID_TEXTLINECOLOR:
253 			_rValue = m_aTextLineColor;
254 			break;
255 
256         case PROPERTY_ID_FONT:
257 			_rValue = makeAny( m_aFont );
258 			break;
259 
260         default:
261             _rValue = lcl_extractFontDescriptorAggregate( _nHandle, m_aFont );
262             break;
263         }
264     }
265 
266     //---------------------------------------------------------------------
267     sal_Bool FontControlModel::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue,
268                 sal_Int32 _nHandle, const Any& _rValue ) throw( IllegalArgumentException )
269     {
270         sal_Bool bModified = sal_False;
271         switch( _nHandle )
272         {
273 		case PROPERTY_ID_TEXTCOLOR:
274 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aTextColor, ::getCppuType( static_cast< const sal_Int32* >( NULL ) ) );
275 			break;
276 
277         case PROPERTY_ID_TEXTLINECOLOR:
278 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aTextLineColor, ::getCppuType( static_cast< sal_Int32* >( NULL ) ) );
279 			break;
280 
281         case PROPERTY_ID_FONTEMPHASISMARK:
282 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_nFontEmphasis );
283 			break;
284 
285         case PROPERTY_ID_FONTRELIEF:
286 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_nFontRelief );
287 			break;
288 
289         case PROPERTY_ID_FONT:
290         {
291             Any aWorkAroundGccLimitation = makeAny( m_aFont );
292             bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, aWorkAroundGccLimitation, ::getCppuType( &m_aFont ) );
293         }
294         break;
295 
296         case PROPERTY_ID_FONT_NAME:
297 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aFont.Name );
298 			break;
299 
300         case PROPERTY_ID_FONT_STYLENAME:
301 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aFont.StyleName );
302 			break;
303 
304         case PROPERTY_ID_FONT_FAMILY:
305 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Family );
306 			break;
307 
308         case PROPERTY_ID_FONT_CHARSET:
309             bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.CharSet );
310 			break;
311 
312         case PROPERTY_ID_FONT_CHARWIDTH:
313             bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, float( m_aFont.CharacterWidth ) );
314             break;
315 
316         case PROPERTY_ID_FONT_KERNING:
317             bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Kerning );
318             break;
319 
320         case PROPERTY_ID_FONT_ORIENTATION:
321             bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, float( m_aFont.Orientation ) );
322             break;
323 
324         case PROPERTY_ID_FONT_PITCH:
325 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Pitch );
326 			break;
327 
328         case PROPERTY_ID_FONT_TYPE:
329 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Type );
330 			break;
331 
332         case PROPERTY_ID_FONT_WIDTH:
333 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Width );
334 			break;
335 
336         case PROPERTY_ID_FONT_HEIGHT:
337 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, float( m_aFont.Height ) );
338 			break;
339 
340         case PROPERTY_ID_FONT_WEIGHT:
341 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aFont.Weight );
342 			break;
343 
344         case PROPERTY_ID_FONT_SLANT:
345 			bModified = tryPropertyValueEnum( _rConvertedValue, _rOldValue, _rValue, m_aFont.Slant );
346 			break;
347 
348         case PROPERTY_ID_FONT_UNDERLINE:
349 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Underline );
350 			break;
351 
352         case PROPERTY_ID_FONT_STRIKEOUT:
353 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Strikeout );
354 			break;
355 
356         case PROPERTY_ID_FONT_WORDLINEMODE:
357 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Bool)m_aFont.WordLineMode );
358 			break;
359 
360         default:
361             DBG_ERROR( "FontControlModel::convertFastPropertyValue: no font aggregate!" );
362         }
363         return bModified;
364     }
365 
366     //------------------------------------------------------------------------------
367     void FontControlModel::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw ( Exception )
368     {
369         switch( _nHandle )
370         {
371 		case PROPERTY_ID_TEXTCOLOR:
372 			m_aTextColor = _rValue;
373 			break;
374 
375 		case PROPERTY_ID_TEXTLINECOLOR:
376 			m_aTextLineColor = _rValue;
377 			break;
378 
379         case PROPERTY_ID_FONTEMPHASISMARK:
380 			_rValue >>= m_nFontEmphasis;
381 			break;
382 
383         case PROPERTY_ID_FONTRELIEF:
384 			_rValue >>= m_nFontRelief;
385 			break;
386 
387         case PROPERTY_ID_FONT:
388 			_rValue >>= m_aFont;
389 			break;
390 
391         case PROPERTY_ID_FONT_NAME:
392 			_rValue >>= m_aFont.Name;
393 			break;
394 
395         case PROPERTY_ID_FONT_STYLENAME:
396 			_rValue >>= m_aFont.StyleName;
397 			break;
398 
399         case PROPERTY_ID_FONT_FAMILY:
400 			_rValue >>= m_aFont.Family;
401 			break;
402 
403         case PROPERTY_ID_FONT_CHARSET:
404 			_rValue >>= m_aFont.CharSet;
405 			break;
406 
407         case PROPERTY_ID_FONT_CHARWIDTH:
408 			_rValue >>= m_aFont.CharacterWidth;
409             break;
410 
411         case PROPERTY_ID_FONT_KERNING:
412 			_rValue >>= m_aFont.Kerning;
413             break;
414 
415         case PROPERTY_ID_FONT_ORIENTATION:
416 			_rValue >>= m_aFont.Orientation;
417             break;
418 
419         case PROPERTY_ID_FONT_PITCH:
420 			_rValue >>= m_aFont.Pitch;
421             break;
422 
423         case PROPERTY_ID_FONT_TYPE:
424 			_rValue >>= m_aFont.Type;
425             break;
426 
427         case PROPERTY_ID_FONT_WIDTH:
428 			_rValue >>= m_aFont.Width;
429             break;
430 
431         case PROPERTY_ID_FONT_HEIGHT:
432         {
433             float nHeight = 0;
434             _rValue >>= nHeight;
435 			m_aFont.Height = (sal_Int16)nHeight;
436         }
437 		break;
438 
439         case PROPERTY_ID_FONT_WEIGHT:
440 			_rValue >>= m_aFont.Weight;
441 			break;
442 
443         case PROPERTY_ID_FONT_SLANT:
444             _rValue >>= m_aFont.Slant;
445 			break;
446 
447         case PROPERTY_ID_FONT_UNDERLINE:
448 			_rValue >>= m_aFont.Underline;
449 			break;
450 
451         case PROPERTY_ID_FONT_STRIKEOUT:
452 			_rValue >>= m_aFont.Strikeout;
453 			break;
454 
455         case PROPERTY_ID_FONT_WORDLINEMODE:
456         {
457             sal_Bool bWordLineMode = sal_False;
458             _rValue >>= bWordLineMode;
459 			m_aFont.WordLineMode = bWordLineMode;
460         }
461         break;
462 
463         default:
464             DBG_ERROR( "FontControlModel::setFastPropertyValue_NoBroadcast: invalid property!" );
465         }
466     }
467 
468     //------------------------------------------------------------------------------
469     Any FontControlModel::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
470     {
471         Any aReturn;
472         // some defaults which are the same, not matter if we have toolkit-compatible
473         // defaults or not
474         bool bHandled = false;
475         switch( _nHandle )
476         {
477 		case PROPERTY_ID_TEXTCOLOR:
478 		case PROPERTY_ID_TEXTLINECOLOR:
479             // void
480             bHandled = true;
481             break;
482 
483         case PROPERTY_ID_FONTEMPHASISMARK:
484 			aReturn <<= FontEmphasisMark::NONE;
485             bHandled = true;
486 			break;
487 
488 		case PROPERTY_ID_FONTRELIEF:
489 			aReturn <<= FontRelief::NONE;
490             bHandled = true;
491 			break;
492         }
493         if ( bHandled )
494             return aReturn;
495 
496         if ( m_bToolkitCompatibleDefaults )
497         {
498             EmptyFontDescriptor aEmpty;
499             if ( PROPERTY_ID_FONT == _nHandle )
500                 return makeAny( (FontDescriptor)aEmpty );
501             return lcl_extractFontDescriptorAggregate( _nHandle, aEmpty );
502         }
503 
504         switch( _nHandle )
505         {
506 		case PROPERTY_ID_FONT:
507 			aReturn <<= ::comphelper::getDefaultFont();
508 			break;
509 
510 		case PROPERTY_ID_FONT_WORDLINEMODE:
511 			aReturn = makeBoolAny(sal_False);
512 
513 		case PROPERTY_ID_FONT_NAME:
514 		case PROPERTY_ID_FONT_STYLENAME:
515 			aReturn <<= ::rtl::OUString();
516 
517 		case PROPERTY_ID_FONT_FAMILY:
518 		case PROPERTY_ID_FONT_CHARSET:
519 		case PROPERTY_ID_FONT_SLANT:
520 		case PROPERTY_ID_FONT_UNDERLINE:
521 		case PROPERTY_ID_FONT_STRIKEOUT:
522 			aReturn <<= (sal_Int16)1;
523             break;
524 
525         case PROPERTY_ID_FONT_KERNING:
526 			aReturn = makeBoolAny(sal_False);
527             break;
528 
529         case PROPERTY_ID_FONT_PITCH:
530         case PROPERTY_ID_FONT_TYPE:
531         case PROPERTY_ID_FONT_WIDTH:
532 			aReturn <<= (sal_Int16)0;
533             break;
534 
535         case PROPERTY_ID_FONT_HEIGHT:
536 		case PROPERTY_ID_FONT_WEIGHT:
537         case PROPERTY_ID_FONT_CHARWIDTH:
538         case PROPERTY_ID_FONT_ORIENTATION:
539 			aReturn <<= (float)0;
540 			break;
541 
542         default:
543             DBG_ERROR( "FontControlModel::getPropertyDefaultByHandle: invalid property!" );
544         }
545 
546         return aReturn;
547     }
548 
549 //.........................................................................
550 }   // namespace frm
551 //.........................................................................
552