/
PopupMenu.cpp
95 lines (79 loc) · 1.79 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
90
91
92
93
94
95
#include "PopupMenu.h"
namespace wxutil
{
// Default constructor
PopupMenu::PopupMenu() :
wxMenu()
{
Connect(wxEVT_MENU, wxCommandEventHandler(PopupMenu::_onItemClick), NULL, 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(ui::IMenuItemPtr(new 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()
{
AppendSeparator();
}
// Show the menu
void PopupMenu::show(wxWindow* parent)
{
// Iterate through the list of MenuItems, enabling or disabling each widget
// based on its SensitivityTest
for (MenuItemList::iterator i = _menuItems.begin();
i != _menuItems.end();
++i)
{
ui::IMenuItem& item = *(*i);
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 (MenuItemList::iterator i = _menuItems.begin();
i != _menuItems.end();
++i)
{
ui::IMenuItem& item = *(*i);
if (item.getMenuItem()->GetId() == commandId)
{
item.execute();
break;
}
}
}
void PopupMenu::foreachMenuItem(const std::function<void(const ui::IMenuItemPtr&)>& functor)
{
for (const ui::IMenuItemPtr& item : _menuItems)
{
functor(item);
}
}
} // namespace