Debian Codegraph @

phoenix/windows/action/menu.cpp

raw
1 namespace phoenix {
2
3 void pMenu::append(Action& action) {
4 action.p.parentMenu = &menu;
5 if(parentWindow) parentWindow->p.updateMenu();
6 }
7
8 void pMenu::remove(Action& action) {
9 if(parentWindow) parentWindow->p.updateMenu();
10 action.p.parentMenu = 0;
11 }
12
13 void pMenu::setImage(const image& image) {
14 createBitmap();
15 if(parentWindow) parentWindow->p.updateMenu();
16 }
17
18 void pMenu::setText(string text) {
19 if(parentWindow) parentWindow->p.updateMenu();
20 }
21
22 void pMenu::constructor() {
23 hmenu = 0;
24 createBitmap();
25 }
26
27 void pMenu::destructor() {
28 if(hbitmap) { DeleteObject(hbitmap); hbitmap = 0; }
29 if(parentMenu) {
30 parentMenu->remove(menu);
31 } else if(parentWindow) {
32 //belongs to window's main menubar
33 parentWindow->remove(menu);
34 }
35 }
36
37 void pMenu::createBitmap() {
38 if(hbitmap) { DeleteObject(hbitmap); hbitmap = 0; }
39
40 if(menu.state.image.width && menu.state.image.height) {
41 nall::image nallImage = menu.state.image;
42 nallImage.transform(0, 32, 255u << 24, 255u << 16, 255u << 8, 255u << 0);
43 nallImage.alphaBlend(GetSysColor(COLOR_MENU)); //Windows does not alpha blend menu icons properly (leaves black outline)
44 nallImage.scale(GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK), Interpolation::Linear);
45 hbitmap = CreateBitmap(nallImage);
46 }
47 }
48
49 //Windows actions lack the ability to toggle visibility.
50 //To support this, menus must be destroyed and recreated when toggling any action's visibility.
51 void pMenu::update(Window& parentWindow, Menu* parentMenu) {
52 this->parentMenu = parentMenu;
53 this->parentWindow = &parentWindow;
54
55 if(hmenu) DestroyMenu(hmenu);
56 hmenu = CreatePopupMenu();
57
58 for(auto& action : menu.state.action) {
59 action.p.parentMenu = &menu;
60 action.p.parentWindow = &parentWindow;
61
62 unsigned enabled = action.state.enabled ? 0 : MF_GRAYED;
63 if(dynamic_cast<Menu*>(&action)) {
64 Menu& item = (Menu&)action;
65 if(action.state.visible) {
66 item.p.update(parentWindow, &menu);
67 AppendMenu(hmenu, MF_STRING | MF_POPUP | enabled, (UINT_PTR)item.p.hmenu, utf16_t(item.state.text));
68
69 if(item.state.image.width && item.state.image.height) {
70 MENUITEMINFO mii = {sizeof(MENUITEMINFO)};
71 //Windows XP and below displays MIIM_BITMAP + hbmpItem in its own column (separate from check/radio marks)
72 //this causes too much spacing, so use a custom checkmark image instead
73 mii.fMask = MIIM_CHECKMARKS;
74 mii.hbmpUnchecked = item.p.hbitmap;
75 SetMenuItemInfo(hmenu, (UINT_PTR)item.p.hmenu, FALSE, &mii);
76 }
77 }
78 } else if(dynamic_cast<Separator*>(&action)) {
79 Separator& item = (Separator&)action;
80 if(action.state.visible) {
81 AppendMenu(hmenu, MF_SEPARATOR | enabled, item.p.id, L"");
82 }
83 } else if(dynamic_cast<Item*>(&action)) {
84 Item& item = (Item&)action;
85 if(action.state.visible) {
86 AppendMenu(hmenu, MF_STRING | enabled, item.p.id, utf16_t(item.state.text));
87
88 if(item.state.image.width && item.state.image.height) {
89 MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
90 //Windows XP and below displays MIIM_BITMAP + hbmpItem in its own column (separate from check/radio marks)
91 //this causes too much spacing, so use a custom checkmark image instead
92 mii.fMask = MIIM_CHECKMARKS;
93 mii.hbmpUnchecked = item.p.hbitmap;
94 SetMenuItemInfo(hmenu, item.p.id, FALSE, &mii);
95 }
96 }
97 } else if(dynamic_cast<CheckItem*>(&action)) {
98 CheckItem& item = (CheckItem&)action;
99 if(action.state.visible) {
100 AppendMenu(hmenu, MF_STRING | enabled, item.p.id, utf16_t(item.state.text));
101 }
102 if(item.state.checked) item.setChecked();
103 } else if(dynamic_cast<RadioItem*>(&action)) {
104 RadioItem& item = (RadioItem&)action;
105 if(action.state.visible) {
106 AppendMenu(hmenu, MF_STRING | enabled, item.p.id, utf16_t(item.state.text));
107 }
108 if(item.state.checked) item.setChecked();
109 }
110 }
111 }
112
113 }