-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSelection.cc
More file actions
219 lines (175 loc) · 5.56 KB
/
Selection.cc
File metadata and controls
219 lines (175 loc) · 5.56 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "Selection.h"
#include "Interpreter.h"
#include "InputLine.h"
#include "StatusLine.h"
#include "StaticBuffer.h"
#include "TTY.h"
#include "MUD.h"
#include "Config.h"
#include "Color.h"
Selection::Selection(Window *_parent, int _w, int _h, int _x, int _y)
: Window (_parent, _w, _h, Bordered, _x, _y), count(0), selection(-1) {
parent->focus(this);
}
Selection::~Selection() {
for (char *s = data.rewind(); s; s = data.next()) {
data.remove(s);
free(s);
}
}
void Selection::add_string (const char *s, int color) {
data.insert(strdup(s));
colors.insert(color);
if (count++ == selection+1) // move selection down if we had the last one
doSelect(++selection); // selected
}
void Selection::prepend_string (const char *s, int color) {
data.append(strdup(s));
colors.append(color);
if (count++ == selection+1) // move selection down if we had the last one
doSelect(++selection); // selected
}
const char * Selection::getData(int i) {
return data[i];
}
void Selection::redraw() {
const char *data;
set_color(bg_blue|fg_white);
clear();
if (!children.empty())
inputLine->redraw(); // aack. how else do we reset this f** cursor?
int top = max(0, selection - height/2);
top = min(max(0,count-height), top);
for (int _y = 0; _y < height && (_y+top) < count; _y++) {
gotoxy(0,_y);
if ( _y+top == selection) // this is the selected line, use another color
set_color(bg_green|fg_black);
else {
int color = colors[_y+top];
if (color)
set_color(color);
else
set_color(bg_blue|fg_white);
}
printf ("%-*.*s", width, width, (data = getData(_y+top)) ? data : "");
}
dirty = false;
}
bool Selection::keypress(int key) {
dirty = true;
if (Window::keypress(key))
return true;
if (selection >= 0) {
if (key == key_arrow_up)
doSelect(selection = max(0,selection-1));
else if (key == key_arrow_down)
doSelect(selection = min(selection+1,count-1));
else if (key == key_page_up)
doSelect(selection = max(0, selection - height/2));
else if (key == key_page_down)
doSelect(selection = min(selection + height/2 , count -1));
else if (key == key_home)
doSelect(selection = 0);
else if (key == key_end)
doSelect(selection = count-1);
else if (key >= ' ' && key < 128) { // jump to the first string with that letter
int i, start;
if (count == 0)
status->setf("There is no data!");
else
{
if (*getData(selection) == key)
start = selection+1;
else
start = 0;
for (i = start; i-start < count; i++)
if (*getData(i%count) == key)
break;
if ((i-start) == count)
status->setf ("Nothing in list that starts with '%c'", key);
else
selection = i%count;
}
}
else if (key == '\n' || key == '\r') {
doChoose(selection, key);
}
else if (key == key_arrow_right) {
doChoose(selection, key);
}
else if (key == key_escape)
die();
else
return false;
} else {
if (key == '\n' || key == '\r') {
die();
}
else if (key == key_escape)
die();
else if (key == key_arrow_right || key == key_arrow_left || key == key_page_up || key == key_arrow_up || key == key_arrow_down
|| key == key_page_down || key == key_home || key == key_end
|| (key >= ' ' && key < 128))
status->setf("There is no data in this list!");
else
return false;
}
return true;
}
void Selection::doSelect (int) {
}
void Selection::doChoose(int, int) {
die();
}
void Selection::setCount(int no) {
count = no;
if (selection >= count)
selection = count-1;
else if (selection == -1 && count > 0)
selection = 0;
force_update();
}
void MUDSelection::doChoose(int no, int) {
MUD *m = (*config->mud_list)[no];
if (m)
{
int CmdChar = config->getOption(opt_commandcharacter);
interpreter.add(Sprintf("%copen %s", CmdChar, m->getName()));
}
die();
}
MUDSelection::MUDSelection (Window *_parent)
: Selection (_parent, _parent->width, _parent->height/2, 0, _parent->height/4)
{
parent->focus(this);
setCount(config->mud_list->count());
doSelect(0);
set_top_message("Mud Selector");
}
const char * MUDSelection::getData(int no) {
static char buf[256];
MUD *mud = (*config->mud_list)[no];
if (mud) {
if (strlen(mud->getHostname()))
snprintf (buf, min(width,256), "%-20s %-20s %4d %s", mud->getName(),
mud->getHostname(), mud->getPort(), mud->getCommands());
else
snprintf (buf, min(width,256), "%s", mud->getName());
return buf;
} else
return "";
}
void MUDSelection::doSelect (int no) {
char buf[128];
if ((*config->mud_list)[no]) {
sprintf (buf, "This mud's name: %s", (*config->mud_list)[no]->getName());
set_bottom_message (buf);
}
}
bool MUDSelection::keypress(int key) {
if (key == key_alt_o)
status->setf("It's already open!");
else
return Selection::keypress(key);
return true;
}