bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / sidebar / ControllerFactory.cxx
blobcc5fe34f09eba90b172c26b02a69d80aa1dd38ac
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "sfx2/sidebar/ControllerFactory.hxx"
20 #include "sfx2/sidebar/CommandInfoProvider.hxx"
21 #include "sfx2/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/XUIControllerFactory.hpp>
27 #include <framework/sfxhelperfunctions.hxx>
28 #include <svtools/generictoolboxcontroller.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <toolkit/helper/vclunohelper.hxx>
33 using namespace css;
34 using namespace cssu;
35 using ::rtl::OUString;
38 namespace sfx2 { namespace sidebar {
40 Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
41 ToolBox* pToolBox,
42 const sal_uInt16 nItemId,
43 const OUString& rsCommandName,
44 const Reference<frame::XFrame>& rxFrame,
45 const Reference<awt::XWindow>& rxParentWindow,
46 const sal_Int32 nWidth)
48 Reference<frame::XToolbarController> xController (
49 CreateToolBarController(
50 pToolBox,
51 rsCommandName,
52 rxFrame,
53 nWidth));
55 // Create a controller for the new item.
56 if ( ! xController.is())
58 xController.set(
59 static_cast<XWeak*>(::framework::CreateToolBoxController(
60 rxFrame,
61 pToolBox,
62 nItemId,
63 rsCommandName)),
64 UNO_QUERY);
66 if ( ! xController.is())
68 xController.set(
69 static_cast<XWeak*>(new svt::GenericToolboxController(
70 ::comphelper::getProcessComponentContext(),
71 rxFrame,
72 pToolBox,
73 nItemId,
74 rsCommandName)),
75 UNO_QUERY);
78 // Initialize the controller with eg a service factory.
79 Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY);
80 if (xInitialization.is())
82 beans::PropertyValue aPropValue;
83 std::vector<Any> aPropertyVector;
85 aPropValue.Name = A2S("Frame");
86 aPropValue.Value <<= rxFrame;
87 aPropertyVector.push_back(makeAny(aPropValue));
89 aPropValue.Name = A2S("ServiceManager");
90 aPropValue.Value <<= ::comphelper::getProcessServiceFactory();
91 aPropertyVector.push_back(makeAny(aPropValue));
93 aPropValue.Name = A2S("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 Window* pItemWindow = VCLUnoHelper::GetWindow(xItemWindow);
107 if (pItemWindow != NULL)
109 WindowType nType = pItemWindow->GetType();
110 if (nType == WINDOW_LISTBOX || nType == WINDOW_MULTILISTBOX || nType == WINDOW_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 label.
123 if (xController.is())
125 const OUString sLabel (sfx2::sidebar::CommandInfoProvider::Instance().GetLabelForCommand(
126 rsCommandName,
127 rxFrame));
128 pToolBox->SetQuickHelpText(nItemId, sLabel);
129 pToolBox->EnableItem(nItemId);
133 return xController;
139 Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController(
140 ToolBox* pToolBox,
141 const OUString& rsCommandName,
142 const Reference<frame::XFrame>& rxFrame,
143 const sal_Int32 nWidth)
147 Reference<frame::XUIControllerFactory> xFactory (
148 comphelper::getProcessServiceFactory()->createInstance(A2S("com.sun.star.frame.ToolbarControllerFactory")),
149 UNO_QUERY);
150 OUString sModuleName (Tools::GetModuleName(rxFrame));
152 if (xFactory.is() && xFactory->hasController(rsCommandName, sModuleName))
154 beans::PropertyValue aPropValue;
155 std::vector<Any> aPropertyVector;
157 aPropValue.Name = A2S("ModuleIdentifier");
158 aPropValue.Value <<= sModuleName;
159 aPropertyVector.push_back( makeAny( aPropValue ));
161 aPropValue.Name = A2S("Frame");
162 aPropValue.Value <<= rxFrame;
163 aPropertyVector.push_back( makeAny( aPropValue ));
165 aPropValue.Name = A2S("ServiceManager");
166 aPropValue.Value <<= comphelper::getProcessServiceFactory();
167 aPropertyVector.push_back( makeAny( aPropValue ));
169 aPropValue.Name = A2S("ParentWindow");
170 aPropValue.Value <<= VCLUnoHelper::GetInterface(pToolBox);
171 aPropertyVector.push_back( makeAny( aPropValue ));
173 if (nWidth > 0)
175 aPropValue.Name = A2S("Width");
176 aPropValue.Value <<= nWidth;
177 aPropertyVector.push_back( makeAny( aPropValue ));
180 Reference<beans::XPropertySet> xFactoryProperties (comphelper::getProcessServiceFactory(), UNO_QUERY);
181 Reference<XComponentContext > xComponentContext;
182 if (xFactoryProperties.is())
183 xFactoryProperties->getPropertyValue(A2S("DefaultContext")) >>= xComponentContext;
185 Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
186 return Reference<frame::XToolbarController>(
187 xFactory->createInstanceWithArgumentsAndContext(
188 rsCommandName,
189 aArgs,
190 xComponentContext),
191 UNO_QUERY);
194 catch (Exception& rException)
196 // Ignore exception.
198 return NULL;
201 } } // end of namespace sfx2::sidebar