LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / sidebar / SidebarToolBox.cxx
blobbb89d007bbb0567029432b8250a7eac43be8d824
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, ChangedIconSizeHandler));
71 #ifdef DEBUG
72 SetText(OUString("SidebarToolBox"));
73 #endif
76 SidebarToolBox::~SidebarToolBox()
78 disposeOnce();
81 void SidebarToolBox::dispose()
83 SvtMiscOptions().RemoveListenerLink(LINK(this, SidebarToolBox, ChangedIconSizeHandler));
85 ControllerContainer aControllers;
86 aControllers.swap(maControllers);
87 for (auto const& controller : aControllers)
89 Reference<lang::XComponent> xComponent(controller.second, UNO_QUERY);
90 if (xComponent.is())
91 xComponent->dispose();
94 if (mbAreHandlersRegistered)
96 SetDropdownClickHdl(Link<ToolBox *, void>());
97 SetClickHdl(Link<ToolBox *, void>());
98 SetDoubleClickHdl(Link<ToolBox *, void>());
99 SetSelectHdl(Link<ToolBox *, void>());
100 SetActivateHdl(Link<ToolBox *, void>());
101 SetDeactivateHdl(Link<ToolBox *, void>());
102 mbAreHandlersRegistered = false;
105 ToolBox::dispose();
108 ToolBoxButtonSize SidebarToolBox::GetDefaultButtonSize() const
110 return static_cast<ToolBoxButtonSize>(officecfg::Office::Common::Misc::SidebarIconSize::get());
113 void SidebarToolBox::InsertItem(const OUString& rCommand,
114 const css::uno::Reference<css::frame::XFrame>& rFrame,
115 ToolBoxItemBits nBits, const Size& rRequestedSize, ImplToolItems::size_type nPos)
117 OUString aCommand( rCommand );
119 if( AllSettings::GetLayoutRTL() )
121 lcl_RTLizeCommandURL( aCommand );
124 ToolBox::InsertItem(aCommand, rFrame, nBits, rRequestedSize, nPos);
126 CreateController(GetItemId(aCommand), rFrame, std::max(rRequestedSize.Width(), ::tools::Long(0)), mbSideBar);
127 RegisterHandlers();
130 bool SidebarToolBox::EventNotify (NotifyEvent& rEvent)
132 if (rEvent.GetType() == MouseNotifyEvent::KEYINPUT)
134 if (rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_TAB)
136 // Special handling for transferring handling of KEY_TAB
137 // that becomes necessary because of our parent that is
138 // not the dialog but a background control.
139 return DockingWindow::EventNotify(rEvent);
142 return ToolBox::EventNotify(rEvent);
145 void SidebarToolBox::KeyInput(const KeyEvent& rKEvt)
147 if (KEY_ESCAPE != rKEvt.GetKeyCode().GetCode())
148 ToolBox::KeyInput(rKEvt);
151 void SidebarToolBox::CreateController (
152 const ToolBoxItemId nItemId,
153 const css::uno::Reference<css::frame::XFrame>& rxFrame,
154 const sal_Int32 nItemWidth, bool bSideBar)
156 const OUString sCommandName (GetItemCommand(nItemId));
158 uno::Reference<frame::XToolbarController> xController(sfx2::sidebar::ControllerFactory::CreateToolBoxController(
159 this, nItemId, sCommandName, rxFrame, rxFrame->getController(),
160 VCLUnoHelper::GetInterface(this), nItemWidth, bSideBar));
162 if (xController.is())
163 maControllers.insert(std::make_pair(nItemId, xController));
166 Reference<frame::XToolbarController> SidebarToolBox::GetControllerForItemId (const ToolBoxItemId nItemId) const
168 ControllerContainer::const_iterator iController (maControllers.find(nItemId));
169 if (iController != maControllers.end())
170 return iController->second;
172 return Reference<frame::XToolbarController>();
175 void SidebarToolBox::RegisterHandlers()
177 if ( ! mbAreHandlersRegistered)
179 mbAreHandlersRegistered = true;
180 SetDropdownClickHdl(LINK(this, SidebarToolBox, DropDownClickHandler));
181 SetClickHdl(LINK(this, SidebarToolBox, ClickHandler));
182 SetDoubleClickHdl(LINK(this, SidebarToolBox, DoubleClickHandler));
183 SetSelectHdl(LINK(this, SidebarToolBox, SelectHandler));
187 IMPL_LINK(SidebarToolBox, DropDownClickHandler, ToolBox*, pToolBox, void)
189 if (pToolBox != nullptr)
191 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
192 if (xController.is())
194 Reference<awt::XWindow> xWindow = xController->createPopupWindow();
195 if (xWindow.is() )
196 xWindow->setFocus();
201 IMPL_LINK(SidebarToolBox, ClickHandler, ToolBox*, pToolBox, void)
203 if (pToolBox == nullptr)
204 return;
206 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
207 if (xController.is())
208 xController->click();
211 IMPL_LINK(SidebarToolBox, DoubleClickHandler, ToolBox*, pToolBox, void)
213 if (pToolBox == nullptr)
214 return;
216 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
217 if (xController.is())
218 xController->doubleClick();
221 IMPL_LINK(SidebarToolBox, SelectHandler, ToolBox*, pToolBox, void)
223 if (pToolBox == nullptr)
224 return;
226 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
227 if (xController.is())
228 xController->execute(static_cast<sal_Int16>(pToolBox->GetModifier()));
231 IMPL_LINK_NOARG(SidebarToolBox, ChangedIconSizeHandler, LinkParamNone*, void)
233 SolarMutexGuard g;
235 if (mbUseDefaultButtonSize)
236 SetToolboxButtonSize(GetDefaultButtonSize());
238 for (auto const& it : maControllers)
240 Reference<frame::XSubToolbarController> xController(it.second, UNO_QUERY);
241 if (xController.is() && xController->opensSubToolbar())
243 // The button should show the last function that was selected from the
244 // dropdown. The controller should know better than us what it was.
245 xController->updateImage();
247 else if (SfxViewFrame::Current())
249 OUString aCommandURL = GetItemCommand(it.first);
250 css::uno::Reference<frame::XFrame> xFrame = SfxViewFrame::Current()->GetFrame().GetFrameInterface();
251 Image aImage = vcl::CommandInfoProvider::GetImageForCommand(aCommandURL, xFrame, GetImageSize());
252 SetItemImage(it.first, aImage);
256 Resize();
257 queue_resize();
260 void SidebarToolBox::InitToolBox(VclBuilder::stringmap& rMap)
262 for (const auto& it : rMap)
264 if (it.first == "toolbar-style")
266 if (it.second == "text")
267 SetButtonType(ButtonType::TEXT);
268 else if (it.second == "both-horiz")
269 SetButtonType(ButtonType::SYMBOLTEXT);
270 else if (it.second == "both")
272 SetButtonType(ButtonType::SYMBOLTEXT);
273 SetToolBoxTextPosition(ToolBoxTextPosition::Bottom);
276 else if (it.first == "icon-size")
278 mbUseDefaultButtonSize = false;
279 if (it.second == "1" || it.second == "2" || it.second == "4")
280 SetToolboxButtonSize(ToolBoxButtonSize::Small);
281 else if (it.second == "3")
282 SetToolboxButtonSize(ToolBoxButtonSize::Large);
283 else if (it.second == "5")
284 SetToolboxButtonSize(ToolBoxButtonSize::Size32);
286 else if (it.first == "orientation" && it.second == "vertical")
287 SetAlign(WindowAlign::Left);
291 namespace {
293 class NotebookbarToolBox : public SidebarToolBox
295 public:
296 explicit NotebookbarToolBox(vcl::Window* pParentWindow)
297 : SidebarToolBox(pParentWindow)
299 mbSideBar = false;
300 SetToolboxButtonSize(GetDefaultButtonSize());
303 virtual ToolBoxButtonSize GetDefaultButtonSize() const override
305 return static_cast<ToolBoxButtonSize>(officecfg::Office::Common::Misc::NotebookbarIconSize::get());
311 extern "C" SAL_DLLPUBLIC_EXPORT void makeNotebookbarToolBox(VclPtr<vcl::Window> & rRet, const VclPtr<vcl::Window> & pParent, VclBuilder::stringmap & rMap)
313 static_assert(std::is_same_v<std::remove_pointer_t<VclBuilder::customMakeWidget>,
314 decltype(makeNotebookbarToolBox)>);
315 VclPtrInstance<NotebookbarToolBox> pBox(pParent);
316 pBox->InitToolBox(rMap);
317 rRet = pBox;
320 } // end of namespace sfx2::sidebar
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */