File: label.c

package info (click to toggle)
newt 0.52.17-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,676 kB
  • ctags: 2,049
  • sloc: ansic: 9,249; python: 711; makefile: 259; sh: 9
file content (94 lines) | stat: -rw-r--r-- 1,898 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <slang.h>
#include <stdlib.h>
#include <string.h>

#include "newt.h"
#include "newt_pr.h"

struct label {
    char * text;
    int length;
    int cs;
};

static void labelDraw(newtComponent co);
static void labelDestroy(newtComponent co);

static struct componentOps labelOps = {
    labelDraw,
    newtDefaultEventHandler,
    labelDestroy,
    newtDefaultPlaceHandler,
    newtDefaultMappedHandler,
} ;

newtComponent newtLabel(int left, int top, const char * text) {
    newtComponent co;
    struct label * la;

    co = malloc(sizeof(*co));
    la = malloc(sizeof(struct label));
    co->data = la;
    co->destroyCallback = NULL;

    co->ops = &labelOps;

    co->height = 1;
    co->width = wstrlen(text, -1);
    co->top = top;
    co->left = left;
    co->takesFocus = 0;
    co->isMapped = 0;

    la->length = strlen(text);
    la->text = strdup(text);
    la->cs = COLORSET_LABEL;

    return co;
}

void newtLabelSetText(newtComponent co, const char * text) {
    int newLength;
    struct label * la = co->data;

    co->width = wstrlen(text,-1);
    newLength = strlen(text);
    if (newLength <= la->length) {
	memset(la->text, ' ', la->length);
	memcpy(la->text, text, newLength);
    } else {
	free(la->text);
	la->text = strdup(text);
	la->length = newLength;
    }

    labelDraw(co);
}

void newtLabelSetColors(newtComponent co, int colorset) {
    struct label * la = co->data;

    la->cs = colorset;
    labelDraw(co);
}

static void labelDraw(newtComponent co) {
    struct label * la = co->data;

    if (!co->isMapped) return;

    SLsmg_set_color(la->cs);

    newtGotorc(co->top, co->left);
    /* BIDI: need to check if nstring is really needed.
     * Where it is used? */
    write_nstring_int(la->text, co->width, NULL);
}

static void labelDestroy(newtComponent co) {
    struct label * la = co->data;

    free(la->text);
    free(la);
    free(co);
}