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 <svtools/toolboxcontroller.hxx>
21 #include <com/sun/star/beans/PropertyAttribute.hpp>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/frame/XDispatchProvider.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/frame/XLayoutManager.hpp>
28 #include <com/sun/star/util/URLTransformer.hpp>
29 #include <vcl/svapp.hxx>
30 #include <toolkit/helper/vclunohelper.hxx>
31 #include <vcl/toolbox.hxx>
32 #include <vcl/weldutils.hxx>
33 #include <comphelper/processfactory.hxx>
35 const int TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
= 1;
36 const char TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE
[] = "SupportsVisible";
39 using namespace ::cppu
;
40 using namespace css::awt
;
41 using namespace css::uno
;
42 using namespace css::util
;
43 using namespace css::beans
;
44 using namespace css::lang
;
45 using namespace css::frame
;
50 ToolboxController::ToolboxController(
51 const Reference
< XComponentContext
>& rxContext
,
52 const Reference
< XFrame
>& xFrame
,
53 const OUString
& aCommandURL
) :
54 OPropertyContainer( GetBroadcastHelper() )
55 , m_bSupportVisible( false )
56 , m_bInitialized( false )
57 , m_bDisposed( false )
59 , m_nToolBoxId( SAL_MAX_UINT16
)
61 , m_xContext( rxContext
)
62 , m_aCommandURL( aCommandURL
)
63 , m_aListenerContainer( m_aMutex
)
67 OSL_ASSERT( m_xContext
.is() );
68 registerProperty( TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE
,
69 TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
,
70 css::beans::PropertyAttribute::TRANSIENT
| css::beans::PropertyAttribute::READONLY
,
71 &m_bSupportVisible
, cppu::UnoType
<decltype(m_bSupportVisible
)>::get());
75 m_xUrlTransformer
= URLTransformer::create( rxContext
);
77 catch(const Exception
&)
82 ToolboxController::ToolboxController() :
83 OPropertyContainer(GetBroadcastHelper())
84 , m_bSupportVisible(false)
85 , m_bInitialized( false )
86 , m_bDisposed( false )
88 , m_nToolBoxId( SAL_MAX_UINT16
)
89 , m_aListenerContainer( m_aMutex
)
93 registerProperty( TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE
,
94 TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
,
95 css::beans::PropertyAttribute::TRANSIENT
| css::beans::PropertyAttribute::READONLY
,
96 &m_bSupportVisible
, cppu::UnoType
<decltype(m_bSupportVisible
)>::get());
99 ToolboxController::~ToolboxController()
103 Reference
< XFrame
> ToolboxController::getFrameInterface() const
105 SolarMutexGuard aSolarMutexGuard
;
109 const Reference
< XComponentContext
> & ToolboxController::getContext() const
111 SolarMutexGuard aSolarMutexGuard
;
115 Reference
< XLayoutManager
> ToolboxController::getLayoutManager() const
117 Reference
< XLayoutManager
> xLayoutManager
;
118 Reference
< XPropertySet
> xPropSet
;
120 SolarMutexGuard aSolarMutexGuard
;
121 xPropSet
.set( m_xFrame
, UNO_QUERY
);
128 xLayoutManager
.set(xPropSet
->getPropertyValue("LayoutManager"),UNO_QUERY
);
135 return xLayoutManager
;
139 Any SAL_CALL
ToolboxController::queryInterface( const Type
& rType
)
141 css::uno::Any
a(ToolboxController_Base::queryInterface(rType
));
142 return a
.hasValue() ? a
: OPropertyContainer::queryInterface(rType
);
145 void SAL_CALL
ToolboxController::acquire() throw ()
147 ToolboxController_Base::acquire();
150 void SAL_CALL
ToolboxController::release() throw ()
152 ToolboxController_Base::release();
155 css::uno::Sequence
<css::uno::Type
> ToolboxController::getTypes()
157 return comphelper::concatSequences(ToolboxController_Base::getTypes(),
161 void SAL_CALL
ToolboxController::initialize( const Sequence
< Any
>& aArguments
)
163 bool bInitialized( true );
166 SolarMutexGuard aSolarMutexGuard
;
169 throw DisposedException();
171 bInitialized
= m_bInitialized
;
177 SolarMutexGuard aSolarMutexGuard
;
178 m_bInitialized
= true;
179 m_bSupportVisible
= false;
180 PropertyValue aPropValue
;
181 for ( const auto& rArgument
: aArguments
)
183 if ( rArgument
>>= aPropValue
)
185 if ( aPropValue
.Name
== "Frame" )
186 m_xFrame
.set(aPropValue
.Value
,UNO_QUERY
);
187 else if ( aPropValue
.Name
== "CommandURL" )
188 aPropValue
.Value
>>= m_aCommandURL
;
189 else if ( aPropValue
.Name
== "ServiceManager" )
191 Reference
<XMultiServiceFactory
> xMSF(aPropValue
.Value
, UNO_QUERY
);
193 m_xContext
= comphelper::getComponentContext(xMSF
);
195 else if ( aPropValue
.Name
== "ParentWindow" )
196 m_xParentWindow
.set(aPropValue
.Value
,UNO_QUERY
);
197 else if ( aPropValue
.Name
== "ModuleIdentifier" )
198 aPropValue
.Value
>>= m_sModuleName
;
199 else if ( aPropValue
.Name
== "Identifier" )
200 aPropValue
.Value
>>= m_nToolBoxId
;
201 else if ( aPropValue
.Name
== "IsSidebar" )
202 aPropValue
.Value
>>= m_bSidebar
;
208 if ( !m_xUrlTransformer
.is() && m_xContext
.is() )
209 m_xUrlTransformer
= URLTransformer::create( m_xContext
);
211 catch(const Exception
&)
215 if ( !m_aCommandURL
.isEmpty() )
216 m_aListenerMap
.emplace( m_aCommandURL
, Reference
< XDispatch
>() );
218 if (weld::TransportAsXWindow
* pTunnel
= dynamic_cast<weld::TransportAsXWindow
*>(getParent().get()))
220 m_pToolbar
= dynamic_cast<weld::Toolbar
*>(pTunnel
->getWidget());
221 assert(m_pToolbar
&& "must be a toolbar");
222 m_pBuilder
= pTunnel
->getBuilder();
226 void SAL_CALL
ToolboxController::update()
229 SolarMutexGuard aSolarMutexGuard
;
231 throw DisposedException();
234 // Bind all registered listeners to their dispatch objects
239 void SAL_CALL
ToolboxController::dispose()
241 Reference
< XComponent
> xThis( static_cast< OWeakObject
* >(this), UNO_QUERY
);
244 SolarMutexGuard aSolarMutexGuard
;
246 throw DisposedException();
249 css::lang::EventObject
aEvent( xThis
);
250 m_aListenerContainer
.disposeAndClear( aEvent
);
252 SolarMutexGuard aSolarMutexGuard
;
253 Reference
< XStatusListener
> xStatusListener( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
254 for (auto const& listener
: m_aListenerMap
)
258 Reference
< XDispatch
> xDispatch( listener
.second
);
260 css::util::URL aTargetURL
;
261 aTargetURL
.Complete
= listener
.first
;
262 if ( m_xUrlTransformer
.is() )
263 m_xUrlTransformer
->parseStrict( aTargetURL
);
265 if ( xDispatch
.is() && xStatusListener
.is() )
266 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
277 void SAL_CALL
ToolboxController::addEventListener( const Reference
< XEventListener
>& xListener
)
279 m_aListenerContainer
.addInterface( cppu::UnoType
<XEventListener
>::get(), xListener
);
282 void SAL_CALL
ToolboxController::removeEventListener( const Reference
< XEventListener
>& aListener
)
284 m_aListenerContainer
.removeInterface( cppu::UnoType
<XEventListener
>::get(), aListener
);
288 void SAL_CALL
ToolboxController::disposing( const EventObject
& Source
)
290 Reference
< XInterface
> xSource( Source
.Source
);
292 SolarMutexGuard aSolarMutexGuard
;
297 for (auto & listener
: m_aListenerMap
)
299 // Compare references and release dispatch references if they are equal.
300 Reference
< XInterface
> xIfac(listener
.second
, UNO_QUERY
);
301 if ( xSource
== xIfac
)
302 listener
.second
.clear();
305 Reference
< XInterface
> xIfac( m_xFrame
, UNO_QUERY
);
306 if ( xIfac
== xSource
)
311 void SAL_CALL
ToolboxController::statusChanged( const FeatureStateEvent
& )
313 // must be implemented by sub class
316 // XToolbarController
317 void SAL_CALL
ToolboxController::execute( sal_Int16 KeyModifier
)
319 Reference
< XDispatch
> xDispatch
;
320 OUString aCommandURL
;
323 SolarMutexGuard aSolarMutexGuard
;
326 throw DisposedException();
328 if ( m_bInitialized
&&
330 !m_aCommandURL
.isEmpty() )
332 aCommandURL
= m_aCommandURL
;
333 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( m_aCommandURL
);
334 if ( pIter
!= m_aListenerMap
.end() )
335 xDispatch
= pIter
->second
;
339 if ( !xDispatch
.is() )
344 css::util::URL aTargetURL
;
345 Sequence
<PropertyValue
> aArgs( 1 );
347 // Provide key modifier information to dispatch function
348 aArgs
[0].Name
= "KeyModifier";
349 aArgs
[0].Value
<<= KeyModifier
;
351 aTargetURL
.Complete
= aCommandURL
;
352 if ( m_xUrlTransformer
.is() )
353 m_xUrlTransformer
->parseStrict( aTargetURL
);
354 xDispatch
->dispatch( aTargetURL
, aArgs
);
356 catch ( DisposedException
& )
361 void SAL_CALL
ToolboxController::click()
365 void SAL_CALL
ToolboxController::doubleClick()
369 Reference
< XWindow
> SAL_CALL
ToolboxController::createPopupWindow()
371 return Reference
< XWindow
>();
374 Reference
< XWindow
> SAL_CALL
ToolboxController::createItemWindow( const Reference
< XWindow
>& )
376 return Reference
< XWindow
>();
379 void ToolboxController::addStatusListener( const OUString
& aCommandURL
)
381 Reference
< XDispatch
> xDispatch
;
382 Reference
< XStatusListener
> xStatusListener
;
383 css::util::URL aTargetURL
;
386 SolarMutexGuard aSolarMutexGuard
;
387 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( aCommandURL
);
389 // Already in the list of status listener. Do nothing.
390 if ( pIter
!= m_aListenerMap
.end() )
393 // Check if we are already initialized. Implementation starts adding itself as status listener when
394 // initialize is called.
395 if ( !m_bInitialized
)
397 // Put into the unordered_map of status listener. Will be activated when initialized is called
398 m_aListenerMap
.emplace( aCommandURL
, Reference
< XDispatch
>() );
403 // Add status listener directly as initialize has already been called.
404 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
405 if ( m_xContext
.is() && xDispatchProvider
.is() )
407 aTargetURL
.Complete
= aCommandURL
;
408 if ( m_xUrlTransformer
.is() )
409 m_xUrlTransformer
->parseStrict( aTargetURL
);
410 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
412 xStatusListener
.set( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
413 URLToDispatchMap::iterator aIter
= m_aListenerMap
.find( aCommandURL
);
414 if ( aIter
!= m_aListenerMap
.end() )
416 Reference
< XDispatch
> xOldDispatch( aIter
->second
);
417 aIter
->second
= xDispatch
;
421 if ( xOldDispatch
.is() )
422 xOldDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
429 m_aListenerMap
.emplace( aCommandURL
, xDispatch
);
434 // Call without locked mutex as we are called back from dispatch implementation
437 if ( xDispatch
.is() )
438 xDispatch
->addStatusListener( xStatusListener
, aTargetURL
);
445 void ToolboxController::removeStatusListener( const OUString
& aCommandURL
)
447 SolarMutexGuard aSolarMutexGuard
;
449 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( aCommandURL
);
450 if ( pIter
== m_aListenerMap
.end() )
453 Reference
< XDispatch
> xDispatch( pIter
->second
);
454 Reference
< XStatusListener
> xStatusListener( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
455 m_aListenerMap
.erase( pIter
);
459 css::util::URL aTargetURL
;
460 aTargetURL
.Complete
= aCommandURL
;
461 if ( m_xUrlTransformer
.is() )
462 m_xUrlTransformer
->parseStrict( aTargetURL
);
464 if ( xDispatch
.is() && xStatusListener
.is() )
465 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
472 void ToolboxController::bindListener()
474 std::vector
< Listener
> aDispatchVector
;
475 Reference
< XStatusListener
> xStatusListener
;
478 SolarMutexGuard aSolarMutexGuard
;
480 if ( !m_bInitialized
)
483 // Collect all registered command URL's and store them temporary
484 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
485 if ( m_xContext
.is() && xDispatchProvider
.is() )
487 xStatusListener
.set( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
488 for (auto & listener
: m_aListenerMap
)
490 css::util::URL aTargetURL
;
491 aTargetURL
.Complete
= listener
.first
;
492 if ( m_xUrlTransformer
.is() )
493 m_xUrlTransformer
->parseStrict( aTargetURL
);
495 Reference
< XDispatch
> xDispatch(listener
.second
);
496 if ( xDispatch
.is() )
498 // We already have a dispatch object => we have to requery.
499 // Release old dispatch object and remove it as listener
502 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
509 listener
.second
.clear();
512 // Query for dispatch object. Old dispatch will be released with this, too.
515 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
520 listener
.second
= xDispatch
;
522 Listener
aListener( aTargetURL
, xDispatch
);
523 aDispatchVector
.push_back( aListener
);
528 // Call without locked mutex as we are called back from dispatch implementation
529 if ( !xStatusListener
.is() )
534 for (Listener
& rListener
: aDispatchVector
)
536 if ( rListener
.xDispatch
.is() )
537 rListener
.xDispatch
->addStatusListener( xStatusListener
, rListener
.aURL
);
538 else if ( rListener
.aURL
.Complete
== m_aCommandURL
)
542 // Send status changed for the main URL, if we cannot get a valid dispatch object.
543 // UI disables the button. Catch exception as we release our mutex, it is possible
544 // that someone else already disposed this instance!
545 FeatureStateEvent aFeatureStateEvent
;
546 aFeatureStateEvent
.IsEnabled
= false;
547 aFeatureStateEvent
.FeatureURL
= rListener
.aURL
;
548 aFeatureStateEvent
.State
= Any();
549 xStatusListener
->statusChanged( aFeatureStateEvent
);
562 void ToolboxController::unbindListener()
564 SolarMutexGuard aSolarMutexGuard
;
566 if ( !m_bInitialized
)
569 // Collect all registered command URL's and store them temporary
570 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
571 if ( !(m_xContext
.is() && xDispatchProvider
.is()) )
574 Reference
< XStatusListener
> xStatusListener( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
575 for (auto & listener
: m_aListenerMap
)
577 css::util::URL aTargetURL
;
578 aTargetURL
.Complete
= listener
.first
;
579 if ( m_xUrlTransformer
.is() )
580 m_xUrlTransformer
->parseStrict( aTargetURL
);
582 Reference
< XDispatch
> xDispatch(listener
.second
);
583 if ( xDispatch
.is() )
585 // We already have a dispatch object => we have to requery.
586 // Release old dispatch object and remove it as listener
589 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
595 listener
.second
.clear();
599 void ToolboxController::updateStatus()
604 void ToolboxController::updateStatus( const OUString
& aCommandURL
)
606 Reference
< XDispatch
> xDispatch
;
607 Reference
< XStatusListener
> xStatusListener
;
608 css::util::URL aTargetURL
;
611 SolarMutexGuard aSolarMutexGuard
;
613 if ( !m_bInitialized
)
616 // Try to find a dispatch object for the requested command URL
617 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
618 xStatusListener
.set( static_cast< OWeakObject
* >( this ), UNO_QUERY
);
619 if ( m_xContext
.is() && xDispatchProvider
.is() )
621 aTargetURL
.Complete
= aCommandURL
;
622 if ( m_xUrlTransformer
.is() )
623 m_xUrlTransformer
->parseStrict( aTargetURL
);
624 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
628 if ( !(xDispatch
.is() && xStatusListener
.is()) )
631 // Catch exception as we release our mutex, it is possible that someone else
632 // has already disposed this instance!
633 // Add/remove status listener to get an update status information from the
634 // requested command.
637 xDispatch
->addStatusListener( xStatusListener
, aTargetURL
);
638 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
646 void ToolboxController::dispatchCommand( const OUString
& sCommandURL
, const Sequence
< PropertyValue
>& rArgs
, const OUString
&sTarget
)
650 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY_THROW
);
652 aURL
.Complete
= sCommandURL
;
653 getURLTransformer()->parseStrict( aURL
);
655 Reference
< XDispatch
> xDispatch( xDispatchProvider
->queryDispatch( aURL
, sTarget
, 0 ), UNO_SET_THROW
);
657 std::unique_ptr
<DispatchInfo
> pDispatchInfo(new DispatchInfo( xDispatch
, aURL
, rArgs
));
658 if ( Application::PostUserEvent( LINK(nullptr, ToolboxController
, ExecuteHdl_Impl
),
659 pDispatchInfo
.get() ) )
660 pDispatchInfo
.release();
669 css::uno::Reference
< css::beans::XPropertySetInfo
> SAL_CALL
ToolboxController::getPropertySetInfo()
671 Reference
<XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
675 ::cppu::IPropertyArrayHelper
& ToolboxController::getInfoHelper()
677 return *getArrayHelper();
681 ::cppu::IPropertyArrayHelper
* ToolboxController::createArrayHelper( ) const
683 css::uno::Sequence
< Property
> aProps
;
684 describeProperties(aProps
);
685 return new ::cppu::OPropertyArrayHelper(aProps
);
688 sal_Bool SAL_CALL
ToolboxController::convertFastPropertyValue( css::uno::Any
& aConvertedValue
,
689 css::uno::Any
& aOldValue
,
691 const css::uno::Any
& aValue
)
695 case TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
:
697 bool aNewValue(false);
698 aValue
>>= aNewValue
;
699 if (aNewValue
!= m_bSupportVisible
)
701 aConvertedValue
<<= aNewValue
;
702 aOldValue
<<= m_bSupportVisible
;
708 return OPropertyContainer::convertFastPropertyValue(aConvertedValue
, aOldValue
, nHandle
, aValue
);
711 void SAL_CALL
ToolboxController::setFastPropertyValue_NoBroadcast(
713 const css::uno::Any
& aValue
)
715 OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle
, aValue
);
716 if (TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
== nHandle
)
719 if (( aValue
>>= rValue
) && m_bInitialized
)
720 m_bSupportVisible
= rValue
;
725 IMPL_STATIC_LINK( ToolboxController
, ExecuteHdl_Impl
, void*, p
, void )
727 DispatchInfo
* pDispatchInfo
= static_cast<DispatchInfo
*>(p
);
728 pDispatchInfo
->mxDispatch
->dispatch( pDispatchInfo
->maURL
, pDispatchInfo
->maArgs
);
729 delete pDispatchInfo
;
732 void ToolboxController::enable( bool bEnable
)
734 ToolBox
* pToolBox
= nullptr;
735 sal_uInt16 nItemId
= 0;
736 if( getToolboxId( nItemId
, &pToolBox
) )
738 pToolBox
->EnableItem( nItemId
, bEnable
);
742 bool ToolboxController::getToolboxId( sal_uInt16
& rItemId
, ToolBox
** ppToolBox
)
744 if( (m_nToolBoxId
!= SAL_MAX_UINT16
) && (ppToolBox
== nullptr) )
747 ToolBox
* pToolBox
= static_cast< ToolBox
* >( VCLUnoHelper::GetWindow( getParent() ).get() );
749 if( (m_nToolBoxId
== SAL_MAX_UINT16
) && pToolBox
)
751 const ToolBox::ImplToolItems::size_type nCount
= pToolBox
->GetItemCount();
752 for ( ToolBox::ImplToolItems::size_type nPos
= 0; nPos
< nCount
; ++nPos
)
754 const sal_uInt16 nItemId
= pToolBox
->GetItemId( nPos
);
755 if ( pToolBox
->GetItemCommand( nItemId
) == m_aCommandURL
)
757 m_nToolBoxId
= nItemId
;
764 *ppToolBox
= pToolBox
;
766 rItemId
= m_nToolBoxId
;
768 return (rItemId
!= SAL_MAX_UINT16
) && (( ppToolBox
== nullptr) || (*ppToolBox
!= nullptr) );
774 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */