Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / control / toolboxcontroller.cxx
blob2f8816e16aec64405ef5250b40752d4167943a4f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
42 extern "C" void SAL_CALL createRegistryInfo_OToolboxController()
44 static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::OToolboxController> aAutoRegistration;
47 namespace dbaui
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;
58 namespace
60 void lcl_copy(Menu* _pMenu,sal_uInt16 _nMenuId,sal_uInt16 _nMenuPos,ToolBox* _pToolBox,sal_uInt16 _nToolId,const OUString& _sCommand)
62 if ( _pMenu->GetItemType(_nMenuPos) != MenuItemType::STRING )
63 _pToolBox->SetItemImage(_nToolId, _pMenu->GetItemImage(_nMenuId));
64 _pToolBox->SetItemCommand( _nToolId, _sCommand);
65 _pToolBox->SetHelpId(_nToolId, _pMenu->GetHelpId(_nMenuId));
66 _pToolBox->SetHelpText(_nToolId, _pMenu->GetHelpText(_nMenuId));
67 _pToolBox->SetQuickHelpText(_nToolId, _pMenu->GetTipHelpText(_nMenuId));
68 _pToolBox->SetItemText(_nToolId, _pMenu->GetItemText(_nMenuId));
72 OToolboxController::OToolboxController(const Reference< XComponentContext >& _rxORB)
73 : m_nToolBoxId(1)
75 osl_atomic_increment(&m_refCount);
76 m_xContext = _rxORB;
77 osl_atomic_decrement(&m_refCount);
80 IMPLEMENT_SERVICE_INFO_IMPLNAME_STATIC(OToolboxController, "com.sun.star.sdb.ApplicationToolboxController")
81 IMPLEMENT_SERVICE_INFO_SUPPORTS(OToolboxController)
82 IMPLEMENT_SERVICE_INFO_GETSUPPORTED1_STATIC(OToolboxController, "com.sun.star.frame.ToolboxController")
84 Reference< XInterface >
85 SAL_CALL OToolboxController::Create(const Reference< XMultiServiceFactory >& _rxORB)
87 return static_cast< XServiceInfo* >(new OToolboxController( comphelper::getComponentContext(_rxORB) ));
90 // XInterface
91 Any SAL_CALL OToolboxController::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
93 Any aReturn = ToolboxController::queryInterface(_rType);
94 if (!aReturn.hasValue())
95 aReturn = TToolboxController_BASE::queryInterface(_rType);
96 return aReturn;
98 void SAL_CALL OToolboxController::acquire() throw ()
100 ToolboxController::acquire();
102 void SAL_CALL OToolboxController::release() throw ()
104 ToolboxController::release();
106 void SAL_CALL OToolboxController::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
108 ToolboxController::initialize(_rArguments);
109 SolarMutexGuard aSolarMutexGuard;
110 ::osl::MutexGuard aGuard(m_aMutex);
112 if ( m_aCommandURL == ".uno:DBNewForm" )
114 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewForm") ,true));
115 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewView") ,true));
116 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewViewSQL") ,true));
117 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewQuery") ,true));
118 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewQuerySql") ,true));
119 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewReport") ,true));
120 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewReportAutoPilot"),true));
121 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBNewTable") ,true));
123 else
125 m_aStates.insert(TCommandState::value_type(OUString(".uno:Refresh") ,true));
126 m_aStates.insert(TCommandState::value_type(OUString(".uno:DBRebuildData") ,true));
129 TCommandState::const_iterator aIter = m_aStates.begin();
130 TCommandState::const_iterator aEnd = m_aStates.end();
131 for (; aIter != aEnd; ++aIter)
132 addStatusListener(aIter->first);
134 VclPtr< ToolBox > pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()).get());
135 if ( pToolBox )
137 sal_uInt16 nCount = pToolBox->GetItemCount();
138 for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
140 sal_uInt16 nItemId = pToolBox->GetItemId(nPos);
141 if ( pToolBox->GetItemCommand(nItemId) == m_aCommandURL )
143 m_nToolBoxId = nItemId;
144 break;
148 // check if paste special is allowed, when not don't add DROPDOWN
149 pToolBox->SetItemBits(m_nToolBoxId,pToolBox->GetItemBits(m_nToolBoxId) | ToolBoxItemBits::DROPDOWN);
152 void SAL_CALL OToolboxController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
154 SolarMutexGuard aSolarMutexGuard;
155 ::osl::MutexGuard aGuard(m_aMutex);
156 TCommandState::iterator aFind = m_aStates.find( Event.FeatureURL.Complete );
157 if ( aFind != m_aStates.end() )
159 aFind->second = Event.IsEnabled;
160 if ( m_aCommandURL == aFind->first && !Event.IsEnabled )
162 ::std::unique_ptr<PopupMenu> pMenu = getMenu();
163 sal_uInt16 nCount = pMenu->GetItemCount();
164 for (sal_uInt16 i = 0; i < nCount; ++i)
166 sal_uInt16 nItemId = pMenu->GetItemId(i);
167 aFind = m_aStates.find(pMenu->GetItemCommand(nItemId));
168 if ( aFind != m_aStates.end() && aFind->second )
170 m_aCommandURL = aFind->first;
172 VclPtr< ToolBox > pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()).get());
173 lcl_copy(pMenu.get(),nItemId,i,pToolBox,m_nToolBoxId, m_aCommandURL);
174 break;
180 ::std::unique_ptr<PopupMenu> OToolboxController::getMenu()
182 ::std::unique_ptr<PopupMenu> pMenu;
183 if ( m_aStates.size() > 2 )
185 pMenu.reset( new PopupMenu( ModuleRes( RID_MENU_APP_NEW ) ) );
189 Reference<XModuleUIConfigurationManagerSupplier> xModuleCfgMgrSupplier = theModuleUIConfigurationManagerSupplier::get( getContext() );
190 Reference<XUIConfigurationManager> xUIConfigMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( "com.sun.star.sdb.OfficeDatabaseDocument" );
191 Reference<XImageManager> xImageMgr(xUIConfigMgr->getImageManager(),UNO_QUERY);
193 Sequence< OUString> aSeq(1);
194 sal_uInt16 nCount = pMenu->GetItemCount();
195 for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
197 if ( pMenu->GetItemType( nPos ) == MenuItemType::SEPARATOR )
198 continue;
200 sal_uInt16 nItemId = pMenu->GetItemId(nPos);
201 aSeq[0] = pMenu->GetItemCommand(nItemId);
202 Sequence< Reference<XGraphic> > aImages = xImageMgr->getImages(ImageType::SIZE_DEFAULT, aSeq);
204 Image aImage(aImages[0]);
205 pMenu->SetItemImage(nItemId,aImage);
206 TCommandState::const_iterator aFind = m_aStates.find( aSeq[0] );
207 if ( aFind != m_aStates.end() )
209 pMenu->EnableItem(nItemId,aFind->second);
213 catch(const Exception&)
215 DBG_UNHANDLED_EXCEPTION();
218 else
220 pMenu.reset( new PopupMenu( ModuleRes( RID_MENU_REFRESH_DATA ) ) );
222 return pMenu;
225 Reference< css::awt::XWindow > SAL_CALL OToolboxController::createPopupWindow() throw (RuntimeException, std::exception)
227 // execute the menu
228 SolarMutexGuard aSolarMutexGuard;
229 ::osl::MutexGuard aGuard(m_aMutex);
231 VclPtr< ToolBox > pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()).get());
232 ::std::unique_ptr<PopupMenu> pMenu = getMenu();
234 sal_uInt16 nSelected = pMenu->Execute(pToolBox, pToolBox->GetItemRect( m_nToolBoxId ),PopupMenuFlags::ExecuteDown);
235 // "cleanup" the toolbox state
236 Point aPoint = pToolBox->GetItemRect( m_nToolBoxId ).TopLeft();
237 MouseEvent aLeave( aPoint, 0, MouseEventModifiers::LEAVEWINDOW | MouseEventModifiers::SYNTHETIC );
238 pToolBox->MouseMove( aLeave );
239 pToolBox->SetItemDown( m_nToolBoxId, false);
241 if ( nSelected )
243 m_aCommandURL = pMenu->GetItemCommand(nSelected);
244 lcl_copy(pMenu.get(),nSelected,pMenu->GetItemPos(nSelected),pToolBox,m_nToolBoxId, m_aCommandURL);
246 Reference<XDispatch> xDispatch = m_aListenerMap.find(m_aCommandURL)->second;
247 if ( xDispatch.is() )
249 URL aUrl;
250 Sequence < PropertyValue > aArgs;
251 aUrl.Complete = m_aCommandURL;
252 OSL_ENSURE(!aUrl.Complete.isEmpty(),"Command is empty!");
253 if ( getURLTransformer().is() )
254 getURLTransformer()->parseStrict(aUrl);
255 xDispatch->dispatch(aUrl,aArgs);
259 return Reference< css::awt::XWindow >();
261 } // dbaui
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */