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_forms.hxx"
30 #include "windowstateguard.hxx"
31 #include "frm_strings.hxx"
32 
33 /** === begin UNO includes === **/
34 #include <com/sun/star/awt/XWindowListener2.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 /** === end UNO includes === **/
37 #include <cppuhelper/implbase1.hxx>
38 #include <tools/diagnose_ex.h>
39 
40 //........................................................................
41 namespace frm
42 {
43 //........................................................................
44 
45 	/** === begin UNO using === **/
46     using ::com::sun::star::awt::XWindowListener2;
47     using ::com::sun::star::uno::Reference;
48     using ::com::sun::star::awt::XWindow2;
49     using ::com::sun::star::awt::WindowEvent;
50     using ::com::sun::star::uno::RuntimeException;
51     using ::com::sun::star::awt::XControlModel;
52     using ::com::sun::star::beans::XPropertySet;
53     using ::com::sun::star::lang::EventObject;
54     using ::com::sun::star::uno::RuntimeException;
55     using ::com::sun::star::uno::UNO_QUERY;
56     using ::com::sun::star::uno::Exception;
57 	/** === end UNO using === **/
58 
59 	//====================================================================
60 	//= WindowStateGuard_Impl
61 	//====================================================================
62     typedef ::cppu::WeakImplHelper1 <   XWindowListener2
63                                     >   WindowStateGuard_Impl_Base;
64     class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base
65     {
66     private:
67         ::osl::Mutex                m_aMutex;
68         Reference< XWindow2 >       m_xWindow;
69         Reference< XPropertySet >   m_xModelProps;
70 
71     public:
72         /** constructs the instance
73             @param _rxWindow
74                 the window at which to listen. Must not be <NULL/>.
75             @param _rxModel
76                 the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
77         */
78         WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
79 
80         void dispose();
81 
82     protected:
83         // XWindowListener2
84         virtual void SAL_CALL windowEnabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
85         virtual void SAL_CALL windowDisabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
86 
87         // XWindowListener
88         virtual void SAL_CALL windowResized( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException);
89         virtual void SAL_CALL windowMoved( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException);
90         virtual void SAL_CALL windowShown( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
91         virtual void SAL_CALL windowHidden( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
92 
93         // XEventListener
94         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
95 
96     private:
97         /** ensures that the window's Enabled state matches what is described at the model
98             @precond
99                 our mutex is locked
100         */
101         void    impl_ensureEnabledState_nothrow_nolck();
102     };
103 
104 	//--------------------------------------------------------------------
105     WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
106         :m_xWindow( _rxWindow )
107         ,m_xModelProps( _rxMdelProps )
108     {
109         if ( !m_xWindow.is() || !m_xModelProps.is() )
110             throw RuntimeException();
111 
112         osl_incrementInterlockedCount( &m_refCount );
113         {
114             m_xWindow->addWindowListener( this );
115         }
116         osl_decrementInterlockedCount( &m_refCount );
117     }
118 
119     //--------------------------------------------------------------------
120     void WindowStateGuard_Impl::dispose()
121     {
122         ::osl::MutexGuard aGuard( m_aMutex );
123         if ( !m_xWindow.is() )
124             // already disposed
125             return;
126 
127         m_xWindow->removeWindowListener( this );
128         m_xWindow.clear();
129     }
130 
131     //--------------------------------------------------------------------
132     void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
133     {
134         try
135         {
136             Reference< XWindow2 > xWindow;
137             sal_Bool bEnabled = sal_False;
138             sal_Bool bShouldBeEnabled = sal_False;
139             {
140                 ::osl::MutexGuard aGuard( m_aMutex );
141                 if ( !m_xWindow.is() || !m_xModelProps.is() )
142                     return;
143                 xWindow = m_xWindow;
144                 bEnabled = xWindow->isEnabled();
145                 OSL_VERIFY( m_xModelProps->getPropertyValue( PROPERTY_ENABLED ) >>= bShouldBeEnabled );
146             }
147 
148             if ( !bShouldBeEnabled && bEnabled && xWindow.is() )
149                 xWindow->setEnable( sal_False );
150         }
151         catch( const Exception& )
152         {
153         	DBG_UNHANDLED_EXCEPTION();
154         }
155     }
156 
157     //--------------------------------------------------------------------
158     void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ ) throw (RuntimeException)
159     {
160         impl_ensureEnabledState_nothrow_nolck();
161     }
162 
163     //--------------------------------------------------------------------
164     void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ ) throw (RuntimeException)
165     {
166         impl_ensureEnabledState_nothrow_nolck();
167     }
168 
169     //--------------------------------------------------------------------
170     void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ ) throw (RuntimeException)
171     {
172         // not interested in
173     }
174 
175     //--------------------------------------------------------------------
176     void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ ) throw (RuntimeException)
177     {
178         // not interested in
179     }
180 
181     //--------------------------------------------------------------------
182     void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ ) throw (RuntimeException)
183     {
184         // not interested in
185     }
186 
187     //--------------------------------------------------------------------
188     void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ ) throw (RuntimeException)
189     {
190         // not interested in
191     }
192 
193     //--------------------------------------------------------------------
194     void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source ) throw (RuntimeException)
195     {
196         OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
197         (void)Source;
198         dispose();
199     }
200 
201 	//====================================================================
202 	//= WindowStateGuard
203 	//====================================================================
204 	//--------------------------------------------------------------------
205     WindowStateGuard::WindowStateGuard()
206     {
207     }
208 
209 	//--------------------------------------------------------------------
210     WindowStateGuard::~WindowStateGuard()
211     {
212     }
213 
214 	//--------------------------------------------------------------------
215     void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
216     {
217         if ( m_pImpl.is() )
218         {
219             m_pImpl->dispose();
220             m_pImpl = NULL;
221         }
222 
223         Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY );
224         OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
225         if ( _rxWindow.is() && xModelProps.is() )
226             m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps );
227     }
228 
229 //........................................................................
230 } // namespace frm
231 //........................................................................
232 
233