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

« back to all changes in this revision

Viewing changes to dialogboxes.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
 
/* simple dialog boxes, used by both whiptail and tcl dialog bindings */
2
 
 
3
 
#include <fcntl.h>
4
 
#include <stdio.h>
5
 
#include <string.h>
6
 
#include <stdlib.h>
7
 
#include <unistd.h>
8
 
 
9
 
#include "dialogboxes.h"
10
 
#include "newt.h"
11
 
#include "popt.h"
12
 
 
13
 
/* globals -- ick */
14
 
static int buttonHeight = 1;
15
 
static newtComponent (*makeButton)(int left, int right, const char * text) = 
16
 
                newtCompactButton;
17
 
 
18
 
static void addButtons(int height, int width, newtComponent form, 
19
 
                       newtComponent * okay, newtComponent * cancel, 
20
 
                       int flags) {
21
 
    if (flags & FLAG_NOCANCEL) {
22
 
        *okay = makeButton((width - 8) / 2, height - buttonHeight - 1, "Ok");
23
 
        *cancel = NULL;
24
 
        newtFormAddComponent(form, *okay);
25
 
    } else {
26
 
        *okay = makeButton((width - 18) / 3, height - buttonHeight - 1, "Ok");
27
 
        *cancel = makeButton(((width - 18) / 3) * 2 + 9, 
28
 
                                height - buttonHeight - 1, "Cancel");
29
 
        newtFormAddComponents(form, *okay, *cancel, NULL);
30
 
    }
31
 
}
32
 
 
33
 
static newtComponent textbox(int maxHeight, int width, const char * text,
34
 
                        int flags, int * height) {
35
 
    newtComponent tb;
36
 
    int sFlag = (flags & FLAG_SCROLL_TEXT) ? NEWT_FLAG_SCROLL : 0;
37
 
    int i;
38
 
    char * buf, * dst;
39
 
    const char * src;
40
 
 
41
 
    dst = buf = alloca(strlen(text) + 1);
42
 
    src = text; 
43
 
    while (*src) {
44
 
        if (*src == '\\' && *(src + 1) == 'n') {
45
 
            src += 2;
46
 
            *dst++ = '\n';
47
 
        } else
48
 
            *dst++ = *src++;
49
 
    }
50
 
    *dst++ = '\0';
51
 
 
52
 
    tb = newtTextbox(1, 0, width, maxHeight, NEWT_FLAG_WRAP | sFlag);
53
 
    newtTextboxSetText(tb, buf);
54
 
 
55
 
    i = newtTextboxGetNumLines(tb);
56
 
    if (i < maxHeight) {
57
 
        newtTextboxSetHeight(tb, i);
58
 
        maxHeight = i;
59
 
    }
60
 
 
61
 
    *height = maxHeight;
62
 
 
63
 
    return tb;
64
 
}
65
 
 
66
 
int gauge(const char * text, int height, int width, poptContext optCon, int fd, 
67
 
                int flags) {
68
 
    newtComponent form, scale, tb;
69
 
    int top;
70
 
    const char * arg;
71
 
    char * end;
72
 
    int val;
73
 
    FILE * f = fdopen(fd, "r");
74
 
    char buf[3000];
75
 
    char buf3[50];
76
 
    int i;
77
 
 
78
 
    setlinebuf(f);
79
 
 
80
 
    if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
81
 
    val = strtoul(arg, &end, 10);
82
 
    if (*end) return DLG_ERROR;
83
 
 
84
 
    tb = textbox(height - 3, width - 2, text, flags, &top);
85
 
 
86
 
    form = newtForm(NULL, NULL, 0);
87
 
 
88
 
    scale = newtScale(2, height - 2, width - 4, 100);
89
 
    newtScaleSet(scale, val);
90
 
 
91
 
    newtFormAddComponents(form, tb, scale, NULL);
92
 
 
93
 
    newtDrawForm(form);
94
 
    newtRefresh();
95
 
 
96
 
    while (fgets(buf, sizeof(buf) - 1, f)) {
97
 
        buf[strlen(buf) - 1] = '\0';
98
 
 
99
 
        if (!strcmp(buf, "XXX")) {
100
 
            fgets(buf3, sizeof(buf3) - 1, f);
101
 
            buf3[strlen(buf3) - 1] = '\0';
102
 
            arg = buf3;
103
 
 
104
 
            i = 0;
105
 
            while (fgets(buf + i, sizeof(buf) - 1 - i, f)) {
106
 
                buf[strlen(buf) - 1] = '\0';
107
 
                if (!strcmp(buf + i, "XXX")) {
108
 
                    *(buf + i) = '\0';
109
 
                    break;
110
 
                }
111
 
                i = strlen(buf);
112
 
            }
113
 
 
114
 
            newtTextboxSetText(tb, buf);
115
 
        } else {
116
 
            arg = buf;
117
 
        }
118
 
 
119
 
        val = strtoul(buf, &end, 10);
120
 
        if (!*end) {
121
 
            newtScaleSet(scale, val);
122
 
            newtDrawForm(form);
123
 
            newtRefresh();
124
 
        }
125
 
    }
126
 
 
127
 
    return DLG_OKAY;
128
 
}
129
 
 
130
 
int inputBox(const char * text, int height, int width, poptContext optCon, 
131
 
                int flags, char ** result) {
132
 
    newtComponent form, entry, okay, cancel, answer, tb;
133
 
    int pFlag = (flags & FLAG_PASSWORD) ? NEWT_FLAG_HIDDEN : 0;
134
 
    char * val;
135
 
    int rc = DLG_OKAY;
136
 
    int top;
137
 
 
138
 
    val = poptGetArg(optCon);
139
 
    tb = textbox(height - 3 - buttonHeight, width - 2,
140
 
                        text, flags, &top);
141
 
 
142
 
    form = newtForm(NULL, NULL, 0);
143
 
    entry = newtEntry(1, top + 1, val, width - 2, &val, 
144
 
                        NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT | pFlag);
145
 
 
146
 
    newtFormAddComponents(form, tb, entry, NULL);
147
 
 
148
 
    addButtons(height, width, form, &okay, &cancel, flags);
149
 
 
150
 
    answer = newtRunForm(form);
151
 
    if (answer == cancel)
152
 
        rc = DLG_CANCEL;
153
 
 
154
 
    *result = val;
155
 
 
156
 
    return rc;
157
 
}
158
 
 
159
 
int listBox(const char * text, int height, int width, poptContext optCon,
160
 
                int flags, char ** result) {
161
 
    newtComponent form, okay, tb, answer, listBox;
162
 
    newtComponent cancel = NULL;
163
 
    const char * arg;
164
 
    char * end;
165
 
    int listHeight;
166
 
    int numItems = 0;
167
 
    int allocedItems = 5;
168
 
    int i, top, left;
169
 
    int rc = DLG_OKAY;
170
 
    char *buf, format[20];
171
 
    int maxTagWidth = 0;
172
 
    int maxTextWidth = 0;
173
 
    int scrollFlag;
174
 
    struct {
175
 
        const char * text;
176
 
        const char * tag;
177
 
    } * itemInfo = malloc(allocedItems * sizeof(*itemInfo));
178
 
 
179
 
    if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
180
 
    listHeight = strtoul(arg, &end, 10);
181
 
    if (*end) return DLG_ERROR;
182
 
 
183
 
    while ((arg = poptGetArg(optCon))) {
184
 
        if (allocedItems == numItems) {
185
 
            allocedItems += 5;
186
 
            itemInfo = realloc(itemInfo, sizeof(*itemInfo) * allocedItems);
187
 
        }
188
 
 
189
 
        itemInfo[numItems].tag = arg;
190
 
        if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
191
 
 
192
 
        if (!(flags & FLAG_NOITEM)) {
193
 
            itemInfo[numItems].text = arg;
194
 
        } else
195
 
            itemInfo[numItems].text = "";
196
 
 
197
 
        if (strlen(itemInfo[numItems].text) > (unsigned int)maxTextWidth)
198
 
            maxTextWidth = strlen(itemInfo[numItems].text);
199
 
        if (strlen(itemInfo[numItems].tag) > (unsigned int)maxTagWidth)
200
 
            maxTagWidth = strlen(itemInfo[numItems].tag);
201
 
 
202
 
        numItems++;
203
 
    }
204
 
 
205
 
    form = newtForm(NULL, NULL, 0);
206
 
 
207
 
    tb = textbox(height - 4 - buttonHeight - listHeight, width - 2,
208
 
                        text, flags, &top);
209
 
 
210
 
    if (listHeight >= numItems) {
211
 
        scrollFlag = 0;
212
 
        i = 0;
213
 
    } else {
214
 
        scrollFlag = NEWT_FLAG_SCROLL;
215
 
        i = 2;
216
 
    }
217
 
 
218
 
    left = (width - 10 - maxTagWidth - maxTextWidth - i) / 2;
219
 
    if (left < 0) left = 0;
220
 
    listBox = newtListbox(3 + left, top + 1, listHeight,
221
 
                            NEWT_FLAG_RETURNEXIT | scrollFlag);
222
 
 
223
 
    sprintf(format, "%%-%ds  %%s", maxTagWidth);
224
 
    /* Junichi Uekawa - fix buffer overrun when width > 80 */
225
 
    if (!(buf = malloc (width - 5)))
226
 
      {
227
 
        /* out of memory */
228
 
        return DLG_ERROR;
229
 
      }
230
 
    
231
 
    for (i = 0; i < numItems; i++) {
232
 
        snprintf(buf, width - 6, format, itemInfo[i].tag, itemInfo[i].text);
233
 
        newtListboxAddEntry(listBox, buf, (void *) i);
234
 
    }
235
 
    free(buf);
236
 
 
237
 
    newtFormAddComponents(form, tb, listBox, NULL);
238
 
 
239
 
    addButtons(height, width, form, &okay, &cancel, flags);
240
 
 
241
 
    answer = newtRunForm(form);
242
 
    if (answer == cancel)
243
 
        rc = DLG_CANCEL;
244
 
 
245
 
    i = (int) newtListboxGetCurrent(listBox);
246
 
    *result = itemInfo[i].tag;
247
 
 
248
 
    return rc;
249
 
}
250
 
 
251
 
int checkList(const char * text, int height, int width, poptContext optCon,
252
 
                int useRadio, int flags, char *** selections) {
253
 
    newtComponent form, okay, tb, subform, answer;
254
 
    newtComponent sb = NULL, cancel = NULL;
255
 
    const char * arg;
256
 
    char * end;
257
 
    int listHeight;
258
 
    int numBoxes = 0;
259
 
    int allocedBoxes = 5;
260
 
    int i;
261
 
    int numSelected;
262
 
    int rc = DLG_OKAY;
263
 
    char buf[80], format[20];
264
 
    int maxWidth = 0;
265
 
    int top;
266
 
    struct {
267
 
        const char * text;
268
 
        const char * tag;
269
 
        newtComponent comp;
270
 
    } * cbInfo = malloc(allocedBoxes * sizeof(*cbInfo));
271
 
    char * cbStates = malloc(allocedBoxes * sizeof(cbStates));
272
 
 
273
 
    if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
274
 
    listHeight = strtoul(arg, &end, 10);
275
 
    if (*end) return DLG_ERROR;
276
 
 
277
 
    while ((arg = poptGetArg(optCon))) {
278
 
        if (allocedBoxes == numBoxes) {
279
 
            allocedBoxes += 5;
280
 
            cbInfo = realloc(cbInfo, sizeof(*cbInfo) * allocedBoxes);
281
 
            cbStates = realloc(cbStates, sizeof(*cbStates) * allocedBoxes);
282
 
        }
283
 
 
284
 
        cbInfo[numBoxes].tag = arg;
285
 
        if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
286
 
 
287
 
        if (!(flags & FLAG_NOITEM)) {
288
 
            cbInfo[numBoxes].text = arg;
289
 
            if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
290
 
        } else
291
 
            cbInfo[numBoxes].text = "";
292
 
 
293
 
        if (!strcmp(arg, "1") || !strcasecmp(arg, "on") || 
294
 
                !strcasecmp(arg, "yes"))
295
 
            cbStates[numBoxes] = '*';
296
 
        else
297
 
            cbStates[numBoxes] = ' ';
298
 
 
299
 
        if (strlen(cbInfo[numBoxes].tag) > (unsigned int)maxWidth)
300
 
            maxWidth = strlen(cbInfo[numBoxes].tag);
301
 
 
302
 
        numBoxes++;
303
 
    }
304
 
 
305
 
    form = newtForm(NULL, NULL, 0);
306
 
 
307
 
    tb = textbox(height - 3 - buttonHeight - listHeight, width - 2,
308
 
                        text, flags, &top);
309
 
 
310
 
    if (listHeight < numBoxes) { 
311
 
        sb = newtVerticalScrollbar(width - 4, 
312
 
                                   top + 1,
313
 
                                   listHeight, NEWT_COLORSET_CHECKBOX,
314
 
                                   NEWT_COLORSET_ACTCHECKBOX);
315
 
        newtFormAddComponent(form, sb);
316
 
    }
317
 
    subform = newtForm(sb, NULL, 0);
318
 
    newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX);
319
 
 
320
 
    sprintf(format, "%%-%ds  %%s", maxWidth);
321
 
    for (i = 0; i < numBoxes; i++) {
322
 
        sprintf(buf, format, cbInfo[i].tag, cbInfo[i].text);
323
 
 
324
 
        if (useRadio)
325
 
            cbInfo[i].comp = newtRadiobutton(4, top + 1 + i, buf,
326
 
                                        cbStates[i] != ' ', 
327
 
                                        i ? cbInfo[i - 1].comp : NULL);
328
 
        else
329
 
            cbInfo[i].comp = newtCheckbox(4, top + 1 + i, buf,
330
 
                              cbStates[i], NULL, cbStates + i);
331
 
 
332
 
        newtFormAddComponent(subform, cbInfo[i].comp);
333
 
    }
334
 
 
335
 
    newtFormSetHeight(subform, listHeight);
336
 
    newtFormSetWidth(subform, width - 10);
337
 
 
338
 
    newtFormAddComponents(form, tb, subform, NULL);
339
 
 
340
 
    addButtons(height, width, form, &okay, &cancel, flags);
341
 
 
342
 
    answer = newtRunForm(form);
343
 
    if (answer == cancel)
344
 
        rc = DLG_CANCEL;
345
 
 
346
 
    if (useRadio) {
347
 
        answer = newtRadioGetCurrent(cbInfo[0].comp);
348
 
        for (i = 0; i < numBoxes; i++) 
349
 
            if (cbInfo[i].comp == answer) {
350
 
                *selections = malloc(sizeof(char *) * 2);
351
 
                (*selections)[0] = cbInfo[i].tag;
352
 
                (*selections)[1] = NULL;
353
 
                break;
354
 
            }
355
 
    } else {
356
 
        numSelected = 0;
357
 
        for (i = 0; i < numBoxes; i++) {
358
 
            if (cbStates[i] != ' ') numSelected++;
359
 
        }
360
 
 
361
 
        *selections = malloc(sizeof(char *) * (numSelected + 1));
362
 
 
363
 
        numSelected = 0;
364
 
        for (i = 0; i < numBoxes; i++) {
365
 
            if (cbStates[i] != ' ') 
366
 
                (*selections)[numSelected++] = cbInfo[i].tag;
367
 
        }
368
 
 
369
 
        (*selections)[numSelected] = NULL;
370
 
    }
371
 
 
372
 
    return rc;
373
 
}
374
 
 
375
 
int messageBox(const char * text, int height, int width, int type, int flags) {
376
 
    newtComponent form, yes, tb, answer;
377
 
    newtComponent no = NULL;
378
 
    int tFlag = (flags & FLAG_SCROLL_TEXT) ? NEWT_FLAG_SCROLL : 0;
379
 
    char * buf, * src, * dst;
380
 
 
381
 
    dst = buf = alloca(strlen(text) + 1);
382
 
    src = text; 
383
 
    while (*src) {
384
 
        if (*src == '\\' && *(src + 1) == 'n') {
385
 
            src += 2;
386
 
            *dst++ = '\n';
387
 
        } else
388
 
            *dst++ = *src++;
389
 
    }
390
 
    *dst++ = '\0';
391
 
 
392
 
    form = newtForm(NULL, NULL, 0);
393
 
 
394
 
    if ( type != MSGBOX_INFO ) {
395
 
      tb = newtTextbox(1, 1, width - 2, height - 3 - buttonHeight, 
396
 
            NEWT_FLAG_WRAP | tFlag);
397
 
    }
398
 
    else {
399
 
      tb = newtTextbox(1, 1, width - 2, height - 2, NEWT_FLAG_WRAP | tFlag);
400
 
    }
401
 
 
402
 
    newtTextboxSetText(tb, buf);
403
 
 
404
 
    newtFormAddComponent(form, tb);
405
 
 
406
 
    switch ( type ) {
407
 
    case MSGBOX_INFO:
408
 
        break;
409
 
    case MSGBOX_MSG:
410
 
        yes = makeButton((width - 8) / 2, height - 1 - buttonHeight, "Ok");
411
 
        newtFormAddComponent(form, yes);
412
 
        break;
413
 
    default:
414
 
        yes = makeButton((width - 16) / 3, height - 1 - buttonHeight, "Yes");
415
 
        no = makeButton(((width - 16) / 3) * 2 + 9, height - 1 - buttonHeight, 
416
 
                        "No");
417
 
        newtFormAddComponents(form, yes, no, NULL);
418
 
 
419
 
        if (flags & FLAG_DEFAULT_NO)
420
 
            newtFormSetCurrent(form, no);
421
 
    }
422
 
 
423
 
    if ( type != MSGBOX_INFO ) {
424
 
        newtRunForm(form);
425
 
 
426
 
        answer = newtFormGetCurrent(form);
427
 
 
428
 
        if (answer == no)
429
 
            return DLG_CANCEL;
430
 
    }
431
 
    else {
432
 
        newtDrawForm(form);
433
 
        newtRefresh();
434
 
    }
435
 
        
436
 
 
437
 
 
438
 
    return DLG_OKAY;
439
 
}
440
 
 
441
 
void useFullButtons(int state) {
442
 
    if (state) {
443
 
        buttonHeight = 3;
444
 
        makeButton = newtButton;
445
 
   } else {
446
 
        buttonHeight = 1;
447
 
        makeButton = newtCompactButton;
448
 
   }
449
 
}