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