Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <stdtypes.h>
21 :
22 : #include <com/sun/star/awt/XDevice.hpp>
23 : #include <com/sun/star/beans/PropertyValue.hpp>
24 : #include <com/sun/star/awt/MenuItemStyle.hpp>
25 : #include <com/sun/star/frame/XDispatchProvider.hpp>
26 :
27 : #include <com/sun/star/embed/VerbAttributes.hpp>
28 : #include <com/sun/star/embed/VerbDescriptor.hpp>
29 : #include <com/sun/star/frame/XDispatch.hpp>
30 : #include <com/sun/star/frame/XStatusListener.hpp>
31 : #include <com/sun/star/frame/XPopupMenuController.hpp>
32 : #include <com/sun/star/lang/XServiceInfo.hpp>
33 :
34 : #include <svtools/popupmenucontrollerbase.hxx>
35 : #include <toolkit/awt/vclxmenu.hxx>
36 : #include <cppuhelper/supportsservice.hxx>
37 : #include <cppuhelper/weak.hxx>
38 : #include <vcl/menu.hxx>
39 : #include <vcl/svapp.hxx>
40 : #include <vcl/i18nhelp.hxx>
41 : #include <rtl/ref.hxx>
42 : #include <rtl/ustrbuf.hxx>
43 : #include <osl/mutex.hxx>
44 :
45 : using namespace com::sun::star::uno;
46 : using namespace com::sun::star::lang;
47 : using namespace com::sun::star::frame;
48 : using namespace com::sun::star::beans;
49 : using namespace com::sun::star::util;
50 : using namespace framework;
51 :
52 : namespace {
53 :
54 : class ObjectMenuController : public svt::PopupMenuControllerBase
55 : {
56 : using svt::PopupMenuControllerBase::disposing;
57 :
58 : public:
59 : ObjectMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext );
60 : virtual ~ObjectMenuController();
61 :
62 : // XServiceInfo
63 3 : virtual OUString SAL_CALL getImplementationName()
64 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
65 : {
66 3 : return OUString("com.sun.star.comp.framework.ObjectMenuController");
67 : }
68 :
69 0 : virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
70 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
71 : {
72 0 : return cppu::supportsService(this, ServiceName);
73 : }
74 :
75 1 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
76 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
77 : {
78 1 : css::uno::Sequence< OUString > aSeq(1);
79 1 : aSeq[0] = "com.sun.star.frame.PopupMenuController";
80 1 : return aSeq;
81 : }
82 :
83 : // XStatusListener
84 : virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
85 :
86 : // XEventListener
87 : virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
88 :
89 : private:
90 : void fillPopupMenu( const css::uno::Sequence< css::embed::VerbDescriptor >& rVerbCommandSeq, css::uno::Reference< css::awt::XPopupMenu >& rPopupMenu );
91 : virtual void impl_select(const css::uno::Reference< css::frame::XDispatch >& _xDispatch,const css::util::URL& aURL) SAL_OVERRIDE;
92 :
93 : css::uno::Reference< css::frame::XDispatch > m_xObjectUpdateDispatch;
94 : };
95 :
96 5 : ObjectMenuController::ObjectMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext ) :
97 5 : svt::PopupMenuControllerBase( xContext )
98 : {
99 5 : }
100 :
101 10 : ObjectMenuController::~ObjectMenuController()
102 : {
103 10 : }
104 :
105 : // private function
106 0 : void ObjectMenuController::fillPopupMenu( const Sequence< com::sun::star::embed::VerbDescriptor >& rVerbCommandSeq, Reference< css::awt::XPopupMenu >& rPopupMenu )
107 : {
108 0 : const css::embed::VerbDescriptor* pVerbCommandArray = rVerbCommandSeq.getConstArray();
109 0 : VCLXPopupMenu* pPopupMenu = static_cast<VCLXPopupMenu *>(VCLXMenu::GetImplementation( rPopupMenu ));
110 0 : PopupMenu* pVCLPopupMenu = 0;
111 :
112 0 : SolarMutexGuard aSolarMutexGuard;
113 :
114 0 : resetPopupMenu( rPopupMenu );
115 0 : if ( pPopupMenu )
116 0 : pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());
117 :
118 0 : if ( pVCLPopupMenu )
119 : {
120 0 : const OUString aVerbCommand( ".uno:ObjectMenue?VerbID:short=" );
121 0 : for ( sal_Int32 i = 0; i < rVerbCommandSeq.getLength(); i++ )
122 : {
123 0 : const com::sun::star::embed::VerbDescriptor& rVerb = pVerbCommandArray[i];
124 0 : if ( rVerb.VerbAttributes & com::sun::star::embed::VerbAttributes::MS_VERBATTR_ONCONTAINERMENU )
125 : {
126 0 : m_xPopupMenu->insertItem( i+1, rVerb.VerbName, 0, i );
127 : // use VCL popup menu pointer to set vital information that are not part of the awt implementation
128 :
129 0 : OUString aCommand( aVerbCommand );
130 0 : aCommand += OUString::number( rVerb.VerbID );
131 0 : pVCLPopupMenu->SetItemCommand( i+1, aCommand ); // Store verb command
132 : }
133 0 : }
134 0 : }
135 0 : }
136 :
137 : // XEventListener
138 2 : void SAL_CALL ObjectMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
139 : {
140 2 : Reference< css::awt::XMenuListener > xHolder(static_cast<OWeakObject *>(this), UNO_QUERY );
141 :
142 4 : osl::MutexGuard aLock( m_aMutex );
143 2 : m_xFrame.clear();
144 2 : m_xDispatch.clear();
145 2 : m_xObjectUpdateDispatch.clear();
146 :
147 2 : if ( m_xPopupMenu.is() )
148 2 : m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(static_cast<OWeakObject *>(this), UNO_QUERY ));
149 4 : m_xPopupMenu.clear();
150 2 : }
151 :
152 : // XStatusListener
153 6 : void SAL_CALL ObjectMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
154 : {
155 6 : Sequence < com::sun::star::embed::VerbDescriptor > aVerbCommandSeq;
156 6 : if ( Event.State >>= aVerbCommandSeq )
157 : {
158 0 : osl::MutexGuard aLock( m_aMutex );
159 0 : if ( m_xPopupMenu.is() )
160 0 : fillPopupMenu( aVerbCommandSeq, m_xPopupMenu );
161 6 : }
162 6 : }
163 :
164 : // XMenuListener
165 0 : void ObjectMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
166 : {
167 0 : Sequence<PropertyValue> aArgs;
168 : OSL_ENSURE(_xDispatch.is(),"ObjectMenuController::impl_select: No dispatch");
169 0 : if ( _xDispatch.is() )
170 0 : _xDispatch->dispatch( aTargetURL, aArgs );
171 0 : }
172 :
173 : }
174 :
175 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
176 5 : com_sun_star_comp_framework_ObjectMenuController_get_implementation(
177 : css::uno::XComponentContext *context,
178 : css::uno::Sequence<css::uno::Any> const &)
179 : {
180 5 : return cppu::acquire(new ObjectMenuController(context));
181 : }
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|