LibreOffice Module forms (master) 1
windowstateguard.cxx
Go to the documentation of this file.
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>
27
28
29namespace frm
30{
31
32
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;
43
44 typedef ::cppu::WeakImplHelper < XWindowListener2
47 {
48 private:
49 ::osl::Mutex m_aMutex;
50 Reference< XWindow2 > m_xWindow;
51 Reference< XPropertySet > m_xModelProps;
52
53 public:
60 WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
61
62 void dispose();
63
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;
68
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;
74
75 // XEventListener
76 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
77
78 private:
84 };
85
86
87 WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
88 :m_xWindow( _rxWindow )
89 ,m_xModelProps( _rxMdelProps )
90 {
91 if ( !m_xWindow.is() || !m_xModelProps.is() )
92 throw RuntimeException();
93
94 osl_atomic_increment( &m_refCount );
95 {
96 m_xWindow->addWindowListener( this );
97 }
98 osl_atomic_decrement( &m_refCount );
99 }
100
101
103 {
104 ::osl::MutexGuard aGuard( m_aMutex );
105 if ( !m_xWindow.is() )
106 // already disposed
107 return;
108
109 m_xWindow->removeWindowListener( this );
110 m_xWindow.clear();
111 }
112
113
115 {
116 try
117 {
118 Reference< XWindow2 > xWindow;
119 Reference< XPropertySet > xModelProps;
120 bool bShouldBeEnabled = false;
121 {
122 ::osl::MutexGuard aGuard( m_aMutex );
123 if ( !m_xWindow.is() || !m_xModelProps.is() )
124 return;
125 xWindow = m_xWindow;
126 xModelProps = m_xModelProps;
127 }
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 );
132
133 if ( !bShouldBeEnabled && bEnabled )
134 xWindow->setEnable( false );
135 }
136 catch( const Exception& )
137 {
138 DBG_UNHANDLED_EXCEPTION("forms.helper");
139 }
140 }
141
142
143 void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ )
144 {
146 }
147
148
149 void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ )
150 {
152 }
153
154
155 void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ )
156 {
157 // not interested in
158 }
159
160
161 void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ )
162 {
163 // not interested in
164 }
165
166
167 void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ )
168 {
169 // not interested in
170 }
171
172
173 void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ )
174 {
175 // not interested in
176 }
177
178
179 void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source )
180 {
181 OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
182 dispose();
183 }
184
186 {
187 }
188
189
191 {
192 }
193
194
195 void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
196 {
197 if ( m_pImpl.is() )
198 {
199 m_pImpl->dispose();
200 m_pImpl = nullptr;
201 }
202
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 );
207 }
208
209
210} // namespace frm
211
212
213/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL windowShown(const css::lang::EventObject &e) override
virtual void SAL_CALL windowEnabled(const css::lang::EventObject &e) override
Reference< XWindow2 > m_xWindow
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual void SAL_CALL windowResized(const css::awt::WindowEvent &e) override
Reference< XPropertySet > m_xModelProps
virtual void SAL_CALL windowMoved(const css::awt::WindowEvent &e) override
WindowStateGuard_Impl(const Reference< XWindow2 > &_rxWindow, const Reference< XPropertySet > &_rxMdelProps)
constructs the instance
virtual void SAL_CALL windowDisabled(const css::lang::EventObject &e) override
void impl_ensureEnabledState_nothrow_nolck()
ensures that the window's Enabled state matches what is described at the model @precond our mutex is ...
virtual void SAL_CALL windowHidden(const css::lang::EventObject &e) override
::rtl::Reference< WindowStateGuard_Impl > m_pImpl
void attach(const css::uno::Reference< css::awt::XWindow2 > &_rxWindow, const css::uno::Reference< css::awt::XControlModel > &_rxModel)
#define DBG_UNHANDLED_EXCEPTION(...)
ULONG m_refCount
constexpr OUStringLiteral PROPERTY_ENABLED
Definition: frm_strings.hxx:39
@ Exception
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
::cppu::WeakImplHelper< XWindowListener2 > WindowStateGuard_Impl_Base