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