/
MenuBar.cpp
75 lines (58 loc) · 1.11 KB
/
MenuBar.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
#include "MenuBar.h"
#include <wx/menu.h>
#include "MenuFolder.h"
namespace ui
{
MenuBar::MenuBar() :
_menuBar(nullptr)
{}
wxMenuBar* MenuBar::getWidget()
{
if (_menuBar == nullptr)
{
construct();
}
return _menuBar;
}
void MenuBar::construct()
{
_needsRefresh = false;
if (_menuBar != nullptr)
{
MenuElement::constructChildren();
return;
}
_menuBar = new wxMenuBar;
// Set up the listener ensuring that the opened menu is up to date
_menuBar->Bind(wxEVT_MENU_OPEN, [&](wxMenuEvent& ev)
{
MenuElementPtr menu = findMenu(ev.GetMenu());
if (menu && menu->needsRefresh() && std::dynamic_pointer_cast<MenuFolder>(menu))
{
// Rebuild this entire menu
std::static_pointer_cast<MenuFolder>(menu)->refresh();
}
});
MenuElement::constructChildren();
}
void MenuBar::deconstruct()
{
MenuElement::deconstructChildren();
if (_menuBar != nullptr)
{
delete _menuBar;
_menuBar = nullptr;
}
}
MenuElementPtr MenuBar::findMenu(wxMenu* menu)
{
for (const MenuElementPtr& candidate : _children)
{
if (candidate->getWidget() == menu)
{
return candidate;
}
}
return MenuElementPtr();
}
}