1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBACCESS_TOOLBOXCONTROLLER_HXX
31 #include "toolboxcontroller.hxx"
32 #endif
33 #ifndef _COM_SUN_STAR_UI_IMAGETYPE_HPP_
34 #include <com/sun/star/ui/ImageType.hpp>
35 #endif
36 #ifndef _COM_SUN_STAR_FRAME_XDISPATCHPROVIDER_HPP_
37 #include <com/sun/star/frame/XDispatchProvider.hpp>
38 #endif
39 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
40 #include <toolkit/helper/vclunohelper.hxx>
41 #endif
42 #ifndef _SV_MENU_HXX
43 #include <vcl/menu.hxx>
44 #endif
45 #ifndef _COM_SUN_STAR_UI_XUICONFIGURATIONMANAGER_HPP_
46 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_UI_XMODULEUICONFIGURATIONMANAGERSUPPLIER_HPP_
49 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_UI_XIMAGEMANAGER_HPP_
52 #include <com/sun/star/ui/XImageManager.hpp>
53 #endif
54 #ifndef _COM_SUN_STAR_UI_IMAGETYPE_HPP_
55 #include <com/sun/star/ui/ImageType.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_GRAPHIC_XGRAPHIC_HPP_
58 #include <com/sun/star/graphic/XGraphic.hpp>
59 #endif
60 #ifndef _SV_SVAPP_HXX //autogen
61 #include <vcl/svapp.hxx>
62 #endif
63 #ifndef _SV_TOOLBOX_HXX
64 #include <vcl/toolbox.hxx>
65 #endif
66 #ifndef _DBU_RESOURCE_HRC_
67 #include "dbu_resource.hrc"
68 #endif
69 #ifndef INCLUDED_SVTOOLS_MISCOPT_HXX
70 #include <svtools/miscopt.hxx>
71 #endif
72 #ifndef INCLUDED_SVTOOLS_MODULEOPTIONS_HXX
73 #include <unotools/moduleoptions.hxx>
74 #endif
75 #ifndef TOOLS_DIAGNOSE_EX_H
76 #include <tools/diagnose_ex.h>
77 #endif
78 #ifndef INCLUDED_SVTOOLS_MENUOPTIONS_HXX
79 #include <svtools/menuoptions.hxx>
80 #endif
81 #ifndef _VOS_MUTEX_HXX_
82 #include <vos/mutex.hxx>
83 #endif
84 #ifndef _DBU_REGHELPER_HXX_
85 #include "dbu_reghelper.hxx"
86 #endif
87 #ifndef DBAUI_TOOLS_HXX
88 #include "UITools.hxx"
89 #endif
90 
91 
92 extern "C" void SAL_CALL createRegistryInfo_OToolboxController()
93 {
94 	static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::OToolboxController> aAutoRegistration;
95 }
96 namespace dbaui
97 {
98 	using namespace svt;
99 	using namespace ::com::sun::star::graphic;
100 	using namespace com::sun::star::uno;
101 	using namespace com::sun::star::beans;
102 	using namespace com::sun::star::lang;
103 	using namespace ::com::sun::star::frame;
104 	using namespace ::com::sun::star::util;
105 	using namespace ::com::sun::star::ui;
106 
107     namespace
108     {
109         void lcl_copy(Menu* _pMenu,sal_uInt16 _nMenuId,sal_uInt16 _nMenuPos,ToolBox* _pToolBox,sal_uInt16 _nToolId,const ::rtl::OUString& _sCommand)
110         {
111             if ( _pMenu->GetItemType(_nMenuPos) != MENUITEM_STRING )
112                 _pToolBox->SetItemImage(_nToolId, _pMenu->GetItemImage(_nMenuId));
113             _pToolBox->SetItemCommand( _nToolId, _sCommand);
114             _pToolBox->SetHelpId(_nToolId, _pMenu->GetHelpId(_nMenuId));
115             _pToolBox->SetHelpText(_nToolId, _pMenu->GetHelpText(_nMenuId));
116             _pToolBox->SetQuickHelpText(_nToolId, _pMenu->GetTipHelpText(_nMenuId));
117             _pToolBox->SetItemText(_nToolId, _pMenu->GetItemText(_nMenuId));
118         }
119     }
120 
121 	OToolboxController::OToolboxController(const Reference< XMultiServiceFactory >& _rxORB)
122 		: m_nToolBoxId(1)
123 	{
124 		osl_incrementInterlockedCount(&m_refCount);
125 		m_xServiceManager = _rxORB;
126 		osl_decrementInterlockedCount(&m_refCount);
127 
128 	}
129 	// -----------------------------------------------------------------------------
130 	IMPLEMENT_SERVICE_INFO1_STATIC(OToolboxController,"com.sun.star.sdb.ApplicationToolboxController","com.sun.star.frame.ToolboxController")
131 	// -----------------------------------------------------------------------------
132 	// XInterface
133 	Any SAL_CALL OToolboxController::queryInterface( const Type& _rType ) throw (RuntimeException)
134 	{
135 		Any aReturn = ToolboxController::queryInterface(_rType);
136 		if (!aReturn.hasValue())
137 			aReturn = TToolboxController_BASE::queryInterface(_rType);
138 		return aReturn;
139 	}
140 	// -----------------------------------------------------------------------------
141 	void SAL_CALL OToolboxController::acquire() throw ()
142 	{
143 		ToolboxController::acquire();
144 	}
145 	// -----------------------------------------------------------------------------
146 	void SAL_CALL OToolboxController::release() throw ()
147 	{
148 		ToolboxController::release();
149 	}
150 	// -----------------------------------------------------------------------------
151 	void SAL_CALL OToolboxController::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
152 	{
153 		ToolboxController::initialize(_rArguments);
154 		vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
155 		::osl::MutexGuard aGuard(m_aMutex);
156 
157 		if ( m_aCommandURL.equalsAscii(".uno:DBNewForm") )
158 		{
159 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewForm"))           ,sal_True));
160 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewView"))           ,sal_True));
161 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewViewSQL"))        ,sal_True));
162 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewQuery"))          ,sal_True));
163 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewQuerySql"))       ,sal_True));
164 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewReport"))			,sal_True));
165 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewReportAutoPilot")),sal_True));
166 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBNewTable"))          ,sal_True));
167 		}
168 		else
169 		{
170 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:Refresh"))         ,sal_True));
171 			m_aStates.insert(TCommandState::value_type(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:DBRebuildData"))   ,sal_True));
172 		}
173 
174 		TCommandState::iterator aIter = m_aStates.begin();
175         TCommandState::iterator aEnd = m_aStates.end();
176 		for (; aIter != aEnd; ++aIter)
177 			addStatusListener(aIter->first);
178 
179 		ToolBox*    pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
180 		if ( pToolBox )
181 		{
182 			sal_uInt16 nCount = pToolBox->GetItemCount();
183 			for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
184 			{
185 				sal_uInt16 nItemId = pToolBox->GetItemId(nPos);
186 				if ( pToolBox->GetItemCommand(nItemId) == String(m_aCommandURL) )
187 				{
188 					m_nToolBoxId = nItemId;
189 					break;
190 				}
191 			}
192 
193             // check if paste special is allowed, when not don't add DROPDOWN
194 			pToolBox->SetItemBits(m_nToolBoxId,pToolBox->GetItemBits(m_nToolBoxId) | TIB_DROPDOWN);
195 		}
196 	}
197 	// -----------------------------------------------------------------------------
198 	void SAL_CALL OToolboxController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
199 	{
200         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
201 		::osl::MutexGuard aGuard(m_aMutex);
202 		TCommandState::iterator aFind = m_aStates.find( Event.FeatureURL.Complete );
203 		if ( aFind != m_aStates.end() )
204         {
205 			aFind->second = Event.IsEnabled;
206             if ( m_aCommandURL == aFind->first && !Event.IsEnabled )
207             {
208                 ::std::auto_ptr<PopupMenu> pMenu = getMenu();
209                 sal_uInt16 nCount = pMenu->GetItemCount();
210                 for (sal_uInt16 i = 0; i < nCount; ++i)
211                 {
212                     sal_uInt16 nItemId = pMenu->GetItemId(i);
213                     aFind = m_aStates.find(pMenu->GetItemCommand(nItemId));
214                     if ( aFind != m_aStates.end() && aFind->second )
215                     {
216                         m_aCommandURL = aFind->first;
217 
218                         ToolBox* pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
219                         lcl_copy(pMenu.get(),nItemId,i,pToolBox,m_nToolBoxId, m_aCommandURL);
220                         break;
221                     }
222                 }
223             }
224         }
225 	}
226     // -----------------------------------------------------------------------------
227     ::std::auto_ptr<PopupMenu> OToolboxController::getMenu()
228     {
229         ::std::auto_ptr<PopupMenu> pMenu;
230 		if ( m_aStates.size() > 2 )
231 		{
232 			pMenu.reset( new PopupMenu( ModuleRes( RID_MENU_APP_NEW ) ) );
233 
234 			sal_Bool bHighContrast = isHighContrast();
235 
236 			try
237 			{
238 				Reference<XModuleUIConfigurationManagerSupplier> xModuleCfgMgrSupplier(getServiceManager()->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ui.ModuleUIConfigurationManagerSupplier"))),UNO_QUERY);
239 				Reference<XUIConfigurationManager> xUIConfigMgr = xModuleCfgMgrSupplier->getUIConfigurationManager(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.OfficeDatabaseDocument")));
240 				Reference<XImageManager> xImageMgr(xUIConfigMgr->getImageManager(),UNO_QUERY);
241 
242 
243 				short nImageType = hasBigImages() ? ImageType::SIZE_LARGE : ImageType::SIZE_DEFAULT;
244 				if ( bHighContrast )
245 					nImageType |= ImageType::COLOR_HIGHCONTRAST;
246 
247 				Sequence< ::rtl::OUString> aSeq(1);
248 				sal_uInt16 nCount = pMenu->GetItemCount();
249 				for (sal_uInt16 nPos = 0; nPos < nCount; ++nPos)
250 				{
251                     if ( pMenu->GetItemType( nPos ) == MENUITEM_SEPARATOR )
252                         continue;
253 
254 					sal_uInt16 nItemId = pMenu->GetItemId(nPos);
255 					aSeq[0] = pMenu->GetItemCommand(nItemId);
256 					Sequence< Reference<XGraphic> > aImages = xImageMgr->getImages(nImageType,aSeq);
257 
258 					Image aImage(aImages[0]);
259 					pMenu->SetItemImage(nItemId,aImage);
260 					TCommandState::iterator aFind = m_aStates.find( aSeq[0] );
261 					if ( aFind != m_aStates.end() )
262 					{
263 						pMenu->EnableItem(nItemId,aFind->second);
264 					}
265 				}
266 			}
267 			catch(const Exception&)
268 			{
269                 DBG_UNHANDLED_EXCEPTION();
270 			}
271 		}
272 		else
273 		{
274 			pMenu.reset( new PopupMenu( ModuleRes( RID_MENU_REFRESH_DATA ) ) );
275 		}
276         return pMenu;
277     }
278 	// -----------------------------------------------------------------------------
279 	Reference< ::com::sun::star::awt::XWindow > SAL_CALL OToolboxController::createPopupWindow() throw (RuntimeException)
280 	{
281 		// execute the menu
282 		vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
283 		::osl::MutexGuard aGuard(m_aMutex);
284 
285 		ToolBox* pToolBox = static_cast<ToolBox*>(VCLUnoHelper::GetWindow(getParent()));
286         ::std::auto_ptr<PopupMenu> pMenu = getMenu();
287 
288 		sal_uInt16 nSelected = pMenu->Execute(pToolBox, pToolBox->GetItemRect( m_nToolBoxId ),POPUPMENU_EXECUTE_DOWN);
289 		// "cleanup" the toolbox state
290 		Point aPoint = pToolBox->GetItemRect( m_nToolBoxId ).TopLeft();
291 		MouseEvent aLeave( aPoint, 0, MOUSE_LEAVEWINDOW | MOUSE_SYNTHETIC );
292 		pToolBox->MouseMove( aLeave );
293 		pToolBox->SetItemDown( m_nToolBoxId, sal_False);
294 
295 		if ( nSelected )
296 		{
297             m_aCommandURL = pMenu->GetItemCommand(nSelected);
298             lcl_copy(pMenu.get(),nSelected,pMenu->GetItemPos(nSelected),pToolBox,m_nToolBoxId, m_aCommandURL);
299 
300 			Reference<XDispatch> xDispatch = m_aListenerMap.find(m_aCommandURL)->second;
301 			if ( xDispatch.is() )
302 			{
303 				URL aUrl;
304 				Sequence < PropertyValue > aArgs;
305 				aUrl.Complete = m_aCommandURL;
306 				OSL_ENSURE(aUrl.Complete.getLength(),"Command is empty!");
307 				if ( getURLTransformer().is() )
308 					getURLTransformer()->parseStrict(aUrl);
309 				xDispatch->dispatch(aUrl,aArgs);
310 
311 			}
312 		}
313 		return Reference< ::com::sun::star::awt::XWindow >();
314 	}
315 	// -----------------------------------------------------------------------------
316 	// -----------------------------------------------------------------------------
317 //..........................................................................
318 } // dbaui
319 //..........................................................................
320 
321 
322