From 267c6f2ac71f92999e969232431ba04678e7437e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 07:54:39 +0200 Subject: Adding upstream version 4:24.2.0. Signed-off-by: Daniel Baumann --- vcl/source/control/managedmenubutton.cxx | 100 +++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 vcl/source/control/managedmenubutton.cxx (limited to 'vcl/source/control/managedmenubutton.cxx') diff --git a/vcl/source/control/managedmenubutton.cxx b/vcl/source/control/managedmenubutton.cxx new file mode 100644 index 0000000000..7545dba9b3 --- /dev/null +++ b/vcl/source/control/managedmenubutton.cxx @@ -0,0 +1,100 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +#include +#include + +#include +#include +#include + +ManagedMenuButton::ManagedMenuButton(vcl::Window* pParent, WinBits nStyle) + : MenuButton(pParent, nStyle) +{ + SetImageAlign(ImageAlign::Left); +} + +ManagedMenuButton::~ManagedMenuButton() +{ + disposeOnce(); +} + +void ManagedMenuButton::dispose() +{ + css::uno::Reference xComponent(m_xPopupController, css::uno::UNO_QUERY); + if (xComponent.is()) + xComponent->dispose(); + + m_xPopupMenu.clear(); + m_xPopupController.clear(); + MenuButton::dispose(); +} + +void ManagedMenuButton::PrepareExecute() +{ + if (!GetPopupMenu()) + SetPopupMenu(VclPtr::Create()); + + MenuButton::PrepareExecute(); + + if (m_xPopupController.is()) + { + m_xPopupController->updatePopupMenu(); + return; + } + + if (!m_xPopupMenu.is()) + m_xPopupMenu = GetPopupMenu()->CreateMenuInterface(); + + // FIXME: get the frame from the parent VclBuilder. + css::uno::Reference xContext(comphelper::getProcessComponentContext()); + css::uno::Reference xDesktop(css::frame::theDesktop::get(xContext)); + css::uno::Reference xFrame(xDesktop->getActiveFrame()); + if (!xFrame.is()) + return; + + OUString aModuleName; + try + { + css::uno::Reference xModuleManager(css::frame::ModuleManager::create(xContext)); + aModuleName = xModuleManager->identify(xFrame); + } + catch( const css::uno::Exception& ) + {} + + css::uno::Sequence aArgs { + css::uno::Any(comphelper::makePropertyValue("ModuleIdentifier", aModuleName)), + css::uno::Any(comphelper::makePropertyValue("Frame", css::uno::Any(xFrame))), + css::uno::Any(comphelper::makePropertyValue("InToolbar", css::uno::Any(true))) + }; + + const OUString aCommand(GetCommand()); + if (!aCommand.isEmpty() && GetPopupMenu()->GetItemCount() == 0) + { + css::uno::Reference xPopupMenuControllerFactory = + css::frame::thePopupMenuControllerFactory::get(xContext); + + if (xPopupMenuControllerFactory->hasController(aCommand, aModuleName)) + m_xPopupController.set(xPopupMenuControllerFactory->createInstanceWithArgumentsAndContext( + aCommand, aArgs, xContext), css::uno::UNO_QUERY); + } + + // No registered controller found, use one the can handle arbitrary menus (e.g. defined in .ui file). + if (!m_xPopupController.is()) + m_xPopupController.set(xContext->getServiceManager()->createInstanceWithArgumentsAndContext( + "com.sun.star.comp.framework.ResourceMenuController", aArgs, xContext), css::uno::UNO_QUERY); + + if (m_xPopupController.is()) + m_xPopupController->setPopupMenu(m_xPopupMenu); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit v1.2.3