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 INCLUDED_SVTOOLS_DIALOGCONTROLLING_HXX
21 #define INCLUDED_SVTOOLS_DIALOGCONTROLLING_HXX
23 #include <svtools/svtdllapi.h>
25 #include <tools/link.hxx>
26 #include <vcl/button.hxx>
37 /** an abstract interface for operating on a ->Window
39 class SVT_DLLPUBLIC SAL_NO_VTABLE IWindowOperator
42 /** called when an event happened which should be reacted to
45 the event which triggered the call. If the Id of the event is 0, then this is the initial
46 call which is made when ->_rOperateOn is added to the responsibility of the DialogController.
48 the window on which to operate
50 virtual void operateOn( const VclWindowEvent
& _rTrigger
, vcl::Window
& _rOperateOn
) const = 0;
52 virtual ~IWindowOperator();
54 typedef std::shared_ptr
< IWindowOperator
> PWindowOperator
;
57 //= IWindowEventFilter
59 /** an abstract interface for deciding whether a ->VclWindowEvent
60 is worth paying attention to
62 class SVT_DLLPUBLIC SAL_NO_VTABLE IWindowEventFilter
65 virtual bool payAttentionTo( const VclWindowEvent
& _rEvent
) const = 0;
67 virtual ~IWindowEventFilter();
69 typedef std::shared_ptr
< IWindowEventFilter
> PWindowEventFilter
;
74 struct DialogController_Data
;
75 /** a class controlling interactions between dialog controls
77 An instance of this class listens to all events fired by a certain
78 ->Control (more precise, a ->Window), the so-called instigator.
80 Additionally, the ->DialogController maintains a list of windows which
81 are affected by changes in the instigator window. Let's call those the
84 Now, by help of an owner-provided ->IWindowEventFilter, the ->DialogController
85 decides which events are worth attention. By help of an owner-provided
86 ->IWindowOperator, it handles those events for all dependent windows.
88 class SVT_DLLPUBLIC DialogController
91 ::std::unique_ptr
< DialogController_Data
> m_pImpl
;
94 DialogController( vcl::Window
& _rInstigator
, const PWindowEventFilter
& _pEventFilter
, const PWindowOperator
& _pOperator
);
95 virtual ~DialogController();
97 /** adds a window to the list of dependent windows
100 The window to add to the list of dependent windows.
102 The caller is responsible for life-time control: The given window
103 must live at least as long as the ->DialogController instance does.
105 void addDependentWindow( vcl::Window
& _rWindow
);
107 /** resets the controller so that no actions happened anymore.
109 The instances is disfunctional after this method has been called.
114 void impl_update( const VclWindowEvent
& _rTriggerEvent
, vcl::Window
& _rWindow
);
116 DECL_LINK( OnWindowEvent
, VclWindowEvent
&, void );
119 DialogController( const DialogController
& ) = delete;
120 DialogController
& operator=( const DialogController
& ) = delete;
123 //= ControlDependencyManager
125 struct ControlDependencyManager_Data
;
126 /** helper class for managing control dependencies
128 Instances of this class are intended to be held as members of a dialog/tabpage/whatever
129 class, with easy administration of inter-control dependencies (such as "Enable
130 control X if and only if checkbox Y is checked).
132 class SVT_DLLPUBLIC ControlDependencyManager
135 ::std::unique_ptr
< ControlDependencyManager_Data
> m_pImpl
;
138 ControlDependencyManager();
139 ~ControlDependencyManager();
141 /** ensures that a given window is enabled or disabled, according to the check state
142 of a given radio button
144 denotes the radio button whose check state is to observe
145 @param _rDependentWindow
146 denotes the window which should be enabled when ->_rRadio is checked, and
147 disabled when it's unchecked
149 void enableOnRadioCheck( RadioButton
& _rRadio
, vcl::Window
& _rDependentWindow
);
150 void enableOnRadioCheck( RadioButton
& _rRadio
, vcl::Window
& _rDependentWindow1
, vcl::Window
& _rDependentWindow2
);
151 void enableOnRadioCheck( RadioButton
& _rRadio
, vcl::Window
& _rDependentWindow1
, vcl::Window
& _rDependentWindow2
, vcl::Window
& _rDependentWindow3
);
152 void enableOnRadioCheck( RadioButton
& _rRadio
, vcl::Window
& _rDependentWindow1
, vcl::Window
& _rDependentWindow2
, vcl::Window
& _rDependentWindow3
, vcl::Window
& _rDependentWindow4
, vcl::Window
& _rDependentWindow5
);
154 /** adds a non-standard controller whose functionality is not covered by the other methods
157 the controller to add to the manager. Must not be <NULL/>.
159 void addController( const std::shared_ptr
<DialogController
>& _pController
);
162 ControlDependencyManager( const ControlDependencyManager
& ) = delete;
163 ControlDependencyManager
& operator=( const ControlDependencyManager
& ) = delete;
169 /** a helper class implementing the ->IWindowOperator interface,
170 which enables a dependent window depending on the check state of
171 an instigator window.
173 @see DialogController
175 template< class CHECKABLE
>
176 class SVT_DLLPUBLIC EnableOnCheck
: public IWindowOperator
179 typedef CHECKABLE SourceType
;
182 SourceType
& m_rCheckable
;
185 /** constructs the instance
188 a ->Window instance which supports a boolean method IsChecked. Usually
189 a ->RadioButton or ->CheckBox
191 EnableOnCheck( SourceType
& _rCheckable
)
192 :m_rCheckable( _rCheckable
)
196 virtual void operateOn( const VclWindowEvent
& /*_rTrigger*/, vcl::Window
& _rOperateOn
) const override
198 _rOperateOn
.Enable( m_rCheckable
.IsChecked() );
203 //= FilterForRadioOrCheckToggle
205 /** a helper class implementing the ->IWindowEventFilter interface,
206 which filters for radio buttons or check boxes being toggled.
208 Technically, the class simply filters for the ->VclEventId::RadiobuttonToggle
209 and the ->VclEventId::CheckboxToggle event.
211 class SVT_DLLPUBLIC FilterForRadioOrCheckToggle
: public IWindowEventFilter
213 const vcl::Window
& m_rWindow
;
215 FilterForRadioOrCheckToggle( const vcl::Window
& _rWindow
)
216 :m_rWindow( _rWindow
)
220 bool payAttentionTo( const VclWindowEvent
& _rEvent
) const override
222 if ( ( _rEvent
.GetWindow() == &m_rWindow
)
223 && ( ( _rEvent
.GetId() == VclEventId::RadiobuttonToggle
)
224 || ( _rEvent
.GetId() == VclEventId::CheckboxToggle
)
233 //= RadioDependentEnabler
235 /** a ->DialogController derivee which enables or disables its dependent windows,
236 depending on the check state of a radio button.
238 The usage of this class is as simple as
240 pController = new RadioDependentEnabler( m_aOptionSelectSomething );
241 pController->addDependentWindow( m_aLabelSelection );
242 pController->addDependentWindow( m_aListSelection );
245 With this, both <code>m_aLabelSelection</code> and <code>m_aListSelection</code> will
246 be disabled if and only <code>m_aOptionSelectSomething</code> is checked.
248 class SVT_DLLPUBLIC RadioDependentEnabler
: public DialogController
251 RadioDependentEnabler( RadioButton
& _rButton
)
252 :DialogController( _rButton
,
253 PWindowEventFilter( new FilterForRadioOrCheckToggle( _rButton
) ),
254 PWindowOperator( new EnableOnCheck
< RadioButton
>( _rButton
) ) )
263 #endif // INCLUDED_SVTOOLS_DIALOGCONTROLLING_HXX
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */