Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <svtools/contextmenuhelper.hxx>
31 : : #include <svtools/menuoptions.hxx>
32 : : #include <svtools/miscopt.hxx>
33 : :
34 : : #include <com/sun/star/frame/XDispatch.hpp>
35 : : #include <com/sun/star/frame/XDispatchProvider.hpp>
36 : : #include <com/sun/star/frame/XModuleManager.hpp>
37 : : #include <com/sun/star/frame/XStatusListener.hpp>
38 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 : : #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
40 : : #include <com/sun/star/ui/XUIConfigurationManager.hpp>
41 : : #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
42 : : #include <com/sun/star/ui/ImageType.hpp>
43 : : #include <com/sun/star/util/URLTransformer.hpp>
44 : : #include <com/sun/star/beans/PropertyValue.hpp>
45 : :
46 : : #include <osl/conditn.hxx>
47 : : #include <cppuhelper/weak.hxx>
48 : : #include <comphelper/processfactory.hxx>
49 : : #include <osl/mutex.hxx>
50 : : #include <vcl/svapp.hxx>
51 : : #include <vcl/image.hxx>
52 : : #include <toolkit/unohlp.hxx>
53 : : #include <toolkit/awt/vclxwindow.hxx>
54 : : #include <toolkit/awt/vclxmenu.hxx>
55 : :
56 : : using namespace ::com::sun::star;
57 : :
58 : : namespace svt
59 : : {
60 : :
61 : : // internal helper class to retrieve status updates
62 : : class StateEventHelper : public ::com::sun::star::frame::XStatusListener,
63 : : public ::cppu::OWeakObject
64 : : {
65 : : public:
66 : : StateEventHelper( const uno::Reference< frame::XDispatchProvider >& xDispatchProvider,
67 : : const uno::Reference< util::XURLTransformer >& xURLTransformer,
68 : : const rtl::OUString& aCommandURL );
69 : : virtual ~StateEventHelper();
70 : :
71 : : bool isCommandEnabled();
72 : :
73 : : // XInterface
74 : : virtual uno::Any SAL_CALL queryInterface( const uno::Type& aType ) throw ( uno::RuntimeException);
75 : : virtual void SAL_CALL acquire() throw ();
76 : : virtual void SAL_CALL release() throw ();
77 : :
78 : : // XEventListener
79 : : virtual void SAL_CALL disposing(const lang::EventObject& Source) throw( uno::RuntimeException );
80 : :
81 : : // XStatusListener
82 : : virtual void SAL_CALL statusChanged(const frame::FeatureStateEvent& Event) throw( uno::RuntimeException );
83 : :
84 : : private:
85 : : StateEventHelper();
86 : : StateEventHelper( const StateEventHelper& );
87 : : StateEventHelper& operator=( const StateEventHelper& );
88 : :
89 : : bool m_bCurrentCommandEnabled;
90 : : ::rtl::OUString m_aCommandURL;
91 : : uno::Reference< frame::XDispatchProvider > m_xDispatchProvider;
92 : : uno::Reference< util::XURLTransformer > m_xURLTransformer;
93 : : osl::Condition m_aCondition;
94 : : };
95 : :
96 : 0 : StateEventHelper::StateEventHelper(
97 : : const uno::Reference< frame::XDispatchProvider >& xDispatchProvider,
98 : : const uno::Reference< util::XURLTransformer >& xURLTransformer,
99 : : const rtl::OUString& rCommandURL ) :
100 : : m_bCurrentCommandEnabled( true ),
101 : : m_aCommandURL( rCommandURL ),
102 : : m_xDispatchProvider( xDispatchProvider ),
103 [ # # ]: 0 : m_xURLTransformer( xURLTransformer )
104 : : {
105 [ # # ]: 0 : m_aCondition.reset();
106 : 0 : }
107 : :
108 [ # # ][ # # ]: 0 : StateEventHelper::~StateEventHelper()
109 [ # # ]: 0 : {}
110 : :
111 : 0 : uno::Any SAL_CALL StateEventHelper::queryInterface(
112 : : const uno::Type& aType )
113 : : throw ( uno::RuntimeException )
114 : : {
115 : : uno::Any a = ::cppu::queryInterface(
116 : : aType,
117 [ # # ]: 0 : (static_cast< XStatusListener* >(this)));
118 : :
119 [ # # ]: 0 : if( a.hasValue() )
120 : 0 : return a;
121 : :
122 [ # # ]: 0 : return ::cppu::OWeakObject::queryInterface( aType );
123 : : }
124 : :
125 : 0 : void SAL_CALL StateEventHelper::acquire()
126 : : throw ()
127 : : {
128 : 0 : ::cppu::OWeakObject::acquire();
129 : 0 : }
130 : :
131 : 0 : void SAL_CALL StateEventHelper::release()
132 : : throw ()
133 : : {
134 : 0 : ::cppu::OWeakObject::release();
135 : 0 : }
136 : :
137 : 0 : void SAL_CALL StateEventHelper::disposing(
138 : : const lang::EventObject& )
139 : : throw ( uno::RuntimeException )
140 : : {
141 [ # # ]: 0 : SolarMutexGuard aSolarGuard;
142 : 0 : m_xDispatchProvider.clear();
143 : 0 : m_xURLTransformer.clear();
144 [ # # ][ # # ]: 0 : m_aCondition.set();
145 : 0 : }
146 : :
147 : 0 : void SAL_CALL StateEventHelper::statusChanged(
148 : : const frame::FeatureStateEvent& Event )
149 : : throw ( uno::RuntimeException )
150 : : {
151 [ # # ]: 0 : SolarMutexGuard aSolarGuard;
152 : 0 : m_bCurrentCommandEnabled = Event.IsEnabled;
153 [ # # ][ # # ]: 0 : m_aCondition.set();
154 : 0 : }
155 : :
156 : 0 : bool StateEventHelper::isCommandEnabled()
157 : : {
158 : : // Be sure that we cannot die during condition wait
159 : : uno::Reference< frame::XStatusListener > xSelf(
160 [ # # ]: 0 : (static_cast< frame::XStatusListener* >(this)));
161 : :
162 : 0 : uno::Reference< frame::XDispatch > xDispatch;
163 : 0 : util::URL aTargetURL;
164 : : {
165 [ # # ]: 0 : SolarMutexGuard aSolarGuard;
166 [ # # ][ # # ]: 0 : if ( m_xDispatchProvider.is() && m_xURLTransformer.is() )
[ # # ]
167 : : {
168 [ # # ]: 0 : ::rtl::OUString aSelf( RTL_CONSTASCII_USTRINGPARAM( "_self" ));
169 : :
170 : 0 : aTargetURL.Complete = m_aCommandURL;
171 [ # # ][ # # ]: 0 : m_xURLTransformer->parseStrict( aTargetURL );
172 : :
173 : : try
174 : : {
175 [ # # ][ # # ]: 0 : xDispatch = m_xDispatchProvider->queryDispatch( aTargetURL, aSelf, 0 );
[ # # ]
176 : : }
177 [ # # # ]: 0 : catch ( uno::RuntimeException& )
178 : : {
179 : 0 : throw;
180 : : }
181 [ # # ]: 0 : catch ( uno::Exception& )
182 : : {
183 : 0 : }
184 [ # # ]: 0 : }
185 : : }
186 : :
187 : 0 : bool bResult( false );
188 [ # # ]: 0 : if ( xDispatch.is() )
189 : : {
190 : : try
191 : : {
192 : : // add/remove ourself to retrieve status by callback
193 [ # # ][ # # ]: 0 : xDispatch->addStatusListener( xSelf, aTargetURL );
194 [ # # ][ # # ]: 0 : xDispatch->removeStatusListener( xSelf, aTargetURL );
195 : :
196 : : // wait for anwser
197 [ # # ]: 0 : m_aCondition.wait();
198 : : }
199 [ # # # ]: 0 : catch ( uno::RuntimeException& )
200 : : {
201 : 0 : throw;
202 : : }
203 [ # # ]: 0 : catch ( uno::Exception& )
204 : : {
205 : : }
206 : :
207 [ # # ]: 0 : SolarMutexGuard aSolarGuard;
208 [ # # ]: 0 : bResult = m_bCurrentCommandEnabled;
209 : : }
210 : :
211 : 0 : return bResult;
212 : : }
213 : :
214 : : /*************************************************************************/
215 : :
216 [ # # ][ # # ]: 0 : struct ExecuteInfo
217 : : {
218 : : uno::Reference< frame::XDispatch > xDispatch;
219 : : util::URL aTargetURL;
220 : : uno::Sequence< beans::PropertyValue > aArgs;
221 : : };
222 : :
223 : 0 : static const PopupMenu* lcl_FindPopupFromItemId( const PopupMenu* pPopupMenu, sal_uInt16 nItemId )
224 : : {
225 [ # # ]: 0 : if ( pPopupMenu )
226 : : {
227 : 0 : sal_uInt16 nCount = pPopupMenu->GetItemCount();
228 [ # # ]: 0 : for ( sal_uInt16 i = 0; i < nCount; i++ )
229 : : {
230 : 0 : sal_uInt16 nId = pPopupMenu->GetItemId( i );
231 [ # # ]: 0 : if ( nId == nItemId )
232 : 0 : return pPopupMenu;
233 : : else
234 : : {
235 : 0 : const PopupMenu* pResult( 0 );
236 : :
237 : 0 : const PopupMenu* pSubPopup = pPopupMenu->GetPopupMenu( i );
238 [ # # ]: 0 : if ( pPopupMenu )
239 : 0 : pResult = lcl_FindPopupFromItemId( pSubPopup, nItemId );
240 [ # # ]: 0 : if ( pResult != 0 )
241 : 0 : return pResult;
242 : : }
243 : : }
244 : : }
245 : :
246 : 0 : return NULL;
247 : : }
248 : :
249 : 0 : static ::rtl::OUString lcl_GetItemCommandRecursive( const PopupMenu* pPopupMenu, sal_uInt16 nItemId )
250 : : {
251 : 0 : const PopupMenu* pPopup = lcl_FindPopupFromItemId( pPopupMenu, nItemId );
252 [ # # ]: 0 : if ( pPopup )
253 : 0 : return pPopup->GetItemCommand( nItemId );
254 : : else
255 : 0 : return ::rtl::OUString();
256 : : }
257 : :
258 : : /*************************************************************************/
259 : :
260 : 0 : ContextMenuHelper::ContextMenuHelper(
261 : : const uno::Reference< frame::XFrame >& xFrame,
262 : : bool bAutoRefresh ) :
263 : : m_xWeakFrame( xFrame ),
264 : : m_aSelf( RTL_CONSTASCII_USTRINGPARAM( "_self" )),
265 : : m_bAutoRefresh( bAutoRefresh ),
266 [ # # ][ # # ]: 0 : m_bUICfgMgrAssociated( false )
267 : : {
268 : 0 : }
269 : :
270 [ # # ]: 0 : ContextMenuHelper::~ContextMenuHelper()
271 : : {
272 : 0 : }
273 : :
274 : : void
275 : 0 : ContextMenuHelper::completeAndExecute(
276 : : const Point& aPos,
277 : : PopupMenu& rPopupMenu )
278 : : {
279 [ # # ]: 0 : SolarMutexGuard aSolarGuard;
280 : :
281 [ # # ]: 0 : associateUIConfigurationManagers();
282 [ # # ]: 0 : completeMenuProperties( &rPopupMenu );
283 [ # # ]: 0 : executePopupMenu( aPos, &rPopupMenu );
284 [ # # ]: 0 : resetAssociations();
285 : 0 : }
286 : :
287 : : void
288 : 0 : ContextMenuHelper::completeAndExecute(
289 : : const Point& aPos,
290 : : const uno::Reference< awt::XPopupMenu >& xPopupMenu )
291 : : {
292 [ # # ]: 0 : SolarMutexGuard aSolarGuard;
293 : :
294 : 0 : VCLXMenu* pXMenu = VCLXMenu::GetImplementation( xPopupMenu );
295 [ # # ]: 0 : if ( pXMenu )
296 : : {
297 [ # # ]: 0 : PopupMenu* pPopupMenu = dynamic_cast< PopupMenu* >( pXMenu->GetMenu() );
298 : : // as dynamic_cast can return zero check pointer
299 [ # # ]: 0 : if ( pPopupMenu )
300 : : {
301 [ # # ]: 0 : associateUIConfigurationManagers();
302 [ # # ]: 0 : completeMenuProperties( pPopupMenu );
303 [ # # ]: 0 : executePopupMenu( aPos, pPopupMenu );
304 : 0 : resetAssociations();
305 : : }
306 [ # # ]: 0 : }
307 : 0 : }
308 : :
309 : : // private member
310 : :
311 : : void
312 : 0 : ContextMenuHelper::executePopupMenu(
313 : : const Point& rPos,
314 : : PopupMenu* pMenu )
315 : : {
316 [ # # ]: 0 : if ( pMenu )
317 : : {
318 [ # # ]: 0 : uno::Reference< frame::XFrame > xFrame( m_xWeakFrame );
319 [ # # ]: 0 : if ( xFrame.is() )
320 : : {
321 [ # # ][ # # ]: 0 : uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow() );
322 [ # # ]: 0 : if ( xWindow.is() )
323 : : {
324 [ # # ]: 0 : Window* pParent = VCLUnoHelper::GetWindow( xWindow );
325 [ # # ]: 0 : sal_uInt16 nResult = pMenu->Execute( pParent, rPos );
326 : :
327 [ # # ]: 0 : if ( nResult > 0 )
328 : : {
329 [ # # ]: 0 : ::rtl::OUString aCommand = lcl_GetItemCommandRecursive( pMenu, nResult );
330 [ # # ]: 0 : if ( !aCommand.isEmpty() )
331 [ # # ]: 0 : dispatchCommand( xFrame, aCommand );
332 : : }
333 : 0 : }
334 : 0 : }
335 : : }
336 : 0 : }
337 : :
338 : : bool
339 : 0 : ContextMenuHelper::dispatchCommand(
340 : : const uno::Reference< ::frame::XFrame >& rFrame,
341 : : const ::rtl::OUString& aCommandURL )
342 : : {
343 [ # # ]: 0 : if ( !m_xURLTransformer.is() )
344 : : {
345 [ # # ][ # # ]: 0 : m_xURLTransformer = util::URLTransformer::create( ::comphelper::getProcessComponentContext() );
[ # # ]
346 : : }
347 : :
348 : 0 : util::URL aTargetURL;
349 : 0 : aTargetURL.Complete = aCommandURL;
350 [ # # ][ # # ]: 0 : m_xURLTransformer->parseStrict( aTargetURL );
351 : :
352 : 0 : uno::Reference< frame::XDispatch > xDispatch;
353 : : uno::Reference< frame::XDispatchProvider > xDispatchProvider(
354 [ # # ]: 0 : rFrame, uno::UNO_QUERY );
355 [ # # ]: 0 : if ( xDispatchProvider.is() )
356 : : {
357 : : try
358 : : {
359 [ # # ][ # # ]: 0 : xDispatch = xDispatchProvider->queryDispatch( aTargetURL, m_aSelf, 0 );
[ # # ]
360 : : }
361 [ # # # ]: 0 : catch ( uno::RuntimeException& )
362 : : {
363 : 0 : throw;
364 : : }
365 [ # # ]: 0 : catch ( uno::Exception& )
366 : : {
367 : : }
368 : : }
369 : :
370 [ # # ]: 0 : if ( xDispatch.is() )
371 : : {
372 [ # # ][ # # ]: 0 : ExecuteInfo* pExecuteInfo = new ExecuteInfo;
373 [ # # ]: 0 : pExecuteInfo->xDispatch = xDispatch;
374 : 0 : pExecuteInfo->aTargetURL = aTargetURL;
375 [ # # ]: 0 : pExecuteInfo->aArgs = m_aDefaultArgs;
376 : :
377 [ # # ][ # # ]: 0 : Application::PostUserEvent( STATIC_LINK(0, ContextMenuHelper , ExecuteHdl_Impl), pExecuteInfo );
378 : 0 : return true;
379 : : }
380 : :
381 : 0 : return false;
382 : : }
383 : :
384 : : // retrieves and stores references to our user-interface
385 : : // configuration managers, like image manager, ui command
386 : : // description manager.
387 : : bool
388 : 0 : ContextMenuHelper::associateUIConfigurationManagers()
389 : : {
390 [ # # ]: 0 : uno::Reference< frame::XFrame > xFrame( m_xWeakFrame );
391 [ # # ][ # # ]: 0 : if ( !m_bUICfgMgrAssociated && xFrame.is() )
[ # # ]
392 : : {
393 : : // clear current state
394 : 0 : m_xDocImageMgr.clear();
395 : 0 : m_xModuleImageMgr.clear();
396 : 0 : m_xUICommandLabels.clear();
397 : :
398 : : try
399 : : {
400 : 0 : uno::Reference < frame::XController > xController;
401 : 0 : uno::Reference < frame::XModel > xModel;
402 [ # # ][ # # ]: 0 : xController = xFrame->getController();
[ # # ]
403 [ # # ]: 0 : if ( xController.is() )
404 [ # # ][ # # ]: 0 : xModel = xController->getModel();
[ # # ]
405 : :
406 [ # # ]: 0 : if ( xModel.is() )
407 : : {
408 : : // retrieve document image manager form model
409 [ # # ]: 0 : uno::Reference< ui::XUIConfigurationManagerSupplier > xSupplier( xModel, uno::UNO_QUERY );
410 [ # # ]: 0 : if ( xSupplier.is() )
411 : : {
412 : : uno::Reference< ui::XUIConfigurationManager > xDocUICfgMgr(
413 [ # # ][ # # ]: 0 : xSupplier->getUIConfigurationManager(), uno::UNO_QUERY );
[ # # ]
414 : : m_xDocImageMgr = uno::Reference< ui::XImageManager >(
415 [ # # ][ # # ]: 0 : xDocUICfgMgr->getImageManager(), uno::UNO_QUERY );
[ # # ][ # # ]
416 : 0 : }
417 : : }
418 : :
419 : : uno::Reference< frame::XModuleManager > xModuleManager(
420 [ # # ][ # # ]: 0 : ::comphelper::getProcessServiceFactory()->createInstance(
421 : : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
422 : 0 : "com.sun.star.frame.ModuleManager" ))),
423 [ # # ][ # # ]: 0 : uno::UNO_QUERY );
[ # # ]
424 : :
425 : 0 : uno::Reference< ui::XImageManager > xModuleImageManager;
426 : 0 : rtl::OUString aModuleId;
427 [ # # ]: 0 : if ( xModuleManager.is() )
428 : : {
429 : : // retrieve module image manager
430 [ # # ][ # # ]: 0 : aModuleId = xModuleManager->identify( xFrame );
431 : :
432 : : uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier(
433 [ # # ][ # # ]: 0 : ::comphelper::getProcessServiceFactory()->createInstance(
434 : : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
435 : 0 : "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ))),
436 [ # # ][ # # ]: 0 : uno::UNO_QUERY );
[ # # ]
437 [ # # ]: 0 : if ( xModuleCfgMgrSupplier.is() )
438 : : {
439 : : uno::Reference< ui::XUIConfigurationManager > xUICfgMgr(
440 [ # # ][ # # ]: 0 : xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId ));
441 [ # # ]: 0 : if ( xUICfgMgr.is() )
442 : : {
443 : : m_xModuleImageMgr = uno::Reference< ui::XImageManager >(
444 [ # # ][ # # ]: 0 : xUICfgMgr->getImageManager(), uno::UNO_QUERY );
[ # # ][ # # ]
445 : 0 : }
446 : 0 : }
447 : : }
448 : :
449 : : uno::Reference< container::XNameAccess > xNameAccess(
450 [ # # ][ # # ]: 0 : ::comphelper::getProcessServiceFactory()->createInstance(
451 : : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
452 : 0 : "com.sun.star.frame.UICommandDescription" ))),
453 [ # # ][ # # ]: 0 : uno::UNO_QUERY );
[ # # ]
454 [ # # ]: 0 : if ( xNameAccess.is() )
455 : : {
456 : : try
457 : : {
458 [ # # ][ # # ]: 0 : uno::Any a = xNameAccess->getByName( aModuleId );
459 [ # # ][ # # ]: 0 : a >>= m_xUICommandLabels;
460 : : }
461 [ # # ]: 0 : catch ( container::NoSuchElementException& )
462 : : {
463 : : }
464 : 0 : }
465 : : }
466 [ # # # ]: 0 : catch ( uno::RuntimeException& )
467 : : {
468 : 0 : throw;
469 : : }
470 [ # # ]: 0 : catch ( uno::Exception& )
471 : : {
472 : 0 : m_bUICfgMgrAssociated = true;
473 : 0 : return false;
474 : : }
475 : 0 : m_bUICfgMgrAssociated = true;
476 : : }
477 : :
478 : 0 : return true;
479 : : }
480 : :
481 : : Image
482 : 0 : ContextMenuHelper::getImageFromCommandURL( const ::rtl::OUString& aCmdURL ) const
483 : : {
484 [ # # ]: 0 : Image aImage;
485 : : sal_Int16 nImageType( ui::ImageType::COLOR_NORMAL|
486 : 0 : ui::ImageType::SIZE_DEFAULT );
487 : :
488 [ # # ]: 0 : uno::Sequence< uno::Reference< graphic::XGraphic > > aGraphicSeq;
489 [ # # ]: 0 : uno::Sequence< ::rtl::OUString > aImageCmdSeq( 1 );
490 [ # # ]: 0 : aImageCmdSeq[0] = aCmdURL;
491 : :
492 [ # # ]: 0 : if ( m_xDocImageMgr.is() )
493 : : {
494 : : try
495 : : {
496 [ # # ][ # # ]: 0 : aGraphicSeq = m_xDocImageMgr->getImages( nImageType, aImageCmdSeq );
[ # # ][ # # ]
497 [ # # ]: 0 : uno::Reference< graphic::XGraphic > xGraphic = aGraphicSeq[0];
498 [ # # ][ # # ]: 0 : aImage = Image( xGraphic );
[ # # ]
499 : :
500 [ # # ]: 0 : if ( !!aImage )
501 [ # # ]: 0 : return aImage;
502 : : }
503 [ # # # ]: 0 : catch ( uno::RuntimeException& )
504 : : {
505 : 0 : throw;
506 : : }
507 [ # # ]: 0 : catch ( uno::Exception& )
508 : : {
509 : : }
510 : : }
511 : :
512 [ # # ]: 0 : if ( m_xModuleImageMgr.is() )
513 : : {
514 : : try
515 : : {
516 [ # # ][ # # ]: 0 : aGraphicSeq = m_xModuleImageMgr->getImages( nImageType, aImageCmdSeq );
[ # # ][ # # ]
517 [ # # ]: 0 : uno::Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
518 [ # # ][ # # ]: 0 : aImage = Image( xGraphic );
[ # # ]
519 : :
520 [ # # ]: 0 : if ( !!aImage )
521 [ # # ]: 0 : return aImage;
522 : : }
523 [ # # # ]: 0 : catch ( uno::RuntimeException& )
524 : : {
525 : 0 : throw;
526 : : }
527 [ # # ]: 0 : catch ( uno::Exception& )
528 : : {
529 : : }
530 : : }
531 : :
532 [ # # ][ # # ]: 0 : return aImage;
533 : : }
534 : :
535 : : rtl::OUString
536 : 0 : ContextMenuHelper::getLabelFromCommandURL(
537 : : const ::rtl::OUString& aCmdURL ) const
538 : : {
539 : 0 : ::rtl::OUString aLabel;
540 : :
541 [ # # ]: 0 : if ( m_xUICommandLabels.is() )
542 : : {
543 : : try
544 : : {
545 [ # # ]: 0 : if ( !aCmdURL.isEmpty() )
546 : : {
547 : 0 : rtl::OUString aStr;
548 [ # # ]: 0 : uno::Sequence< beans::PropertyValue > aPropSeq;
549 [ # # ][ # # ]: 0 : uno::Any a( m_xUICommandLabels->getByName( aCmdURL ));
550 [ # # ][ # # ]: 0 : if ( a >>= aPropSeq )
551 : : {
552 [ # # ]: 0 : for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
553 : : {
554 [ # # ][ # # ]: 0 : if ( aPropSeq[i].Name == "Label" )
555 : : {
556 [ # # ]: 0 : aPropSeq[i].Value >>= aStr;
557 : 0 : break;
558 : : }
559 : : }
560 : : }
561 [ # # ]: 0 : aLabel = aStr;
[ # # # ]
562 : : }
563 : : }
564 [ # # ]: 0 : catch ( uno::RuntimeException& )
565 : : {
566 : : }
567 [ # # ]: 0 : catch ( uno::Exception& )
568 : : {
569 : : }
570 : : }
571 : :
572 : 0 : return aLabel;
573 : : }
574 : :
575 : : void
576 : 0 : ContextMenuHelper::completeMenuProperties(
577 : : Menu* pMenu )
578 : : {
579 : : // Retrieve some settings necessary to display complete context
580 : : // menu correctly.
581 : 0 : const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
582 : 0 : bool bShowMenuImages( rSettings.GetUseImagesInMenus() );
583 : :
584 [ # # ]: 0 : if ( pMenu )
585 : : {
586 [ # # ]: 0 : uno::Reference< frame::XFrame > xFrame( m_xWeakFrame );
587 [ # # ]: 0 : uno::Reference< frame::XDispatchProvider > xDispatchProvider( xFrame, uno::UNO_QUERY );
588 : :
589 [ # # ]: 0 : if ( !m_xURLTransformer.is() )
590 : : {
591 [ # # ][ # # ]: 0 : m_xURLTransformer = util::URLTransformer::create( ::comphelper::getProcessComponentContext() );
[ # # ]
592 : : }
593 : :
594 [ # # ][ # # ]: 0 : for ( sal_uInt16 nPos = 0; nPos < pMenu->GetItemCount(); nPos++ )
595 : : {
596 [ # # ]: 0 : sal_uInt16 nId = pMenu->GetItemId( nPos );
597 [ # # ]: 0 : PopupMenu* pPopupMenu = pMenu->GetPopupMenu( nId );
598 [ # # ]: 0 : if ( pPopupMenu )
599 [ # # ]: 0 : completeMenuProperties( pPopupMenu );
600 [ # # ][ # # ]: 0 : if ( pMenu->GetItemType( nPos ) != MENUITEM_SEPARATOR )
601 : : {
602 [ # # ][ # # ]: 0 : ::rtl::OUString aCmdURL( pMenu->GetItemCommand( nId ));
603 : :
604 [ # # ]: 0 : if ( bShowMenuImages )
605 : : {
606 [ # # ]: 0 : Image aImage;
607 [ # # ]: 0 : if ( !aCmdURL.isEmpty() )
608 [ # # ][ # # ]: 0 : aImage = getImageFromCommandURL( aCmdURL );
[ # # ]
609 [ # # ][ # # ]: 0 : pMenu->SetItemImage( nId, aImage );
610 : : }
611 : : else
612 [ # # ][ # # ]: 0 : pMenu->SetItemImage( nId, Image() );
[ # # ]
613 : :
614 [ # # ][ # # ]: 0 : if ( pMenu->GetItemText( nId ).Len() == 0 )
[ # # ]
615 : : {
616 [ # # ]: 0 : ::rtl::OUString aLabel( getLabelFromCommandURL( aCmdURL ));
617 [ # # ][ # # ]: 0 : pMenu->SetItemText( nId, aLabel );
[ # # ]
618 : : }
619 : :
620 : : // Use helper to retrieve state of the command URL
621 : : StateEventHelper* pHelper = new StateEventHelper(
622 : : xDispatchProvider,
623 : : m_xURLTransformer,
624 [ # # ]: 0 : aCmdURL );
625 : :
626 [ # # ]: 0 : uno::Reference< frame::XStatusListener > xHelper( pHelper );
627 [ # # ][ # # ]: 0 : pMenu->EnableItem( nId, pHelper->isCommandEnabled() );
628 : : }
629 : 0 : }
630 : : }
631 : 0 : }
632 : :
633 : :
634 : 0 : IMPL_STATIC_LINK_NOINSTANCE( ContextMenuHelper, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
635 : : {
636 : : // Release solar mutex to prevent deadlocks with clipboard thread
637 : 0 : const sal_uInt32 nRef = Application::ReleaseSolarMutex();
638 : : try
639 : : {
640 : : // Asynchronous execution as this can lead to our own destruction while we are
641 : : // on the stack. Stack unwinding would access the destroyed context menu.
642 [ # # ][ # # ]: 0 : pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
643 : : }
644 : 0 : catch ( uno::Exception& )
645 : : {
646 : : }
647 : :
648 : : // Acquire solar mutex again
649 : 0 : Application::AcquireSolarMutex( nRef );
650 [ # # ]: 0 : delete pExecuteInfo;
651 [ # # ]: 0 : return 0;
652 : : }
653 : :
654 : : } // namespace svt
655 : :
656 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|