-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuitoolkit.c
353 lines (344 loc) · 11.4 KB
/
tuitoolkit.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
// file:tuitoolkit.c
#include "tuitoolkit.h"
#define TYPE_HEAD 0
#define TYPE_LABEL 1
#define TYPE_BUTTON 2
#define TYPE_CHECKBOX 3
#define TYPE_LIST 4
int _returnLength(char *string){
unsigned int t = 0;
while(string[t] != '\0')
t++;
return t;
}
//return palette number,; mode: 0 - fill, 1 - widget
int _returnPalette(tuiWindow *tWnd, char mode){
switch(mode){
//fill
case 0:
switch(tWnd -> palette){
//light
case 0: case 1: case 2:
return 1;
break;
//dark
case 3: case 4: case 5:
return 2;
break;
}
break;
//widget
case 1:
switch(tWnd -> palette){
//blue
case 0: case 3:
return 3;
break;
//green
case 1: case 4:
return 4;
break;
//red
case 2: case 5:
return 5;
break;
}
break;
//invalid - fallback to default colors
default:
return 0;
break;
}
}
//debug function
void _drawERROR(tuiWindow *wnd, char *name, int errcode, unsigned int posX, unsigned int posY){
switch(errcode){
case 1:
mvwprintw(wnd -> wndPtr, posY, posX, "[X]ERROR in %s; INVALID ARGUMENT", name);
break;
case 3:
mvwprintw(wnd -> wndPtr, posY, posX, "[X]ERROR in %s; OUT OF BOUNDS", name);
break;
case 4:
mvwprintw(wnd -> wndPtr, posY, posX, "[X]ERROR in %s; CANNOT ALLOCATE MEMORY", name);
break;
default:
mvwprintw(wnd -> wndPtr, posY, posX, "[?]META ERROR in %s; WRONG ERROR CODE", name);
break;
}
wrefresh(wnd -> wndPtr);
}
//create new node of linked list
void _appendToEnd(struct listNode** headerPtr, void *newData, int newDataType){
//allocate new node
struct listNode* newNode = malloc(sizeof(struct listNode));
newNode -> data = newData;
newNode -> dataType = newDataType;
newNode -> next = NULL;
//pointer to current last element
struct listNode *lastPtr = *headerPtr;
//if header is NULL, set it to point to new node
if(*headerPtr == NULL){
*headerPtr = newNode;
return;
}
//else find the last node that points to NULL and set it to point to new node instead
while(lastPtr -> next != NULL)
lastPtr = lastPtr -> next;
lastPtr -> next = newNode;
}
/* widgets */
void tuiInitLabel(tuiWindow *tWnd, tuiWidgetLabel *tLabel, unsigned int posX, unsigned int posY, char *label){
*tLabel = (tuiWidgetLabel){.posX = posX, .posY = posY, .text = label, .tWndPtr = tWnd};
_appendToEnd(&tWnd -> wdgHead, tLabel, TYPE_LABEL);
}
void tuiInitButton(tuiWindow *tWnd, tuiWidgetButton *tButton, unsigned int posX, unsigned int posY, char *label){
*tButton = (tuiWidgetButton){.posX = posX, .posY = posY, .text = label, .tWndPtr = tWnd, .highlighted = 0};
_appendToEnd(&tWnd -> wdgHead, tButton, TYPE_BUTTON);
}
void tuiInitCheckbox(tuiWindow *tWnd, tuiWidgetCheckbox *tCheckbox, unsigned int posX, unsigned int posY, char *label){
*tCheckbox = (tuiWidgetCheckbox){.posX = posX, .posY = posY, .text = label, .tWndPtr = tWnd, .highlighted = 0, .ticked = 0};
_appendToEnd(&tWnd -> wdgHead, tCheckbox, TYPE_CHECKBOX);
}
void tuiInitList(tuiWindow *tWnd, tuiWidgetList *tList, unsigned int posX, unsigned int posY, char *(*values)[], unsigned int listLength, unsigned int width, unsigned int height){
*tList = (tuiWidgetList){.posX = posX, .posY = posY, .values = values,.listLength = listLength, .tWndPtr = tWnd, .selection = 0, .scroll = 0, .highlighted = 0, .width = width, .height = height, .scroll = 0};
_appendToEnd(&tWnd -> wdgHead, tList, TYPE_LIST);
}
int _drawLabel(tuiWidgetLabel *tWdg){
//check bounds
if(( (tWdg -> posX) < 0 ) || ( (tWdg -> posY) < 0)){
_drawERROR((tWdg -> tWndPtr), "label", TUI_ERR_OUT_OF_BOUNDS, tWdg -> posX, tWdg -> posY);
return 3;
}
//print
wattron((tWdg -> tWndPtr) -> wndPtr, COLOR_PAIR(_returnPalette(tWdg -> tWndPtr, 0)));
mvwprintw((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX, tWdg -> text);
return 0;
}
int _drawButton(tuiWidgetButton *tWdg){
//check bounds
if(( (tWdg -> posX) < 0 ) || ( (tWdg -> posY) < 0)){
_drawERROR((tWdg -> tWndPtr), "button", TUI_ERR_OUT_OF_BOUNDS, tWdg -> posX, tWdg -> posY);
return 3;
}
if( !( (tWdg -> highlighted == 0) || (tWdg -> highlighted == 1) ) ){
_drawERROR((tWdg -> tWndPtr), "button", TUI_ERR_OUT_OF_BOUNDS, tWdg -> posX, tWdg -> posY);
return 3;
}
//set highlight
switch(tWdg -> highlighted){
case 0:
wattron((tWdg -> tWndPtr) -> wndPtr, COLOR_PAIR(_returnPalette(tWdg -> tWndPtr, 0)));
break;
case 1:
wattron((tWdg -> tWndPtr) -> wndPtr, COLOR_PAIR(_returnPalette(tWdg -> tWndPtr, 1)));
break;
}
//print
char *label = tWdg -> text;
mvwaddch((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX, '<');
mvwprintw((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX + 1, label);
mvwaddch((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX + _returnLength(label) + 1, '>');
return 0;
}
int _drawCheckbox(tuiWidgetCheckbox *tWdg){
//check bounds
if(( (tWdg -> posX) < 0 ) || ( (tWdg -> posY) < 0)){
_drawERROR((tWdg -> tWndPtr), "checkbox", TUI_ERR_OUT_OF_BOUNDS, tWdg -> posX, tWdg -> posY);
return 3;
}
//set highlight
switch(tWdg -> highlighted){
case 0:
wattron((tWdg -> tWndPtr) -> wndPtr, COLOR_PAIR(_returnPalette(tWdg -> tWndPtr, 0)));
break;
case 1:
wattron((tWdg -> tWndPtr) -> wndPtr, COLOR_PAIR(_returnPalette(tWdg -> tWndPtr, 1)));
break;
}
char *label = tWdg -> text;
//box
switch(tWdg -> ticked){
case 0:
mvwprintw((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX, "[ ]");
break;
case 1:
mvwprintw((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX, "[#]");
break;
}
//label
wattron((tWdg -> tWndPtr) -> wndPtr, COLOR_PAIR(_returnPalette(tWdg -> tWndPtr, 0)));
mvwprintw((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX + 4, label);
return 0;
}
int _drawList(tuiWidgetList *tWdg){
//check bounds
if(( (tWdg -> posX) < 0 ) || ( (tWdg -> posY) < 0)){
_drawERROR((tWdg -> tWndPtr), "list", TUI_ERR_OUT_OF_BOUNDS, tWdg -> posX, tWdg -> posY);
return 3;
}
if(tWdg -> selection <= 0){
_drawERROR((tWdg -> tWndPtr), "list", TUI_ERR_OUT_OF_BOUNDS, tWdg -> posX, tWdg -> posY);
return 3;
}
if(tWdg -> selection > tWdg -> listLength){
_drawERROR((tWdg -> tWndPtr), "list", TUI_ERR_OUT_OF_BOUNDS, tWdg -> posX, tWdg -> posY);
return 3;
}
//upper line
mvwaddch((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX, ACS_ULCORNER);
mvwhline((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX + 1, ACS_HLINE, tWdg -> width - 2);
mvwaddch((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY, tWdg -> posX + tWdg -> width - 1, ACS_URCORNER);
//entries
char text[tWdg -> width];
int offset = (tWdg -> selection) - ((tWdg -> height)/2);
//draw values
for(int i = 0; i < (tWdg -> height - 2); i++){
//frame
mvwaddch((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY + i + 1, tWdg -> posX, ACS_VLINE);
//clear field
mvwhline((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY + i + 1, tWdg -> posX + 1, ' ', tWdg -> width - 2);
//options / empty field
if( !( ((offset + i) < 0) || ( (offset + i) > (tWdg -> listLength - 1 ) )) ){//empty field
//set color if index in the middle
if(i == ((tWdg -> height)/2 - 1))
wattron( (tWdg -> tWndPtr) -> wndPtr, COLOR_PAIR(_returnPalette( tWdg -> tWndPtr, 1 ) ) );
//set text and truncate if necessary
if(_returnLength( (*tWdg -> values)[i + offset] ) < ( (tWdg -> width) - 2 ) ){//if the value is shorter than width
int j = 0;
while((*tWdg ->values)[i + offset][j] != '\0'){
text[j] = (*tWdg -> values)[i + offset][j];
j++;
}
text[j] = '\0';
} else{//else truncate
unsigned int ll = (tWdg -> width) - 2;
char trunc[ll+1];
int j = 0;
for(; j < ll-3; j++)
text[j] = (*tWdg -> values)[i + offset][j];
for(; j<ll+3; j++)
text[j] = '.';
text[ll] = '\0';
}
//print the value
mvwprintw((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY + i + 1, tWdg -> posX + 1, text);
}
wattroff((tWdg -> tWndPtr) -> wndPtr, COLOR_PAIR(_returnPalette(tWdg -> tWndPtr, 1)));
//frame
mvwaddch((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY + i + 1, tWdg -> posX + tWdg -> width - 1, ACS_VLINE);
}
//lower line
mvwaddch((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY + tWdg -> height - 1, tWdg -> posX, ACS_LLCORNER);
mvwhline((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY + tWdg -> height - 1, tWdg -> posX + 1, ACS_HLINE, tWdg -> width - 2);
mvwaddch((tWdg -> tWndPtr) -> wndPtr, tWdg -> posY + tWdg -> height - 1, tWdg -> posX + tWdg -> width - 1, ACS_LRCORNER);
return 0;
}
/* window */
void tuiInitWindow(tuiWindow *tWnd, unsigned int posX, unsigned int posY, unsigned int wdt, unsigned int hgt, char *title, char decor, char palt){
//create ncurses window
WINDOW *ncrsWnd = newwin(hgt, wdt, posY, posX);
noecho();
wclear(ncrsWnd);
//set keypad support
keypad(stdscr, TRUE);
*tWnd = (tuiWindow){.posX = posX, .posY = posY, .width = wdt, .height = hgt, .title = title, .decoration = decor, .palette = palt, .wndPtr = ncrsWnd};
tWnd -> wdgHead = NULL;
}
#define tuiDrawWidget(wdgt) _Generic (wdgt, tuiWidgetLabel*: _drawLabel, tuiWidgetButton*: _drawButton,tuiWidgetList*: _drawList, tuiWidgetCheckbox*: _drawCheckbox, tuiWidgetList: _drawList)(wdgt)
void tuiDrawWindow(tuiWindow *tWnd){
/* draw window body */
//set color palette
char fillpalette = _returnPalette(tWnd, 0);
char wdgtpalette = _returnPalette(tWnd, 1);
//fill background
wbkgd((*tWnd).wndPtr, COLOR_PAIR(fillpalette));
//draw frame
switch(tWnd -> decoration){
case 1: case 4: case 5: case 7://decoration modes
box((*tWnd).wndPtr, 0, 0);
break;
}
wattron((*tWnd).wndPtr, COLOR_PAIR(wdgtpalette));
//draw title
switch(tWnd -> decoration){
case 2: case 4: case 6: case 7://decoration modes
if(_returnLength(tWnd ->title) < tWnd -> width - 4){//short enough
mvwprintw((*tWnd).wndPtr, 0, 2, tWnd-> title);
} else{//longer than window width
//truncate title
unsigned int len = tWnd -> width - 7;
char trunc[len+1];
for(int i = 0; i < len; i++)
trunc[i] = tWnd -> title[i];
trunc[len] = '\0';
mvwprintw((*tWnd).wndPtr, 0, 2, trunc);
//ellipsis
mvwprintw((*tWnd).wndPtr, 0, tWnd -> width - 5, "...");
}
break;
}
wattroff((*tWnd).wndPtr, COLOR_PAIR(wdgtpalette));
/* draw widgets */
struct listNode* wdgPtr;
wdgPtr = tWnd -> wdgHead;
while(wdgPtr != NULL){
switch(wdgPtr -> dataType){
case TYPE_LABEL:
tuiDrawWidget( (tuiWidgetLabel*)wdgPtr -> data );
break;
case TYPE_BUTTON:
tuiDrawWidget( (tuiWidgetButton*)wdgPtr -> data );
break;
case TYPE_CHECKBOX:
tuiDrawWidget( (tuiWidgetCheckbox*)wdgPtr -> data );
break;
case TYPE_LIST:
tuiDrawWidget( (tuiWidgetList*)wdgPtr -> data );
break;
default:
break;
}
wdgPtr = wdgPtr -> next;
}
wrefresh((*tWnd).wndPtr);
}
void tuiEndWindow(tuiWindow *tWnd){
if(tWnd -> wdgHead != NULL){
struct listNode* Ptr1;
struct listNode* Ptr2;
struct listNode* Ptr3;
Ptr1 = tWnd -> wdgHead;
Ptr2 = Ptr1 -> next;
while(Ptr2 != NULL){
Ptr3 = Ptr2;
//deallocate node
free(Ptr1);
Ptr1 = Ptr2;
Ptr2 = Ptr3 -> next;
}
}
delwin(tWnd -> wndPtr);
refresh();
}
/* screen */
int tuiInitScreen(){
initscr();
//probe for terminal color support
if(has_colors() == 0){
endwin();
return 1;//no color support
}
start_color();
//set color palette
init_pair(1, COLOR_BLACK, COLOR_WHITE);//light variant
init_pair(2, COLOR_WHITE, COLOR_BLACK);//dark variant
init_pair(3, COLOR_BLACK, COLOR_CYAN);//elements blue
init_pair(4, COLOR_BLACK, COLOR_GREEN);//elements green
init_pair(5, COLOR_BLACK, COLOR_RED);//elements red
refresh();
return 0;
}
void tuiEndScreen(){
endwin();
}