Line data Source code
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 :
20 : #include <sfx2/sidebar/ControllerFactory.hxx>
21 : #include <sfx2/sidebar/CommandInfoProvider.hxx>
22 : #include <sfx2/sidebar/Tools.hxx>
23 :
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>
28 :
29 : #include <framework/sfxhelperfunctions.hxx>
30 : #include <svtools/generictoolboxcontroller.hxx>
31 : #include <comphelper/processfactory.hxx>
32 : #include <toolkit/helper/vclunohelper.hxx>
33 :
34 : using namespace css;
35 : using namespace css::uno;
36 : using ::rtl::OUString;
37 :
38 : namespace sfx2 { namespace sidebar {
39 :
40 17881 : 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)
47 : {
48 : Reference<frame::XToolbarController> xController (
49 : CreateToolBarController(
50 : pToolBox,
51 : rsCommandName,
52 : rxFrame,
53 17881 : nWidth));
54 :
55 : // Create a controller for the new item.
56 17881 : if ( ! xController.is())
57 : {
58 : xController.set(
59 : static_cast<XWeak*>(::framework::CreateToolBoxController(
60 : rxFrame,
61 : pToolBox,
62 : nItemId,
63 16716 : rsCommandName)),
64 16716 : UNO_QUERY);
65 : }
66 17881 : if ( ! xController.is())
67 : {
68 : xController.set(
69 : static_cast<XWeak*>(new svt::GenericToolboxController(
70 : ::comphelper::getProcessComponentContext(),
71 : rxFrame,
72 : pToolBox,
73 : nItemId,
74 13056 : rsCommandName)),
75 26112 : UNO_QUERY);
76 : }
77 :
78 : // Initialize the controller with eg a service factory.
79 35762 : Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY);
80 17881 : if (xInitialization.is())
81 : {
82 17881 : beans::PropertyValue aPropValue;
83 35762 : std::vector<Any> aPropertyVector;
84 :
85 17881 : aPropValue.Name = "Frame";
86 17881 : aPropValue.Value <<= rxFrame;
87 17881 : aPropertyVector.push_back(makeAny(aPropValue));
88 :
89 17881 : aPropValue.Name = "ServiceManager";
90 17881 : aPropValue.Value <<= ::comphelper::getProcessServiceFactory();
91 17881 : aPropertyVector.push_back(makeAny(aPropValue));
92 :
93 17881 : aPropValue.Name = "CommandURL";
94 17881 : aPropValue.Value <<= rsCommandName;
95 17881 : aPropertyVector.push_back(makeAny(aPropValue));
96 :
97 35762 : Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
98 35762 : xInitialization->initialize(aArgs);
99 : }
100 :
101 17881 : if (xController.is())
102 : {
103 17881 : if (rxParentWindow.is())
104 : {
105 17881 : Reference<awt::XWindow> xItemWindow (xController->createItemWindow(rxParentWindow));
106 17881 : vcl::Window* pItemWindow = VCLUnoHelper::GetWindow(xItemWindow);
107 17881 : if (pItemWindow != NULL)
108 : {
109 978 : WindowType nType = pItemWindow->GetType();
110 978 : if (nType == WINDOW_LISTBOX || nType == WINDOW_MULTILISTBOX || nType == WINDOW_COMBOBOX)
111 978 : pItemWindow->SetAccessibleName(pToolBox->GetItemText(nItemId));
112 978 : if (nWidth > 0)
113 0 : pItemWindow->SetSizePixel(Size(nWidth, pItemWindow->GetSizePixel().Height()));
114 978 : pToolBox->SetItemWindow(nItemId, pItemWindow);
115 17881 : }
116 : }
117 :
118 17881 : Reference<util::XUpdatable> xUpdatable (xController, UNO_QUERY);
119 17881 : if (xUpdatable.is())
120 17881 : xUpdatable->update();
121 :
122 : // Add label.
123 17881 : if (xController.is())
124 : {
125 17881 : const OUString sLabel (sfx2::sidebar::CommandInfoProvider::Instance().GetLabelForCommand(
126 : rsCommandName,
127 17881 : rxFrame));
128 17881 : pToolBox->SetQuickHelpText(nItemId, sLabel);
129 17881 : pToolBox->EnableItem(nItemId);
130 17881 : }
131 : }
132 :
133 35762 : return xController;
134 : }
135 :
136 17881 : Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController(
137 : ToolBox* pToolBox,
138 : const OUString& rsCommandName,
139 : const Reference<frame::XFrame>& rxFrame,
140 : const sal_Int32 nWidth)
141 : {
142 : try
143 : {
144 17881 : Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
145 34597 : Reference<frame::XUIControllerFactory> xFactory = frame::theToolbarControllerFactory::get( xContext );
146 34597 : OUString sModuleName (Tools::GetModuleName(rxFrame));
147 :
148 17881 : if (xFactory.is() && xFactory->hasController(rsCommandName, sModuleName))
149 : {
150 1165 : beans::PropertyValue aPropValue;
151 2330 : std::vector<Any> aPropertyVector;
152 :
153 1165 : aPropValue.Name = "ModuleIdentifier";
154 1165 : aPropValue.Value <<= sModuleName;
155 1165 : aPropertyVector.push_back( makeAny( aPropValue ));
156 :
157 1165 : aPropValue.Name = "Frame";
158 1165 : aPropValue.Value <<= rxFrame;
159 1165 : aPropertyVector.push_back( makeAny( aPropValue ));
160 :
161 1165 : aPropValue.Name = "ServiceManager";
162 1165 : aPropValue.Value <<= comphelper::getProcessServiceFactory();
163 1165 : aPropertyVector.push_back( makeAny( aPropValue ));
164 :
165 1165 : aPropValue.Name = "ParentWindow";
166 1165 : aPropValue.Value <<= VCLUnoHelper::GetInterface(pToolBox);
167 1165 : aPropertyVector.push_back( makeAny( aPropValue ));
168 :
169 1165 : if (nWidth > 0)
170 : {
171 0 : aPropValue.Name = "Width";
172 0 : aPropValue.Value <<= nWidth;
173 0 : aPropertyVector.push_back( makeAny( aPropValue ));
174 : }
175 :
176 2330 : Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
177 : return Reference<frame::XToolbarController>(
178 1165 : xFactory->createInstanceWithArgumentsAndContext(
179 : rsCommandName,
180 : aArgs,
181 1165 : xContext),
182 3495 : UNO_QUERY);
183 16716 : }
184 : }
185 0 : catch (Exception&)
186 : {
187 : // Ignore exception.
188 : }
189 16716 : return NULL;
190 : }
191 :
192 : } } // end of namespace sfx2::sidebar
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|