Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / control / toolboxcontroller.cxx
blobcd01aa1527945e1cecd5b10172b69ed8ddefd2a5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "toolboxcontroller.hxx"
30 #include <com/sun/star/ui/ImageType.hpp>
31 #include <com/sun/star/frame/XDispatchProvider.hpp>
32 #include <toolkit/helper/vclunohelper.hxx>
33 #include <vcl/menu.hxx>
34 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
35 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
36 #include <com/sun/star/ui/XImageManager.hpp>
37 #include <com/sun/star/graphic/XGraphic.hpp>
38 #include <vcl/svapp.hxx>
39 #include <vcl/toolbox.hxx>
40 #include "dbu_resource.hrc"
41 #include <svtools/miscopt.hxx>
42 #include <unotools/moduleoptions.hxx>
43 #include <tools/diagnose_ex.h>
44 #include <svtools/menuoptions.hxx>
45 #include <osl/mutex.hxx>
46 #include "dbu_reghelper.hxx"
47 #include "UITools.hxx"
50 extern "C" void SAL_CALL createRegistryInfo_OToolboxController()
52 static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::OToolboxController> aAutoRegistration;
54 namespace dbaui
56 using namespace svt;
57 using namespace ::com::sun::star::graphic;
58 using namespace com::sun::star::uno;
59 using namespace com::sun::star::beans;
60 using namespace com::sun::star::lang;
61 using namespace ::com::sun::star::frame;
62 using namespace ::com::sun::star::util;
63 using namespace ::com::sun::star::ui;
65 namespace
67 void lcl_copy(Menu* _pMenu,sal_uInt16 _nMenuId,sal_uInt16 _nMenuPos,ToolBox* _pToolBox,sal_uInt16 _nToolId,const ::rtl::OUString& _sCommand)
69 if ( _pMenu->GetItemType(_nMenuPos) != MENUITEM_STRING )
70 _pToolBox->SetItemImage(_nToolId, _pMenu->GetItemImage(_nMenuId));
71 _pToolBox->SetItemCommand( _nToolId, _sCommand);
72 _pToolBox->SetHelpId(_nToolId, _pMenu->GetHelpId(_nMenuId));
73 _pToolBox->SetHelpText(_nToolId, _pMenu->GetHelpText(_nMenuId));
74 _pToolBox->SetQuickHelpText(_nToolId, _pMenu->GetTipHelpText(_nMenuId));
75 _pToolBox->SetItemText(_nToolId, _pMenu->GetItemText(_nMenuId));
79 OToolboxController::OToolboxController(const Reference< XMultiServiceFactory >& _rxORB)
80 : m_nToolBoxId(1)
82 osl_incrementInterlockedCount(&m_refCount);
83 m_xServiceManager = _rxORB;
84 osl_decrementInterlockedCount(&m_refCount);
87 // -----------------------------------------------------------------------------
88 IMPLEMENT_SERVICE_INFO1_STATIC(OToolboxController,"com.sun.star.sdb.ApplicationToolboxController","com.sun.star.frame.ToolboxController")
89 // -----------------------------------------------------------------------------
90 // XInterface
91 Any SAL_CALL OToolboxController::queryInterface( const Type& _rType ) throw (RuntimeException)
93 Any aReturn = ToolboxController::queryInterface(_rType);
94 if (!aReturn.hasValue())
95 aReturn = TToolboxController_BASE::queryInterface(_rType);
96 return aReturn;
98 // -----------------------------------------------------------------------------
99 void SAL_CALL OToolboxController::acquire() throw ()
101 ToolboxController::acquire();
103 // -----------------------------------------------------------------------------
104 void SAL_CALL OToolboxController::release() throw ()
106 ToolboxController::release();
108 // -----------------------------------------------------------------------------
109 void SAL_CALL OToolboxController::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
111 ToolboxController::initialize(_rArguments);
112 SolarMutexGuard aSolarMutexGuard;
113 ::osl::MutexGuard aGuard(m_aMutex);
115 if ( m_aCommandURL == ".uno:DBNewForm" )
117 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewForm")) ,sal_True));
118 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewView")) ,sal_True));
119 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewViewSQL")) ,sal_True));
120 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewQuery")) ,sal_True));
121 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewQuerySql")) ,sal_True));
122 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewReport")) ,sal_True));
123 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewReportAutoPilot")),sal_True));
124 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewTable")) ,sal_True));
126 else
128 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:Refresh")) ,sal_True));
129 m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBRebuildData")) ,sal_True));
132 TCommandState::iterator aIter = m_aStates.begin();
133 TCommandState::iterator aEnd = m_aStates.end();
134 for (; aIter != aEnd; ++aIter)
135 addStatusListener(aIter->first);
137 ToolBox* pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
138 if ( pToolBox )
140 sal_uInt16 nCount = pToolBox->GetItemCount();
141 for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
143 sal_uInt16 nItemId = pToolBox->GetItemId(nPos);
144 if ( pToolBox->GetItemCommand(nItemId) == String(m_aCommandURL) )
146 m_nToolBoxId = nItemId;
147 break;
151 // check if paste special is allowed, when not don't add DROPDOWN
152 pToolBox->SetItemBits(m_nToolBoxId,pToolBox->GetItemBits(m_nToolBoxId) | TIB_DROPDOWN);
155 // -----------------------------------------------------------------------------
156 void SAL_CALL OToolboxController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
158 SolarMutexGuard aSolarMutexGuard;
159 ::osl::MutexGuard aGuard(m_aMutex);
160 TCommandState::iterator aFind = m_aStates.find( Event.FeatureURL.Complete );
161 if ( aFind != m_aStates.end() )
163 aFind->second = Event.IsEnabled;
164 if ( m_aCommandURL == aFind->first && !Event.IsEnabled )
166 SAL_WNODEPRECATED_DECLARATIONS_PUSH
167 ::std::auto_ptr<PopupMenu> pMenu = getMenu();
168 SAL_WNODEPRECATED_DECLARATIONS_POP
169 sal_uInt16 nCount = pMenu->GetItemCount();
170 for (sal_uInt16 i = 0; i < nCount; ++i)
172 sal_uInt16 nItemId = pMenu->GetItemId(i);
173 aFind = m_aStates.find(pMenu->GetItemCommand(nItemId));
174 if ( aFind != m_aStates.end() && aFind->second )
176 m_aCommandURL = aFind->first;
178 ToolBox* pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
179 lcl_copy(pMenu.get(),nItemId,i,pToolBox,m_nToolBoxId, m_aCommandURL);
180 break;
186 // -----------------------------------------------------------------------------
187 SAL_WNODEPRECATED_DECLARATIONS_PUSH
188 ::std::auto_ptr<PopupMenu> OToolboxController::getMenu()
190 ::std::auto_ptr<PopupMenu> pMenu;
191 if ( m_aStates.size() > 2 )
193 pMenu.reset( new PopupMenu( ModuleRes( RID_MENU_APP_NEW ) ) );
197 Reference<XModuleUIConfigurationManagerSupplier> xModuleCfgMgrSupplier(getServiceManager()->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ui.ModuleUIConfigurationManagerSupplier"))),UNO_QUERY);
198 Reference<XUIConfigurationManager> xUIConfigMgr = xModuleCfgMgrSupplier->getUIConfigurationManager(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.OfficeDatabaseDocument")));
199 Reference<XImageManager> xImageMgr(xUIConfigMgr->getImageManager(),UNO_QUERY);
202 short nImageType = hasBigImages() ? ImageType::SIZE_LARGE : ImageType::SIZE_DEFAULT;
204 Sequence< ::rtl::OUString> aSeq(1);
205 sal_uInt16 nCount = pMenu->GetItemCount();
206 for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
208 if ( pMenu->GetItemType( nPos ) == MENUITEM_SEPARATOR )
209 continue;
211 sal_uInt16 nItemId = pMenu->GetItemId(nPos);
212 aSeq[0] = pMenu->GetItemCommand(nItemId);
213 Sequence< Reference<XGraphic> > aImages = xImageMgr->getImages(nImageType,aSeq);
215 Image aImage(aImages[0]);
216 pMenu->SetItemImage(nItemId,aImage);
217 TCommandState::iterator aFind = m_aStates.find( aSeq[0] );
218 if ( aFind != m_aStates.end() )
220 pMenu->EnableItem(nItemId,aFind->second);
224 catch(const Exception&)
226 DBG_UNHANDLED_EXCEPTION();
229 else
231 pMenu.reset( new PopupMenu( ModuleRes( RID_MENU_REFRESH_DATA ) ) );
233 return pMenu;
235 SAL_WNODEPRECATED_DECLARATIONS_POP
236 // -----------------------------------------------------------------------------
237 Reference< ::com::sun::star::awt::XWindow > SAL_CALL OToolboxController::createPopupWindow() throw (RuntimeException)
239 // execute the menu
240 SolarMutexGuard aSolarMutexGuard;
241 ::osl::MutexGuard aGuard(m_aMutex);
243 ToolBox* pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
244 SAL_WNODEPRECATED_DECLARATIONS_PUSH
245 ::std::auto_ptr<PopupMenu> pMenu = getMenu();
246 SAL_WNODEPRECATED_DECLARATIONS_POP
248 sal_uInt16 nSelected = pMenu->Execute(pToolBox, pToolBox->GetItemRect( m_nToolBoxId ),POPUPMENU_EXECUTE_DOWN);
249 // "cleanup" the toolbox state
250 Point aPoint = pToolBox->GetItemRect( m_nToolBoxId ).TopLeft();
251 MouseEvent aLeave( aPoint, 0, MOUSE_LEAVEWINDOW | MOUSE_SYNTHETIC );
252 pToolBox->MouseMove( aLeave );
253 pToolBox->SetItemDown( m_nToolBoxId, sal_False);
255 if ( nSelected )
257 m_aCommandURL = pMenu->GetItemCommand(nSelected);
258 lcl_copy(pMenu.get(),nSelected,pMenu->GetItemPos(nSelected),pToolBox,m_nToolBoxId, m_aCommandURL);
260 Reference<XDispatch> xDispatch = m_aListenerMap.find(m_aCommandURL)->second;
261 if ( xDispatch.is() )
263 URL aUrl;
264 Sequence < PropertyValue > aArgs;
265 aUrl.Complete = m_aCommandURL;
266 OSL_ENSURE(!aUrl.Complete.isEmpty(),"Command is empty!");
267 if ( getURLTransformer().is() )
268 getURLTransformer()->parseStrict(aUrl);
269 xDispatch->dispatch(aUrl,aArgs);
273 return Reference< ::com::sun::star::awt::XWindow >();
275 // -----------------------------------------------------------------------------
276 // -----------------------------------------------------------------------------
277 //..........................................................................
278 } // dbaui
279 //..........................................................................
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */