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 <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
, weld::Builder
& rBuilder
,
75 const css::uno::Reference
<css::frame::XFrame
>& rFrame
,
78 , m_pToolbar(&rToolbar
)
79 , m_pBuilder(&rBuilder
)
80 , m_bSideBar(bSideBar
)
82 rToolbar
.connect_clicked(LINK(this, ToolbarUnoDispatcher
, SelectHdl
));
83 rToolbar
.connect_menu_toggled(LINK(this, ToolbarUnoDispatcher
, ToggleMenuHdl
));
85 OUString
aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame
));
86 vcl::ImageType eSize
= GetIconSize();
87 rToolbar
.set_icon_size(eSize
);
89 bool bRTL
= AllSettings::GetLayoutRTL();
91 for (int i
= 0, nItems
= rToolbar
.get_n_items(); i
< nItems
; ++i
)
93 OString
sIdent(rToolbar
.get_item_ident(i
));
94 if (!sIdent
.startsWith(".uno:"))
96 OUString sCommand
= OUString::fromUtf8(sIdent
);
97 if (bRTL
&& lcl_RTLizeCommandURL(sCommand
))
98 rToolbar
.set_item_ident(i
, sCommand
.toUtf8());
100 auto aProperties
= vcl::CommandInfoProvider::GetCommandProperties(sCommand
, aModuleName
);
101 OUString
aLabel(vcl::CommandInfoProvider::GetLabelForCommand(aProperties
));
102 rToolbar
.set_item_label(i
, aLabel
);
104 vcl::CommandInfoProvider::GetTooltipForCommand(sCommand
, aProperties
, rFrame
));
105 rToolbar
.set_item_tooltip_text(i
, aTooltip
);
106 auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand
, rFrame
, eSize
));
107 rToolbar
.set_item_image(i
, xImage
);
109 CreateController(sCommand
);
112 m_aToolbarOptions
.AddListenerLink(LINK(this, ToolbarUnoDispatcher
, ChangedIconSizeHandler
));
115 void ToolbarUnoDispatcher::CreateController(const OUString
& rCommand
)
117 css::uno::Reference
<css::frame::XToolbarController
> xController(
118 sfx2::sidebar::ControllerFactory::CreateToolBoxController(
119 *m_pToolbar
, *m_pBuilder
, rCommand
, m_xFrame
, m_xFrame
->getController(), m_bSideBar
));
121 if (xController
.is())
122 maControllers
.insert(std::make_pair(rCommand
, xController
));
125 css::uno::Reference
<css::frame::XToolbarController
>
126 ToolbarUnoDispatcher::GetControllerForCommand(const OUString
& rCommand
) const
128 ControllerContainer::const_iterator
iController(maControllers
.find(rCommand
));
129 if (iController
!= maControllers
.end())
130 return iController
->second
;
132 return css::uno::Reference
<css::frame::XToolbarController
>();
135 IMPL_LINK(ToolbarUnoDispatcher
, SelectHdl
, const OString
&, rCommand
, void)
137 css::uno::Reference
<css::frame::XToolbarController
> xController(
138 GetControllerForCommand(OUString::fromUtf8(rCommand
)));
140 if (xController
.is())
141 xController
->execute(0);
144 IMPL_LINK(ToolbarUnoDispatcher
, ToggleMenuHdl
, const OString
&, rCommand
, void)
146 css::uno::Reference
<css::frame::XToolbarController
> xController(
147 GetControllerForCommand(OUString::fromUtf8(rCommand
)));
149 if (xController
.is())
150 xController
->click();
153 IMPL_LINK_NOARG(ToolbarUnoDispatcher
, ChangedIconSizeHandler
, LinkParamNone
*, void)
155 vcl::ImageType eSize
= GetIconSize();
156 m_pToolbar
->set_icon_size(eSize
);
158 for (int i
= 0, nItems
= m_pToolbar
->get_n_items(); i
< nItems
; ++i
)
160 OUString sCommand
= OUString::fromUtf8(m_pToolbar
->get_item_ident(i
));
161 auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand
, m_xFrame
, eSize
));
162 m_pToolbar
->set_item_image(i
, xImage
);
165 for (auto const& it
: maControllers
)
167 css::uno::Reference
<css::frame::XSubToolbarController
> xController(it
.second
,
168 css::uno::UNO_QUERY
);
169 if (xController
.is() && xController
->opensSubToolbar())
171 // The button should show the last function that was selected from the
172 // dropdown. The controller should know better than us what it was.
173 xController
->updateImage();
178 void ToolbarUnoDispatcher::dispose()
183 m_aToolbarOptions
.RemoveListenerLink(LINK(this, ToolbarUnoDispatcher
, ChangedIconSizeHandler
));
185 ControllerContainer aControllers
;
186 aControllers
.swap(maControllers
);
187 for (auto const& controller
: aControllers
)
189 css::uno::Reference
<css::lang::XComponent
> xComponent(controller
.second
,
190 css::uno::UNO_QUERY
);
192 xComponent
->dispose();
195 m_pToolbar
->connect_clicked(Link
<const OString
&, void>());
196 m_pToolbar
= nullptr;
197 m_pBuilder
= nullptr;
200 ToolbarUnoDispatcher::~ToolbarUnoDispatcher() { dispose(); }
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */