Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef SVTOOLS_DIALOGCONTROLLING_HXX
30 : : #define SVTOOLS_DIALOGCONTROLLING_HXX
31 : :
32 : : #include <svtools/svtdllapi.h>
33 : :
34 : : #include <tools/link.hxx>
35 : : #include <vcl/button.hxx>
36 : :
37 : : #include <vector>
38 : : #include <boost/shared_ptr.hpp>
39 : :
40 : : //........................................................................
41 : : namespace svt
42 : : {
43 : : //........................................................................
44 : :
45 : : //=====================================================================
46 : : //= IWindowOperator
47 : : //=====================================================================
48 : : /** an abstract interface for operating on a ->Window
49 : : */
50 : 0 : class SVT_DLLPUBLIC SAL_NO_VTABLE IWindowOperator
51 : : {
52 : : public:
53 : : /** called when an event happened which should be reacted to
54 : :
55 : : @param _rTrigger
56 : : the event which triggered the call. If the Id of the event is 0, then this is the initial
57 : : call which is made when ->_rOperateOn is added to the responsibility of the DialogController.
58 : : @param _rOperateOn
59 : : the window on which to operate
60 : : */
61 : : virtual void operateOn( const VclWindowEvent& _rTrigger, Window& _rOperateOn ) const = 0;
62 : :
63 : : virtual ~IWindowOperator();
64 : : };
65 : : typedef ::boost::shared_ptr< IWindowOperator > PWindowOperator;
66 : :
67 : : //=====================================================================
68 : : //= IWindowEventFilter
69 : : //=====================================================================
70 : : /** an abstract interface for deciding whether a ->VclWindowEvent
71 : : is worth paying attention to
72 : : */
73 : 0 : class SVT_DLLPUBLIC SAL_NO_VTABLE IWindowEventFilter
74 : : {
75 : : public:
76 : : virtual bool payAttentionTo( const VclWindowEvent& _rEvent ) const = 0;
77 : :
78 : : virtual ~IWindowEventFilter();
79 : : };
80 : : typedef ::boost::shared_ptr< IWindowEventFilter > PWindowEventFilter;
81 : :
82 : : //=====================================================================
83 : : //= DialogController
84 : : //=====================================================================
85 : : struct DialogController_Data;
86 : : /** a class controlling interactions between dialog controls
87 : :
88 : : An instance of this class listens to all events fired by a certain
89 : : ->Control (more precise, a ->Window), the so-called instigator.
90 : :
91 : : Additionally, the ->DialogController maintains a list of windows which
92 : : are affected by changes in the instigator window. Let's call those the
93 : : dependent windows.
94 : :
95 : : Now, by help of an owner-provided ->IWindowEventFilter, the ->DialogController
96 : : decides which events are worth attention. By help of an owner-provided
97 : : ->IWindowOperator, it handles those events for all dependent windows.
98 : : */
99 : : class SVT_DLLPUBLIC DialogController
100 : : {
101 : : private:
102 : : ::std::auto_ptr< DialogController_Data > m_pImpl;
103 : :
104 : : public:
105 : : DialogController( Window& _rInstigator, const PWindowEventFilter& _pEventFilter, const PWindowOperator& _pOperator );
106 : : virtual ~DialogController();
107 : :
108 : : /** adds a window to the list of dependent windows
109 : :
110 : : @param _rWindow
111 : : The window to add to the list of dependent windows.
112 : :
113 : : The caller is responsible for life-time control: The given window
114 : : must live at least as long as the ->DialogController instance does.
115 : : */
116 : : void addDependentWindow( Window& _rWindow );
117 : :
118 : : /** resets the controller so that no actions happend anymore.
119 : :
120 : : The instances is disfunctional after this method has been called.
121 : : */
122 : : void reset();
123 : :
124 : : private:
125 : : void impl_Init();
126 : : void impl_updateAll( const VclWindowEvent& _rTriggerEvent );
127 : : void impl_update( const VclWindowEvent& _rTriggerEvent, Window& _rWindow );
128 : :
129 : : DECL_LINK( OnWindowEvent, const VclWindowEvent* );
130 : :
131 : : private:
132 : : DialogController( const DialogController& ); // never implemented
133 : : DialogController& operator=( const DialogController& ); // never implemented
134 : : };
135 : : typedef ::boost::shared_ptr< DialogController > PDialogController;
136 : :
137 : : //=====================================================================
138 : : //= ControlDependencyManager
139 : : //=====================================================================
140 : : struct ControlDependencyManager_Data;
141 : : /** helper class for managing control dependencies
142 : :
143 : : Instances of this class are intended to be held as members of a dialog/tabpage/whatever
144 : : class, with easy administration of inter-control dependencies (such as "Enable
145 : : control X if and only if checkbox Y is checked).
146 : : */
147 : : class SVT_DLLPUBLIC ControlDependencyManager
148 : : {
149 : : private:
150 : : ::std::auto_ptr< ControlDependencyManager_Data > m_pImpl;
151 : :
152 : : public:
153 : : ControlDependencyManager();
154 : : ~ControlDependencyManager();
155 : :
156 : : /** clears all dialog controllers previously added to the manager
157 : : */
158 : : void clear();
159 : :
160 : : /** ensures that a given window is enabled or disabled, according to the check state
161 : : of a given radio button
162 : : @param _rRadio
163 : : denotes the radio button whose check state is to observe
164 : : @param _rDependentWindow
165 : : denotes the window which should be enabled when ->_rRadio is checked, and
166 : : disabled when it's unchecked
167 : : */
168 : : void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow );
169 : : void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3 );
170 : : void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5 );
171 : :
172 : : /** ensures that a given window is enabled or disabled, according to the mark state
173 : : of a given check box
174 : : @param _rBox
175 : : denotes the check box whose mark state is to observe
176 : : @param _rDependentWindow
177 : : denotes the window which should be enabled when ->_rBox is marked, and
178 : : disabled when it's unmarked
179 : : */
180 : : void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow );
181 : : void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2 );
182 : : void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4 );
183 : :
184 : : /** adds a non-standard controller whose functionality is not covered by the other methods
185 : :
186 : : @param _pController
187 : : the controller to add to the manager. Must not be <NULL/>.
188 : : */
189 : : void addController( const PDialogController& _pController );
190 : :
191 : : private:
192 : : ControlDependencyManager( const ControlDependencyManager& ); // never implemented
193 : : ControlDependencyManager& operator=( const ControlDependencyManager& ); // never implemented
194 : : };
195 : :
196 : : //=====================================================================
197 : : //= EnableOnCheck
198 : : //=====================================================================
199 : : /** a helper class implementing the ->IWindowOperator interface,
200 : : which enables a dependent window depending on the check state of
201 : : an instigator window.
202 : :
203 : : @see DialogController
204 : : */
205 : : template< class CHECKABLE >
206 [ # # ][ # # ]: 0 : class SVT_DLLPUBLIC EnableOnCheck : public IWindowOperator
207 : : {
208 : : public:
209 : : typedef CHECKABLE SourceType;
210 : :
211 : : private:
212 : : SourceType& m_rCheckable;
213 : :
214 : : public:
215 : : /** constructs the instance
216 : :
217 : : @param _rCheckable
218 : : a ->Window instance which supports a boolean method IsChecked. Usually
219 : : a ->RadioButton or ->CheckBox
220 : : */
221 : 0 : EnableOnCheck( SourceType& _rCheckable )
222 : 0 : :m_rCheckable( _rCheckable )
223 : : {
224 : 0 : }
225 : :
226 : 0 : virtual void operateOn( const VclWindowEvent& /*_rTrigger*/, Window& _rOperateOn ) const
227 : : {
228 : 0 : _rOperateOn.Enable( m_rCheckable.IsChecked() );
229 : 0 : }
230 : : };
231 : :
232 : : //=====================================================================
233 : : //= FilterForRadioOrCheckToggle
234 : : //=====================================================================
235 : : /** a helper class implementing the ->IWindowEventFilter interface,
236 : : which filters for radio buttons or check boxes being toggled.
237 : :
238 : : Technically, the class simply filters for the ->VCLEVENT_RADIOBUTTON_TOGGLE
239 : : and the ->VCLEVENT_CHECKBOX_TOGGLE event.
240 : : */
241 [ # # ]: 0 : class SVT_DLLPUBLIC FilterForRadioOrCheckToggle : public IWindowEventFilter
242 : : {
243 : : const Window& m_rWindow;
244 : : public:
245 : 0 : FilterForRadioOrCheckToggle( const Window& _rWindow )
246 : 0 : :m_rWindow( _rWindow )
247 : : {
248 : 0 : }
249 : :
250 : 0 : bool payAttentionTo( const VclWindowEvent& _rEvent ) const
251 : : {
252 [ # # # # : 0 : if ( ( _rEvent.GetWindow() == &m_rWindow )
# # ][ # # ]
253 : 0 : && ( ( _rEvent.GetId() == VCLEVENT_RADIOBUTTON_TOGGLE )
254 : 0 : || ( _rEvent.GetId() == VCLEVENT_CHECKBOX_TOGGLE )
255 : : )
256 : : )
257 : 0 : return true;
258 : 0 : return false;
259 : : }
260 : : };
261 : :
262 : : //=====================================================================
263 : : //= RadioDependentEnabler
264 : : //=====================================================================
265 : : /** a ->DialogController derivee which enables or disables its dependent windows,
266 : : depending on the check state of a radio button.
267 : :
268 : : The usage of this class is as simple as
269 : : <code>
270 : : pController = new RadioDependentEnabler( m_aOptionSelectSomething );
271 : : pController->addDependentWindow( m_aLabelSelection );
272 : : pController->addDependentWindow( m_aListSelection );
273 : : </code>
274 : :
275 : : With this, both <code>m_aLabelSelection</code> and <code>m_aListSelection</code> will
276 : : be disabled if and only <code>m_aOptionSelectSomething</code> is checked.
277 : : */
278 [ # # ]: 0 : class SVT_DLLPUBLIC RadioDependentEnabler : public DialogController
279 : : {
280 : : public:
281 : 0 : RadioDependentEnabler( RadioButton& _rButton )
282 : : :DialogController( _rButton,
283 [ # # ]: 0 : PWindowEventFilter( new FilterForRadioOrCheckToggle( _rButton ) ),
284 [ # # ][ # # ]: 0 : PWindowOperator( new EnableOnCheck< RadioButton >( _rButton ) ) )
[ # # ][ # # ]
[ # # ]
285 : : {
286 : 0 : }
287 : :
288 : 0 : RadioDependentEnabler( CheckBox& _rBox )
289 : : :DialogController( _rBox,
290 [ # # ]: 0 : PWindowEventFilter( new FilterForRadioOrCheckToggle( _rBox ) ),
291 [ # # ][ # # ]: 0 : PWindowOperator( new EnableOnCheck< CheckBox >( _rBox ) ) )
[ # # ][ # # ]
[ # # ]
292 : : {
293 : 0 : }
294 : : };
295 : :
296 : : //........................................................................
297 : : } // namespace svt
298 : : //........................................................................
299 : :
300 : : #endif // SVTOOLS_DIALOGCONTROLLING_HXX
301 : :
302 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|