1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 
10 #include <comphelper/processfactory.hxx>
11 #include <comphelper/propertyvalue.hxx>
12 #include <toolkit/awt/vclxmenu.hxx>
13 #include <vcl/builderfactory.hxx>
14 #include <vcl/menu.hxx>
15 #include <vcl/menubtn.hxx>
16 
17 #include <com/sun/star/frame/theDesktop.hpp>
18 #include <com/sun/star/frame/ModuleManager.hpp>
19 #include <com/sun/star/frame/thePopupMenuControllerFactory.hpp>
20 #include <com/sun/star/frame/XPopupMenuController.hpp>
21 
22 namespace {
23 
24 class ManagedMenuButton : public MenuButton
25 {
26 public:
27     ManagedMenuButton(vcl::Window* pParent, WinBits nStyle);
28     ~ManagedMenuButton() override;
29 
30     void Activate() override;
31     void dispose() override;
32 
33 private:
34     rtl::Reference<VCLXPopupMenu> m_xPopupMenu;
35     css::uno::Reference<css::frame::XPopupMenuController> m_xPopupController;
36 };
37 
ManagedMenuButton(vcl::Window * pParent,WinBits nStyle)38 ManagedMenuButton::ManagedMenuButton(vcl::Window* pParent, WinBits nStyle)
39     : MenuButton(pParent, nStyle)
40 {
41     SetImageAlign(ImageAlign::Left);
42 }
43 
~ManagedMenuButton()44 ManagedMenuButton::~ManagedMenuButton()
45 {
46     disposeOnce();
47 }
48 
dispose()49 void ManagedMenuButton::dispose()
50 {
51     css::uno::Reference<css::lang::XComponent> xComponent(m_xPopupController, css::uno::UNO_QUERY);
52     if (xComponent.is())
53         xComponent->dispose();
54 
55     m_xPopupMenu.clear();
56     m_xPopupController.clear();
57     MenuButton::dispose();
58 }
59 
Activate()60 void ManagedMenuButton::Activate()
61 {
62     if (!GetPopupMenu())
63         SetPopupMenu(VclPtr<PopupMenu>::Create());
64 
65     MenuButton::Activate();
66 
67     if (m_xPopupController.is())
68     {
69         m_xPopupController->updatePopupMenu();
70         return;
71     }
72 
73     if (!m_xPopupMenu.is())
74         m_xPopupMenu.set(new VCLXPopupMenu(GetPopupMenu()));
75 
76     // FIXME: get the frame from the parent VclBuilder.
77     css::uno::Reference<css::uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
78     css::uno::Reference<css::frame::XDesktop2> xDesktop(css::frame::theDesktop::get(xContext));
79     css::uno::Reference<css::frame::XFrame> xFrame(xDesktop->getActiveFrame());
80     if (!xFrame.is())
81         return;
82 
83     OUString aModuleName;
84     try
85     {
86         css::uno::Reference<css::frame::XModuleManager> xModuleManager(css::frame::ModuleManager::create(xContext));
87         aModuleName = xModuleManager->identify(xFrame);
88     }
89     catch( const css::uno::Exception& )
90     {}
91 
92     css::uno::Sequence<css::uno::Any> aArgs {
93         css::uno::makeAny(comphelper::makePropertyValue("ModuleIdentifier", aModuleName)),
94         css::uno::makeAny(comphelper::makePropertyValue("Frame", css::uno::makeAny(xFrame))),
95         css::uno::makeAny(comphelper::makePropertyValue("InToolbar", css::uno::makeAny(true)))
96     };
97 
98     const OUString aCommand(GetCommand());
99     if (!aCommand.isEmpty() && GetPopupMenu()->GetItemCount() == 0)
100     {
101         css::uno::Reference<css::frame::XUIControllerFactory> xPopupMenuControllerFactory =
102             css::frame::thePopupMenuControllerFactory::get(xContext);
103 
104         if (xPopupMenuControllerFactory->hasController(aCommand, aModuleName))
105             m_xPopupController.set(xPopupMenuControllerFactory->createInstanceWithArgumentsAndContext(
106                 aCommand, aArgs, xContext), css::uno::UNO_QUERY);
107     }
108 
109     // No registered controller found, use one the can handle arbitrary menus (e.g. defined in .ui file).
110     if (!m_xPopupController.is())
111         m_xPopupController.set(xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
112             "com.sun.star.comp.framework.ResourceMenuController", aArgs, xContext), css::uno::UNO_QUERY);
113 
114     if (m_xPopupController.is())
115         m_xPopupController->setPopupMenu(m_xPopupMenu.get());
116 }
117 
118 }
119 
120 VCL_BUILDER_FACTORY_ARGS(ManagedMenuButton, WB_CLIPCHILDREN|WB_CENTER|WB_VCENTER|WB_FLATBUTTON)
121 
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
123