LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / sidebar / ControllerFactory.cxx
blob1a4fcd51a9b52844aad19c9e83e52eeeb136b75a
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/ControllerFactory.hxx>
21 #include <sidebar/Tools.hxx>
23 #include <com/sun/star/frame/XToolbarController.hpp>
24 #include <com/sun/star/frame/XFrame.hpp>
25 #include <com/sun/star/frame/theToolbarControllerFactory.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <framework/sfxhelperfunctions.hxx>
29 #include <framework/generictoolbarcontroller.hxx>
30 #include <vcl/toolbox.hxx>
31 #include <vcl/commandinfoprovider.hxx>
32 #include <vcl/weldutils.hxx>
33 #include <comphelper/processfactory.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
36 using namespace css;
37 using namespace css::uno;
39 namespace sfx2::sidebar {
41 Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
42 ToolBox* pToolBox,
43 const ToolBoxItemId nItemId,
44 const OUString& rsCommandName,
45 const Reference<frame::XFrame>& rxFrame,
46 const Reference<frame::XController>& rxController,
47 const Reference<awt::XWindow>& rxParentWindow,
48 const sal_Int32 nWidth, bool bSideBar)
50 Reference<frame::XToolbarController> xController (
51 CreateToolBarController(
52 VCLUnoHelper::GetInterface(pToolBox),
53 rsCommandName,
54 rxFrame, rxController,
55 nWidth, bSideBar));
57 bool bFactoryHasController( xController.is() );
59 // Create a controller for the new item.
60 if ( !bFactoryHasController )
62 xController = ::framework::CreateToolBoxController(
63 rxFrame,
64 pToolBox,
65 nItemId,
66 rsCommandName);
68 if ( ! xController.is())
70 xController = new framework::GenericToolbarController(
71 ::comphelper::getProcessComponentContext(),
72 rxFrame,
73 pToolBox,
74 nItemId,
75 rsCommandName);
78 // Initialize the controller with eg a service factory.
79 Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY);
80 if (!bFactoryHasController && xInitialization.is())
82 beans::PropertyValue aPropValue;
83 std::vector<Any> aPropertyVector;
85 aPropValue.Name = "Frame";
86 aPropValue.Value <<= rxFrame;
87 aPropertyVector.push_back(makeAny(aPropValue));
89 aPropValue.Name = "ServiceManager";
90 aPropValue.Value <<= ::comphelper::getProcessServiceFactory();
91 aPropertyVector.push_back(makeAny(aPropValue));
93 aPropValue.Name = "CommandURL";
94 aPropValue.Value <<= rsCommandName;
95 aPropertyVector.push_back(makeAny(aPropValue));
97 Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
98 xInitialization->initialize(aArgs);
101 if (xController.is())
103 if (rxParentWindow.is())
105 Reference<awt::XWindow> xItemWindow (xController->createItemWindow(rxParentWindow));
106 VclPtr<vcl::Window> pItemWindow = VCLUnoHelper::GetWindow(xItemWindow);
107 if (pItemWindow != nullptr)
109 WindowType nType = pItemWindow->GetType();
110 if (nType == WindowType::LISTBOX || nType == WindowType::MULTILISTBOX || nType == WindowType::COMBOBOX)
111 pItemWindow->SetAccessibleName(pToolBox->GetItemText(nItemId));
112 if (nWidth > 0)
113 pItemWindow->SetSizePixel(Size(nWidth, pItemWindow->GetSizePixel().Height()));
114 pToolBox->SetItemWindow(nItemId, pItemWindow);
118 Reference<util::XUpdatable> xUpdatable (xController, UNO_QUERY);
119 if (xUpdatable.is())
120 xUpdatable->update();
122 // Add tooltip.
123 if (xController.is())
125 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(rsCommandName,
126 vcl::CommandInfoProvider::GetModuleIdentifier(rxFrame));
127 const OUString sTooltip (vcl::CommandInfoProvider::GetTooltipForCommand(
128 rsCommandName, aProperties, rxFrame));
129 if (pToolBox->GetQuickHelpText(nItemId).isEmpty())
130 pToolBox->SetQuickHelpText(nItemId, sTooltip);
131 pToolBox->EnableItem(nItemId);
135 return xController;
138 Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
139 weld::Toolbar& rToolbar, weld::Builder& rBuilder,
140 const OUString& rsCommandName,
141 const Reference<frame::XFrame>& rxFrame,
142 const Reference<frame::XController>& rxController,
143 bool bSideBar)
145 css::uno::Reference<css::awt::XWindow> xWidget(new weld::TransportAsXWindow(&rToolbar, &rBuilder));
147 Reference<frame::XToolbarController> xController(
148 CreateToolBarController(
149 xWidget,
150 rsCommandName,
151 rxFrame, rxController,
152 -1, bSideBar));
154 if (!xController.is())
156 xController = new framework::GenericToolbarController(
157 ::comphelper::getProcessComponentContext(),
158 rxFrame,
159 rToolbar,
160 rsCommandName);
163 if (xController.is())
165 xController->createItemWindow(xWidget);
167 Reference<util::XUpdatable> xUpdatable(xController, UNO_QUERY);
168 if (xUpdatable.is())
169 xUpdatable->update();
172 return xController;
176 Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController(
177 const Reference<awt::XWindow>& rxToolbar,
178 const OUString& rsCommandName,
179 const Reference<frame::XFrame>& rxFrame,
180 const Reference<frame::XController>& rxController,
181 const sal_Int32 nWidth, bool bSideBar)
185 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
186 Reference<frame::XUIControllerFactory> xFactory = frame::theToolbarControllerFactory::get( xContext );
187 OUString sModuleName (Tools::GetModuleName(rxController));
189 if (xFactory.is() && xFactory->hasController(rsCommandName, sModuleName))
191 beans::PropertyValue aPropValue;
192 std::vector<Any> aPropertyVector;
194 aPropValue.Name = "ModuleIdentifier";
195 aPropValue.Value <<= sModuleName;
196 aPropertyVector.push_back( makeAny( aPropValue ));
198 aPropValue.Name = "Frame";
199 aPropValue.Value <<= rxFrame;
200 aPropertyVector.push_back( makeAny( aPropValue ));
202 aPropValue.Name = "ServiceManager";
203 aPropValue.Value <<= comphelper::getProcessServiceFactory();
204 aPropertyVector.push_back( makeAny( aPropValue ));
206 aPropValue.Name = "ParentWindow";
207 aPropValue.Value <<= rxToolbar;
208 aPropertyVector.push_back( makeAny( aPropValue ));
210 aPropValue.Name = "IsSidebar";
211 aPropValue.Value <<= bSideBar;
212 aPropertyVector.push_back( makeAny( aPropValue ));
214 if (nWidth > 0)
216 aPropValue.Name = "Width";
217 aPropValue.Value <<= nWidth;
218 aPropertyVector.push_back( makeAny( aPropValue ));
221 Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
222 return Reference<frame::XToolbarController>(
223 xFactory->createInstanceWithArgumentsAndContext(
224 rsCommandName,
225 aArgs,
226 xContext),
227 UNO_QUERY);
230 catch (Exception&)
232 // Ignore exception.
234 return nullptr;
237 } // end of namespace sfx2::sidebar
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */