1*24acc546SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*24acc546SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*24acc546SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*24acc546SAndrew Rist * distributed with this work for additional information 6*24acc546SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*24acc546SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*24acc546SAndrew Rist * "License"); you may not use this file except in compliance 9*24acc546SAndrew Rist * with the License. You may obtain a copy of the License at 10*24acc546SAndrew Rist * 11*24acc546SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*24acc546SAndrew Rist * 13*24acc546SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*24acc546SAndrew Rist * software distributed under the License is distributed on an 15*24acc546SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*24acc546SAndrew Rist * KIND, either express or implied. See the License for the 17*24acc546SAndrew Rist * specific language governing permissions and limitations 18*24acc546SAndrew Rist * under the License. 19*24acc546SAndrew Rist * 20*24acc546SAndrew Rist *************************************************************/ 21*24acc546SAndrew Rist 22*24acc546SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_forms.hxx" 26cdf0e10cSrcweir #include "windowstateguard.hxx" 27cdf0e10cSrcweir #include "frm_strings.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir /** === begin UNO includes === **/ 30cdf0e10cSrcweir #include <com/sun/star/awt/XWindowListener2.hpp> 31cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 32cdf0e10cSrcweir /** === end UNO includes === **/ 33cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 34cdf0e10cSrcweir #include <tools/diagnose_ex.h> 35cdf0e10cSrcweir 36cdf0e10cSrcweir //........................................................................ 37cdf0e10cSrcweir namespace frm 38cdf0e10cSrcweir { 39cdf0e10cSrcweir //........................................................................ 40cdf0e10cSrcweir 41cdf0e10cSrcweir /** === begin UNO using === **/ 42cdf0e10cSrcweir using ::com::sun::star::awt::XWindowListener2; 43cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 44cdf0e10cSrcweir using ::com::sun::star::awt::XWindow2; 45cdf0e10cSrcweir using ::com::sun::star::awt::WindowEvent; 46cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 47cdf0e10cSrcweir using ::com::sun::star::awt::XControlModel; 48cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet; 49cdf0e10cSrcweir using ::com::sun::star::lang::EventObject; 50cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 51cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 52cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 53cdf0e10cSrcweir /** === end UNO using === **/ 54cdf0e10cSrcweir 55cdf0e10cSrcweir //==================================================================== 56cdf0e10cSrcweir //= WindowStateGuard_Impl 57cdf0e10cSrcweir //==================================================================== 58cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1 < XWindowListener2 59cdf0e10cSrcweir > WindowStateGuard_Impl_Base; 60cdf0e10cSrcweir class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base 61cdf0e10cSrcweir { 62cdf0e10cSrcweir private: 63cdf0e10cSrcweir ::osl::Mutex m_aMutex; 64cdf0e10cSrcweir Reference< XWindow2 > m_xWindow; 65cdf0e10cSrcweir Reference< XPropertySet > m_xModelProps; 66cdf0e10cSrcweir 67cdf0e10cSrcweir public: 68cdf0e10cSrcweir /** constructs the instance 69cdf0e10cSrcweir @param _rxWindow 70cdf0e10cSrcweir the window at which to listen. Must not be <NULL/>. 71cdf0e10cSrcweir @param _rxModel 72cdf0e10cSrcweir the model which acts as the reference for the states to be enforced. Must not be <NULL/>. 73cdf0e10cSrcweir */ 74cdf0e10cSrcweir WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps ); 75cdf0e10cSrcweir 76cdf0e10cSrcweir void dispose(); 77cdf0e10cSrcweir 78cdf0e10cSrcweir protected: 79cdf0e10cSrcweir // XWindowListener2 80cdf0e10cSrcweir virtual void SAL_CALL windowEnabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException); 81cdf0e10cSrcweir virtual void SAL_CALL windowDisabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException); 82cdf0e10cSrcweir 83cdf0e10cSrcweir // XWindowListener 84cdf0e10cSrcweir virtual void SAL_CALL windowResized( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException); 85cdf0e10cSrcweir virtual void SAL_CALL windowMoved( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException); 86cdf0e10cSrcweir virtual void SAL_CALL windowShown( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException); 87cdf0e10cSrcweir virtual void SAL_CALL windowHidden( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException); 88cdf0e10cSrcweir 89cdf0e10cSrcweir // XEventListener 90cdf0e10cSrcweir virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException); 91cdf0e10cSrcweir 92cdf0e10cSrcweir private: 93cdf0e10cSrcweir /** ensures that the window's Enabled state matches what is described at the model 94cdf0e10cSrcweir @precond 95cdf0e10cSrcweir our mutex is locked 96cdf0e10cSrcweir */ 97cdf0e10cSrcweir void impl_ensureEnabledState_nothrow_nolck(); 98cdf0e10cSrcweir }; 99cdf0e10cSrcweir 100cdf0e10cSrcweir //-------------------------------------------------------------------- WindowStateGuard_Impl(const Reference<XWindow2> & _rxWindow,const Reference<XPropertySet> & _rxMdelProps)101cdf0e10cSrcweir WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps ) 102cdf0e10cSrcweir :m_xWindow( _rxWindow ) 103cdf0e10cSrcweir ,m_xModelProps( _rxMdelProps ) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir if ( !m_xWindow.is() || !m_xModelProps.is() ) 106cdf0e10cSrcweir throw RuntimeException(); 107cdf0e10cSrcweir 108cdf0e10cSrcweir osl_incrementInterlockedCount( &m_refCount ); 109cdf0e10cSrcweir { 110cdf0e10cSrcweir m_xWindow->addWindowListener( this ); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir osl_decrementInterlockedCount( &m_refCount ); 113cdf0e10cSrcweir } 114cdf0e10cSrcweir 115cdf0e10cSrcweir //-------------------------------------------------------------------- dispose()116cdf0e10cSrcweir void WindowStateGuard_Impl::dispose() 117cdf0e10cSrcweir { 118cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 119cdf0e10cSrcweir if ( !m_xWindow.is() ) 120cdf0e10cSrcweir // already disposed 121cdf0e10cSrcweir return; 122cdf0e10cSrcweir 123cdf0e10cSrcweir m_xWindow->removeWindowListener( this ); 124cdf0e10cSrcweir m_xWindow.clear(); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir 127cdf0e10cSrcweir //-------------------------------------------------------------------- impl_ensureEnabledState_nothrow_nolck()128cdf0e10cSrcweir void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck() 129cdf0e10cSrcweir { 130cdf0e10cSrcweir try 131cdf0e10cSrcweir { 132cdf0e10cSrcweir Reference< XWindow2 > xWindow; 133cdf0e10cSrcweir sal_Bool bEnabled = sal_False; 134cdf0e10cSrcweir sal_Bool bShouldBeEnabled = sal_False; 135cdf0e10cSrcweir { 136cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 137cdf0e10cSrcweir if ( !m_xWindow.is() || !m_xModelProps.is() ) 138cdf0e10cSrcweir return; 139cdf0e10cSrcweir xWindow = m_xWindow; 140cdf0e10cSrcweir bEnabled = xWindow->isEnabled(); 141cdf0e10cSrcweir OSL_VERIFY( m_xModelProps->getPropertyValue( PROPERTY_ENABLED ) >>= bShouldBeEnabled ); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir 144cdf0e10cSrcweir if ( !bShouldBeEnabled && bEnabled && xWindow.is() ) 145cdf0e10cSrcweir xWindow->setEnable( sal_False ); 146cdf0e10cSrcweir } 147cdf0e10cSrcweir catch( const Exception& ) 148cdf0e10cSrcweir { 149cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 150cdf0e10cSrcweir } 151cdf0e10cSrcweir } 152cdf0e10cSrcweir 153cdf0e10cSrcweir //-------------------------------------------------------------------- windowEnabled(const EventObject &)154cdf0e10cSrcweir void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ ) throw (RuntimeException) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir impl_ensureEnabledState_nothrow_nolck(); 157cdf0e10cSrcweir } 158cdf0e10cSrcweir 159cdf0e10cSrcweir //-------------------------------------------------------------------- windowDisabled(const EventObject &)160cdf0e10cSrcweir void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ ) throw (RuntimeException) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir impl_ensureEnabledState_nothrow_nolck(); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir 165cdf0e10cSrcweir //-------------------------------------------------------------------- windowResized(const WindowEvent &)166cdf0e10cSrcweir void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ ) throw (RuntimeException) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir // not interested in 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir //-------------------------------------------------------------------- windowMoved(const WindowEvent &)172cdf0e10cSrcweir void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ ) throw (RuntimeException) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir // not interested in 175cdf0e10cSrcweir } 176cdf0e10cSrcweir 177cdf0e10cSrcweir //-------------------------------------------------------------------- windowShown(const EventObject &)178cdf0e10cSrcweir void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ ) throw (RuntimeException) 179cdf0e10cSrcweir { 180cdf0e10cSrcweir // not interested in 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir //-------------------------------------------------------------------- windowHidden(const EventObject &)184cdf0e10cSrcweir void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ ) throw (RuntimeException) 185cdf0e10cSrcweir { 186cdf0e10cSrcweir // not interested in 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir //-------------------------------------------------------------------- disposing(const EventObject & Source)190cdf0e10cSrcweir void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source ) throw (RuntimeException) 191cdf0e10cSrcweir { 192cdf0e10cSrcweir OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" ); 193cdf0e10cSrcweir (void)Source; 194cdf0e10cSrcweir dispose(); 195cdf0e10cSrcweir } 196cdf0e10cSrcweir 197cdf0e10cSrcweir //==================================================================== 198cdf0e10cSrcweir //= WindowStateGuard 199cdf0e10cSrcweir //==================================================================== 200cdf0e10cSrcweir //-------------------------------------------------------------------- WindowStateGuard()201cdf0e10cSrcweir WindowStateGuard::WindowStateGuard() 202cdf0e10cSrcweir { 203cdf0e10cSrcweir } 204cdf0e10cSrcweir 205cdf0e10cSrcweir //-------------------------------------------------------------------- ~WindowStateGuard()206cdf0e10cSrcweir WindowStateGuard::~WindowStateGuard() 207cdf0e10cSrcweir { 208cdf0e10cSrcweir } 209cdf0e10cSrcweir 210cdf0e10cSrcweir //-------------------------------------------------------------------- attach(const Reference<XWindow2> & _rxWindow,const Reference<XControlModel> & _rxModel)211cdf0e10cSrcweir void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel ) 212cdf0e10cSrcweir { 213cdf0e10cSrcweir if ( m_pImpl.is() ) 214cdf0e10cSrcweir { 215cdf0e10cSrcweir m_pImpl->dispose(); 216cdf0e10cSrcweir m_pImpl = NULL; 217cdf0e10cSrcweir } 218cdf0e10cSrcweir 219cdf0e10cSrcweir Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY ); 220cdf0e10cSrcweir OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" ); 221cdf0e10cSrcweir if ( _rxWindow.is() && xModelProps.is() ) 222cdf0e10cSrcweir m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps ); 223cdf0e10cSrcweir } 224cdf0e10cSrcweir 225cdf0e10cSrcweir //........................................................................ 226cdf0e10cSrcweir } // namespace frm 227cdf0e10cSrcweir //........................................................................ 228cdf0e10cSrcweir 229