-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandStateChain.cpp
More file actions
53 lines (46 loc) · 1.45 KB
/
CommandStateChain.cpp
File metadata and controls
53 lines (46 loc) · 1.45 KB
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
#include "CommandStateChain.h"
#include "Rad/Windowxx.h"
#include "Rad/WinError.h"
namespace
{
inline bool SetFlag(UINT& v, UINT mask, UINT n)
{
const UINT o = v;
_ASSERT((~mask & n) == 0);
v = (~mask & v) | (mask & n);
return o != v;
}
}
LRESULT CommandStateChain::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT ret = 0;
switch (uMsg)
{
HANDLE_MSG(WM_INITMENUPOPUP, OnInitMenuPopup);
}
return ret;
}
void CommandStateChain::OnInitMenuPopup(HMENU hMenu, UINT item, BOOL fSystemMenu)
{
if (fSystemMenu || GetMenuItemID(hMenu, 0) == SC_RESTORE)
{
SetHandled(false);
return;
}
const int count = GetMenuItemCount(hMenu);
MENUITEMINFO mii = {};
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_STATE | MIIM_ID;
for (int i = 0; i < count; ++i)
{
CHECK_LE(GetMenuItemInfo(hMenu, i, TRUE, &mii));
if (mii.wID == 0xFFFF) // SubMenu
continue;
CommandState::State state = { !(mii.fState & MFS_DISABLED), bool(mii.fState & MFS_CHECKED) };
m_pState->GetState(mii.wID, state);
if (SetFlag(mii.fState, MFS_ENABLED | MFS_DISABLED, state.enabled ? MFS_ENABLED : MFS_DISABLED))
SetMenuItemInfo(hMenu, i, TRUE, &mii);
if (SetFlag(mii.fState, MFS_CHECKED | MFS_UNCHECKED, state.checked ? MFS_CHECKED : MFS_UNCHECKED))
SetMenuItemInfo(hMenu, i, TRUE, &mii);
}
}