Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / objectmenucontroller.cxx
blob9c86a091337f548002388f7feacf85beb27916fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <stdtypes.h>
22 #include <com/sun/star/embed/VerbAttributes.hpp>
23 #include <com/sun/star/embed/VerbDescriptor.hpp>
25 #include <svtools/popupmenucontrollerbase.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <cppuhelper/weak.hxx>
28 #include <vcl/svapp.hxx>
29 #include <osl/mutex.hxx>
30 #include <toolkit/awt/vclxmenu.hxx>
32 using namespace com::sun::star::uno;
33 using namespace com::sun::star::lang;
34 using namespace com::sun::star::frame;
35 using namespace com::sun::star::beans;
36 using namespace com::sun::star::util;
37 using namespace framework;
39 namespace {
41 class ObjectMenuController : public svt::PopupMenuControllerBase
43 using svt::PopupMenuControllerBase::disposing;
45 public:
46 explicit ObjectMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext );
48 // XServiceInfo
49 virtual OUString SAL_CALL getImplementationName() override
51 return "com.sun.star.comp.framework.ObjectMenuController";
54 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
56 return cppu::supportsService(this, ServiceName);
59 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
61 return {"com.sun.star.frame.PopupMenuController"};
64 // XStatusListener
65 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) override;
67 // XEventListener
68 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
70 private:
71 void fillPopupMenu( const css::uno::Sequence< css::embed::VerbDescriptor >& rVerbCommandSeq, css::uno::Reference< css::awt::XPopupMenu > const & rPopupMenu );
74 ObjectMenuController::ObjectMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
75 svt::PopupMenuControllerBase( xContext )
79 // private function
80 void ObjectMenuController::fillPopupMenu( const Sequence< css::embed::VerbDescriptor >& rVerbCommandSeq, Reference< css::awt::XPopupMenu > const & rPopupMenu )
82 const css::embed::VerbDescriptor* pVerbCommandArray = rVerbCommandSeq.getConstArray();
84 SolarMutexGuard aSolarMutexGuard;
86 resetPopupMenu( rPopupMenu );
88 static constexpr OUStringLiteral aVerbCommand( u".uno:ObjectMenue?VerbID:short=" );
89 for ( sal_Int32 i = 0; i < rVerbCommandSeq.getLength(); i++ )
91 const css::embed::VerbDescriptor& rVerb = pVerbCommandArray[i];
92 if ( rVerb.VerbAttributes & css::embed::VerbAttributes::MS_VERBATTR_ONCONTAINERMENU )
94 m_xPopupMenu->insertItem( i+1, rVerb.VerbName, 0, i );
95 OUString aCommand = aVerbCommand + OUString::number( rVerb.VerbID );
96 m_xPopupMenu->setCommand( i+1, aCommand ); // Store verb command
101 // XEventListener
102 void SAL_CALL ObjectMenuController::disposing( const EventObject& )
104 Reference< css::awt::XMenuListener > xHolder(this);
106 std::unique_lock aLock( m_aMutex );
107 m_xFrame.clear();
108 m_xDispatch.clear();
110 if ( m_xPopupMenu.is() )
111 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
112 m_xPopupMenu.clear();
115 // XStatusListener
116 void SAL_CALL ObjectMenuController::statusChanged( const FeatureStateEvent& Event )
118 Sequence < css::embed::VerbDescriptor > aVerbCommandSeq;
119 if ( Event.State >>= aVerbCommandSeq )
121 std::unique_lock aLock( m_aMutex );
122 if ( m_xPopupMenu.is() )
123 fillPopupMenu( aVerbCommandSeq, m_xPopupMenu );
129 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
130 com_sun_star_comp_framework_ObjectMenuController_get_implementation(
131 css::uno::XComponentContext *context,
132 css::uno::Sequence<css::uno::Any> const &)
134 return cppu::acquire(new ObjectMenuController(context));
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */