1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 #include "windowstateguard.hxx"
27 #include "frm_strings.hxx"
28 
29 /** === begin UNO includes === **/
30 #include <com/sun/star/awt/XWindowListener2.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 /** === end UNO includes === **/
33 #include <cppuhelper/implbase1.hxx>
34 #include <tools/diagnose_ex.h>
35 
36 //........................................................................
37 namespace frm
38 {
39 //........................................................................
40 
41 	/** === begin UNO using === **/
42     using ::com::sun::star::awt::XWindowListener2;
43     using ::com::sun::star::uno::Reference;
44     using ::com::sun::star::awt::XWindow2;
45     using ::com::sun::star::awt::WindowEvent;
46     using ::com::sun::star::uno::RuntimeException;
47     using ::com::sun::star::awt::XControlModel;
48     using ::com::sun::star::beans::XPropertySet;
49     using ::com::sun::star::lang::EventObject;
50     using ::com::sun::star::uno::RuntimeException;
51     using ::com::sun::star::uno::UNO_QUERY;
52     using ::com::sun::star::uno::Exception;
53 	/** === end UNO using === **/
54 
55 	//====================================================================
56 	//= WindowStateGuard_Impl
57 	//====================================================================
58     typedef ::cppu::WeakImplHelper1 <   XWindowListener2
59                                     >   WindowStateGuard_Impl_Base;
60     class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base
61     {
62     private:
63         ::osl::Mutex                m_aMutex;
64         Reference< XWindow2 >       m_xWindow;
65         Reference< XPropertySet >   m_xModelProps;
66 
67     public:
68         /** constructs the instance
69             @param _rxWindow
70                 the window at which to listen. Must not be <NULL/>.
71             @param _rxModel
72                 the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
73         */
74         WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
75 
76         void dispose();
77 
78     protected:
79         // XWindowListener2
80         virtual void SAL_CALL windowEnabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
81         virtual void SAL_CALL windowDisabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
82 
83         // XWindowListener
84         virtual void SAL_CALL windowResized( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException);
85         virtual void SAL_CALL windowMoved( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException);
86         virtual void SAL_CALL windowShown( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
87         virtual void SAL_CALL windowHidden( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
88 
89         // XEventListener
90         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
91 
92     private:
93         /** ensures that the window's Enabled state matches what is described at the model
94             @precond
95                 our mutex is locked
96         */
97         void    impl_ensureEnabledState_nothrow_nolck();
98     };
99 
100 	//--------------------------------------------------------------------
WindowStateGuard_Impl(const Reference<XWindow2> & _rxWindow,const Reference<XPropertySet> & _rxMdelProps)101     WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
102         :m_xWindow( _rxWindow )
103         ,m_xModelProps( _rxMdelProps )
104     {
105         if ( !m_xWindow.is() || !m_xModelProps.is() )
106             throw RuntimeException();
107 
108         osl_incrementInterlockedCount( &m_refCount );
109         {
110             m_xWindow->addWindowListener( this );
111         }
112         osl_decrementInterlockedCount( &m_refCount );
113     }
114 
115     //--------------------------------------------------------------------
dispose()116     void WindowStateGuard_Impl::dispose()
117     {
118         ::osl::MutexGuard aGuard( m_aMutex );
119         if ( !m_xWindow.is() )
120             // already disposed
121             return;
122 
123         m_xWindow->removeWindowListener( this );
124         m_xWindow.clear();
125     }
126 
127     //--------------------------------------------------------------------
impl_ensureEnabledState_nothrow_nolck()128     void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
129     {
130         try
131         {
132             Reference< XWindow2 > xWindow;
133             sal_Bool bEnabled = sal_False;
134             sal_Bool bShouldBeEnabled = sal_False;
135             {
136                 ::osl::MutexGuard aGuard( m_aMutex );
137                 if ( !m_xWindow.is() || !m_xModelProps.is() )
138                     return;
139                 xWindow = m_xWindow;
140                 bEnabled = xWindow->isEnabled();
141                 OSL_VERIFY( m_xModelProps->getPropertyValue( PROPERTY_ENABLED ) >>= bShouldBeEnabled );
142             }
143 
144             if ( !bShouldBeEnabled && bEnabled && xWindow.is() )
145                 xWindow->setEnable( sal_False );
146         }
147         catch( const Exception& )
148         {
149         	DBG_UNHANDLED_EXCEPTION();
150         }
151     }
152 
153     //--------------------------------------------------------------------
windowEnabled(const EventObject &)154     void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ ) throw (RuntimeException)
155     {
156         impl_ensureEnabledState_nothrow_nolck();
157     }
158 
159     //--------------------------------------------------------------------
windowDisabled(const EventObject &)160     void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ ) throw (RuntimeException)
161     {
162         impl_ensureEnabledState_nothrow_nolck();
163     }
164 
165     //--------------------------------------------------------------------
windowResized(const WindowEvent &)166     void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ ) throw (RuntimeException)
167     {
168         // not interested in
169     }
170 
171     //--------------------------------------------------------------------
windowMoved(const WindowEvent &)172     void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ ) throw (RuntimeException)
173     {
174         // not interested in
175     }
176 
177     //--------------------------------------------------------------------
windowShown(const EventObject &)178     void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ ) throw (RuntimeException)
179     {
180         // not interested in
181     }
182 
183     //--------------------------------------------------------------------
windowHidden(const EventObject &)184     void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ ) throw (RuntimeException)
185     {
186         // not interested in
187     }
188 
189     //--------------------------------------------------------------------
disposing(const EventObject & Source)190     void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source ) throw (RuntimeException)
191     {
192         OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
193         (void)Source;
194         dispose();
195     }
196 
197 	//====================================================================
198 	//= WindowStateGuard
199 	//====================================================================
200 	//--------------------------------------------------------------------
WindowStateGuard()201     WindowStateGuard::WindowStateGuard()
202     {
203     }
204 
205 	//--------------------------------------------------------------------
~WindowStateGuard()206     WindowStateGuard::~WindowStateGuard()
207     {
208     }
209 
210 	//--------------------------------------------------------------------
attach(const Reference<XWindow2> & _rxWindow,const Reference<XControlModel> & _rxModel)211     void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
212     {
213         if ( m_pImpl.is() )
214         {
215             m_pImpl->dispose();
216             m_pImpl = NULL;
217         }
218 
219         Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY );
220         OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
221         if ( _rxWindow.is() && xModelProps.is() )
222             m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps );
223     }
224 
225 //........................................................................
226 } // namespace frm
227 //........................................................................
228 
229