~ubuntu-branches/ubuntu/edgy/newt/edgy

« back to all changes in this revision

Viewing changes to label.c

  • Committer: Bazaar Package Importer
  • Author(s): Junichi Uekawa
  • Date: 2002-03-31 09:38:18 UTC
  • Revision ID: james.westby@ubuntu.com-20020331093818-t3cla7103r07qnyw
Tags: upstream-0.50.17
ImportĀ upstreamĀ versionĀ 0.50.17

Show diffs side-by-side

added added

removed removed

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