]> git.ipfire.org Git - thirdparty/newt.git/blob - label.c
0.52.24
[thirdparty/newt.git] / label.c
1 #include <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;
10 int length;
11 int cs;
12 };
13
14 static void labelDraw(newtComponent co);
15 static void labelDestroy(newtComponent co);
16
17 static struct componentOps labelOps = {
18 labelDraw,
19 newtDefaultEventHandler,
20 labelDestroy,
21 newtDefaultPlaceHandler,
22 newtDefaultMappedHandler,
23 } ;
24
25 newtComponent newtLabel(int left, int top, const char * text) {
26 newtComponent co;
27 struct label * la;
28
29 co = malloc(sizeof(*co));
30 la = malloc(sizeof(struct label));
31 co->data = la;
32 co->destroyCallback = NULL;
33
34 co->ops = &labelOps;
35
36 co->height = 1;
37 co->width = wstrlen(text, -1);
38 co->top = top;
39 co->left = left;
40 co->takesFocus = 0;
41 co->isMapped = 0;
42
43 la->length = strlen(text);
44 la->text = strdup(text);
45 la->cs = COLORSET_LABEL;
46
47 return co;
48 }
49
50 void newtLabelSetText(newtComponent co, const char * text) {
51 int newLength;
52 struct label * la = co->data;
53
54 co->width = wstrlen(text,-1);
55 newLength = strlen(text);
56 if (newLength <= la->length) {
57 memset(la->text, ' ', la->length);
58 memcpy(la->text, text, newLength);
59 } else {
60 free(la->text);
61 la->text = strdup(text);
62 la->length = newLength;
63 }
64
65 labelDraw(co);
66 }
67
68 void newtLabelSetColors(newtComponent co, int colorset) {
69 struct label * la = co->data;
70
71 la->cs = colorset;
72 labelDraw(co);
73 }
74
75 static void labelDraw(newtComponent co) {
76 struct label * la = co->data;
77
78 if (!co->isMapped) return;
79
80 SLsmg_set_color(la->cs);
81
82 newtGotorc(co->top, co->left);
83 SLsmg_write_string(la->text);
84 }
85
86 static void labelDestroy(newtComponent co) {
87 struct label * la = co->data;
88
89 free(la->text);
90 free(la);
91 free(co);
92 }