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_framework.hxx"
30 #include <uielement/objectmenucontroller.hxx>
31 
32 //_________________________________________________________________________________________________________________
33 //	my own includes
34 //_________________________________________________________________________________________________________________
35 #include <threadhelp/resetableguard.hxx>
36 #include "services.h"
37 
38 //_________________________________________________________________________________________________________________
39 //	interface includes
40 //_________________________________________________________________________________________________________________
41 #include <com/sun/star/awt/XDevice.hpp>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <com/sun/star/awt/MenuItemStyle.hpp>
44 #include <com/sun/star/frame/XDispatchProvider.hpp>
45 
46 #ifndef _COM_SUN_STAR_EMBED_VERBDATTRIBUTES_HPP_
47 #include <com/sun/star/embed/VerbAttributes.hpp>
48 #endif
49 
50 //_________________________________________________________________________________________________________________
51 //	includes of other projects
52 //_________________________________________________________________________________________________________________
53 
54 #ifndef _VCL_MENU_HXX_
55 #include <vcl/menu.hxx>
56 #endif
57 #include <vcl/svapp.hxx>
58 #include <vcl/i18nhelp.hxx>
59 #include <tools/urlobj.hxx>
60 #include <rtl/ustrbuf.hxx>
61 #include <dispatch/uieventloghelper.hxx>
62 #include <vos/mutex.hxx>
63 
64 //_________________________________________________________________________________________________________________
65 //	Defines
66 //_________________________________________________________________________________________________________________
67 //
68 
69 using namespace com::sun::star::uno;
70 using namespace com::sun::star::lang;
71 using namespace com::sun::star::frame;
72 using namespace com::sun::star::beans;
73 using namespace com::sun::star::util;
74 
75 namespace framework
76 {
77 
78 DEFINE_XSERVICEINFO_MULTISERVICE        (   ObjectMenuController				    ,
79                                             OWeakObject                             ,
80                                             SERVICENAME_POPUPMENUCONTROLLER		    ,
81 											IMPLEMENTATIONNAME_OBJECTMENUCONTROLLER
82 										)
83 
84 DEFINE_INIT_SERVICE                     (   ObjectMenuController, {} )
85 
86 ObjectMenuController::ObjectMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
87 	svt::PopupMenuControllerBase( xServiceManager )
88 {
89 }
90 
91 ObjectMenuController::~ObjectMenuController()
92 {
93 }
94 
95 // private function
96 void ObjectMenuController::fillPopupMenu( const Sequence< com::sun::star::embed::VerbDescriptor >& rVerbCommandSeq, Reference< css::awt::XPopupMenu >& rPopupMenu )
97 {
98     const com::sun::star::embed::VerbDescriptor* pVerbCommandArray = rVerbCommandSeq.getConstArray();
99     VCLXPopupMenu*                                     pPopupMenu        = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
100     PopupMenu*                                         pVCLPopupMenu     = 0;
101 
102     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
103 
104     resetPopupMenu( rPopupMenu );
105     if ( pPopupMenu )
106         pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
107 
108     if ( pVCLPopupMenu )
109     {
110         const rtl::OUString aVerbCommand( RTL_CONSTASCII_USTRINGPARAM( ".uno:ObjectMenue?VerbID:short=" ));
111 	    for ( sal_uInt16 i = 0; i < rVerbCommandSeq.getLength(); i++ )
112 	    {
113 			const com::sun::star::embed::VerbDescriptor& rVerb = pVerbCommandArray[i];
114             if ( rVerb.VerbAttributes & com::sun::star::embed::VerbAttributes::MS_VERBATTR_ONCONTAINERMENU )
115 		    {
116 		        m_xPopupMenu->insertItem( i+1, rVerb.VerbName, 0, i );
117                 // use VCL popup menu pointer to set vital information that are not part of the awt implementation
118 
119 		        rtl::OUString aCommand( aVerbCommand );
120                 aCommand += rtl::OUString::valueOf( rVerb.VerbID );
121 		        pVCLPopupMenu->SetItemCommand( i+1, aCommand ); // Store verb command
122             }
123 	    }
124     }
125 }
126 
127 // XEventListener
128 void SAL_CALL ObjectMenuController::disposing( const EventObject& ) throw ( RuntimeException )
129 {
130     Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
131 
132     osl::MutexGuard aLock( m_aMutex );
133     m_xFrame.clear();
134     m_xDispatch.clear();
135     m_xObjectUpdateDispatch.clear();
136     m_xServiceManager.clear();
137 
138     if ( m_xPopupMenu.is() )
139         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
140     m_xPopupMenu.clear();
141 }
142 
143 // XStatusListener
144 void SAL_CALL ObjectMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
145 {
146 	Sequence < com::sun::star::embed::VerbDescriptor > aVerbCommandSeq;
147     if ( Event.State >>= aVerbCommandSeq )
148     {
149         osl::MutexGuard aLock( m_aMutex );
150         if ( m_xPopupMenu.is() )
151             fillPopupMenu( aVerbCommandSeq, m_xPopupMenu );
152     }
153 }
154 
155 // XMenuListener
156 void ObjectMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
157 {
158     Sequence<PropertyValue>	     aArgs;
159     if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
160         UiEventLogHelper(::rtl::OUString::createFromAscii("ObjectMenuController")).log(m_xServiceManager, m_xFrame, aTargetURL, aArgs);
161 	OSL_ENSURE(_xDispatch.is(),"ObjectMenuController::impl_select: No dispatch");
162 	if ( _xDispatch.is() )
163 		_xDispatch->dispatch( aTargetURL, aArgs );
164 }
165 
166 }
167