1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
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>
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>
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
;
54 class ObjectMenuController
: public svt::PopupMenuControllerBase
56 using svt::PopupMenuControllerBase::disposing
;
59 ObjectMenuController( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
60 virtual ~ObjectMenuController();
63 virtual OUString SAL_CALL
getImplementationName()
64 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
66 return OUString("com.sun.star.comp.framework.ObjectMenuController");
69 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
70 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
72 return cppu::supportsService(this, ServiceName
);
75 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
76 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
78 css::uno::Sequence
< OUString
> aSeq(1);
79 aSeq
[0] = "com.sun.star.frame.PopupMenuController";
84 virtual void SAL_CALL
statusChanged( const css::frame::FeatureStateEvent
& Event
) throw ( css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
87 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) throw ( css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
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
;
93 css::uno::Reference
< css::frame::XDispatch
> m_xObjectUpdateDispatch
;
96 ObjectMenuController::ObjectMenuController( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& xContext
) :
97 svt::PopupMenuControllerBase( xContext
)
101 ObjectMenuController::~ObjectMenuController()
106 void ObjectMenuController::fillPopupMenu( const Sequence
< com::sun::star::embed::VerbDescriptor
>& rVerbCommandSeq
, Reference
< css::awt::XPopupMenu
>& rPopupMenu
)
108 const css::embed::VerbDescriptor
* pVerbCommandArray
= rVerbCommandSeq
.getConstArray();
109 VCLXPopupMenu
* pPopupMenu
= static_cast<VCLXPopupMenu
*>(VCLXMenu::GetImplementation( rPopupMenu
));
110 PopupMenu
* pVCLPopupMenu
= 0;
112 SolarMutexGuard aSolarMutexGuard
;
114 resetPopupMenu( rPopupMenu
);
116 pVCLPopupMenu
= static_cast<PopupMenu
*>(pPopupMenu
->GetMenu());
120 const OUString
aVerbCommand( ".uno:ObjectMenue?VerbID:short=" );
121 for ( sal_uInt16 i
= 0; i
< rVerbCommandSeq
.getLength(); i
++ )
123 const com::sun::star::embed::VerbDescriptor
& rVerb
= pVerbCommandArray
[i
];
124 if ( rVerb
.VerbAttributes
& com::sun::star::embed::VerbAttributes::MS_VERBATTR_ONCONTAINERMENU
)
126 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
129 OUString
aCommand( aVerbCommand
);
130 aCommand
+= OUString::number( rVerb
.VerbID
);
131 pVCLPopupMenu
->SetItemCommand( i
+1, aCommand
); // Store verb command
138 void SAL_CALL
ObjectMenuController::disposing( const EventObject
& ) throw ( RuntimeException
, std::exception
)
140 Reference
< css::awt::XMenuListener
> xHolder(( OWeakObject
*)this, UNO_QUERY
);
142 osl::MutexGuard
aLock( m_aMutex
);
145 m_xObjectUpdateDispatch
.clear();
147 if ( m_xPopupMenu
.is() )
148 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(( OWeakObject
*)this, UNO_QUERY
));
149 m_xPopupMenu
.clear();
153 void SAL_CALL
ObjectMenuController::statusChanged( const FeatureStateEvent
& Event
) throw ( RuntimeException
, std::exception
)
155 Sequence
< com::sun::star::embed::VerbDescriptor
> aVerbCommandSeq
;
156 if ( Event
.State
>>= aVerbCommandSeq
)
158 osl::MutexGuard
aLock( m_aMutex
);
159 if ( m_xPopupMenu
.is() )
160 fillPopupMenu( aVerbCommandSeq
, m_xPopupMenu
);
165 void ObjectMenuController::impl_select(const Reference
< XDispatch
>& _xDispatch
,const ::com::sun::star::util::URL
& aTargetURL
)
167 Sequence
<PropertyValue
> aArgs
;
168 OSL_ENSURE(_xDispatch
.is(),"ObjectMenuController::impl_select: No dispatch");
169 if ( _xDispatch
.is() )
170 _xDispatch
->dispatch( aTargetURL
, aArgs
);
175 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
176 com_sun_star_comp_framework_ObjectMenuController_get_implementation(
177 css::uno::XComponentContext
*context
,
178 css::uno::Sequence
<css::uno::Any
> const &)
180 return cppu::acquire(new ObjectMenuController(context
));
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */