diff options
author | Caolán McNamara <caolanm@redhat.com> | 2019-12-23 11:16:28 +0000 |
---|---|---|
committer | Szymon Kłos <eszkadev@gmail.com> | 2020-06-03 12:28:38 +0200 |
commit | 151911ffced654d49b3af1135111b00db4c680a6 (patch) | |
tree | 4c5865bb760fe1194e97680a97f89538b42efb73 /sfx2 | |
parent | d455add0c642d4426bf1593f9fe5203eca8f8c7c (diff) |
reorganize ToolbarUnoDispatcher to be useful for sidebar
Change-Id: If129d4832f04758705e121bff88ea7d2e45bf96b
Reviewed-on: https://gerrit.libreoffice.org/85755
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
Diffstat (limited to 'sfx2')
-rw-r--r-- | sfx2/Library_sfx.mk | 1 | ||||
-rw-r--r-- | sfx2/source/dialog/recfloat.cxx | 3 | ||||
-rw-r--r-- | sfx2/source/inc/recfloat.hxx | 6 | ||||
-rw-r--r-- | sfx2/source/sidebar/ControllerFactory.cxx | 49 | ||||
-rw-r--r-- | sfx2/source/toolbox/weldutils.cxx | 135 |
5 files changed, 190 insertions, 4 deletions
diff --git a/sfx2/Library_sfx.mk b/sfx2/Library_sfx.mk index 8f386caaa283..cc2386fe5f82 100644 --- a/sfx2/Library_sfx.mk +++ b/sfx2/Library_sfx.mk @@ -291,6 +291,7 @@ $(eval $(call gb_Library_add_exception_objects,sfx,\ sfx2/source/statbar/stbitem \ sfx2/source/styles/StyleManager \ sfx2/source/toolbox/tbxitem \ + sfx2/source/toolbox/weldutils \ sfx2/source/view/classificationcontroller \ sfx2/source/view/classificationhelper \ sfx2/source/view/frame \ diff --git a/sfx2/source/dialog/recfloat.cxx b/sfx2/source/dialog/recfloat.cxx index b431cc619298..471d611f745c 100644 --- a/sfx2/source/dialog/recfloat.cxx +++ b/sfx2/source/dialog/recfloat.cxx @@ -99,7 +99,7 @@ SfxRecordingFloat_Impl::SfxRecordingFloat_Impl(SfxBindings* pBind, SfxChildWindo : SfxModelessDialogController(pBind, pChildWin, pParent, "sfx/ui/floatingrecord.ui", "FloatingRecord") , m_xToolbar(m_xBuilder->weld_toolbar("toolbar")) - , m_aDispatcher(*m_xToolbar, pBind->GetActiveFrame()) + , m_xDispatcher(new ToolbarUnoDispatcher(*m_xToolbar, pBind->GetActiveFrame())) { // start recording SfxBoolItem aItem( SID_RECORDMACRO, true ); @@ -109,6 +109,7 @@ SfxRecordingFloat_Impl::SfxRecordingFloat_Impl(SfxBindings* pBind, SfxChildWindo SfxRecordingFloat_Impl::~SfxRecordingFloat_Impl() { + m_xDispatcher->dispose(); } void SfxRecordingFloat_Impl::FillInfo( SfxChildWinInfo& rInfo ) const diff --git a/sfx2/source/inc/recfloat.hxx b/sfx2/source/inc/recfloat.hxx index e9d2dd7eb97e..b7aa71668491 100644 --- a/sfx2/source/inc/recfloat.hxx +++ b/sfx2/source/inc/recfloat.hxx @@ -20,9 +20,9 @@ #ifndef INCLUDED_SFX2_SOURCE_INC_RECFLOAT_HXX #define INCLUDED_SFX2_SOURCE_INC_RECFLOAT_HXX -#include <sfx2/childwin.hxx> #include <sfx2/basedlgs.hxx> -#include <vcl/weldutils.hxx> +#include <sfx2/childwin.hxx> +#include <sfx2/weldutils.hxx> class SfxRecordingFloatWrapper_Impl : public SfxChildWindow { @@ -41,7 +41,7 @@ public: class SfxRecordingFloat_Impl : public SfxModelessDialogController { std::unique_ptr<weld::Toolbar> m_xToolbar; - weld::ToolbarUnoDispatcher m_aDispatcher; + std::unique_ptr<ToolbarUnoDispatcher> m_xDispatcher; public: SfxRecordingFloat_Impl(SfxBindings* pBindings, SfxChildWindow* pChildWin, diff --git a/sfx2/source/sidebar/ControllerFactory.cxx b/sfx2/source/sidebar/ControllerFactory.cxx index 643d364bb266..edb29abd5280 100644 --- a/sfx2/source/sidebar/ControllerFactory.cxx +++ b/sfx2/source/sidebar/ControllerFactory.cxx @@ -137,6 +137,55 @@ Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController( return xController; } +Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController( + weld::Toolbar& rToolbar, + const OUString& rsCommandName, + const Reference<frame::XFrame>& rxFrame) +{ + Reference<frame::XToolbarController> xController; + + xController.set( + static_cast<XWeak*>(new svt::GenericToolboxController( + ::comphelper::getProcessComponentContext(), + rxFrame, + rToolbar, + rsCommandName)), + UNO_QUERY); + + // Initialize the controller with eg a service factory. + Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY); + if (/*!bFactoryHasController &&*/ xInitialization.is()) + { + beans::PropertyValue aPropValue; + std::vector<Any> aPropertyVector; + + aPropValue.Name = "Frame"; + aPropValue.Value <<= rxFrame; + aPropertyVector.push_back(makeAny(aPropValue)); + + aPropValue.Name = "ServiceManager"; + aPropValue.Value <<= ::comphelper::getProcessServiceFactory(); + aPropertyVector.push_back(makeAny(aPropValue)); + + aPropValue.Name = "CommandURL"; + aPropValue.Value <<= rsCommandName; + aPropertyVector.push_back(makeAny(aPropValue)); + + Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector)); + xInitialization->initialize(aArgs); + } + + if (xController.is()) + { + Reference<util::XUpdatable> xUpdatable(xController, UNO_QUERY); + if (xUpdatable.is()) + xUpdatable->update(); + } + + return xController; +} + + Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController( ToolBox* pToolBox, const OUString& rsCommandName, diff --git a/sfx2/source/toolbox/weldutils.cxx b/sfx2/source/toolbox/weldutils.cxx new file mode 100644 index 000000000000..3f2d0970cc6e --- /dev/null +++ b/sfx2/source/toolbox/weldutils.cxx @@ -0,0 +1,135 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <comphelper/dispatchcommand.hxx> +#include <sfx2/sidebar/ControllerFactory.hxx> +#include <sfx2/weldutils.hxx> +#include <vcl/commandinfoprovider.hxx> +#include <vcl/settings.hxx> +#include <vcl/weld.hxx> + +namespace +{ +bool lcl_RTLizeCommandURL(OUString& rCommandURL) +{ + if (rCommandURL == ".uno:ParaLeftToRight") + { + rCommandURL = ".uno:ParaRightToLeft"; + return true; + } + if (rCommandURL == ".uno:ParaRightToLeft") + { + rCommandURL = ".uno:ParaLeftToRight"; + return true; + } + if (rCommandURL == ".uno:LeftPara") + { + rCommandURL = ".uno:RightPara"; + return true; + } + if (rCommandURL == ".uno:RightPara") + { + rCommandURL = ".uno:LeftPara"; + return true; + } + if (rCommandURL == ".uno:AlignLeft") + { + rCommandURL = ".uno:AlignRight"; + return true; + } + if (rCommandURL == ".uno:AlignRight") + { + rCommandURL = ".uno:AlignLeft"; + return true; + } + return false; +} +} + +ToolbarUnoDispatcher::ToolbarUnoDispatcher(weld::Toolbar& rToolbar, + const css::uno::Reference<css::frame::XFrame>& rFrame) + : m_xFrame(rFrame) + , m_pToolbar(&rToolbar) +{ + OUString aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame)); + vcl::ImageType eSize = rToolbar.get_icon_size(); + + bool bRTL = AllSettings::GetLayoutRTL(); + + for (int i = 0, nItems = rToolbar.get_n_items(); i < nItems; ++i) + { + OUString sCommand = OUString::fromUtf8(rToolbar.get_item_ident(i)); + if (bRTL && lcl_RTLizeCommandURL(sCommand)) + rToolbar.set_item_ident(i, sCommand.toUtf8()); + + auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(sCommand, aModuleName); + OUString aLabel(vcl::CommandInfoProvider::GetLabelForCommand(aProperties)); + rToolbar.set_item_label(i, aLabel); + OUString aTooltip( + vcl::CommandInfoProvider::GetTooltipForCommand(sCommand, aProperties, rFrame)); + rToolbar.set_item_tooltip_text(i, aTooltip); + auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand, rFrame, eSize)); + rToolbar.set_item_icon(i, xImage); + + CreateController(sCommand); + } + + rToolbar.connect_clicked(LINK(this, ToolbarUnoDispatcher, SelectHdl)); +} + +void ToolbarUnoDispatcher::CreateController(const OUString& rCommand) +{ + css::uno::Reference<css::frame::XToolbarController> xController( + sfx2::sidebar::ControllerFactory::CreateToolBoxController(*m_pToolbar, rCommand, m_xFrame)); + + if (xController.is()) + maControllers.insert(std::make_pair(rCommand, xController)); +} + +css::uno::Reference<css::frame::XToolbarController> +ToolbarUnoDispatcher::GetControllerForCommand(const OUString& rCommand) const +{ + ControllerContainer::const_iterator iController(maControllers.find(rCommand)); + if (iController != maControllers.end()) + return iController->second; + + return css::uno::Reference<css::frame::XToolbarController>(); +} + +IMPL_LINK(ToolbarUnoDispatcher, SelectHdl, const OString&, rCommand, void) +{ + css::uno::Reference<css::frame::XToolbarController> xController( + GetControllerForCommand(OUString::fromUtf8(rCommand))); + + if (xController.is()) + xController->execute(0); +} + +void ToolbarUnoDispatcher::dispose() +{ + if (!m_pToolbar) + return; + + ControllerContainer aControllers; + aControllers.swap(maControllers); + for (auto const& controller : aControllers) + { + css::uno::Reference<css::lang::XComponent> xComponent(controller.second, + css::uno::UNO_QUERY); + if (xComponent.is()) + xComponent->dispose(); + } + + m_pToolbar->connect_clicked(Link<const OString&, void>()); + m_pToolbar = nullptr; +} + +ToolbarUnoDispatcher::~ToolbarUnoDispatcher() { dispose(); } + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |