GUIFramework 1.1.0
Framework for desktop GUI applications in C++.
Loading...
Searching...
No Matches
Menu.cpp
Go to the documentation of this file.
1#include "Menu.h"
2
3#include "Utility/Utility.h"
4
5using namespace std;
6
7namespace gui_framework
8{
10 handle(nullptr),
11 parent(nullptr)
12 {
13
14 }
15
16 Menu::Menu(const wstring& name, HWND parent) :
17 name(name),
18 handle(CreateMenu()),
19 parent(parent)
20 {
21 MENUINFO info = {};
22
23 info.cbSize = sizeof(info);
24 info.dwStyle = MNS_NOTIFYBYPOS;
25 info.fMask = MIM_STYLE;
26
27 SetMenuInfo(handle, &info);
28
29 if (parent)
30 {
31 SetMenu(parent, handle);
32 }
33 }
34
35 Menu::Menu(Menu&& other) noexcept :
36 name(move(other.name)),
37 handle(other.handle),
38 items(move(other.items)),
39 parent(other.parent)
40 {
41 other.handle = nullptr;
42 other.parent = nullptr;
43 }
44
45 Menu& Menu::operator = (Menu&& other) noexcept
46 {
47 name = move(other.name);
48 handle = other.handle;
49 items = move(other.items);
50 parent = other.parent;
51
52 other.handle = nullptr;
53 other.parent = nullptr;
54
55 return *this;
56 }
57
58 Menu& Menu::addMenuItem(unique_ptr<interfaces::IMenuItem>&& item)
59 {
60 items.push_back(move(item));
61
62 items.back()->createMenuItem(handle);
63 items.back()->setIndex(static_cast<uint32_t>(items.size() - 1));
64
65 this->updateMenu();
66
67 return *this;
68 }
69
70 void Menu::removeMenuItem(uint32_t index)
71 {
72 items.erase(items.begin() + index);
73
74 for (uint32_t i = index; i < items.size(); i++)
75 {
76 items[i]->setIndex(i);
77 }
78
79 this->updateMenu();
80 }
81
82 void Menu::handleMessage(uint32_t index)
83 {
84 items[index]->processMessage();
85 }
86
87 void Menu::updateMenu() const
88 {
89 DrawMenuBar(parent);
90 }
91
92 const wstring& Menu::getName() const
93 {
94 return name;
95 }
96
97 const vector<unique_ptr<interfaces::IMenuItem>>& Menu::getItems() const
98 {
99 return items;
100 }
101
102 HMENU Menu::getHandle() const
103 {
104 return handle;
105 }
106
107 json::JSONBuilder Menu::getStructure() const
108 {
109 uint32_t codepage = ISerializable::getCodepage();
110 json::JSONBuilder builder(codepage);
111
112 vector<json::utility::jsonObject> children;
113
114 for (const auto& i : items)
115 {
116 json::utility::jsonObject child;
117 json::JSONBuilder structure = i->getStructure();
118
119 const string& itemText = get<string>(structure["itemText"]);
120 const string& itemType = get<string>(structure["itemType"]);
121
122 child.data.push_back({ "itemText"s, itemText });
123 child.data.push_back({ "itemType"s, itemType });
124
125 if (structure.contains("functionName", json::utility::variantTypeEnum::jString))
126 {
127 child.data.push_back({ "functionName"s, get<string>(structure["functionName"]) });
128 child.data.push_back({ "moduleName"s, get<string>(structure["moduleName"]) });
129 }
130
132 {
133 child.data.push_back({ "popupId"s, get<uint64_t>(structure["popupId"]) });
134 }
135
136 json::utility::appendArray(move(child), children);
137 }
138
139 builder.
140 append("menuName"s, utility::to_string(name, codepage)).
141 append("menuId"s, reinterpret_cast<uint64_t>(handle)).
142 append("items"s, move(children));
143
144 return builder;
145 }
146
148 {
149 DestroyMenu(handle);
150 }
151}
Menu class.
Definition Menu.h:9
const std::vector< std::unique_ptr< interfaces::IMenuItem > > & getItems() const
Definition Menu.cpp:97
void updateMenu() const
Definition Menu.cpp:87
void handleMessage(uint32_t index)
Definition Menu.cpp:82
const std::wstring & getName() const
Definition Menu.cpp:92
json::JSONBuilder getStructure() const override
Definition Menu.cpp:107
HMENU getHandle() const
Definition Menu.cpp:102
void removeMenuItem(uint32_t index)
Definition Menu.cpp:70
Menu & addMenuItem(std::unique_ptr< interfaces::IMenuItem > &&item)
Also calls item's createMenuItem method.
Definition Menu.cpp:58
Menu & operator=(const Menu &)=delete
string to_string(wstring_view stringToConvert, uint32_t codepage)
Definition Utility.cpp:41