Debian Codegraph @

src/modules/frontend/newt/newt.c

raw
1 /***********************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * File: newt.c
7 *
8 * Description: Newt UI for cdebconf
9 *
10 * cdebconf is (c) 2000-2001 Randolph Chung and others under the following
11 * license.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 ***********************************************************************/
37 #include "common.h"
38 #include "template.h"
39 #include "question.h"
40 #include "frontend.h"
41 #include "database.h"
42 #include "plugin.h"
43 #include "strutl.h"
44
45 #include <ctype.h>
46 #include <fcntl.h>
47 #include <signal.h>
48 #include <string.h>
49 #include <termios.h>
50 #include <unistd.h>
51 #include <sys/ioctl.h>
52 #include <sys/stat.h>
53 #include <sys/types.h>
54 #include <limits.h>
55 #include <wchar.h>
56 #include <assert.h>
57
58 #include <slang.h>
59 #include <newt.h>
60
61 #ifdef HAVE_LIBTEXTWRAP
62 #include <textwrap.h>
63 #endif
64
65 struct newt_data {
66 newtComponent scale_form,
67 scale_bar,
68 scale_textbox,
69 scale_cancel,
70 perc_label;
71 int scale_textbox_height;
72 struct question *scale_info;
73 };
74
75 struct newtColors newtAltColorPalette = {
76 /*"white", "blue",*/ /* root fg, bg */
77 "white", "black", /* root fg, bg */
78 /*"black", "lightgray",*/ /* border fg, bg */
79 "white", "black", /* border fg, bg */
80 /*"black", "lightgray",*/ /* window fg, bg */
81 "white", "black", /* window fg, bg */
82 /*"white", "black",*/ /* shadow fg, bg */
83 "white", "black", /* shadow fg, bg */
84 /*"red", "lightgray",*/ /* title fg, bg */
85 "yellow", "black", /* title fg, bg */
86 /*"brightred", "gray",*/ /* button fg, bg */
87 "black", "lightgray", /* button fg, bg */
88 "gray", "brightred", /* active button fg, bg */
89 "white", "gray", /* checkbox fg, bg */
90 /*"yellow", "brown",*/ /* active checkbox fg, bg */
91 "black", "lightgray", /* active checkbox fg, bg */
92 /*"yellow", "blue",*/ /* entry box fg, bg */
93 "white", "black", /* entry box fg, bg */
94 /*"blue", "lightgray",*/ /* label fg, bg */
95 "brightred", "black", /* label fg, bg */
96 /*"black", "lightgray",*/ /* listbox fg, bg */
97 "white", "black", /* listbox fg, bg */
98 "yellow", "blue", /* active listbox fg, bg */
99 /*"black", "lightgray",*/ /* textbox fg, bg */
100 "white", "black", /* textbox fg, bg */
101 "lightgray", "black", /* active textbox fg, bg */
102 /*"white", "blue",*/ /* help line */
103 "white", "black", /* help line */
104 "yellow", "blue", /* root text */
105 /*"blue",*/ /* scale full */
106 "black", /* scale full */
107 /*"red",*/ /* scale empty */
108 "blue", /* scale empty */
109 "blue", "lightgray", /* disabled entry fg, bg */
110 /*"black", "lightgray",*/ /* compact button fg, bg */
111 "white", "black", /* compact button fg, bg */
112 "black", "lightgray", /* active & sel listbox */
113 "black", "brown" /* selected listbox */
114 };
115
116 typedef int (newt_handler)(struct frontend *obj, struct question *q);
117
118 struct newt_help_callback_data {
119 struct frontend *obj;
120 const char *tag;
121 };
122
123 static void newt_progress_stop(struct frontend *obj);
124
125 /* Result must be freed by the caller */
126 static char *get_full_description(struct frontend *obj, struct question *q);
127
128 #include "cdebconf_newt.h"
129
130 /* Padding of title width, allows for leading "[!!] " before title
131 * and the two vertical bars and spaces that limit the title.
132 */
133 #define TITLE_PADDING 9
134
135 /* At least one blue line on top
136 * One line for the title / top frame
137 * One line for the bottom frame
138 * One line for shadow on the bottom
139 * One line with a menu
140 */
141 #define MIN_DECORATION_HEIGHT 5
142
143 /* gettext would be much nicer :-( */
144 static const char *
145 continue_text(struct frontend *obj)
146 {
147 return question_get_text(obj, "debconf/button-continue", "Continue");
148 }
149
150 static const char *
151 goback_text(struct frontend *obj)
152 {
153 return question_get_text(obj, "debconf/button-goback", "Go Back");
154 }
155
156 static const char *
157 yes_text(struct frontend *obj)
158 {
159 return question_get_text(obj, "debconf/button-yes", "Yes");
160 }
161
162 static const char *
163 no_text(struct frontend *obj)
164 {
165 return question_get_text(obj, "debconf/button-no", "No");
166 }
167
168 static const char *
169 cancel_text(struct frontend *obj)
170 {
171 return question_get_text(obj, "debconf/button-cancel", "Cancel");
172 }
173
174 static const char *
175 help_text(struct frontend *obj)
176 {
177 return question_get_text(obj, "debconf/help-line", "<Tab> moves; <Space> selects; <Enter> activates buttons");
178 }
179
180 static const char *
181 help_text_f1(struct frontend *obj)
182 {
183 return question_get_text(obj, "debconf/help-line-f1", "<F1> for help; <Tab> moves; <Space> selects; <Enter> activates buttons");
184 }
185
186 static const char *
187 show_password_text(struct frontend *obj)
188 {
189 return question_get_text(obj, "debconf/show-password", "Show Password in Clear");
190 }
191
192 void
193 cdebconf_newt_setup(void)
194 {
195 SLang_init_tty(0, 1, 0); /* disable flow control */
196 newtInit();
197 newtSetColors(newtAltColorPalette);
198 newtCls();
199 }
200
201 int
202 cdebconf_newt_brl(void)
203 {
204 if (access("/var/run/brltty.pid", O_RDONLY) == 0)
205 return 1;
206 return 0;
207 }
208
209 int
210 cdebconf_newt_left(int left)
211 {
212 if (cdebconf_newt_brl())
213 return 1;
214 return left;
215 }
216
217 /* cdebconf-newt-terminal needs this in order to be able to restore the
218 * display properly after tearing down the terminal.
219 */
220 struct question *
221 cdebconf_newt_get_progress_info(struct frontend *obj)
222 {
223 struct newt_data *data = (struct newt_data *)obj->data;
224 if (data->scale_info) {
225 question_ref(data->scale_info);
226 return data->scale_info;
227 } else
228 return NULL;
229 }
230
231 /* This is global due to cdebconf_newt_create_form's interface. It might be
232 * worth fixing this at the next convenient API break.
233 */
234 static struct newt_help_callback_data *help_cb_data;
235
236 newtComponent
237 cdebconf_newt_create_form(newtComponent scrollbar)
238 {
239 return newtForm(scrollbar, help_cb_data, 0);
240 }
241
242 void
243 cdebconf_newt_create_window(const int width, const int height, const char *title, const char *priority)
244 {
245 static const char *sigils[][2] = {
246 { "low", "." },
247 { "medium", "?" },
248 { "high", "!" },
249 { "critical", "!!" },
250 { NULL, NULL }
251 };
252 char *buf = NULL;
253 int i = -1;
254
255 if (priority != NULL) {
256 for (i = 0; sigils[i][0] != NULL; i++) {
257 if (strcmp(priority, sigils[i][0]) == 0)
258 break;
259 }
260 if (sigils[i][0] != NULL)
261 if (asprintf(&buf, "[%s] %s", sigils[i][1], title) == -1)
262 buf = NULL;
263 }
264
265 if (buf != NULL)
266 title = buf;
267
268 if (cdebconf_newt_brl())
269 newtOpenWindow(1, 1, width, height, title);
270 else
271 newtCenteredWindow(width, height, title);
272
273 if (buf != NULL)
274 free(buf);
275 }
276
277 int
278 cdebconf_newt_get_text_height(const char *text, int win_width)
279 {
280 newtComponent textbox, f;
281 int t_height;
282 #ifdef HAVE_LIBTEXTWRAP
283 textwrap_t tw;
284 char *wrappedtext;
285
286 textwrap_init(&tw);
287 textwrap_columns(&tw, win_width - 2 - 2*TEXT_PADDING);
288 wrappedtext = textwrap(&tw, text);
289 textbox = newtTextbox(TEXT_PADDING, 2, win_width - 2 - 2*TEXT_PADDING, 10, NEWT_FLAG_SCROLL);
290 #else
291
292 // Create a dummy testbox to find out how many lines the text in the
293 // question will occupy
294 textbox = newtTextbox(TEXT_PADDING, 2, win_width - 2 - 2*TEXT_PADDING, 10, NEWT_FLAG_SCROLL|NEWT_FLAG_WRAP);
295 #endif
296 assert(textbox);
297 assert(text);
298 #ifdef HAVE_LIBTEXTWRAP
299 newtTextboxSetText(textbox, wrappedtext);
300 free(wrappedtext);
301 #else
302 newtTextboxSetText(textbox, text);
303 #endif
304 t_height = newtTextboxGetNumLines(textbox);
305 // This is needed so the textbox gets freed...ick
306 f = cdebconf_newt_create_form(NULL);
307 newtFormAddComponent(f, textbox);
308 newtFormDestroy(f);
309 return t_height;
310 }
311
312 int
313 cdebconf_newt_get_text_width(const char *text)
314 {
315 int t_width = 0;
316 const char *p = text;
317 size_t res;
318 int k;
319 wchar_t c;
320
321 p = text;
322 do {
323 for (res = 0; (k = mbtowc (&c, p, MB_LEN_MAX)) > 0 && c != L'\n'; p += k)
324 res += wcwidth (c);
325 if (res > t_width)
326 t_width = res;
327 if (*p)
328 p++;
329 } while (*p);
330
331 return t_width;
332 }
333
334 static int
335 min_window_height(struct frontend *obj, struct question *q, int win_width)
336 {
337 // start with a blank or description (note and error)
338 // End with <Continue>/boolean buttons + blank
339 int height = 3;
340 char *type = q->template->type;
341 char *q_text;
342
343 if (strcmp(q->template->type, "note") == 0 || strcmp(q->template->type, "error") == 0)
344 q_text = q_get_extended_description(obj, q);
345 else
346 q_text = get_full_description(obj, q);
347 if (q_text != NULL) {
348 height += cdebconf_newt_get_text_height(q_text, win_width) + 1;
349 free (q_text);
350 }
351 if (strcmp(type, "multiselect") == 0)
352 height += 4; // x lines for choices + blank line
353 else if (strcmp(type, "select") == 0) {
354 height += 2; // as multiselect, but without a button and blank line
355 if (obj->methods.can_go_back(obj, q))
356 height += 2;
357 }
358 else if (strcmp(type, "string") == 0 || strcmp(type, "password") == 0)
359 height += 2; // input line + blank line
360 // the others don't need more space
361 return height;
362 }
363
364 static int
365 need_separate_window(struct frontend *obj, struct question *q)
366 {
367 int width = 80, height = 24;
368 int x;
369
370 newtGetScreenSize(&width, &height);
371 x = min_window_height(obj, q, width-7);
372 return (x > height-MIN_DECORATION_HEIGHT);
373 }
374
375 static char *
376 get_full_description(struct frontend *obj, struct question *q)
377 {
378 char *res = NULL;
379 char *descr = q_get_description(obj, q);
380 char *ext_descr = q_get_extended_description(obj, q);
381
382 assert(descr);
383 assert(ext_descr);
384 res = malloc(strlen(descr)+strlen(ext_descr)+3);
385 *res = '\0';
386 if (*ext_descr) {
387 strcpy(res, ext_descr);
388 strcat(res, "\n\n");
389 }
390 strcat(res, descr);
391 free(descr);
392 free(ext_descr);
393 return res;
394 }
395
396 static int
397 show_separate_window(struct frontend *obj, struct question *q, int is_help)
398 {
399 newtComponent form, textbox, bOk, bCancel, cRet;
400 int width = 80, height = 24, t_height, t_width, win_width, win_height;
401 int t_width_scroll = 0, t_width_title, t_width_descr, t_width_buttons;
402 // buttons
403 int extra = 3;
404 int format_note = 0;
405 int t_voffset = 0;
406 char *full_description;
407 int ret;
408 #ifdef HAVE_LIBTEXTWRAP
409 int flags = 0;
410 textwrap_t tw;
411 char *wrappedtext;
412 #else
413 int flags = NEWT_FLAG_WRAP;
414 #endif
415 char *descr = q_get_description(obj, q);
416 char *ext_descr = q_get_extended_description(obj, q);
417
418 assert(descr);
419 assert(ext_descr);
420
421 if (strcmp(q->template->type, "note") == 0 || strcmp(q->template->type, "error") == 0)
422 {
423 format_note = 1;
424 t_voffset = 1;
425 extra += 2;
426 full_description = strdup(ext_descr);
427 }
428 else
429 full_description = get_full_description(obj, q);
430
431 newtGetScreenSize(&width, &height);
432 win_width = width-7;
433 /* There are 5 characters for sigils, plus 4 for borders */
434 strtruncate(obj->title, win_width-9);
435 t_height = cdebconf_newt_get_text_height(full_description, win_width);
436 if (t_height+extra <= height-5)
437 win_height = t_height+extra;
438 else {
439 win_height = height-5;
440 flags |= NEWT_FLAG_SCROLL;
441 t_width_scroll = 2;
442 }
443 t_height = win_height-extra;
444 #ifdef HAVE_LIBTEXTWRAP
445 textwrap_init(&tw);
446 textwrap_columns(&tw, win_width - 2 - 2*TEXT_PADDING);
447 wrappedtext = textwrap(&tw, full_description);
448 free(full_description);
449 full_description = wrappedtext;
450 #endif
451 t_width = cdebconf_newt_get_text_width(full_description);
452 t_width_descr = cdebconf_newt_get_text_width(descr);
453 if (t_width_descr > t_width)
454 t_width = t_width_descr;
455 t_width_buttons = 2*BUTTON_PADDING + cdebconf_newt_get_text_width(continue_text(obj)) + 2;
456 if (!is_help && obj->methods.can_go_back(obj, q))
457 // Add an interspace
458 t_width_buttons += cdebconf_newt_get_text_width(goback_text(obj)) + 3;
459 if (t_width_buttons > t_width)
460 t_width = t_width_buttons;
461 if (win_width > t_width + 2*TEXT_PADDING + t_width_scroll)
462 win_width = t_width + 2*TEXT_PADDING + t_width_scroll;
463 t_width_title = cdebconf_newt_get_text_width(obj->title) + TITLE_PADDING;
464 if (t_width_title > win_width)
465 win_width = t_width_title;
466 cdebconf_newt_create_window(win_width, win_height, obj->title, q->priority);
467 form = cdebconf_newt_create_form(NULL);
468 if (format_note)
469 newtFormAddComponent(form, newtLabel(cdebconf_newt_left((win_width - strwidth(descr))/2), 1, descr));
470 textbox = newtTextbox(TEXT_PADDING, 1 + t_voffset, t_width, t_height, flags);
471 assert(textbox);
472 newtTextboxSetText(textbox, full_description);
473 free(full_description);
474 if (!is_help && obj->methods.can_go_back(obj, q)) {
475 bOk = newtCompactButton(win_width - TEXT_PADDING - BUTTON_PADDING - strwidth(continue_text(obj)) - 3, win_height-2, continue_text(obj));
476 bCancel = newtCompactButton(TEXT_PADDING + BUTTON_PADDING - 1, win_height-2, goback_text(obj));
477 newtFormAddComponents(form, bCancel, textbox, bOk, NULL);
478 } else {
479 bOk = newtCompactButton(cdebconf_newt_left((win_width-strwidth(continue_text(obj))-2)/2 - 1), win_height-2, continue_text(obj));
480 bCancel = NULL;
481 newtFormAddComponents(form, textbox, bOk, NULL);
482 }
483 newtFormSetCurrent(form, bOk);
484 cRet = newtRunForm(form);
485 if (cRet == bOk)
486 ret = DC_OK;
487 else if ((cRet == NULL) || (bCancel != NULL && cRet == bCancel))
488 ret = DC_GOBACK;
489 else
490 ret = DC_NOTOK;
491 newtFormDestroy(form);
492 newtPopWindow();
493 free(descr);
494 free(ext_descr);
495 return ret;
496 }
497
498 static void checkbox_callback(newtComponent co, void * data)
499 {
500 char value = newtCheckboxGetValue(co);
501 newtComponent entry = (newtComponent)data;
502 newtEntrySetFlags(entry, NEWT_FLAG_PASSWORD, (value == '*') ? NEWT_FLAGS_RESET : NEWT_FLAGS_SET);
503 }
504
505 static int
506 generic_handler_string(struct frontend *obj, struct question *q, int eflags)
507 {
508 newtComponent form, textbox, bOk, bCancel, entry, checkbox, cRet;
509 int width = 80, height = 24, t_height, t_width, win_width, win_height, win_height_margin = 6;
510 int t_width_scroll = 0, t_width_title, t_width_buttons;
511 int ret;
512 #ifdef HAVE_LIBTEXTWRAP
513 int tflags = 0;
514 textwrap_t tw;
515 char *wrappedtext;
516 #else
517 int tflags = NEWT_FLAG_WRAP;
518 #endif
519 char *defval;
520 const char *result;
521 char *full_description = get_full_description(obj, q);
522
523 eflags |= NEWT_ENTRY_SCROLL | NEWT_FLAG_RETURNEXIT;
524 newtGetScreenSize(&width, &height);
525 win_width = width-7;
526 strtruncate(obj->title, win_width-9);
527 #ifdef HAVE_LIBTEXTWRAP
528 textwrap_init(&tw);
529 textwrap_columns(&tw, win_width - 2 - 2*TEXT_PADDING);
530 wrappedtext = textwrap(&tw, full_description);
531 free(full_description);
532 full_description = wrappedtext;
533 #endif
534 if (full_description != NULL)
535 t_height = cdebconf_newt_get_text_height(full_description, win_width);
536 else
537 t_height = 0;
538 if (eflags & NEWT_FLAG_PASSWORD)
539 win_height_margin += 2;
540 if (t_height + win_height_margin <= height-5)
541 win_height = t_height + win_height_margin;
542 else {
543 win_height = height - 5;
544 tflags |= NEWT_FLAG_SCROLL;
545 t_width_scroll = 2;
546 }
547 t_height = win_height - win_height_margin;
548 t_width = cdebconf_newt_get_text_width(full_description);
549 t_width_buttons = 2*BUTTON_PADDING + cdebconf_newt_get_text_width(continue_text(obj)) + 2;
550 if (obj->methods.can_go_back(obj, q))
551 // Add an interspace
552 t_width_buttons += cdebconf_newt_get_text_width(goback_text(obj)) + 3;
553 if (t_width_buttons > t_width)
554 t_width = t_width_buttons;
555 if (win_width > t_width + 2*TEXT_PADDING + t_width_scroll)
556 win_width = t_width + 2*TEXT_PADDING + t_width_scroll;
557 t_width_title = cdebconf_newt_get_text_width(obj->title) + TITLE_PADDING;
558 if (t_width_title > win_width)
559 win_width = t_width_title;
560 cdebconf_newt_create_window(win_width, win_height, obj->title, q->priority);
561 form = cdebconf_newt_create_form(NULL);
562 textbox = newtTextbox(TEXT_PADDING, 1, t_width, t_height, tflags);
563 assert(textbox);
564 if (full_description != NULL)
565 {
566 newtTextboxSetText(textbox, full_description);
567 free(full_description);
568 }
569 if (eflags & NEWT_FLAG_PASSWORD || question_getvalue(q, "") == NULL)
570 defval = "";
571 else
572 defval = (char *)question_getvalue(q, "");
573 entry = newtEntry(TEXT_PADDING, 1+t_height+1, defval, t_width, &result, eflags);
574 if (obj->methods.can_go_back(obj, q)) {
575 bOk = newtCompactButton(win_width - TEXT_PADDING - BUTTON_PADDING - strwidth(continue_text(obj)) - 3, win_height-2, continue_text(obj));
576 bCancel = newtCompactButton(TEXT_PADDING + BUTTON_PADDING - 1, win_height-2, goback_text(obj));
577 newtFormAddComponent(form, bCancel);
578 } else {
579 bOk = newtCompactButton(cdebconf_newt_left((win_width-strwidth(continue_text(obj))-2)/2 - 1), win_height-2, continue_text(obj));
580 bCancel = NULL;
581 }
582 newtFormAddComponents(form, textbox, entry, NULL);
583 if (eflags & NEWT_FLAG_PASSWORD)
584 {
585 checkbox = newtCheckbox(1, win_height-4, show_password_text(obj), ' ', " *", NULL);
586 newtComponentAddCallback(checkbox, checkbox_callback, entry);
587 newtFormAddComponent(form, checkbox);
588 }
589 newtFormAddComponent(form, bOk);
590 newtFormSetCurrent(form, entry);
591 cRet = newtRunForm(form);
592 if ((cRet == NULL) || (bCancel != NULL && cRet == bCancel))
593 ret = DC_GOBACK;
594 else {
595 ret = DC_OK;
596 question_setvalue(q, result);
597 }
598 newtFormDestroy(form);
599 newtPopWindow();
600 return ret;
601 }
602
603 static int
604 show_multiselect_window(struct frontend *obj, struct question *q, int show_ext_desc)
605 {
606 newtComponent form, sform = NULL, scrollbar, textbox, checkbox, bOk, bCancel, cRet;
607 int width = 80, height = 24;
608 int win_width, win_height = -1, t_height, t_width, sel_height, sel_width;
609 int t_width_title, t_width_buttons;
610 char **choices, **choices_trans, **defvals, *answer;
611 int count = 0, defcount, i, k, ret, def;
612 char *p;
613 size_t res;
614 wchar_t c;
615 int *tindex = NULL;
616 const char *indices = q_get_indices(obj, q);
617 char *full_description = get_full_description(obj, q);
618 #ifdef HAVE_LIBTEXTWRAP
619 textwrap_t tw;
620 char *wrappedtext;
621 int tflags = 0;
622 #else
623 int tflags = NEWT_FLAG_WRAP;
624 #endif
625
626 newtGetScreenSize(&width, &height);
627 win_width = width-7;
628 strtruncate(obj->title, win_width-9);
629 count = strgetargc(q_get_choices_vals(obj, q));
630 if (count <= 0)
631 return DC_NOTOK;
632 choices = malloc(sizeof(char *) * count);
633 choices_trans = malloc(sizeof(char *) * count);
634 tindex = malloc(sizeof(int) * count);
635 if (strchoicesplitsort(q_get_choices_vals(obj, q), q_get_choices(obj, q), indices, choices, choices_trans, tindex, count) != count)
636 return DC_NOTOK;
637
638 if (obj->methods.can_align(obj, q)) {
639 stralign(choices_trans, count);
640 }
641
642 defvals = malloc(sizeof(char *) * count);
643 defcount = strchoicesplit(question_getvalue(q, ""), defvals, count);
644 answer = malloc(sizeof(char) * count);
645 sel_height = count;
646 form = cdebconf_newt_create_form(NULL);
647 #ifdef HAVE_LIBTEXTWRAP
648 textwrap_init(&tw);
649 textwrap_columns(&tw, win_width - 2 - 2*TEXT_PADDING);
650 wrappedtext = textwrap(&tw, full_description);
651 free(full_description);
652 full_description = wrappedtext;
653 #endif
654 sel_width = strlongest(choices_trans, count);
655 t_width = cdebconf_newt_get_text_width(full_description);
656 t_width_buttons = 2*BUTTON_PADDING + cdebconf_newt_get_text_width(continue_text(obj)) + 2;
657 if (obj->methods.can_go_back(obj, q))
658 // Add an interspace
659 t_width_buttons += cdebconf_newt_get_text_width(goback_text(obj)) + 3;
660 if (t_width_buttons > t_width)
661 t_width = t_width_buttons;
662 // Truncate too wide items
663 if (sel_width > win_width-8) {
664 sel_width = win_width-8;
665 for (i = 0; i < count; i++) {
666 if (strwidth(choices_trans[i]) > sel_width) {
667 for (res = 0, p = choices_trans[i]; (k = mbtowc (&c, p, MB_LEN_MAX)) > 0; p += k) {
668 res += wcwidth (c);
669 if (res > sel_width)
670 break;
671 }
672 *p = 0;
673 }
674 }
675 }
676 if (t_width < sel_width)
677 t_width = sel_width;
678 if (win_width > t_width + 8)
679 win_width = t_width + 8;
680 t_width_title = cdebconf_newt_get_text_width(obj->title) + TITLE_PADDING;
681 if (t_width_title > win_width)
682 win_width = t_width_title;
683 if (show_ext_desc && full_description) {
684 textbox = newtTextbox(TEXT_PADDING, 1, t_width, 10, tflags);
685 assert(textbox);
686 newtTextboxSetText(textbox, full_description);
687 t_height = newtTextboxGetNumLines(textbox);
688 newtTextboxSetHeight(textbox, t_height);
689 newtFormAddComponent(form, textbox);
690 } else
691 t_height = 0;
692 free(full_description);
693 win_height = t_height + 5 + sel_height;
694 if (win_height > height-5) {
695 win_height = height-5;
696 sel_height = win_height - t_height - 5;
697 }
698 cdebconf_newt_create_window(win_width, win_height, obj->title, q->priority);
699 if (count > sel_height) {
700 int left = cdebconf_newt_brl() ? sel_width + 5: (win_width+sel_width+5)/2;
701 scrollbar = newtVerticalScrollbar(left, 1+t_height+1, sel_height,
702 NEWT_COLORSET_WINDOW, NEWT_COLORSET_ACTCHECKBOX);
703 newtFormAddComponent(form, scrollbar);
704 }
705 else
706 scrollbar = NULL;
707 sform = cdebconf_newt_create_form(scrollbar);
708 newtFormSetBackground(sform, NEWT_COLORSET_CHECKBOX);
709 newtFormSetHeight(sform, sel_height);
710 newtFormSetWidth(sform, sel_width+5);
711 for (i = 0; i < count; i++) {
712 def = 0;
713 for (k = 0; k < defcount; k++)
714 if (strcmp(choices[tindex[i]], defvals[k]) == 0)
715 def = 1;
716 checkbox = newtCheckbox(cdebconf_newt_left((win_width-sel_width-3)/2), 1+t_height+1+i, choices_trans[i], def ? '*' : ' ', " *", &answer[tindex[i]]);
717 newtCheckboxSetFlags(checkbox, NEWT_FLAG_RETURNEXIT, NEWT_FLAGS_SET);
718 newtFormAddComponent(sform, checkbox);
719 }
720 if (obj->methods.can_go_back(obj, q) || !show_ext_desc) {
721 bOk = newtCompactButton(win_width - TEXT_PADDING - BUTTON_PADDING - strwidth(continue_text(obj)) - 3, win_height-2, continue_text(obj));
722 bCancel = newtCompactButton(TEXT_PADDING + BUTTON_PADDING - 1, win_height-2, goback_text(obj));
723 newtFormAddComponents(form, bCancel, sform, bOk, NULL);
724 } else {
725 bOk = newtCompactButton(cdebconf_newt_left((win_width-strwidth(continue_text(obj))-2)/2 - 1), win_height-2, continue_text(obj));
726 bCancel = NULL;
727 newtFormAddComponents(form, sform, bOk, NULL);
728 }
729 newtFormSetCurrent(form, sform);
730 cRet = newtRunForm(form);
731 if ((cRet == NULL) || (bCancel != NULL && cRet == bCancel))
732 ret = DC_GOBACK;
733 else {
734 char *ans = strdup(""), *tmp;
735
736 for (i = 0; i < count; i++) {
737 if (answer[i] != ' ') {
738 if (ans[0] == '\0')
739 tmp = strdup(choices[i]);
740 else
741 asprintf(&tmp, "%s, %s", ans, choices[i]);
742 ans = tmp;
743 }
744 free(choices[i]);
745 free(choices_trans[i]);
746 }
747 free(choices);
748 free(choices_trans);
749 free(tindex);
750 free(answer);
751 question_setvalue(q, ans);
752 for (i = 0; i < defcount; i++)
753 free(defvals[i]);
754 free(defvals);
755 ret = DC_OK;
756 }
757 newtFormDestroy(form);
758 newtPopWindow();
759 return ret;
760 }
761
762 static int
763 show_select_window(struct frontend *obj, struct question *q, int show_ext_desc)
764 {
765 newtComponent form, listbox, textbox, bCancel, cRet;
766 int listflags = NEWT_FLAG_RETURNEXIT;
767 int width = 80, height = 24;
768 int win_width, win_height = -1, t_height, t_width, sel_height, sel_width;
769 int select_list_top;
770 int b_height;
771 int t_width_title, t_width_buttons;
772 char **choices, **choices_trans, *defval;
773 int count = 0, i, ret, defchoice = -1;
774 int *tindex = NULL;
775 const char *indices = q_get_indices(obj, q);
776 char *full_description = get_full_description(obj, q);
777 char *p;
778 size_t res;
779 int k;
780 wchar_t c;
781 #ifdef HAVE_LIBTEXTWRAP
782 textwrap_t tw;
783 char *wrappedtext;
784 int tflags = 0;
785 #else
786 int tflags = NEWT_FLAG_WRAP;
787 #endif
788
789 newtGetScreenSize(&width, &height);
790 win_width = width-7;
791 strtruncate(obj->title, win_width-9);
792 count = strgetargc(q_get_choices_vals(obj, q));
793 if (count <= 0)
794 return DC_NOTOK;
795 choices = malloc(sizeof(char *) * count);
796 choices_trans = malloc(sizeof(char *) * count);
797 tindex = malloc(sizeof(int) * count);
798 if (strchoicesplitsort(q_get_choices_vals(obj, q), q_get_choices(obj, q), indices, choices, choices_trans, tindex, count) != count)
799 return DC_NOTOK;
800
801 if (obj->methods.can_align(obj, q)) {
802 stralign(choices_trans, count);
803 }
804
805 sel_height = count;
806 form = cdebconf_newt_create_form(NULL);
807 #ifdef HAVE_LIBTEXTWRAP
808 textwrap_init(&tw);
809 textwrap_columns(&tw, win_width - 2 - 2*TEXT_PADDING);
810 wrappedtext = textwrap(&tw, full_description);
811 free(full_description);
812 full_description = wrappedtext;
813 #endif
814 sel_width = strlongest(choices_trans, count);
815 t_width = cdebconf_newt_get_text_width(full_description);
816 t_width_buttons = 2*BUTTON_PADDING;
817 if (obj->methods.can_go_back(obj, q))
818 // Add an interspace
819 t_width_buttons += cdebconf_newt_get_text_width(goback_text(obj)) + 3;
820 if (t_width_buttons > t_width)
821 t_width = t_width_buttons;
822 // Truncate too wide items
823 if (sel_width > win_width-8) {
824 sel_width = win_width-8;
825 for (i = 0; i < count; i++) {
826 if (strwidth(choices_trans[i]) > sel_width) {
827 for (res = 0, p = choices_trans[i]; (k = mbtowc (&c, p, MB_LEN_MAX)) > 0; p += k) {
828 res += wcwidth (c);
829 if (res > sel_width)
830 break;
831 }
832 *p = 0;
833 }
834 }
835 }
836 if (t_width < sel_width)
837 t_width = sel_width;
838 if (win_width > t_width + 8)
839 win_width = t_width + 8;
840 t_width_title = cdebconf_newt_get_text_width(obj->title) + TITLE_PADDING;
841 if (t_width_title > win_width)
842 win_width = t_width_title;
843 if (show_ext_desc && full_description) {
844 textbox = newtTextbox(TEXT_PADDING, 1, t_width, 10, tflags);
845 assert(textbox);
846 newtTextboxSetText(textbox, full_description);
847 t_height = newtTextboxGetNumLines(textbox);
848 newtTextboxSetHeight(textbox, t_height);
849 newtFormAddComponent(form, textbox);
850 select_list_top = 1+t_height+1;
851 } else {
852 t_height = 0;
853 select_list_top = 1; // No description. Only insert a blank line.
854 }
855 if (obj->methods.can_go_back(obj, q))
856 b_height = 2;
857 else
858 b_height = 0;
859 free(full_description);
860 win_height = t_height + sel_height + b_height;
861 // 3 == First blank line + blanks before and after select
862 // or 3 == Blanks before and after select + blank after <Go Back>
863 win_height += 3;
864 if (win_height > height-MIN_DECORATION_HEIGHT) {
865 win_height = height-MIN_DECORATION_HEIGHT;
866 sel_height = win_height - t_height - 3 - b_height;
867 }
868 if (count > sel_height)
869 listflags |= NEWT_FLAG_SCROLL;
870 cdebconf_newt_create_window(win_width, win_height, obj->title, q->priority);
871 listbox = newtListbox(cdebconf_newt_left((win_width-sel_width-3)/2), select_list_top, sel_height, listflags);
872 defval = (char *)question_getvalue(q, "");
873 for (i = 0; i < count; i++) {
874 newtListboxAppendEntry(listbox, choices_trans[i], choices[tindex[i]]);
875 if (defval != NULL && strcmp(defval, choices[tindex[i]]) == 0)
876 defchoice = i;
877 }
878 free(tindex);
879 free(choices);
880 free(choices_trans);
881 if (count == 1)
882 defchoice = 0;
883 if (defchoice >= 0)
884 newtListboxSetCurrent(listbox, defchoice);
885 if (obj->methods.can_go_back(obj, q) || !show_ext_desc) {
886 bCancel = newtCompactButton(TEXT_PADDING + BUTTON_PADDING - 1, win_height-2, goback_text(obj));
887 newtFormAddComponents(form, bCancel, listbox, NULL);
888 } else {
889 bCancel = NULL;
890 newtFormAddComponents(form, listbox, NULL);
891 }
892 newtFormSetCurrent(form, listbox);
893 cRet = newtRunForm(form);
894 if ((cRet == NULL) || (bCancel != NULL && cRet == bCancel))
895 ret = DC_GOBACK;
896 else {
897 if (newtListboxGetCurrent(listbox) != NULL)
898 question_setvalue(q, newtListboxGetCurrent(listbox));
899 ret = DC_OK;
900 }
901 newtFormDestroy(form);
902 newtPopWindow();
903 return ret;
904 }
905
906 /*
907 * Function: newt_handler_boolean
908 * Input: struct frontend *obj - frontend object
909 * struct question *q - question to ask
910 * Output: int - DC_OK, DC_NOTOK
911 * Description: handler for the boolean question type
912 * Assumptions: none
913 */
914 static int
915 newt_handler_boolean(struct frontend *obj, struct question *q)
916 {
917 newtComponent form, bYes, bNo, bCancel, textbox, cRet;
918 int width = 80, height = 24;
919 int win_width, win_height = -1, t_height, t_width;
920 int t_width_scroll = 0, t_width_title, t_width_buttons;
921 int ret;
922 #ifdef HAVE_LIBTEXTWRAP
923 textwrap_t tw;
924 char *wrappedtext;
925 int flags = 0;
926 #else
927 int flags = NEWT_FLAG_WRAP;
928 #endif
929 char *full_description = get_full_description(obj, q);
930
931 newtGetScreenSize(&width, &height);
932 win_width = width-7;
933 strtruncate(obj->title, win_width-9);
934 #ifdef HAVE_LIBTEXTWRAP
935 textwrap_init(&tw);
936 textwrap_columns(&tw, win_width - 2 - 2*TEXT_PADDING);
937 wrappedtext = textwrap(&tw, full_description);
938 free(full_description);
939 full_description = wrappedtext;
940 #endif
941 if (full_description != NULL)
942 t_height = cdebconf_newt_get_text_height(full_description, win_width);
943 else
944 t_height = 0;
945 if (t_height + 4 <= height-5)
946 win_height = t_height + 4;
947 else {
948 win_height = height-5;
949 flags |= NEWT_FLAG_SCROLL;
950 t_width_scroll = 2;
951 }
952 t_height = win_height - 4;
953 t_width = cdebconf_newt_get_text_width(full_description);
954 t_width_buttons = 2*BUTTON_PADDING;
955 t_width_buttons += cdebconf_newt_get_text_width(yes_text(obj)) + 3;
956 t_width_buttons += cdebconf_newt_get_text_width(no_text(obj)) + 3;
957 if (obj->methods.can_go_back(obj, q))
958 t_width_buttons += 3;
959 if (obj->methods.can_go_back(obj, q))
960 t_width_buttons += cdebconf_newt_get_text_width(goback_text(obj)) + 3;
961 if (t_width_buttons > t_width)
962 t_width = t_width_buttons;
963 if (win_width > t_width + 2*TEXT_PADDING + t_width_scroll)
964 win_width = t_width + 2*TEXT_PADDING + t_width_scroll;
965 t_width_title = cdebconf_newt_get_text_width(obj->title) + TITLE_PADDING;
966 if (t_width_title > win_width)
967 win_width = t_width_title;
968 cdebconf_newt_create_window(win_width, win_height, obj->title, q->priority);
969 form = cdebconf_newt_create_form(NULL);
970 textbox = newtTextbox(TEXT_PADDING, 1, t_width, t_height, flags);
971 assert(textbox);
972 if (full_description != NULL)
973 newtTextboxSetText(textbox, full_description);
974 free(full_description);
975 if (obj->methods.can_go_back(obj, q)) {
976 bYes = newtCompactButton(win_width - TEXT_PADDING - BUTTON_PADDING - strwidth(yes_text(obj)) - 3 - strwidth(no_text(obj)) - 3 - 3, win_height-2, yes_text(obj));
977 bNo = newtCompactButton(win_width - TEXT_PADDING - BUTTON_PADDING - strwidth(no_text(obj)) - 3, win_height-2, no_text(obj));
978 bCancel = newtCompactButton(TEXT_PADDING + BUTTON_PADDING - 1, win_height-2, goback_text(obj));
979 newtFormAddComponents(form, bCancel, textbox, bYes, bNo, NULL);
980 } else {
981 bYes = newtCompactButton(TEXT_PADDING + BUTTON_PADDING - 1, win_height-2, yes_text(obj));
982 bNo = newtCompactButton(win_width - TEXT_PADDING - BUTTON_PADDING - strwidth(no_text(obj)) - 3, win_height-2, no_text(obj));
983 bCancel = NULL;
984 newtFormAddComponents(form, textbox, bYes, bNo, NULL);
985 }
986 if (question_getvalue(q, "") != NULL && strcmp(question_getvalue(q, ""), "true") == 0)
987 newtFormSetCurrent(form, bYes);
988 else
989 newtFormSetCurrent(form, bNo);
990 cRet = newtRunForm(form);
991 if (cRet == bYes) {
992 ret = DC_OK;
993 question_setvalue(q, "true");
994 } else if (cRet == bNo) {
995 ret = DC_OK;
996 question_setvalue(q, "false");
997 } else if ((cRet == NULL) || (bCancel != NULL && cRet == bCancel))
998 ret = DC_GOBACK;
999 else
1000 ret = DC_NOTOK;
1001 newtFormDestroy(form);
1002 newtPopWindow();
1003 return ret;
1004 }
1005
1006 /*
1007 * Function: newt_handler_multiselect
1008 * Input: struct frontend *obj - frontend object
1009 * struct question *q - question to ask
1010 * Output: int - DC_OK, DC_NOTOK
1011 * Description: handler for the multiselect question type
1012 * Assumptions: none
1013 */
1014 static int
1015 newt_handler_multiselect(struct frontend *obj, struct question *q)
1016 {
1017 int separate_window, ret;
1018
1019 separate_window = need_separate_window(obj, q);
1020 while (1) {
1021 if (separate_window) {
1022 ret = show_separate_window(obj, q, 0);
1023 if (ret != DC_OK)
1024 return ret;
1025 ret = show_multiselect_window(obj, q, 0);
1026 }
1027 else
1028 ret = show_multiselect_window(obj, q, 1);
1029 if (!separate_window || ret != DC_GOBACK)
1030 return ret;
1031 }
1032 }
1033
1034 /*
1035 * Function: newt_handler_select
1036 * Input: struct frontend *obj - frontend object
1037 * struct question *q - question to ask
1038 * Output: int - DC_OK, DC_NOTOK
1039 * Description: handler for the select question type
1040 * Assumptions: none
1041 */
1042 static int
1043 newt_handler_select(struct frontend *obj, struct question *q)
1044 {
1045 int separate_window, ret;
1046
1047 separate_window = need_separate_window(obj, q);
1048 while (1) {
1049 if (separate_window) {
1050 ret = show_separate_window(obj, q, 0);
1051 if (ret != DC_OK)
1052 return ret;
1053 ret = show_select_window(obj, q, 0);
1054 }
1055 else
1056 ret = show_select_window(obj, q, 1);
1057 if (!separate_window || ret != DC_GOBACK)
1058 return ret;
1059 }
1060 }
1061
1062 /*
1063 * Function: newt_handler_string
1064 * Input: struct frontend *obj - frontend object
1065 * struct question *q - question to ask
1066 * Output: int - DC_OK, DC_NOTOK
1067 * Description: handler for the string question type
1068 * Assumptions: none
1069 */
1070 static int
1071 newt_handler_string(struct frontend *obj, struct question *q)
1072 {
1073 return generic_handler_string(obj, q, 0);
1074 }
1075
1076 /*
1077 * Function: newt_handler_password
1078 * Input: struct frontend *obj - frontend object
1079 * struct question *q - question to ask
1080 * Output: int - DC_OK, DC_NOTOK
1081 * Description: handler for the password question type
1082 * Assumptions: none
1083 */
1084 static int newt_handler_password(struct frontend *obj, struct question *q)
1085 {
1086 return generic_handler_string(obj, q, NEWT_FLAG_PASSWORD);
1087 }
1088
1089 /*
1090 * Function: newt_handler_note
1091 * Input: struct frontend *obj - frontend object
1092 * struct question *q - question to ask
1093 * Output: int - DC_OK, DC_NOTOK
1094 * Description: handler for the note question type
1095 * Assumptions: none
1096 */
1097 static int newt_handler_note(struct frontend *obj, struct question *q)
1098 {
1099 // Sort of hack-ish, yes, but it works :-)
1100 return show_separate_window(obj, q, 0);
1101 }
1102
1103 /*
1104 * Function: newt_handler_text
1105 * Input: struct frontend *obj - frontend object
1106 * struct question *q - question to ask
1107 * Output: int - DC_OK, DC_NOTOK
1108 * Description: handler for the text question type
1109 * Assumptions: none
1110 */
1111 static int
1112 newt_handler_text(struct frontend *obj, struct question *q)
1113 {
1114 /* FIXME: Is text really an interactive question type? */
1115 return newt_handler_note(obj, q);
1116 }
1117
1118 static int
1119 newt_handler_error(struct frontend *obj, struct question *q)
1120 {
1121 char *oldrootBg, *oldshadowBg, *oldhelpLineFg, *oldhelpLineBg;
1122 int ret;
1123 struct newtColors palette = newtAltColorPalette;
1124
1125 oldrootBg = palette.rootBg;
1126 oldshadowBg = palette.shadowBg;
1127 oldhelpLineFg = palette.helpLineFg;
1128 oldhelpLineBg = palette.helpLineBg;
1129 if (strcmp(oldrootBg, oldshadowBg) == 0)
1130 palette.shadowBg = "red";
1131 palette.rootBg = "red";
1132 palette.helpLineFg = "white";
1133 palette.helpLineBg = "red";
1134 newtSetColors(palette);
1135 ret = newt_handler_note(obj, q);
1136 palette.rootBg = oldrootBg;
1137 palette.shadowBg = oldshadowBg;
1138 palette.helpLineFg = oldhelpLineFg;
1139 palette.helpLineBg = oldhelpLineBg;
1140 newtSetColors(palette);
1141 return ret;
1142 }
1143
1144 static void
1145 newt_help_callback(newtComponent co, void *d)
1146 {
1147 struct newt_help_callback_data *data = d;
1148 struct frontend *obj = data->obj;
1149 struct question *q = obj->qdb->methods.get(obj->qdb, data->tag);
1150
1151 /* Don't allow recursive calls. */
1152 newtSetHelpCallback(NULL);
1153
1154 show_separate_window(obj, q, 1);
1155
1156 newtSetHelpCallback(newt_help_callback);
1157
1158 question_deref(q);
1159 }
1160
1161 /* ----------------------------------------------------------------------- */
1162 static struct question_handlers {
1163 const char *type;
1164 newt_handler *handler;
1165 } question_handlers[] = {
1166 { "boolean", newt_handler_boolean }, // OK
1167 { "multiselect", newt_handler_multiselect },
1168 { "select", newt_handler_select }, // OK
1169 { "string", newt_handler_string }, // OK
1170 { "password", newt_handler_password }, // OK
1171 { "note", newt_handler_note }, // OK
1172 { "text", newt_handler_text },
1173 { "error", newt_handler_error },
1174 { "", NULL },
1175 };
1176
1177 /*
1178 * Function: newt_initialize
1179 * Input: struct frontend *obj - frontend UI object
1180 * struct configuration *cfg - configuration parameters
1181 * Output: int - DC_OK, DC_NOTOK
1182 * Description: initializes the text UI
1183 * Assumptions: none
1184 *
1185 * TODO: SIGINT is ignored by the newt UI, otherwise it interferes with
1186 * the way question/answers are handled. this is probably not optimal
1187 */
1188 static int
1189 newt_initialize(struct frontend *obj, struct configuration *conf)
1190 {
1191 int width = 80, height = 24;
1192 const char *palette;
1193
1194 obj->interactive = 1;
1195 obj->data = calloc(1, sizeof(struct newt_data));
1196 SLang_init_tty(0, 1, 0); /* disable flow control */
1197 newtInit();
1198 palette = getenv("FRONTEND_BACKGROUND");
1199 if (palette == NULL || strcmp(palette, "dark") != 0)
1200 newtAltColorPalette = newtDefaultColorPalette;
1201 newtSetColors(newtAltColorPalette);
1202 newtGetScreenSize(&width, &height);
1203 newtFinished();
1204 return DC_OK;
1205 }
1206
1207
1208 /*
1209 * Function: newt_go
1210 * Input: struct frontend *obj - frontend object
1211 * Output: int - DC_OK, DC_NOTOK
1212 * Description: asks all pending questions
1213 * Assumptions: none
1214 */
1215 static int
1216 newt_go(struct frontend *obj)
1217 {
1218 struct newt_data *data = (struct newt_data *)obj->data;
1219 struct question *q = obj->questions;
1220 int i, ret = DC_OK, cleared;
1221
1222 cleared = 0;
1223 while (q != NULL) {
1224 for (i = 0; i < DIM(question_handlers); i++) {
1225 newt_handler *handler;
1226 struct plugin *plugin = NULL;
1227
1228 if (*question_handlers[i].type)
1229 handler = question_handlers[i].handler;
1230 else {
1231 plugin = plugin_find(obj, q->template->type);
1232 if (plugin) {
1233 INFO(INFO_DEBUG, "Found plugin for %s", q->template->type);
1234 handler = (newt_handler *) plugin->handler;
1235 } else {
1236 INFO(INFO_DEBUG, "No plugin for %s", q->template->type);
1237 continue;
1238 }
1239 }
1240
1241 if (plugin || strcmp(q->template->type, question_handlers[i].type) == 0) {
1242 char *help = q_get_help(obj, q);
1243 struct newt_help_callback_data my_help_cb_data;
1244 if (!cleared && !data->scale_form) {
1245 cleared = 1;
1246 cdebconf_newt_setup();
1247 }
1248 if (*help) {
1249 my_help_cb_data.obj = obj;
1250 my_help_cb_data.tag = help;
1251 help_cb_data = &my_help_cb_data;
1252 newtSetHelpCallback(newt_help_callback);
1253 }
1254 if (obj->info != NULL) {
1255 char *text = q_get_description(obj, obj->info);
1256 if (text)
1257 newtDrawRootText(0, 0, text);
1258 free(text);
1259 }
1260 if (*help)
1261 newtPushHelpLine(help_text_f1(obj));
1262 else
1263 newtPushHelpLine(help_text(obj));
1264 ret = handler(obj, q);
1265 newtPopHelpLine();
1266 if (ret == DC_OK)
1267 frontend_qdb_set(obj->qdb, q, 0);
1268 if (*help) {
1269 newtSetHelpCallback(NULL);
1270 help_cb_data = NULL;
1271 }
1272 free(help);
1273 if (plugin)
1274 plugin_delete(plugin);
1275 break;
1276 }
1277 }
1278 if (ret == DC_NOTOK)
1279 break;
1280 if (ret == DC_OK)
1281 q = q->next;
1282 else if (ret == DC_GOBACK) {
1283 do {
1284 q = q->prev;
1285 } while (q != NULL && 0 == strcmp("error", q->template->type));
1286 }
1287 }
1288 if (cleared && !data->scale_form)
1289 newtFinished();
1290 return ret;
1291 }
1292
1293 static bool
1294 newt_can_go_back(struct frontend *obj, struct question *q)
1295 {
1296 return (obj->capability & DCF_CAPB_BACKUP);
1297 }
1298
1299 static bool
1300 newt_can_cancel_progress(struct frontend *obj)
1301 {
1302 return (obj->capability & DCF_CAPB_PROGRESSCANCEL);
1303 }
1304
1305 static bool
1306 newt_can_align(struct frontend *obj, struct question *q)
1307 {
1308 return (obj->capability & DCF_CAPB_ALIGN);
1309 }
1310
1311 static const char *
1312 newt_lookup_directive(struct frontend *obj, const char *directive)
1313 {
1314 if (obj->methods.can_align(obj, obj->questions)) {
1315 if (strcmp("TAB", directive) == 0) {
1316 return STRALIGN_TAB;
1317 } else if (strcmp("ALIGN=CENTER", directive) == 0) {
1318 return STRALIGN_ALIGN_CENTER;
1319 } else if (strcmp("ALIGN=RIGHT", directive) == 0) {
1320 return STRALIGN_ALIGN_RIGHT;
1321 }
1322 }
1323 /* Remove unhandled directives */
1324 return "";
1325 }
1326
1327 static void
1328 newt_make_progress_bar(struct frontend *obj, struct question *info)
1329 {
1330 struct newt_data *data = (struct newt_data *)obj->data;
1331 int width = 80, height = 24, win_width, win_height, text_height;
1332 int extra = 0;
1333 bool can_cancel_progress;
1334 char *info_desc = NULL;
1335 bool changed = false;
1336 #ifdef HAVE_LIBTEXTWRAP
1337 textwrap_t tw;
1338 char *wrappedtext;
1339 int flags = 0;
1340 #else
1341 int flags = NEWT_FLAG_WRAP;
1342 #endif
1343
1344 can_cancel_progress = obj->methods.can_cancel_progress(obj);
1345 if (can_cancel_progress)
1346 extra += 2;
1347 newtGetScreenSize(&width, &height);
1348 win_width = width-7;
1349 if (!info)
1350 info = data->scale_info;
1351 else {
1352 data->scale_info = info;
1353 question_ref(data->scale_info);
1354 }
1355 if (info)
1356 info_desc = q_get_raw_description(info);
1357 if (info_desc)
1358 text_height = cdebconf_newt_get_text_height(info_desc, win_width);
1359 else
1360 text_height = 0;
1361 /* Minimal height set to 2 to prevent box flashing */
1362 if (text_height < 2)
1363 text_height = 2;
1364 if (data->scale_form) {
1365 if (text_height != data->scale_textbox_height)
1366 changed = true;
1367 else if (can_cancel_progress && !data->scale_cancel)
1368 changed = true;
1369 else if (!can_cancel_progress && data->scale_cancel)
1370 changed = true;
1371 if (changed) {
1372 newtFormDestroy(data->scale_form);
1373 newtPopWindow();
1374 }
1375 }
1376 if (!data->scale_form || changed) {
1377 char *title_desc;
1378 if (text_height + 3 + extra <= height - 5)
1379 win_height = text_height + 3 + extra;
1380 else
1381 win_height = height - 5;
1382 title_desc = q_get_raw_description(obj->progress_title);
1383 cdebconf_newt_create_window(win_width, win_height, title_desc, NULL);
1384 free(title_desc);
1385 data->scale_bar = newtScale(TEXT_PADDING, 1, win_width-2*TEXT_PADDING, obj->progress_max - obj->progress_min);
1386 data->scale_textbox = newtTextbox(TEXT_PADDING, 3, win_width-2*TEXT_PADDING, text_height, flags);
1387 data->scale_textbox_height = text_height;
1388 data->scale_form = cdebconf_newt_create_form(NULL);
1389 newtFormAddComponents(data->scale_form, data->scale_bar, data->scale_textbox, NULL);
1390 if (obj->methods.can_cancel_progress(obj)) {
1391 data->scale_cancel = newtCompactButton(TEXT_PADDING + BUTTON_PADDING - 1, win_height-2, cancel_text(obj));
1392 newtFormAddComponent(data->scale_form, data->scale_cancel);
1393 } else
1394 data->scale_cancel = NULL;
1395 newtFormSetTimer(data->scale_form, 1);
1396 }
1397 newtScaleSet(data->scale_bar, obj->progress_cur - obj->progress_min);
1398 if (info_desc) {
1399 #ifdef HAVE_LIBTEXTWRAP
1400 textwrap_init(&tw);
1401 textwrap_columns(&tw, win_width - 2 - 2*TEXT_PADDING);
1402 wrappedtext = textwrap(&tw, info_desc);
1403 newtTextboxSetText(data->scale_textbox, wrappedtext);
1404 free(wrappedtext);
1405 #else
1406 newtTextboxSetText(data->scale_textbox, info_desc);
1407 #endif
1408 }
1409 free(info_desc);
1410 }
1411
1412 static void
1413 newt_progress_start(struct frontend *obj, int min, int max,
1414 struct question *title)
1415 {
1416 struct newt_data *data = (struct newt_data *)obj->data;
1417
1418 if (data->scale_form != NULL)
1419 /* Nested progress bars are not currently supported. If you try to
1420 * use one anyway, tear down the old progress bar first.
1421 */
1422 newt_progress_stop(obj);
1423 question_deref(obj->progress_title);
1424 obj->progress_title = title;
1425 question_ref(obj->progress_title);
1426 obj->progress_min = min;
1427 obj->progress_max = max;
1428 obj->progress_cur = min;
1429 data->scale_info = NULL;
1430 cdebconf_newt_setup();
1431 if (obj->info != NULL) {
1432 char *text = q_get_description(obj, obj->info);
1433 if (text)
1434 newtDrawRootText(0, 0, text);
1435 free(text);
1436 }
1437 newt_make_progress_bar(obj, NULL);
1438 newtDrawForm(data->scale_form);
1439 newtRefresh();
1440 }
1441
1442 static int
1443 newt_progress_set(struct frontend *obj, int val)
1444 {
1445 struct newt_data *data = (struct newt_data *)obj->data;
1446 struct newtExitStruct es;
1447 int ret;
1448
1449 if (data->scale_form != NULL)
1450 {
1451 obj->progress_cur = val;
1452 /* if (obj->progress_max - obj->progress_min > 0)
1453 {
1454 char buf[64];
1455 float perc;
1456
1457 perc = 100.0 * (float)(obj->progress_cur - obj->progress_min) /
1458 (float)(obj->progress_max - obj->progress_min);
1459 sprintf(buf, "%3d%%", (int)perc);
1460 newtLabelSetText(data->perc_label, buf);
1461 } */
1462 newt_make_progress_bar(obj, NULL);
1463 newtFormRun(data->scale_form, &es);
1464 if (es.reason == NEWT_EXIT_TIMER || data->scale_cancel == NULL)
1465 ret = DC_OK;
1466 else if (es.reason == NEWT_EXIT_COMPONENT && es.u.co == data->scale_cancel)
1467 ret = DC_GOBACK;
1468 else if (es.reason == NEWT_EXIT_HOTKEY && es.u.key == NEWT_KEY_F12)
1469 ret = DC_GOBACK;
1470 else
1471 ret = DC_OK;
1472 } else
1473 ret = DC_OK;
1474
1475 return ret;
1476 }
1477
1478 static int
1479 newt_progress_info(struct frontend *obj, struct question *info)
1480 {
1481 struct newt_data *data = (struct newt_data *)obj->data;
1482 struct newtExitStruct es;
1483 int ret;
1484
1485 if (data->scale_form != NULL) {
1486 newt_make_progress_bar(obj, info);
1487 newtFormRun(data->scale_form, &es);
1488 if (es.reason == NEWT_EXIT_TIMER || data->scale_cancel == NULL)
1489 ret = DC_OK;
1490 else if (es.reason == NEWT_EXIT_COMPONENT && es.u.co == data->scale_cancel)
1491 ret = DC_GOBACK;
1492 else if (es.reason == NEWT_EXIT_HOTKEY && es.u.key == NEWT_KEY_F12)
1493 ret = DC_GOBACK;
1494 else
1495 ret = DC_OK;
1496 } else
1497 ret = DC_OK;
1498
1499 return ret;
1500 }
1501
1502 static void
1503 newt_progress_stop(struct frontend *obj)
1504 {
1505 struct newt_data *data = (struct newt_data *)obj->data;
1506
1507 if (data->scale_form != NULL) {
1508 newtFormDestroy(data->scale_form);
1509 newtPopWindow();
1510 newtFinished();
1511 data->scale_form = data->scale_bar = data->perc_label = data->scale_textbox = data->scale_cancel = NULL;
1512 question_deref(data->scale_info);
1513 data->scale_info = NULL;
1514 }
1515 }
1516
1517 static int
1518 newt_shutdown(struct frontend *obj)
1519 {
1520 newt_progress_stop(obj);
1521 newtFinished();
1522 return DC_OK;
1523 }
1524
1525 struct frontend_module debconf_frontend_module =
1526 {
1527 .initialize = newt_initialize,
1528 .shutdown = newt_shutdown,
1529 .lookup_directive = newt_lookup_directive,
1530 .go = newt_go,
1531 .can_go_back = newt_can_go_back,
1532 .can_cancel_progress = newt_can_cancel_progress,
1533 .can_align = newt_can_align,
1534 .progress_start = newt_progress_start,
1535 .progress_set = newt_progress_set,
1536 .progress_info = newt_progress_info,
1537 .progress_stop = newt_progress_stop,
1538 };