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 12 : extern "C" void SAL_CALL createRegistryInfo_OToolboxController()
43 : {
44 12 : static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::OToolboxController> aAutoRegistration;
45 12 : }
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 1 : OToolboxController::OToolboxController(const Reference< XComponentContext >& _rxORB)
73 1 : : m_nToolBoxId(1)
74 : {
75 1 : osl_atomic_increment(&m_refCount);
76 1 : m_xContext = _rxORB;
77 1 : osl_atomic_decrement(&m_refCount);
78 :
79 1 : }
80 24 : IMPLEMENT_SERVICE_INFO_IMPLNAME_STATIC(OToolboxController, "com.sun.star.sdb.ApplicationToolboxController")
81 0 : IMPLEMENT_SERVICE_INFO_SUPPORTS(OToolboxController)
82 12 : IMPLEMENT_SERVICE_INFO_GETSUPPORTED1_STATIC(OToolboxController, "com.sun.star.frame.ToolboxController")
83 :
84 : Reference< XInterface >
85 1 : SAL_CALL OToolboxController::Create(const Reference< XMultiServiceFactory >& _rxORB)
86 : {
87 1 : return static_cast< XServiceInfo* >(new OToolboxController( comphelper::getComponentContext(_rxORB) ));
88 : }
89 :
90 : // XInterface
91 30 : Any SAL_CALL OToolboxController::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
92 : {
93 30 : Any aReturn = ToolboxController::queryInterface(_rType);
94 30 : if (!aReturn.hasValue())
95 1 : aReturn = TToolboxController_BASE::queryInterface(_rType);
96 30 : return aReturn;
97 : }
98 126 : void SAL_CALL OToolboxController::acquire() throw ()
99 : {
100 126 : ToolboxController::acquire();
101 126 : }
102 126 : void SAL_CALL OToolboxController::release() throw ()
103 : {
104 126 : ToolboxController::release();
105 126 : }
106 1 : void SAL_CALL OToolboxController::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
107 : {
108 1 : ToolboxController::initialize(_rArguments);
109 1 : SolarMutexGuard aSolarMutexGuard;
110 2 : ::osl::MutexGuard aGuard(m_aMutex);
111 :
112 1 : if ( m_aCommandURL == ".uno:DBNewForm" )
113 : {
114 1 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewForm") ,sal_True));
115 1 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewView") ,sal_True));
116 1 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewViewSQL") ,sal_True));
117 1 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewQuery") ,sal_True));
118 1 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewQuerySql") ,sal_True));
119 1 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewReport") ,sal_True));
120 1 : m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewReportAutoPilot"),sal_True));
121 1 : 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 1 : TCommandState::iterator aIter = m_aStates.begin();
130 1 : TCommandState::iterator aEnd = m_aStates.end();
131 9 : for (; aIter != aEnd; ++aIter)
132 8 : addStatusListener(aIter->first);
133 :
134 2 : VclPtr< ToolBox > pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()).get());
135 1 : if ( pToolBox )
136 : {
137 1 : sal_uInt16 nCount = pToolBox->GetItemCount();
138 12 : for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
139 : {
140 12 : sal_uInt16 nItemId = pToolBox->GetItemId(nPos);
141 12 : if ( pToolBox->GetItemCommand(nItemId) == m_aCommandURL )
142 : {
143 1 : m_nToolBoxId = nItemId;
144 1 : break;
145 : }
146 : }
147 :
148 : // check if paste special is allowed, when not don't add DROPDOWN
149 1 : pToolBox->SetItemBits(m_nToolBoxId,pToolBox->GetItemBits(m_nToolBoxId) | ToolBoxItemBits::DROPDOWN);
150 1 : }
151 1 : }
152 7 : void SAL_CALL OToolboxController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
153 : {
154 7 : SolarMutexGuard aSolarMutexGuard;
155 14 : ::osl::MutexGuard aGuard(m_aMutex);
156 7 : TCommandState::iterator aFind = m_aStates.find( Event.FeatureURL.Complete );
157 7 : if ( aFind != m_aStates.end() )
158 : {
159 7 : aFind->second = Event.IsEnabled;
160 7 : 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 : VclPtr< ToolBox > pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()).get());
173 0 : lcl_copy(pMenu.get(),nItemId,i,pToolBox,m_nToolBoxId, m_aCommandURL);
174 0 : break;
175 : }
176 0 : }
177 : }
178 7 : }
179 7 : }
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 : VclPtr< ToolBox > pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()).get());
232 0 : ::std::unique_ptr<PopupMenu> pMenu = getMenu();
233 :
234 0 : sal_uInt16 nSelected = pMenu->Execute(pToolBox, pToolBox->GetItemRect( m_nToolBoxId ),PopupMenuFlags::ExecuteDown);
235 : // "cleanup" the toolbox state
236 0 : Point aPoint = pToolBox->GetItemRect( m_nToolBoxId ).TopLeft();
237 0 : MouseEvent aLeave( aPoint, 0, MouseEventModifiers::LEAVEWINDOW | MouseEventModifiers::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 : } // dbaui
262 :
263 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|