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