Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / sidebar / ControllerFactory.cxx
blob24bb15c3ec98350eb3c8bb00bea8e3f54322e21e
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 sal_uInt16 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.set(
63 static_cast<XWeak*>(::framework::CreateToolBoxController(
64 rxFrame,
65 pToolBox,
66 nItemId,
67 rsCommandName)),
68 UNO_QUERY);
70 if ( ! xController.is())
72 xController.set(
73 static_cast<XWeak*>(new framework::GenericToolbarController(
74 ::comphelper::getProcessComponentContext(),
75 rxFrame,
76 pToolBox,
77 nItemId,
78 rsCommandName)),
79 UNO_QUERY);
82 // Initialize the controller with eg a service factory.
83 Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY);
84 if (!bFactoryHasController && xInitialization.is())
86 beans::PropertyValue aPropValue;
87 std::vector<Any> aPropertyVector;
89 aPropValue.Name = "Frame";
90 aPropValue.Value <<= rxFrame;
91 aPropertyVector.push_back(makeAny(aPropValue));
93 aPropValue.Name = "ServiceManager";
94 aPropValue.Value <<= ::comphelper::getProcessServiceFactory();
95 aPropertyVector.push_back(makeAny(aPropValue));
97 aPropValue.Name = "CommandURL";
98 aPropValue.Value <<= rsCommandName;
99 aPropertyVector.push_back(makeAny(aPropValue));
101 Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
102 xInitialization->initialize(aArgs);
105 if (xController.is())
107 if (rxParentWindow.is())
109 Reference<awt::XWindow> xItemWindow (xController->createItemWindow(rxParentWindow));
110 VclPtr<vcl::Window> pItemWindow = VCLUnoHelper::GetWindow(xItemWindow);
111 if (pItemWindow != nullptr)
113 WindowType nType = pItemWindow->GetType();
114 if (nType == WindowType::LISTBOX || nType == WindowType::MULTILISTBOX || nType == WindowType::COMBOBOX)
115 pItemWindow->SetAccessibleName(pToolBox->GetItemText(nItemId));
116 if (nWidth > 0)
117 pItemWindow->SetSizePixel(Size(nWidth, pItemWindow->GetSizePixel().Height()));
118 pToolBox->SetItemWindow(nItemId, pItemWindow);
122 Reference<util::XUpdatable> xUpdatable (xController, UNO_QUERY);
123 if (xUpdatable.is())
124 xUpdatable->update();
126 // Add tooltip.
127 if (xController.is())
129 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(rsCommandName,
130 vcl::CommandInfoProvider::GetModuleIdentifier(rxFrame));
131 const OUString sTooltip (vcl::CommandInfoProvider::GetTooltipForCommand(
132 rsCommandName, aProperties, rxFrame));
133 if (pToolBox->GetQuickHelpText(nItemId).isEmpty())
134 pToolBox->SetQuickHelpText(nItemId, sTooltip);
135 pToolBox->EnableItem(nItemId);
139 return xController;
142 Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
143 weld::Toolbar& rToolbar, weld::Builder& rBuilder,
144 const OUString& rsCommandName,
145 const Reference<frame::XFrame>& rxFrame,
146 const Reference<frame::XController>& rxController,
147 bool bSideBar)
149 css::uno::Reference<css::awt::XWindow> xWidget(new weld::TransportAsXWindow(&rToolbar, &rBuilder));
151 Reference<frame::XToolbarController> xController(
152 CreateToolBarController(
153 xWidget,
154 rsCommandName,
155 rxFrame, rxController,
156 -1, bSideBar));
158 if (!xController.is())
160 xController.set(
161 static_cast<XWeak*>(new framework::GenericToolbarController(
162 ::comphelper::getProcessComponentContext(),
163 rxFrame,
164 rToolbar,
165 rsCommandName)),
166 UNO_QUERY);
169 if (xController.is())
171 xController->createItemWindow(xWidget);
173 Reference<util::XUpdatable> xUpdatable(xController, UNO_QUERY);
174 if (xUpdatable.is())
175 xUpdatable->update();
178 return xController;
182 Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController(
183 const Reference<awt::XWindow>& rxToolbar,
184 const OUString& rsCommandName,
185 const Reference<frame::XFrame>& rxFrame,
186 const Reference<frame::XController>& rxController,
187 const sal_Int32 nWidth, bool bSideBar)
191 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
192 Reference<frame::XUIControllerFactory> xFactory = frame::theToolbarControllerFactory::get( xContext );
193 OUString sModuleName (Tools::GetModuleName(rxController));
195 if (xFactory.is() && xFactory->hasController(rsCommandName, sModuleName))
197 beans::PropertyValue aPropValue;
198 std::vector<Any> aPropertyVector;
200 aPropValue.Name = "ModuleIdentifier";
201 aPropValue.Value <<= sModuleName;
202 aPropertyVector.push_back( makeAny( aPropValue ));
204 aPropValue.Name = "Frame";
205 aPropValue.Value <<= rxFrame;
206 aPropertyVector.push_back( makeAny( aPropValue ));
208 aPropValue.Name = "ServiceManager";
209 aPropValue.Value <<= comphelper::getProcessServiceFactory();
210 aPropertyVector.push_back( makeAny( aPropValue ));
212 aPropValue.Name = "ParentWindow";
213 aPropValue.Value <<= rxToolbar;
214 aPropertyVector.push_back( makeAny( aPropValue ));
216 aPropValue.Name = "IsSidebar";
217 aPropValue.Value <<= bSideBar;
218 aPropertyVector.push_back( makeAny( aPropValue ));
220 if (nWidth > 0)
222 aPropValue.Name = "Width";
223 aPropValue.Value <<= nWidth;
224 aPropertyVector.push_back( makeAny( aPropValue ));
227 Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
228 return Reference<frame::XToolbarController>(
229 xFactory->createInstanceWithArgumentsAndContext(
230 rsCommandName,
231 aArgs,
232 xContext),
233 UNO_QUERY);
236 catch (Exception&)
238 // Ignore exception.
240 return nullptr;
243 } // end of namespace sfx2::sidebar
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */