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>
35 using ::rtl::OUString
;
38 namespace sfx2
{ namespace sidebar
{
40 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBoxController(
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(
55 // Create a controller for the new item.
56 if ( ! xController
.is())
59 static_cast<XWeak
*>(::framework::CreateToolBoxController(
66 if ( ! xController
.is())
69 static_cast<XWeak
*>(new svt::GenericToolboxController(
70 ::comphelper::getProcessComponentContext(),
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
));
113 pItemWindow
->SetSizePixel(Size(nWidth
, pItemWindow
->GetSizePixel().Height()));
114 pToolBox
->SetItemWindow(nItemId
, pItemWindow
);
118 Reference
<util::XUpdatable
> xUpdatable (xController
, UNO_QUERY
);
120 xUpdatable
->update();
123 if (xController
.is())
125 const OUString
sLabel (sfx2::sidebar::CommandInfoProvider::Instance().GetLabelForCommand(
128 pToolBox
->SetQuickHelpText(nItemId
, sLabel
);
129 pToolBox
->EnableItem(nItemId
);
139 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBarController(
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")),
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
));
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(
194 catch (Exception
& rException
)
201 } } // end of namespace sfx2::sidebar