1 /* simple dialog boxes, used by both whiptail and tcl dialog bindings */
9 #include "dialogboxes.h"
15 newtComponent (*makeButton
)(int left
, int right
, 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
, char * text
, int flags
,
36 int sFlag
= (flags
& FLAG_SCROLL_TEXT
) ? NEWT_FLAG_SCROLL
: 0;
38 char * buf
, * src
, * dst
;
40 dst
= buf
= alloca(strlen(text
) + 1);
43 if (*src
== '\\' && *(src
+ 1) == 'n') {
51 tb
= newtTextbox(1, 0, width
, maxHeight
, NEWT_FLAG_WRAP
| sFlag
);
52 newtTextboxSetText(tb
, buf
);
54 i
= newtTextboxGetNumLines(tb
);
56 newtTextboxSetHeight(tb
, i
);
65 int gauge(char * text
, int height
, int width
, poptContext optCon
, int fd
,
67 newtComponent form
, scale
, tb
;
71 FILE * f
= fdopen(fd
, "r");
78 if (!(arg
= poptGetArg(optCon
))) return DLG_ERROR
;
79 val
= strtoul(arg
, &end
, 10);
80 if (*end
) return DLG_ERROR
;
82 tb
= textbox(height
- 3, width
- 2, text
, flags
, &top
);
84 form
= newtForm(NULL
, NULL
, 0);
86 scale
= newtScale(2, height
- 2, width
- 4, 100);
87 newtScaleSet(scale
, val
);
89 newtFormAddComponents(form
, tb
, scale
, NULL
);
94 while (fgets(buf
, sizeof(buf
) - 1, f
)) {
95 buf
[strlen(buf
) - 1] = '\0';
97 if (!strcmp(buf
, "XXX")) {
98 fgets(buf3
, sizeof(buf3
) - 1, f
);
99 buf3
[strlen(buf3
) - 1] = '\0';
103 while (fgets(buf
+ i
, sizeof(buf
) - 1 - i
, f
)) {
104 buf
[strlen(buf
) - 1] = '\0';
105 if (!strcmp(buf
+ i
, "XXX")) {
112 newtTextboxSetText(tb
, buf
);
117 val
= strtoul(buf
, &end
, 10);
119 newtScaleSet(scale
, val
);
128 int inputBox(char * text
, int height
, int width
, poptContext optCon
,
129 int flags
, char ** result
) {
130 newtComponent form
, entry
, okay
, cancel
, answer
, tb
;
135 val
= poptGetArg(optCon
);
136 tb
= textbox(height
- 3 - buttonHeight
, width
- 2,
139 form
= newtForm(NULL
, NULL
, 0);
140 entry
= newtEntry(1, top
+ 1, val
, width
- 2, &val
,
141 NEWT_FLAG_SCROLL
| NEWT_FLAG_RETURNEXIT
);
143 newtFormAddComponents(form
, tb
, entry
, NULL
);
145 addButtons(height
, width
, form
, &okay
, &cancel
, flags
);
147 answer
= newtRunForm(form
);
148 if (answer
== cancel
)
156 int listBox(char * text
, int height
, int width
, poptContext optCon
,
157 int flags
, char ** result
) {
158 newtComponent form
, okay
, tb
, answer
, listBox
;
159 newtComponent cancel
= NULL
;
163 int allocedItems
= 5;
166 char buf
[80], format
[20];
168 int maxTextWidth
= 0;
173 } * itemInfo
= malloc(allocedItems
* sizeof(*itemInfo
));
175 if (!(arg
= poptGetArg(optCon
))) return DLG_ERROR
;
176 listHeight
= strtoul(arg
, &end
, 10);
177 if (*end
) return DLG_ERROR
;
179 while ((arg
= poptGetArg(optCon
))) {
180 if (allocedItems
== numItems
) {
182 itemInfo
= realloc(itemInfo
, sizeof(*itemInfo
) * allocedItems
);
185 itemInfo
[numItems
].tag
= arg
;
186 if (!(arg
= poptGetArg(optCon
))) return DLG_ERROR
;
188 if (!(flags
& FLAG_NOITEM
)) {
189 itemInfo
[numItems
].text
= arg
;
191 itemInfo
[numItems
].text
= "";
193 if (strlen(itemInfo
[numItems
].text
) > maxTextWidth
)
194 maxTextWidth
= strlen(itemInfo
[numItems
].text
);
195 if (strlen(itemInfo
[numItems
].tag
) > maxTagWidth
)
196 maxTagWidth
= strlen(itemInfo
[numItems
].tag
);
201 form
= newtForm(NULL
, NULL
, 0);
203 tb
= textbox(height
- 4 - buttonHeight
- listHeight
, width
- 2,
206 if (listHeight
>= numItems
) {
207 noScrollFlag
= NEWT_FLAG_NOSCROLL
;
214 listBox
= newtListbox(3 + ((width
- 10 - maxTagWidth
- maxTextWidth
- i
)
217 NEWT_FLAG_RETURNEXIT
| noScrollFlag
);
219 sprintf(format
, "%%-%ds %%s", maxTagWidth
);
220 for (i
= 0; i
< numItems
; i
++) {
221 sprintf(buf
, format
, itemInfo
[i
].tag
, itemInfo
[i
].text
);
222 newtListboxAddEntry(listBox
, buf
, (void *) i
);
225 newtFormAddComponents(form
, tb
, listBox
, NULL
);
227 addButtons(height
, width
, form
, &okay
, &cancel
, flags
);
229 answer
= newtRunForm(form
);
230 if (answer
== cancel
)
233 i
= (int) newtListboxGetCurrent(listBox
);
234 *result
= itemInfo
[i
].tag
;
239 int checkList(char * text
, int height
, int width
, poptContext optCon
,
240 int useRadio
, int flags
, char *** selections
) {
241 newtComponent form
, okay
, tb
, subform
, answer
;
242 newtComponent sb
= NULL
, cancel
= NULL
;
246 int allocedBoxes
= 5;
250 char buf
[80], format
[20];
257 } * cbInfo
= malloc(allocedBoxes
* sizeof(*cbInfo
));
258 char * cbStates
= malloc(allocedBoxes
* sizeof(cbStates
));
260 if (!(arg
= poptGetArg(optCon
))) return DLG_ERROR
;
261 listHeight
= strtoul(arg
, &end
, 10);
262 if (*end
) return DLG_ERROR
;
264 while ((arg
= poptGetArg(optCon
))) {
265 if (allocedBoxes
== numBoxes
) {
267 cbInfo
= realloc(cbInfo
, sizeof(*cbInfo
) * allocedBoxes
);
268 cbStates
= realloc(cbStates
, sizeof(*cbStates
) * allocedBoxes
);
271 cbInfo
[numBoxes
].tag
= arg
;
272 if (!(arg
= poptGetArg(optCon
))) return DLG_ERROR
;
274 if (!(flags
& FLAG_NOITEM
)) {
275 cbInfo
[numBoxes
].text
= arg
;
276 if (!(arg
= poptGetArg(optCon
))) return DLG_ERROR
;
278 cbInfo
[numBoxes
].text
= "";
280 if (!strcmp(arg
, "1") || !strcasecmp(arg
, "on") ||
281 !strcasecmp(arg
, "yes"))
282 cbStates
[numBoxes
] = '*';
284 cbStates
[numBoxes
] = ' ';
286 if (strlen(cbInfo
[numBoxes
].tag
) > maxWidth
)
287 maxWidth
= strlen(cbInfo
[numBoxes
].tag
);
292 form
= newtForm(NULL
, NULL
, 0);
294 tb
= textbox(height
- 3 - buttonHeight
- listHeight
, width
- 2,
297 if (listHeight
< numBoxes
) {
298 sb
= newtVerticalScrollbar(width
- 4,
300 listHeight
, NEWT_COLORSET_CHECKBOX
,
301 NEWT_COLORSET_ACTCHECKBOX
);
302 newtFormAddComponent(form
, sb
);
304 subform
= newtForm(sb
, NULL
, 0);
305 newtFormSetBackground(subform
, NEWT_COLORSET_CHECKBOX
);
307 sprintf(format
, "%%-%ds %%s", maxWidth
);
308 for (i
= 0; i
< numBoxes
; i
++) {
309 sprintf(buf
, format
, cbInfo
[i
].tag
, cbInfo
[i
].text
);
312 cbInfo
[i
].comp
= newtRadiobutton(4, top
+ 1 + i
, buf
,
314 i
? cbInfo
[i
- 1].comp
: NULL
);
316 cbInfo
[i
].comp
= newtCheckbox(4, top
+ 1 + i
, buf
,
317 cbStates
[i
], NULL
, cbStates
+ i
);
319 newtFormAddComponent(subform
, cbInfo
[i
].comp
);
322 newtFormSetHeight(subform
, listHeight
);
323 newtFormSetWidth(subform
, width
- 10);
325 newtFormAddComponents(form
, tb
, subform
, NULL
);
327 addButtons(height
, width
, form
, &okay
, &cancel
, flags
);
329 answer
= newtRunForm(form
);
330 if (answer
== cancel
)
334 answer
= newtRadioGetCurrent(cbInfo
[0].comp
);
335 for (i
= 0; i
< numBoxes
; i
++)
336 if (cbInfo
[i
].comp
== answer
) {
337 *selections
= malloc(sizeof(char *) * 2);
338 (*selections
)[0] = cbInfo
[i
].tag
;
339 (*selections
)[1] = NULL
;
344 for (i
= 0; i
< numBoxes
; i
++) {
345 if (cbStates
[i
] != ' ') numSelected
++;
348 *selections
= malloc(sizeof(char *) * (numSelected
+ 1));
351 for (i
= 0; i
< numBoxes
; i
++) {
352 if (cbStates
[i
] != ' ')
353 (*selections
)[numSelected
++] = cbInfo
[i
].tag
;
356 (*selections
)[numSelected
] = NULL
;
362 int messageBox(char * text
, int height
, int width
, int type
, int flags
) {
363 newtComponent form
, yes
, tb
, answer
;
364 newtComponent no
= NULL
;
365 int tFlag
= (flags
& FLAG_SCROLL_TEXT
) ? NEWT_FLAG_SCROLL
: 0;
367 form
= newtForm(NULL
, NULL
, 0);
369 tb
= newtTextbox(1, 1, width
- 2, height
- 3 - buttonHeight
,
370 NEWT_FLAG_WRAP
| tFlag
);
371 newtTextboxSetText(tb
, text
);
373 newtFormAddComponent(form
, tb
);
375 if (type
== MSGBOX_MSG
) {
376 yes
= makeButton((width
- 8) / 2, height
- 1 - buttonHeight
, "Ok");
377 newtFormAddComponent(form
, yes
);
379 yes
= makeButton((width
- 16) / 3, height
- 1 - buttonHeight
, "Yes");
380 no
= makeButton(((width
- 16) / 3) * 2 + 9, height
- 1 - buttonHeight
,
382 newtFormAddComponents(form
, yes
, no
, NULL
);
384 if (flags
& FLAG_DEFAULT_NO
)
385 newtFormSetCurrent(form
, no
);
389 answer
= newtFormGetCurrent(form
);
397 void useFullButtons(int state
) {
400 makeButton
= newtButton
;
403 makeButton
= newtCompactButton
;