LibreOffice Module sfx2 (master) 1
ControllerFactory.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
21#include <sidebar/Tools.hxx>
22
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>
27
30#include <vcl/toolbox.hxx>
32#include <vcl/weldutils.hxx>
35
36using namespace css;
37using namespace css::uno;
38
39namespace sfx2::sidebar {
40
41Reference<lang::XComponent> ControllerFactory::CreateImageController(
42 const Reference<frame::XFrame>& rxFrame,
43 const Reference<awt::XWindow>& rxParentWindow)
44{
46 comphelper::getProcessComponentContext(), rxFrame, rxParentWindow,
48
49 xController->update();
50 return xController;
51}
52
53Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
54 ToolBox* pToolBox,
55 const ToolBoxItemId nItemId,
56 const OUString& rsCommandName,
57 const Reference<frame::XFrame>& rxFrame,
58 const Reference<frame::XController>& rxController,
59 const Reference<awt::XWindow>& rxParentWindow,
60 const sal_Int32 nWidth, bool bSideBar)
61{
62 Reference<frame::XToolbarController> xController (
65 rsCommandName,
66 rxFrame, rxController,
67 nWidth, bSideBar));
68
69 bool bFactoryHasController( xController.is() );
70
71 // Create a controller for the new item.
72 if ( !bFactoryHasController )
73 {
74 xController = ::framework::CreateToolBoxController(
75 rxFrame,
76 pToolBox,
77 nItemId,
78 rsCommandName);
79 }
80 if ( ! xController.is())
81 {
83 ::comphelper::getProcessComponentContext(),
84 rxFrame,
85 pToolBox,
86 nItemId,
87 rsCommandName);
88 }
89
90 // Initialize the controller with eg a service factory.
91 Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY);
92 if (!bFactoryHasController && xInitialization.is())
93 {
94 beans::PropertyValue aPropValue;
95 std::vector<Any> aPropertyVector;
96
97 aPropValue.Name = "Frame";
98 aPropValue.Value <<= rxFrame;
99 aPropertyVector.push_back(Any(aPropValue));
100
101 aPropValue.Name = "ServiceManager";
102 aPropValue.Value <<= ::comphelper::getProcessServiceFactory();
103 aPropertyVector.push_back(Any(aPropValue));
104
105 aPropValue.Name = "CommandURL";
106 aPropValue.Value <<= rsCommandName;
107 aPropertyVector.push_back(Any(aPropValue));
108
109 Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
110 xInitialization->initialize(aArgs);
111 }
112
113 if (xController.is())
114 {
115 if (rxParentWindow.is())
116 {
117 Reference<awt::XWindow> xItemWindow (xController->createItemWindow(rxParentWindow));
118 VclPtr<vcl::Window> pItemWindow = VCLUnoHelper::GetWindow(xItemWindow);
119 if (pItemWindow != nullptr)
120 {
121 WindowType nType = pItemWindow->GetType();
122 if (nType == WindowType::LISTBOX || nType == WindowType::MULTILISTBOX || nType == WindowType::COMBOBOX)
123 pItemWindow->SetAccessibleName(pToolBox->GetItemText(nItemId));
124 if (nWidth > 0)
125 pItemWindow->SetSizePixel(Size(nWidth, pItemWindow->GetSizePixel().Height()));
126 pToolBox->SetItemWindow(nItemId, pItemWindow);
127 }
128 }
129
130 Reference<util::XUpdatable> xUpdatable (xController, UNO_QUERY);
131 if (xUpdatable.is())
132 xUpdatable->update();
133
134 // Add tooltip.
135 if (xController.is())
136 {
139 const OUString sTooltip (vcl::CommandInfoProvider::GetTooltipForCommand(
140 rsCommandName, aProperties, rxFrame));
141 if (pToolBox->GetQuickHelpText(nItemId).isEmpty())
142 pToolBox->SetQuickHelpText(nItemId, sTooltip);
143 pToolBox->EnableItem(nItemId);
144 }
145 }
146
147 return xController;
148}
149
150Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
151 weld::Toolbar& rToolbar, weld::Builder& rBuilder,
152 const OUString& rsCommandName,
153 const Reference<frame::XFrame>& rxFrame,
154 const Reference<frame::XController>& rxController,
155 bool bSideBar)
156{
157 css::uno::Reference<css::awt::XWindow> xWidget(new weld::TransportAsXWindow(&rToolbar, &rBuilder));
158
159 Reference<frame::XToolbarController> xController(
161 xWidget,
162 rsCommandName,
163 rxFrame, rxController,
164 -1, bSideBar));
165
166 if (!xController.is())
167 {
169 ::comphelper::getProcessComponentContext(),
170 rxFrame,
171 rToolbar,
172 rsCommandName);
173 }
174
175 if (xController.is())
176 {
177 xController->createItemWindow(xWidget);
178
179 Reference<util::XUpdatable> xUpdatable(xController, UNO_QUERY);
180 if (xUpdatable.is())
181 xUpdatable->update();
182 }
183
184 return xController;
185}
186
187
188Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController(
189 const Reference<awt::XWindow>& rxToolbar,
190 const OUString& rsCommandName,
191 const Reference<frame::XFrame>& rxFrame,
192 const Reference<frame::XController>& rxController,
193 const sal_Int32 nWidth, bool bSideBar)
194{
195 try
196 {
197 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
198 Reference<frame::XUIControllerFactory> xFactory = frame::theToolbarControllerFactory::get( xContext );
199 OUString sModuleName (Tools::GetModuleName(rxController));
200
201 if (xFactory.is() && xFactory->hasController(rsCommandName, sModuleName))
202 {
203 beans::PropertyValue aPropValue;
204 std::vector<Any> aPropertyVector;
205
206 aPropValue.Name = "ModuleIdentifier";
207 aPropValue.Value <<= sModuleName;
208 aPropertyVector.push_back( Any( aPropValue ));
209
210 aPropValue.Name = "Frame";
211 aPropValue.Value <<= rxFrame;
212 aPropertyVector.push_back( Any( aPropValue ));
213
214 aPropValue.Name = "ServiceManager";
215 aPropValue.Value <<= comphelper::getProcessServiceFactory();
216 aPropertyVector.push_back( Any( aPropValue ));
217
218 aPropValue.Name = "ParentWindow";
219 aPropValue.Value <<= rxToolbar;
220 aPropertyVector.push_back( Any( aPropValue ));
221
222 aPropValue.Name = "IsSidebar";
223 aPropValue.Value <<= bSideBar;
224 aPropertyVector.push_back( Any( aPropValue ));
225
226 if (nWidth > 0)
227 {
228 aPropValue.Name = "Width";
229 aPropValue.Value <<= nWidth;
230 aPropertyVector.push_back( Any( aPropValue ));
231 }
232
233 Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
234 return Reference<frame::XToolbarController>(
235 xFactory->createInstanceWithArgumentsAndContext(
236 rsCommandName,
237 aArgs,
238 xContext),
239 UNO_QUERY);
240 }
241 }
242 catch (Exception&)
243 {
244 // Ignore exception.
245 }
246 return nullptr;
247}
248
249} // end of namespace sfx2::sidebar
250
251/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
OUString GetQuickHelpText(ToolBoxItemId nItemId) const
void SetQuickHelpText(ToolBoxItemId nItemId, const OUString &rText)
void EnableItem(ToolBoxItemId nItemId, bool bEnable=true)
void SetItemWindow(ToolBoxItemId nItemId, vcl::Window *pNewWindow)
const OUString & GetItemText(ToolBoxItemId nItemId) const
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
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::frame::XToolbarController > CreateToolBarController(const css::uno::Reference< css::awt::XWindow > &rToolbar, const OUString &rsCommandName, const css::uno::Reference< css::frame::XFrame > &rxFrame, const css::uno::Reference< css::frame::XController > &rxController, const sal_Int32 nWidth, 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)
static OUString GetModuleName(const css::uno::Reference< css::frame::XController > &rxFrame)
Definition: Tools.cxx:92
Reference< XSingleServiceFactory > xFactory
@ Exception
Reference< XMultiServiceFactory > getProcessServiceFactory()
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Reference< XComponentContext > getProcessComponentContext()
Sequence< beans::PropertyValue > GetCommandProperties(const OUString &rsCommandName, const OUString &rsModuleName)
OUString GetTooltipForCommand(const OUString &rsCommandName, const css::uno::Sequence< css::beans::PropertyValue > &rProperties, const Reference< frame::XFrame > &rxFrame)
OUString GetModuleIdentifier(const Reference< frame::XFrame > &rxFrame)
QPRO_FUNC_TYPE nType
Reference< XController > xController
WindowType