summaryrefslogtreecommitdiff
path: root/client/menu.cpp
blob: ebbd323a1adedf1141520ab3f8b053e2f9ec2062 (plain)
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
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "common.h"
#include "menu.h"
#include "utils.h"
#include "debug.h"

Menu::Menu(CommandTarget& target, const std::string& name, int id)
    : _refs (1)
    , _target (target)
    , _name (name)
    , _id (id)
{
}

Menu::~Menu()
{
    clear();
}

void Menu::add_item(MenuItem& item)
{
    int pos = _items.size();
    _items.resize(pos + 1);
    _items[pos] = item;
}

void Menu::add_command(const std::string& name, int cmd_id, int state)
{
    MenuCommand* cmd = new MenuCommand(name, cmd_id, state);
    MenuItem item;
    item.type = MENU_ITEM_TYPE_COMMAND;
    item.obj = cmd;
    add_item(item);
}

void Menu::add_separator()
{
    MenuItem item;
    item.type = MENU_ITEM_TYPE_SEPARATOR;
    item.obj = NULL;
    add_item(item);
}

void Menu::add_sub(Menu* menu)
{
    ASSERT(menu);
    MenuItem item;
    item.type = MENU_ITEM_TYPE_MENU;
    item.obj = menu->ref();
    add_item(item);
}

void Menu::remove_command(int cmd_id)
{
    for (unsigned int i = 0; i < _items.size(); i++) {
        if (_items[i].type == MENU_ITEM_TYPE_COMMAND &&
                ((MenuCommand*)_items[i].obj)->get_cmd_id() == cmd_id) {
            delete (MenuCommand*)_items[i].obj;
            _items.erase(_items.begin() + i);
            return;
        }
    }
}

void Menu::remove_sub(Menu* menu)
{
    for (unsigned int i = 0; i < _items.size(); i++) {
        if (_items[i].type == MENU_ITEM_TYPE_MENU && (Menu*)_items[i].obj == menu) {
            ((Menu*)_items[i].obj)->unref();
            _items.erase(_items.begin() + i);
            return;
        }
    }
}

Menu::ItemType Menu::item_type_at(int pos)
{
    if (pos >= (int)_items.size()) {
        return MENU_ITEM_TYPE_INVALID;
    }
    return _items[pos].type;
}

void Menu::command_at(int pos, std::string& name, int& cmd_id, int& state)
{
    if (_items[pos].type != MENU_ITEM_TYPE_COMMAND) {
        THROW("incorrect item type");
    }
    MenuCommand* cmd = (MenuCommand*)_items[pos].obj;
    name = cmd->get_name();
    cmd_id = cmd->get_cmd_id();
    state = cmd->get_state();
}

Menu* Menu::sub_at(int pos)
{
    if (_items[pos].type != MENU_ITEM_TYPE_MENU) {
        THROW("incorrect item type");
    }
    return ((Menu*)_items[pos].obj)->ref();
}

Menu* Menu::find_sub(int id)
{
    Menu* sub;

    if (_id == id) {
        return ref();
    }
    for (unsigned int i = 0; i < _items.size(); i++) {
        if (_items[i].type == MENU_ITEM_TYPE_MENU && (sub = ((Menu*)_items[i].obj)->find_sub(id))) {
            return sub;
        }
    }
    return NULL;
}

void Menu::clear()
{
    for (unsigned int i = 0; i < _items.size(); i++) {
        if (_items[i].type == MENU_ITEM_TYPE_COMMAND) {
            delete (MenuCommand*)_items[i].obj;
        } else if (_items[i].type == MENU_ITEM_TYPE_MENU) {
            ((Menu*)_items[i].obj)->unref();
        }
    }
    _items.clear();
}