fix mismatching gb_Trace_StartRange and gb_Trace_EndRage
[LibreOffice.git] / forms / source / helper / windowstateguard.cxx
blobdd47dd2989b99daca02acfcfa4bd5c003e9aee3f
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/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/awt/XWindowListener2.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <cppuhelper/implbase.hxx>
27 #include <comphelper/diagnose_ex.hxx>
30 namespace frm
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::lang::IllegalArgumentException;
40 using ::com::sun::star::awt::XControlModel;
41 using ::com::sun::star::beans::XPropertySet;
42 using ::com::sun::star::lang::EventObject;
43 using ::com::sun::star::uno::UNO_QUERY;
44 using ::com::sun::star::uno::Exception;
46 typedef ::cppu::WeakImplHelper < XWindowListener2
47 > WindowStateGuard_Impl_Base;
48 class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base
50 private:
51 ::osl::Mutex m_aMutex;
52 Reference< XWindow2 > m_xWindow;
53 Reference< XPropertySet > m_xModelProps;
55 public:
56 /** constructs the instance
57 @param _rxWindow
58 the window at which to listen. Must not be <NULL/>.
59 @param _rxModel
60 the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
62 WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
64 void dispose();
66 protected:
67 // XWindowListener2
68 virtual void SAL_CALL windowEnabled( const css::lang::EventObject& e ) override;
69 virtual void SAL_CALL windowDisabled( const css::lang::EventObject& e ) override;
71 // XWindowListener
72 virtual void SAL_CALL windowResized( const css::awt::WindowEvent& e ) override;
73 virtual void SAL_CALL windowMoved( const css::awt::WindowEvent& e ) override;
74 virtual void SAL_CALL windowShown( const css::lang::EventObject& e ) override;
75 virtual void SAL_CALL windowHidden( const css::lang::EventObject& e ) override;
77 // XEventListener
78 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
80 private:
81 /** ensures that the window's Enabled state matches what is described at the model
82 @precond
83 our mutex is locked
85 void impl_ensureEnabledState_nothrow_nolck();
89 WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
90 :m_xWindow( _rxWindow )
91 ,m_xModelProps( _rxMdelProps )
93 if ( !m_xWindow.is() )
94 throw IllegalArgumentException("no window supplied", *this, 0);
95 if ( !m_xModelProps.is() )
96 throw IllegalArgumentException("no property set supplied", *this, 1);
98 osl_atomic_increment( &m_refCount );
100 m_xWindow->addWindowListener( this );
102 osl_atomic_decrement( &m_refCount );
106 void WindowStateGuard_Impl::dispose()
108 ::osl::MutexGuard aGuard( m_aMutex );
109 if ( !m_xWindow.is() )
110 // already disposed
111 return;
113 m_xWindow->removeWindowListener( this );
114 m_xWindow.clear();
118 void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
122 Reference< XWindow2 > xWindow;
123 Reference< XPropertySet > xModelProps;
124 bool bShouldBeEnabled = false;
126 ::osl::MutexGuard aGuard( m_aMutex );
127 if ( !m_xWindow.is() || !m_xModelProps.is() )
128 return;
129 xWindow = m_xWindow;
130 xModelProps = m_xModelProps;
132 // fdo#42157: do not lock m_aMutex to prevent deadlock
133 bool const bEnabled = xWindow->isEnabled();
134 OSL_VERIFY( xModelProps->getPropertyValue( PROPERTY_ENABLED )
135 >>= bShouldBeEnabled );
137 if ( !bShouldBeEnabled && bEnabled )
138 xWindow->setEnable( false );
140 catch( const Exception& )
142 DBG_UNHANDLED_EXCEPTION("forms.helper");
147 void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ )
149 impl_ensureEnabledState_nothrow_nolck();
153 void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ )
155 impl_ensureEnabledState_nothrow_nolck();
159 void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ )
161 // not interested in
165 void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ )
167 // not interested in
171 void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ )
173 // not interested in
177 void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ )
179 // not interested in
183 void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source )
185 OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
186 dispose();
189 WindowStateGuard::WindowStateGuard()
194 WindowStateGuard::~WindowStateGuard()
199 void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
201 if ( m_pImpl.is() )
203 m_pImpl->dispose();
204 m_pImpl = nullptr;
207 Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY );
208 OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
209 if ( _rxWindow.is() && xModelProps.is() )
210 m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps );
214 } // namespace frm
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */