2
Copyright (C) 2009 Red Hat, Inc.
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; either
7
version 2.1 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public
15
License along with this library; if not, see <http://www.gnu.org/licenses/>.
23
Menu::Menu(CommandTarget& target, const std::string& name, int id)
36
void Menu::add_item(MenuItem& item)
38
int pos = _items.size();
39
_items.resize(pos + 1);
43
void Menu::add_command(const std::string& name, int cmd_id, int state)
45
MenuCommand* cmd = new MenuCommand(name, cmd_id, state);
47
item.type = MENU_ITEM_TYPE_COMMAND;
52
void Menu::add_separator()
55
item.type = MENU_ITEM_TYPE_SEPARATOR;
60
void Menu::add_sub(Menu* menu)
64
item.type = MENU_ITEM_TYPE_MENU;
65
item.obj = menu->ref();
69
void Menu::remove_command(int cmd_id)
71
for (unsigned int i = 0; i < _items.size(); i++) {
72
if (_items[i].type == MENU_ITEM_TYPE_COMMAND &&
73
((MenuCommand*)_items[i].obj)->get_cmd_id() == cmd_id) {
74
delete (MenuCommand*)_items[i].obj;
75
_items.erase(_items.begin() + i);
81
void Menu::remove_sub(Menu* menu)
83
for (unsigned int i = 0; i < _items.size(); i++) {
84
if (_items[i].type == MENU_ITEM_TYPE_MENU && (Menu*)_items[i].obj == menu) {
85
((Menu*)_items[i].obj)->unref();
86
_items.erase(_items.begin() + i);
92
Menu::ItemType Menu::item_type_at(int pos)
94
if (pos >= (int)_items.size()) {
95
return MENU_ITEM_TYPE_INVALID;
97
return _items[pos].type;
100
void Menu::command_at(int pos, std::string& name, int& cmd_id, int& state)
102
if (_items[pos].type != MENU_ITEM_TYPE_COMMAND) {
103
THROW("incorrect item type");
105
MenuCommand* cmd = (MenuCommand*)_items[pos].obj;
106
name = cmd->get_name();
107
cmd_id = cmd->get_cmd_id();
108
state = cmd->get_state();
111
Menu* Menu::sub_at(int pos)
113
if (_items[pos].type != MENU_ITEM_TYPE_MENU) {
114
THROW("incorrect item type");
116
return ((Menu*)_items[pos].obj)->ref();
119
Menu* Menu::find_sub(int id)
126
for (unsigned int i = 0; i < _items.size(); i++) {
127
if (_items[i].type == MENU_ITEM_TYPE_MENU && (sub = ((Menu*)_items[i].obj)->find_sub(id))) {
136
for (unsigned int i = 0; i < _items.size(); i++) {
137
if (_items[i].type == MENU_ITEM_TYPE_COMMAND) {
138
delete (MenuCommand*)_items[i].obj;
139
} else if (_items[i].type == MENU_ITEM_TYPE_MENU) {
140
((Menu*)_items[i].obj)->unref();