1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/implbase1.hxx>
26 #include <tools/diagnose_ex.h>
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::WeakImplHelper1
< XWindowListener2
45 > WindowStateGuard_Impl_Base
;
46 class WindowStateGuard_Impl
: public WindowStateGuard_Impl_Base
49 ::osl::Mutex m_aMutex
;
50 Reference
< XWindow2
> m_xWindow
;
51 Reference
< XPropertySet
> m_xModelProps
;
54 /** constructs the instance
56 the window at which to listen. Must not be <NULL/>.
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
);
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
;
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
;
76 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
79 /** ensures that the window's Enabled state matches what is described at the model
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() )
109 m_xWindow
->removeWindowListener( this );
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() )
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( sal_False
);
136 catch( const Exception
& )
138 DBG_UNHANDLED_EXCEPTION();
143 void SAL_CALL
WindowStateGuard_Impl::windowEnabled( const EventObject
& /*e*/ ) throw (RuntimeException
, std::exception
)
145 impl_ensureEnabledState_nothrow_nolck();
149 void SAL_CALL
WindowStateGuard_Impl::windowDisabled( const EventObject
& /*e*/ ) throw (RuntimeException
, std::exception
)
151 impl_ensureEnabledState_nothrow_nolck();
155 void SAL_CALL
WindowStateGuard_Impl::windowResized( const WindowEvent
& /*e*/ ) throw (RuntimeException
, std::exception
)
161 void SAL_CALL
WindowStateGuard_Impl::windowMoved( const WindowEvent
& /*e*/ ) throw (RuntimeException
, std::exception
)
167 void SAL_CALL
WindowStateGuard_Impl::windowShown( const EventObject
& /*e*/ ) throw (RuntimeException
, std::exception
)
173 void SAL_CALL
WindowStateGuard_Impl::windowHidden( const EventObject
& /*e*/ ) throw (RuntimeException
, std::exception
)
179 void SAL_CALL
WindowStateGuard_Impl::disposing( const EventObject
& Source
) throw (RuntimeException
, std::exception
)
181 OSL_ENSURE( Source
.Source
== m_xWindow
, "WindowStateGuard_Impl::disposing: where does this come from?" );
186 WindowStateGuard::WindowStateGuard()
191 WindowStateGuard::~WindowStateGuard()
196 void WindowStateGuard::attach( const Reference
< XWindow2
>& _rxWindow
, const Reference
< XControlModel
>& _rxModel
)
204 Reference
< XPropertySet
> xModelProps( _rxModel
, UNO_QUERY
);
205 OSL_ENSURE( xModelProps
.is() || !_rxModel
.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
206 if ( _rxWindow
.is() && xModelProps
.is() )
207 m_pImpl
= new WindowStateGuard_Impl( _rxWindow
, xModelProps
);
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */