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