-
Notifications
You must be signed in to change notification settings - Fork 9
/
Panel.c
432 lines (377 loc) · 11.2 KB
/
Panel.c
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "Prototypes.h"
//#needs Object RichString Display
#include <math.h>
#include <sys/param.h>
#include <stdbool.h>
/*{
typedef enum HandlerResult_ {
HANDLED,
IGNORED,
BREAK_LOOP
} HandlerResult;
typedef HandlerResult(*Method_Panel_eventHandler)(Panel*, int);
struct PanelClass_ {
ObjectClass super;
};
struct Panel_ {
Object super;
int x, y, w, h;
List* items;
int selected;
int scrollV, scrollH;
int oldSelected;
bool needsRedraw;
RichString header;
bool highlightBar;
int cursorX;
int displaying;
int color;
bool focus;
Method_Panel_eventHandler eventHandler;
};
extern PanelClass PanelType;
}*/
PanelClass PanelType = {
.super = {
.size = sizeof(Panel),
.display = NULL,
.equals = Object_equals,
.delete = Panel_delete
}
};
Panel* Panel_new(int x, int y, int w, int h, int color, ListItemClass* class, bool owner, void* data) {
Panel* this = Alloc(Panel);
Panel_init(this, x, y, w, h, color, class, owner, data);
return this;
}
void Panel_delete(Object* cast) {
Panel* this = (Panel*)cast;
RichString_end(this->header);
Panel_done(this);
free(this);
}
void Panel_init(Panel* this, int x, int y, int w, int h, int color, ListItemClass* class, bool owner, void* data) {
this->x = x;
this->y = y;
this->w = w;
this->h = h;
this->color = color;
this->eventHandler = NULL;
this->items = List_new(class, data);
this->scrollV = 0;
this->scrollH = 0;
this->selected = 0;
this->oldSelected = 0;
this->needsRedraw = true;
this->highlightBar = false;
this->cursorX = 0;
this->displaying = 0;
this->focus = true;
RichString_beginAllocated(this->header);
}
void Panel_done(Panel* this) {
assert (this != NULL);
List_delete(this->items);
}
void Panel_setFocus(Panel* this, bool focus) {
this->focus = focus;
}
void Panel_setHeader(Panel* this, RichString header) {
assert (this != NULL);
this->header = header;
this->needsRedraw = true;
}
void Panel_move(Panel* this, int x, int y) {
assert (this != NULL);
this->x = x;
this->y = y;
this->needsRedraw = true;
}
void Panel_resize(Panel* this, int w, int h) {
assert (this != NULL);
if (RichString_sizeVal(this->header) > 0)
h--;
this->w = w;
this->h = h;
this->needsRedraw = true;
}
void Panel_prune(Panel* this) {
assert (this != NULL);
List_prune(this->items);
this->scrollV = 0;
this->selected = 0;
this->oldSelected = 0;
this->needsRedraw = true;
}
void Panel_add(Panel* this, ListItem* l) {
assert (this != NULL);
List_add(this->items, l);
this->needsRedraw = true;
}
void Panel_set(Panel* this, int i, ListItem* l) {
assert (this != NULL);
List_set(this->items, i, l);
}
ListItem* Panel_get(Panel* this, int i) {
assert (this != NULL);
return List_get(this->items, i);
}
Object* Panel_remove(Panel* this, int i) {
assert (this != NULL);
this->needsRedraw = true;
Object* removed = NULL;
List_remove(this->items, i);
if (this->selected > 0 && this->selected >= List_size(this->items))
this->selected--;
return removed;
}
ListItem* Panel_getSelected(Panel* this) {
assert (this != NULL);
return List_get(this->items, this->selected);
}
/*
void Panel_moveSelectedUp(Panel* this) {
assert (this != NULL);
List_moveUp(this->items, this->selected);
if (this->selected > 0)
this->selected--;
}
void Panel_moveSelectedDown(Panel* this) {
assert (this != NULL);
List_moveDown(this->items, this->selected);
if (this->selected + 1 < List_size(this->items))
this->selected++;
}
*/
int Panel_getSelectedIndex(Panel* this) {
assert (this != NULL);
return this->selected;
}
int Panel_size(Panel* this) {
assert (this != NULL);
return List_size(this->items);
}
void Panel_setSelected(Panel* this, int selected) {
assert (this != NULL);
selected = MAX(0, MIN(List_size(this->items) - 1, selected));
this->selected = selected;
}
void Panel_draw(Panel* this) {
assert (this != NULL);
int cursorY = 0;
int first, last;
int itemCount = List_size(this->items);
int scrollH = this->scrollH;
int y = this->y; int x = this->x;
int w = this->w; int h = this->h;
first = this->scrollV;
if (h > itemCount) {
last = itemCount;
} else {
last = MIN(itemCount, this->scrollV + h);
}
if (this->selected < first) {
first = this->selected;
this->scrollV = first;
this->needsRedraw = true;
}
if (this->selected >= last) {
last = MIN(itemCount, this->selected + 1);
first = MAX(0, last - h);
this->scrollV = first;
this->needsRedraw = true;
}
assert(first >= 0);
assert(last <= itemCount);
int headerLen = RichString_sizeVal(this->header);
if (headerLen > 0) {
Display_attrset(CRT_colors[HeaderColor]);
Display_mvhline(y, x, ' ', w);
if (scrollH < headerLen) {
assert(headerLen > 0);
Display_writeChstrAtn(y, x, RichString_at(this->header, scrollH),
MIN(headerLen - scrollH, w));
}
y++;
}
scrollH = 0;
int highlight;
if (this->focus) {
highlight = CRT_colors[SelectionColor];
} else {
highlight = CRT_colors[UnfocusedSelectionColor];
}
Display_attrset(this->color);
if (this->needsRedraw) {
for(int i = first, j = 0; j < h && i < last; i++, j++) {
Object* itemObj = (Object*) List_get(this->items, i);
assert(itemObj);
RichString_begin(itemRef);
this->displaying = i;
Msg(Object, display, itemObj, &itemRef);
int amt = MIN(RichString_sizeVal(itemRef) - scrollH, w);
if (i == this->selected) {
if (this->highlightBar) {
Display_attrset(highlight);
RichString_setAttr(&itemRef, highlight);
}
cursorY = y + j;
Display_mvhline(cursorY, x+amt, ' ', w-amt);
if (amt > 0)
Display_writeChstrAtn(y+j, x+0, RichString_at(itemRef, scrollH), amt);
if (this->highlightBar)
Display_attrset(this->color);
} else {
Display_mvhline(y+j, x+amt, ' ', w-amt);
if (amt > 0)
Display_writeChstrAtn(y+j, x+0, RichString_at(itemRef, scrollH), amt);
}
RichString_end(itemRef);
}
for (int i = y + (last - first); i < y + h; i++)
Display_mvhline(i, x+0, ' ', w);
/* paint scrollbar */
{
int nBlocks = h * 2;
float linesPerBlock = (float)itemCount / (float)nBlocks;
int handleHeight = ceil((float)h / linesPerBlock);
int startAt = (float)this->scrollV / linesPerBlock;
Color bar = CRT_colors[ScrollBarColor];
Color handle = CRT_colors[ScrollHandleColor];
Color handleTop = CRT_colors[ScrollHandleTopColor];
Color handleBottom = CRT_colors[ScrollHandleBottomColor];
RichString_begin(barStr);
for (int i = 0; i < nBlocks; i += 2) {
bool set1 = false;
bool set2 = false;
if (i >= startAt && handleHeight) {
set1 = true;
handleHeight--;
}
if (i+1 >= startAt && handleHeight) {
set2 = true;
handleHeight--;
}
if (set1 && set2) {
RichString_write(&barStr, handle, CRT_scrollHandle);
} else if (set1) {
RichString_write(&barStr, handleTop, CRT_scrollHandleTop);
} else if (set2) {
RichString_write(&barStr, handleBottom, CRT_scrollHandleBottom);
} else {
RichString_write(&barStr, bar, CRT_scrollBar);
}
Display_writeChstrAtn(y + (i/2), w, RichString_at(barStr, 0), 1);
}
RichString_end(barStr);
Display_attrset(this->color);
}
this->needsRedraw = false;
} else {
Object* oldObj = (Object*) List_get(this->items, this->oldSelected);
RichString_begin(oldRef);
this->displaying = this->oldSelected;
Msg(Object, display, oldObj, &oldRef);
Object* newObj = (Object*) List_get(this->items, this->selected);
RichString_begin(newRef);
this->displaying = this->selected;
Msg(Object, display, newObj, &newRef);
Display_mvhline(y+ this->oldSelected - this->scrollV, x+0, ' ', w);
int oldLen = RichString_sizeVal(oldRef);
if (scrollH < oldLen)
Display_writeChstrAtn(y+ this->oldSelected - this->scrollV, x+0, RichString_at(oldRef, scrollH), MIN(oldLen - scrollH, w));
if (this->highlightBar)
Display_attrset(highlight);
cursorY = y+this->selected - this->scrollV;
Display_mvhline(cursorY, x+0, ' ', w);
if (this->highlightBar)
RichString_setAttr(&newRef, highlight);
int newLen = RichString_sizeVal(newRef);
if (scrollH < newLen)
Display_writeChstrAtn(y+this->selected - this->scrollV, x+0, RichString_at(newRef, scrollH), MIN(newLen - scrollH, w));
if (this->highlightBar)
Display_attrset(this->color);
RichString_end(oldRef);
RichString_end(newRef);
}
this->oldSelected = this->selected;
Display_move(cursorY, this->cursorX);
}
void Panel_slide(Panel* this, int n) {
while (n != 0) {
if (n > 0) {
if (this->selected + 1 < List_size(this->items)) {
this->selected++;
if (this->scrollV < List_size(this->items) - this->h) {
this->scrollV++;
this->needsRedraw = true;
}
}
n--;
} else {
if (this->selected > 0) {
this->selected--;
if (this->scrollV > 0) {
this->scrollV--;
this->needsRedraw = true;
}
}
n++;
}
}
}
bool Panel_onKey(Panel* this, int key) {
assert (this != NULL);
switch (key) {
case KEY_DOWN:
if (this->selected + 1 < List_size(this->items))
this->selected++;
return true;
case KEY_UP:
if (this->selected > 0)
this->selected--;
return true;
case KEY_C_DOWN:
Panel_slide(this, 1);
return true;
case KEY_C_UP:
Panel_slide(this, -1);
return true;
case KEY_LEFT:
if (this->scrollH > 0) {
this->scrollH -= 5;
this->needsRedraw = true;
}
return true;
case KEY_RIGHT:
this->scrollH += 5;
this->needsRedraw = true;
return true;
case KEY_PPAGE:
this->selected -= (this->h - 1);
this->scrollV -= (this->h - 1);
if (this->selected < 0)
this->selected = 0;
if (this->scrollV < 0)
this->scrollV = 0;
this->needsRedraw = true;
return true;
case KEY_NPAGE:
this->selected += (this->h - 1);
int size = List_size(this->items);
if (this->selected >= size)
this->selected = size - 1;
this->scrollV += (this->h - 1);
if (this->scrollV >= MAX(0, size - this->h))
this->scrollV = MAX(0, size - this->h - 1);
this->needsRedraw = true;
return true;
case KEY_HOME:
this->selected = 0;
return true;
case KEY_END:
this->selected = List_size(this->items) - 1;
return true;
}
return false;
}