1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sidebar/ControllerFactory.hxx>
21 #include <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/theToolbarControllerFactory.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <framework/sfxhelperfunctions.hxx>
29 #include <framework/generictoolbarcontroller.hxx>
30 #include <vcl/toolbox.hxx>
31 #include <vcl/commandinfoprovider.hxx>
32 #include <vcl/weldutils.hxx>
33 #include <comphelper/processfactory.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
37 using namespace css::uno
;
39 namespace sfx2::sidebar
{
41 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBoxController(
43 const sal_uInt16 nItemId
,
44 const OUString
& rsCommandName
,
45 const Reference
<frame::XFrame
>& rxFrame
,
46 const Reference
<frame::XController
>& rxController
,
47 const Reference
<awt::XWindow
>& rxParentWindow
,
48 const sal_Int32 nWidth
, bool bSideBar
)
50 Reference
<frame::XToolbarController
> xController (
51 CreateToolBarController(
52 VCLUnoHelper::GetInterface(pToolBox
),
54 rxFrame
, rxController
,
57 bool bFactoryHasController( xController
.is() );
59 // Create a controller for the new item.
60 if ( !bFactoryHasController
)
63 static_cast<XWeak
*>(::framework::CreateToolBoxController(
70 if ( ! xController
.is())
73 static_cast<XWeak
*>(new framework::GenericToolbarController(
74 ::comphelper::getProcessComponentContext(),
82 // Initialize the controller with eg a service factory.
83 Reference
<lang::XInitialization
> xInitialization (xController
, UNO_QUERY
);
84 if (!bFactoryHasController
&& xInitialization
.is())
86 beans::PropertyValue aPropValue
;
87 std::vector
<Any
> aPropertyVector
;
89 aPropValue
.Name
= "Frame";
90 aPropValue
.Value
<<= rxFrame
;
91 aPropertyVector
.push_back(makeAny(aPropValue
));
93 aPropValue
.Name
= "ServiceManager";
94 aPropValue
.Value
<<= ::comphelper::getProcessServiceFactory();
95 aPropertyVector
.push_back(makeAny(aPropValue
));
97 aPropValue
.Name
= "CommandURL";
98 aPropValue
.Value
<<= rsCommandName
;
99 aPropertyVector
.push_back(makeAny(aPropValue
));
101 Sequence
<Any
> aArgs (comphelper::containerToSequence(aPropertyVector
));
102 xInitialization
->initialize(aArgs
);
105 if (xController
.is())
107 if (rxParentWindow
.is())
109 Reference
<awt::XWindow
> xItemWindow (xController
->createItemWindow(rxParentWindow
));
110 VclPtr
<vcl::Window
> pItemWindow
= VCLUnoHelper::GetWindow(xItemWindow
);
111 if (pItemWindow
!= nullptr)
113 WindowType nType
= pItemWindow
->GetType();
114 if (nType
== WindowType::LISTBOX
|| nType
== WindowType::MULTILISTBOX
|| nType
== WindowType::COMBOBOX
)
115 pItemWindow
->SetAccessibleName(pToolBox
->GetItemText(nItemId
));
117 pItemWindow
->SetSizePixel(Size(nWidth
, pItemWindow
->GetSizePixel().Height()));
118 pToolBox
->SetItemWindow(nItemId
, pItemWindow
);
122 Reference
<util::XUpdatable
> xUpdatable (xController
, UNO_QUERY
);
124 xUpdatable
->update();
127 if (xController
.is())
129 auto aProperties
= vcl::CommandInfoProvider::GetCommandProperties(rsCommandName
,
130 vcl::CommandInfoProvider::GetModuleIdentifier(rxFrame
));
131 const OUString
sTooltip (vcl::CommandInfoProvider::GetTooltipForCommand(
132 rsCommandName
, aProperties
, rxFrame
));
133 if (pToolBox
->GetQuickHelpText(nItemId
).isEmpty())
134 pToolBox
->SetQuickHelpText(nItemId
, sTooltip
);
135 pToolBox
->EnableItem(nItemId
);
142 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBoxController(
143 weld::Toolbar
& rToolbar
, weld::Builder
& rBuilder
,
144 const OUString
& rsCommandName
,
145 const Reference
<frame::XFrame
>& rxFrame
,
146 const Reference
<frame::XController
>& rxController
,
149 css::uno::Reference
<css::awt::XWindow
> xWidget(new weld::TransportAsXWindow(&rToolbar
, &rBuilder
));
151 Reference
<frame::XToolbarController
> xController(
152 CreateToolBarController(
155 rxFrame
, rxController
,
158 if (!xController
.is())
161 static_cast<XWeak
*>(new framework::GenericToolbarController(
162 ::comphelper::getProcessComponentContext(),
169 if (xController
.is())
171 xController
->createItemWindow(xWidget
);
173 Reference
<util::XUpdatable
> xUpdatable(xController
, UNO_QUERY
);
175 xUpdatable
->update();
182 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBarController(
183 const Reference
<awt::XWindow
>& rxToolbar
,
184 const OUString
& rsCommandName
,
185 const Reference
<frame::XFrame
>& rxFrame
,
186 const Reference
<frame::XController
>& rxController
,
187 const sal_Int32 nWidth
, bool bSideBar
)
191 Reference
<XComponentContext
> xContext
= comphelper::getProcessComponentContext();
192 Reference
<frame::XUIControllerFactory
> xFactory
= frame::theToolbarControllerFactory::get( xContext
);
193 OUString
sModuleName (Tools::GetModuleName(rxController
));
195 if (xFactory
.is() && xFactory
->hasController(rsCommandName
, sModuleName
))
197 beans::PropertyValue aPropValue
;
198 std::vector
<Any
> aPropertyVector
;
200 aPropValue
.Name
= "ModuleIdentifier";
201 aPropValue
.Value
<<= sModuleName
;
202 aPropertyVector
.push_back( makeAny( aPropValue
));
204 aPropValue
.Name
= "Frame";
205 aPropValue
.Value
<<= rxFrame
;
206 aPropertyVector
.push_back( makeAny( aPropValue
));
208 aPropValue
.Name
= "ServiceManager";
209 aPropValue
.Value
<<= comphelper::getProcessServiceFactory();
210 aPropertyVector
.push_back( makeAny( aPropValue
));
212 aPropValue
.Name
= "ParentWindow";
213 aPropValue
.Value
<<= rxToolbar
;
214 aPropertyVector
.push_back( makeAny( aPropValue
));
216 aPropValue
.Name
= "IsSidebar";
217 aPropValue
.Value
<<= bSideBar
;
218 aPropertyVector
.push_back( makeAny( aPropValue
));
222 aPropValue
.Name
= "Width";
223 aPropValue
.Value
<<= nWidth
;
224 aPropertyVector
.push_back( makeAny( aPropValue
));
227 Sequence
<Any
> aArgs (comphelper::containerToSequence(aPropertyVector
));
228 return Reference
<frame::XToolbarController
>(
229 xFactory
->createInstanceWithArgumentsAndContext(
243 } // end of namespace sfx2::sidebar
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */