summaryrefslogtreecommitdiffstats
path: root/svtools/source/control/managedmenubutton.cxx
blob: 45e9d98ca1a8176e7a5d61f4e98f26f9d24d9104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- 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 <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <toolkit/awt/vclxmenu.hxx>
#include <vcl/builderfactory.hxx>
#include <vcl/menu.hxx>
#include <vcl/menubtn.hxx>

#include <com/sun/star/frame/theDesktop.hpp>
#include <com/sun/star/frame/ModuleManager.hpp>
#include <com/sun/star/frame/thePopupMenuControllerFactory.hpp>
#include <com/sun/star/frame/XPopupMenuController.hpp>

namespace {

class ManagedMenuButton : public MenuButton
{
public:
    ManagedMenuButton(vcl::Window* pParent, WinBits nStyle);
    ~ManagedMenuButton() override;

    void Activate() override;
    void dispose() override;

private:
    rtl::Reference<VCLXPopupMenu> m_xPopupMenu;
    css::uno::Reference<css::frame::XPopupMenuController> m_xPopupController;
};

ManagedMenuButton::ManagedMenuButton(vcl::Window* pParent, WinBits nStyle)
    : MenuButton(pParent, nStyle)
{
    SetImageAlign(ImageAlign::Left);
}

ManagedMenuButton::~ManagedMenuButton()
{
    disposeOnce();
}

void ManagedMenuButton::dispose()
{
    css::uno::Reference<css::lang::XComponent> xComponent(m_xPopupController, css::uno::UNO_QUERY);
    if (xComponent.is())
        xComponent->dispose();

    m_xPopupMenu.clear();
    m_xPopupController.clear();
    MenuButton::dispose();
}

void ManagedMenuButton::Activate()
{
    if (!GetPopupMenu())
        SetPopupMenu(VclPtr<PopupMenu>::Create());

    MenuButton::Activate();

    if (m_xPopupController.is())
    {
        m_xPopupController->updatePopupMenu();
        return;
    }

    if (!m_xPopupMenu.is())
        m_xPopupMenu.set(new VCLXPopupMenu(GetPopupMenu()));

    // FIXME: get the frame from the parent VclBuilder.
    css::uno::Reference<css::uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
    css::uno::Reference<css::frame::XDesktop2> xDesktop(css::frame::theDesktop::get(xContext));
    css::uno::Reference<css::frame::XFrame> xFrame(xDesktop->getActiveFrame());
    if (!xFrame.is())
        return;

    OUString aModuleName;
    try
    {
        css::uno::Reference<css::frame::XModuleManager> xModuleManager(css::frame::ModuleManager::create(xContext));
        aModuleName = xModuleManager->identify(xFrame);
    }
    catch( const css::uno::Exception& )
    {}

    css::uno::Sequence<css::uno::Any> aArgs {
        css::uno::makeAny(comphelper::makePropertyValue("ModuleIdentifier", aModuleName)),
        css::uno::makeAny(comphelper::makePropertyValue("Frame", css::uno::makeAny(xFrame))),
        css::uno::makeAny(comphelper::makePropertyValue("InToolbar", css::uno::makeAny(true)))
    };

    const OUString aCommand(GetCommand());
    if (!aCommand.isEmpty() && GetPopupMenu()->GetItemCount() == 0)
    {
        css::uno::Reference<css::frame::XUIControllerFactory> 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.get());
}

}

VCL_BUILDER_FACTORY_ARGS(ManagedMenuButton, WB_CLIPCHILDREN|WB_CENTER|WB_VCENTER|WB_FLATBUTTON)

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */