Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "windowstateguard.hxx"
21 : #include "frm_strings.hxx"
22 :
23 : #include <com/sun/star/awt/XWindowListener2.hpp>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <cppuhelper/implbase1.hxx>
26 : #include <tools/diagnose_ex.h>
27 :
28 :
29 : namespace frm
30 : {
31 :
32 :
33 : using ::com::sun::star::awt::XWindowListener2;
34 : using ::com::sun::star::uno::Reference;
35 : using ::com::sun::star::awt::XWindow2;
36 : using ::com::sun::star::awt::WindowEvent;
37 : using ::com::sun::star::uno::RuntimeException;
38 : using ::com::sun::star::awt::XControlModel;
39 : using ::com::sun::star::beans::XPropertySet;
40 : using ::com::sun::star::lang::EventObject;
41 : using ::com::sun::star::uno::UNO_QUERY;
42 : using ::com::sun::star::uno::Exception;
43 :
44 : typedef ::cppu::WeakImplHelper1 < XWindowListener2
45 : > WindowStateGuard_Impl_Base;
46 1406 : class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base
47 : {
48 : private:
49 : ::osl::Mutex m_aMutex;
50 : Reference< XWindow2 > m_xWindow;
51 : Reference< XPropertySet > m_xModelProps;
52 :
53 : public:
54 : /** constructs the instance
55 : @param _rxWindow
56 : the window at which to listen. Must not be <NULL/>.
57 : @param _rxModel
58 : the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
59 : */
60 : WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
61 :
62 : void dispose();
63 :
64 : protected:
65 : // XWindowListener2
66 : virtual void SAL_CALL windowEnabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
67 : virtual void SAL_CALL windowDisabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
68 :
69 : // XWindowListener
70 : virtual void SAL_CALL windowResized( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
71 : virtual void SAL_CALL windowMoved( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
72 : virtual void SAL_CALL windowShown( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 : virtual void SAL_CALL windowHidden( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
74 :
75 : // XEventListener
76 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
77 :
78 : private:
79 : /** ensures that the window's Enabled state matches what is described at the model
80 : @precond
81 : our mutex is locked
82 : */
83 : void impl_ensureEnabledState_nothrow_nolck();
84 : };
85 :
86 :
87 703 : WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
88 : :m_xWindow( _rxWindow )
89 703 : ,m_xModelProps( _rxMdelProps )
90 : {
91 703 : if ( !m_xWindow.is() || !m_xModelProps.is() )
92 0 : throw RuntimeException();
93 :
94 703 : osl_atomic_increment( &m_refCount );
95 : {
96 703 : m_xWindow->addWindowListener( this );
97 : }
98 703 : osl_atomic_decrement( &m_refCount );
99 703 : }
100 :
101 :
102 1016 : void WindowStateGuard_Impl::dispose()
103 : {
104 1016 : ::osl::MutexGuard aGuard( m_aMutex );
105 1016 : if ( !m_xWindow.is() )
106 : // already disposed
107 1329 : return;
108 :
109 703 : m_xWindow->removeWindowListener( this );
110 703 : m_xWindow.clear();
111 : }
112 :
113 :
114 48 : void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
115 : {
116 : try
117 : {
118 48 : Reference< XWindow2 > xWindow;
119 96 : Reference< XPropertySet > xModelProps;
120 48 : bool bShouldBeEnabled = false;
121 : {
122 48 : ::osl::MutexGuard aGuard( m_aMutex );
123 48 : if ( !m_xWindow.is() || !m_xModelProps.is() )
124 48 : return;
125 48 : xWindow = m_xWindow;
126 48 : xModelProps = m_xModelProps;
127 : }
128 : // fdo#42157: do not lock m_aMutex to prevent deadlock
129 48 : bool const bEnabled = xWindow->isEnabled();
130 48 : OSL_VERIFY( xModelProps->getPropertyValue( PROPERTY_ENABLED )
131 : >>= bShouldBeEnabled );
132 :
133 48 : if ( !bShouldBeEnabled && bEnabled )
134 66 : xWindow->setEnable( sal_False );
135 : }
136 0 : catch( const Exception& )
137 : {
138 : DBG_UNHANDLED_EXCEPTION();
139 : }
140 : }
141 :
142 :
143 24 : void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
144 : {
145 24 : impl_ensureEnabledState_nothrow_nolck();
146 24 : }
147 :
148 :
149 24 : void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
150 : {
151 24 : impl_ensureEnabledState_nothrow_nolck();
152 24 : }
153 :
154 :
155 502 : void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ ) throw (RuntimeException, std::exception)
156 : {
157 : // not interested in
158 502 : }
159 :
160 :
161 978 : void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ ) throw (RuntimeException, std::exception)
162 : {
163 : // not interested in
164 978 : }
165 :
166 :
167 2298 : void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
168 : {
169 : // not interested in
170 2298 : }
171 :
172 :
173 2324 : void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
174 : {
175 : // not interested in
176 2324 : }
177 :
178 :
179 313 : void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source ) throw (RuntimeException, std::exception)
180 : {
181 : OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
182 : (void)Source;
183 313 : dispose();
184 313 : }
185 :
186 602 : WindowStateGuard::WindowStateGuard()
187 : {
188 602 : }
189 :
190 :
191 524 : WindowStateGuard::~WindowStateGuard()
192 : {
193 524 : }
194 :
195 :
196 1713 : void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
197 : {
198 1713 : if ( m_pImpl.is() )
199 : {
200 703 : m_pImpl->dispose();
201 703 : m_pImpl = NULL;
202 : }
203 :
204 1713 : Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY );
205 : OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
206 1713 : if ( _rxWindow.is() && xModelProps.is() )
207 703 : m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps );
208 1713 : }
209 :
210 :
211 : } // namespace frm
212 :
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|