1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #include <comphelper/processfactory.hxx>
11 #include <comphelper/propertyvalue.hxx>
13 #include <managedmenubutton.hxx>
14 #include <vcl/menu.hxx>
16 #include <com/sun/star/frame/ModuleManager.hpp>
17 #include <com/sun/star/frame/theDesktop.hpp>
18 #include <com/sun/star/frame/thePopupMenuControllerFactory.hpp>
20 ManagedMenuButton::ManagedMenuButton(vcl::Window
* pParent
, WinBits nStyle
)
21 : MenuButton(pParent
, nStyle
)
23 SetImageAlign(ImageAlign::Left
);
26 ManagedMenuButton::~ManagedMenuButton()
31 void ManagedMenuButton::dispose()
33 css::uno::Reference
<css::lang::XComponent
> xComponent(m_xPopupController
, css::uno::UNO_QUERY
);
35 xComponent
->dispose();
38 m_xPopupController
.clear();
39 MenuButton::dispose();
42 void ManagedMenuButton::PrepareExecute()
45 SetPopupMenu(VclPtr
<PopupMenu
>::Create(), true);
47 MenuButton::PrepareExecute();
49 if (m_xPopupController
.is())
51 m_xPopupController
->updatePopupMenu();
55 if (!m_xPopupMenu
.is())
56 m_xPopupMenu
= GetPopupMenu()->CreateMenuInterface();
58 // FIXME: get the frame from the parent VclBuilder.
59 const css::uno::Reference
<css::uno::XComponentContext
>& xContext(comphelper::getProcessComponentContext());
60 css::uno::Reference
<css::frame::XDesktop2
> xDesktop(css::frame::theDesktop::get(xContext
));
61 css::uno::Reference
<css::frame::XFrame
> xFrame(xDesktop
->getActiveFrame());
68 css::uno::Reference
<css::frame::XModuleManager
> xModuleManager(css::frame::ModuleManager::create(xContext
));
69 aModuleName
= xModuleManager
->identify(xFrame
);
71 catch( const css::uno::Exception
& )
74 css::uno::Sequence
<css::uno::Any
> aArgs
{
75 css::uno::Any(comphelper::makePropertyValue(u
"ModuleIdentifier"_ustr
, aModuleName
)),
76 css::uno::Any(comphelper::makePropertyValue(u
"Frame"_ustr
, css::uno::Any(xFrame
))),
77 css::uno::Any(comphelper::makePropertyValue(u
"InToolbar"_ustr
, css::uno::Any(true)))
80 const OUString
aCommand(GetCommand());
81 if (!aCommand
.isEmpty() && GetPopupMenu()->GetItemCount() == 0)
83 css::uno::Reference
<css::frame::XUIControllerFactory
> xPopupMenuControllerFactory
=
84 css::frame::thePopupMenuControllerFactory::get(xContext
);
86 if (xPopupMenuControllerFactory
->hasController(aCommand
, aModuleName
))
87 m_xPopupController
.set(xPopupMenuControllerFactory
->createInstanceWithArgumentsAndContext(
88 aCommand
, aArgs
, xContext
), css::uno::UNO_QUERY
);
91 // No registered controller found, use one the can handle arbitrary menus (e.g. defined in .ui file).
92 if (!m_xPopupController
.is())
93 m_xPopupController
.set(xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
94 u
"com.sun.star.comp.framework.ResourceMenuController"_ustr
, aArgs
, xContext
), css::uno::UNO_QUERY
);
96 if (m_xPopupController
.is())
97 m_xPopupController
->setPopupMenu(m_xPopupMenu
);
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */