bump product version to 7.6.3.2-android
[LibreOffice.git] / forms / source / helper / windowstateguard.cxx
blobed7d2932abb9efc5ed855347a3de4973ec91de5a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <windowstateguard.hxx>
21 #include <frm_strings.hxx>
23 #include <com/sun/star/awt/XWindowListener2.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <cppuhelper/implbase.hxx>
26 #include <comphelper/diagnose_ex.hxx>
29 namespace frm
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;
44 typedef ::cppu::WeakImplHelper < XWindowListener2
45 > WindowStateGuard_Impl_Base;
46 class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base
48 private:
49 ::osl::Mutex m_aMutex;
50 Reference< XWindow2 > m_xWindow;
51 Reference< XPropertySet > m_xModelProps;
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/>.
60 WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
62 void dispose();
64 protected:
65 // XWindowListener2
66 virtual void SAL_CALL windowEnabled( const css::lang::EventObject& e ) override;
67 virtual void SAL_CALL windowDisabled( const css::lang::EventObject& e ) override;
69 // XWindowListener
70 virtual void SAL_CALL windowResized( const css::awt::WindowEvent& e ) override;
71 virtual void SAL_CALL windowMoved( const css::awt::WindowEvent& e ) override;
72 virtual void SAL_CALL windowShown( const css::lang::EventObject& e ) override;
73 virtual void SAL_CALL windowHidden( const css::lang::EventObject& e ) override;
75 // XEventListener
76 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
78 private:
79 /** ensures that the window's Enabled state matches what is described at the model
80 @precond
81 our mutex is locked
83 void impl_ensureEnabledState_nothrow_nolck();
87 WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
88 :m_xWindow( _rxWindow )
89 ,m_xModelProps( _rxMdelProps )
91 if ( !m_xWindow.is() || !m_xModelProps.is() )
92 throw RuntimeException();
94 osl_atomic_increment( &m_refCount );
96 m_xWindow->addWindowListener( this );
98 osl_atomic_decrement( &m_refCount );
102 void WindowStateGuard_Impl::dispose()
104 ::osl::MutexGuard aGuard( m_aMutex );
105 if ( !m_xWindow.is() )
106 // already disposed
107 return;
109 m_xWindow->removeWindowListener( this );
110 m_xWindow.clear();
114 void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
118 Reference< XWindow2 > xWindow;
119 Reference< XPropertySet > xModelProps;
120 bool bShouldBeEnabled = false;
122 ::osl::MutexGuard aGuard( m_aMutex );
123 if ( !m_xWindow.is() || !m_xModelProps.is() )
124 return;
125 xWindow = m_xWindow;
126 xModelProps = m_xModelProps;
128 // fdo#42157: do not lock m_aMutex to prevent deadlock
129 bool const bEnabled = xWindow->isEnabled();
130 OSL_VERIFY( xModelProps->getPropertyValue( PROPERTY_ENABLED )
131 >>= bShouldBeEnabled );
133 if ( !bShouldBeEnabled && bEnabled )
134 xWindow->setEnable( false );
136 catch( const Exception& )
138 DBG_UNHANDLED_EXCEPTION("forms.helper");
143 void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ )
145 impl_ensureEnabledState_nothrow_nolck();
149 void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ )
151 impl_ensureEnabledState_nothrow_nolck();
155 void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ )
157 // not interested in
161 void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ )
163 // not interested in
167 void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ )
169 // not interested in
173 void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ )
175 // not interested in
179 void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source )
181 OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
182 dispose();
185 WindowStateGuard::WindowStateGuard()
190 WindowStateGuard::~WindowStateGuard()
195 void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
197 if ( m_pImpl.is() )
199 m_pImpl->dispose();
200 m_pImpl = nullptr;
203 Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY );
204 OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
205 if ( _rxWindow.is() && xModelProps.is() )
206 m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps );
210 } // namespace frm
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */