~serge-hallyn/ubuntu/raring/spice/spice-fixed2

« back to all changes in this revision

Viewing changes to client/menu.cpp

  • Committer: Package Import Robot
  • Author(s): Liang Guo
  • Date: 2011-07-23 12:21:04 UTC
  • Revision ID: package-import@ubuntu.com-20110723122104-gvv70gd6ui6213qd
Tags: upstream-0.8.2
ImportĀ upstreamĀ versionĀ 0.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2009 Red Hat, Inc.
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "common.h"
 
19
#include "menu.h"
 
20
#include "utils.h"
 
21
#include "debug.h"
 
22
 
 
23
Menu::Menu(CommandTarget& target, const std::string& name, int id)
 
24
    : _refs (1)
 
25
    , _target (target)
 
26
    , _name (name)
 
27
    , _id (id)
 
28
{
 
29
}
 
30
 
 
31
Menu::~Menu()
 
32
{
 
33
    clear();
 
34
}
 
35
 
 
36
void Menu::add_item(MenuItem& item)
 
37
{
 
38
    int pos = _items.size();
 
39
    _items.resize(pos + 1);
 
40
    _items[pos] = item;
 
41
}
 
42
 
 
43
void Menu::add_command(const std::string& name, int cmd_id, int state)
 
44
{
 
45
    MenuCommand* cmd = new MenuCommand(name, cmd_id, state);
 
46
    MenuItem item;
 
47
    item.type = MENU_ITEM_TYPE_COMMAND;
 
48
    item.obj = cmd;
 
49
    add_item(item);
 
50
}
 
51
 
 
52
void Menu::add_separator()
 
53
{
 
54
    MenuItem item;
 
55
    item.type = MENU_ITEM_TYPE_SEPARATOR;
 
56
    item.obj = NULL;
 
57
    add_item(item);
 
58
}
 
59
 
 
60
void Menu::add_sub(Menu* menu)
 
61
{
 
62
    ASSERT(menu);
 
63
    MenuItem item;
 
64
    item.type = MENU_ITEM_TYPE_MENU;
 
65
    item.obj = menu->ref();
 
66
    add_item(item);
 
67
}
 
68
 
 
69
void Menu::remove_command(int cmd_id)
 
70
{
 
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);
 
76
            return;
 
77
        }
 
78
    }
 
79
}
 
80
 
 
81
void Menu::remove_sub(Menu* menu)
 
82
{
 
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);
 
87
            return;
 
88
        }
 
89
    }
 
90
}
 
91
 
 
92
Menu::ItemType Menu::item_type_at(int pos)
 
93
{
 
94
    if (pos >= (int)_items.size()) {
 
95
        return MENU_ITEM_TYPE_INVALID;
 
96
    }
 
97
    return _items[pos].type;
 
98
}
 
99
 
 
100
void Menu::command_at(int pos, std::string& name, int& cmd_id, int& state)
 
101
{
 
102
    if (_items[pos].type != MENU_ITEM_TYPE_COMMAND) {
 
103
        THROW("incorrect item type");
 
104
    }
 
105
    MenuCommand* cmd = (MenuCommand*)_items[pos].obj;
 
106
    name = cmd->get_name();
 
107
    cmd_id = cmd->get_cmd_id();
 
108
    state = cmd->get_state();
 
109
}
 
110
 
 
111
Menu* Menu::sub_at(int pos)
 
112
{
 
113
    if (_items[pos].type != MENU_ITEM_TYPE_MENU) {
 
114
        THROW("incorrect item type");
 
115
    }
 
116
    return ((Menu*)_items[pos].obj)->ref();
 
117
}
 
118
 
 
119
Menu* Menu::find_sub(int id)
 
120
{
 
121
    Menu* sub;
 
122
 
 
123
    if (_id == id) {
 
124
        return ref();
 
125
    }
 
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))) {
 
128
            return sub;
 
129
        }
 
130
    }
 
131
    return NULL;
 
132
}
 
133
 
 
134
void Menu::clear()
 
135
{
 
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();
 
141
        }
 
142
    }
 
143
    _items.clear();
 
144
}