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 #ifndef SFX_CONTROLWRAPPER_HXX
21 #define SFX_CONTROLWRAPPER_HXX
23 #include <tools/debug.hxx>
24 #include "sal/config.h"
25 #include "sfx2/dllapi.h"
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>
36 // ============================================================================
40 // ============================================================================
42 /** List position type of VCL ListBox. */
43 typedef sal_uInt16 ListBoxPosType
;
44 /** List position type of SVTOOLS ValueSet. */
45 typedef sal_uInt16 ValueSetPosType
;
47 // ============================================================================
49 // ============================================================================
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
>
67 typedef ValueT ValueType
;
68 typedef PosValueMapper
< PosT
, ValueT
> MapperType
;
70 /** A helper struct that contains a list position - value pair. */
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
; }
98 const MapEntryType
* mpMap
; /// The list position/value map.
99 PosT mnNFPos
; /// Special "not found" list position.
102 // ============================================================================
103 // Base control wrapper classes
104 // ============================================================================
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:
115 +- SingleControlWrapper< ControlT, ValueT >
117 | +- DummyWindowWrapper [1]
118 | +- CheckBoxWrapper [1]
120 | +- ColorListBoxWrapper [1]
122 | +- NumericFieldWrapper< ValueT > [1]
124 | | +- [ValueType]NumericFieldWrapper [1] [2]
126 | +- MetricFieldWrapper< ValueT > [1]
128 | | +- [ValueType]MetricFieldWrapper [1] [2]
130 | +- ListBoxWrapper< ValueT > [1]
132 | | +- [ValueType]ListBoxWrapper [1] [2]
134 | +- ValueSetWrapper< ValueT > [1]
136 | +- [ValueType]ValueSetWrapper [1] [2]
138 +- MultiControlWrapperHelper
140 +- MultiControlWrapper< ValueT >
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
149 class SFX2_DLLPUBLIC ControlWrapperBase
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 STATE_DONTKNOW. */
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;
165 /* Disable copy c'tor and assignment. */
166 ControlWrapperBase( const ControlWrapperBase
& );
167 ControlWrapperBase
& operator=( const ControlWrapperBase
& );
170 // ============================================================================
171 // Single control wrappers
172 // ============================================================================
174 /** Base class template for control wrappers containing one single control.
176 Classes created from this template store the reference to a single control
177 object. It is not required that the control is derived from VCL's Window
178 class. Derived classes have to implement the abstract functions
179 ShowControl(), EnableControl(), IsControlDontKnow(), SetControlDontKnow(),
180 GetControlValue(), and SetControlValue().
182 As already stated, it is not required for ControlT to be a VCL Window.
183 Anyway, ControlT must support the following functions:
184 - void ControlT::Enable( bool )
185 - void ControlT::Show( bool )
187 template< typename ControlT
, typename ValueT
>
188 class SingleControlWrapper
: public ControlWrapperBase
191 typedef ControlT ControlType
;
192 typedef ValueT ControlValueType
;
193 typedef SingleControlWrapper
< ControlT
, ValueT
> SingleControlWrapperType
;
195 inline explicit SingleControlWrapper( ControlT
& rControl
) : mrControl( rControl
) {}
197 /** Returns a reference to the control this connection works on. */
198 inline const ControlT
& GetControl() const { return mrControl
; }
199 /** Returns a reference to the control this connection works on. */
200 inline ControlT
& GetControl() { return mrControl
; }
202 /** Enables, disables, shows, or hides the control.
203 @descr Does nothing, if the corresponding parameter is STATE_DONTKNOW. */
204 virtual void ModifyControl( TriState eEnable
, TriState eShow
);
206 /** Derived classes return the value the control contains. */
207 virtual ValueT
GetControlValue() const = 0;
208 /** Derived classes set the contents of the control to the passed value. */
209 virtual void SetControlValue( ValueT aValue
) = 0;
212 ControlT
& mrControl
; /// The control of this wrapper.
215 // ============================================================================
217 /** A dummy wrapper for a VCL Window that does nothing special.
219 This wrapper is used to implement the DummyItemConnection. It does not
220 connect an item to a control, but handles the special flags to disable or
221 hide a control, if an item is unknown.
223 class SFX2_DLLPUBLIC DummyWindowWrapper
:
224 public SingleControlWrapper
< Window
, void* >
227 explicit DummyWindowWrapper( Window
& rWindow
);
229 virtual bool IsControlDontKnow() const;
230 virtual void SetControlDontKnow( bool );
232 virtual void* GetControlValue() const;
233 virtual void SetControlValue( void* );
236 // ----------------------------------------------------------------------------
238 /** A wrapper for the VCL CheckBox. */
239 class SFX2_DLLPUBLIC CheckBoxWrapper
:
240 public SingleControlWrapper
< CheckBox
, sal_Bool
>
243 explicit CheckBoxWrapper( CheckBox
& rCheckBox
);
245 virtual bool IsControlDontKnow() const;
246 virtual void SetControlDontKnow( bool bSet
);
248 virtual sal_Bool
GetControlValue() const;
249 virtual void SetControlValue( sal_Bool bValue
);
252 // ----------------------------------------------------------------------------
254 /** A wrapper for the SVTOOLS ColorListBox. */
255 class SFX2_DLLPUBLIC ColorListBoxWrapper
:
256 public SingleControlWrapper
< ColorListBox
, Color
>
258 /* Note: cannot use 'const Color&' as template argument, because the
259 SVTOOLS ColorListBox returns the color by value and not by reference,
260 therefore GetControlValue() must return a temporary object too. */
262 explicit ColorListBoxWrapper(ColorListBox
& rListBox
);
264 virtual ~ColorListBoxWrapper();
266 virtual bool IsControlDontKnow() const;
267 virtual void SetControlDontKnow( bool bSet
);
269 virtual Color
GetControlValue() const;
270 virtual void SetControlValue( Color aColor
);
273 // ============================================================================
275 /** A wrapper for the VCL NumericField. */
276 template< typename ValueT
>
277 class NumericFieldWrapper
: public SingleControlWrapper
< NumericField
, ValueT
>
280 inline explicit NumericFieldWrapper( NumericField
& rField
) :
281 SingleControlWrapper
< NumericField
, ValueT
>( rField
) {}
283 virtual bool IsControlDontKnow() const;
284 virtual void SetControlDontKnow( bool bSet
);
286 virtual ValueT
GetControlValue() const;
287 virtual void SetControlValue( ValueT nValue
);
290 // ----------------------------------------------------------------------------
292 typedef NumericFieldWrapper
< sal_Int16
> Int16NumericFieldWrapper
;
293 typedef NumericFieldWrapper
< sal_uInt16
> UInt16NumericFieldWrapper
;
294 typedef NumericFieldWrapper
< sal_Int32
> Int32NumericFieldWrapper
;
295 typedef NumericFieldWrapper
< sal_uInt32
> UInt32NumericFieldWrapper
;
297 typedef NumericFieldWrapper
< sal_uInt16
> UShortNumericFieldWrapper
;
298 typedef NumericFieldWrapper
< sal_uIntPtr
> ULongNumericFieldWrapper
;
300 // ============================================================================
302 /** A wrapper for the VCL MetricField.
304 Adds support for field units during accessing the control value. The
305 wrapper respects the field unit set at the control itself and converts it
306 from/to the field unit passed to the constructor.
308 template< typename ValueT
>
309 class MetricFieldWrapper
: public SingleControlWrapper
< MetricField
, ValueT
>
312 inline explicit MetricFieldWrapper( MetricField
& rField
, FieldUnit eUnit
= FUNIT_NONE
) :
313 SingleControlWrapper
< MetricField
, ValueT
>( rField
), meUnit( eUnit
) {}
315 virtual bool IsControlDontKnow() const;
316 virtual void SetControlDontKnow( bool bSet
);
318 virtual ValueT
GetControlValue() const;
319 virtual void SetControlValue( ValueT nValue
);
325 // ----------------------------------------------------------------------------
327 typedef MetricFieldWrapper
< sal_Int16
> Int16MetricFieldWrapper
;
328 typedef MetricFieldWrapper
< sal_uInt16
> UInt16MetricFieldWrapper
;
329 typedef MetricFieldWrapper
< sal_Int32
> Int32MetricFieldWrapper
;
330 typedef MetricFieldWrapper
< sal_uInt32
> UInt32MetricFieldWrapper
;
332 typedef MetricFieldWrapper
< sal_uInt16
> UShortMetricFieldWrapper
;
333 typedef MetricFieldWrapper
< sal_uIntPtr
> ULongMetricFieldWrapper
;
335 // ============================================================================
337 /** A wrapper for the VCL ListBox.
339 If a position<->value map is passed to the constructor, it MUST be
340 terminated with an entry containing LISTBOX_ENTRY_NOTFOUND as list
341 position. See documentation of the PosValueMapper template for details.
343 template< typename ValueT
>
344 class ListBoxWrapper
:
345 public SingleControlWrapper
< ListBox
, ValueT
>,
346 public PosValueMapper
< ListBoxPosType
, ValueT
>
348 typedef PosValueMapper
< ListBoxPosType
, ValueT
> MapperType
;
351 typedef typename
MapperType::MapEntryType MapEntryType
;
353 /** @param pMap Optional list position <-> value map.
354 See PosValueMapper documentation for details. */
355 inline explicit ListBoxWrapper( ListBox
& rListBox
, const MapEntryType
* pMap
= 0 ) :
356 SingleControlWrapper
< ListBox
, ValueT
>( rListBox
), MapperType( LISTBOX_ENTRY_NOTFOUND
, pMap
) {}
358 virtual bool IsControlDontKnow() const
359 { return this->GetControl().GetSelectEntryCount() == 0; }
360 virtual void SetControlDontKnow( bool bSet
)
361 { if( bSet
) this->GetControl().SetNoSelection(); }
363 virtual ValueT
GetControlValue() const;
364 virtual void SetControlValue( ValueT nValue
);
367 // ----------------------------------------------------------------------------
369 typedef ListBoxWrapper
< sal_Int16
> Int16ListBoxWrapper
;
370 typedef ListBoxWrapper
< sal_uInt16
> UInt16ListBoxWrapper
;
371 typedef ListBoxWrapper
< sal_Int32
> Int32ListBoxWrapper
;
372 typedef ListBoxWrapper
< sal_uInt32
> UInt32ListBoxWrapper
;
374 typedef ListBoxWrapper
< sal_uInt16
> UShortListBoxWrapper
;
375 typedef ListBoxWrapper
< sal_uIntPtr
> ULongListBoxWrapper
;
377 // ============================================================================
379 /** A wrapper for the SVTOOLS ValueSet.
381 If a position<->value map is passed to the constructor, it MUST be
382 terminated with an entry containing VALUESET_ITEM_NOTFOUND as list
383 position. See documentation of the PosValueMapper template for details.
385 template< typename ValueT
>
386 class ValueSetWrapper
:
387 public SingleControlWrapper
< ValueSet
, ValueT
>,
388 public PosValueMapper
< ValueSetPosType
, ValueT
>
390 typedef PosValueMapper
< ValueSetPosType
, ValueT
> MapperType
;
393 typedef typename
MapperType::MapEntryType MapEntryType
;
395 /** @param pMap Optional position <-> value map.
396 See PosValueMapper documentation for details. */
397 inline explicit ValueSetWrapper( ValueSet
& rValueSet
, const MapEntryType
* pMap
= 0 ) :
398 SingleControlWrapper
< ValueSet
, ValueT
>( rValueSet
), MapperType( VALUESET_ITEM_NOTFOUND
, pMap
) {}
400 virtual bool IsControlDontKnow() const
401 { return this->GetControl().IsNoSelection(); }
402 virtual void SetControlDontKnow( bool bSet
)
403 { if( bSet
) this->GetControl().SetNoSelection(); }
405 virtual ValueT
GetControlValue() const;
406 virtual void SetControlValue( ValueT nValue
);
409 // ----------------------------------------------------------------------------
411 typedef ValueSetWrapper
< sal_Int16
> Int16ValueSetWrapper
;
412 typedef ValueSetWrapper
< sal_uInt16
> UInt16ValueSetWrapper
;
413 typedef ValueSetWrapper
< sal_Int32
> Int32ValueSetWrapper
;
414 typedef ValueSetWrapper
< sal_uInt32
> UInt32ValueSetWrapper
;
416 typedef ValueSetWrapper
< sal_uInt16
> UShortValueSetWrapper
;
417 typedef ValueSetWrapper
< sal_uIntPtr
> ULongValueSetWrapper
;
419 // ============================================================================
420 // Multi control wrappers
421 // ============================================================================
423 struct MultiControlWrapperHelper_Impl
;
425 /** A container of control wrappers.
427 Derived classes should define control wrapper members and register them in
428 their constructor, using the function RegisterControlWrapper().
430 This wrapper implements the abstract functions of the ControlWrapperBase
431 base class by calling the functions of all registered wrappers.
433 class SFX2_DLLPUBLIC MultiControlWrapperHelper
: public ControlWrapperBase
436 explicit MultiControlWrapperHelper();
437 virtual ~MultiControlWrapperHelper();
439 /** Registers a control wrapper (should be a member of a derived class). */
440 void RegisterControlWrapper( ControlWrapperBase
& rWrapper
);
442 /** Enables, disables, shows, or hides the registered controls. */
443 virtual void ModifyControl( TriState eEnable
, TriState eShow
);
445 /** Returns true if all registered controls are in "don't know" state. */
446 virtual bool IsControlDontKnow() const;
447 /** Sets all registered controls to "don't know" state. */
448 virtual void SetControlDontKnow( bool bSet
);
451 std::auto_ptr
< MultiControlWrapperHelper_Impl
> mxImpl
;
454 // ----------------------------------------------------------------------------
456 /** A multi control wrapper with extended interface.
458 This template class extends the MultiControlWrapperHelper class by the
459 functions GetControlValue() and SetControlValue(), known from the
460 SingleControlWrapper template. This makes it possible to use this template
461 in item connections expecting a single control wrapper. The type ValueT
462 should be able to contain the values of all controls handled in this
463 wrapper. In most cases, the easiest way to achieve this is to use the
464 related item type directly, using the IdentItemWrapper template
467 template< typename ValueT
>
468 class MultiControlWrapper
: public MultiControlWrapperHelper
471 typedef MultiControlWrapperHelper ControlType
;
472 typedef ValueT ControlValueType
;
473 typedef MultiControlWrapper
< ValueT
> MultiControlWrapperType
;
475 MultiControlWrapper() : maDefValue( 0 ){}
477 /** Returns the default value that can be used in GetControlValue(). */
478 inline const ValueT
& GetDefaultValue() const { return maDefValue
; }
479 /** Sets a default value that can be used in GetControlValue(). */
480 inline void SetDefaultValue( const ValueT
& rDefValue
) { maDefValue
= rDefValue
; }
482 /** Derived classes return the value the control contains. */
483 virtual ValueT
GetControlValue() const = 0;
484 /** Derived classes set the contents of the control to the passed value. */
485 virtual void SetControlValue( ValueT aValue
) = 0;
491 // ============================================================================
494 // ============================================================================
495 // *** Implementation of template functions ***
496 // ============================================================================
498 // ============================================================================
500 // ============================================================================
502 template< typename PosT
, typename ValueT
>
503 ValueT PosValueMapper
< PosT
, ValueT
>::GetValueFromPos( PosT nPos
) const
508 const MapEntryType
* pEntry
= mpMap
;
509 while( (pEntry
->mnPos
!= nPos
) && (pEntry
->mnPos
!= mnNFPos
) )
511 nValue
= pEntry
->mnValue
;
513 else /* if( nPos != mnNFPos ) */
515 DBG_ASSERT( nPos
!= mnNFPos
, "sfx2::PosValueMapper< PosT, ValueT >::GetValueFromPos(), previously uninitialized value found!" );
516 nValue
= static_cast< ValueT
>( nPos
);
522 template< typename PosT
, typename ValueT
>
523 PosT PosValueMapper
< PosT
, ValueT
>::GetPosFromValue( ValueT nValue
) const
528 const MapEntryType
* pEntry
= mpMap
;
529 while( (pEntry
->mnValue
!= nValue
) && (pEntry
->mnPos
!= mnNFPos
) )
531 nPos
= pEntry
->mnPos
;
533 else if( nValue
>= 0 )
534 nPos
= static_cast< PosT
>( nValue
);
538 // ============================================================================
539 // Single control wrappers
540 // ============================================================================
542 template< typename ControlT
, typename ValueT
>
543 inline void SingleControlWrapper
< ControlT
, ValueT
>::ModifyControl( TriState eEnable
, TriState eShow
)
545 if( eEnable
!= STATE_DONTKNOW
)
546 mrControl
.Enable( eEnable
== STATE_CHECK
);
547 if( eShow
!= STATE_DONTKNOW
)
548 mrControl
.Show( eShow
== STATE_CHECK
);
551 // ============================================================================
553 template< typename ValueT
>
554 bool NumericFieldWrapper
< ValueT
>::IsControlDontKnow() const
556 return this->GetControl().GetText().Len() == 0;
559 template< typename ValueT
>
560 void NumericFieldWrapper
< ValueT
>::SetControlDontKnow( bool bSet
)
563 this->GetControl().SetText( String() );
566 template< typename ValueT
>
567 ValueT NumericFieldWrapper
< ValueT
>::GetControlValue() const
569 return static_cast< ValueT
>( this->GetControl().Denormalize( this->GetControl().GetValue() ) );
572 template< typename ValueT
>
573 void NumericFieldWrapper
< ValueT
>::SetControlValue( ValueT nValue
)
575 this->GetControl().SetValue( this->GetControl().Normalize( static_cast< sal_Int64
>( nValue
) ) );
578 // ============================================================================
580 template< typename ValueT
>
581 bool MetricFieldWrapper
< ValueT
>::IsControlDontKnow() const
583 return this->GetControl().GetText().isEmpty();
586 template< typename ValueT
>
587 void MetricFieldWrapper
< ValueT
>::SetControlDontKnow( bool bSet
)
590 this->GetControl().SetText( String() );
593 template< typename ValueT
>
594 ValueT MetricFieldWrapper
< ValueT
>::GetControlValue() const
596 return static_cast< ValueT
>( this->GetControl().Denormalize( this->GetControl().GetValue( meUnit
) ) );
599 template< typename ValueT
>
600 void MetricFieldWrapper
< ValueT
>::SetControlValue( ValueT nValue
)
602 this->GetControl().SetValue( this->GetControl().Normalize( static_cast< sal_Int64
>( nValue
) ), meUnit
);
605 // ============================================================================
607 template< typename ValueT
>
608 ValueT ListBoxWrapper
< ValueT
>::GetControlValue() const
610 return this->GetValueFromPos( this->GetControl().GetSelectEntryPos() );
613 template< typename ValueT
>
614 void ListBoxWrapper
< ValueT
>::SetControlValue( ValueT nValue
)
616 sal_uInt16 nPos
= this->GetPosFromValue( nValue
);
617 if( nPos
!= this->GetNotFoundPos() )
618 this->GetControl().SelectEntryPos( nPos
);
621 // ----------------------------------------------------------------------------
623 template< typename ValueT
>
624 ValueT ValueSetWrapper
< ValueT
>::GetControlValue() const
626 return this->GetValueFromPos( this->GetControl().GetSelectItemId() );
629 template< typename ValueT
>
630 void ValueSetWrapper
< ValueT
>::SetControlValue( ValueT nValue
)
632 sal_uInt16 nPos
= this->GetPosFromValue( nValue
);
633 if( nPos
!= this->GetNotFoundPos() )
634 this->GetControl().SelectItem( nPos
);
637 // ============================================================================
644 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */