1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 #include <svtools/dialogcontrolling.hxx>
31 #include <vcl/window.hxx>
32 
33 #include <algorithm>
34 #include <functional>
35 
36 //........................................................................
37 namespace svt
38 {
39 //........................................................................
40 
41     //=====================================================================
42     //= IWindowOperator
43     //=====================================================================
44 	//---------------------------------------------------------------------
45     IWindowOperator::~IWindowOperator()
46     {
47     }
48 
49     //=====================================================================
50     //= IWindowEventFilter
51     //=====================================================================
52 	//---------------------------------------------------------------------
53     IWindowEventFilter::~IWindowEventFilter()
54     {
55     }
56 
57     //=====================================================================
58     //= DialogController_Data
59     //=====================================================================
60     struct DialogController_Data
61     {
62         Window&                     rInstigator;
63         ::std::vector< Window* >    aConcernedWindows;
64         PWindowEventFilter          pEventFilter;
65         PWindowOperator             pOperator;
66 
67         DialogController_Data( Window& _rInstigator, const PWindowEventFilter _pEventFilter, const PWindowOperator _pOperator )
68             :rInstigator( _rInstigator )
69             ,pEventFilter( _pEventFilter )
70             ,pOperator( _pOperator )
71         {
72         }
73     };
74 
75     //=====================================================================
76     //= DialogController
77     //=====================================================================
78 	//---------------------------------------------------------------------
79     DialogController::DialogController( Window& _rInstigator, const PWindowEventFilter& _pEventFilter,
80             const PWindowOperator& _pOperator )
81         :m_pImpl( new DialogController_Data( _rInstigator, _pEventFilter, _pOperator ) )
82     {
83         DBG_ASSERT( m_pImpl->pEventFilter.get() && m_pImpl->pOperator.get(),
84             "DialogController::DialogController: invalid filter and/or operator!" );
85 
86         m_pImpl->rInstigator.AddEventListener( LINK( this, DialogController, OnWindowEvent ) );
87     }
88 
89     //---------------------------------------------------------------------
90     DialogController::~DialogController()
91     {
92         reset();
93     }
94 
95     //---------------------------------------------------------------------
96     void DialogController::reset()
97     {
98         m_pImpl->rInstigator.RemoveEventListener( LINK( this, DialogController, OnWindowEvent ) );
99         m_pImpl->aConcernedWindows.clear();
100         m_pImpl->pEventFilter.reset();
101         m_pImpl->pOperator.reset();
102     }
103 
104     //---------------------------------------------------------------------
105     void DialogController::addDependentWindow( Window& _rWindow )
106     {
107         m_pImpl->aConcernedWindows.push_back( &_rWindow );
108 
109         VclWindowEvent aEvent( &_rWindow, 0, NULL );
110         impl_update( aEvent, _rWindow );
111     }
112 
113     //---------------------------------------------------------------------
114     IMPL_LINK( DialogController, OnWindowEvent, const VclWindowEvent*, _pEvent )
115     {
116         if ( m_pImpl->pEventFilter->payAttentionTo( *_pEvent ) )
117             impl_updateAll( *_pEvent );
118         return 0L;
119     }
120 
121     //---------------------------------------------------------------------
122     void DialogController::impl_updateAll( const VclWindowEvent& _rTriggerEvent )
123     {
124         for ( ::std::vector< Window* >::iterator loop = m_pImpl->aConcernedWindows.begin();
125                 loop != m_pImpl->aConcernedWindows.end();
126                 ++loop
127             )
128             impl_update( _rTriggerEvent, *(*loop) );
129     }
130 
131     //---------------------------------------------------------------------
132     void DialogController::impl_update( const VclWindowEvent& _rTriggerEvent, Window& _rWindow )
133     {
134         m_pImpl->pOperator->operateOn( _rTriggerEvent, _rWindow );
135     }
136 
137     //=====================================================================
138     //= ControlDependencyManager_Data
139 	//=====================================================================
140     struct ControlDependencyManager_Data
141     {
142         ::std::vector< PDialogController >  aControllers;
143     };
144 
145     //=====================================================================
146     //= ControlDependencyManager
147 	//=====================================================================
148     //---------------------------------------------------------------------
149     ControlDependencyManager::ControlDependencyManager()
150         :m_pImpl( new ControlDependencyManager_Data )
151     {
152     }
153 
154     //---------------------------------------------------------------------
155     ControlDependencyManager::~ControlDependencyManager()
156     {
157     }
158 
159     //---------------------------------------------------------------------
160     namespace
161     {
162         struct ResetDialogController : public ::std::unary_function< const PDialogController&, void >
163         {
164             void operator()( const PDialogController& _pController )
165             {
166                 _pController->reset();
167             }
168         };
169     }
170 
171     //---------------------------------------------------------------------
172     void ControlDependencyManager::clear()
173     {
174         ::std::for_each( m_pImpl->aControllers.begin(), m_pImpl->aControllers.end(), ResetDialogController() );
175         m_pImpl->aControllers.clear();
176     }
177 
178     //---------------------------------------------------------------------
179     void ControlDependencyManager::addController( const PDialogController& _pController )
180     {
181         OSL_ENSURE( _pController.get() != NULL, "ControlDependencyManager::addController: invalid controller, this will crash, sooner or later!" );
182         m_pImpl->aControllers.push_back( _pController );
183     }
184 
185     //---------------------------------------------------------------------
186     void ControlDependencyManager::enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow )
187     {
188         PDialogController pController( new RadioDependentEnabler( _rRadio ) );
189         pController->addDependentWindow( _rDependentWindow );
190         m_pImpl->aControllers.push_back( pController );
191     }
192 
193     //---------------------------------------------------------------------
194     void ControlDependencyManager::enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2 )
195     {
196         PDialogController pController( new RadioDependentEnabler( _rRadio ) );
197         pController->addDependentWindow( _rDependentWindow1 );
198         pController->addDependentWindow( _rDependentWindow2 );
199         m_pImpl->aControllers.push_back( pController );
200     }
201 
202     //---------------------------------------------------------------------
203     void ControlDependencyManager::enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3 )
204     {
205         PDialogController pController( new RadioDependentEnabler( _rRadio ) );
206         pController->addDependentWindow( _rDependentWindow1 );
207         pController->addDependentWindow( _rDependentWindow2 );
208         pController->addDependentWindow( _rDependentWindow3 );
209         m_pImpl->aControllers.push_back( pController );
210     }
211 
212     //---------------------------------------------------------------------
213     void ControlDependencyManager::enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4 )
214     {
215         PDialogController pController( new RadioDependentEnabler( _rRadio ) );
216         pController->addDependentWindow( _rDependentWindow1 );
217         pController->addDependentWindow( _rDependentWindow2 );
218         pController->addDependentWindow( _rDependentWindow3 );
219         pController->addDependentWindow( _rDependentWindow4 );
220         m_pImpl->aControllers.push_back( pController );
221     }
222 
223     //---------------------------------------------------------------------
224     void ControlDependencyManager::enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5 )
225     {
226         PDialogController pController( new RadioDependentEnabler( _rRadio ) );
227         pController->addDependentWindow( _rDependentWindow1 );
228         pController->addDependentWindow( _rDependentWindow2 );
229         pController->addDependentWindow( _rDependentWindow3 );
230         pController->addDependentWindow( _rDependentWindow4 );
231         pController->addDependentWindow( _rDependentWindow5 );
232         m_pImpl->aControllers.push_back( pController );
233     }
234 
235     //---------------------------------------------------------------------
236     void ControlDependencyManager::enableOnRadioCheck( RadioButton& _rRadio, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5, Window& _rDependentWindow6 )
237     {
238         PDialogController pController( new RadioDependentEnabler( _rRadio ) );
239         pController->addDependentWindow( _rDependentWindow1 );
240         pController->addDependentWindow( _rDependentWindow2 );
241         pController->addDependentWindow( _rDependentWindow3 );
242         pController->addDependentWindow( _rDependentWindow4 );
243         pController->addDependentWindow( _rDependentWindow5 );
244         pController->addDependentWindow( _rDependentWindow6 );
245         m_pImpl->aControllers.push_back( pController );
246     }
247 
248     //---------------------------------------------------------------------
249     void ControlDependencyManager::enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow )
250     {
251         PDialogController pController( new RadioDependentEnabler( _rBox ) );
252         pController->addDependentWindow( _rDependentWindow );
253         m_pImpl->aControllers.push_back( pController );
254     }
255 
256     //---------------------------------------------------------------------
257     void ControlDependencyManager::enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2 )
258     {
259         PDialogController pController( new RadioDependentEnabler( _rBox ) );
260         pController->addDependentWindow( _rDependentWindow1 );
261         pController->addDependentWindow( _rDependentWindow2 );
262         m_pImpl->aControllers.push_back( pController );
263     }
264 
265     //---------------------------------------------------------------------
266     void ControlDependencyManager::enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3 )
267     {
268         PDialogController pController( new RadioDependentEnabler( _rBox ) );
269         pController->addDependentWindow( _rDependentWindow1 );
270         pController->addDependentWindow( _rDependentWindow2 );
271         pController->addDependentWindow( _rDependentWindow3 );
272         m_pImpl->aControllers.push_back( pController );
273     }
274 
275     //---------------------------------------------------------------------
276     void ControlDependencyManager::enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4 )
277     {
278         PDialogController pController( new RadioDependentEnabler( _rBox ) );
279         pController->addDependentWindow( _rDependentWindow1 );
280         pController->addDependentWindow( _rDependentWindow2 );
281         pController->addDependentWindow( _rDependentWindow3 );
282         pController->addDependentWindow( _rDependentWindow4 );
283         m_pImpl->aControllers.push_back( pController );
284     }
285 
286     //---------------------------------------------------------------------
287     void ControlDependencyManager::enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5 )
288     {
289         PDialogController pController( new RadioDependentEnabler( _rBox ) );
290         pController->addDependentWindow( _rDependentWindow1 );
291         pController->addDependentWindow( _rDependentWindow2 );
292         pController->addDependentWindow( _rDependentWindow3 );
293         pController->addDependentWindow( _rDependentWindow4 );
294         pController->addDependentWindow( _rDependentWindow5 );
295         m_pImpl->aControllers.push_back( pController );
296     }
297 
298     //---------------------------------------------------------------------
299     void ControlDependencyManager::enableOnCheckMark( CheckBox& _rBox, Window& _rDependentWindow1, Window& _rDependentWindow2, Window& _rDependentWindow3, Window& _rDependentWindow4, Window& _rDependentWindow5, Window& _rDependentWindow6 )
300     {
301         PDialogController pController( new RadioDependentEnabler( _rBox ) );
302         pController->addDependentWindow( _rDependentWindow1 );
303         pController->addDependentWindow( _rDependentWindow2 );
304         pController->addDependentWindow( _rDependentWindow3 );
305         pController->addDependentWindow( _rDependentWindow4 );
306         pController->addDependentWindow( _rDependentWindow5 );
307         pController->addDependentWindow( _rDependentWindow6 );
308         m_pImpl->aControllers.push_back( pController );
309     }
310 
311 //........................................................................
312 } // namespace svt
313 //........................................................................
314 
315