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
<lang::XComponent
> ControllerFactory::CreateImageController(
42 const Reference
<frame::XFrame
>& rxFrame
,
43 const Reference
<awt::XWindow
>& rxParentWindow
)
45 rtl::Reference
xController(new framework::ImageOrientationController(
46 comphelper::getProcessComponentContext(), rxFrame
, rxParentWindow
,
47 vcl::CommandInfoProvider::GetModuleIdentifier(rxFrame
)));
49 xController
->update();
53 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBoxController(
55 const ToolBoxItemId nItemId
,
56 const OUString
& rsCommandName
,
57 const Reference
<frame::XFrame
>& rxFrame
,
58 const Reference
<frame::XController
>& rxController
,
59 const Reference
<awt::XWindow
>& rxParentWindow
,
60 const sal_Int32 nWidth
, bool bSideBar
)
62 Reference
<frame::XToolbarController
> xController (
63 CreateToolBarController(
64 VCLUnoHelper::GetInterface(pToolBox
),
66 rxFrame
, rxController
,
69 bool bFactoryHasController( xController
.is() );
71 // Create a controller for the new item.
72 if ( !bFactoryHasController
)
74 xController
= ::framework::CreateToolBoxController(
80 if ( ! xController
.is())
82 xController
= new framework::GenericToolbarController(
83 ::comphelper::getProcessComponentContext(),
90 // Initialize the controller with eg a service factory.
91 Reference
<lang::XInitialization
> xInitialization (xController
, UNO_QUERY
);
92 if (!bFactoryHasController
&& xInitialization
.is())
94 beans::PropertyValue aPropValue
;
95 std::vector
<Any
> aPropertyVector
;
97 aPropValue
.Name
= "Frame";
98 aPropValue
.Value
<<= rxFrame
;
99 aPropertyVector
.push_back(Any(aPropValue
));
101 aPropValue
.Name
= "ServiceManager";
102 aPropValue
.Value
<<= ::comphelper::getProcessServiceFactory();
103 aPropertyVector
.push_back(Any(aPropValue
));
105 aPropValue
.Name
= "CommandURL";
106 aPropValue
.Value
<<= rsCommandName
;
107 aPropertyVector
.push_back(Any(aPropValue
));
109 Sequence
<Any
> aArgs (comphelper::containerToSequence(aPropertyVector
));
110 xInitialization
->initialize(aArgs
);
113 if (xController
.is())
115 if (rxParentWindow
.is())
117 Reference
<awt::XWindow
> xItemWindow (xController
->createItemWindow(rxParentWindow
));
118 VclPtr
<vcl::Window
> pItemWindow
= VCLUnoHelper::GetWindow(xItemWindow
);
119 if (pItemWindow
!= nullptr)
121 WindowType nType
= pItemWindow
->GetType();
122 if (nType
== WindowType::LISTBOX
|| nType
== WindowType::MULTILISTBOX
|| nType
== WindowType::COMBOBOX
)
123 pItemWindow
->SetAccessibleName(pToolBox
->GetItemText(nItemId
));
125 pItemWindow
->SetSizePixel(Size(nWidth
, pItemWindow
->GetSizePixel().Height()));
126 pToolBox
->SetItemWindow(nItemId
, pItemWindow
);
130 Reference
<util::XUpdatable
> xUpdatable (xController
, UNO_QUERY
);
132 xUpdatable
->update();
135 if (xController
.is())
137 auto aProperties
= vcl::CommandInfoProvider::GetCommandProperties(rsCommandName
,
138 vcl::CommandInfoProvider::GetModuleIdentifier(rxFrame
));
139 const OUString
sTooltip (vcl::CommandInfoProvider::GetTooltipForCommand(
140 rsCommandName
, aProperties
, rxFrame
));
141 if (pToolBox
->GetQuickHelpText(nItemId
).isEmpty())
142 pToolBox
->SetQuickHelpText(nItemId
, sTooltip
);
143 pToolBox
->EnableItem(nItemId
);
150 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBoxController(
151 weld::Toolbar
& rToolbar
, weld::Builder
& rBuilder
,
152 const OUString
& rsCommandName
,
153 const Reference
<frame::XFrame
>& rxFrame
,
154 const Reference
<frame::XController
>& rxController
,
157 css::uno::Reference
<css::awt::XWindow
> xWidget(new weld::TransportAsXWindow(&rToolbar
, &rBuilder
));
159 Reference
<frame::XToolbarController
> xController(
160 CreateToolBarController(
163 rxFrame
, rxController
,
166 if (!xController
.is())
168 xController
= new framework::GenericToolbarController(
169 ::comphelper::getProcessComponentContext(),
175 if (xController
.is())
177 xController
->createItemWindow(xWidget
);
179 Reference
<util::XUpdatable
> xUpdatable(xController
, UNO_QUERY
);
181 xUpdatable
->update();
188 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBarController(
189 const Reference
<awt::XWindow
>& rxToolbar
,
190 const OUString
& rsCommandName
,
191 const Reference
<frame::XFrame
>& rxFrame
,
192 const Reference
<frame::XController
>& rxController
,
193 const sal_Int32 nWidth
, bool bSideBar
)
197 Reference
<XComponentContext
> xContext
= comphelper::getProcessComponentContext();
198 Reference
<frame::XUIControllerFactory
> xFactory
= frame::theToolbarControllerFactory::get( xContext
);
199 OUString
sModuleName (Tools::GetModuleName(rxController
));
201 if (xFactory
.is() && xFactory
->hasController(rsCommandName
, sModuleName
))
203 beans::PropertyValue aPropValue
;
204 std::vector
<Any
> aPropertyVector
;
206 aPropValue
.Name
= "ModuleIdentifier";
207 aPropValue
.Value
<<= sModuleName
;
208 aPropertyVector
.push_back( Any( aPropValue
));
210 aPropValue
.Name
= "Frame";
211 aPropValue
.Value
<<= rxFrame
;
212 aPropertyVector
.push_back( Any( aPropValue
));
214 aPropValue
.Name
= "ServiceManager";
215 aPropValue
.Value
<<= comphelper::getProcessServiceFactory();
216 aPropertyVector
.push_back( Any( aPropValue
));
218 aPropValue
.Name
= "ParentWindow";
219 aPropValue
.Value
<<= rxToolbar
;
220 aPropertyVector
.push_back( Any( aPropValue
));
222 aPropValue
.Name
= "IsSidebar";
223 aPropValue
.Value
<<= bSideBar
;
224 aPropertyVector
.push_back( Any( aPropValue
));
228 aPropValue
.Name
= "Width";
229 aPropValue
.Value
<<= nWidth
;
230 aPropertyVector
.push_back( Any( aPropValue
));
233 Sequence
<Any
> aArgs (comphelper::containerToSequence(aPropertyVector
));
234 return Reference
<frame::XToolbarController
>(
235 xFactory
->createInstanceWithArgumentsAndContext(
249 } // end of namespace sfx2::sidebar
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */