update dev300-m57
[ooovba.git] / svtools / inc / dialogcontrolling.hxx
blobfec4b1220ad08a22ade68fcc31099dd692de2fbc
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 virtual void operateOn( Window& _rOperateOn ) const = 0;
57 virtual ~IWindowOperator();
59 typedef ::boost::shared_ptr< IWindowOperator > PWindowOperator;
61 //=====================================================================
62 //= IWindowEventFilter
63 //=====================================================================
64 /** an abstract interface for deciding whether a ->VclSimpleEvent
65 is worth paying attention to
67 class SVT_DLLPUBLIC SAL_NO_VTABLE IWindowEventFilter
69 public:
70 virtual bool payAttentionTo( const VclSimpleEvent& _rEvent ) const = 0;
72 virtual ~IWindowEventFilter();
74 typedef ::boost::shared_ptr< IWindowEventFilter > PWindowEventFilter;
76 //=====================================================================
77 //= DialogController
78 //=====================================================================
79 struct DialogController_Data;
80 /** a class controlling interactions between dialog controls
82 An instance of this class listens to all events fired by a certain
83 ->Control (more precise, a ->Window), the so-called instigator.
85 Additionally, the ->DialogController maintains a list of windows which
86 are affected by changes in the instigator window. Let's call those the
87 dependent windows.
89 Now, by help of an owner-provided ->IWindowEventFilter, the ->DialogController
90 decides which events are worth attention. By help of an owner-provided
91 ->IWindowOperator, it handles those events for all dependent windows.
93 class SVT_DLLPUBLIC DialogController
95 private:
96 ::std::auto_ptr< DialogController_Data > m_pImpl;
98 public:
99 DialogController( Window& _rInstigator, const PWindowEventFilter _pEventFilter, const PWindowOperator _pOperator );
100 virtual ~DialogController();
102 /** adds a window to the list of dependent windows
104 @param _rWindow
105 The window to add to the list of dependent windows.
107 The caller is responsible for life-time control: The given window
108 must live at least as long as the ->DialogController instance does.
110 void addDependentWindow( Window& _rWindow );
112 /** resets the controller so that no actions happend anymore.
114 The instances is disfunctional after this method has been called.
116 void reset();
118 private:
119 void impl_Init();
120 void impl_updateAll();
121 void impl_update( Window& _rWindow );
123 DECL_LINK( OnWindowEvent, const VclSimpleEvent* );
125 private:
126 DialogController( const DialogController& ); // never implemented
127 DialogController& operator=( const DialogController& ); // never implemented
129 typedef ::boost::shared_ptr< DialogController > PDialogController;
131 //=====================================================================
132 //= ControlDependencyManager
133 //=====================================================================
134 struct ControlDependencyManager_Data;
135 /** helper class for managing control dependencies
137 Instances of this class are intended to be held as members of a dialog/tabpage/whatever
138 class, with easy administration of inter-control dependencies (such as "Enable
139 control X if and only if checkbox Y is checked).
141 class SVT_DLLPUBLIC ControlDependencyManager
143 private:
144 ::std::auto_ptr< ControlDependencyManager_Data > m_pImpl;
146 public:
147 ControlDependencyManager();
148 ~ControlDependencyManager();
150 /** clears all dialog controllers previously added to the manager
152 void clear();
154 /** ensures that a given window is enabled or disabled, according to the check state
155 of a given radio button
156 @param _rRadio
157 denotes the radio button whose check state is to observe
158 @param _rDependentWindow
159 denotes the window which should be enabled when ->_rRadio is checked, and
160 disabled when it's unchecked
162 void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow );
163 void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2 );
164 void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3 );
165 void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4 );
166 void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5 );
167 void enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5, Window& _rDependentWindow6 );
169 /** ensures that a given window is enabled or disabled, according to the mark state
170 of a given check box
171 @param _rBox
172 denotes the check box whose mark state is to observe
173 @param _rDependentWindow
174 denotes the window which should be enabled when ->_rBox is marked, and
175 disabled when it's unmarked
177 void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow );
178 void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2 );
179 void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3 );
180 void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4 );
181 void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5 );
182 void enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5, Window& _rDependentWindow6 );
184 /** adds a non-standard controller whose functionality is not covered by the other methods
186 @param _pController
187 the controller to add to the manager. Must not be <NULL/>. The manager takes ownership
188 of the controller.
190 void addController( DialogController* _pController );
192 private:
193 ControlDependencyManager( const ControlDependencyManager& ); // never implemented
194 ControlDependencyManager& operator=( const ControlDependencyManager& ); // never implemented
197 //=====================================================================
198 //= EnableOnCheck
199 //=====================================================================
200 /** a helper class implementing the ->IWindowOperator interface,
201 which enables a dependent window depending on the check state of
202 an instigator window.
204 @see DialogController
206 template< class CHECKABLE >
207 class SVT_DLLPUBLIC EnableOnCheck : public IWindowOperator
209 public:
210 typedef CHECKABLE SourceType;
212 private:
213 SourceType& m_rCheckable;
215 public:
216 /** constructs the instance
218 @param _rCheckable
219 a ->Window instance which supports a boolean method IsChecked. Usually
220 a ->RadioButton or ->CheckBox
222 EnableOnCheck( SourceType& _rCheckable )
223 :m_rCheckable( _rCheckable )
227 virtual void operateOn( Window& _rOperateOn ) const
229 _rOperateOn.Enable( m_rCheckable.IsChecked() );
233 //=====================================================================
234 //= FilterForRadioOrCheckToggle
235 //=====================================================================
236 /** a helper class implementing the ->IWindowEventFilter interface,
237 which filters for radio buttons or check boxes being toggled.
239 Technically, the class simply filters for the ->VCLEVENT_RADIOBUTTON_TOGGLE
240 and the ->VCLEVENT_CHECKBOX_TOGGLE event.
242 class SVT_DLLPUBLIC FilterForRadioOrCheckToggle : public IWindowEventFilter
244 const Window& m_rWindow;
245 public:
246 FilterForRadioOrCheckToggle( const Window& _rWindow )
247 :m_rWindow( _rWindow )
251 bool payAttentionTo( const VclSimpleEvent& _rEvent ) const
253 const VclWindowEvent& rWindowEvent = dynamic_cast< const VclWindowEvent& >( _rEvent );
254 if ( ( rWindowEvent.GetWindow() == &m_rWindow )
255 && ( ( rWindowEvent.GetId() == VCLEVENT_RADIOBUTTON_TOGGLE )
256 || ( rWindowEvent.GetId() == VCLEVENT_CHECKBOX_TOGGLE )
259 return true;
260 return false;
264 //=====================================================================
265 //= RadioDependentEnabler
266 //=====================================================================
267 /** a ->DialogController derivee which enables or disables its dependent windows,
268 depending on the check state of a radio button.
270 The usage of this class is as simple as
271 <code>
272 pController = new RadioDependentEnabler( m_aOptionSelectSomething );
273 pController->addDependentWindow( m_aLabelSelection );
274 pController->addDependentWindow( m_aListSelection );
275 </code>
277 With this, both <code>m_aLabelSelection</code> and <code>m_aListSelection</code> will
278 be disabled if and only <code>m_aOptionSelectSomething</code> is checked.
280 class SVT_DLLPUBLIC RadioDependentEnabler : public DialogController
282 public:
283 RadioDependentEnabler( RadioButton& _rButton )
284 :DialogController( _rButton,
285 PWindowEventFilter( new FilterForRadioOrCheckToggle( _rButton ) ),
286 PWindowOperator( new EnableOnCheck< RadioButton >( _rButton ) ) )
290 RadioDependentEnabler( CheckBox& _rBox )
291 :DialogController( _rBox,
292 PWindowEventFilter( new FilterForRadioOrCheckToggle( _rBox ) ),
293 PWindowOperator( new EnableOnCheck< CheckBox >( _rBox ) ) )
298 //........................................................................
299 } // namespace svt
300 //........................................................................
302 #endif // SVTOOLS_DIALOGCONTROLLING_HXX