-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindowSet.cpp
More file actions
300 lines (281 loc) · 10.4 KB
/
WindowSet.cpp
File metadata and controls
300 lines (281 loc) · 10.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include<seq_view.h>
namespace SeqView {
void WindowSet::adjust_to_fill_evenly() {
if(windows.size() == 0)
return;
// Space the windows equally in vertical distance
// and fill full horizontal space
update_size();
std::vector<int> newheights;
int used_height = 0;
int h = (int) ((height - 1) / (double)windows.size());
unsigned win = 0;
int adj = 0;
while(win < windows.size()) {
if(height - used_height < h)
h = height - used_height - 1;
if(win == windows.size() - 1)
if(height - used_height > h + 1)
h = height - used_height - 1;
windows[win] -> resize(0, used_height + adj, width, h);
used_height += h;
win++;
}
}
void WindowSet::adjust_to_fill_proportionally() {
if(windows.size() == 0)
return;
// Space the windows vertically so that they take up no more
// space than needed and fill full horizontal space
update_size();
std::vector<int> newheights;
int even_height = (int) ((height - 1) / (double)windows.size());
int n_less_than_even_height = 0;
int space_less_than_even_height = 0;
for(auto const & window: windows) {
int64_t wn = window -> numseqs() + 3;
if(wn < even_height) {
n_less_than_even_height++;
space_less_than_even_height += wn;
}
}
// Calculate the vertical space needed by the windows with
// large numbers of sequences.
int large_height = (int) ((height - space_less_than_even_height - 1) /
(double)(windows.size()-n_less_than_even_height));
int used_height = 0;
unsigned win = 0;
int adj = 0;
int h = 0;
while(win < windows.size()) {
SeqWindow* window = windows[win];
if(height - used_height < large_height)
large_height = height - used_height - 1;
if(win == windows.size() - 1) {
if(height - used_height > large_height + 1)
large_height = height - used_height - 1;
window -> resize(0, used_height + adj, width, large_height);
h = large_height;
} else {
int64_t wn = window -> numseqs() + 3;
if(wn < even_height) {
window -> resize(0, used_height + adj, width, wn);
h = wn;
} else {
window -> resize(0, used_height + adj, width, large_height);
h = large_height;
}
}
used_height += h;
win++;
}
}
WindowSet::WindowSet() {
getmaxyx(stdscr, height, width);
focal_window = 0;
std::vector<SeqWindow*> windows();
}
void WindowSet::update_size() {
getmaxyx(stdscr, height, width);
}
void WindowSet::print_message(string message) {
wattron(stdscr, A_NORMAL | A_BOLD);
mvwprintw(stdscr, height - 1, 0, string(width, ' ').c_str());
mvwprintw(stdscr, height - 1, 0, message.c_str());
wattroff(stdscr, A_BOLD);
}
void WindowSet::update() {
int xtemp = width;
int ytemp = height;
update_size();
if(width != xtemp || height != ytemp)
adjust_to_fill_proportionally();
for(unsigned i = 0; i < windows.size(); i++) {
windows[i] -> display();
}
refresh();
}
void WindowSet::add_window(string filename) {
try {
SeqWindow * s = new SeqWindow(0, 0, width, height,
filename);
windows.push_back(s);
update_size();
adjust_to_fill_proportionally();
change_focus(windows.size() - 1);
update();
return;
} catch(...) {
print_message("Cannot open file " + filename);
}
}
void WindowSet::change_focus(int newfocus) {
if(newfocus > -1 && newfocus < (int)windows.size()) {
windows[focal_window] -> set_focus(false);
focal_window = newfocus;
windows[focal_window] -> set_focus(true);
update();
}
}
// Return true if the program should keep going,
// otherwise false; If there any windows left,
// keep going.
bool WindowSet::close_focus() {
if(windows.size() == 0) {
return false;
} else {
SeqWindow * s = windows[focal_window];
windows.erase(windows.begin() + focal_window);
delete s;
if(focal_window >= windows.size())
focal_window = 0;
change_focus(focal_window);
update_size();
adjust_to_fill_proportionally();
update();
return (windows.size() > 0);
}
}
void WindowSet::close_all() {
if(windows.size() != 0) {
int i = 0;
while(windows.size() > 0) {
SeqWindow * s = windows[i];
windows.erase(windows.begin() + i);
delete s;
}
}
}
bool WindowSet::handle_command(Command command) {
if(seqSetCommands.count(command.first) > 0 &&
windows.size() > 0) {
windows[focal_window] -> handle_command(command);
return true;
} else {
if(command.first == QUIT)
return close_focus();
if(command.first == ALLQUIT) {
close_all();
return false;
}
if(command.first == CHANGEFOCUS) {
if(command.second == 0) {
command.second = focal_window + 2; // Add 2 b/c command is 1 based, but focal_window is 0 based
if(command.second > (int)windows.size())
command.second = 1;
}
change_focus(command.second - 1);
return true;
}
if(command.first == CHANGEFOCUSREV) {
command.second = focal_window; // Subtract nothing b/c command is 1 based, but focal_window is 0 based
if(command.second == 0)
command.second = windows.size();
change_focus(command.second - 1);
return true;
}
if(command.first == RESIZE) {
update();
}
if(command.first == SPECIAL) {
handle_special_command();
return true;
}
}
return true;
}
void WindowSet::handle_special_command() {
/*
* TODO:
* 2) Implement a simple command history
* 3) Allow commands to stretch over more than one line:
* - keep track of line length relative to window width
* - keep track of which line
* - when command is run, remember to redisplay the bottom
* window if the command took up multiple lines.
* 4) DELETE KEY
* 6) Tab file completion
*/
for(int i = 0; i <= width; i++)
mvwaddch(stdscr, height - 1, i, ' ');
mvwprintw(stdscr, height - 1, 0, ";");
string buffer = "";
int cursor = 0; // where to insert the new character
//int line = height - 1; // line to print commands - to deal with
// // long commands (not implemented yet)
wchar_t ch;
curs_set(1);
while(true) {
ch = getch();
// Eventually keep a stack of previous commands
// and cycle through them with UP and DOWN.
// Backspace
if(ch == KEY_BACKSPACE || ch == KEY_DL || ch == 7) {
if(buffer.size()) {
backspace();
cursor -= 1;
}
if(buffer.size())
buffer.erase(buffer.size() - 1);
continue;
}
if(ch == KEY_ENTER || ch == '\n' || ch == '\r')
break;
if(ch < 27 || ch > 126)
continue;
if(ch == 27) {
for(int i = 0; i <= width; i++)
mvwaddch(stdscr, height - 1, i, ' ');
return; // ESC key
}
waddch(stdscr, ch); // Also modify to deal with cursor
buffer += ch; // This can be modified to deal with arrow keys
cursor += 1;
}
curs_set(0);
// break buffer into tokens separated by spaces
// and process
if(buffer.size() > 0) {
std::vector<string> tokens;
string tok = "";
for(unsigned i = 0; i < buffer.size(); i++) {
if(buffer[i] == ' ') {
tokens.push_back(string(tok));
// Get rid of these printing commands
printw(tok.c_str());
printw(" ");
tok = "";
} else {
tok += buffer[i];
}
}
if(tok.size())
tokens.push_back(string(tok));
// open file
// Eventually this should allow file completion
// on tab.
if(!tokens[0].compare("open") && tokens.size() == 2) {
clear_line(height - 1);
add_window(tokens[1]);
} else if(!tokens[0].compare("mode") && tokens.size() == 2) {
if(!tokens[1].compare("ten"))
handle_command(Command(DISPLAYMODE, 3));
if(!tokens[1].compare("codon"))
handle_command(Command(DISPLAYMODE, 2));
if(!tokens[1].compare("normal"))
handle_command(Command(DISPLAYMODE, 1));
clear_line(height - 1);
} else if(!tokens[0].compare("bold") && tokens.size() == 2) {
handle_command(Command(TOGGLEBOLD, 0));
} else if(!tokens[0].compare("compare-mode") &&
tokens.size() == 2 && CompModeMap.count(string(tokens[1]))) {
handle_command(Command(COMPARE, CompModeMap[string(tokens[1])]));
clear_line(height-1);
} else {
clear_line(height - 1);
}
} else {
clear_line(height - 1);
}
}
}