-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathmenu-bar.cpp
98 lines (78 loc) · 2.13 KB
/
menu-bar.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
96
97
98
#if defined(Hiro_MenuBar)
namespace hiro {
auto pMenuBar::construct() -> void {
_update();
}
auto pMenuBar::destruct() -> void {
pApplication::state().staleMenus.removeByValue(this);
if(hmenu) { DestroyMenu(hmenu); hmenu = nullptr; }
if(auto parent = _parent()) {
SetMenu(parent->hwnd, nullptr);
}
}
auto pMenuBar::append(sMenu) -> void {
_update();
}
auto pMenuBar::remove(sMenu) -> void {
_update();
}
auto pMenuBar::setEnabled(bool enabled) -> void {
_update();
}
auto pMenuBar::setFont(const Font& font) -> void {
//unsupported
}
auto pMenuBar::setVisible(bool visible) -> void {
if(auto parent = _parent()) {
SetMenu(parent->hwnd, visible ? hmenu : nullptr);
parent->setGeometry(parent->state().geometry);
}
}
auto pMenuBar::_parent() -> maybe<pWindow&> {
if(auto parent = self().parentWindow(true)) {
if(auto self = parent->self()) return *self;
}
return nothing;
}
auto pMenuBar::_update() -> void {
bool updateNow = false;
if(auto parent = _parent()) {
updateNow = GetMenu(parent->hwnd) == nullptr;
}
if (updateNow) {
_updateDeferred();
} else {
pApplication::state().staleMenus.removeByValue(this);
pApplication::state().staleMenus.append(this);
}
}
auto pMenuBar::_updateDeferred() -> void {
if(hmenu) DestroyMenu(hmenu);
hmenu = CreateMenu();
MENUINFO mi{sizeof(MENUINFO)};
mi.fMask = MIM_STYLE;
mi.dwStyle = MNS_NOTIFYBYPOS; //| MNS_MODELESS;
SetMenuInfo(hmenu, &mi);
unsigned position = 0;
#if defined(Hiro_Menu)
for(auto& menu : state().menus) {
unsigned enabled = menu->enabled() ? 0 : MF_GRAYED;
MENUITEMINFO mii{sizeof(MENUITEMINFO)};
mii.fMask = MIIM_DATA;
mii.dwItemData = (ULONG_PTR)menu.data();
if(menu->visible()) {
if(auto self = menu->self()) {
self->_update();
AppendMenu(hmenu, MF_STRING | MF_POPUP | enabled, (UINT_PTR)self->hmenu, utf16_t(menu->text()));
SetMenuItemInfo(hmenu, position++, true, &mii);
}
}
}
#endif
if(auto parent = _parent()) {
SetMenu(parent->hwnd, self().visible(true) ? hmenu : nullptr);
parent->setGeometry(parent->state().geometry);
}
}
}
#endif