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 .
23 #include <com/sun/star/awt/XVclWindowPeer.hpp>
25 #include <comphelper/streamsection.hxx>
26 #include <comphelper/basicio.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <tools/diagnose_ex.h>
29 #include <tools/debug.hxx>
30 #include <tools/urlobj.hxx>
31 #include <vcl/svapp.hxx>
32 #include <osl/mutex.hxx>
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::sdb
;
40 using namespace ::com::sun::star::sdbc
;
41 using namespace ::com::sun::star::sdbcx
;
42 using namespace ::com::sun::star::beans
;
43 using namespace ::com::sun::star::container
;
44 using namespace ::com::sun::star::form
;
45 using namespace ::com::sun::star::awt
;
46 using namespace ::com::sun::star::io
;
47 using namespace ::com::sun::star::lang
;
48 using namespace ::com::sun::star::util
;
49 using ::com::sun::star::frame::XDispatchProviderInterceptor
;
55 OButtonModel::OButtonModel(const Reference
<XComponentContext
>& _rxFactory
)
56 :OClickableImageBaseModel( _rxFactory
, VCL_CONTROLMODEL_COMMANDBUTTON
, FRM_SUN_CONTROL_COMMANDBUTTON
)
57 // use the old control name for compatibility reasons
58 ,m_aResetHelper( *this, m_aMutex
)
59 ,m_eDefaultState( TRISTATE_FALSE
)
61 m_nClassId
= FormComponentType::COMMANDBUTTON
;
65 Any SAL_CALL
OButtonModel::queryAggregation( const Type
& _type
) throw(RuntimeException
, std::exception
)
67 Any aReturn
= OClickableImageBaseModel::queryAggregation( _type
);
68 if ( !aReturn
.hasValue() )
69 aReturn
= OButtonModel_Base::queryInterface( _type
);
74 Sequence
< Type
> OButtonModel::_getTypes()
76 return ::comphelper::concatSequences(
77 OClickableImageBaseModel::_getTypes(),
78 OButtonModel_Base::getTypes()
83 OButtonModel::OButtonModel( const OButtonModel
* _pOriginal
, const Reference
<XComponentContext
>& _rxFactory
)
84 :OClickableImageBaseModel( _pOriginal
, _rxFactory
)
85 ,m_aResetHelper( *this, m_aMutex
)
86 ,m_eDefaultState( _pOriginal
->m_eDefaultState
)
88 m_nClassId
= FormComponentType::COMMANDBUTTON
;
90 implInitializeImageURL();
94 OButtonModel::~OButtonModel()
99 void OButtonModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
101 BEGIN_DESCRIBE_PROPERTIES( 6, OClickableImageBaseModel
)
102 DECL_PROP1( BUTTONTYPE
, FormButtonType
, BOUND
);
103 DECL_PROP1( DEFAULT_STATE
, sal_Int16
, BOUND
);
104 DECL_PROP1( DISPATCHURLINTERNAL
, sal_Bool
, BOUND
);
105 DECL_PROP1( TARGET_URL
, OUString
, BOUND
);
106 DECL_PROP1( TARGET_FRAME
, OUString
, BOUND
);
107 DECL_PROP1( TABINDEX
, sal_Int16
, BOUND
);
108 END_DESCRIBE_PROPERTIES();
112 IMPLEMENT_DEFAULT_CLONING( OButtonModel
)
116 StringSequence
OButtonModel::getSupportedServiceNames() throw(std::exception
)
118 StringSequence aSupported
= OClickableImageBaseModel::getSupportedServiceNames();
119 aSupported
.realloc( aSupported
.getLength() + 2 );
121 OUString
* pArray
= aSupported
.getArray();
122 pArray
[ aSupported
.getLength() - 2 ] = FRM_SUN_COMPONENT_COMMANDBUTTON
;
123 pArray
[ aSupported
.getLength() - 1 ] = FRM_COMPONENT_COMMANDBUTTON
;
129 OUString
OButtonModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException
, std::exception
)
131 return OUString(FRM_COMPONENT_COMMANDBUTTON
); // old (non-sun) name for compatibility !
135 void OButtonModel::write(const Reference
<XObjectOutputStream
>& _rxOutStream
) throw (::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
137 OClickableImageBaseModel::write(_rxOutStream
);
139 _rxOutStream
->writeShort(0x0003); // Version
142 OStreamSection
aSection( _rxOutStream
.get() );
143 // this will allow readers to skip unknown bytes in their dtor
145 _rxOutStream
->writeShort( (sal_uInt16
)m_eButtonType
);
147 OUString sTmp
= INetURLObject::decode( m_sTargetURL
, INetURLObject::DECODE_UNAMBIGUOUS
);
148 _rxOutStream
<< sTmp
;
149 _rxOutStream
<< m_sTargetFrame
;
150 writeHelpTextCompatibly(_rxOutStream
);
151 _rxOutStream
<< isDispatchUrlInternal();
156 void OButtonModel::read(const Reference
<XObjectInputStream
>& _rxInStream
) throw (::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
158 OClickableImageBaseModel::read(_rxInStream
);
160 sal_uInt16 nVersion
= _rxInStream
->readShort(); // Version
165 m_eButtonType
= (FormButtonType
)_rxInStream
->readShort();
167 _rxInStream
>> m_sTargetURL
;
168 _rxInStream
>> m_sTargetFrame
;
174 m_eButtonType
= (FormButtonType
)_rxInStream
->readShort();
176 _rxInStream
>> m_sTargetURL
;
177 _rxInStream
>> m_sTargetFrame
;
178 readHelpTextCompatibly(_rxInStream
);
184 OStreamSection
aSection( _rxInStream
.get() );
185 // this will skip any unknown bytes in it's dtor
188 m_eButtonType
= (FormButtonType
)_rxInStream
->readShort();
191 _rxInStream
>> m_sTargetURL
;
194 _rxInStream
>> m_sTargetFrame
;
197 readHelpTextCompatibly(_rxInStream
);
201 _rxInStream
>> bDispath
;
202 setDispatchUrlInternal(bDispath
);
207 OSL_FAIL("OButtonModel::read : unknown version !");
208 m_eButtonType
= FormButtonType_PUSH
;
209 m_sTargetURL
.clear();
210 m_sTargetFrame
.clear();
216 void SAL_CALL
OButtonModel::disposing()
218 m_aResetHelper
.disposing();
219 OClickableImageBaseModel::disposing();
223 void SAL_CALL
OButtonModel::reset() throw (RuntimeException
, std::exception
)
225 if ( !m_aResetHelper
.approveReset() )
228 impl_resetNoBroadcast_nothrow();
230 m_aResetHelper
.notifyResetted();
234 void SAL_CALL
OButtonModel::addResetListener( const Reference
< XResetListener
>& _listener
) throw (RuntimeException
, std::exception
)
236 m_aResetHelper
.addResetListener( _listener
);
240 void SAL_CALL
OButtonModel::removeResetListener( const Reference
< XResetListener
>& _listener
) throw (RuntimeException
, std::exception
)
242 m_aResetHelper
.removeResetListener( _listener
);
246 void SAL_CALL
OButtonModel::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
250 case PROPERTY_ID_DEFAULT_STATE
:
251 _rValue
<<= (sal_Int16
)m_eDefaultState
;
255 OClickableImageBaseModel::getFastPropertyValue( _rValue
, _nHandle
);
261 void SAL_CALL
OButtonModel::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle
, const Any
& _rValue
) throw (Exception
, std::exception
)
265 case PROPERTY_ID_DEFAULT_STATE
:
267 sal_Int16
nDefaultState( (sal_Int16
)TRISTATE_FALSE
);
268 OSL_VERIFY( _rValue
>>= nDefaultState
);
269 m_eDefaultState
= (ToggleState
)nDefaultState
;
270 impl_resetNoBroadcast_nothrow();
275 OClickableImageBaseModel::setFastPropertyValue_NoBroadcast( _nHandle
, _rValue
);
281 sal_Bool SAL_CALL
OButtonModel::convertFastPropertyValue( Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
) throw (IllegalArgumentException
)
283 bool bModified
= false;
286 case PROPERTY_ID_DEFAULT_STATE
:
287 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, (sal_Int16
)m_eDefaultState
);
291 bModified
= OClickableImageBaseModel::convertFastPropertyValue( _rConvertedValue
, _rOldValue
, _nHandle
, _rValue
);
298 Any
OButtonModel::getPropertyDefaultByHandle( sal_Int32 _nHandle
) const
303 case PROPERTY_ID_DEFAULT_STATE
:
304 aDefault
<<= (sal_Int16
)TRISTATE_FALSE
;
308 aDefault
= OClickableImageBaseModel::getPropertyDefaultByHandle( _nHandle
);
315 void OButtonModel::impl_resetNoBroadcast_nothrow()
319 setPropertyValue( PROPERTY_STATE
, getPropertyValue( PROPERTY_DEFAULT_STATE
) );
321 catch( const Exception
& )
323 DBG_UNHANDLED_EXCEPTION();
331 Sequence
<Type
> OButtonControl::_getTypes()
333 return ::comphelper::concatSequences(
334 OButtonControl_BASE::getTypes(),
335 OClickableImageBaseControl::_getTypes(),
336 OFormNavigationHelper::getTypes()
341 StringSequence
OButtonControl::getSupportedServiceNames() throw(std::exception
)
343 StringSequence aSupported
= OClickableImageBaseControl::getSupportedServiceNames();
344 aSupported
.realloc(aSupported
.getLength() + 2);
346 OUString
*pArray
= aSupported
.getArray();
347 pArray
[aSupported
.getLength()-2] = FRM_SUN_CONTROL_COMMANDBUTTON
;
348 pArray
[aSupported
.getLength()-1] = STARDIV_ONE_FORM_CONTROL_COMMANDBUTTON
;
353 OButtonControl::OButtonControl(const Reference
<XComponentContext
>& _rxFactory
)
354 :OClickableImageBaseControl(_rxFactory
, VCL_CONTROL_COMMANDBUTTON
)
355 ,OFormNavigationHelper( _rxFactory
)
357 ,m_nTargetUrlFeatureId( -1 )
358 ,m_bEnabledByPropertyValue( false )
360 osl_atomic_increment(&m_refCount
);
362 // Register as ActionListener
363 Reference
<XButton
> xButton
;
364 query_aggregation( m_xAggregate
, xButton
);
366 xButton
->addActionListener(this);
368 // For Listener: refcount at one
369 osl_atomic_decrement(&m_refCount
);
373 OButtonControl::~OButtonControl()
376 Application::RemoveUserEvent(m_nClickEvent
);
381 Any SAL_CALL
OButtonControl::queryAggregation(const Type
& _rType
) throw (RuntimeException
, std::exception
)
383 // if asked for the XTypeProvider, don't let OButtonControl_BASE do this
385 if ( !_rType
.equals( cppu::UnoType
<XTypeProvider
>::get() ) )
386 aReturn
= OButtonControl_BASE::queryInterface( _rType
);
388 if ( !aReturn
.hasValue() )
389 aReturn
= OClickableImageBaseControl::queryAggregation( _rType
);
391 if ( !aReturn
.hasValue() )
392 aReturn
= OFormNavigationHelper::queryInterface( _rType
);
398 void SAL_CALL
OButtonControl::disposing()
400 startOrStopModelPropertyListening( false );
402 OClickableImageBaseControl::disposing();
403 OFormNavigationHelper::dispose();
407 void SAL_CALL
OButtonControl::disposing( const EventObject
& _rSource
) throw( RuntimeException
, std::exception
)
409 OControl::disposing( _rSource
);
410 OFormNavigationHelper::disposing( _rSource
);
415 void OButtonControl::actionPerformed(const ActionEvent
& /*rEvent*/) throw ( ::com::sun::star::uno::RuntimeException
, std::exception
)
417 // Asynchronous for css::util::URL-Button
418 ImplSVEvent
* n
= Application::PostUserEvent( LINK(this, OButtonControl
,OnClick
) );
420 ::osl::MutexGuard
aGuard( m_aMutex
);
426 IMPL_LINK_NOARG(OButtonControl
, OnClick
)
428 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
431 if (m_aApproveActionListeners
.getLength())
433 // if there are listeners, start the action in an own thread, to not allow
434 // them to block us here (we're in the application's main thread)
435 getImageProducerThread()->addEvent();
439 // Else, don't. We then must not notify the Listeners in any case,
440 // not even if added later on.
443 // recognize the button type
444 Reference
<XPropertySet
> xSet(getModel(), UNO_QUERY
);
448 if (FormButtonType_PUSH
== *static_cast<FormButtonType
const *>(xSet
->getPropertyValue(PROPERTY_BUTTONTYPE
).getValue()))
450 // notify the action listeners for a push button
451 ::cppu::OInterfaceIteratorHelper
aIter(m_aActionListeners
);
452 ActionEvent
aEvt(static_cast<XWeak
*>(this), m_aActionCommand
);
453 while(aIter
.hasMoreElements() )
456 // and catch them on a per-listener basis - if one listener fails, the others still need
460 static_cast< XActionListener
* >( aIter
.next() )->actionPerformed(aEvt
);
463 catch( const RuntimeException
& )
468 catch( const Exception
& )
470 OSL_FAIL( "OButtonControl::OnClick: caught a exception other than RuntimeException!" );
475 actionPerformed_Impl( false, ::com::sun::star::awt::MouseEvent() );
481 void OButtonControl::actionPerformed_Impl( bool _bNotifyListener
, const ::com::sun::star::awt::MouseEvent
& _rEvt
)
484 sal_Int16 nFeatureId
= -1;
486 ::osl::MutexGuard
aGuard( m_aMutex
);
487 nFeatureId
= m_nTargetUrlFeatureId
;
490 if ( nFeatureId
!= -1 )
492 if ( !approveAction() )
495 SolarMutexGuard aGuard
;
496 dispatch( nFeatureId
);
501 OClickableImageBaseControl::actionPerformed_Impl( _bNotifyListener
, _rEvt
);
506 void OButtonControl::setLabel(const OUString
& Label
) throw( RuntimeException
, std::exception
)
508 Reference
<XButton
> xButton
;
509 query_aggregation( m_xAggregate
, xButton
);
511 xButton
->setLabel(Label
);
515 void SAL_CALL
OButtonControl::setActionCommand(const OUString
& _rCommand
) throw( RuntimeException
, std::exception
)
518 ::osl::MutexGuard
aGuard( m_aMutex
);
519 m_aActionCommand
= _rCommand
;
522 Reference
<XButton
> xButton
;
523 query_aggregation( m_xAggregate
, xButton
);
525 xButton
->setActionCommand(_rCommand
);
529 void SAL_CALL
OButtonControl::addActionListener(const Reference
<XActionListener
>& _rxListener
) throw( RuntimeException
, std::exception
)
531 m_aActionListeners
.addInterface(_rxListener
);
535 void SAL_CALL
OButtonControl::removeActionListener(const Reference
<XActionListener
>& _rxListener
) throw( RuntimeException
, std::exception
)
537 m_aActionListeners
.removeInterface(_rxListener
);
541 class DoPropertyListening
544 Reference
< XPropertySet
> m_xProps
;
545 Reference
< XPropertyChangeListener
> m_xListener
;
546 bool m_bStartListening
;
550 const Reference
< XInterface
>& _rxComponent
,
551 const Reference
< XPropertyChangeListener
>& _rxListener
,
555 void handleListening( const OUString
& _rPropertyName
);
559 DoPropertyListening::DoPropertyListening(
560 const Reference
< XInterface
>& _rxComponent
, const Reference
< XPropertyChangeListener
>& _rxListener
,
562 :m_xProps( _rxComponent
, UNO_QUERY
)
563 ,m_xListener( _rxListener
)
564 ,m_bStartListening( _bStart
)
566 DBG_ASSERT( m_xProps
.is() || !_rxComponent
.is(), "DoPropertyListening::DoPropertyListening: valid component, but no property set!" );
567 DBG_ASSERT( m_xListener
.is(), "DoPropertyListening::DoPropertyListening: invalid listener!" );
571 void DoPropertyListening::handleListening( const OUString
& _rPropertyName
)
575 if ( m_bStartListening
)
576 m_xProps
->addPropertyChangeListener( _rPropertyName
, m_xListener
);
578 m_xProps
->removePropertyChangeListener( _rPropertyName
, m_xListener
);
583 void OButtonControl::startOrStopModelPropertyListening( bool _bStart
)
585 DoPropertyListening
aListeningHandler( getModel(), this, _bStart
);
586 aListeningHandler
.handleListening( PROPERTY_TARGET_URL
);
587 aListeningHandler
.handleListening( PROPERTY_BUTTONTYPE
);
588 aListeningHandler
.handleListening( PROPERTY_ENABLED
);
592 sal_Bool SAL_CALL
OButtonControl::setModel( const Reference
< XControlModel
>& _rxModel
) throw ( RuntimeException
, std::exception
)
594 startOrStopModelPropertyListening( false );
595 bool bResult
= OClickableImageBaseControl::setModel( _rxModel
);
596 startOrStopModelPropertyListening( true );
598 m_bEnabledByPropertyValue
= true;
599 Reference
< XPropertySet
> xModelProps( _rxModel
, UNO_QUERY
);
600 if ( xModelProps
.is() )
601 xModelProps
->getPropertyValue( PROPERTY_ENABLED
) >>= m_bEnabledByPropertyValue
;
603 modelFeatureUrlPotentiallyChanged( );
609 void OButtonControl::modelFeatureUrlPotentiallyChanged( )
611 sal_Int16 nOldUrlFeatureId
= m_nTargetUrlFeatureId
;
613 // Do we have another TargetURL now? If so, we need to update our dispatches
614 m_nTargetUrlFeatureId
= getModelUrlFeatureId( );
615 if ( nOldUrlFeatureId
!= m_nTargetUrlFeatureId
)
617 invalidateSupportedFeaturesSet();
618 if ( !isDesignMode() )
624 void SAL_CALL
OButtonControl::propertyChange( const PropertyChangeEvent
& _rEvent
) throw ( RuntimeException
, std::exception
)
626 if ( _rEvent
.PropertyName
== PROPERTY_TARGET_URL
627 || _rEvent
.PropertyName
== PROPERTY_BUTTONTYPE
630 modelFeatureUrlPotentiallyChanged( );
632 else if ( _rEvent
.PropertyName
== PROPERTY_ENABLED
)
634 _rEvent
.NewValue
>>= m_bEnabledByPropertyValue
;
641 bool isFormControllerURL( const OUString
& _rURL
)
643 return ( _rURL
.getLength() > RTL_CONSTASCII_LENGTH( ".uno:FormController/" ) )
644 && ( _rURL
.startsWith( ".uno:FormController/" ) );
649 sal_Int16
OButtonControl::getModelUrlFeatureId( ) const
651 sal_Int16 nFeatureId
= -1;
653 // some URL related properties of the model
655 FormButtonType eButtonType
= FormButtonType_PUSH
;
657 Reference
< XPropertySet
> xModelProps( const_cast< OButtonControl
* >( this )->getModel(), UNO_QUERY
);
658 if ( xModelProps
.is() )
660 xModelProps
->getPropertyValue( PROPERTY_TARGET_URL
) >>= sUrl
;
661 xModelProps
->getPropertyValue( PROPERTY_BUTTONTYPE
) >>= eButtonType
;
664 // are we a URL button?
665 if ( eButtonType
== FormButtonType_URL
)
667 // is it a feature URL?
668 if ( isFormControllerURL( sUrl
) )
670 nFeatureId
= OFormNavigationMapper::getFeatureId( sUrl
);
678 void SAL_CALL
OButtonControl::setDesignMode( sal_Bool _bOn
) throw( RuntimeException
, std::exception
)
680 OClickableImageBaseControl::setDesignMode( _bOn
);
683 disconnectDispatchers();
685 connectDispatchers();
686 // this will connect if not already connected and just update else
690 void OButtonControl::getSupportedFeatures( ::std::vector
< sal_Int16
>& /* [out] */ _rFeatureIds
)
692 if ( -1 != m_nTargetUrlFeatureId
)
693 _rFeatureIds
.push_back( m_nTargetUrlFeatureId
);
697 void OButtonControl::featureStateChanged( sal_Int16 _nFeatureId
, bool _bEnabled
)
699 if ( _nFeatureId
== m_nTargetUrlFeatureId
)
701 // enable or disable our peer, according to the new state
702 Reference
< XVclWindowPeer
> xPeer( getPeer(), UNO_QUERY
);
704 xPeer
->setProperty( PROPERTY_ENABLED
, makeAny( m_bEnabledByPropertyValue
&& _bEnabled
) );
705 // if we're disabled according to our model's property, then
706 // we don't care for the feature state, but *are* disabled.
707 // If the model's property states that we're enabled, then we *do*
708 // care for the feature state
712 OFormNavigationHelper::featureStateChanged( _nFeatureId
, _bEnabled
);
716 void OButtonControl::allFeatureStatesChanged( )
718 if ( -1 != m_nTargetUrlFeatureId
)
719 // we have only one supported feature, so simulate it has changed ...
720 featureStateChanged( m_nTargetUrlFeatureId
, isEnabled( m_nTargetUrlFeatureId
) );
723 OFormNavigationHelper::allFeatureStatesChanged( );
727 bool OButtonControl::isEnabled( sal_Int16 _nFeatureId
) const
729 if ( const_cast< OButtonControl
* >( this )->isDesignMode() )
730 // TODO: the model property?
733 return OFormNavigationHelper::isEnabled( _nFeatureId
);
737 void SAL_CALL
OButtonControl::registerDispatchProviderInterceptor( const Reference
< XDispatchProviderInterceptor
>& _rxInterceptor
) throw (RuntimeException
, std::exception
)
739 OClickableImageBaseControl::registerDispatchProviderInterceptor( _rxInterceptor
);
740 OFormNavigationHelper::registerDispatchProviderInterceptor( _rxInterceptor
);
744 void SAL_CALL
OButtonControl::releaseDispatchProviderInterceptor( const Reference
< XDispatchProviderInterceptor
>& _rxInterceptor
) throw (RuntimeException
, std::exception
)
746 OClickableImageBaseControl::releaseDispatchProviderInterceptor( _rxInterceptor
);
747 OFormNavigationHelper::releaseDispatchProviderInterceptor( _rxInterceptor
);
752 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
753 com_sun_star_form_OButtonModel_get_implementation(::com::sun::star::uno::XComponentContext
* component
,
754 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
756 return cppu::acquire(new frm::OButtonModel(component
));
759 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
760 com_sun_star_form_OButtonControl_get_implementation(::com::sun::star::uno::XComponentContext
* component
,
761 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
763 return cppu::acquire(new frm::OButtonControl(component
));
766 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */