]>
Commit | Line | Data |
---|---|---|
139f06bc | 1 | #include <slang.h> |
0d5acc76 | 2 | #include <stdlib.h> |
3 | #include <string.h> | |
4 | ||
5 | #include "newt.h" | |
6 | #include "newt_pr.h" | |
7 | ||
8 | struct label { | |
9 | char * text; | |
ce63478f | 10 | int length; |
0d5acc76 | 11 | }; |
12 | ||
13 | static void labelDraw(newtComponent co); | |
14 | static void labelDestroy(newtComponent co); | |
15 | ||
16 | static struct componentOps labelOps = { | |
17 | labelDraw, | |
18 | newtDefaultEventHandler, | |
19 | labelDestroy, | |
8f52cd47 | 20 | newtDefaultPlaceHandler, |
21 | newtDefaultMappedHandler, | |
0d5acc76 | 22 | } ; |
23 | ||
d4109c37 | 24 | newtComponent newtLabel(int left, int top, const char * text) { |
0d5acc76 | 25 | newtComponent co; |
26 | struct label * la; | |
27 | ||
28 | co = malloc(sizeof(*co)); | |
29 | la = malloc(sizeof(struct label)); | |
30 | co->data = la; | |
31 | ||
32 | co->ops = &labelOps; | |
33 | ||
34 | co->height = 1; | |
63231242 | 35 | co->width = strlen(text); |
0d5acc76 | 36 | co->top = top; |
37 | co->left = left; | |
38 | co->takesFocus = 0; | |
39 | ||
ce63478f | 40 | la->length = strlen(text); |
0d5acc76 | 41 | la->text = strdup(text); |
42 | ||
43 | return co; | |
44 | } | |
45 | ||
d4109c37 | 46 | void newtLabelSetText(newtComponent co, const char * text) { |
ce63478f | 47 | int newLength; |
48 | struct label * la = co->data; | |
49 | ||
63231242 | 50 | newLength = strlen(text); |
ce63478f | 51 | if (newLength <= la->length) { |
52 | memset(la->text, ' ', la->length); | |
53 | memcpy(la->text, text, newLength); | |
54 | } else { | |
55 | free(la->text); | |
56 | la->text = strdup(text); | |
57 | la->length = newLength; | |
58 | co->width = newLength; | |
59 | } | |
60 | ||
61 | labelDraw(co); | |
62 | } | |
63 | ||
0d5acc76 | 64 | static void labelDraw(newtComponent co) { |
65 | struct label * la = co->data; | |
66 | ||
f15c08e5 | 67 | if (co->isMapped == -1) return; |
0d5acc76 | 68 | |
69 | SLsmg_set_color(COLORSET_LABEL); | |
70 | ||
71 | newtGotorc(co->top, co->left); | |
72 | SLsmg_write_string(la->text); | |
73 | } | |
74 | ||
75 | static void labelDestroy(newtComponent co) { | |
76 | struct label * la = co->data; | |
77 | ||
78 | free(la->text); | |
79 | free(la); | |
80 | free(co); | |
81 | } |