update emoji autocorrect entries from po-files
[LibreOffice.git] / include / sfx2 / controlwrapper.hxx
blob42aaab620fbd7d32a6b3c8c30c6f7133159254b4
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 #ifndef INCLUDED_SFX2_CONTROLWRAPPER_HXX
21 #define INCLUDED_SFX2_CONTROLWRAPPER_HXX
23 #include <tools/debug.hxx>
24 #include <sal/config.h>
25 #include <sfx2/dllapi.h>
27 #include <memory>
29 #include <vcl/button.hxx>
30 #include <vcl/edit.hxx>
31 #include <vcl/field.hxx>
32 #include <vcl/lstbox.hxx>
33 #include <svtools/valueset.hxx>
34 #include <svtools/ctrlbox.hxx>
38 namespace sfx {
42 /** List position type of VCL ListBox. */
43 typedef sal_uInt16 ListBoxPosType;
44 /** List position type of SVTOOLS ValueSet. */
45 typedef sal_uInt16 ValueSetPosType;
48 // Helpers
51 /** A helper class for mapping list positions from/to represented values.
53 Deriving from this helper class adds the two functions GetValueFromPos()
54 and GetPosFromValue(). The constructor receives an array of MapEntryType
55 structures that represents the table of positions and values. It is
56 possible to pass a null pointer, this results in a direct mapping between
57 list positions and values. If the map exists, it MUST be terminated with an
58 entry containing the special "not found" list position passed to the
59 constructor. The value contained in this last entry is used as default
60 value in case of an error.
62 template< typename PosT, typename ValueT >
63 class PosValueMapper
65 public:
66 typedef PosT PosType;
67 typedef ValueT ValueType;
68 typedef PosValueMapper< PosT, ValueT > MapperType;
70 /** A helper struct that contains a list position - value pair. */
71 struct MapEntryType
73 PosT mnPos; /// Position in the list.
74 ValueT mnValue; /// Corresponding value.
77 /** Constructs the map helper with the passed map.
78 @param nNFPos This list position is used to represent the
79 "not found" or "not existing" state.
80 @param pMap The map of list positions/values. If 0, a direct mapping
81 is used (simply casting between list position and values). If the map
82 exists, it *MUST* be terminated by an entry containing the special
83 "not found" list position. */
84 inline explicit PosValueMapper( PosT nNFPos, const MapEntryType* pMap = 0 ) :
85 mpMap( pMap ), mnNFPos( nNFPos ) {}
87 /** Returns the value at the specified list position.
88 @return The found value, or the value of the last map entry on error. */
89 ValueT GetValueFromPos( PosT nPos ) const;
90 /** Returns the list position of the specified value.
91 @return The position, or the special "not found" position on error. */
92 PosT GetPosFromValue( ValueT nValue ) const;
94 /** Returns the special "not found" list position. */
95 inline PosT GetNotFoundPos() const { return mnNFPos; }
97 private:
98 const MapEntryType* mpMap; /// The list position/value map.
99 PosT mnNFPos; /// Special "not found" list position.
103 // Base control wrapper classes
106 /** Base class for all control wrappers.
108 Control wrappers are used to have an equal interface for various functions
109 used in connections for different types of controls.
111 The current tree of base classes/templates and standard control wrappers:
113 ControlWrapperBase
115 +- SingleControlWrapper< ControlT, ValueT >
117 | +- DummyWindowWrapper [1]
118 | +- CheckBoxWrapper [1]
119 | +- EditWrapper [1]
120 | +- ColorListBoxWrapper [1]
122 | +- NumericFieldWrapper< ValueT > [1]
123 | | |
124 | | +- [ValueType]NumericFieldWrapper [1] [2]
126 | +- MetricFieldWrapper< ValueT > [1]
127 | | |
128 | | +- [ValueType]MetricFieldWrapper [1] [2]
130 | +- ListBoxWrapper< ValueT > [1]
131 | | |
132 | | +- [ValueType]ListBoxWrapper [1] [2]
134 | +- ValueSetWrapper< ValueT > [1]
136 | +- [ValueType]ValueSetWrapper [1] [2]
138 +- MultiControlWrapperHelper
140 +- MultiControlWrapper< ValueT >
142 Notes:
143 [1] Standard wrappers ready to use.
144 [2] [ValueType] is one of Int16, UInt16, Int32, UInt32, UShort, ULong.
146 See documentation of class ItemConnectionBase (itemconnect.hxx) for more
147 details.
149 class SFX2_DLLPUBLIC ControlWrapperBase
151 public:
152 inline explicit ControlWrapperBase() {}
153 virtual ~ControlWrapperBase();
155 /** Derived classes enable, disable, show, or hide control(s).
156 @descr Will do nothing, if the corresponding parameter is TRISTATE_INDET. */
157 virtual void ModifyControl( TriState eEnable, TriState eShow ) = 0;
159 /** Derived classes return true if the control is in "don't know" state. */
160 virtual bool IsControlDontKnow() const = 0;
161 /** Derived classes set the control to "don't know" state. */
162 virtual void SetControlDontKnow( bool bSet ) = 0;
164 private:
165 ControlWrapperBase( const ControlWrapperBase& ) SAL_DELETED_FUNCTION;
166 ControlWrapperBase& operator=( const ControlWrapperBase& ) SAL_DELETED_FUNCTION;
170 // Single control wrappers
173 /** Base class template for control wrappers containing one single control.
175 Classes created from this template store the reference to a single control
176 object. It is not required that the control is derived from VCL's Window
177 class. Derived classes have to implement the abstract functions
178 ShowControl(), EnableControl(), IsControlDontKnow(), SetControlDontKnow(),
179 GetControlValue(), and SetControlValue().
181 As already stated, it is not required for ControlT to be a VCL Window.
182 Anyway, ControlT must support the following functions:
183 - void ControlT::Enable( bool )
184 - void ControlT::Show( bool )
186 template< typename ControlT, typename ValueT >
187 class SingleControlWrapper : public ControlWrapperBase
189 public:
190 typedef ControlT ControlType;
191 typedef ValueT ControlValueType;
192 typedef SingleControlWrapper< ControlT, ValueT > SingleControlWrapperType;
194 inline explicit SingleControlWrapper( ControlT& rControl ) : mrControl( rControl ) {}
196 /** Returns a reference to the control this connection works on. */
197 inline const ControlT& GetControl() const { return mrControl; }
198 /** Returns a reference to the control this connection works on. */
199 inline ControlT& GetControl() { return mrControl; }
201 /** Enables, disables, shows, or hides the control.
202 @descr Does nothing, if the corresponding parameter is TRISTATE_INDET. */
203 virtual void ModifyControl( TriState eEnable, TriState eShow ) SAL_OVERRIDE;
205 /** Derived classes return the value the control contains. */
206 virtual ValueT GetControlValue() const = 0;
207 /** Derived classes set the contents of the control to the passed value. */
208 virtual void SetControlValue( ValueT aValue ) = 0;
210 private:
211 ControlT& mrControl; /// The control of this wrapper.
216 /** A dummy wrapper for a VCL Window that does nothing special.
218 This wrapper is used to implement the DummyItemConnection. It does not
219 connect an item to a control, but handles the special flags to disable or
220 hide a control, if an item is unknown.
222 class SFX2_DLLPUBLIC DummyWindowWrapper:
223 public SingleControlWrapper< vcl::Window, void* >
225 public:
226 explicit DummyWindowWrapper( vcl::Window& rWindow );
228 virtual bool IsControlDontKnow() const SAL_OVERRIDE;
229 virtual void SetControlDontKnow( bool ) SAL_OVERRIDE;
231 virtual void* GetControlValue() const SAL_OVERRIDE;
232 virtual void SetControlValue( void* ) SAL_OVERRIDE;
237 /** A wrapper for the VCL CheckBox. */
238 class SFX2_DLLPUBLIC CheckBoxWrapper:
239 public SingleControlWrapper< CheckBox, bool >
241 public:
242 explicit CheckBoxWrapper( CheckBox& rCheckBox );
244 virtual bool IsControlDontKnow() const SAL_OVERRIDE;
245 virtual void SetControlDontKnow( bool bSet ) SAL_OVERRIDE;
247 virtual bool GetControlValue() const SAL_OVERRIDE;
248 virtual void SetControlValue( bool bValue ) SAL_OVERRIDE;
253 /** A wrapper for the SVTOOLS ColorListBox. */
254 class SFX2_DLLPUBLIC ColorListBoxWrapper:
255 public SingleControlWrapper< ColorListBox, Color >
257 /* Note: cannot use 'const Color&' as template argument, because the
258 SVTOOLS ColorListBox returns the color by value and not by reference,
259 therefore GetControlValue() must return a temporary object too. */
260 public:
261 explicit ColorListBoxWrapper(ColorListBox & rListBox);
263 virtual ~ColorListBoxWrapper();
265 virtual bool IsControlDontKnow() const SAL_OVERRIDE;
266 virtual void SetControlDontKnow( bool bSet ) SAL_OVERRIDE;
268 virtual Color GetControlValue() const SAL_OVERRIDE;
269 virtual void SetControlValue( Color aColor ) SAL_OVERRIDE;
274 /** A wrapper for the VCL NumericField. */
275 template< typename ValueT >
276 class NumericFieldWrapper : public SingleControlWrapper< NumericField, ValueT >
278 public:
279 inline explicit NumericFieldWrapper( NumericField& rField ) :
280 SingleControlWrapper< NumericField, ValueT >( rField ) {}
282 virtual bool IsControlDontKnow() const;
283 virtual void SetControlDontKnow( bool bSet );
285 virtual ValueT GetControlValue() const;
286 virtual void SetControlValue( ValueT nValue );
291 typedef NumericFieldWrapper< sal_uInt16 > UInt16NumericFieldWrapper;
292 typedef NumericFieldWrapper< sal_uInt32 > UInt32NumericFieldWrapper;
294 typedef NumericFieldWrapper< sal_uInt16 > UShortNumericFieldWrapper;
295 typedef NumericFieldWrapper< sal_uIntPtr > ULongNumericFieldWrapper;
299 /** A wrapper for the VCL MetricField.
301 Adds support for field units during accessing the control value. The
302 wrapper respects the field unit set at the control itself and converts it
303 from/to the field unit passed to the constructor.
305 template< typename ValueT >
306 class MetricFieldWrapper : public SingleControlWrapper< MetricField, ValueT >
308 public:
309 inline explicit MetricFieldWrapper( MetricField& rField, FieldUnit eUnit = FUNIT_NONE ) :
310 SingleControlWrapper< MetricField, ValueT >( rField ), meUnit( eUnit ) {}
312 virtual bool IsControlDontKnow() const;
313 virtual void SetControlDontKnow( bool bSet );
315 virtual ValueT GetControlValue() const;
316 virtual void SetControlValue( ValueT nValue );
318 private:
319 FieldUnit meUnit;
324 typedef MetricFieldWrapper< sal_Int16 > Int16MetricFieldWrapper;
325 typedef MetricFieldWrapper< sal_uInt16 > UInt16MetricFieldWrapper;
326 typedef MetricFieldWrapper< sal_uInt32 > UInt32MetricFieldWrapper;
328 typedef MetricFieldWrapper< sal_uInt16 > UShortMetricFieldWrapper;
329 typedef MetricFieldWrapper< sal_uIntPtr > ULongMetricFieldWrapper;
333 #define WRAPPER_LISTBOX_ENTRY_NOTFOUND 0xFFFF /* XXX was value of LISTBOX_ENTRY_NOTFOUND */
335 /** A wrapper for the VCL ListBox.
337 If a position<->value map is passed to the constructor, it MUST be
338 terminated with an entry containing WRAPPER_LISTBOX_ENTRY_NOTFOUND as list
339 position. See documentation of the PosValueMapper template for details.
341 template< typename ValueT >
342 class ListBoxWrapper :
343 public SingleControlWrapper< ListBox, ValueT >,
344 public PosValueMapper< ListBoxPosType, ValueT >
346 typedef PosValueMapper< ListBoxPosType, ValueT > MapperType;
348 public:
349 typedef typename MapperType::MapEntryType MapEntryType;
351 /** @param pMap Optional list position <-> value map.
352 See PosValueMapper documentation for details. */
353 inline explicit ListBoxWrapper( ListBox& rListBox, const MapEntryType* pMap = 0 ) :
354 SingleControlWrapper< ListBox, ValueT >( rListBox ), MapperType( WRAPPER_LISTBOX_ENTRY_NOTFOUND, pMap ) {}
356 virtual bool IsControlDontKnow() const
357 { return this->GetControl().GetSelectEntryCount() == 0; }
358 virtual void SetControlDontKnow( bool bSet )
359 { if( bSet ) this->GetControl().SetNoSelection(); }
361 virtual ValueT GetControlValue() const;
362 virtual void SetControlValue( ValueT nValue );
367 typedef ListBoxWrapper< sal_uInt16 > UInt16ListBoxWrapper;
368 typedef ListBoxWrapper< sal_uInt32 > UInt32ListBoxWrapper;
370 typedef ListBoxWrapper< sal_uInt16 > UShortListBoxWrapper;
371 typedef ListBoxWrapper< sal_uIntPtr > ULongListBoxWrapper;
375 #define WRAPPER_VALUESET_ITEM_NOTFOUND 0xFFFF /* XXX was value of VALUESET_ITEM_NOTFOUND */
377 /** A wrapper for the SVTOOLS ValueSet.
379 If a position<->value map is passed to the constructor, it MUST be
380 terminated with an entry containing WRAPPER_VALUESET_ITEM_NOTFOUND as list
381 position. See documentation of the PosValueMapper template for details.
383 template< typename ValueT >
384 class ValueSetWrapper :
385 public SingleControlWrapper< ValueSet, ValueT >,
386 public PosValueMapper< ValueSetPosType, ValueT >
388 typedef PosValueMapper< ValueSetPosType, ValueT > MapperType;
390 public:
391 typedef typename MapperType::MapEntryType MapEntryType;
393 /** @param pMap Optional position <-> value map.
394 See PosValueMapper documentation for details. */
395 inline explicit ValueSetWrapper( ValueSet& rValueSet, const MapEntryType* pMap = 0 ) :
396 SingleControlWrapper< ValueSet, ValueT >( rValueSet ), MapperType( WRAPPER_VALUESET_ITEM_NOTFOUND, pMap ) {}
398 virtual bool IsControlDontKnow() const
399 { return this->GetControl().IsNoSelection(); }
400 virtual void SetControlDontKnow( bool bSet )
401 { if( bSet ) this->GetControl().SetNoSelection(); }
403 virtual ValueT GetControlValue() const;
404 virtual void SetControlValue( ValueT nValue );
409 typedef ValueSetWrapper< sal_uInt16 > UInt16ValueSetWrapper;
410 typedef ValueSetWrapper< sal_uInt32 > UInt32ValueSetWrapper;
412 typedef ValueSetWrapper< sal_uInt16 > UShortValueSetWrapper;
413 typedef ValueSetWrapper< sal_uIntPtr > ULongValueSetWrapper;
416 // Multi control wrappers
419 struct MultiControlWrapperHelper_Impl;
421 /** A container of control wrappers.
423 Derived classes should define control wrapper members and register them in
424 their constructor, using the function RegisterControlWrapper().
426 This wrapper implements the abstract functions of the ControlWrapperBase
427 base class by calling the functions of all registered wrappers.
429 class SFX2_DLLPUBLIC MultiControlWrapperHelper : public ControlWrapperBase
431 public:
432 explicit MultiControlWrapperHelper();
433 virtual ~MultiControlWrapperHelper();
435 /** Registers a control wrapper (should be a member of a derived class). */
436 void RegisterControlWrapper( ControlWrapperBase& rWrapper );
438 /** Enables, disables, shows, or hides the registered controls. */
439 virtual void ModifyControl( TriState eEnable, TriState eShow ) SAL_OVERRIDE;
441 /** Returns true if all registered controls are in "don't know" state. */
442 virtual bool IsControlDontKnow() const SAL_OVERRIDE;
443 /** Sets all registered controls to "don't know" state. */
444 virtual void SetControlDontKnow( bool bSet ) SAL_OVERRIDE;
446 private:
447 std::unique_ptr< MultiControlWrapperHelper_Impl > mxImpl;
452 /** A multi control wrapper with extended interface.
454 This template class extends the MultiControlWrapperHelper class by the
455 functions GetControlValue() and SetControlValue(), known from the
456 SingleControlWrapper template. This makes it possible to use this template
457 in item connections expecting a single control wrapper. The type ValueT
458 should be able to contain the values of all controls handled in this
459 wrapper. In most cases, the easiest way to achieve this is to use the
460 related item type directly, using the IdentItemWrapper template
461 (itemwrapper.hxx).
463 template< typename ValueT >
464 class MultiControlWrapper : public MultiControlWrapperHelper
466 public:
467 typedef MultiControlWrapperHelper ControlType;
468 typedef ValueT ControlValueType;
469 typedef MultiControlWrapper< ValueT > MultiControlWrapperType;
471 MultiControlWrapper() : maDefValue( 0 ){}
473 /** Returns the default value that can be used in GetControlValue(). */
474 inline const ValueT& GetDefaultValue() const { return maDefValue; }
475 /** Sets a default value that can be used in GetControlValue(). */
476 inline void SetDefaultValue( const ValueT& rDefValue ) { maDefValue = rDefValue; }
478 /** Derived classes return the value the control contains. */
479 virtual ValueT GetControlValue() const = 0;
480 /** Derived classes set the contents of the control to the passed value. */
481 virtual void SetControlValue( ValueT aValue ) = 0;
483 private:
484 ValueT maDefValue;
491 // *** Implementation of template functions ***
495 // Helpers
498 template< typename PosT, typename ValueT >
499 ValueT PosValueMapper< PosT, ValueT >::GetValueFromPos( PosT nPos ) const
501 ValueT nValue;
502 if( mpMap )
504 const MapEntryType* pEntry = mpMap;
505 while( (pEntry->mnPos != nPos) && (pEntry->mnPos != mnNFPos) )
506 ++pEntry;
507 nValue = pEntry->mnValue;
509 else /* if( nPos != mnNFPos ) */
511 DBG_ASSERT( nPos != mnNFPos, "sfx2::PosValueMapper< PosT, ValueT >::GetValueFromPos(), previously uninitialized value found!" );
512 nValue = static_cast< ValueT >( nPos );
515 return nValue;
518 template< typename PosT, typename ValueT >
519 PosT PosValueMapper< PosT, ValueT >::GetPosFromValue( ValueT nValue ) const
521 PosT nPos = mnNFPos;
522 if( mpMap )
524 const MapEntryType* pEntry = mpMap;
525 while( (pEntry->mnValue != nValue) && (pEntry->mnPos != mnNFPos) )
526 ++pEntry;
527 nPos = pEntry->mnPos;
529 else if( nValue >= 0 )
530 nPos = static_cast< PosT >( nValue );
531 return nPos;
535 // Single control wrappers
538 template< typename ControlT, typename ValueT >
539 inline void SingleControlWrapper< ControlT, ValueT >::ModifyControl( TriState eEnable, TriState eShow )
541 if( eEnable != TRISTATE_INDET )
542 mrControl.Enable( eEnable == TRISTATE_TRUE );
543 if( eShow != TRISTATE_INDET )
544 mrControl.Show( eShow == TRISTATE_TRUE );
549 template< typename ValueT >
550 bool NumericFieldWrapper< ValueT >::IsControlDontKnow() const
552 return this->GetControl().GetText().Len() == 0;
555 template< typename ValueT >
556 void NumericFieldWrapper< ValueT >::SetControlDontKnow( bool bSet )
558 if( bSet )
559 this->GetControl().SetText( OUString() );
562 template< typename ValueT >
563 ValueT NumericFieldWrapper< ValueT >::GetControlValue() const
565 return static_cast< ValueT >( this->GetControl().Denormalize( this->GetControl().GetValue() ) );
568 template< typename ValueT >
569 void NumericFieldWrapper< ValueT >::SetControlValue( ValueT nValue )
571 this->GetControl().SetValue( this->GetControl().Normalize( static_cast< sal_Int64 >( nValue ) ) );
576 template< typename ValueT >
577 bool MetricFieldWrapper< ValueT >::IsControlDontKnow() const
579 return this->GetControl().GetText().isEmpty();
582 template< typename ValueT >
583 void MetricFieldWrapper< ValueT >::SetControlDontKnow( bool bSet )
585 if( bSet )
586 this->GetControl().SetText( OUString() );
589 template< typename ValueT >
590 ValueT MetricFieldWrapper< ValueT >::GetControlValue() const
592 return static_cast< ValueT >( this->GetControl().Denormalize( this->GetControl().GetValue( meUnit ) ) );
595 template< typename ValueT >
596 void MetricFieldWrapper< ValueT >::SetControlValue( ValueT nValue )
598 this->GetControl().SetValue( this->GetControl().Normalize( static_cast< sal_Int64 >( nValue ) ), meUnit );
603 template< typename ValueT >
604 ValueT ListBoxWrapper< ValueT >::GetControlValue() const
606 return this->GetValueFromPos( this->GetControl().GetSelectEntryPos() );
609 template< typename ValueT >
610 void ListBoxWrapper< ValueT >::SetControlValue( ValueT nValue )
612 sal_uInt16 nPos = this->GetPosFromValue( nValue );
613 if( nPos != this->GetNotFoundPos() )
614 this->GetControl().SelectEntryPos( nPos );
619 template< typename ValueT >
620 ValueT ValueSetWrapper< ValueT >::GetControlValue() const
622 return this->GetValueFromPos( this->GetControl().GetSelectItemId() );
625 template< typename ValueT >
626 void ValueSetWrapper< ValueT >::SetControlValue( ValueT nValue )
628 sal_uInt16 nPos = this->GetPosFromValue( nValue );
629 if( nPos != this->GetNotFoundPos() )
630 this->GetControl().SelectItem( nPos );
636 } // namespace sfx
638 #endif
640 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */