LibreOffice Module sfx2 (master) 1
SidebarToolBox.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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
22#include <sfx2/viewfrm.hxx>
23
24#include <officecfg/Office/Common.hxx>
26#include <vcl/event.hxx>
27#include <vcl/settings.hxx>
28#include <vcl/svapp.hxx>
30#include <svtools/miscopt.hxx>
31#include <com/sun/star/frame/XSubToolbarController.hpp>
32#include <com/sun/star/lang/XComponent.hpp>
33#include <com/sun/star/frame/XFrame.hpp>
34#include <com/sun/star/frame/XToolbarController.hpp>
35
36using namespace css;
37using namespace css::uno;
38
39namespace {
40 void lcl_RTLizeCommandURL( OUString& rCommandURL )
41 {
42 if (rCommandURL == ".uno:ParaLeftToRight")
43 rCommandURL = ".uno:ParaRightToLeft";
44 else if (rCommandURL == ".uno:ParaRightToLeft")
45 rCommandURL = ".uno:ParaLeftToRight";
46 else if (rCommandURL == ".uno:LeftPara")
47 rCommandURL = ".uno:RightPara";
48 else if (rCommandURL == ".uno:RightPara")
49 rCommandURL = ".uno:LeftPara";
50 else if (rCommandURL == ".uno:AlignLeft")
51 rCommandURL = ".uno:AlignRight";
52 else if (rCommandURL == ".uno:AlignRight")
53 rCommandURL = ".uno:AlignLeft";
54 }
55}
56
57namespace sfx2::sidebar {
58
60 : ToolBox(pParentWindow, 0),
61 mbAreHandlersRegistered(false),
62 mbUseDefaultButtonSize(true),
63 mbSideBar(true)
64{
68
69 SvtMiscOptions().AddListenerLink(LINK(this, SidebarToolBox, ChangedIconHandler));
70 SetDataChangedHdl(LINK(this, SidebarToolBox, ChangedDataHandler));
71 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
72 {
73 auto xFrame(pViewFrm->GetFrame().GetFrameInterface());
74 auto xWidget(VCLUnoHelper::GetInterface(this));
76 }
77
78#ifdef DEBUG
79 SetText(OUString("SidebarToolBox"));
80#endif
81}
82
84{
86}
87
89{
90 SvtMiscOptions().RemoveListenerLink(LINK(this, SidebarToolBox, ChangedIconHandler));
92
93 ControllerContainer aControllers;
94 aControllers.swap(maControllers);
95 for (auto const& controller : aControllers)
96 {
97 Reference<lang::XComponent> xComponent(controller.second, UNO_QUERY);
98 if (xComponent.is())
99 xComponent->dispose();
100 }
101
103 mxImageController->dispose();
104
106 {
114 }
115
117}
118
120{
121 return static_cast<ToolBoxButtonSize>(officecfg::Office::Common::Misc::SidebarIconSize::get());
122}
123
124void SidebarToolBox::InsertItem(const OUString& rCommand,
125 const css::uno::Reference<css::frame::XFrame>& rFrame,
126 ToolBoxItemBits nBits, const Size& rRequestedSize, ImplToolItems::size_type nPos)
127{
128 OUString aCommand( rCommand );
129
131 {
132 lcl_RTLizeCommandURL( aCommand );
133 }
134
135 ToolBox::InsertItem(aCommand, rFrame, nBits, rRequestedSize, nPos);
136
137 CreateController(GetItemId(aCommand), rFrame, std::max(rRequestedSize.Width(), ::tools::Long(0)), mbSideBar);
139}
140
142{
143 if (rEvent.GetType() == NotifyEventType::KEYINPUT)
144 {
145 if (rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_TAB)
146 {
147 // Special handling for transferring handling of KEY_TAB
148 // that becomes necessary because of our parent that is
149 // not the dialog but a background control.
150 return DockingWindow::EventNotify(rEvent);
151 }
152 }
153 return ToolBox::EventNotify(rEvent);
154}
155
157{
158 if (KEY_ESCAPE != rKEvt.GetKeyCode().GetCode())
159 ToolBox::KeyInput(rKEvt);
160}
161
163 const ToolBoxItemId nItemId,
164 const css::uno::Reference<css::frame::XFrame>& rxFrame,
165 const sal_Int32 nItemWidth, bool bSideBar)
166{
167 const OUString sCommandName (GetItemCommand(nItemId));
168
169 uno::Reference<frame::XToolbarController> xController(sfx2::sidebar::ControllerFactory::CreateToolBoxController(
170 this, nItemId, sCommandName, rxFrame, rxFrame->getController(),
171 VCLUnoHelper::GetInterface(this), nItemWidth, bSideBar));
172
173 if (xController.is())
174 maControllers.insert(std::make_pair(nItemId, xController));
175}
176
177Reference<frame::XToolbarController> SidebarToolBox::GetControllerForItemId (const ToolBoxItemId nItemId) const
178{
179 ControllerContainer::const_iterator iController (maControllers.find(nItemId));
180 if (iController != maControllers.end())
181 return iController->second;
182
183 return Reference<frame::XToolbarController>();
184}
185
187{
189 {
191 SetDropdownClickHdl(LINK(this, SidebarToolBox, DropDownClickHandler));
192 SetClickHdl(LINK(this, SidebarToolBox, ClickHandler));
193 SetDoubleClickHdl(LINK(this, SidebarToolBox, DoubleClickHandler));
194 SetSelectHdl(LINK(this, SidebarToolBox, SelectHandler));
195 }
196}
197
198IMPL_LINK(SidebarToolBox, DropDownClickHandler, ToolBox*, pToolBox, void)
199{
200 if (pToolBox != nullptr)
201 {
202 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
203 if (xController.is())
204 {
205 Reference<awt::XWindow> xWindow = xController->createPopupWindow();
206 if (xWindow.is() )
207 xWindow->setFocus();
208 }
209 }
210}
211
212IMPL_LINK(SidebarToolBox, ClickHandler, ToolBox*, pToolBox, void)
213{
214 if (pToolBox == nullptr)
215 return;
216
217 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
218 if (xController.is())
219 xController->click();
220}
221
222IMPL_LINK(SidebarToolBox, DoubleClickHandler, ToolBox*, pToolBox, void)
223{
224 if (pToolBox == nullptr)
225 return;
226
227 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
228 if (xController.is())
229 xController->doubleClick();
230}
231
232IMPL_LINK(SidebarToolBox, SelectHandler, ToolBox*, pToolBox, void)
233{
234 if (pToolBox == nullptr)
235 return;
236
237 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
238 if (xController.is())
239 xController->execute(static_cast<sal_Int16>(pToolBox->GetModifier()));
240}
241
242IMPL_LINK_NOARG(SidebarToolBox, ChangedIconHandler, LinkParamNone*, void)
243{
245
246 if (mbUseDefaultButtonSize)
247 SetToolboxButtonSize(GetDefaultButtonSize());
248
249 for (auto const& it : maControllers)
250 {
251 Reference<frame::XSubToolbarController> xController(it.second, UNO_QUERY);
252 if (xController.is() && xController->opensSubToolbar())
253 {
254 // The button should show the last function that was selected from the
255 // dropdown. The controller should know better than us what it was.
256 xController->updateImage();
257 }
258 else if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
259 {
260 OUString aCommandURL = GetItemCommand(it.first);
261 css::uno::Reference<frame::XFrame> xFrame = pViewFrm->GetFrame().GetFrameInterface();
262 Image aImage = vcl::CommandInfoProvider::GetImageForCommand(aCommandURL, xFrame, GetImageSize());
263 SetItemImage(it.first, aImage);
264 }
265 }
266
267 Resize();
268 queue_resize();
269}
270
271IMPL_LINK(SidebarToolBox, ChangedDataHandler, const DataChangedEvent*, pDataChangedEvent, void)
272{
273 if ((( pDataChangedEvent->GetType() == DataChangedEventType::SETTINGS ) ||
274 ( pDataChangedEvent->GetType() == DataChangedEventType::DISPLAY )) &&
275 ( pDataChangedEvent->GetFlags() & AllSettingsFlags::STYLE ))
276 {
277 ChangedIconHandler(nullptr);
278 }
279}
280
282{
283 for (const auto& it : rMap)
284 {
285 if (it.first == "toolbar-style")
286 {
287 if (it.second == "text")
288 SetButtonType(ButtonType::TEXT);
289 else if (it.second == "both-horiz")
290 SetButtonType(ButtonType::SYMBOLTEXT);
291 else if (it.second == "both")
292 {
293 SetButtonType(ButtonType::SYMBOLTEXT);
294 SetToolBoxTextPosition(ToolBoxTextPosition::Bottom);
295 }
296 }
297 else if (it.first == "icon-size")
298 {
300 if (it.second == "1" || it.second == "2" || it.second == "4")
301 SetToolboxButtonSize(ToolBoxButtonSize::Small);
302 else if (it.second == "3")
303 SetToolboxButtonSize(ToolBoxButtonSize::Large);
304 else if (it.second == "5")
305 SetToolboxButtonSize(ToolBoxButtonSize::Size32);
306 }
307 else if (it.first == "orientation" && it.second == "vertical")
308 SetAlign(WindowAlign::Left);
309 }
310}
311
312namespace {
313
314class NotebookbarToolBox : public SidebarToolBox
315{
316public:
317 explicit NotebookbarToolBox(vcl::Window* pParentWindow)
318 : SidebarToolBox(pParentWindow)
319 {
320 mbSideBar = false;
321 SetToolboxButtonSize(GetDefaultButtonSize());
322 }
323
324 virtual ToolBoxButtonSize GetDefaultButtonSize() const override
325 {
326 return static_cast<ToolBoxButtonSize>(officecfg::Office::Common::Misc::NotebookbarIconSize::get());
327 }
328};
329
330}
331
332extern "C" SAL_DLLPUBLIC_EXPORT void makeNotebookbarToolBox(VclPtr<vcl::Window> & rRet, const VclPtr<vcl::Window> & pParent, VclBuilder::stringmap & rMap)
333{
334 static_assert(std::is_same_v<std::remove_pointer_t<VclBuilder::customMakeWidget>,
335 decltype(makeNotebookbarToolBox)>);
337 pBox->InitToolBox(rMap);
338 rRet = pBox;
339}
340
341} // end of namespace sfx2::sidebar
342
343/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool GetLayoutRTL()
virtual void SetText(const OUString &rStr) override
virtual bool EventNotify(NotifyEvent &rNEvt) override
const vcl::KeyCode & GetKeyCode() const
const KeyEvent * GetKeyEvent() const
NotifyEventType GetType() const
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
Definition: viewfrm.cxx:1975
constexpr tools::Long Width() const
void RemoveListenerLink(const Link< LinkParamNone *, void > &rLink)
void AddListenerLink(const Link< LinkParamNone *, void > &rLink)
ToolBoxItemId GetItemId(ImplToolItems::size_type nPos) const
void SetSelectHdl(const Link< ToolBox *, void > &rLink)
void SetDataChangedHdl(const Link< DataChangedEvent const *, void > &aLink)
void SetDeactivateHdl(const Link< ToolBox *, void > &rLink)
void SetDropdownClickHdl(const Link< ToolBox *, void > &rLink)
OUString GetItemCommand(ToolBoxItemId nItemId) const
void SetDoubleClickHdl(const Link< ToolBox *, void > &rLink)
virtual void dispose() override
virtual bool EventNotify(NotifyEvent &rNEvt) override
void SetToolBoxTextPosition(ToolBoxTextPosition ePosition)
void SetActivateHdl(const Link< ToolBox *, void > &rLink)
virtual void KeyInput(const KeyEvent &rKEvt) override
virtual void InsertItem(const OUString &rCommand, const css::uno::Reference< css::frame::XFrame > &rFrame, ToolBoxItemBits nBits, const Size &rRequestedSize, ImplToolItems::size_type nPos=APPEND)
void SetButtonType(ButtonType eNewType)
void SetClickHdl(const Link< ToolBox *, void > &rLink)
void SetAlign(WindowAlign eNewAlign=WindowAlign::Top)
void SetToolboxButtonSize(ToolBoxButtonSize eSize)
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
std::map< OUString, OUString > stringmap
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)
The sidebar tool box has two responsibilities:
css::uno::Reference< css::frame::XToolbarController > GetControllerForItemId(const ToolBoxItemId nItemId) const
void CreateController(const ToolBoxItemId nItemId, const css::uno::Reference< css::frame::XFrame > &rxFrame, const sal_Int32 nItemWidth, bool bSideBar)
void InitToolBox(VclBuilder::stringmap &rMap)
SidebarToolBox(vcl::Window *pParentWindow)
css::uno::Reference< css::lang::XComponent > mxImageController
virtual void InsertItem(const OUString &rCommand, const css::uno::Reference< css::frame::XFrame > &rFrame, ToolBoxItemBits nBits, const Size &rRequestedSize, ImplToolItems::size_type nPos=APPEND) override
std::map< ToolBoxItemId, css::uno::Reference< css::frame::XToolbarController > > ControllerContainer
virtual bool EventNotify(NotifyEvent &rEvent) override
virtual void dispose() override
ControllerContainer maControllers
virtual void KeyInput(const KeyEvent &rKEvt) override
virtual ToolBoxButtonSize GetDefaultButtonSize() const
virtual ~SidebarToolBox() override
sal_uInt16 GetCode() const
void SetPaintTransparent(bool bTransparent)
void SetBackground()
constexpr sal_uInt16 KEY_ESCAPE
constexpr sal_uInt16 KEY_TAB
sal_uInt16 nPos
Definition: linksrc.cxx:118
IMPL_LINK_NOARG(AsynchronousCall, HandleUserCall, void *, void)
SAL_DLLPUBLIC_EXPORT void makeNotebookbarToolBox(VclPtr< vcl::Window > &rRet, const VclPtr< vcl::Window > &pParent, VclBuilder::stringmap &rMap)
IMPL_LINK(FocusManager, KeyInputHdl, const KeyEvent &, rKeyEvent, bool)
long Long
Image GetImageForCommand(const OUString &rsCommandName, const Reference< frame::XFrame > &rxFrame, vcl::ImageType eImageType)
Reference< XController > xController
Reference< XFrame > xFrame
OUString aCommand
ToolBoxButtonSize
ToolBoxItemBits