Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / sidebar / ControllerFactory.cxx
blobc635751e680902a645f7b3a0980feaf5c66365b1
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 <sfx2/sidebar/ControllerFactory.hxx>
21 #include <sfx2/sidebar/CommandInfoProvider.hxx>
22 #include <sfx2/sidebar/Tools.hxx>
24 #include <com/sun/star/frame/XToolbarController.hpp>
25 #include <com/sun/star/frame/XFrame.hpp>
26 #include <com/sun/star/frame/theToolbarControllerFactory.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <framework/sfxhelperfunctions.hxx>
30 #include <svtools/generictoolboxcontroller.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <toolkit/helper/vclunohelper.hxx>
34 using namespace css;
35 using namespace css::uno;
36 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 = "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 vcl::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;
136 Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController(
137 ToolBox* pToolBox,
138 const OUString& rsCommandName,
139 const Reference<frame::XFrame>& rxFrame,
140 const sal_Int32 nWidth)
144 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
145 Reference<frame::XUIControllerFactory> xFactory = frame::theToolbarControllerFactory::get( xContext );
146 OUString sModuleName (Tools::GetModuleName(rxFrame));
148 if (xFactory.is() && xFactory->hasController(rsCommandName, sModuleName))
150 beans::PropertyValue aPropValue;
151 std::vector<Any> aPropertyVector;
153 aPropValue.Name = "ModuleIdentifier";
154 aPropValue.Value <<= sModuleName;
155 aPropertyVector.push_back( makeAny( aPropValue ));
157 aPropValue.Name = "Frame";
158 aPropValue.Value <<= rxFrame;
159 aPropertyVector.push_back( makeAny( aPropValue ));
161 aPropValue.Name = "ServiceManager";
162 aPropValue.Value <<= comphelper::getProcessServiceFactory();
163 aPropertyVector.push_back( makeAny( aPropValue ));
165 aPropValue.Name = "ParentWindow";
166 aPropValue.Value <<= VCLUnoHelper::GetInterface(pToolBox);
167 aPropertyVector.push_back( makeAny( aPropValue ));
169 if (nWidth > 0)
171 aPropValue.Name = "Width";
172 aPropValue.Value <<= nWidth;
173 aPropertyVector.push_back( makeAny( aPropValue ));
176 Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
177 return Reference<frame::XToolbarController>(
178 xFactory->createInstanceWithArgumentsAndContext(
179 rsCommandName,
180 aArgs,
181 xContext),
182 UNO_QUERY);
185 catch (Exception&)
187 // Ignore exception.
189 return NULL;
192 } } // end of namespace sfx2::sidebar
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */