]> git.ipfire.org Git - thirdparty/newt.git/blame - label.c
Version 0.10
[thirdparty/newt.git] / label.c
CommitLineData
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
8struct label {
9 char * text;
ce63478f 10 int length;
0d5acc76 11};
12
13static void labelDraw(newtComponent co);
14static void labelDestroy(newtComponent co);
15
16static struct componentOps labelOps = {
17 labelDraw,
18 newtDefaultEventHandler,
19 labelDestroy,
20} ;
21
22newtComponent 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 44void 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 62static 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
73static void labelDestroy(newtComponent co) {
74 struct label * la = co->data;
75
76 free(la->text);
77 free(la);
78 free(co);
79}