- Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathmenu.cpp
155 lines (122 loc) · 4.97 KB
/
menu.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0,
* as published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include"mforms/mforms.h"
usingnamespacemforms;
Menu::Menu() {
_menu_impl = &ControlFactory::get_instance()->_menu_impl;
_menu_impl->create(this);
}
boolMenu::empty() const {
return _item_map.size() == 0;
}
voidMenu::remove_item(int i) {
_menu_impl->remove_item(this, i);
std::string to_remove;
for (std::map<const std::string, int>::iterator index = _item_map.begin(); index != _item_map.end(); index++) {
if ((*index).second == i)
to_remove = (*index).first;
elseif ((*index).second > i)
(*index).second--;
}
if (!to_remove.empty())
_item_map.erase(to_remove);
}
intMenu::add_item(const std::string &caption, const std::string &action) {
int item_index = _menu_impl->add_item(this, caption, action);
_item_map[action] = item_index;
return item_index;
}
intMenu::add_separator() {
return _menu_impl->add_separator(this);
}
intMenu::add_submenu(const std::string &caption, Menu *submenu) {
// Connect our action handler to the sub menu so all commands can be triggered via the top
// menu in the hierarchy.
submenu->signal_on_action()->connect(std::bind(&Menu::handle_action, this, std::placeholders::_1));
return _menu_impl->add_submenu(this, caption, submenu);
}
//--------------------------------------------------------------------------------------------------
/**
* Adds menu entries for all the items in the given list.
*/
voidMenu::add_items_from_list(const bec::MenuItemList &list) {
for (bec::MenuItemList::const_iterator item = list.begin(); item != list.end(); ++item) {
if (item->type == bec::MenuAction) {
int i = add_item(item->caption, item->internalName);
set_item_enabled(i, item->enabled);
} elseif (item->type == bec::MenuSeparator)
add_separator();
elseif (item->type == bec::MenuCascade) {
Menu *submenu = mforms::manage(newmforms::Menu());
submenu->add_items_from_list(item->subitems);
int i = add_submenu(item->caption, submenu);
set_item_enabled(i, item->enabled);
}
}
}
//--------------------------------------------------------------------------------------------------
voidMenu::set_item_enabled(int i, bool flag) {
_menu_impl->set_item_enabled(this, i, flag);
}
voidMenu::set_item_enabled(const std::string &action, bool flag) {
int i = get_item_index(action);
if (i < 0)
throwstd::invalid_argument("invalid menu action " + action);
_menu_impl->set_item_enabled(this, i, flag);
}
voidMenu::set_handler(const std::function<void(const std::string &)> &action_handler) {
_action_handler = action_handler;
}
voidMenu::popup_at(Object *control, int x, int y) {
#ifndef _MSC_VER
_on_will_show(); // Popping up the menu will trigger the on_show event on Win.
#endif
_menu_impl->popup_at(this, control, x, y);
}
voidMenu::popup() {
#ifndef _MSC_VER
_on_will_show(); // Popping up the menu will trigger the on_show event on Win.
#endif
_menu_impl->popup_at(this, 0, 0, 0);
}
//--------------------------------------------------------------------------------------------------
voidMenu::handle_action(const std::string &action) {
if (_action_handler)
_action_handler(action);
_on_action(action);
}
//--------------------------------------------------------------------------------------------------
voidmforms::Menu::clear() {
_menu_impl->clear(this);
_item_map.clear();
}
//--------------------------------------------------------------------------------------------------
intmforms::Menu::get_item_index(const std::string &action) {
int item_index = -1;
std::map<const std::string, int>::iterator location = _item_map.find(action);
if (location != _item_map.end())
item_index = _item_map[action];
return item_index;
}
//--------------------------------------------------------------------------------------------------