1
/* simple dialog boxes, used by both whiptail and tcl dialog bindings */
9
#include "dialogboxes.h"
14
static int buttonHeight = 1;
15
static newtComponent (*makeButton)(int left, int right, const char * text) =
18
static void addButtons(int height, int width, newtComponent form,
19
newtComponent * okay, newtComponent * cancel,
21
if (flags & FLAG_NOCANCEL) {
22
*okay = makeButton((width - 8) / 2, height - buttonHeight - 1, "Ok");
24
newtFormAddComponent(form, *okay);
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);
33
static newtComponent textbox(int maxHeight, int width, const char * text,
34
int flags, int * height) {
36
int sFlag = (flags & FLAG_SCROLL_TEXT) ? NEWT_FLAG_SCROLL : 0;
41
dst = buf = alloca(strlen(text) + 1);
44
if (*src == '\\' && *(src + 1) == 'n') {
52
tb = newtTextbox(1, 0, width, maxHeight, NEWT_FLAG_WRAP | sFlag);
53
newtTextboxSetText(tb, buf);
55
i = newtTextboxGetNumLines(tb);
57
newtTextboxSetHeight(tb, i);
66
int gauge(const char * text, int height, int width, poptContext optCon, int fd,
68
newtComponent form, scale, tb;
73
FILE * f = fdopen(fd, "r");
80
if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
81
val = strtoul(arg, &end, 10);
82
if (*end) return DLG_ERROR;
84
tb = textbox(height - 3, width - 2, text, flags, &top);
86
form = newtForm(NULL, NULL, 0);
88
scale = newtScale(2, height - 2, width - 4, 100);
89
newtScaleSet(scale, val);
91
newtFormAddComponents(form, tb, scale, NULL);
96
while (fgets(buf, sizeof(buf) - 1, f)) {
97
buf[strlen(buf) - 1] = '\0';
99
if (!strcmp(buf, "XXX")) {
100
fgets(buf3, sizeof(buf3) - 1, f);
101
buf3[strlen(buf3) - 1] = '\0';
105
while (fgets(buf + i, sizeof(buf) - 1 - i, f)) {
106
buf[strlen(buf) - 1] = '\0';
107
if (!strcmp(buf + i, "XXX")) {
114
newtTextboxSetText(tb, buf);
119
val = strtoul(buf, &end, 10);
121
newtScaleSet(scale, val);
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;
138
val = poptGetArg(optCon);
139
tb = textbox(height - 3 - buttonHeight, width - 2,
142
form = newtForm(NULL, NULL, 0);
143
entry = newtEntry(1, top + 1, val, width - 2, &val,
144
NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT | pFlag);
146
newtFormAddComponents(form, tb, entry, NULL);
148
addButtons(height, width, form, &okay, &cancel, flags);
150
answer = newtRunForm(form);
151
if (answer == cancel)
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;
167
int allocedItems = 5;
170
char *buf, format[20];
172
int maxTextWidth = 0;
177
} * itemInfo = malloc(allocedItems * sizeof(*itemInfo));
179
if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
180
listHeight = strtoul(arg, &end, 10);
181
if (*end) return DLG_ERROR;
183
while ((arg = poptGetArg(optCon))) {
184
if (allocedItems == numItems) {
186
itemInfo = realloc(itemInfo, sizeof(*itemInfo) * allocedItems);
189
itemInfo[numItems].tag = arg;
190
if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
192
if (!(flags & FLAG_NOITEM)) {
193
itemInfo[numItems].text = arg;
195
itemInfo[numItems].text = "";
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);
205
form = newtForm(NULL, NULL, 0);
207
tb = textbox(height - 4 - buttonHeight - listHeight, width - 2,
210
if (listHeight >= numItems) {
214
scrollFlag = NEWT_FLAG_SCROLL;
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);
223
sprintf(format, "%%-%ds %%s", maxTagWidth);
224
/* Junichi Uekawa - fix buffer overrun when width > 80 */
225
if (!(buf = malloc (width - 5)))
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);
237
newtFormAddComponents(form, tb, listBox, NULL);
239
addButtons(height, width, form, &okay, &cancel, flags);
241
answer = newtRunForm(form);
242
if (answer == cancel)
245
i = (int) newtListboxGetCurrent(listBox);
246
*result = itemInfo[i].tag;
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;
259
int allocedBoxes = 5;
263
char buf[80], format[20];
270
} * cbInfo = malloc(allocedBoxes * sizeof(*cbInfo));
271
char * cbStates = malloc(allocedBoxes * sizeof(cbStates));
273
if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
274
listHeight = strtoul(arg, &end, 10);
275
if (*end) return DLG_ERROR;
277
while ((arg = poptGetArg(optCon))) {
278
if (allocedBoxes == numBoxes) {
280
cbInfo = realloc(cbInfo, sizeof(*cbInfo) * allocedBoxes);
281
cbStates = realloc(cbStates, sizeof(*cbStates) * allocedBoxes);
284
cbInfo[numBoxes].tag = arg;
285
if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
287
if (!(flags & FLAG_NOITEM)) {
288
cbInfo[numBoxes].text = arg;
289
if (!(arg = poptGetArg(optCon))) return DLG_ERROR;
291
cbInfo[numBoxes].text = "";
293
if (!strcmp(arg, "1") || !strcasecmp(arg, "on") ||
294
!strcasecmp(arg, "yes"))
295
cbStates[numBoxes] = '*';
297
cbStates[numBoxes] = ' ';
299
if (strlen(cbInfo[numBoxes].tag) > (unsigned int)maxWidth)
300
maxWidth = strlen(cbInfo[numBoxes].tag);
305
form = newtForm(NULL, NULL, 0);
307
tb = textbox(height - 3 - buttonHeight - listHeight, width - 2,
310
if (listHeight < numBoxes) {
311
sb = newtVerticalScrollbar(width - 4,
313
listHeight, NEWT_COLORSET_CHECKBOX,
314
NEWT_COLORSET_ACTCHECKBOX);
315
newtFormAddComponent(form, sb);
317
subform = newtForm(sb, NULL, 0);
318
newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX);
320
sprintf(format, "%%-%ds %%s", maxWidth);
321
for (i = 0; i < numBoxes; i++) {
322
sprintf(buf, format, cbInfo[i].tag, cbInfo[i].text);
325
cbInfo[i].comp = newtRadiobutton(4, top + 1 + i, buf,
327
i ? cbInfo[i - 1].comp : NULL);
329
cbInfo[i].comp = newtCheckbox(4, top + 1 + i, buf,
330
cbStates[i], NULL, cbStates + i);
332
newtFormAddComponent(subform, cbInfo[i].comp);
335
newtFormSetHeight(subform, listHeight);
336
newtFormSetWidth(subform, width - 10);
338
newtFormAddComponents(form, tb, subform, NULL);
340
addButtons(height, width, form, &okay, &cancel, flags);
342
answer = newtRunForm(form);
343
if (answer == cancel)
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;
357
for (i = 0; i < numBoxes; i++) {
358
if (cbStates[i] != ' ') numSelected++;
361
*selections = malloc(sizeof(char *) * (numSelected + 1));
364
for (i = 0; i < numBoxes; i++) {
365
if (cbStates[i] != ' ')
366
(*selections)[numSelected++] = cbInfo[i].tag;
369
(*selections)[numSelected] = NULL;
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;
381
dst = buf = alloca(strlen(text) + 1);
384
if (*src == '\\' && *(src + 1) == 'n') {
392
form = newtForm(NULL, NULL, 0);
394
if ( type != MSGBOX_INFO ) {
395
tb = newtTextbox(1, 1, width - 2, height - 3 - buttonHeight,
396
NEWT_FLAG_WRAP | tFlag);
399
tb = newtTextbox(1, 1, width - 2, height - 2, NEWT_FLAG_WRAP | tFlag);
402
newtTextboxSetText(tb, buf);
404
newtFormAddComponent(form, tb);
410
yes = makeButton((width - 8) / 2, height - 1 - buttonHeight, "Ok");
411
newtFormAddComponent(form, yes);
414
yes = makeButton((width - 16) / 3, height - 1 - buttonHeight, "Yes");
415
no = makeButton(((width - 16) / 3) * 2 + 9, height - 1 - buttonHeight,
417
newtFormAddComponents(form, yes, no, NULL);
419
if (flags & FLAG_DEFAULT_NO)
420
newtFormSetCurrent(form, no);
423
if ( type != MSGBOX_INFO ) {
426
answer = newtFormGetCurrent(form);
441
void useFullButtons(int state) {
444
makeButton = newtButton;
447
makeButton = newtCompactButton;