tdf#42949 Fix IWYU warnings in sfx2/source/[s-v]*/*cxx and sfx2/qa
[LibreOffice.git] / sfx2 / source / toolbox / weldutils.cxx
blob281b7594660a76e639d3152a21979b2b4a9b1acd
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/.
8 */
10 #include <com/sun/star/frame/XSubToolbarController.hpp>
11 #include <sfx2/sidebar/ControllerFactory.hxx>
12 #include <sfx2/weldutils.hxx>
13 #include <vcl/commandinfoprovider.hxx>
14 #include <vcl/settings.hxx>
15 #include <vcl/weld.hxx>
17 namespace
19 bool lcl_RTLizeCommandURL(OUString& rCommandURL)
21 if (rCommandURL == ".uno:ParaLeftToRight")
23 rCommandURL = ".uno:ParaRightToLeft";
24 return true;
26 if (rCommandURL == ".uno:ParaRightToLeft")
28 rCommandURL = ".uno:ParaLeftToRight";
29 return true;
31 if (rCommandURL == ".uno:LeftPara")
33 rCommandURL = ".uno:RightPara";
34 return true;
36 if (rCommandURL == ".uno:RightPara")
38 rCommandURL = ".uno:LeftPara";
39 return true;
41 if (rCommandURL == ".uno:AlignLeft")
43 rCommandURL = ".uno:AlignRight";
44 return true;
46 if (rCommandURL == ".uno:AlignRight")
48 rCommandURL = ".uno:AlignLeft";
49 return true;
51 return false;
55 // for now all controllers are in the sidebar
56 vcl::ImageType ToolbarUnoDispatcher::GetIconSize() const
58 vcl::ImageType eType = vcl::ImageType::Size16;
59 switch (m_aToolbarOptions.GetSidebarIconSize())
61 case ToolBoxButtonSize::Large:
62 eType = vcl::ImageType::Size26;
63 break;
64 case ToolBoxButtonSize::Size32:
65 eType = vcl::ImageType::Size32;
66 break;
67 case ToolBoxButtonSize::DontCare:
68 case ToolBoxButtonSize::Small:
69 break;
71 return eType;
74 ToolbarUnoDispatcher::ToolbarUnoDispatcher(weld::Toolbar& rToolbar,
75 const css::uno::Reference<css::frame::XFrame>& rFrame)
76 : m_xFrame(rFrame)
77 , m_pToolbar(&rToolbar)
79 rToolbar.connect_clicked(LINK(this, ToolbarUnoDispatcher, SelectHdl));
80 rToolbar.connect_menu_toggled(LINK(this, ToolbarUnoDispatcher, ToggleMenuHdl));
82 OUString aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame));
83 vcl::ImageType eSize = GetIconSize();
84 rToolbar.set_icon_size(eSize);
86 bool bRTL = AllSettings::GetLayoutRTL();
88 for (int i = 0, nItems = rToolbar.get_n_items(); i < nItems; ++i)
90 OUString sCommand = OUString::fromUtf8(rToolbar.get_item_ident(i));
91 if (bRTL && lcl_RTLizeCommandURL(sCommand))
92 rToolbar.set_item_ident(i, sCommand.toUtf8());
94 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(sCommand, aModuleName);
95 OUString aLabel(vcl::CommandInfoProvider::GetLabelForCommand(aProperties));
96 rToolbar.set_item_label(i, aLabel);
97 OUString aTooltip(
98 vcl::CommandInfoProvider::GetTooltipForCommand(sCommand, aProperties, rFrame));
99 rToolbar.set_item_tooltip_text(i, aTooltip);
100 auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand, rFrame, eSize));
101 rToolbar.set_item_image(i, xImage);
103 CreateController(sCommand);
106 m_aToolbarOptions.AddListenerLink(LINK(this, ToolbarUnoDispatcher, ChangedIconSizeHandler));
109 void ToolbarUnoDispatcher::CreateController(const OUString& rCommand)
111 css::uno::Reference<css::frame::XToolbarController> xController(
112 sfx2::sidebar::ControllerFactory::CreateToolBoxController(*m_pToolbar, rCommand, m_xFrame));
114 if (xController.is())
115 maControllers.insert(std::make_pair(rCommand, xController));
118 css::uno::Reference<css::frame::XToolbarController>
119 ToolbarUnoDispatcher::GetControllerForCommand(const OUString& rCommand) const
121 ControllerContainer::const_iterator iController(maControllers.find(rCommand));
122 if (iController != maControllers.end())
123 return iController->second;
125 return css::uno::Reference<css::frame::XToolbarController>();
128 IMPL_LINK(ToolbarUnoDispatcher, SelectHdl, const OString&, rCommand, void)
130 css::uno::Reference<css::frame::XToolbarController> xController(
131 GetControllerForCommand(OUString::fromUtf8(rCommand)));
133 if (xController.is())
134 xController->execute(0);
137 IMPL_LINK(ToolbarUnoDispatcher, ToggleMenuHdl, const OString&, rCommand, void)
139 css::uno::Reference<css::frame::XToolbarController> xController(
140 GetControllerForCommand(OUString::fromUtf8(rCommand)));
142 if (xController.is())
143 xController->click();
146 IMPL_LINK_NOARG(ToolbarUnoDispatcher, ChangedIconSizeHandler, LinkParamNone*, void)
148 vcl::ImageType eSize = GetIconSize();
149 m_pToolbar->set_icon_size(eSize);
151 for (int i = 0, nItems = m_pToolbar->get_n_items(); i < nItems; ++i)
153 OUString sCommand = OUString::fromUtf8(m_pToolbar->get_item_ident(i));
154 auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand, m_xFrame, eSize));
155 m_pToolbar->set_item_image(i, xImage);
158 for (auto const& it : maControllers)
160 css::uno::Reference<css::frame::XSubToolbarController> xController(it.second,
161 css::uno::UNO_QUERY);
162 if (xController.is() && xController->opensSubToolbar())
164 // The button should show the last function that was selected from the
165 // dropdown. The controller should know better than us what it was.
166 xController->updateImage();
171 void ToolbarUnoDispatcher::dispose()
173 if (!m_pToolbar)
174 return;
176 m_aToolbarOptions.RemoveListenerLink(LINK(this, ToolbarUnoDispatcher, ChangedIconSizeHandler));
178 ControllerContainer aControllers;
179 aControllers.swap(maControllers);
180 for (auto const& controller : aControllers)
182 css::uno::Reference<css::lang::XComponent> xComponent(controller.second,
183 css::uno::UNO_QUERY);
184 if (xComponent.is())
185 xComponent->dispose();
188 m_pToolbar->connect_clicked(Link<const OString&, void>());
189 m_pToolbar = nullptr;
192 ToolbarUnoDispatcher::~ToolbarUnoDispatcher() { dispose(); }
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */