Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / framework / source / uielement / popuptoolbarcontroller.cxx
blobb17e8a6bfc609421a79a4f5f4333932fc8c41aee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <bitmaps.hlst>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <comphelper/propertyvalue.hxx>
25 #include <menuconfiguration.hxx>
26 #include <svtools/imagemgr.hxx>
27 #include <svtools/toolboxcontroller.hxx>
28 #include <toolkit/awt/vclxmenu.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
30 #include <comphelper/diagnose_ex.hxx>
31 #include <tools/urlobj.hxx>
32 #include <utility>
33 #include <vcl/commandinfoprovider.hxx>
34 #include <vcl/menu.hxx>
35 #include <vcl/svapp.hxx>
36 #include <vcl/toolbox.hxx>
38 #include <com/sun/star/awt/PopupMenuDirection.hpp>
39 #include <com/sun/star/awt/XPopupMenu.hpp>
40 #include <com/sun/star/frame/thePopupMenuControllerFactory.hpp>
41 #include <com/sun/star/frame/XPopupMenuController.hpp>
42 #include <com/sun/star/frame/XStorable.hpp>
43 #include <com/sun/star/frame/XSubToolbarController.hpp>
44 #include <com/sun/star/frame/XUIControllerFactory.hpp>
45 #include <com/sun/star/frame/XController.hpp>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 #include <com/sun/star/ucb/CommandFailedException.hpp>
48 #include <com/sun/star/ucb/ContentCreationException.hpp>
49 #include <com/sun/star/util/XModifiable.hpp>
51 using namespace framework;
53 namespace
56 typedef cppu::ImplInheritanceHelper< svt::ToolboxController,
57 css::lang::XServiceInfo >
58 ToolBarBase;
60 class PopupMenuToolbarController : public ToolBarBase
62 public:
63 // XComponent
64 virtual void SAL_CALL dispose() override;
65 // XInitialization
66 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
67 // XToolbarController
68 virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createPopupWindow() override;
69 // XStatusListener
70 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
72 protected:
73 PopupMenuToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
74 OUString aPopupCommand = OUString() );
75 virtual void functionExecuted( const OUString &rCommand );
76 virtual ToolBoxItemBits getDropDownStyle() const;
77 void createPopupMenuController();
79 bool m_bHasController;
80 bool m_bResourceURL;
81 OUString m_aPopupCommand;
82 rtl::Reference< VCLXPopupMenu > m_xPopupMenu;
84 private:
85 css::uno::Reference< css::frame::XUIControllerFactory > m_xPopupMenuFactory;
86 css::uno::Reference< css::frame::XPopupMenuController > m_xPopupMenuController;
89 PopupMenuToolbarController::PopupMenuToolbarController(
90 const css::uno::Reference< css::uno::XComponentContext >& xContext,
91 OUString aPopupCommand )
92 : ToolBarBase( xContext, css::uno::Reference< css::frame::XFrame >(), /*aCommandURL*/OUString() )
93 , m_bHasController( false )
94 , m_bResourceURL( false )
95 , m_aPopupCommand(std::move( aPopupCommand ))
99 void SAL_CALL PopupMenuToolbarController::dispose()
101 svt::ToolboxController::dispose();
103 osl::MutexGuard aGuard( m_aMutex );
104 if( m_xPopupMenuController.is() )
106 css::uno::Reference< css::lang::XComponent > xComponent(
107 m_xPopupMenuController, css::uno::UNO_QUERY );
108 if( xComponent.is() )
112 xComponent->dispose();
114 catch (...)
117 m_xPopupMenuController.clear();
120 m_xContext.clear();
121 m_xPopupMenuFactory.clear();
122 m_xPopupMenu.clear();
125 void SAL_CALL PopupMenuToolbarController::initialize(
126 const css::uno::Sequence< css::uno::Any >& aArguments )
128 ToolboxController::initialize( aArguments );
130 osl::MutexGuard aGuard( m_aMutex );
131 if ( !m_aPopupCommand.getLength() )
132 m_aPopupCommand = m_aCommandURL;
136 m_xPopupMenuFactory.set(
137 css::frame::thePopupMenuControllerFactory::get( m_xContext ) );
138 m_bHasController = m_xPopupMenuFactory->hasController(
139 m_aPopupCommand, getModuleName() );
141 catch (const css::uno::Exception&)
143 TOOLS_INFO_EXCEPTION( "fwk.uielement", "" );
146 if ( !m_bHasController && m_aPopupCommand.startsWith( "private:resource/" ) )
148 m_bResourceURL = true;
149 m_bHasController = true;
152 SolarMutexGuard aSolarLock;
153 ToolBox* pToolBox = nullptr;
154 ToolBoxItemId nItemId;
155 if ( getToolboxId( nItemId, &pToolBox ) )
157 ToolBoxItemBits nCurStyle( pToolBox->GetItemBits( nItemId ) );
158 ToolBoxItemBits nSetStyle( getDropDownStyle() );
159 pToolBox->SetItemBits( nItemId,
160 m_bHasController ?
161 nCurStyle | nSetStyle :
162 nCurStyle & ~nSetStyle );
167 void SAL_CALL PopupMenuToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
169 if ( m_bResourceURL )
170 return;
172 ToolBox* pToolBox = nullptr;
173 ToolBoxItemId nItemId;
174 if ( getToolboxId( nItemId, &pToolBox ) )
176 SolarMutexGuard aSolarLock;
177 pToolBox->EnableItem( nItemId, rEvent.IsEnabled );
178 bool bValue;
179 if ( rEvent.State >>= bValue )
180 pToolBox->CheckItem( nItemId, bValue );
184 css::uno::Reference< css::awt::XWindow > SAL_CALL
185 PopupMenuToolbarController::createPopupWindow()
187 css::uno::Reference< css::awt::XWindow > xRet;
189 osl::MutexGuard aGuard( m_aMutex );
190 if ( !m_bHasController )
191 return xRet;
193 createPopupMenuController();
195 SolarMutexGuard aSolarLock;
196 VclPtr< ToolBox > pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
197 if ( !pToolBox )
198 return xRet;
200 pToolBox->SetItemDown( m_nToolBoxId, true );
201 WindowAlign eAlign( pToolBox->GetAlign() );
203 // If the parent ToolBox is in popup mode (e.g. sub toolbar, overflow popup),
204 // its ToolBarManager can be disposed along with our controller, destroying
205 // m_xPopupMenu, while the latter still in execute. This should be fixed at a
206 // different level, for now just hold it here so it won't crash.
207 css::uno::Reference< css::awt::XPopupMenu > xPopupMenu ( m_xPopupMenu );
208 sal_uInt16 nId = xPopupMenu->execute(
209 css::uno::Reference< css::awt::XWindowPeer >( getParent(), css::uno::UNO_QUERY ),
210 VCLUnoHelper::ConvertToAWTRect( pToolBox->GetItemRect( m_nToolBoxId ) ),
211 ( eAlign == WindowAlign::Top || eAlign == WindowAlign::Bottom ) ?
212 css::awt::PopupMenuDirection::EXECUTE_DOWN :
213 css::awt::PopupMenuDirection::EXECUTE_RIGHT );
214 pToolBox->SetItemDown( m_nToolBoxId, false );
216 if ( nId )
217 functionExecuted( xPopupMenu->getCommand( nId ) );
219 return xRet;
222 void PopupMenuToolbarController::functionExecuted( const OUString &/*rCommand*/)
226 ToolBoxItemBits PopupMenuToolbarController::getDropDownStyle() const
228 return ToolBoxItemBits::DROPDOWN;
231 void PopupMenuToolbarController::createPopupMenuController()
233 if( !m_bHasController )
234 return;
236 if ( m_xPopupMenuController.is() )
238 m_xPopupMenuController->updatePopupMenu();
240 else
242 css::uno::Sequence<css::uno::Any> aArgs {
243 css::uno::Any(comphelper::makePropertyValue("Frame", m_xFrame)),
244 css::uno::Any(comphelper::makePropertyValue("ModuleIdentifier", m_sModuleName)),
245 css::uno::Any(comphelper::makePropertyValue("InToolbar", true))
250 m_xPopupMenu = new VCLXPopupMenu();
252 if (m_bResourceURL)
254 sal_Int32 nAppendIndex = aArgs.getLength();
255 aArgs.realloc(nAppendIndex + 1);
256 aArgs.getArray()[nAppendIndex] <<= comphelper::makePropertyValue("ResourceURL", m_aPopupCommand);
258 m_xPopupMenuController.set( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
259 "com.sun.star.comp.framework.ResourceMenuController", aArgs, m_xContext), css::uno::UNO_QUERY_THROW );
261 else
263 m_xPopupMenuController.set( m_xPopupMenuFactory->createInstanceWithArgumentsAndContext(
264 m_aPopupCommand, aArgs, m_xContext), css::uno::UNO_QUERY_THROW );
267 m_xPopupMenuController->setPopupMenu( m_xPopupMenu );
269 catch ( const css::uno::Exception & )
271 TOOLS_INFO_EXCEPTION( "fwk.uielement", "" );
272 m_xPopupMenu.clear();
277 class GenericPopupToolbarController : public PopupMenuToolbarController
279 public:
280 GenericPopupToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
281 const css::uno::Sequence< css::uno::Any >& rxArgs );
283 // XInitialization
284 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& rxArgs ) override;
286 // XStatusListener
287 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
289 // XServiceInfo
290 virtual OUString SAL_CALL getImplementationName() override;
292 virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override;
294 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
296 private:
297 bool m_bSplitButton, m_bReplaceWithLast;
298 void functionExecuted(const OUString &rCommand) override;
299 ToolBoxItemBits getDropDownStyle() const override;
302 GenericPopupToolbarController::GenericPopupToolbarController(
303 const css::uno::Reference< css::uno::XComponentContext >& xContext,
304 const css::uno::Sequence< css::uno::Any >& rxArgs )
305 : PopupMenuToolbarController( xContext )
306 , m_bReplaceWithLast( false )
308 css::beans::PropertyValue aPropValue;
309 for ( const auto& arg: rxArgs )
311 if ( ( arg >>= aPropValue ) && aPropValue.Name == "Value" )
313 sal_Int32 nIdx{ 0 };
314 OUString aValue;
315 aPropValue.Value >>= aValue;
316 m_aPopupCommand = aValue.getToken(0, ';', nIdx);
317 m_bReplaceWithLast = aValue.getToken(0, ';', nIdx).toBoolean();
318 break;
321 m_bSplitButton = m_bReplaceWithLast || !m_aPopupCommand.isEmpty();
324 OUString GenericPopupToolbarController::getImplementationName()
326 return "com.sun.star.comp.framework.GenericPopupToolbarController";
329 sal_Bool GenericPopupToolbarController::supportsService(OUString const & rServiceName)
331 return cppu::supportsService( this, rServiceName );
334 css::uno::Sequence<OUString> GenericPopupToolbarController::getSupportedServiceNames()
336 return {"com.sun.star.frame.ToolbarController"};
339 void GenericPopupToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& rxArgs )
341 PopupMenuToolbarController::initialize( rxArgs );
342 if ( m_bReplaceWithLast )
343 // Create early, so we can use the menu is statusChanged method.
344 createPopupMenuController();
347 void GenericPopupToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
349 SolarMutexGuard aGuard;
351 if ( m_bReplaceWithLast && !rEvent.IsEnabled && m_xPopupMenu.is() )
353 ToolBox* pToolBox = nullptr;
354 ToolBoxItemId nId;
355 if ( getToolboxId( nId, &pToolBox ) && pToolBox->IsItemEnabled( nId ) )
357 Menu* pVclMenu = m_xPopupMenu->GetMenu();
358 pVclMenu->Activate();
359 pVclMenu->Deactivate();
362 for (sal_uInt16 i = 0, nCount = m_xPopupMenu->getItemCount(); i < nCount; ++i )
364 sal_uInt16 nItemId = m_xPopupMenu->getItemId(i);
365 if (nItemId && m_xPopupMenu->isItemEnabled(nItemId) && !m_xPopupMenu->getPopupMenu(nItemId).is())
367 functionExecuted(m_xPopupMenu->getCommand(nItemId));
368 return;
373 PopupMenuToolbarController::statusChanged( rEvent );
376 void GenericPopupToolbarController::functionExecuted( const OUString& rCommand )
378 if ( !m_bReplaceWithLast )
379 return;
381 removeStatusListener( m_aCommandURL );
383 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(rCommand, m_sModuleName);
384 OUString aRealCommand( vcl::CommandInfoProvider::GetRealCommandForCommand(aProperties) );
385 m_aCommandURL = aRealCommand.isEmpty() ? rCommand : aRealCommand;
386 addStatusListener( m_aCommandURL );
388 ToolBox* pToolBox = nullptr;
389 ToolBoxItemId nId;
390 if ( getToolboxId( nId, &pToolBox ) )
392 pToolBox->SetItemCommand( nId, rCommand );
393 pToolBox->SetHelpText( nId, OUString() ); // Will retrieve the new one from help.
394 pToolBox->SetItemText(nId, vcl::CommandInfoProvider::GetLabelForCommand(aProperties));
395 pToolBox->SetQuickHelpText(nId, vcl::CommandInfoProvider::GetTooltipForCommand(rCommand, aProperties, m_xFrame));
397 Image aImage = vcl::CommandInfoProvider::GetImageForCommand(rCommand, m_xFrame, pToolBox->GetImageSize());
398 if ( !!aImage )
399 pToolBox->SetItemImage( nId, aImage );
403 ToolBoxItemBits GenericPopupToolbarController::getDropDownStyle() const
405 return m_bSplitButton ? ToolBoxItemBits::DROPDOWN : ToolBoxItemBits::DROPDOWNONLY;
408 class SaveToolbarController : public cppu::ImplInheritanceHelper< PopupMenuToolbarController,
409 css::frame::XSubToolbarController,
410 css::util::XModifyListener >
412 public:
413 explicit SaveToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
415 // XInitialization
416 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
418 // XSubToolbarController
419 // Make ToolBarManager ask our controller for updated image, in case of icon theme change.
420 virtual sal_Bool SAL_CALL opensSubToolbar() override;
421 virtual OUString SAL_CALL getSubToolbarName() override;
422 virtual void SAL_CALL functionSelected( const OUString& aCommand ) override;
423 virtual void SAL_CALL updateImage() override;
425 // XStatusListener
426 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
428 // XModifyListener
429 virtual void SAL_CALL modified( const css::lang::EventObject& rEvent ) override;
431 // XEventListener
432 virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) override;
434 // XComponent
435 virtual void SAL_CALL dispose() override;
437 // XServiceInfo
438 virtual OUString SAL_CALL getImplementationName() override;
439 virtual sal_Bool SAL_CALL supportsService( OUString const & rServiceName ) override;
440 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
442 private:
443 bool m_bReadOnly;
444 bool m_bModified;
445 css::uno::Reference< css::frame::XStorable > m_xStorable;
446 css::uno::Reference< css::util::XModifiable > m_xModifiable;
449 SaveToolbarController::SaveToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
450 : ImplInheritanceHelper( rxContext, ".uno:SaveAsMenu" )
451 , m_bReadOnly( false )
452 , m_bModified( false )
456 void SaveToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
458 PopupMenuToolbarController::initialize( aArguments );
460 ToolBox* pToolBox = nullptr;
461 ToolBoxItemId nId;
462 if ( !getToolboxId( nId, &pToolBox ) )
463 return;
465 css::uno::Reference< css::frame::XController > xController = m_xFrame->getController();
466 if ( xController.is() )
467 m_xModifiable.set( xController->getModel(), css::uno::UNO_QUERY );
469 if ( m_xModifiable.is() && pToolBox->GetItemCommand( nId ) == m_aCommandURL )
470 // Will also enable the save as only mode.
471 m_xStorable.set( m_xModifiable, css::uno::UNO_QUERY );
472 else if ( !m_xModifiable.is() )
473 // Can be in table/query design.
474 m_xModifiable.set( xController, css::uno::UNO_QUERY );
475 else
476 // Simple save button, without the dropdown.
477 pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) & ~ ToolBoxItemBits::DROPDOWN );
479 if ( m_xModifiable.is() )
481 m_xModifiable->addModifyListener( this );
482 modified( css::lang::EventObject() );
486 sal_Bool SaveToolbarController::opensSubToolbar()
488 return true;
491 OUString SaveToolbarController::getSubToolbarName()
493 return OUString();
496 void SaveToolbarController::functionSelected( const OUString& /*aCommand*/ )
500 void SaveToolbarController::updateImage()
502 SolarMutexGuard aGuard;
503 ToolBox* pToolBox = nullptr;
504 ToolBoxItemId nId;
505 if ( !getToolboxId( nId, &pToolBox ) )
506 return;
508 vcl::ImageType eImageType = pToolBox->GetImageSize();
510 Image aImage;
512 if ( m_bReadOnly )
514 aImage = vcl::CommandInfoProvider::GetImageForCommand(".uno:SaveAs", m_xFrame, eImageType);
516 else if ( m_bModified )
518 if (eImageType == vcl::ImageType::Size26)
519 aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_LARGE);
520 else if (eImageType == vcl::ImageType::Size32)
521 aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_EXTRALARGE);
522 else
523 aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_SMALL);
526 if ( !aImage )
527 aImage = vcl::CommandInfoProvider::GetImageForCommand(m_aCommandURL, m_xFrame, eImageType);
529 if ( !!aImage )
530 pToolBox->SetItemImage( nId, aImage );
533 void SaveToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
535 ToolBox* pToolBox = nullptr;
536 ToolBoxItemId nId;
537 if ( !getToolboxId( nId, &pToolBox ) )
538 return;
540 bool bLastReadOnly = m_bReadOnly;
541 m_bReadOnly = m_xStorable.is() && m_xStorable->isReadonly();
542 if ( bLastReadOnly != m_bReadOnly )
544 OUString sCommand = m_bReadOnly ? OUString( ".uno:SaveAs" ) : m_aCommandURL;
545 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(sCommand,
546 vcl::CommandInfoProvider::GetModuleIdentifier(m_xFrame));
547 pToolBox->SetQuickHelpText( nId,
548 vcl::CommandInfoProvider::GetTooltipForCommand(sCommand, aProperties, m_xFrame) );
549 pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) & ~( m_bReadOnly ? ToolBoxItemBits::DROPDOWN : ToolBoxItemBits::DROPDOWNONLY ) );
550 pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) | ( m_bReadOnly ? ToolBoxItemBits::DROPDOWNONLY : ToolBoxItemBits::DROPDOWN ) );
551 updateImage();
554 if ( !m_bReadOnly )
555 pToolBox->EnableItem( nId, rEvent.IsEnabled );
558 void SaveToolbarController::modified( const css::lang::EventObject& /*rEvent*/ )
560 bool bLastModified = m_bModified;
561 m_bModified = m_xModifiable->isModified();
562 if ( bLastModified != m_bModified )
563 updateImage();
566 void SaveToolbarController::disposing( const css::lang::EventObject& rEvent )
568 if ( rEvent.Source == m_xModifiable )
570 m_xModifiable.clear();
571 m_xStorable.clear();
573 else
574 PopupMenuToolbarController::disposing( rEvent );
577 void SaveToolbarController::dispose()
579 PopupMenuToolbarController::dispose();
580 if ( m_xModifiable.is() )
582 m_xModifiable->removeModifyListener( this );
583 m_xModifiable.clear();
585 m_xStorable.clear();
588 OUString SaveToolbarController::getImplementationName()
590 return "com.sun.star.comp.framework.SaveToolbarController";
593 sal_Bool SaveToolbarController::supportsService( OUString const & rServiceName )
595 return cppu::supportsService( this, rServiceName );
598 css::uno::Sequence< OUString > SaveToolbarController::getSupportedServiceNames()
600 return {"com.sun.star.frame.ToolbarController"};
603 class NewToolbarController : public cppu::ImplInheritanceHelper<PopupMenuToolbarController, css::frame::XSubToolbarController>
605 public:
606 explicit NewToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
608 // XServiceInfo
609 OUString SAL_CALL getImplementationName() override;
611 virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override;
613 css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
615 void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
617 // XSubToolbarController
618 // Make ToolBarManager ask our controller for updated image, in case of icon theme change.
619 sal_Bool SAL_CALL opensSubToolbar() override { return true; }
620 OUString SAL_CALL getSubToolbarName() override { return OUString(); }
621 void SAL_CALL functionSelected( const OUString& ) override {}
622 void SAL_CALL updateImage() override;
624 private:
625 void functionExecuted( const OUString &rCommand ) override;
626 void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
627 void SAL_CALL execute( sal_Int16 KeyModifier ) override;
628 sal_uInt16 getMenuIdForCommand( std::u16string_view rCommand );
630 sal_uInt16 m_nMenuId;
633 NewToolbarController::NewToolbarController(
634 const css::uno::Reference< css::uno::XComponentContext >& xContext )
635 : ImplInheritanceHelper( xContext )
636 , m_nMenuId( 0 )
640 OUString NewToolbarController::getImplementationName()
642 return "org.apache.openoffice.comp.framework.NewToolbarController";
645 sal_Bool NewToolbarController::supportsService(OUString const & rServiceName)
647 return cppu::supportsService( this, rServiceName );
650 css::uno::Sequence<OUString> NewToolbarController::getSupportedServiceNames()
652 return {"com.sun.star.frame.ToolbarController"};
655 void SAL_CALL NewToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
657 PopupMenuToolbarController::initialize( aArguments );
659 osl::MutexGuard aGuard( m_aMutex );
660 createPopupMenuController();
663 void SAL_CALL NewToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
665 if ( rEvent.IsEnabled )
667 OUString aState;
668 rEvent.State >>= aState;
671 // set the image even if the state is not a string
672 // the toolbar item command will be used as a fallback
673 functionExecuted( aState );
675 catch (const css::ucb::CommandFailedException&)
678 catch (const css::ucb::ContentCreationException&)
683 enable( rEvent.IsEnabled );
686 void SAL_CALL NewToolbarController::execute( sal_Int16 /*KeyModifier*/ )
688 osl::MutexGuard aGuard( m_aMutex );
690 OUString aURL, aTarget;
691 if ( m_xPopupMenu.is() && m_nMenuId )
693 SolarMutexGuard aSolarMutexGuard;
694 aURL = m_xPopupMenu->getCommand(m_nMenuId);
696 // TODO investigate how to wrap Get/SetUserValue in css::awt::XMenu
697 MenuAttributes* pMenuAttributes(static_cast<MenuAttributes*>(m_xPopupMenu->getUserValue(m_nMenuId)));
698 if ( pMenuAttributes )
699 aTarget = pMenuAttributes->aTargetFrame;
700 else
701 aTarget = "_default";
703 else
704 aURL = m_aCommandURL;
706 css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
707 "Referer", OUString( "private:user" )) };
709 dispatchCommand( aURL, aArgs, aTarget );
712 void NewToolbarController::functionExecuted( const OUString &rCommand )
714 m_nMenuId = getMenuIdForCommand( rCommand );
715 updateImage();
718 sal_uInt16 NewToolbarController::getMenuIdForCommand( std::u16string_view rCommand )
720 if ( m_xPopupMenu.is() && !rCommand.empty() )
722 sal_uInt16 nCount = m_xPopupMenu->getItemCount();
723 for ( sal_uInt16 n = 0; n < nCount; ++n )
725 sal_uInt16 nId = m_xPopupMenu->getItemId(n);
726 OUString aCmd(m_xPopupMenu->getCommand(nId));
728 // match even if the menu command is more detailed
729 // (maybe an additional query) #i28667#
730 if ( aCmd.match( rCommand ) )
731 return nId;
735 return 0;
738 void SAL_CALL NewToolbarController::updateImage()
740 SolarMutexGuard aSolarLock;
741 VclPtr< ToolBox> pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
742 if ( !pToolBox )
743 return;
745 OUString aURL, aImageId;
746 if ( m_xPopupMenu.is() && m_nMenuId )
748 aURL = m_xPopupMenu->getCommand(m_nMenuId);
749 MenuAttributes* pMenuAttributes(static_cast<MenuAttributes*>(m_xPopupMenu->getUserValue(m_nMenuId)));
750 if ( pMenuAttributes )
751 aImageId = pMenuAttributes->aImageId;
753 else
754 aURL = m_aCommandURL;
756 INetURLObject aURLObj( aImageId.isEmpty() ? aURL : aImageId );
757 vcl::ImageType eImageType( pToolBox->GetImageSize() );
758 Image aImage = SvFileInformationManager::GetImageNoDefault( aURLObj, eImageType );
759 if ( !aImage )
760 aImage = vcl::CommandInfoProvider::GetImageForCommand( aURL, m_xFrame, eImageType );
762 if ( !aImage )
763 return;
765 pToolBox->SetItemImage( m_nToolBoxId, aImage );
770 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
771 com_sun_star_comp_framework_GenericPopupToolbarController_get_implementation(
772 css::uno::XComponentContext *context,
773 css::uno::Sequence<css::uno::Any> const &args)
775 return cppu::acquire(new GenericPopupToolbarController(context, args));
778 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
779 com_sun_star_comp_framework_SaveToolbarController_get_implementation(
780 css::uno::XComponentContext *context,
781 css::uno::Sequence<css::uno::Any> const &)
783 return cppu::acquire(new SaveToolbarController(context));
786 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
787 org_apache_openoffice_comp_framework_NewToolbarController_get_implementation(
788 css::uno::XComponentContext *context,
789 css::uno::Sequence<css::uno::Any> const &)
791 return cppu::acquire(new NewToolbarController(context));
794 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */