-
Notifications
You must be signed in to change notification settings - Fork 10
/
label.c
92 lines (71 loc) · 1.76 KB
/
label.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
#include <slang.h>
#include <stdlib.h>
#include <string.h>
#include "newt.h"
#include "newt_pr.h"
struct label {
char * text;
int length;
int cs;
};
static void labelDraw(newtComponent co);
static void labelDestroy(newtComponent co);
static struct componentOps labelOps = {
labelDraw,
newtDefaultEventHandler,
labelDestroy,
newtDefaultPlaceHandler,
newtDefaultMappedHandler,
} ;
newtComponent newtLabel(int left, int top, const char * text) {
newtComponent co;
struct label * la;
co = malloc(sizeof(*co));
la = malloc(sizeof(struct label));
co->data = la;
co->destroyCallback = NULL;
co->ops = &labelOps;
co->height = 1;
co->width = wstrlen(text, -1);
co->top = top;
co->left = left;
co->takesFocus = 0;
co->isMapped = 0;
la->length = strlen(text);
la->text = strdup(text);
la->cs = COLORSET_LABEL;
return co;
}
void newtLabelSetText(newtComponent co, const char * text) {
int newLength;
struct label * la = co->data;
co->width = wstrlen(text,-1);
newLength = strlen(text);
if (newLength <= la->length) {
memset(la->text, ' ', la->length);
memcpy(la->text, text, newLength);
} else {
free(la->text);
la->text = strdup(text);
la->length = newLength;
}
labelDraw(co);
}
void newtLabelSetColors(newtComponent co, int colorset) {
struct label * la = co->data;
la->cs = colorset;
labelDraw(co);
}
static void labelDraw(newtComponent co) {
struct label * la = co->data;
if (!co->isMapped) return;
SLsmg_set_color(la->cs);
newtGotorc(co->top, co->left);
SLsmg_write_string(la->text);
}
static void labelDestroy(newtComponent co) {
struct label * la = co->data;
free(la->text);
free(la);
free(co);
}