1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_forms.hxx"
30*cdf0e10cSrcweir #include "windowstateguard.hxx"
31*cdf0e10cSrcweir #include "frm_strings.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir /** === begin UNO includes === **/
34*cdf0e10cSrcweir #include <com/sun/star/awt/XWindowListener2.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
36*cdf0e10cSrcweir /** === end UNO includes === **/
37*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
38*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir //........................................................................
41*cdf0e10cSrcweir namespace frm
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir //........................................................................
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 	/** === begin UNO using === **/
46*cdf0e10cSrcweir     using ::com::sun::star::awt::XWindowListener2;
47*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
48*cdf0e10cSrcweir     using ::com::sun::star::awt::XWindow2;
49*cdf0e10cSrcweir     using ::com::sun::star::awt::WindowEvent;
50*cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
51*cdf0e10cSrcweir     using ::com::sun::star::awt::XControlModel;
52*cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
53*cdf0e10cSrcweir     using ::com::sun::star::lang::EventObject;
54*cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
55*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
56*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
57*cdf0e10cSrcweir 	/** === end UNO using === **/
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	//====================================================================
60*cdf0e10cSrcweir 	//= WindowStateGuard_Impl
61*cdf0e10cSrcweir 	//====================================================================
62*cdf0e10cSrcweir     typedef ::cppu::WeakImplHelper1 <   XWindowListener2
63*cdf0e10cSrcweir                                     >   WindowStateGuard_Impl_Base;
64*cdf0e10cSrcweir     class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir     private:
67*cdf0e10cSrcweir         ::osl::Mutex                m_aMutex;
68*cdf0e10cSrcweir         Reference< XWindow2 >       m_xWindow;
69*cdf0e10cSrcweir         Reference< XPropertySet >   m_xModelProps;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     public:
72*cdf0e10cSrcweir         /** constructs the instance
73*cdf0e10cSrcweir             @param _rxWindow
74*cdf0e10cSrcweir                 the window at which to listen. Must not be <NULL/>.
75*cdf0e10cSrcweir             @param _rxModel
76*cdf0e10cSrcweir                 the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
77*cdf0e10cSrcweir         */
78*cdf0e10cSrcweir         WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         void dispose();
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     protected:
83*cdf0e10cSrcweir         // XWindowListener2
84*cdf0e10cSrcweir         virtual void SAL_CALL windowEnabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
85*cdf0e10cSrcweir         virtual void SAL_CALL windowDisabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         // XWindowListener
88*cdf0e10cSrcweir         virtual void SAL_CALL windowResized( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException);
89*cdf0e10cSrcweir         virtual void SAL_CALL windowMoved( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException);
90*cdf0e10cSrcweir         virtual void SAL_CALL windowShown( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
91*cdf0e10cSrcweir         virtual void SAL_CALL windowHidden( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException);
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         // XEventListener
94*cdf0e10cSrcweir         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     private:
97*cdf0e10cSrcweir         /** ensures that the window's Enabled state matches what is described at the model
98*cdf0e10cSrcweir             @precond
99*cdf0e10cSrcweir                 our mutex is locked
100*cdf0e10cSrcweir         */
101*cdf0e10cSrcweir         void    impl_ensureEnabledState_nothrow_nolck();
102*cdf0e10cSrcweir     };
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	//--------------------------------------------------------------------
105*cdf0e10cSrcweir     WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
106*cdf0e10cSrcweir         :m_xWindow( _rxWindow )
107*cdf0e10cSrcweir         ,m_xModelProps( _rxMdelProps )
108*cdf0e10cSrcweir     {
109*cdf0e10cSrcweir         if ( !m_xWindow.is() || !m_xModelProps.is() )
110*cdf0e10cSrcweir             throw RuntimeException();
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             m_xWindow->addWindowListener( this );
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_refCount );
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     //--------------------------------------------------------------------
120*cdf0e10cSrcweir     void WindowStateGuard_Impl::dispose()
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
123*cdf0e10cSrcweir         if ( !m_xWindow.is() )
124*cdf0e10cSrcweir             // already disposed
125*cdf0e10cSrcweir             return;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         m_xWindow->removeWindowListener( this );
128*cdf0e10cSrcweir         m_xWindow.clear();
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     //--------------------------------------------------------------------
132*cdf0e10cSrcweir     void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir         try
135*cdf0e10cSrcweir         {
136*cdf0e10cSrcweir             Reference< XWindow2 > xWindow;
137*cdf0e10cSrcweir             sal_Bool bEnabled = sal_False;
138*cdf0e10cSrcweir             sal_Bool bShouldBeEnabled = sal_False;
139*cdf0e10cSrcweir             {
140*cdf0e10cSrcweir                 ::osl::MutexGuard aGuard( m_aMutex );
141*cdf0e10cSrcweir                 if ( !m_xWindow.is() || !m_xModelProps.is() )
142*cdf0e10cSrcweir                     return;
143*cdf0e10cSrcweir                 xWindow = m_xWindow;
144*cdf0e10cSrcweir                 bEnabled = xWindow->isEnabled();
145*cdf0e10cSrcweir                 OSL_VERIFY( m_xModelProps->getPropertyValue( PROPERTY_ENABLED ) >>= bShouldBeEnabled );
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir             if ( !bShouldBeEnabled && bEnabled && xWindow.is() )
149*cdf0e10cSrcweir                 xWindow->setEnable( sal_False );
150*cdf0e10cSrcweir         }
151*cdf0e10cSrcweir         catch( const Exception& )
152*cdf0e10cSrcweir         {
153*cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     //--------------------------------------------------------------------
158*cdf0e10cSrcweir     void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ ) throw (RuntimeException)
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir         impl_ensureEnabledState_nothrow_nolck();
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     //--------------------------------------------------------------------
164*cdf0e10cSrcweir     void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ ) throw (RuntimeException)
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         impl_ensureEnabledState_nothrow_nolck();
167*cdf0e10cSrcweir     }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     //--------------------------------------------------------------------
170*cdf0e10cSrcweir     void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ ) throw (RuntimeException)
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         // not interested in
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     //--------------------------------------------------------------------
176*cdf0e10cSrcweir     void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ ) throw (RuntimeException)
177*cdf0e10cSrcweir     {
178*cdf0e10cSrcweir         // not interested in
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     //--------------------------------------------------------------------
182*cdf0e10cSrcweir     void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ ) throw (RuntimeException)
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         // not interested in
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     //--------------------------------------------------------------------
188*cdf0e10cSrcweir     void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ ) throw (RuntimeException)
189*cdf0e10cSrcweir     {
190*cdf0e10cSrcweir         // not interested in
191*cdf0e10cSrcweir     }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     //--------------------------------------------------------------------
194*cdf0e10cSrcweir     void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source ) throw (RuntimeException)
195*cdf0e10cSrcweir     {
196*cdf0e10cSrcweir         OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
197*cdf0e10cSrcweir         (void)Source;
198*cdf0e10cSrcweir         dispose();
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	//====================================================================
202*cdf0e10cSrcweir 	//= WindowStateGuard
203*cdf0e10cSrcweir 	//====================================================================
204*cdf0e10cSrcweir 	//--------------------------------------------------------------------
205*cdf0e10cSrcweir     WindowStateGuard::WindowStateGuard()
206*cdf0e10cSrcweir     {
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 	//--------------------------------------------------------------------
210*cdf0e10cSrcweir     WindowStateGuard::~WindowStateGuard()
211*cdf0e10cSrcweir     {
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 	//--------------------------------------------------------------------
215*cdf0e10cSrcweir     void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         if ( m_pImpl.is() )
218*cdf0e10cSrcweir         {
219*cdf0e10cSrcweir             m_pImpl->dispose();
220*cdf0e10cSrcweir             m_pImpl = NULL;
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir         Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY );
224*cdf0e10cSrcweir         OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
225*cdf0e10cSrcweir         if ( _rxWindow.is() && xModelProps.is() )
226*cdf0e10cSrcweir             m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps );
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir //........................................................................
230*cdf0e10cSrcweir } // namespace frm
231*cdf0e10cSrcweir //........................................................................
232*cdf0e10cSrcweir 
233