~ubuntu-branches/ubuntu/dapper/newt/dapper

« back to all changes in this revision

Viewing changes to label.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2005-03-22 12:44:37 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050322124437-nuhl0pqjcijjno9z
Tags: 0.51.6-20ubuntu3
Add Xhosa translation (thanks, Adi Attar).

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
 
    newtColor(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
 
}