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 #include <com/sun/star/frame/XUIControllerFactory.hpp>
31 
32 #include <framework/sfxhelperfunctions.hxx>
33 #include <svtools/generictoolboxcontroller.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <toolkit/helper/vclunohelper.hxx>
36 
37 
38 using namespace css;
39 using namespace cssu;
40 using ::rtl::OUString;
41 
42 
43 namespace sfx2 { namespace sidebar {
44 
CreateToolBoxController(ToolBox * pToolBox,const sal_uInt16 nItemId,const OUString & rsCommandName,const Reference<frame::XFrame> & rxFrame,const Reference<awt::XWindow> & rxParentWindow,const sal_Int32 nWidth)45 Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
46     ToolBox* pToolBox,
47     const sal_uInt16 nItemId,
48     const OUString& rsCommandName,
49     const Reference<frame::XFrame>& rxFrame,
50     const Reference<awt::XWindow>& rxParentWindow,
51     const sal_Int32 nWidth)
52 {
53     Reference<frame::XToolbarController> xController (
54         CreateToolBarController(
55             pToolBox,
56             rsCommandName,
57             rxFrame,
58             nWidth));
59 
60     // Create a controller for the new item.
61     if ( ! xController.is())
62     {
63         xController.set(
64             static_cast<XWeak*>(::framework::CreateToolBoxController(
65                     rxFrame,
66                     pToolBox,
67                     nItemId,
68                     rsCommandName)),
69             UNO_QUERY);
70     }
71     if ( ! xController.is())
72     {
73         xController.set(
74             static_cast<XWeak*>(new svt::GenericToolboxController(
75                     ::comphelper::getProcessServiceFactory(),
76                     rxFrame,
77                     pToolBox,
78                     nItemId,
79                     rsCommandName)),
80             UNO_QUERY);
81     }
82 
83     // Initialize the controller with eg a service factory.
84     Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY);
85     if (xInitialization.is())
86     {
87         beans::PropertyValue aPropValue;
88         std::vector<Any> aPropertyVector;
89 
90         aPropValue.Name = A2S("Frame");
91         aPropValue.Value <<= rxFrame;
92         aPropertyVector.push_back(makeAny(aPropValue));
93 
94         aPropValue.Name = A2S("ServiceManager");
95         aPropValue.Value <<= ::comphelper::getProcessServiceFactory();
96         aPropertyVector.push_back(makeAny(aPropValue));
97 
98         aPropValue.Name = A2S("CommandURL");
99         aPropValue.Value <<= rsCommandName;
100         aPropertyVector.push_back(makeAny(aPropValue));
101 
102         Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
103         xInitialization->initialize(aArgs);
104     }
105 
106     if (xController.is())
107     {
108         if (rxParentWindow.is())
109         {
110             Reference<awt::XWindow> xItemWindow (xController->createItemWindow(rxParentWindow));
111             Window* pItemWindow = VCLUnoHelper::GetWindow(xItemWindow);
112             if (pItemWindow != NULL)
113             {
114                 WindowType nType = pItemWindow->GetType();
115                 if (nType == WINDOW_LISTBOX || nType == WINDOW_MULTILISTBOX || nType == WINDOW_COMBOBOX)
116                     pItemWindow->SetAccessibleName(pToolBox->GetItemText(nItemId));
117                 if (nWidth > 0)
118                     pItemWindow->SetSizePixel(Size(nWidth, pItemWindow->GetSizePixel().Height()));
119                 pToolBox->SetItemWindow(nItemId, pItemWindow);
120             }
121         }
122 
123         Reference<util::XUpdatable> xUpdatable (xController, UNO_QUERY);
124         if (xUpdatable.is())
125             xUpdatable->update();
126 
127         // Add label.
128         if (xController.is())
129         {
130             const OUString sLabel (sfx2::sidebar::CommandInfoProvider::Instance().GetLabelForCommand(
131                     rsCommandName,
132                     rxFrame));
133             pToolBox->SetQuickHelpText(nItemId, sLabel);
134             pToolBox->EnableItem(nItemId);
135         }
136     }
137 
138     return xController;
139 }
140 
141 
142 
143 
CreateToolBarController(ToolBox * pToolBox,const OUString & rsCommandName,const Reference<frame::XFrame> & rxFrame,const sal_Int32 nWidth)144 Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController(
145     ToolBox* pToolBox,
146     const OUString& rsCommandName,
147     const Reference<frame::XFrame>& rxFrame,
148     const sal_Int32 nWidth)
149 {
150     try
151     {
152         Reference<frame::XUIControllerFactory> xFactory (
153             comphelper::getProcessServiceFactory()->createInstance(A2S("com.sun.star.frame.ToolbarControllerFactory")),
154             UNO_QUERY);
155         OUString sModuleName (Tools::GetModuleName(rxFrame));
156 
157         if (xFactory.is() && xFactory->hasController(rsCommandName,  sModuleName))
158         {
159             beans::PropertyValue aPropValue;
160             std::vector<Any> aPropertyVector;
161 
162             aPropValue.Name = A2S("ModuleIdentifier");
163             aPropValue.Value <<= sModuleName;
164             aPropertyVector.push_back( makeAny( aPropValue ));
165 
166             aPropValue.Name = A2S("Frame");
167             aPropValue.Value <<= rxFrame;
168             aPropertyVector.push_back( makeAny( aPropValue ));
169 
170             aPropValue.Name = A2S("ServiceManager");
171             aPropValue.Value <<= comphelper::getProcessServiceFactory();
172             aPropertyVector.push_back( makeAny( aPropValue ));
173 
174             aPropValue.Name = A2S("ParentWindow");
175             aPropValue.Value <<= VCLUnoHelper::GetInterface(pToolBox);
176             aPropertyVector.push_back( makeAny( aPropValue ));
177 
178             if (nWidth > 0)
179             {
180                 aPropValue.Name = A2S("Width");
181                 aPropValue.Value <<= nWidth;
182                 aPropertyVector.push_back( makeAny( aPropValue ));
183             }
184 
185             Reference<beans::XPropertySet> xFactoryProperties (comphelper::getProcessServiceFactory(), UNO_QUERY);
186             Reference<XComponentContext > xComponentContext;
187             if (xFactoryProperties.is())
188                 xFactoryProperties->getPropertyValue(A2S("DefaultContext")) >>= xComponentContext;
189 
190             Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
191             return Reference<frame::XToolbarController>(
192                 xFactory->createInstanceWithArgumentsAndContext(
193                     rsCommandName,
194                     aArgs,
195                     xComponentContext),
196                 UNO_QUERY);
197         }
198     }
199     catch (Exception& rException)
200     {
201         // Ignore exception.
202     }
203     return NULL;
204 }
205 
206 } } // end of namespace sfx2::sidebar
207