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/.
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>
19 bool lcl_RTLizeCommandURL(OUString
& rCommandURL
)
21 if (rCommandURL
== ".uno:ParaLeftToRight")
23 rCommandURL
= ".uno:ParaRightToLeft";
26 if (rCommandURL
== ".uno:ParaRightToLeft")
28 rCommandURL
= ".uno:ParaLeftToRight";
31 if (rCommandURL
== ".uno:LeftPara")
33 rCommandURL
= ".uno:RightPara";
36 if (rCommandURL
== ".uno:RightPara")
38 rCommandURL
= ".uno:LeftPara";
41 if (rCommandURL
== ".uno:AlignLeft")
43 rCommandURL
= ".uno:AlignRight";
46 if (rCommandURL
== ".uno:AlignRight")
48 rCommandURL
= ".uno:AlignLeft";
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
;
64 case ToolBoxButtonSize::Size32
:
65 eType
= vcl::ImageType::Size32
;
67 case ToolBoxButtonSize::DontCare
:
68 case ToolBoxButtonSize::Small
:
74 ToolbarUnoDispatcher::ToolbarUnoDispatcher(weld::Toolbar
& rToolbar
,
75 const css::uno::Reference
<css::frame::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
);
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()
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
);
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: */