~ubuntu-branches/ubuntu/vivid/newt/vivid

« back to all changes in this revision

Viewing changes to label.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-11-08 14:50:00 UTC
  • mfrom: (2.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20121108145000-0yfrol7obct0jufq
Tags: 0.52.14-11ubuntu1
* Merge with Debian; remaining changes:
  - Port python module to python3.
  - Fix installation for multiple python3 versions.
  - Move libnewt to /lib and whiptail to /bin so they can be used by
    friendly-recovery on systems that have a separate /usr.
  - debian/libnewt0.52.install, debian/libnewt0.52.postinst,
    debian/palette => debian/palette.original:
    - move palette from /usr to /etc such that they can be edited by an
      admin.
* Configure --with-colorsfile=/etc/newt/palette.

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
    int cs;
 
12
};
 
13
 
 
14
static void labelDraw(newtComponent co);
 
15
static void labelDestroy(newtComponent co);
 
16
 
 
17
static struct componentOps labelOps = {
 
18
    labelDraw,
 
19
    newtDefaultEventHandler,
 
20
    labelDestroy,
 
21
    newtDefaultPlaceHandler,
 
22
    newtDefaultMappedHandler,
 
23
} ;
 
24
 
 
25
newtComponent newtLabel(int left, int top, const char * text) {
 
26
    newtComponent co;
 
27
    struct label * la;
 
28
 
 
29
    co = malloc(sizeof(*co));
 
30
    la = malloc(sizeof(struct label));
 
31
    co->data = la;
 
32
    co->destroyCallback = NULL;
 
33
 
 
34
    co->ops = &labelOps;
 
35
 
 
36
    co->height = 1;
 
37
    co->width = wstrlen(text, -1);
 
38
    co->top = top;
 
39
    co->left = left;
 
40
    co->takesFocus = 0;
 
41
    co->isMapped = 0;
 
42
 
 
43
    la->length = strlen(text);
 
44
    la->text = strdup(text);
 
45
    la->cs = COLORSET_LABEL;
 
46
 
 
47
    return co;
 
48
}
 
49
 
 
50
void newtLabelSetText(newtComponent co, const char * text) {
 
51
    int newLength;
 
52
    struct label * la = co->data;
 
53
 
 
54
    co->width = wstrlen(text,-1);
 
55
    newLength = strlen(text);
 
56
    if (newLength <= la->length) {
 
57
        memset(la->text, ' ', la->length);
 
58
        memcpy(la->text, text, newLength);
 
59
    } else {
 
60
        free(la->text);
 
61
        la->text = strdup(text);
 
62
        la->length = newLength;
 
63
    }
 
64
 
 
65
    labelDraw(co);
 
66
}
 
67
 
 
68
void newtLabelSetColors(newtComponent co, int colorset) {
 
69
    struct label * la = co->data;
 
70
 
 
71
    la->cs = colorset;
 
72
    labelDraw(co);
 
73
}
 
74
 
 
75
static void labelDraw(newtComponent co) {
 
76
    struct label * la = co->data;
 
77
 
 
78
    if (!co->isMapped) return;
 
79
 
 
80
    SLsmg_set_color(la->cs);
 
81
 
 
82
    newtGotorc(co->top, co->left);
 
83
    /* BIDI: need to check if nstring is really needed.
 
84
     * Where it is used? */
 
85
    write_nstring_int(la->text, co->width, NULL);
 
86
}
 
87
 
 
88
static void labelDestroy(newtComponent co) {
 
89
    struct label * la = co->data;
 
90
 
 
91
    free(la->text);
 
92
    free(la);
 
93
    free(co);
 
94
}