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