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 <officecfg/Office/Common.hxx>
11 #include <com/sun/star/frame/XSubToolbarController.hpp>
12 #include <sidebar/ControllerFactory.hxx>
13 #include <sfx2/weldutils.hxx>
14 #include <vcl/commandinfoprovider.hxx>
15 #include <vcl/settings.hxx>
16 #include <vcl/weld.hxx>
20 bool lcl_RTLizeCommandURL(OUString
& rCommandURL
)
22 if (rCommandURL
== ".uno:ParaLeftToRight")
24 rCommandURL
= ".uno:ParaRightToLeft";
27 if (rCommandURL
== ".uno:ParaRightToLeft")
29 rCommandURL
= ".uno:ParaLeftToRight";
32 if (rCommandURL
== ".uno:LeftPara")
34 rCommandURL
= ".uno:RightPara";
37 if (rCommandURL
== ".uno:RightPara")
39 rCommandURL
= ".uno:LeftPara";
42 if (rCommandURL
== ".uno:AlignLeft")
44 rCommandURL
= ".uno:AlignRight";
47 if (rCommandURL
== ".uno:AlignRight")
49 rCommandURL
= ".uno:AlignLeft";
56 // for now all controllers are in the sidebar
57 vcl::ImageType
ToolbarUnoDispatcher::GetIconSize()
59 vcl::ImageType eType
= vcl::ImageType::Size16
;
60 switch (static_cast<ToolBoxButtonSize
>(officecfg::Office::Common::Misc::SidebarIconSize::get()))
62 case ToolBoxButtonSize::Large
:
63 eType
= vcl::ImageType::Size26
;
65 case ToolBoxButtonSize::Size32
:
66 eType
= vcl::ImageType::Size32
;
68 case ToolBoxButtonSize::DontCare
:
69 case ToolBoxButtonSize::Small
:
75 ToolbarUnoDispatcher::ToolbarUnoDispatcher(weld::Toolbar
& rToolbar
, weld::Builder
& rBuilder
,
76 const css::uno::Reference
<css::frame::XFrame
>& rFrame
,
79 , m_pToolbar(&rToolbar
)
80 , m_pBuilder(&rBuilder
)
81 , m_bSideBar(bSideBar
)
83 rToolbar
.connect_clicked(LINK(this, ToolbarUnoDispatcher
, SelectHdl
));
84 rToolbar
.connect_menu_toggled(LINK(this, ToolbarUnoDispatcher
, ToggleMenuHdl
));
86 OUString
aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame
));
87 vcl::ImageType eSize
= GetIconSize();
88 rToolbar
.set_icon_size(eSize
);
90 bool bRTL
= AllSettings::GetLayoutRTL();
92 for (int i
= 0, nItems
= rToolbar
.get_n_items(); i
< nItems
; ++i
)
94 OString
sIdent(rToolbar
.get_item_ident(i
));
95 if (!sIdent
.startsWith(".uno:"))
97 OUString sCommand
= OUString::fromUtf8(sIdent
);
98 if (bRTL
&& lcl_RTLizeCommandURL(sCommand
))
99 rToolbar
.set_item_ident(i
, sCommand
.toUtf8());
101 auto aProperties
= vcl::CommandInfoProvider::GetCommandProperties(sCommand
, aModuleName
);
102 OUString
aLabel(vcl::CommandInfoProvider::GetLabelForCommand(aProperties
));
103 rToolbar
.set_item_label(i
, aLabel
);
105 vcl::CommandInfoProvider::GetTooltipForCommand(sCommand
, aProperties
, rFrame
));
106 rToolbar
.set_item_tooltip_text(i
, aTooltip
);
107 auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand
, rFrame
, eSize
));
108 rToolbar
.set_item_image(i
, xImage
);
110 CreateController(sCommand
);
113 m_aToolbarOptions
.AddListenerLink(LINK(this, ToolbarUnoDispatcher
, ChangedIconSizeHandler
));
116 void ToolbarUnoDispatcher::CreateController(const OUString
& rCommand
)
118 css::uno::Reference
<css::frame::XToolbarController
> xController(
119 sfx2::sidebar::ControllerFactory::CreateToolBoxController(
120 *m_pToolbar
, *m_pBuilder
, rCommand
, m_xFrame
, m_xFrame
->getController(), m_bSideBar
));
122 if (xController
.is())
123 maControllers
.insert(std::make_pair(rCommand
, xController
));
126 css::uno::Reference
<css::frame::XToolbarController
>
127 ToolbarUnoDispatcher::GetControllerForCommand(const OUString
& rCommand
) const
129 ControllerContainer::const_iterator
iController(maControllers
.find(rCommand
));
130 if (iController
!= maControllers
.end())
131 return iController
->second
;
133 return css::uno::Reference
<css::frame::XToolbarController
>();
136 IMPL_LINK(ToolbarUnoDispatcher
, SelectHdl
, const OString
&, rCommand
, void)
138 css::uno::Reference
<css::frame::XToolbarController
> xController(
139 GetControllerForCommand(OUString::fromUtf8(rCommand
)));
141 if (xController
.is())
142 xController
->execute(0);
145 IMPL_LINK(ToolbarUnoDispatcher
, ToggleMenuHdl
, const OString
&, rCommand
, void)
147 css::uno::Reference
<css::frame::XToolbarController
> xController(
148 GetControllerForCommand(OUString::fromUtf8(rCommand
)));
150 if (xController
.is())
151 xController
->click();
154 IMPL_LINK_NOARG(ToolbarUnoDispatcher
, ChangedIconSizeHandler
, LinkParamNone
*, void)
156 vcl::ImageType eSize
= GetIconSize();
157 m_pToolbar
->set_icon_size(eSize
);
159 for (int i
= 0, nItems
= m_pToolbar
->get_n_items(); i
< nItems
; ++i
)
161 OUString sCommand
= OUString::fromUtf8(m_pToolbar
->get_item_ident(i
));
162 auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand
, m_xFrame
, eSize
));
163 m_pToolbar
->set_item_image(i
, xImage
);
166 for (auto const& it
: maControllers
)
168 css::uno::Reference
<css::frame::XSubToolbarController
> xController(it
.second
,
169 css::uno::UNO_QUERY
);
170 if (xController
.is() && xController
->opensSubToolbar())
172 // The button should show the last function that was selected from the
173 // dropdown. The controller should know better than us what it was.
174 xController
->updateImage();
179 void ToolbarUnoDispatcher::dispose()
184 m_aToolbarOptions
.RemoveListenerLink(LINK(this, ToolbarUnoDispatcher
, ChangedIconSizeHandler
));
186 ControllerContainer aControllers
;
187 aControllers
.swap(maControllers
);
188 for (auto const& controller
: aControllers
)
190 css::uno::Reference
<css::lang::XComponent
> xComponent(controller
.second
,
191 css::uno::UNO_QUERY
);
193 xComponent
->dispose();
196 m_pToolbar
->connect_clicked(Link
<const OString
&, void>());
197 m_pToolbar
= nullptr;
198 m_pBuilder
= nullptr;
201 ToolbarUnoDispatcher::~ToolbarUnoDispatcher() { dispose(); }
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */