1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <frm_strings.hxx>
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 <sal/log.hxx>
28 #include <toolkit/helper/emptyfontdescriptor.hxx>
29 #include <com/sun/star/awt/FontRelief.hpp>
30 #include <com/sun/star/awt/FontEmphasisMark.hpp>
31 #include <com/sun/star/beans/PropertyAttribute.hpp>
38 using namespace ::comphelper
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::awt
;
41 using namespace ::com::sun::star::lang
;
42 using namespace ::com::sun::star::beans
;
47 Any
lcl_extractFontDescriptorAggregate( sal_Int32 _nHandle
, const FontDescriptor
& _rFont
)
52 case PROPERTY_ID_FONT_NAME
:
53 aValue
<<= _rFont
.Name
;
56 case PROPERTY_ID_FONT_STYLENAME
:
57 aValue
<<= _rFont
.StyleName
;
60 case PROPERTY_ID_FONT_FAMILY
:
61 aValue
<<= _rFont
.Family
;
64 case PROPERTY_ID_FONT_CHARSET
:
65 aValue
<<= _rFont
.CharSet
;
68 case PROPERTY_ID_FONT_CHARWIDTH
:
69 aValue
<<= _rFont
.CharacterWidth
;
72 case PROPERTY_ID_FONT_KERNING
:
73 aValue
<<= _rFont
.Kerning
;
76 case PROPERTY_ID_FONT_ORIENTATION
:
77 aValue
<<= _rFont
.Orientation
;
80 case PROPERTY_ID_FONT_PITCH
:
81 aValue
<<= _rFont
.Pitch
;
84 case PROPERTY_ID_FONT_TYPE
:
85 aValue
<<= _rFont
.Type
;
88 case PROPERTY_ID_FONT_WIDTH
:
89 aValue
<<= _rFont
.Width
;
92 case PROPERTY_ID_FONT_HEIGHT
:
93 aValue
<<= static_cast<float>( _rFont
.Height
);
96 case PROPERTY_ID_FONT_WEIGHT
:
97 aValue
<<= _rFont
.Weight
;
100 case PROPERTY_ID_FONT_SLANT
:
101 aValue
<<= _rFont
.Slant
;
104 case PROPERTY_ID_FONT_UNDERLINE
:
105 aValue
<<= _rFont
.Underline
;
108 case PROPERTY_ID_FONT_STRIKEOUT
:
109 aValue
<<= _rFont
.Strikeout
;
112 case PROPERTY_ID_FONT_WORDLINEMODE
:
113 aValue
<<= _rFont
.WordLineMode
;
117 OSL_FAIL( "lcl_extractFontDescriptorAggregate: invalid handle!" );
124 FontControlModel::FontControlModel( bool _bToolkitCompatibleDefaults
)
125 :m_nFontRelief( css::awt::FontRelief::NONE
)
126 ,m_nFontEmphasis( css::awt::FontEmphasisMark::NONE
)
127 ,m_bToolkitCompatibleDefaults( _bToolkitCompatibleDefaults
)
132 FontControlModel::FontControlModel( const FontControlModel
* _pOriginal
)
134 m_aFont
= _pOriginal
->m_aFont
;
135 m_nFontRelief
= _pOriginal
->m_nFontRelief
;
136 m_nFontEmphasis
= _pOriginal
->m_nFontEmphasis
;
137 m_aTextLineColor
= _pOriginal
->m_aTextLineColor
;
138 m_aTextColor
= _pOriginal
->m_aTextColor
;
139 m_bToolkitCompatibleDefaults
= _pOriginal
->m_bToolkitCompatibleDefaults
;
143 bool FontControlModel::isFontRelatedProperty( sal_Int32 _nPropertyHandle
)
145 return isFontAggregateProperty( _nPropertyHandle
)
146 || ( _nPropertyHandle
== PROPERTY_ID_FONT
)
147 || ( _nPropertyHandle
== PROPERTY_ID_FONTEMPHASISMARK
)
148 || ( _nPropertyHandle
== PROPERTY_ID_FONTRELIEF
)
149 || ( _nPropertyHandle
== PROPERTY_ID_TEXTLINECOLOR
)
150 || ( _nPropertyHandle
== PROPERTY_ID_TEXTCOLOR
);
154 bool FontControlModel::isFontAggregateProperty( sal_Int32 _nPropertyHandle
)
156 return ( _nPropertyHandle
== PROPERTY_ID_FONT_CHARWIDTH
)
157 || ( _nPropertyHandle
== PROPERTY_ID_FONT_ORIENTATION
)
158 || ( _nPropertyHandle
== PROPERTY_ID_FONT_WIDTH
)
159 || ( _nPropertyHandle
== PROPERTY_ID_FONT_NAME
)
160 || ( _nPropertyHandle
== PROPERTY_ID_FONT_STYLENAME
)
161 || ( _nPropertyHandle
== PROPERTY_ID_FONT_FAMILY
)
162 || ( _nPropertyHandle
== PROPERTY_ID_FONT_CHARSET
)
163 || ( _nPropertyHandle
== PROPERTY_ID_FONT_HEIGHT
)
164 || ( _nPropertyHandle
== PROPERTY_ID_FONT_WEIGHT
)
165 || ( _nPropertyHandle
== PROPERTY_ID_FONT_SLANT
)
166 || ( _nPropertyHandle
== PROPERTY_ID_FONT_UNDERLINE
)
167 || ( _nPropertyHandle
== PROPERTY_ID_FONT_STRIKEOUT
)
168 || ( _nPropertyHandle
== PROPERTY_ID_FONT_WORDLINEMODE
)
169 || ( _nPropertyHandle
== PROPERTY_ID_FONT_PITCH
)
170 || ( _nPropertyHandle
== PROPERTY_ID_FONT_KERNING
)
171 || ( _nPropertyHandle
== PROPERTY_ID_FONT_TYPE
);
175 Color
FontControlModel::getTextColor( ) const
177 Color nColor
= COL_TRANSPARENT
;
178 m_aTextColor
>>= nColor
;
183 Color
FontControlModel::getTextLineColor( ) const
185 Color nColor
= COL_TRANSPARENT
;
186 m_aTextLineColor
>>= nColor
;
191 void FontControlModel::describeFontRelatedProperties( Sequence
< Property
>& /* [out] */ _rProps
)
193 sal_Int32 nPos
= _rProps
.getLength();
194 _rProps
.realloc( nPos
+ 21 );
195 Property
* pProperties
= _rProps
.getArray();
197 *pProperties
++ = css::beans::Property(PROPERTY_FONT
, PROPERTY_ID_FONT
, cppu::UnoType
<FontDescriptor
>::get(), css::beans::PropertyAttribute::BOUND
| css::beans::PropertyAttribute::MAYBEDEFAULT
);
198 *pProperties
++ = css::beans::Property(PROPERTY_FONTEMPHASISMARK
, PROPERTY_ID_FONTEMPHASISMARK
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::BOUND
| css::beans::PropertyAttribute::MAYBEDEFAULT
);
199 *pProperties
++ = css::beans::Property(PROPERTY_FONTRELIEF
, PROPERTY_ID_FONTRELIEF
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::BOUND
| css::beans::PropertyAttribute::MAYBEDEFAULT
);
200 *pProperties
++ = css::beans::Property(PROPERTY_TEXTCOLOR
, PROPERTY_ID_TEXTCOLOR
, cppu::UnoType
<sal_Int32
>::get(), css::beans::PropertyAttribute::BOUND
| css::beans::PropertyAttribute::MAYBEDEFAULT
| css::beans::PropertyAttribute::MAYBEVOID
);
201 *pProperties
++ = css::beans::Property(PROPERTY_TEXTLINECOLOR
, PROPERTY_ID_TEXTLINECOLOR
, cppu::UnoType
<sal_Int32
>::get(), css::beans::PropertyAttribute::BOUND
| css::beans::PropertyAttribute::MAYBEDEFAULT
| css::beans::PropertyAttribute::MAYBEVOID
);
203 *pProperties
++ = css::beans::Property(PROPERTY_FONT_CHARWIDTH
, PROPERTY_ID_FONT_CHARWIDTH
, cppu::UnoType
<float>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
204 *pProperties
++ = css::beans::Property(PROPERTY_FONT_KERNING
, PROPERTY_ID_FONT_KERNING
, cppu::UnoType
<bool>::get(),
205 css::beans::PropertyAttribute::MAYBEDEFAULT
);
206 *pProperties
++ = css::beans::Property(PROPERTY_FONT_ORIENTATION
, PROPERTY_ID_FONT_ORIENTATION
, cppu::UnoType
<float>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
207 *pProperties
++ = css::beans::Property(PROPERTY_FONT_PITCH
, PROPERTY_ID_FONT_PITCH
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
208 *pProperties
++ = css::beans::Property(PROPERTY_FONT_TYPE
, PROPERTY_ID_FONT_TYPE
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
209 *pProperties
++ = css::beans::Property(PROPERTY_FONT_WIDTH
, PROPERTY_ID_FONT_WIDTH
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
210 *pProperties
++ = css::beans::Property(PROPERTY_FONT_NAME
, PROPERTY_ID_FONT_NAME
, cppu::UnoType
<OUString
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
211 *pProperties
++ = css::beans::Property(PROPERTY_FONT_STYLENAME
, PROPERTY_ID_FONT_STYLENAME
, cppu::UnoType
<OUString
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
212 *pProperties
++ = css::beans::Property(PROPERTY_FONT_FAMILY
, PROPERTY_ID_FONT_FAMILY
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
213 *pProperties
++ = css::beans::Property(PROPERTY_FONT_CHARSET
, PROPERTY_ID_FONT_CHARSET
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
214 *pProperties
++ = css::beans::Property(PROPERTY_FONT_HEIGHT
, PROPERTY_ID_FONT_HEIGHT
, cppu::UnoType
<float>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
215 *pProperties
++ = css::beans::Property(PROPERTY_FONT_WEIGHT
, PROPERTY_ID_FONT_WEIGHT
, cppu::UnoType
<float>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
216 *pProperties
++ = css::beans::Property(PROPERTY_FONT_SLANT
, PROPERTY_ID_FONT_SLANT
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
217 *pProperties
++ = css::beans::Property(PROPERTY_FONT_UNDERLINE
, PROPERTY_ID_FONT_UNDERLINE
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
218 *pProperties
++ = css::beans::Property(PROPERTY_FONT_STRIKEOUT
, PROPERTY_ID_FONT_STRIKEOUT
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::MAYBEDEFAULT
);
219 *pProperties
++ = css::beans::Property(PROPERTY_FONT_WORDLINEMODE
, PROPERTY_ID_FONT_WORDLINEMODE
, cppu::UnoType
<bool>::get(),
220 css::beans::PropertyAttribute::MAYBEDEFAULT
);
224 void FontControlModel::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
228 case PROPERTY_ID_TEXTCOLOR
:
229 _rValue
= m_aTextColor
;
232 case PROPERTY_ID_FONTEMPHASISMARK
:
233 _rValue
<<= m_nFontEmphasis
;
236 case PROPERTY_ID_FONTRELIEF
:
237 _rValue
<<= m_nFontRelief
;
240 case PROPERTY_ID_TEXTLINECOLOR
:
241 _rValue
= m_aTextLineColor
;
244 case PROPERTY_ID_FONT
:
249 _rValue
= lcl_extractFontDescriptorAggregate( _nHandle
, m_aFont
);
255 bool FontControlModel::convertFastPropertyValue( Any
& _rConvertedValue
, Any
& _rOldValue
,
256 sal_Int32 _nHandle
, const Any
& _rValue
)
258 bool bModified
= false;
261 case PROPERTY_ID_TEXTCOLOR
:
262 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aTextColor
, cppu::UnoType
<sal_Int32
>::get() );
265 case PROPERTY_ID_TEXTLINECOLOR
:
266 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aTextLineColor
, cppu::UnoType
<sal_Int32
>::get() );
269 case PROPERTY_ID_FONTEMPHASISMARK
:
270 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_nFontEmphasis
);
273 case PROPERTY_ID_FONTRELIEF
:
274 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_nFontRelief
);
277 case PROPERTY_ID_FONT
:
279 Any
aWorkAroundGccLimitation( m_aFont
);
280 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, aWorkAroundGccLimitation
, cppu::UnoType
<decltype(m_aFont
)>::get() );
284 case PROPERTY_ID_FONT_NAME
:
285 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Name
);
288 case PROPERTY_ID_FONT_STYLENAME
:
289 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.StyleName
);
292 case PROPERTY_ID_FONT_FAMILY
:
293 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Family
);
296 case PROPERTY_ID_FONT_CHARSET
:
297 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.CharSet
);
300 case PROPERTY_ID_FONT_CHARWIDTH
:
301 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.CharacterWidth
);
304 case PROPERTY_ID_FONT_KERNING
:
305 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Kerning
);
308 case PROPERTY_ID_FONT_ORIENTATION
:
309 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Orientation
);
312 case PROPERTY_ID_FONT_PITCH
:
313 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Pitch
);
316 case PROPERTY_ID_FONT_TYPE
:
317 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Type
);
320 case PROPERTY_ID_FONT_WIDTH
:
321 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Width
);
324 case PROPERTY_ID_FONT_HEIGHT
:
325 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, float( m_aFont
.Height
) );
328 case PROPERTY_ID_FONT_WEIGHT
:
329 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Weight
);
332 case PROPERTY_ID_FONT_SLANT
:
333 bModified
= tryPropertyValueEnum( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Slant
);
336 case PROPERTY_ID_FONT_UNDERLINE
:
337 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Underline
);
340 case PROPERTY_ID_FONT_STRIKEOUT
:
341 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.Strikeout
);
344 case PROPERTY_ID_FONT_WORDLINEMODE
:
345 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_aFont
.WordLineMode
);
349 OSL_FAIL( "FontControlModel::convertFastPropertyValue: no font aggregate!" );
354 /// @throws Exception
355 static void setFastPropertyValue_NoBroadcast_implimpl(
356 FontDescriptor
& rFont
,
357 sal_Int32 nHandle
, const Any
& rValue
)
361 case PROPERTY_ID_FONT_NAME
:
362 rValue
>>= rFont
.Name
;
365 case PROPERTY_ID_FONT_STYLENAME
:
366 rValue
>>= rFont
.StyleName
;
369 case PROPERTY_ID_FONT_FAMILY
:
370 rValue
>>= rFont
.Family
;
373 case PROPERTY_ID_FONT_CHARSET
:
374 rValue
>>= rFont
.CharSet
;
377 case PROPERTY_ID_FONT_CHARWIDTH
:
378 rValue
>>= rFont
.CharacterWidth
;
381 case PROPERTY_ID_FONT_KERNING
:
382 rValue
>>= rFont
.Kerning
;
385 case PROPERTY_ID_FONT_ORIENTATION
:
386 rValue
>>= rFont
.Orientation
;
389 case PROPERTY_ID_FONT_PITCH
:
390 rValue
>>= rFont
.Pitch
;
393 case PROPERTY_ID_FONT_TYPE
:
394 rValue
>>= rFont
.Type
;
397 case PROPERTY_ID_FONT_WIDTH
:
398 rValue
>>= rFont
.Width
;
401 case PROPERTY_ID_FONT_HEIGHT
:
405 rFont
.Height
= static_cast<sal_Int16
>(nHeight
);
409 case PROPERTY_ID_FONT_WEIGHT
:
410 rValue
>>= rFont
.Weight
;
413 case PROPERTY_ID_FONT_SLANT
:
414 rValue
>>= rFont
.Slant
;
417 case PROPERTY_ID_FONT_UNDERLINE
:
418 rValue
>>= rFont
.Underline
;
421 case PROPERTY_ID_FONT_STRIKEOUT
:
422 rValue
>>= rFont
.Strikeout
;
425 case PROPERTY_ID_FONT_WORDLINEMODE
:
427 bool bWordLineMode
= false;
428 rValue
>>= bWordLineMode
;
429 rFont
.WordLineMode
= bWordLineMode
;
434 assert(false); // isFontAggregateProperty
438 void FontControlModel::setFastPropertyValue_NoBroadcast_impl(
439 ::cppu::OPropertySetHelper
& rBase
,
440 void (::cppu::OPropertySetHelper::*pSet
)(sal_Int32
, Any
const&),
441 sal_Int32 nHandle
, const Any
& rValue
)
443 if (isFontAggregateProperty(nHandle
))
445 // need to fire an event for PROPERTY_ID_FONT too apparently, so:
446 FontDescriptor
font(getFont());
448 // first set new value on backup copy
449 setFastPropertyValue_NoBroadcast_implimpl(font
, nHandle
, rValue
);
451 // then set that as the actual property - will eventually call
452 // this method recursively again...
453 (rBase
.*pSet
)(PROPERTY_ID_FONT
, Any(font
));
455 // verify that the nHandle property has the new value
457 getFastPropertyValue(tmp
, nHandle
);
458 assert(tmp
== rValue
|| PROPERTY_ID_FONT_HEIGHT
== nHandle
/*rounded*/);
465 case PROPERTY_ID_TEXTCOLOR
:
466 m_aTextColor
= rValue
;
469 case PROPERTY_ID_TEXTLINECOLOR
:
470 m_aTextLineColor
= rValue
;
473 case PROPERTY_ID_FONTEMPHASISMARK
:
474 rValue
>>= m_nFontEmphasis
;
477 case PROPERTY_ID_FONTRELIEF
:
478 rValue
>>= m_nFontRelief
;
481 case PROPERTY_ID_FONT
:
486 SAL_WARN("forms.component", "invalid property: " << nHandle
);
492 Any
FontControlModel::getPropertyDefaultByHandle( sal_Int32 _nHandle
) const
495 // some defaults which are the same, not matter if we have toolkit-compatible
497 bool bHandled
= false;
500 case PROPERTY_ID_TEXTCOLOR
:
501 case PROPERTY_ID_TEXTLINECOLOR
:
506 case PROPERTY_ID_FONTEMPHASISMARK
:
507 aReturn
<<= css::awt::FontEmphasisMark::NONE
;
511 case PROPERTY_ID_FONTRELIEF
:
512 aReturn
<<= css::awt::FontRelief::NONE
;
519 if ( m_bToolkitCompatibleDefaults
)
521 EmptyFontDescriptor aEmpty
;
522 if ( PROPERTY_ID_FONT
== _nHandle
)
523 return Any( FontDescriptor(aEmpty
) );
524 return lcl_extractFontDescriptorAggregate( _nHandle
, aEmpty
);
529 case PROPERTY_ID_FONT
:
530 aReturn
<<= ::comphelper::getDefaultFont();
533 case PROPERTY_ID_FONT_WORDLINEMODE
:
537 case PROPERTY_ID_FONT_NAME
:
538 case PROPERTY_ID_FONT_STYLENAME
:
539 aReturn
<<= OUString();
542 case PROPERTY_ID_FONT_FAMILY
:
543 case PROPERTY_ID_FONT_CHARSET
:
544 case PROPERTY_ID_FONT_SLANT
:
545 case PROPERTY_ID_FONT_UNDERLINE
:
546 case PROPERTY_ID_FONT_STRIKEOUT
:
547 aReturn
<<= sal_Int16(1);
550 case PROPERTY_ID_FONT_KERNING
:
554 case PROPERTY_ID_FONT_PITCH
:
555 case PROPERTY_ID_FONT_TYPE
:
556 case PROPERTY_ID_FONT_WIDTH
:
557 aReturn
<<= sal_Int16(0);
560 case PROPERTY_ID_FONT_HEIGHT
:
561 case PROPERTY_ID_FONT_WEIGHT
:
562 case PROPERTY_ID_FONT_CHARWIDTH
:
563 case PROPERTY_ID_FONT_ORIENTATION
:
564 aReturn
<<= float(0);
568 OSL_FAIL( "FontControlModel::getPropertyDefaultByHandle: invalid property!" );
578 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */