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>
30 #include <vcl/commandinfoprovider.hxx>
31 #include <vcl/svapp.hxx>
32 #include <toolkit/helper/vclunohelper.hxx>
33 #include <vcl/toolbox.hxx>
34 #include <vcl/weldutils.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <comphelper/propertyvalue.hxx>
38 const int TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
= 1;
39 constexpr OUString TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE
= u
"SupportsVisible"_ustr
;
42 using namespace ::cppu
;
43 using namespace css::awt
;
44 using namespace css::uno
;
45 using namespace css::util
;
46 using namespace css::beans
;
47 using namespace css::lang
;
48 using namespace css::frame
;
53 ToolboxController::ToolboxController(
54 const Reference
< XComponentContext
>& rxContext
,
55 const Reference
< XFrame
>& xFrame
,
56 OUString aCommandURL
) :
57 OPropertyContainer( GetBroadcastHelper() )
58 , m_bSupportVisible( false )
59 , m_bInitialized( false )
60 , m_bDisposed( false )
62 , m_nToolBoxId( SAL_MAX_UINT16
)
64 , m_xContext( rxContext
)
65 , m_aCommandURL(std::move( aCommandURL
))
66 , m_aListenerContainer( m_aMutex
)
70 OSL_ASSERT( m_xContext
.is() );
71 registerProperty( TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE
,
72 TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
,
73 css::beans::PropertyAttribute::TRANSIENT
| css::beans::PropertyAttribute::READONLY
,
74 &m_bSupportVisible
, cppu::UnoType
<decltype(m_bSupportVisible
)>::get());
78 m_xUrlTransformer
= URLTransformer::create( rxContext
);
80 catch(const Exception
&)
85 ToolboxController::ToolboxController() :
86 OPropertyContainer(GetBroadcastHelper())
87 , m_bSupportVisible(false)
88 , m_bInitialized( false )
89 , m_bDisposed( false )
91 , m_nToolBoxId( SAL_MAX_UINT16
)
92 , m_aListenerContainer( m_aMutex
)
96 registerProperty( TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE
,
97 TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
,
98 css::beans::PropertyAttribute::TRANSIENT
| css::beans::PropertyAttribute::READONLY
,
99 &m_bSupportVisible
, cppu::UnoType
<decltype(m_bSupportVisible
)>::get());
102 ToolboxController::~ToolboxController()
106 Reference
< XFrame
> ToolboxController::getFrameInterface() const
108 SolarMutexGuard aSolarMutexGuard
;
112 const Reference
< XComponentContext
> & ToolboxController::getContext() const
114 SolarMutexGuard aSolarMutexGuard
;
118 Reference
< XLayoutManager
> ToolboxController::getLayoutManager() const
120 Reference
< XLayoutManager
> xLayoutManager
;
121 Reference
< XPropertySet
> xPropSet
;
123 SolarMutexGuard aSolarMutexGuard
;
124 xPropSet
.set( m_xFrame
, UNO_QUERY
);
131 xLayoutManager
.set(xPropSet
->getPropertyValue(u
"LayoutManager"_ustr
),UNO_QUERY
);
138 return xLayoutManager
;
142 Any SAL_CALL
ToolboxController::queryInterface( const Type
& rType
)
144 css::uno::Any
a(ToolboxController_Base::queryInterface(rType
));
145 return a
.hasValue() ? a
: OPropertyContainer::queryInterface(rType
);
148 void SAL_CALL
ToolboxController::acquire() noexcept
150 ToolboxController_Base::acquire();
153 void SAL_CALL
ToolboxController::release() noexcept
155 ToolboxController_Base::release();
158 css::uno::Sequence
<css::uno::Type
> ToolboxController::getTypes()
160 return comphelper::concatSequences(ToolboxController_Base::getTypes(),
164 void SAL_CALL
ToolboxController::initialize( const Sequence
< Any
>& aArguments
)
166 SolarMutexGuard aSolarMutexGuard
;
169 throw DisposedException();
171 if ( m_bInitialized
)
174 m_bInitialized
= true;
175 m_bSupportVisible
= false;
176 PropertyValue aPropValue
;
177 for ( const auto& rArgument
: aArguments
)
179 if ( rArgument
>>= aPropValue
)
181 if ( aPropValue
.Name
== "Frame" )
182 m_xFrame
.set(aPropValue
.Value
,UNO_QUERY
);
183 else if ( aPropValue
.Name
== "CommandURL" )
184 aPropValue
.Value
>>= m_aCommandURL
;
185 else if ( aPropValue
.Name
== "ServiceManager" )
187 Reference
<XMultiServiceFactory
> xMSF(aPropValue
.Value
, UNO_QUERY
);
189 m_xContext
= comphelper::getComponentContext(xMSF
);
191 else if ( aPropValue
.Name
== "ParentWindow" )
192 m_xParentWindow
.set(aPropValue
.Value
,UNO_QUERY
);
193 else if ( aPropValue
.Name
== "ModuleIdentifier" )
194 aPropValue
.Value
>>= m_sModuleName
;
195 else if ( aPropValue
.Name
== "Identifier" )
198 if (aPropValue
.Value
>>= nTmp
)
199 m_nToolBoxId
= ToolBoxItemId(nTmp
);
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(this);
244 SolarMutexGuard aSolarMutexGuard
;
249 css::lang::EventObject
aEvent( xThis
);
250 m_aListenerContainer
.disposeAndClear( aEvent
);
252 SolarMutexGuard aSolarMutexGuard
;
253 Reference
< XStatusListener
> xStatusListener(this);
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
;
346 // Provide key modifier information to dispatch function
347 Sequence
<PropertyValue
> aArgs
{ comphelper::makePropertyValue(u
"KeyModifier"_ustr
, KeyModifier
) };
349 aTargetURL
.Complete
= aCommandURL
;
350 if ( m_xUrlTransformer
.is() )
351 m_xUrlTransformer
->parseStrict( aTargetURL
);
352 xDispatch
->dispatch( aTargetURL
, aArgs
);
354 catch ( DisposedException
& )
359 void SAL_CALL
ToolboxController::click()
363 void SAL_CALL
ToolboxController::doubleClick()
367 Reference
< XWindow
> SAL_CALL
ToolboxController::createPopupWindow()
369 return Reference
< XWindow
>();
372 Reference
< XWindow
> SAL_CALL
ToolboxController::createItemWindow( const Reference
< XWindow
>& )
374 return Reference
< XWindow
>();
377 void ToolboxController::addStatusListener( const OUString
& aCommandURL
)
379 Reference
< XDispatch
> xDispatch
;
380 Reference
< XStatusListener
> xStatusListener
;
381 css::util::URL aTargetURL
;
384 SolarMutexGuard aSolarMutexGuard
;
385 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( aCommandURL
);
387 // Already in the list of status listener. Do nothing.
388 if ( pIter
!= m_aListenerMap
.end() )
391 // Check if we are already initialized. Implementation starts adding itself as status listener when
392 // initialize is called.
393 if ( !m_bInitialized
)
395 // Put into the unordered_map of status listener. Will be activated when initialized is called
396 m_aListenerMap
.emplace( aCommandURL
, Reference
< XDispatch
>() );
401 // Add status listener directly as initialize has already been called.
402 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
403 if ( m_xContext
.is() && xDispatchProvider
.is() )
405 aTargetURL
.Complete
= aCommandURL
;
406 if ( m_xUrlTransformer
.is() )
407 m_xUrlTransformer
->parseStrict( aTargetURL
);
408 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
410 xStatusListener
= this;
411 URLToDispatchMap::iterator aIter
= m_aListenerMap
.find( aCommandURL
);
412 if ( aIter
!= m_aListenerMap
.end() )
414 Reference
< XDispatch
> xOldDispatch( aIter
->second
);
415 aIter
->second
= xDispatch
;
419 if ( xOldDispatch
.is() )
420 xOldDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
427 m_aListenerMap
.emplace( aCommandURL
, xDispatch
);
432 // Call without locked mutex as we are called back from dispatch implementation
435 if ( xDispatch
.is() )
436 xDispatch
->addStatusListener( xStatusListener
, aTargetURL
);
443 void ToolboxController::removeStatusListener( const OUString
& aCommandURL
)
445 SolarMutexGuard aSolarMutexGuard
;
447 URLToDispatchMap::iterator pIter
= m_aListenerMap
.find( aCommandURL
);
448 if ( pIter
== m_aListenerMap
.end() )
451 Reference
< XDispatch
> xDispatch( pIter
->second
);
452 Reference
< XStatusListener
> xStatusListener(this);
453 m_aListenerMap
.erase( pIter
);
457 css::util::URL aTargetURL
;
458 aTargetURL
.Complete
= aCommandURL
;
459 if ( m_xUrlTransformer
.is() )
460 m_xUrlTransformer
->parseStrict( aTargetURL
);
462 if ( xDispatch
.is() && xStatusListener
.is() )
463 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
470 void ToolboxController::bindListener()
472 std::vector
< Listener
> aDispatchVector
;
473 Reference
< XStatusListener
> xStatusListener
;
476 SolarMutexGuard aSolarMutexGuard
;
478 if ( !m_bInitialized
)
481 // Collect all registered command URL's and store them temporary
482 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
483 if ( m_xContext
.is() && xDispatchProvider
.is() )
485 xStatusListener
= this;
486 for (auto & listener
: m_aListenerMap
)
488 css::util::URL aTargetURL
;
489 aTargetURL
.Complete
= listener
.first
;
490 if ( m_xUrlTransformer
.is() )
491 m_xUrlTransformer
->parseStrict( aTargetURL
);
493 Reference
< XDispatch
> xDispatch(listener
.second
);
494 if ( xDispatch
.is() )
496 // We already have a dispatch object => we have to requery.
497 // Release old dispatch object and remove it as listener
500 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
507 listener
.second
.clear();
510 // Query for dispatch object. Old dispatch will be released with this, too.
513 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
519 // it may be a command alias
524 auto aProperties
= vcl::CommandInfoProvider::GetCommandProperties(listener
.first
,
525 vcl::CommandInfoProvider::GetModuleIdentifier(m_xFrame
));
526 OUString sRealCommand
= vcl::CommandInfoProvider::GetRealCommandForCommand(aProperties
);
528 if (!sRealCommand
.isEmpty())
530 aTargetURL
.Complete
= sRealCommand
;
531 if ( m_xUrlTransformer
.is() )
532 m_xUrlTransformer
->parseStrict( aTargetURL
);
534 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
542 listener
.second
= xDispatch
;
544 aDispatchVector
.emplace_back(aTargetURL
, xDispatch
);
549 // Call without locked mutex as we are called back from dispatch implementation
550 if ( !xStatusListener
.is() )
555 for (Listener
& rListener
: aDispatchVector
)
557 if ( rListener
.xDispatch
.is() )
558 rListener
.xDispatch
->addStatusListener( xStatusListener
, rListener
.aURL
);
559 else if ( rListener
.aURL
.Complete
== m_aCommandURL
)
563 // Send status changed for the main URL, if we cannot get a valid dispatch object.
564 // UI disables the button. Catch exception as we release our mutex, it is possible
565 // that someone else already disposed this instance!
566 FeatureStateEvent aFeatureStateEvent
;
567 aFeatureStateEvent
.IsEnabled
= false;
568 aFeatureStateEvent
.FeatureURL
= rListener
.aURL
;
569 aFeatureStateEvent
.State
= Any();
570 xStatusListener
->statusChanged( aFeatureStateEvent
);
583 void ToolboxController::unbindListener()
585 SolarMutexGuard aSolarMutexGuard
;
587 if ( !m_bInitialized
)
590 // Collect all registered command URL's and store them temporary
591 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
592 if ( !(m_xContext
.is() && xDispatchProvider
.is()) )
595 Reference
< XStatusListener
> xStatusListener(this);
596 for (auto & listener
: m_aListenerMap
)
598 css::util::URL aTargetURL
;
599 aTargetURL
.Complete
= listener
.first
;
600 if ( m_xUrlTransformer
.is() )
601 m_xUrlTransformer
->parseStrict( aTargetURL
);
603 Reference
< XDispatch
> xDispatch(listener
.second
);
604 if ( xDispatch
.is() )
606 // We already have a dispatch object => we have to requery.
607 // Release old dispatch object and remove it as listener
610 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
616 listener
.second
.clear();
620 void ToolboxController::updateStatus()
625 void ToolboxController::updateStatus( const OUString
& aCommandURL
)
627 Reference
< XDispatch
> xDispatch
;
628 Reference
< XStatusListener
> xStatusListener
;
629 css::util::URL aTargetURL
;
632 SolarMutexGuard aSolarMutexGuard
;
634 if ( !m_bInitialized
)
637 // Try to find a dispatch object for the requested command URL
638 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY
);
639 xStatusListener
= this;
640 if ( m_xContext
.is() && xDispatchProvider
.is() )
642 aTargetURL
.Complete
= aCommandURL
;
643 if ( m_xUrlTransformer
.is() )
644 m_xUrlTransformer
->parseStrict( aTargetURL
);
645 xDispatch
= xDispatchProvider
->queryDispatch( aTargetURL
, OUString(), 0 );
649 if ( !(xDispatch
.is() && xStatusListener
.is()) )
652 // Catch exception as we release our mutex, it is possible that someone else
653 // has already disposed this instance!
654 // Add/remove status listener to get an update status information from the
655 // requested command.
658 xDispatch
->addStatusListener( xStatusListener
, aTargetURL
);
659 xDispatch
->removeStatusListener( xStatusListener
, aTargetURL
);
667 void ToolboxController::dispatchCommand( const OUString
& sCommandURL
, const Sequence
< PropertyValue
>& rArgs
, const OUString
&sTarget
)
671 Reference
< XDispatchProvider
> xDispatchProvider( m_xFrame
, UNO_QUERY_THROW
);
673 aURL
.Complete
= sCommandURL
;
674 getURLTransformer()->parseStrict( aURL
);
676 Reference
< XDispatch
> xDispatch( xDispatchProvider
->queryDispatch( aURL
, sTarget
, 0 ), UNO_SET_THROW
);
678 std::unique_ptr
<DispatchInfo
> pDispatchInfo(new DispatchInfo( xDispatch
, std::move(aURL
), rArgs
));
679 if ( Application::PostUserEvent( LINK(nullptr, ToolboxController
, ExecuteHdl_Impl
),
680 pDispatchInfo
.get() ) )
681 pDispatchInfo
.release();
690 css::uno::Reference
< css::beans::XPropertySetInfo
> SAL_CALL
ToolboxController::getPropertySetInfo()
692 Reference
<XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
696 ::cppu::IPropertyArrayHelper
& ToolboxController::getInfoHelper()
698 return *getArrayHelper();
702 ::cppu::IPropertyArrayHelper
* ToolboxController::createArrayHelper( ) const
704 css::uno::Sequence
< Property
> aProps
;
705 describeProperties(aProps
);
706 return new ::cppu::OPropertyArrayHelper(aProps
);
709 sal_Bool SAL_CALL
ToolboxController::convertFastPropertyValue( css::uno::Any
& aConvertedValue
,
710 css::uno::Any
& aOldValue
,
712 const css::uno::Any
& aValue
)
716 case TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
:
718 bool aNewValue(false);
719 aValue
>>= aNewValue
;
720 if (aNewValue
!= m_bSupportVisible
)
722 aConvertedValue
<<= aNewValue
;
723 aOldValue
<<= m_bSupportVisible
;
729 return OPropertyContainer::convertFastPropertyValue(aConvertedValue
, aOldValue
, nHandle
, aValue
);
732 void SAL_CALL
ToolboxController::setFastPropertyValue_NoBroadcast(
734 const css::uno::Any
& aValue
)
736 OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle
, aValue
);
737 if (TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE
== nHandle
)
740 if (( aValue
>>= rValue
) && m_bInitialized
)
741 m_bSupportVisible
= rValue
;
746 IMPL_STATIC_LINK( ToolboxController
, ExecuteHdl_Impl
, void*, p
, void )
748 DispatchInfo
* pDispatchInfo
= static_cast<DispatchInfo
*>(p
);
749 pDispatchInfo
->mxDispatch
->dispatch( pDispatchInfo
->maURL
, pDispatchInfo
->maArgs
);
750 delete pDispatchInfo
;
753 void ToolboxController::enable( bool bEnable
)
755 ToolBox
* pToolBox
= nullptr;
756 ToolBoxItemId nItemId
;
757 if( getToolboxId( nItemId
, &pToolBox
) )
759 pToolBox
->EnableItem( nItemId
, bEnable
);
763 bool ToolboxController::getToolboxId( ToolBoxItemId
& rItemId
, ToolBox
** ppToolBox
)
765 if( (m_nToolBoxId
!= ToolBoxItemId(SAL_MAX_UINT16
)) && (ppToolBox
== nullptr) )
768 ToolBox
* pToolBox
= static_cast< ToolBox
* >( VCLUnoHelper::GetWindow( getParent() ) );
770 if( (m_nToolBoxId
== ToolBoxItemId(SAL_MAX_UINT16
)) && pToolBox
)
772 const ToolBox::ImplToolItems::size_type nCount
= pToolBox
->GetItemCount();
773 for ( ToolBox::ImplToolItems::size_type nPos
= 0; nPos
< nCount
; ++nPos
)
775 const ToolBoxItemId nItemId
= pToolBox
->GetItemId( nPos
);
776 if ( pToolBox
->GetItemCommand( nItemId
) == m_aCommandURL
)
778 m_nToolBoxId
= nItemId
;
785 *ppToolBox
= pToolBox
;
787 rItemId
= m_nToolBoxId
;
789 return (rItemId
!= ToolBoxItemId(SAL_MAX_UINT16
)) && (( ppToolBox
== nullptr) || (*ppToolBox
!= nullptr) );
795 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */