~ubuntu-branches/ubuntu/lucid/newt/lucid

« back to all changes in this revision

Viewing changes to checkbox.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
 
enum type { CHECK, RADIO };
9
 
 
10
 
struct checkbox {
11
 
    char * text;
12
 
    char * seq;
13
 
    char * result;
14
 
    newtComponent prevButton, lastButton;
15
 
    enum type type;
16
 
    char value;
17
 
    int active, inactive;
18
 
    const void * data;
19
 
    int flags;
20
 
    int hasFocus;
21
 
};
22
 
 
23
 
static void makeActive(newtComponent co);
24
 
 
25
 
static void cbDraw(newtComponent c);
26
 
static void cbDestroy(newtComponent co);
27
 
struct eventResult cbEvent(newtComponent co, struct event ev);
28
 
 
29
 
static struct componentOps cbOps = {
30
 
    cbDraw,
31
 
    cbEvent,
32
 
    cbDestroy,
33
 
    newtDefaultPlaceHandler,
34
 
    newtDefaultMappedHandler,
35
 
} ;
36
 
 
37
 
newtComponent newtRadiobutton(int left, int top, const char * text, int isDefault,
38
 
                              newtComponent prevButton) {
39
 
    newtComponent co;
40
 
    newtComponent curr;
41
 
    struct checkbox * rb;
42
 
    char initialValue;
43
 
 
44
 
    if (isDefault)
45
 
        initialValue = '*';
46
 
    else
47
 
        initialValue = ' ';
48
 
 
49
 
    co = newtCheckbox(left, top, text, initialValue, " *", NULL);
50
 
    rb = co->data;
51
 
    rb->type = RADIO;
52
 
 
53
 
    rb->prevButton = prevButton;
54
 
 
55
 
    for (curr = co; curr; curr = rb->prevButton) {
56
 
        rb = curr->data;
57
 
        rb->lastButton = co;
58
 
    }
59
 
 
60
 
    return co;
61
 
}
62
 
 
63
 
newtComponent newtRadioGetCurrent(newtComponent setMember) {
64
 
    struct checkbox * rb = setMember->data;
65
 
 
66
 
    setMember = rb->lastButton;
67
 
    rb = setMember->data;
68
 
 
69
 
    while (rb && rb->value != '*') {
70
 
        setMember = rb->prevButton;
71
 
        if (!setMember)
72
 
          return NULL;
73
 
        rb = setMember->data;
74
 
    }
75
 
 
76
 
    return setMember;
77
 
}
78
 
 
79
 
char newtCheckboxGetValue(newtComponent co) {
80
 
    struct checkbox * cb = co->data;
81
 
 
82
 
    return cb->value;
83
 
}
84
 
 
85
 
void newtCheckboxSetValue(newtComponent co, char value) {
86
 
    struct checkbox * cb = co->data;
87
 
 
88
 
    *cb->result = value;
89
 
    cbDraw(co);
90
 
}
91
 
 
92
 
newtComponent newtCheckbox(int left, int top, const char * text, char defValue,
93
 
                           const char * seq, char * result) {
94
 
    newtComponent co;
95
 
    struct checkbox * cb;
96
 
 
97
 
    if (!seq) seq = " *";
98
 
 
99
 
    co = malloc(sizeof(*co));
100
 
    cb = malloc(sizeof(struct checkbox));
101
 
    co->data = cb;
102
 
    cb->flags = 0;
103
 
    if (result)
104
 
        cb->result = result;
105
 
    else
106
 
        cb->result = &cb->value;
107
 
 
108
 
    cb->text = strdup(text);
109
 
    cb->seq = strdup(seq);
110
 
    cb->type = CHECK;
111
 
    cb->hasFocus = 0;
112
 
    cb->inactive = COLORSET_CHECKBOX;
113
 
    cb->active = COLORSET_ACTCHECKBOX;
114
 
    defValue ? (*cb->result = defValue) : (*cb->result = cb->seq[0]);
115
 
 
116
 
    co->ops = &cbOps;
117
 
 
118
 
    co->callback = NULL;
119
 
    co->height = 1;
120
 
#ifdef UTF8
121
 
    co->width = strwidth(text) + 4;
122
 
#else
123
 
    co->width = strlen(text) + 4;
124
 
#endif
125
 
    co->top = top;
126
 
    co->left = left;
127
 
    co->takesFocus = 1;
128
 
 
129
 
    return co;
130
 
}
131
 
 
132
 
void newtCheckboxSetFlags(newtComponent co, int flags, enum newtFlagsSense sense) {
133
 
    struct checkbox * cb = co->data;
134
 
    int row, col;
135
 
 
136
 
    cb->flags = newtSetFlags(cb->flags, flags, sense);
137
 
 
138
 
    if (!(cb->flags & NEWT_FLAG_DISABLED))
139
 
        co->takesFocus = 1;
140
 
    else
141
 
        co->takesFocus = 0;
142
 
 
143
 
    newtGetrc(&row, &col);
144
 
    cbDraw(co);
145
 
    newtGotorc(row, col);
146
 
}
147
 
 
148
 
static void cbDraw(newtComponent c) {
149
 
    struct checkbox * cb = c->data;
150
 
 
151
 
    if (c->top == -1 || !c->isMapped) return;
152
 
 
153
 
    if (cb->flags & NEWT_FLAG_DISABLED) {
154
 
        cb->inactive = NEWT_COLORSET_DISENTRY;
155
 
        cb->active = NEWT_COLORSET_DISENTRY;
156
 
    } else {
157
 
        cb->inactive = COLORSET_CHECKBOX;
158
 
        cb->active = COLORSET_ACTCHECKBOX;
159
 
    }
160
 
 
161
 
    newtColor(cb->inactive);
162
 
 
163
 
    newtGotorc(c->top, c->left);
164
 
 
165
 
    switch (cb->type) {
166
 
      case RADIO:
167
 
        SLsmg_write_string("( ) ");
168
 
        break;
169
 
 
170
 
      case CHECK:
171
 
        SLsmg_write_string("[ ] ");
172
 
        break;
173
 
 
174
 
      default:
175
 
        break;
176
 
    }
177
 
 
178
 
    SLsmg_write_string(cb->text);
179
 
 
180
 
    if (cb->hasFocus)
181
 
        newtColor(cb->active);
182
 
 
183
 
    newtGotorc(c->top, c->left + 1);
184
 
    SLsmg_write_char(*cb->result);
185
 
}
186
 
 
187
 
static void cbDestroy(newtComponent co) {
188
 
    struct checkbox * cb = co->data;
189
 
 
190
 
    free(cb->text);
191
 
    free(cb->seq);
192
 
    free(cb);
193
 
    free(co);
194
 
}
195
 
 
196
 
struct eventResult cbEvent(newtComponent co, struct event ev) {
197
 
    struct checkbox * cb = co->data;
198
 
    struct eventResult er;
199
 
    const char * cur;
200
 
 
201
 
    if (ev.when == EV_NORMAL) {
202
 
        switch (ev.event) {
203
 
          case EV_FOCUS:
204
 
            cb->hasFocus = 1;
205
 
            cbDraw(co);
206
 
            er.result = ER_SWALLOWED;
207
 
            break;
208
 
 
209
 
          case EV_UNFOCUS:
210
 
            cb->hasFocus = 0;
211
 
            cbDraw(co);
212
 
            er.result = ER_SWALLOWED;
213
 
            break;
214
 
 
215
 
          case EV_KEYPRESS:
216
 
            if (ev.u.key == ' ') {
217
 
                if (cb->type == RADIO) {
218
 
                    makeActive(co);
219
 
                } else if (cb->type == CHECK) {
220
 
                    cur = strchr(cb->seq, *cb->result);
221
 
                    if (!cur)
222
 
                        *cb->result = *cb->seq;
223
 
                    else {
224
 
                        cur++;
225
 
                        if (! *cur)
226
 
                            *cb->result = *cb->seq;
227
 
                        else
228
 
                            *cb->result = *cur;
229
 
                    }
230
 
                    cbDraw(co);
231
 
                    er.result = ER_SWALLOWED;
232
 
 
233
 
                    if (co->callback)
234
 
                        co->callback(co, co->callbackData);
235
 
                } else {
236
 
                    er.result = ER_IGNORED;
237
 
                }
238
 
            } else if(ev.u.key == NEWT_KEY_ENTER) {
239
 
                er.result = ER_IGNORED;
240
 
            } else {
241
 
                er.result = ER_IGNORED;
242
 
            }
243
 
            break;
244
 
          case EV_MOUSE:
245
 
            if (ev.u.mouse.type == MOUSE_BUTTON_DOWN) {
246
 
                if (cb->type == RADIO) {
247
 
                    makeActive(co);
248
 
                } else if (cb->type == CHECK) {
249
 
                    cur = strchr(cb->seq, *cb->result);
250
 
                    if (!cur)
251
 
                        *cb->result = *cb->seq;
252
 
                    else {
253
 
                        cur++;
254
 
                        if (! *cur)
255
 
                            *cb->result = *cb->seq;
256
 
                        else
257
 
                            *cb->result = *cur;
258
 
                    }
259
 
                    cbDraw(co);
260
 
                    er.result = ER_SWALLOWED;
261
 
 
262
 
                    if (co->callback)
263
 
                        co->callback(co, co->callbackData);
264
 
                }
265
 
            }
266
 
        }
267
 
    } else
268
 
        er.result = ER_IGNORED;
269
 
 
270
 
    return er;
271
 
}
272
 
 
273
 
static void makeActive(newtComponent co) {
274
 
    struct checkbox * cb = co->data;
275
 
    struct checkbox * rb;
276
 
    newtComponent curr;
277
 
 
278
 
    /* find the one that's turned off */
279
 
    curr = cb->lastButton;
280
 
    rb = curr->data;
281
 
    while (curr && rb->value == rb->seq[0]) {
282
 
        curr = rb->prevButton;
283
 
        if (curr) rb = curr->data;
284
 
    }
285
 
    if (curr) {
286
 
        rb->value = rb->seq[0];
287
 
        cbDraw(curr);
288
 
    }
289
 
    cb->value = cb->seq[1];
290
 
    cbDraw(co);
291
 
 
292
 
    if (co->callback)
293
 
        co->callback(co, co->callbackData);
294
 
}