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