tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sfx2 / source / sidebar / SidebarToolBox.cxx
blob768262e3003f6bb48614fc731b4df54d43e9b912
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sidebar/SidebarToolBox.hxx>
21 #include <sidebar/ControllerFactory.hxx>
22 #include <sfx2/viewfrm.hxx>
24 #include <officecfg/Office/Common.hxx>
25 #include <vcl/commandinfoprovider.hxx>
26 #include <vcl/event.hxx>
27 #include <vcl/settings.hxx>
28 #include <vcl/svapp.hxx>
29 #include <toolkit/helper/vclunohelper.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>
36 using namespace css;
37 using namespace css::uno;
39 namespace {
40 void lcl_RTLizeCommandURL( OUString& rCommandURL )
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";
57 namespace sfx2::sidebar {
59 SidebarToolBox::SidebarToolBox (vcl::Window* pParentWindow)
60 : ToolBox(pParentWindow, 0),
61 mbAreHandlersRegistered(false),
62 mbUseDefaultButtonSize(true),
63 mbSideBar(true)
65 SetBackground(Wallpaper());
66 SetPaintTransparent(true);
67 SetToolboxButtonSize(GetDefaultButtonSize());
69 SvtMiscOptions().AddListenerLink(LINK(this, SidebarToolBox, ChangedIconHandler));
70 SetDataChangedHdl(LINK(this, SidebarToolBox, ChangedDataHandler));
71 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
73 auto xFrame(pViewFrm->GetFrame().GetFrameInterface());
74 auto xWidget(VCLUnoHelper::GetInterface(this));
75 mxImageController = sfx2::sidebar::ControllerFactory::CreateImageController(xFrame, xWidget);
78 #if OSL_DEBUG_LEVEL >= 2
79 SetText(OUString("SidebarToolBox"));
80 #endif
83 SidebarToolBox::~SidebarToolBox()
85 disposeOnce();
88 void SidebarToolBox::dispose()
90 SvtMiscOptions().RemoveListenerLink(LINK(this, SidebarToolBox, ChangedIconHandler));
91 SetDataChangedHdl(Link<const DataChangedEvent*, void>());
93 ControllerContainer aControllers;
94 aControllers.swap(maControllers);
95 for (auto const& controller : aControllers)
97 Reference<lang::XComponent> xComponent(controller.second, UNO_QUERY);
98 if (xComponent.is())
99 xComponent->dispose();
102 if (mxImageController)
103 mxImageController->dispose();
105 if (mbAreHandlersRegistered)
107 SetDropdownClickHdl(Link<ToolBox *, void>());
108 SetClickHdl(Link<ToolBox *, void>());
109 SetDoubleClickHdl(Link<ToolBox *, void>());
110 SetSelectHdl(Link<ToolBox *, void>());
111 SetActivateHdl(Link<ToolBox *, void>());
112 SetDeactivateHdl(Link<ToolBox *, void>());
113 mbAreHandlersRegistered = false;
116 ToolBox::dispose();
119 ToolBoxButtonSize SidebarToolBox::GetDefaultButtonSize() const
121 return static_cast<ToolBoxButtonSize>(officecfg::Office::Common::Misc::SidebarIconSize::get());
124 void SidebarToolBox::InsertItem(const OUString& rCommand,
125 const css::uno::Reference<css::frame::XFrame>& rFrame,
126 ToolBoxItemBits nBits, const Size& rRequestedSize, ImplToolItems::size_type nPos)
128 OUString aCommand( rCommand );
130 if( AllSettings::GetLayoutRTL() )
132 lcl_RTLizeCommandURL( aCommand );
135 ToolBox::InsertItem(aCommand, rFrame, nBits, rRequestedSize, nPos);
137 CreateController(GetItemId(aCommand), rFrame, std::max(rRequestedSize.Width(), ::tools::Long(0)), mbSideBar);
138 RegisterHandlers();
141 bool SidebarToolBox::EventNotify (NotifyEvent& rEvent)
143 if (rEvent.GetType() == NotifyEventType::KEYINPUT)
145 if (rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_TAB)
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);
153 return ToolBox::EventNotify(rEvent);
156 void SidebarToolBox::KeyInput(const KeyEvent& rKEvt)
158 if (KEY_ESCAPE != rKEvt.GetKeyCode().GetCode())
159 ToolBox::KeyInput(rKEvt);
162 void SidebarToolBox::CreateController (
163 const ToolBoxItemId nItemId,
164 const css::uno::Reference<css::frame::XFrame>& rxFrame,
165 const sal_Int32 nItemWidth, bool bSideBar)
167 const OUString sCommandName (GetItemCommand(nItemId));
169 uno::Reference<frame::XToolbarController> xController(sfx2::sidebar::ControllerFactory::CreateToolBoxController(
170 this, nItemId, sCommandName, rxFrame, rxFrame->getController(),
171 VCLUnoHelper::GetInterface(this), nItemWidth, bSideBar));
173 if (xController.is())
174 maControllers.insert(std::make_pair(nItemId, xController));
177 Reference<frame::XToolbarController> SidebarToolBox::GetControllerForItemId (const ToolBoxItemId nItemId) const
179 ControllerContainer::const_iterator iController (maControllers.find(nItemId));
180 if (iController != maControllers.end())
181 return iController->second;
183 return Reference<frame::XToolbarController>();
186 void SidebarToolBox::RegisterHandlers()
188 if ( ! mbAreHandlersRegistered)
190 mbAreHandlersRegistered = true;
191 SetDropdownClickHdl(LINK(this, SidebarToolBox, DropDownClickHandler));
192 SetClickHdl(LINK(this, SidebarToolBox, ClickHandler));
193 SetDoubleClickHdl(LINK(this, SidebarToolBox, DoubleClickHandler));
194 SetSelectHdl(LINK(this, SidebarToolBox, SelectHandler));
198 IMPL_LINK(SidebarToolBox, DropDownClickHandler, ToolBox*, pToolBox, void)
200 if (pToolBox != nullptr)
202 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
203 if (xController.is())
205 Reference<awt::XWindow> xWindow = xController->createPopupWindow();
206 if (xWindow.is() )
207 xWindow->setFocus();
212 IMPL_LINK(SidebarToolBox, ClickHandler, ToolBox*, pToolBox, void)
214 if (pToolBox == nullptr)
215 return;
217 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
218 if (xController.is())
219 xController->click();
222 IMPL_LINK(SidebarToolBox, DoubleClickHandler, ToolBox*, pToolBox, void)
224 if (pToolBox == nullptr)
225 return;
227 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
228 if (xController.is())
229 xController->doubleClick();
232 IMPL_LINK(SidebarToolBox, SelectHandler, ToolBox*, pToolBox, void)
234 if (pToolBox == nullptr)
235 return;
237 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
238 if (xController.is())
239 xController->execute(static_cast<sal_Int16>(pToolBox->GetModifier()));
242 IMPL_LINK_NOARG(SidebarToolBox, ChangedIconHandler, LinkParamNone*, void)
244 SolarMutexGuard g;
246 if (mbUseDefaultButtonSize)
247 SetToolboxButtonSize(GetDefaultButtonSize());
249 for (auto const& it : maControllers)
251 Reference<frame::XSubToolbarController> xController(it.second, UNO_QUERY);
252 if (xController.is() && xController->opensSubToolbar())
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();
258 else if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
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);
267 Resize();
268 queue_resize();
271 IMPL_LINK(SidebarToolBox, ChangedDataHandler, const DataChangedEvent*, pDataChangedEvent, void)
273 if ((( pDataChangedEvent->GetType() == DataChangedEventType::SETTINGS ) ||
274 ( pDataChangedEvent->GetType() == DataChangedEventType::DISPLAY )) &&
275 ( pDataChangedEvent->GetFlags() & AllSettingsFlags::STYLE ))
277 ChangedIconHandler(nullptr);
281 void SidebarToolBox::InitToolBox(VclBuilder::stringmap& rMap)
283 for (const auto& it : rMap)
285 if (it.first == "toolbar-style")
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")
293 SetButtonType(ButtonType::SYMBOLTEXT);
294 SetToolBoxTextPosition(ToolBoxTextPosition::Bottom);
297 else if (it.first == "icon-size")
299 mbUseDefaultButtonSize = false;
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);
307 else if (it.first == "orientation" && it.second == "vertical")
308 SetAlign(WindowAlign::Left);
312 namespace {
314 class NotebookbarToolBox : public SidebarToolBox
316 public:
317 explicit NotebookbarToolBox(vcl::Window* pParentWindow)
318 : SidebarToolBox(pParentWindow)
320 mbSideBar = false;
321 SetToolboxButtonSize(GetDefaultButtonSize());
324 virtual ToolBoxButtonSize GetDefaultButtonSize() const override
326 return static_cast<ToolBoxButtonSize>(officecfg::Office::Common::Misc::NotebookbarIconSize::get());
332 extern "C" SAL_DLLPUBLIC_EXPORT void makeNotebookbarToolBox(VclPtr<vcl::Window> & rRet, const VclPtr<vcl::Window> & pParent, VclBuilder::stringmap & rMap)
334 static_assert(std::is_same_v<std::remove_pointer_t<VclBuilder::customMakeWidget>,
335 decltype(makeNotebookbarToolBox)>);
336 VclPtrInstance<NotebookbarToolBox> pBox(pParent);
337 pBox->InitToolBox(rMap);
338 rRet = pBox;
341 } // end of namespace sfx2::sidebar
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */