Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "toolboxcontroller.hxx"
21 : #include "uiservices.hxx"
22 : #include <com/sun/star/ui/ImageType.hpp>
23 : #include <com/sun/star/frame/XDispatchProvider.hpp>
24 : #include <toolkit/helper/vclunohelper.hxx>
25 : #include <vcl/menu.hxx>
26 : #include <com/sun/star/ui/XUIConfigurationManager.hpp>
27 : #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
28 : #include <com/sun/star/ui/XImageManager.hpp>
29 : #include <com/sun/star/graphic/XGraphic.hpp>
30 : #include <vcl/svapp.hxx>
31 : #include <vcl/toolbox.hxx>
32 : #include "dbu_resource.hrc"
33 : #include <svtools/miscopt.hxx>
34 : #include <unotools/moduleoptions.hxx>
35 : #include <tools/diagnose_ex.h>
36 : #include <svtools/menuoptions.hxx>
37 : #include <osl/mutex.hxx>
38 : #include "dbu_reghelper.hxx"
39 : #include "UITools.hxx"
40 : #include <comphelper/processfactory.hxx>
41 :
42 24 : extern "C" void SAL_CALL createRegistryInfo_OToolboxController()
43 : {
44 24 : static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::OToolboxController> aAutoRegistration;
45 24 : }
46 :
47 : namespace dbaui
48 : {
49 : using namespace svt;
50 : using namespace ::com::sun::star::graphic;
51 : using namespace com::sun::star::uno;
52 : using namespace com::sun::star::beans;
53 : using namespace com::sun::star::lang;
54 : using namespace ::com::sun::star::frame;
55 : using namespace ::com::sun::star::util;
56 : using namespace ::com::sun::star::ui;
57 :
58 : namespace
59 : {
60 0 : void lcl_copy(Menu* _pMenu,sal_uInt16 _nMenuId,sal_uInt16 _nMenuPos,ToolBox* _pToolBox,sal_uInt16 _nToolId,const OUString& _sCommand)
61 : {
62 0 : if ( _pMenu->GetItemType(_nMenuPos) != MenuItemType::STRING )
63 0 : _pToolBox->SetItemImage(_nToolId, _pMenu->GetItemImage(_nMenuId));
64 0 : _pToolBox->SetItemCommand( _nToolId, _sCommand);
65 0 : _pToolBox->SetHelpId(_nToolId, _pMenu->GetHelpId(_nMenuId));
66 0 : _pToolBox->SetHelpText(_nToolId, _pMenu->GetHelpText(_nMenuId));
67 0 : _pToolBox->SetQuickHelpText(_nToolId, _pMenu->GetTipHelpText(_nMenuId));
68 0 : _pToolBox->SetItemText(_nToolId, _pMenu->GetItemText(_nMenuId));
69 0 : }
70 : }
71 :
72 2 : OToolboxController::OToolboxController(const Reference< XComponentContext >& _rxORB)
73 2 : : m_nToolBoxId(1)
74 : {
75 2 : osl_atomic_increment(&m_refCount);
76 2 : m_xContext = _rxORB;
77 2 : osl_atomic_decrement(&m_refCount);
78 :
79 2 : }
80 48 : IMPLEMENT_SERVICE_INFO_IMPLNAME_STATIC(OToolboxController, "com.sun.star.sdb.ApplicationToolboxController")
81 0 : IMPLEMENT_SERVICE_INFO_SUPPORTS(OToolboxController)
82 24 : IMPLEMENT_SERVICE_INFO_GETSUPPORTED1_STATIC(OToolboxController, "com.sun.star.frame.ToolboxController")
83 :
84 : Reference< XInterface >
85 2 : SAL_CALL OToolboxController::Create(const Reference< XMultiServiceFactory >& _rxORB)
86 : {
87 2 : return static_cast< XServiceInfo* >(new OToolboxController( comphelper::getComponentContext(_rxORB) ));
88 : }
89 :
90 : // XInterface
91 60 : Any SAL_CALL OToolboxController::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
92 : {
93 60 : Any aReturn = ToolboxController::queryInterface(_rType);
94 60 : if (!aReturn.hasValue())
95 2 : aReturn = TToolboxController_BASE::queryInterface(_rType);
96 60 : return aReturn;
97 : }
98 320 : void SAL_CALL OToolboxController::acquire() throw ()
99 : {
100 320 : ToolboxController::acquire();
101 320 : }
102 320 : void SAL_CALL OToolboxController::release() throw ()
103 : {
104 320 : ToolboxController::release();
105 320 : }
106 2 : void SAL_CALL OToolboxController::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
107 : {
108 2 : ToolboxController::initialize(_rArguments);
109 2 : SolarMutexGuard aSolarMutexGuard;
110 4 : ::osl::MutexGuard aGuard(m_aMutex);
111 :
112 2 : if ( m_aCommandURL == ".uno:DBNewForm" )
113 : {
114 2 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewForm") ,sal_True));
115 2 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewView") ,sal_True));
116 2 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewViewSQL") ,sal_True));
117 2 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewQuery") ,sal_True));
118 2 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewQuerySql") ,sal_True));
119 2 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewReport") ,sal_True));
120 2 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewReportAutoPilot"),sal_True));
121 2 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewTable") ,sal_True));
122 : }
123 : else
124 : {
125 0 : m_aStates.insert(TCommandState::value_type(OUString(".uno:Refresh") ,sal_True));
126 0 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBRebuildData") ,sal_True));
127 : }
128 :
129 2 : TCommandState::iterator aIter = m_aStates.begin();
130 2 : TCommandState::iterator aEnd = m_aStates.end();
131 18 : for (; aIter != aEnd; ++aIter)
132 16 : addStatusListener(aIter->first);
133 :
134 2 : ToolBox* pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
135 2 : if ( pToolBox )
136 : {
137 2 : sal_uInt16 nCount = pToolBox->GetItemCount();
138 24 : for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
139 : {
140 24 : sal_uInt16 nItemId = pToolBox->GetItemId(nPos);
141 24 : if ( pToolBox->GetItemCommand(nItemId) == m_aCommandURL )
142 : {
143 2 : m_nToolBoxId = nItemId;
144 2 : break;
145 : }
146 : }
147 :
148 : // check if paste special is allowed, when not don't add DROPDOWN
149 2 : pToolBox->SetItemBits(m_nToolBoxId,pToolBox->GetItemBits(m_nToolBoxId) | ToolBoxItemBits::DROPDOWN);
150 2 : }
151 2 : }
152 14 : void SAL_CALL OToolboxController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
153 : {
154 14 : SolarMutexGuard aSolarMutexGuard;
155 28 : ::osl::MutexGuard aGuard(m_aMutex);
156 14 : TCommandState::iterator aFind = m_aStates.find( Event.FeatureURL.Complete );
157 14 : if ( aFind != m_aStates.end() )
158 : {
159 14 : aFind->second = Event.IsEnabled;
160 14 : if ( m_aCommandURL == aFind->first && !Event.IsEnabled )
161 : {
162 0 : ::std::unique_ptr<PopupMenu> pMenu = getMenu();
163 0 : sal_uInt16 nCount = pMenu->GetItemCount();
164 0 : for (sal_uInt16 i = 0; i < nCount; ++i)
165 : {
166 0 : sal_uInt16 nItemId = pMenu->GetItemId(i);
167 0 : aFind = m_aStates.find(pMenu->GetItemCommand(nItemId));
168 0 : if ( aFind != m_aStates.end() && aFind->second )
169 : {
170 0 : m_aCommandURL = aFind->first;
171 :
172 0 : ToolBox* pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
173 0 : lcl_copy(pMenu.get(),nItemId,i,pToolBox,m_nToolBoxId, m_aCommandURL);
174 0 : break;
175 : }
176 0 : }
177 : }
178 14 : }
179 14 : }
180 0 : ::std::unique_ptr<PopupMenu> OToolboxController::getMenu()
181 : {
182 0 : ::std::unique_ptr<PopupMenu> pMenu;
183 0 : if ( m_aStates.size() > 2 )
184 : {
185 0 : pMenu.reset( new PopupMenu( ModuleRes( RID_MENU_APP_NEW ) ) );
186 :
187 : try
188 : {
189 0 : Reference<XModuleUIConfigurationManagerSupplier> xModuleCfgMgrSupplier = theModuleUIConfigurationManagerSupplier::get( getContext() );
190 0 : Reference<XUIConfigurationManager> xUIConfigMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( OUString("com.sun.star.sdb.OfficeDatabaseDocument") );
191 0 : Reference<XImageManager> xImageMgr(xUIConfigMgr->getImageManager(),UNO_QUERY);
192 :
193 0 : Sequence< OUString> aSeq(1);
194 0 : sal_uInt16 nCount = pMenu->GetItemCount();
195 0 : for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
196 : {
197 0 : if ( pMenu->GetItemType( nPos ) == MenuItemType::SEPARATOR )
198 0 : continue;
199 :
200 0 : sal_uInt16 nItemId = pMenu->GetItemId(nPos);
201 0 : aSeq[0] = pMenu->GetItemCommand(nItemId);
202 0 : Sequence< Reference<XGraphic> > aImages = xImageMgr->getImages(ImageType::SIZE_DEFAULT, aSeq);
203 :
204 0 : Image aImage(aImages[0]);
205 0 : pMenu->SetItemImage(nItemId,aImage);
206 0 : TCommandState::iterator aFind = m_aStates.find( aSeq[0] );
207 0 : if ( aFind != m_aStates.end() )
208 : {
209 0 : pMenu->EnableItem(nItemId,aFind->second);
210 : }
211 0 : }
212 : }
213 0 : catch(const Exception&)
214 : {
215 : DBG_UNHANDLED_EXCEPTION();
216 : }
217 : }
218 : else
219 : {
220 0 : pMenu.reset( new PopupMenu( ModuleRes( RID_MENU_REFRESH_DATA ) ) );
221 : }
222 0 : return pMenu;
223 : }
224 :
225 0 : Reference< ::com::sun::star::awt::XWindow > SAL_CALL OToolboxController::createPopupWindow() throw (RuntimeException, std::exception)
226 : {
227 : // execute the menu
228 0 : SolarMutexGuard aSolarMutexGuard;
229 0 : ::osl::MutexGuard aGuard(m_aMutex);
230 :
231 0 : ToolBox* pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
232 0 : ::std::unique_ptr<PopupMenu> pMenu = getMenu();
233 :
234 0 : sal_uInt16 nSelected = pMenu->Execute(pToolBox, pToolBox->GetItemRect( m_nToolBoxId ),POPUPMENU_EXECUTE_DOWN);
235 : // "cleanup" the toolbox state
236 0 : Point aPoint = pToolBox->GetItemRect( m_nToolBoxId ).TopLeft();
237 0 : MouseEvent aLeave( aPoint, 0, MOUSE_LEAVEWINDOW | MOUSE_SYNTHETIC );
238 0 : pToolBox->MouseMove( aLeave );
239 0 : pToolBox->SetItemDown( m_nToolBoxId, false);
240 :
241 0 : if ( nSelected )
242 : {
243 0 : m_aCommandURL = pMenu->GetItemCommand(nSelected);
244 0 : lcl_copy(pMenu.get(),nSelected,pMenu->GetItemPos(nSelected),pToolBox,m_nToolBoxId, m_aCommandURL);
245 :
246 0 : Reference<XDispatch> xDispatch = m_aListenerMap.find(m_aCommandURL)->second;
247 0 : if ( xDispatch.is() )
248 : {
249 0 : URL aUrl;
250 0 : Sequence < PropertyValue > aArgs;
251 0 : aUrl.Complete = m_aCommandURL;
252 : OSL_ENSURE(!aUrl.Complete.isEmpty(),"Command is empty!");
253 0 : if ( getURLTransformer().is() )
254 0 : getURLTransformer()->parseStrict(aUrl);
255 0 : xDispatch->dispatch(aUrl,aArgs);
256 :
257 0 : }
258 : }
259 0 : return Reference< ::com::sun::star::awt::XWindow >();
260 : }
261 72 : } // dbaui
262 :
263 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|