1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: windowstateguard.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
33 #include "windowstateguard.hxx"
34 #include "frm_strings.hxx"
36 /** === begin UNO includes === **/
37 #include <com/sun/star/awt/XWindowListener2.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 /** === end UNO includes === **/
40 #include <cppuhelper/implbase1.hxx>
41 #include <tools/diagnose_ex.h>
43 //........................................................................
46 //........................................................................
48 /** === begin UNO using === **/
49 using ::com::sun::star::awt::XWindowListener2
;
50 using ::com::sun::star::uno::Reference
;
51 using ::com::sun::star::awt::XWindow2
;
52 using ::com::sun::star::awt::WindowEvent
;
53 using ::com::sun::star::uno::RuntimeException
;
54 using ::com::sun::star::awt::XControlModel
;
55 using ::com::sun::star::beans::XPropertySet
;
56 using ::com::sun::star::lang::EventObject
;
57 using ::com::sun::star::uno::RuntimeException
;
58 using ::com::sun::star::uno::UNO_QUERY
;
59 using ::com::sun::star::uno::Exception
;
60 /** === end UNO using === **/
62 //====================================================================
63 //= WindowStateGuard_Impl
64 //====================================================================
65 typedef ::cppu::WeakImplHelper1
< XWindowListener2
66 > WindowStateGuard_Impl_Base
;
67 class WindowStateGuard_Impl
: public WindowStateGuard_Impl_Base
70 ::osl::Mutex m_aMutex
;
71 Reference
< XWindow2
> m_xWindow
;
72 Reference
< XPropertySet
> m_xModelProps
;
75 /** constructs the instance
77 the window at which to listen. Must not be <NULL/>.
79 the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
81 WindowStateGuard_Impl( const Reference
< XWindow2
>& _rxWindow
, const Reference
< XPropertySet
>& _rxMdelProps
);
87 virtual void SAL_CALL
windowEnabled( const ::com::sun::star::lang::EventObject
& e
) throw (::com::sun::star::uno::RuntimeException
);
88 virtual void SAL_CALL
windowDisabled( const ::com::sun::star::lang::EventObject
& e
) throw (::com::sun::star::uno::RuntimeException
);
91 virtual void SAL_CALL
windowResized( const ::com::sun::star::awt::WindowEvent
& e
) throw (::com::sun::star::uno::RuntimeException
);
92 virtual void SAL_CALL
windowMoved( const ::com::sun::star::awt::WindowEvent
& e
) throw (::com::sun::star::uno::RuntimeException
);
93 virtual void SAL_CALL
windowShown( const ::com::sun::star::lang::EventObject
& e
) throw (::com::sun::star::uno::RuntimeException
);
94 virtual void SAL_CALL
windowHidden( const ::com::sun::star::lang::EventObject
& e
) throw (::com::sun::star::uno::RuntimeException
);
97 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
) throw (::com::sun::star::uno::RuntimeException
);
100 /** ensures that the window's Enabled state matches what is described at the model
104 void impl_ensureEnabledState_nothrow() const;
107 //--------------------------------------------------------------------
108 WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference
< XWindow2
>& _rxWindow
, const Reference
< XPropertySet
>& _rxMdelProps
)
109 :m_xWindow( _rxWindow
)
110 ,m_xModelProps( _rxMdelProps
)
112 if ( !m_xWindow
.is() || !m_xModelProps
.is() )
113 throw RuntimeException();
115 osl_incrementInterlockedCount( &m_refCount
);
117 m_xWindow
->addWindowListener( this );
119 osl_decrementInterlockedCount( &m_refCount
);
122 //--------------------------------------------------------------------
123 void WindowStateGuard_Impl::dispose()
125 ::osl::MutexGuard
aGuard( m_aMutex
);
126 if ( !m_xWindow
.is() )
130 m_xWindow
->removeWindowListener( this );
134 //--------------------------------------------------------------------
135 void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow() const
139 sal_Bool bEnabled
= m_xWindow
->isEnabled();
140 sal_Bool bShouldBeEnabled
= sal_False
;
141 OSL_VERIFY( m_xModelProps
->getPropertyValue( PROPERTY_ENABLED
) >>= bShouldBeEnabled
);
143 if ( !bShouldBeEnabled
&& bEnabled
)
144 m_xWindow
->setEnable( sal_False
);
146 catch( const Exception
& )
148 DBG_UNHANDLED_EXCEPTION();
152 //--------------------------------------------------------------------
153 void SAL_CALL
WindowStateGuard_Impl::windowEnabled( const EventObject
& /*e*/ ) throw (RuntimeException
)
155 ::osl::MutexGuard
aGuard( m_aMutex
);
156 impl_ensureEnabledState_nothrow();
159 //--------------------------------------------------------------------
160 void SAL_CALL
WindowStateGuard_Impl::windowDisabled( const EventObject
& /*e*/ ) throw (RuntimeException
)
162 ::osl::MutexGuard
aGuard( m_aMutex
);
163 impl_ensureEnabledState_nothrow();
166 //--------------------------------------------------------------------
167 void SAL_CALL
WindowStateGuard_Impl::windowResized( const WindowEvent
& /*e*/ ) throw (RuntimeException
)
172 //--------------------------------------------------------------------
173 void SAL_CALL
WindowStateGuard_Impl::windowMoved( const WindowEvent
& /*e*/ ) throw (RuntimeException
)
178 //--------------------------------------------------------------------
179 void SAL_CALL
WindowStateGuard_Impl::windowShown( const EventObject
& /*e*/ ) throw (RuntimeException
)
184 //--------------------------------------------------------------------
185 void SAL_CALL
WindowStateGuard_Impl::windowHidden( const EventObject
& /*e*/ ) throw (RuntimeException
)
190 //--------------------------------------------------------------------
191 void SAL_CALL
WindowStateGuard_Impl::disposing( const EventObject
& Source
) throw (RuntimeException
)
193 OSL_ENSURE( Source
.Source
== m_xWindow
, "WindowStateGuard_Impl::disposing: where does this come from?" );
198 //====================================================================
200 //====================================================================
201 //--------------------------------------------------------------------
202 WindowStateGuard::WindowStateGuard()
206 //--------------------------------------------------------------------
207 WindowStateGuard::~WindowStateGuard()
211 //--------------------------------------------------------------------
212 void WindowStateGuard::attach( const Reference
< XWindow2
>& _rxWindow
, const Reference
< XControlModel
>& _rxModel
)
220 Reference
< XPropertySet
> xModelProps( _rxModel
, UNO_QUERY
);
221 OSL_ENSURE( xModelProps
.is() || !_rxModel
.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
222 if ( _rxWindow
.is() && xModelProps
.is() )
223 m_pImpl
= new WindowStateGuard_Impl( _rxWindow
, xModelProps
);
226 //........................................................................
228 //........................................................................