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