1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sfx2.hxx"
23 
24 #include "sfx2/sidebar/ControllerFactory.hxx"
25 #include "sfx2/sidebar/CommandInfoProvider.hxx"
26 #include "sfx2/sidebar/Tools.hxx"
27 
28 #include <com/sun/star/frame/XToolbarController.hpp>
29 #include <com/sun/star/frame/XFrame.hpp>
30 
31 #include <framework/sfxhelperfunctions.hxx>
32 #include <svtools/generictoolboxcontroller.hxx>
33 #include <comphelper/processfactory.hxx>
34 
35 
36 using namespace css;
37 using namespace cssu;
38 using ::rtl::OUString;
39 
40 
41 namespace sfx2 { namespace sidebar {
42 
43 Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
44     ToolBox* pToolBox,
45     const sal_uInt16 nItemId,
46     const OUString& rsCommandName,
47     const Reference<frame::XFrame>& rxFrame)
48 {
49     // Create a controller for the new item.
50     Reference<frame::XToolbarController> xController(
51         static_cast<XWeak*>(::framework::CreateToolBoxController(
52                 rxFrame,
53                 pToolBox,
54                 nItemId,
55                 rsCommandName)),
56             UNO_QUERY);
57     if ( ! xController.is())
58         xController.set(
59             static_cast<XWeak*>(new svt::GenericToolboxController(
60                     ::comphelper::getProcessServiceFactory(),
61                     rxFrame,
62                     pToolBox,
63                     nItemId,
64                     rsCommandName)),
65             UNO_QUERY);
66 
67     // Initialize the controller with eg a service factory.
68     Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY);
69     if (xInitialization.is())
70     {
71         beans::PropertyValue aPropValue;
72         std::vector<Any> aPropertyVector;
73 
74         aPropValue.Name = A2S("Frame");
75         aPropValue.Value <<= rxFrame;
76         aPropertyVector.push_back(makeAny(aPropValue));
77 
78         aPropValue.Name = A2S("ServiceManager");
79         aPropValue.Value <<= ::comphelper::getProcessServiceFactory();
80         aPropertyVector.push_back(makeAny(aPropValue));
81 
82         aPropValue.Name = A2S("CommandURL");
83         aPropValue.Value <<= rsCommandName;
84         aPropertyVector.push_back(makeAny(aPropValue));
85 
86         Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
87         xInitialization->initialize(aArgs);
88     }
89 
90     Reference<util::XUpdatable> xUpdatable (xController, UNO_QUERY);
91     if (xUpdatable.is())
92         xUpdatable->update();
93 
94     // Add label.
95     if (xController.is())
96     {
97         const OUString sLabel (sfx2::sidebar::CommandInfoProvider::Instance().GetLabelForCommand(
98                 rsCommandName,
99                 rxFrame));
100         pToolBox->SetQuickHelpText(nItemId, sLabel);
101         pToolBox->EnableItem(nItemId);
102     }
103 
104     return xController;
105 }
106 
107 
108 } } // end of namespace sfx2::sidebar
109