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/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>
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::lang::IllegalArgumentException
;
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::UNO_QUERY
;
43 using ::com::sun::star::uno::Exception
;
45 typedef ::cppu::WeakImplHelper
< XWindowListener2
46 > WindowStateGuard_Impl_Base
;
47 class WindowStateGuard_Impl
: public WindowStateGuard_Impl_Base
50 ::osl::Mutex m_aMutex
;
51 Reference
< XWindow2
> m_xWindow
;
52 Reference
< XPropertySet
> m_xModelProps
;
55 /** constructs the instance
57 the window at which to listen. Must not be <NULL/>.
59 the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
61 WindowStateGuard_Impl( const Reference
< XWindow2
>& _rxWindow
, const Reference
< XPropertySet
>& _rxMdelProps
);
67 virtual void SAL_CALL
windowEnabled( const css::lang::EventObject
& e
) override
;
68 virtual void SAL_CALL
windowDisabled( const css::lang::EventObject
& e
) override
;
71 virtual void SAL_CALL
windowResized( const css::awt::WindowEvent
& e
) override
;
72 virtual void SAL_CALL
windowMoved( const css::awt::WindowEvent
& e
) override
;
73 virtual void SAL_CALL
windowShown( const css::lang::EventObject
& e
) override
;
74 virtual void SAL_CALL
windowHidden( const css::lang::EventObject
& e
) override
;
77 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
80 /** ensures that the window's Enabled state matches what is described at the model
84 void impl_ensureEnabledState_nothrow_nolck();
88 WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference
< XWindow2
>& _rxWindow
, const Reference
< XPropertySet
>& _rxMdelProps
)
89 :m_xWindow( _rxWindow
)
90 ,m_xModelProps( _rxMdelProps
)
92 if ( !m_xWindow
.is() )
93 throw IllegalArgumentException(u
"no window supplied"_ustr
, *this, 0);
94 if ( !m_xModelProps
.is() )
95 throw IllegalArgumentException(u
"no property set supplied"_ustr
, *this, 1);
97 osl_atomic_increment( &m_refCount
);
99 m_xWindow
->addWindowListener( this );
101 osl_atomic_decrement( &m_refCount
);
105 void WindowStateGuard_Impl::dispose()
107 ::osl::MutexGuard
aGuard( m_aMutex
);
108 if ( !m_xWindow
.is() )
112 m_xWindow
->removeWindowListener( this );
117 void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
121 Reference
< XWindow2
> xWindow
;
122 Reference
< XPropertySet
> xModelProps
;
123 bool bShouldBeEnabled
= false;
125 ::osl::MutexGuard
aGuard( m_aMutex
);
126 if ( !m_xWindow
.is() || !m_xModelProps
.is() )
129 xModelProps
= m_xModelProps
;
131 // fdo#42157: do not lock m_aMutex to prevent deadlock
132 bool const bEnabled
= xWindow
->isEnabled();
133 OSL_VERIFY( xModelProps
->getPropertyValue( PROPERTY_ENABLED
)
134 >>= bShouldBeEnabled
);
136 if ( !bShouldBeEnabled
&& bEnabled
)
137 xWindow
->setEnable( false );
139 catch( const Exception
& )
141 DBG_UNHANDLED_EXCEPTION("forms.helper");
146 void SAL_CALL
WindowStateGuard_Impl::windowEnabled( const EventObject
& /*e*/ )
148 impl_ensureEnabledState_nothrow_nolck();
152 void SAL_CALL
WindowStateGuard_Impl::windowDisabled( const EventObject
& /*e*/ )
154 impl_ensureEnabledState_nothrow_nolck();
158 void SAL_CALL
WindowStateGuard_Impl::windowResized( const WindowEvent
& /*e*/ )
164 void SAL_CALL
WindowStateGuard_Impl::windowMoved( const WindowEvent
& /*e*/ )
170 void SAL_CALL
WindowStateGuard_Impl::windowShown( const EventObject
& /*e*/ )
176 void SAL_CALL
WindowStateGuard_Impl::windowHidden( const EventObject
& /*e*/ )
182 void SAL_CALL
WindowStateGuard_Impl::disposing( const EventObject
& Source
)
184 OSL_ENSURE( Source
.Source
== m_xWindow
, "WindowStateGuard_Impl::disposing: where does this come from?" );
188 WindowStateGuard::WindowStateGuard()
193 WindowStateGuard::~WindowStateGuard()
198 void WindowStateGuard::attach( const Reference
< XWindow2
>& _rxWindow
, const Reference
< XControlModel
>& _rxModel
)
206 Reference
< XPropertySet
> xModelProps( _rxModel
, UNO_QUERY
);
207 OSL_ENSURE( xModelProps
.is() || !_rxModel
.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
208 if ( _rxWindow
.is() && xModelProps
.is() )
209 m_pImpl
= new WindowStateGuard_Impl( _rxWindow
, xModelProps
);
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */