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