LibreOffice Module sfx2 (master) 1
weldutils.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10#include <officecfg/Office/Common.hxx>
11#include <com/sun/star/frame/XSubToolbarController.hpp>
13#include <sfx2/weldutils.hxx>
15#include <vcl/settings.hxx>
16#include <vcl/weldutils.hxx>
17
18namespace
19{
20bool lcl_RTLizeCommandURL(OUString& rCommandURL)
21{
22 if (rCommandURL == ".uno:ParaLeftToRight")
23 {
24 rCommandURL = ".uno:ParaRightToLeft";
25 return true;
26 }
27 if (rCommandURL == ".uno:ParaRightToLeft")
28 {
29 rCommandURL = ".uno:ParaLeftToRight";
30 return true;
31 }
32 if (rCommandURL == ".uno:LeftPara")
33 {
34 rCommandURL = ".uno:RightPara";
35 return true;
36 }
37 if (rCommandURL == ".uno:RightPara")
38 {
39 rCommandURL = ".uno:LeftPara";
40 return true;
41 }
42 if (rCommandURL == ".uno:AlignLeft")
43 {
44 rCommandURL = ".uno:AlignRight";
45 return true;
46 }
47 if (rCommandURL == ".uno:AlignRight")
48 {
49 rCommandURL = ".uno:AlignLeft";
50 return true;
51 }
52 return false;
53}
54}
55
56// for now all controllers are in the sidebar
58{
60 switch (static_cast<ToolBoxButtonSize>(officecfg::Office::Common::Misc::SidebarIconSize::get()))
61 {
62 case ToolBoxButtonSize::Large:
64 break;
65 case ToolBoxButtonSize::Size32:
67 break;
68 case ToolBoxButtonSize::DontCare:
69 case ToolBoxButtonSize::Small:
70 break;
71 }
72 return eType;
73}
74
76 const css::uno::Reference<css::frame::XFrame>& rFrame,
77 bool bSideBar)
78 : m_xFrame(rFrame)
79 , m_pToolbar(&rToolbar)
80 , m_pBuilder(&rBuilder)
81 , m_bSideBar(bSideBar)
82{
83 rToolbar.connect_clicked(LINK(this, ToolbarUnoDispatcher, SelectHdl));
84 rToolbar.connect_menu_toggled(LINK(this, ToolbarUnoDispatcher, ToggleMenuHdl));
85
86 OUString aModuleName(vcl::CommandInfoProvider::GetModuleIdentifier(rFrame));
88 rToolbar.set_icon_size(eSize);
89
90 bool bRTL = AllSettings::GetLayoutRTL();
91
92 for (int i = 0, nItems = rToolbar.get_n_items(); i < nItems; ++i)
93 {
94 OUString sIdent(rToolbar.get_item_ident(i));
95 if (!sIdent.startsWith(".uno:"))
96 continue;
97 OUString sCommand = sIdent;
98 if (bRTL && lcl_RTLizeCommandURL(sCommand))
99 rToolbar.set_item_ident(i, sCommand);
100
103 rToolbar.set_item_label(i, aLabel);
104 OUString aTooltip(
106 rToolbar.set_item_tooltip_text(i, aTooltip);
107 auto xImage(vcl::CommandInfoProvider::GetXGraphicForCommand(sCommand, rFrame, eSize));
108 rToolbar.set_item_image(i, xImage);
109
110 CreateController(sCommand);
111 }
112
115 m_aToolbarOptions.AddListenerLink(LINK(this, ToolbarUnoDispatcher, ChangedIconSizeHandler));
116}
117
118void ToolbarUnoDispatcher::CreateController(const OUString& rCommand)
119{
120 css::uno::Reference<css::frame::XToolbarController> xController(
122 *m_pToolbar, *m_pBuilder, rCommand, m_xFrame, m_xFrame->getController(), m_bSideBar));
123
124 if (xController.is())
125 maControllers.insert(std::make_pair(rCommand, xController));
126}
127
128css::uno::Reference<css::frame::XToolbarController>
130{
131 ControllerContainer::const_iterator iController(maControllers.find(rCommand));
132 if (iController != maControllers.end())
133 return iController->second;
134
135 return css::uno::Reference<css::frame::XToolbarController>();
136}
137
138IMPL_LINK(ToolbarUnoDispatcher, SelectHdl, const OUString&, rCommand, void)
139{
140 css::uno::Reference<css::frame::XToolbarController> xController(
141 GetControllerForCommand(rCommand));
142
143 if (xController.is())
144 xController->execute(0);
145}
146
147IMPL_LINK(ToolbarUnoDispatcher, ToggleMenuHdl, const OUString&, rCommand, void)
148{
149 css::uno::Reference<css::frame::XToolbarController> xController(
150 GetControllerForCommand(rCommand));
151
152 if (xController.is())
153 xController->click();
154}
155
156IMPL_LINK_NOARG(ToolbarUnoDispatcher, ChangedIconSizeHandler, LinkParamNone*, void)
157{
158 vcl::ImageType eSize = GetIconSize();
159 m_pToolbar->set_icon_size(eSize);
160
161 for (int i = 0, nItems = m_pToolbar->get_n_items(); i < nItems; ++i)
162 {
163 OUString sIdent(m_pToolbar->get_item_ident(i));
165 m_pToolbar->set_item_image(sIdent, xImage);
166 }
167
168 for (auto const& it : maControllers)
169 {
170 css::uno::Reference<css::frame::XSubToolbarController> xController(it.second,
171 css::uno::UNO_QUERY);
172 if (xController.is() && xController->opensSubToolbar())
173 {
174 // The button should show the last function that was selected from the
175 // dropdown. The controller should know better than us what it was.
176 xController->updateImage();
177 }
178 }
179}
180
182{
183 if (!m_pToolbar)
184 return;
185
186 m_aToolbarOptions.RemoveListenerLink(LINK(this, ToolbarUnoDispatcher, ChangedIconSizeHandler));
187
188 ControllerContainer aControllers;
189 aControllers.swap(maControllers);
190 for (auto const& controller : aControllers)
191 {
192 css::uno::Reference<css::lang::XComponent> xComponent(controller.second,
193 css::uno::UNO_QUERY);
194 if (xComponent.is())
195 xComponent->dispose();
196 }
197
198 m_xImageController->dispose();
200 m_pToolbar = nullptr;
201 m_pBuilder = nullptr;
202}
203
205
206/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
css::uno::Reference< css::lang::XComponent > m_xFrame
static bool GetLayoutRTL()
void RemoveListenerLink(const Link< LinkParamNone *, void > &rLink)
void AddListenerLink(const Link< LinkParamNone *, void > &rLink)
ToolbarUnoDispatcher(weld::Toolbar &rToolbar, weld::Builder &rBuilder, const css::uno::Reference< css::frame::XFrame > &rFrame, bool bSideBar=true)
Definition: weldutils.cxx:75
SvtMiscOptions m_aToolbarOptions
Definition: weldutils.hxx:35
css::uno::Reference< css::lang::XComponent > m_xImageController
Definition: weldutils.hxx:34
ControllerContainer maControllers
Definition: weldutils.hxx:49
weld::Toolbar * m_pToolbar
Definition: weldutils.hxx:36
void CreateController(const OUString &rCommand)
Definition: weldutils.cxx:118
std::map< OUString, css::uno::Reference< css::frame::XToolbarController > > ControllerContainer
Definition: weldutils.hxx:48
css::uno::Reference< css::frame::XToolbarController > GetControllerForCommand(const OUString &rCommand) const
Definition: weldutils.cxx:129
css::uno::Reference< css::frame::XFrame > m_xFrame
Definition: weldutils.hxx:33
static vcl::ImageType GetIconSize()
Definition: weldutils.cxx:57
weld::Builder * m_pBuilder
Definition: weldutils.hxx:37
static css::uno::Reference< css::frame::XToolbarController > CreateToolBoxController(ToolBox *pToolBox, const ToolBoxItemId nItemId, const OUString &rsCommandName, const css::uno::Reference< css::frame::XFrame > &rxFrame, const css::uno::Reference< css::frame::XController > &rxController, const css::uno::Reference< css::awt::XWindow > &rxParentWindow, const sal_Int32 nItemWidth, bool bSideBar)
static css::uno::Reference< css::lang::XComponent > CreateImageController(const css::uno::Reference< css::frame::XFrame > &rxFrame, const css::uno::Reference< css::awt::XWindow > &rxParentWindow)
void connect_clicked(const Link< const OUString &, void > &rLink)
virtual void set_item_ident(int nIndex, const OUString &rIdent)=0
virtual int get_n_items() const=0
virtual void set_item_label(const OUString &rIdent, const OUString &rLabel)=0
virtual void set_item_tooltip_text(const OUString &rIdent, const OUString &rTip)=0
virtual OUString get_item_ident(int nIndex) const=0
void connect_menu_toggled(const Link< const OUString &, void > &rLink)
virtual void set_item_image(const OUString &rIdent, const css::uno::Reference< css::graphic::XGraphic > &rIcon)=0
virtual void set_icon_size(vcl::ImageType eType)=0
DocumentType eType
int i
Sequence< beans::PropertyValue > GetCommandProperties(const OUString &rsCommandName, const OUString &rsModuleName)
OUString GetTooltipForCommand(const OUString &rsCommandName, const css::uno::Sequence< css::beans::PropertyValue > &rProperties, const Reference< frame::XFrame > &rxFrame)
Reference< graphic::XGraphic > GetXGraphicForCommand(const OUString &rsCommandName, const Reference< frame::XFrame > &rxFrame, vcl::ImageType eImageType)
OUString GetModuleIdentifier(const Reference< frame::XFrame > &rxFrame)
OUString GetLabelForCommand(const css::uno::Sequence< css::beans::PropertyValue > &rProperties)
Reference< XController > xController
weld::Builder * m_pBuilder
OUString aLabel
ToolBoxButtonSize
IMPL_LINK_NOARG(ToolbarUnoDispatcher, ChangedIconSizeHandler, LinkParamNone *, void)
Definition: weldutils.cxx:156
IMPL_LINK(ToolbarUnoDispatcher, SelectHdl, const OUString &, rCommand, void)
Definition: weldutils.cxx:138