/
PopupMenu.cpp
89 lines (75 loc) · 1.7 KB
/
PopupMenu.cpp
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
#include "PopupMenu.h"
#include "SeparatorItem.h"
namespace wxutil
{
// Default constructor
PopupMenu::PopupMenu() :
wxMenu()
{
Bind(wxEVT_MENU, &PopupMenu::_onItemClick, this);
}
PopupMenu::~PopupMenu()
{
}
// Add a named menu item
void PopupMenu::addItem(wxMenuItem* widget,
const Callback& callback,
const SensitivityTest& sensTest,
const VisibilityTest& visTest)
{
// Construct a wrapper and pass to specialised method
addItem(std::make_shared<wxutil::MenuItem>(widget, callback, sensTest, visTest));
}
void PopupMenu::addItem(const ui::IMenuItemPtr& item)
{
_menuItems.push_back(item);
// Add the widget to the menu
Append(item->getMenuItem());
}
void PopupMenu::addSeparator()
{
addItem(std::make_shared<SeparatorItem>(
new wxMenuItem(this, wxID_ANY, wxEmptyString, wxEmptyString, wxITEM_SEPARATOR))
);
}
void PopupMenu::show(wxWindow* parent)
{
// Iterate through the list of MenuItems, enabling or disabling each widget
// based on its SensitivityTest
for (auto item : _menuItems)
{
bool visible = item->isVisible();
if (visible)
{
// Visibility check passed
item->getMenuItem()->Enable(item->isSensitive());
}
else
{
// Visibility check failed, skip sensitivity check
item->getMenuItem()->Enable(false);
}
}
parent->PopupMenu(this);
}
void PopupMenu::_onItemClick(wxCommandEvent& ev)
{
int commandId = ev.GetId();
// Find the menu item with that ID
for (auto item : _menuItems)
{
if (item->getMenuItem()->GetId() == commandId)
{
item->execute();
break;
}
}
}
void PopupMenu::foreachMenuItem(const std::function<void(const ui::IMenuItemPtr&)>& functor)
{
for (const auto& item : _menuItems)
{
functor(item);
}
}
} // namespace