bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / formcontrolfont.cxx
blobc188a079fbae9706c1b3b4bc27effb016dee3be1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "formcontrolfont.hxx"
21 #include "property.hrc"
22 #include "property.hxx"
23 #include <cppuhelper/propshlp.hxx>
24 #include <comphelper/property.hxx>
25 #include <comphelper/types.hxx>
26 #include <tools/color.hxx>
27 #include <toolkit/helper/emptyfontdescriptor.hxx>
28 #include <com/sun/star/awt/FontRelief.hpp>
29 #include <com/sun/star/awt/FontEmphasisMark.hpp>
32 namespace frm
36 using namespace ::comphelper;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::awt;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::beans;
43 namespace
45 Any lcl_extractFontDescriptorAggregate( sal_Int32 _nHandle, const FontDescriptor& _rFont )
47 Any aValue;
48 switch ( _nHandle )
50 case PROPERTY_ID_FONT_NAME:
51 aValue <<= _rFont.Name;
52 break;
54 case PROPERTY_ID_FONT_STYLENAME:
55 aValue <<= _rFont.StyleName;
56 break;
58 case PROPERTY_ID_FONT_FAMILY:
59 aValue <<= (sal_Int16)_rFont.Family;
60 break;
62 case PROPERTY_ID_FONT_CHARSET:
63 aValue <<= (sal_Int16)_rFont.CharSet;
64 break;
66 case PROPERTY_ID_FONT_CHARWIDTH:
67 aValue <<= _rFont.CharacterWidth;
68 break;
70 case PROPERTY_ID_FONT_KERNING:
71 aValue <<= _rFont.Kerning;
72 break;
74 case PROPERTY_ID_FONT_ORIENTATION:
75 aValue <<= _rFont.Orientation;
76 break;
78 case PROPERTY_ID_FONT_PITCH:
79 aValue <<= _rFont.Pitch;
80 break;
82 case PROPERTY_ID_FONT_TYPE:
83 aValue <<= _rFont.Type;
84 break;
86 case PROPERTY_ID_FONT_WIDTH:
87 aValue <<= _rFont.Width;
88 break;
90 case PROPERTY_ID_FONT_HEIGHT:
91 aValue <<= (float)( _rFont.Height );
92 break;
94 case PROPERTY_ID_FONT_WEIGHT:
95 aValue <<= (float)_rFont.Weight;
96 break;
98 case PROPERTY_ID_FONT_SLANT:
99 aValue = makeAny(_rFont.Slant);
100 break;
102 case PROPERTY_ID_FONT_UNDERLINE:
103 aValue <<= (sal_Int16)_rFont.Underline;
104 break;
106 case PROPERTY_ID_FONT_STRIKEOUT:
107 aValue <<= (sal_Int16)_rFont.Strikeout;
108 break;
110 case PROPERTY_ID_FONT_WORDLINEMODE:
111 aValue = makeAny( _rFont.WordLineMode );
112 break;
114 default:
115 OSL_FAIL( "lcl_extractFontDescriptorAggregate: invalid handle!" );
116 break;
118 return aValue;
122 FontControlModel::FontControlModel( bool _bToolkitCompatibleDefaults )
123 :m_nFontRelief( css::awt::FontRelief::NONE )
124 ,m_nFontEmphasis( FontEmphasisMark::NONE )
125 ,m_bToolkitCompatibleDefaults( _bToolkitCompatibleDefaults )
130 FontControlModel::FontControlModel( const FontControlModel* _pOriginal )
132 m_aFont = _pOriginal->m_aFont;
133 m_nFontRelief = _pOriginal->m_nFontRelief;
134 m_nFontEmphasis = _pOriginal->m_nFontEmphasis;
135 m_aTextLineColor = _pOriginal->m_aTextLineColor;
136 m_aTextColor = _pOriginal->m_aTextColor;
137 m_bToolkitCompatibleDefaults = _pOriginal->m_bToolkitCompatibleDefaults;
141 bool FontControlModel::isFontRelatedProperty( sal_Int32 _nPropertyHandle )
143 return isFontAggregateProperty( _nPropertyHandle )
144 || ( _nPropertyHandle == PROPERTY_ID_FONT )
145 || ( _nPropertyHandle == PROPERTY_ID_FONTEMPHASISMARK )
146 || ( _nPropertyHandle == PROPERTY_ID_FONTRELIEF )
147 || ( _nPropertyHandle == PROPERTY_ID_TEXTLINECOLOR )
148 || ( _nPropertyHandle == PROPERTY_ID_TEXTCOLOR );
152 bool FontControlModel::isFontAggregateProperty( sal_Int32 _nPropertyHandle )
154 return ( _nPropertyHandle == PROPERTY_ID_FONT_CHARWIDTH )
155 || ( _nPropertyHandle == PROPERTY_ID_FONT_ORIENTATION )
156 || ( _nPropertyHandle == PROPERTY_ID_FONT_WIDTH )
157 || ( _nPropertyHandle == PROPERTY_ID_FONT_NAME )
158 || ( _nPropertyHandle == PROPERTY_ID_FONT_STYLENAME )
159 || ( _nPropertyHandle == PROPERTY_ID_FONT_FAMILY )
160 || ( _nPropertyHandle == PROPERTY_ID_FONT_CHARSET )
161 || ( _nPropertyHandle == PROPERTY_ID_FONT_HEIGHT )
162 || ( _nPropertyHandle == PROPERTY_ID_FONT_WEIGHT )
163 || ( _nPropertyHandle == PROPERTY_ID_FONT_SLANT )
164 || ( _nPropertyHandle == PROPERTY_ID_FONT_UNDERLINE )
165 || ( _nPropertyHandle == PROPERTY_ID_FONT_STRIKEOUT )
166 || ( _nPropertyHandle == PROPERTY_ID_FONT_WORDLINEMODE )
167 || ( _nPropertyHandle == PROPERTY_ID_FONT_PITCH )
168 || ( _nPropertyHandle == PROPERTY_ID_FONT_KERNING )
169 || ( _nPropertyHandle == PROPERTY_ID_FONT_TYPE );
173 sal_Int32 FontControlModel::getTextColor( ) const
175 sal_Int32 nColor = COL_TRANSPARENT;
176 m_aTextColor >>= nColor;
177 return nColor;
181 sal_Int32 FontControlModel::getTextLineColor( ) const
183 sal_Int32 nColor = COL_TRANSPARENT;
184 m_aTextLineColor >>= nColor;
185 return nColor;
189 void FontControlModel::describeFontRelatedProperties( Sequence< Property >& /* [out] */ _rProps)
191 sal_Int32 nPos = _rProps.getLength();
192 _rProps.realloc( nPos + 21 );
193 Property* pProperties = _rProps.getArray();
195 DECL_PROP2 ( FONT, FontDescriptor, BOUND, MAYBEDEFAULT );
196 DECL_PROP2 ( FONTEMPHASISMARK, sal_Int16, BOUND, MAYBEDEFAULT );
197 DECL_PROP2 ( FONTRELIEF, sal_Int16, BOUND, MAYBEDEFAULT );
198 DECL_PROP3 ( TEXTCOLOR, sal_Int32, BOUND, MAYBEDEFAULT, MAYBEVOID );
199 DECL_PROP3 ( TEXTLINECOLOR, sal_Int32, BOUND, MAYBEDEFAULT, MAYBEVOID );
201 DECL_PROP1 ( FONT_CHARWIDTH, float, MAYBEDEFAULT );
202 DECL_BOOL_PROP1 ( FONT_KERNING, MAYBEDEFAULT );
203 DECL_PROP1 ( FONT_ORIENTATION, float, MAYBEDEFAULT );
204 DECL_PROP1 ( FONT_PITCH, sal_Int16, MAYBEDEFAULT );
205 DECL_PROP1 ( FONT_TYPE, sal_Int16, MAYBEDEFAULT );
206 DECL_PROP1 ( FONT_WIDTH, sal_Int16, MAYBEDEFAULT );
207 DECL_PROP1 ( FONT_NAME, OUString, MAYBEDEFAULT );
208 DECL_PROP1 ( FONT_STYLENAME, OUString, MAYBEDEFAULT );
209 DECL_PROP1 ( FONT_FAMILY, sal_Int16, MAYBEDEFAULT );
210 DECL_PROP1 ( FONT_CHARSET, sal_Int16, MAYBEDEFAULT );
211 DECL_PROP1 ( FONT_HEIGHT, float, MAYBEDEFAULT );
212 DECL_PROP1 ( FONT_WEIGHT, float, MAYBEDEFAULT );
213 DECL_PROP1 ( FONT_SLANT, sal_Int16, MAYBEDEFAULT );
214 DECL_PROP1 ( FONT_UNDERLINE, sal_Int16, MAYBEDEFAULT );
215 DECL_PROP1 ( FONT_STRIKEOUT, sal_Int16, MAYBEDEFAULT );
216 DECL_BOOL_PROP1 ( FONT_WORDLINEMODE, MAYBEDEFAULT );
220 void FontControlModel::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
222 switch( _nHandle )
224 case PROPERTY_ID_TEXTCOLOR:
225 _rValue = m_aTextColor;
226 break;
228 case PROPERTY_ID_FONTEMPHASISMARK:
229 _rValue <<= m_nFontEmphasis;
230 break;
232 case PROPERTY_ID_FONTRELIEF:
233 _rValue <<= m_nFontRelief;
234 break;
236 case PROPERTY_ID_TEXTLINECOLOR:
237 _rValue = m_aTextLineColor;
238 break;
240 case PROPERTY_ID_FONT:
241 _rValue = makeAny( m_aFont );
242 break;
244 default:
245 _rValue = lcl_extractFontDescriptorAggregate( _nHandle, m_aFont );
246 break;
251 bool FontControlModel::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue,
252 sal_Int32 _nHandle, const Any& _rValue ) throw( IllegalArgumentException )
254 bool bModified = false;
255 switch( _nHandle )
257 case PROPERTY_ID_TEXTCOLOR:
258 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aTextColor, cppu::UnoType<sal_Int32>::get() );
259 break;
261 case PROPERTY_ID_TEXTLINECOLOR:
262 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aTextLineColor, cppu::UnoType<sal_Int32>::get() );
263 break;
265 case PROPERTY_ID_FONTEMPHASISMARK:
266 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_nFontEmphasis );
267 break;
269 case PROPERTY_ID_FONTRELIEF:
270 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_nFontRelief );
271 break;
273 case PROPERTY_ID_FONT:
275 Any aWorkAroundGccLimitation = makeAny( m_aFont );
276 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, aWorkAroundGccLimitation, cppu::UnoType<decltype(m_aFont)>::get() );
278 break;
280 case PROPERTY_ID_FONT_NAME:
281 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aFont.Name );
282 break;
284 case PROPERTY_ID_FONT_STYLENAME:
285 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aFont.StyleName );
286 break;
288 case PROPERTY_ID_FONT_FAMILY:
289 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Family );
290 break;
292 case PROPERTY_ID_FONT_CHARSET:
293 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.CharSet );
294 break;
296 case PROPERTY_ID_FONT_CHARWIDTH:
297 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, float( m_aFont.CharacterWidth ) );
298 break;
300 case PROPERTY_ID_FONT_KERNING:
301 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aFont.Kerning );
302 break;
304 case PROPERTY_ID_FONT_ORIENTATION:
305 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, float( m_aFont.Orientation ) );
306 break;
308 case PROPERTY_ID_FONT_PITCH:
309 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Pitch );
310 break;
312 case PROPERTY_ID_FONT_TYPE:
313 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Type );
314 break;
316 case PROPERTY_ID_FONT_WIDTH:
317 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Width );
318 break;
320 case PROPERTY_ID_FONT_HEIGHT:
321 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, float( m_aFont.Height ) );
322 break;
324 case PROPERTY_ID_FONT_WEIGHT:
325 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aFont.Weight );
326 break;
328 case PROPERTY_ID_FONT_SLANT:
329 bModified = tryPropertyValueEnum( _rConvertedValue, _rOldValue, _rValue, m_aFont.Slant );
330 break;
332 case PROPERTY_ID_FONT_UNDERLINE:
333 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Underline );
334 break;
336 case PROPERTY_ID_FONT_STRIKEOUT:
337 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_aFont.Strikeout );
338 break;
340 case PROPERTY_ID_FONT_WORDLINEMODE:
341 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aFont.WordLineMode );
342 break;
344 default:
345 OSL_FAIL( "FontControlModel::convertFastPropertyValue: no font aggregate!" );
347 return bModified;
351 static void setFastPropertyValue_NoBroadcast_implimpl(
352 FontDescriptor & rFont,
353 sal_Int32 nHandle, const Any& rValue) throw (Exception)
355 switch (nHandle)
357 case PROPERTY_ID_FONT_NAME:
358 rValue >>= rFont.Name;
359 break;
361 case PROPERTY_ID_FONT_STYLENAME:
362 rValue >>= rFont.StyleName;
363 break;
365 case PROPERTY_ID_FONT_FAMILY:
366 rValue >>= rFont.Family;
367 break;
369 case PROPERTY_ID_FONT_CHARSET:
370 rValue >>= rFont.CharSet;
371 break;
373 case PROPERTY_ID_FONT_CHARWIDTH:
374 rValue >>= rFont.CharacterWidth;
375 break;
377 case PROPERTY_ID_FONT_KERNING:
378 rValue >>= rFont.Kerning;
379 break;
381 case PROPERTY_ID_FONT_ORIENTATION:
382 rValue >>= rFont.Orientation;
383 break;
385 case PROPERTY_ID_FONT_PITCH:
386 rValue >>= rFont.Pitch;
387 break;
389 case PROPERTY_ID_FONT_TYPE:
390 rValue >>= rFont.Type;
391 break;
393 case PROPERTY_ID_FONT_WIDTH:
394 rValue >>= rFont.Width;
395 break;
397 case PROPERTY_ID_FONT_HEIGHT:
399 float nHeight = 0;
400 rValue >>= nHeight;
401 rFont.Height = (sal_Int16)nHeight;
403 break;
405 case PROPERTY_ID_FONT_WEIGHT:
406 rValue >>= rFont.Weight;
407 break;
409 case PROPERTY_ID_FONT_SLANT:
410 rValue >>= rFont.Slant;
411 break;
413 case PROPERTY_ID_FONT_UNDERLINE:
414 rValue >>= rFont.Underline;
415 break;
417 case PROPERTY_ID_FONT_STRIKEOUT:
418 rValue >>= rFont.Strikeout;
419 break;
421 case PROPERTY_ID_FONT_WORDLINEMODE:
423 bool bWordLineMode = false;
424 rValue >>= bWordLineMode;
425 rFont.WordLineMode = bWordLineMode;
427 break;
429 default:
430 assert(false); // isFontAggregateProperty
434 void FontControlModel::setFastPropertyValue_NoBroadcast_impl(
435 ::cppu::OPropertySetHelper & rBase,
436 void (::cppu::OPropertySetHelper::*pSet)(sal_Int32, Any const&),
437 sal_Int32 nHandle, const Any& rValue) throw (Exception)
439 if (isFontAggregateProperty(nHandle))
441 // need to fire a event for PROPERTY_ID_FONT too apparently, so:
442 FontDescriptor font(getFont());
444 // first set new value on backup copy
445 setFastPropertyValue_NoBroadcast_implimpl(font, nHandle, rValue);
447 // then set that as the actual property - will eventually call
448 // this method recursively again...
449 (rBase.*pSet)(PROPERTY_ID_FONT, makeAny(font));
450 #ifndef NDEBUG
451 // verify that the nHandle property has the new value
452 Any tmp;
453 getFastPropertyValue(tmp, nHandle);
454 assert(tmp == rValue || PROPERTY_ID_FONT_HEIGHT == nHandle /*rounded*/);
455 #endif
457 else
459 switch (nHandle)
461 case PROPERTY_ID_TEXTCOLOR:
462 m_aTextColor = rValue;
463 break;
465 case PROPERTY_ID_TEXTLINECOLOR:
466 m_aTextLineColor = rValue;
467 break;
469 case PROPERTY_ID_FONTEMPHASISMARK:
470 rValue >>= m_nFontEmphasis;
471 break;
473 case PROPERTY_ID_FONTRELIEF:
474 rValue >>= m_nFontRelief;
475 break;
477 case PROPERTY_ID_FONT:
478 rValue >>= m_aFont;
479 break;
481 default:
482 SAL_WARN("forms.component", "invalid property: " << nHandle);
488 Any FontControlModel::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
490 Any aReturn;
491 // some defaults which are the same, not matter if we have toolkit-compatible
492 // defaults or not
493 bool bHandled = false;
494 switch( _nHandle )
496 case PROPERTY_ID_TEXTCOLOR:
497 case PROPERTY_ID_TEXTLINECOLOR:
498 // void
499 bHandled = true;
500 break;
502 case PROPERTY_ID_FONTEMPHASISMARK:
503 aReturn <<= FontEmphasisMark::NONE;
504 bHandled = true;
505 break;
507 case PROPERTY_ID_FONTRELIEF:
508 aReturn <<= css::awt::FontRelief::NONE;
509 bHandled = true;
510 break;
512 if ( bHandled )
513 return aReturn;
515 if ( m_bToolkitCompatibleDefaults )
517 EmptyFontDescriptor aEmpty;
518 if ( PROPERTY_ID_FONT == _nHandle )
519 return makeAny( FontDescriptor(aEmpty) );
520 return lcl_extractFontDescriptorAggregate( _nHandle, aEmpty );
523 switch( _nHandle )
525 case PROPERTY_ID_FONT:
526 aReturn <<= ::comphelper::getDefaultFont();
527 break;
529 case PROPERTY_ID_FONT_WORDLINEMODE:
530 aReturn = makeBoolAny(false);
531 break;
533 case PROPERTY_ID_FONT_NAME:
534 case PROPERTY_ID_FONT_STYLENAME:
535 aReturn <<= OUString();
536 break;
538 case PROPERTY_ID_FONT_FAMILY:
539 case PROPERTY_ID_FONT_CHARSET:
540 case PROPERTY_ID_FONT_SLANT:
541 case PROPERTY_ID_FONT_UNDERLINE:
542 case PROPERTY_ID_FONT_STRIKEOUT:
543 aReturn <<= (sal_Int16)1;
544 break;
546 case PROPERTY_ID_FONT_KERNING:
547 aReturn = makeBoolAny(false);
548 break;
550 case PROPERTY_ID_FONT_PITCH:
551 case PROPERTY_ID_FONT_TYPE:
552 case PROPERTY_ID_FONT_WIDTH:
553 aReturn <<= (sal_Int16)0;
554 break;
556 case PROPERTY_ID_FONT_HEIGHT:
557 case PROPERTY_ID_FONT_WEIGHT:
558 case PROPERTY_ID_FONT_CHARWIDTH:
559 case PROPERTY_ID_FONT_ORIENTATION:
560 aReturn <<= (float)0;
561 break;
563 default:
564 OSL_FAIL( "FontControlModel::getPropertyDefaultByHandle: invalid property!" );
567 return aReturn;
571 } // namespace frm
574 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */