]>
Commit | Line | Data |
---|---|---|
0d5acc76 | 1 | #include <slang/slang.h> |
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, | |
20 | } ; | |
21 | ||
22 | newtComponent newtLabel(int left, int top, char * text) { | |
23 | newtComponent co; | |
24 | struct label * la; | |
25 | ||
26 | co = malloc(sizeof(*co)); | |
27 | la = malloc(sizeof(struct label)); | |
28 | co->data = la; | |
29 | ||
30 | co->ops = &labelOps; | |
31 | ||
32 | co->height = 1; | |
33 | co->width = strlen(text); | |
34 | co->top = top; | |
35 | co->left = left; | |
36 | co->takesFocus = 0; | |
37 | ||
ce63478f | 38 | la->length = strlen(text); |
0d5acc76 | 39 | la->text = strdup(text); |
40 | ||
41 | return co; | |
42 | } | |
43 | ||
ce63478f | 44 | void newtLabelSetText(newtComponent co, char * text) { |
45 | int newLength; | |
46 | struct label * la = co->data; | |
47 | ||
48 | newLength = strlen(text); | |
49 | if (newLength <= la->length) { | |
50 | memset(la->text, ' ', la->length); | |
51 | memcpy(la->text, text, newLength); | |
52 | } else { | |
53 | free(la->text); | |
54 | la->text = strdup(text); | |
55 | la->length = newLength; | |
56 | co->width = newLength; | |
57 | } | |
58 | ||
59 | labelDraw(co); | |
60 | } | |
61 | ||
0d5acc76 | 62 | static void labelDraw(newtComponent co) { |
63 | struct label * la = co->data; | |
64 | ||
65 | if (co->top == -1) return; | |
66 | ||
67 | SLsmg_set_color(COLORSET_LABEL); | |
68 | ||
69 | newtGotorc(co->top, co->left); | |
70 | SLsmg_write_string(la->text); | |
71 | } | |
72 | ||
73 | static void labelDestroy(newtComponent co) { | |
74 | struct label * la = co->data; | |
75 | ||
76 | free(la->text); | |
77 | free(la); | |
78 | free(co); | |
79 | } |