1ae13266dSAndre Fischer /**************************************************************
2ae13266dSAndre Fischer  *
3ae13266dSAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
4ae13266dSAndre Fischer  * or more contributor license agreements.  See the NOTICE file
5ae13266dSAndre Fischer  * distributed with this work for additional information
6ae13266dSAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
7ae13266dSAndre Fischer  * to you under the Apache License, Version 2.0 (the
8ae13266dSAndre Fischer  * "License"); you may not use this file except in compliance
9ae13266dSAndre Fischer  * with the License.  You may obtain a copy of the License at
10ae13266dSAndre Fischer  *
11ae13266dSAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
12ae13266dSAndre Fischer  *
13ae13266dSAndre Fischer  * Unless required by applicable law or agreed to in writing,
14ae13266dSAndre Fischer  * software distributed under the License is distributed on an
15ae13266dSAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ae13266dSAndre Fischer  * KIND, either express or implied.  See the License for the
17ae13266dSAndre Fischer  * specific language governing permissions and limitations
18ae13266dSAndre Fischer  * under the License.
19ae13266dSAndre Fischer  *
20ae13266dSAndre Fischer  *************************************************************/
21ae13266dSAndre Fischer 
22ae13266dSAndre Fischer #include "precompiled_sfx2.hxx"
23ae13266dSAndre Fischer 
24ae13266dSAndre Fischer #include "sfx2/sidebar/ControllerFactory.hxx"
25ae13266dSAndre Fischer #include "sfx2/sidebar/CommandInfoProvider.hxx"
26ae13266dSAndre Fischer #include "sfx2/sidebar/Tools.hxx"
27ae13266dSAndre Fischer 
28ae13266dSAndre Fischer #include <com/sun/star/frame/XToolbarController.hpp>
29ae13266dSAndre Fischer #include <com/sun/star/frame/XFrame.hpp>
30*d46a1e42SAndre Fischer #include <com/sun/star/frame/XUIControllerFactory.hpp>
31ae13266dSAndre Fischer 
32ae13266dSAndre Fischer #include <framework/sfxhelperfunctions.hxx>
33ae13266dSAndre Fischer #include <svtools/generictoolboxcontroller.hxx>
34ae13266dSAndre Fischer #include <comphelper/processfactory.hxx>
35*d46a1e42SAndre Fischer #include <toolkit/helper/vclunohelper.hxx>
36ae13266dSAndre Fischer 
37ae13266dSAndre Fischer 
38ae13266dSAndre Fischer using namespace css;
39ae13266dSAndre Fischer using namespace cssu;
40ae13266dSAndre Fischer using ::rtl::OUString;
41ae13266dSAndre Fischer 
42ae13266dSAndre Fischer 
43ae13266dSAndre Fischer namespace sfx2 { namespace sidebar {
44ae13266dSAndre Fischer 
CreateToolBoxController(ToolBox * pToolBox,const sal_uInt16 nItemId,const OUString & rsCommandName,const Reference<frame::XFrame> & rxFrame,const Reference<awt::XWindow> & rxParentWindow,const sal_Int32 nWidth)45ae13266dSAndre Fischer Reference<frame::XToolbarController> ControllerFactory::CreateToolBoxController(
46ae13266dSAndre Fischer     ToolBox* pToolBox,
47ae13266dSAndre Fischer     const sal_uInt16 nItemId,
48ae13266dSAndre Fischer     const OUString& rsCommandName,
49*d46a1e42SAndre Fischer     const Reference<frame::XFrame>& rxFrame,
50*d46a1e42SAndre Fischer     const Reference<awt::XWindow>& rxParentWindow,
51*d46a1e42SAndre Fischer     const sal_Int32 nWidth)
52ae13266dSAndre Fischer {
53*d46a1e42SAndre Fischer     Reference<frame::XToolbarController> xController (
54*d46a1e42SAndre Fischer         CreateToolBarController(
55*d46a1e42SAndre Fischer             pToolBox,
56*d46a1e42SAndre Fischer             rsCommandName,
57*d46a1e42SAndre Fischer             rxFrame,
58*d46a1e42SAndre Fischer             nWidth));
59*d46a1e42SAndre Fischer 
60ae13266dSAndre Fischer     // Create a controller for the new item.
61*d46a1e42SAndre Fischer     if ( ! xController.is())
62*d46a1e42SAndre Fischer     {
63*d46a1e42SAndre Fischer         xController.set(
64*d46a1e42SAndre Fischer             static_cast<XWeak*>(::framework::CreateToolBoxController(
65*d46a1e42SAndre Fischer                     rxFrame,
66*d46a1e42SAndre Fischer                     pToolBox,
67*d46a1e42SAndre Fischer                     nItemId,
68*d46a1e42SAndre Fischer                     rsCommandName)),
69ae13266dSAndre Fischer             UNO_QUERY);
70*d46a1e42SAndre Fischer     }
71ae13266dSAndre Fischer     if ( ! xController.is())
72*d46a1e42SAndre Fischer     {
73ae13266dSAndre Fischer         xController.set(
74ae13266dSAndre Fischer             static_cast<XWeak*>(new svt::GenericToolboxController(
75ae13266dSAndre Fischer                     ::comphelper::getProcessServiceFactory(),
76ae13266dSAndre Fischer                     rxFrame,
77ae13266dSAndre Fischer                     pToolBox,
78ae13266dSAndre Fischer                     nItemId,
79ae13266dSAndre Fischer                     rsCommandName)),
80ae13266dSAndre Fischer             UNO_QUERY);
81*d46a1e42SAndre Fischer     }
82ae13266dSAndre Fischer 
83ae13266dSAndre Fischer     // Initialize the controller with eg a service factory.
84ae13266dSAndre Fischer     Reference<lang::XInitialization> xInitialization (xController, UNO_QUERY);
85ae13266dSAndre Fischer     if (xInitialization.is())
86ae13266dSAndre Fischer     {
87ae13266dSAndre Fischer         beans::PropertyValue aPropValue;
88ae13266dSAndre Fischer         std::vector<Any> aPropertyVector;
89ae13266dSAndre Fischer 
90ae13266dSAndre Fischer         aPropValue.Name = A2S("Frame");
91ae13266dSAndre Fischer         aPropValue.Value <<= rxFrame;
92ae13266dSAndre Fischer         aPropertyVector.push_back(makeAny(aPropValue));
93ae13266dSAndre Fischer 
94ae13266dSAndre Fischer         aPropValue.Name = A2S("ServiceManager");
95ae13266dSAndre Fischer         aPropValue.Value <<= ::comphelper::getProcessServiceFactory();
96ae13266dSAndre Fischer         aPropertyVector.push_back(makeAny(aPropValue));
97ae13266dSAndre Fischer 
98ae13266dSAndre Fischer         aPropValue.Name = A2S("CommandURL");
99ae13266dSAndre Fischer         aPropValue.Value <<= rsCommandName;
100ae13266dSAndre Fischer         aPropertyVector.push_back(makeAny(aPropValue));
101ae13266dSAndre Fischer 
102ae13266dSAndre Fischer         Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
103ae13266dSAndre Fischer         xInitialization->initialize(aArgs);
104ae13266dSAndre Fischer     }
105ae13266dSAndre Fischer 
106ae13266dSAndre Fischer     if (xController.is())
107ae13266dSAndre Fischer     {
108*d46a1e42SAndre Fischer         if (rxParentWindow.is())
109*d46a1e42SAndre Fischer         {
110*d46a1e42SAndre Fischer             Reference<awt::XWindow> xItemWindow (xController->createItemWindow(rxParentWindow));
111*d46a1e42SAndre Fischer             Window* pItemWindow = VCLUnoHelper::GetWindow(xItemWindow);
112*d46a1e42SAndre Fischer             if (pItemWindow != NULL)
113*d46a1e42SAndre Fischer             {
114*d46a1e42SAndre Fischer                 WindowType nType = pItemWindow->GetType();
115*d46a1e42SAndre Fischer                 if (nType == WINDOW_LISTBOX || nType == WINDOW_MULTILISTBOX || nType == WINDOW_COMBOBOX)
116*d46a1e42SAndre Fischer                     pItemWindow->SetAccessibleName(pToolBox->GetItemText(nItemId));
117*d46a1e42SAndre Fischer                 if (nWidth > 0)
118*d46a1e42SAndre Fischer                     pItemWindow->SetSizePixel(Size(nWidth, pItemWindow->GetSizePixel().Height()));
119*d46a1e42SAndre Fischer                 pToolBox->SetItemWindow(nItemId, pItemWindow);
120*d46a1e42SAndre Fischer             }
121*d46a1e42SAndre Fischer         }
122*d46a1e42SAndre Fischer 
123*d46a1e42SAndre Fischer         Reference<util::XUpdatable> xUpdatable (xController, UNO_QUERY);
124*d46a1e42SAndre Fischer         if (xUpdatable.is())
125*d46a1e42SAndre Fischer             xUpdatable->update();
126*d46a1e42SAndre Fischer 
127*d46a1e42SAndre Fischer         // Add label.
128*d46a1e42SAndre Fischer         if (xController.is())
129*d46a1e42SAndre Fischer         {
130*d46a1e42SAndre Fischer             const OUString sLabel (sfx2::sidebar::CommandInfoProvider::Instance().GetLabelForCommand(
131*d46a1e42SAndre Fischer                     rsCommandName,
132*d46a1e42SAndre Fischer                     rxFrame));
133*d46a1e42SAndre Fischer             pToolBox->SetQuickHelpText(nItemId, sLabel);
134*d46a1e42SAndre Fischer             pToolBox->EnableItem(nItemId);
135*d46a1e42SAndre Fischer         }
136ae13266dSAndre Fischer     }
137ae13266dSAndre Fischer 
138ae13266dSAndre Fischer     return xController;
139ae13266dSAndre Fischer }
140ae13266dSAndre Fischer 
141ae13266dSAndre Fischer 
142*d46a1e42SAndre Fischer 
143*d46a1e42SAndre Fischer 
CreateToolBarController(ToolBox * pToolBox,const OUString & rsCommandName,const Reference<frame::XFrame> & rxFrame,const sal_Int32 nWidth)144*d46a1e42SAndre Fischer Reference<frame::XToolbarController> ControllerFactory::CreateToolBarController(
145*d46a1e42SAndre Fischer     ToolBox* pToolBox,
146*d46a1e42SAndre Fischer     const OUString& rsCommandName,
147*d46a1e42SAndre Fischer     const Reference<frame::XFrame>& rxFrame,
148*d46a1e42SAndre Fischer     const sal_Int32 nWidth)
149*d46a1e42SAndre Fischer {
150*d46a1e42SAndre Fischer     try
151*d46a1e42SAndre Fischer     {
152*d46a1e42SAndre Fischer         Reference<frame::XUIControllerFactory> xFactory (
153*d46a1e42SAndre Fischer             comphelper::getProcessServiceFactory()->createInstance(A2S("com.sun.star.frame.ToolbarControllerFactory")),
154*d46a1e42SAndre Fischer             UNO_QUERY);
155*d46a1e42SAndre Fischer         OUString sModuleName (Tools::GetModuleName(rxFrame));
156*d46a1e42SAndre Fischer 
157*d46a1e42SAndre Fischer         if (xFactory.is() && xFactory->hasController(rsCommandName,  sModuleName))
158*d46a1e42SAndre Fischer         {
159*d46a1e42SAndre Fischer             beans::PropertyValue aPropValue;
160*d46a1e42SAndre Fischer             std::vector<Any> aPropertyVector;
161*d46a1e42SAndre Fischer 
162*d46a1e42SAndre Fischer             aPropValue.Name = A2S("ModuleIdentifier");
163*d46a1e42SAndre Fischer             aPropValue.Value <<= sModuleName;
164*d46a1e42SAndre Fischer             aPropertyVector.push_back( makeAny( aPropValue ));
165*d46a1e42SAndre Fischer 
166*d46a1e42SAndre Fischer             aPropValue.Name = A2S("Frame");
167*d46a1e42SAndre Fischer             aPropValue.Value <<= rxFrame;
168*d46a1e42SAndre Fischer             aPropertyVector.push_back( makeAny( aPropValue ));
169*d46a1e42SAndre Fischer 
170*d46a1e42SAndre Fischer             aPropValue.Name = A2S("ServiceManager");
171*d46a1e42SAndre Fischer             aPropValue.Value <<= comphelper::getProcessServiceFactory();
172*d46a1e42SAndre Fischer             aPropertyVector.push_back( makeAny( aPropValue ));
173*d46a1e42SAndre Fischer 
174*d46a1e42SAndre Fischer             aPropValue.Name = A2S("ParentWindow");
175*d46a1e42SAndre Fischer             aPropValue.Value <<= VCLUnoHelper::GetInterface(pToolBox);
176*d46a1e42SAndre Fischer             aPropertyVector.push_back( makeAny( aPropValue ));
177*d46a1e42SAndre Fischer 
178*d46a1e42SAndre Fischer             if (nWidth > 0)
179*d46a1e42SAndre Fischer             {
180*d46a1e42SAndre Fischer                 aPropValue.Name = A2S("Width");
181*d46a1e42SAndre Fischer                 aPropValue.Value <<= nWidth;
182*d46a1e42SAndre Fischer                 aPropertyVector.push_back( makeAny( aPropValue ));
183*d46a1e42SAndre Fischer             }
184*d46a1e42SAndre Fischer 
185*d46a1e42SAndre Fischer             Reference<beans::XPropertySet> xFactoryProperties (comphelper::getProcessServiceFactory(), UNO_QUERY);
186*d46a1e42SAndre Fischer             Reference<XComponentContext > xComponentContext;
187*d46a1e42SAndre Fischer             if (xFactoryProperties.is())
188*d46a1e42SAndre Fischer                 xFactoryProperties->getPropertyValue(A2S("DefaultContext")) >>= xComponentContext;
189*d46a1e42SAndre Fischer 
190*d46a1e42SAndre Fischer             Sequence<Any> aArgs (comphelper::containerToSequence(aPropertyVector));
191*d46a1e42SAndre Fischer             return Reference<frame::XToolbarController>(
192*d46a1e42SAndre Fischer                 xFactory->createInstanceWithArgumentsAndContext(
193*d46a1e42SAndre Fischer                     rsCommandName,
194*d46a1e42SAndre Fischer                     aArgs,
195*d46a1e42SAndre Fischer                     xComponentContext),
196*d46a1e42SAndre Fischer                 UNO_QUERY);
197*d46a1e42SAndre Fischer         }
198*d46a1e42SAndre Fischer     }
199*d46a1e42SAndre Fischer     catch (Exception& rException)
200*d46a1e42SAndre Fischer     {
201*d46a1e42SAndre Fischer         // Ignore exception.
202*d46a1e42SAndre Fischer     }
203*d46a1e42SAndre Fischer     return NULL;
204*d46a1e42SAndre Fischer }
205*d46a1e42SAndre Fischer 
206ae13266dSAndre Fischer } } // end of namespace sfx2::sidebar
207