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 ToolBoxItemId 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
)
62 xController
= ::framework::CreateToolBoxController(
68 if ( ! xController
.is())
70 xController
= new framework::GenericToolbarController(
71 ::comphelper::getProcessComponentContext(),
78 // Initialize the controller with eg a service factory.
79 Reference
<lang::XInitialization
> xInitialization (xController
, UNO_QUERY
);
80 if (!bFactoryHasController
&& xInitialization
.is())
82 beans::PropertyValue aPropValue
;
83 std::vector
<Any
> aPropertyVector
;
85 aPropValue
.Name
= "Frame";
86 aPropValue
.Value
<<= rxFrame
;
87 aPropertyVector
.push_back(makeAny(aPropValue
));
89 aPropValue
.Name
= "ServiceManager";
90 aPropValue
.Value
<<= ::comphelper::getProcessServiceFactory();
91 aPropertyVector
.push_back(makeAny(aPropValue
));
93 aPropValue
.Name
= "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 VclPtr
<vcl::Window
> pItemWindow
= VCLUnoHelper::GetWindow(xItemWindow
);
107 if (pItemWindow
!= nullptr)
109 WindowType nType
= pItemWindow
->GetType();
110 if (nType
== WindowType::LISTBOX
|| nType
== WindowType::MULTILISTBOX
|| nType
== WindowType::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 auto aProperties
= vcl::CommandInfoProvider::GetCommandProperties(rsCommandName
,
126 vcl::CommandInfoProvider::GetModuleIdentifier(rxFrame
));
127 const OUString
sTooltip (vcl::CommandInfoProvider::GetTooltipForCommand(
128 rsCommandName
, aProperties
, rxFrame
));
129 if (pToolBox
->GetQuickHelpText(nItemId
).isEmpty())
130 pToolBox
->SetQuickHelpText(nItemId
, sTooltip
);
131 pToolBox
->EnableItem(nItemId
);
138 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBoxController(
139 weld::Toolbar
& rToolbar
, weld::Builder
& rBuilder
,
140 const OUString
& rsCommandName
,
141 const Reference
<frame::XFrame
>& rxFrame
,
142 const Reference
<frame::XController
>& rxController
,
145 css::uno::Reference
<css::awt::XWindow
> xWidget(new weld::TransportAsXWindow(&rToolbar
, &rBuilder
));
147 Reference
<frame::XToolbarController
> xController(
148 CreateToolBarController(
151 rxFrame
, rxController
,
154 if (!xController
.is())
156 xController
= new framework::GenericToolbarController(
157 ::comphelper::getProcessComponentContext(),
163 if (xController
.is())
165 xController
->createItemWindow(xWidget
);
167 Reference
<util::XUpdatable
> xUpdatable(xController
, UNO_QUERY
);
169 xUpdatable
->update();
176 Reference
<frame::XToolbarController
> ControllerFactory::CreateToolBarController(
177 const Reference
<awt::XWindow
>& rxToolbar
,
178 const OUString
& rsCommandName
,
179 const Reference
<frame::XFrame
>& rxFrame
,
180 const Reference
<frame::XController
>& rxController
,
181 const sal_Int32 nWidth
, bool bSideBar
)
185 Reference
<XComponentContext
> xContext
= comphelper::getProcessComponentContext();
186 Reference
<frame::XUIControllerFactory
> xFactory
= frame::theToolbarControllerFactory::get( xContext
);
187 OUString
sModuleName (Tools::GetModuleName(rxController
));
189 if (xFactory
.is() && xFactory
->hasController(rsCommandName
, sModuleName
))
191 beans::PropertyValue aPropValue
;
192 std::vector
<Any
> aPropertyVector
;
194 aPropValue
.Name
= "ModuleIdentifier";
195 aPropValue
.Value
<<= sModuleName
;
196 aPropertyVector
.push_back( makeAny( aPropValue
));
198 aPropValue
.Name
= "Frame";
199 aPropValue
.Value
<<= rxFrame
;
200 aPropertyVector
.push_back( makeAny( aPropValue
));
202 aPropValue
.Name
= "ServiceManager";
203 aPropValue
.Value
<<= comphelper::getProcessServiceFactory();
204 aPropertyVector
.push_back( makeAny( aPropValue
));
206 aPropValue
.Name
= "ParentWindow";
207 aPropValue
.Value
<<= rxToolbar
;
208 aPropertyVector
.push_back( makeAny( aPropValue
));
210 aPropValue
.Name
= "IsSidebar";
211 aPropValue
.Value
<<= bSideBar
;
212 aPropertyVector
.push_back( makeAny( aPropValue
));
216 aPropValue
.Name
= "Width";
217 aPropValue
.Value
<<= nWidth
;
218 aPropertyVector
.push_back( makeAny( aPropValue
));
221 Sequence
<Any
> aArgs (comphelper::containerToSequence(aPropertyVector
));
222 return Reference
<frame::XToolbarController
>(
223 xFactory
->createInstanceWithArgumentsAndContext(
237 } // end of namespace sfx2::sidebar
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */