ui/ncurses: Update keybindings for subsets
[petitboot] / ui / ncurses / nc-widgets.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <linux/input.h> /* This must be included before ncurses.h */
23 #if defined HAVE_NCURSESW_CURSES_H
24 #  include <ncursesw/curses.h>
25 #elif defined HAVE_NCURSESW_H
26 #  include <ncursesw.h>
27 #elif defined HAVE_NCURSES_CURSES_H
28 #  include <ncurses/curses.h>
29 #elif defined HAVE_NCURSES_H
30 #  include <ncurses.h>
31 #elif defined HAVE_CURSES_H
32 #  include <curses.h>
33 #else
34 #  error "Curses header file not found."
35 #endif
36
37 #if defined HAVE_NCURSESW_FORM_H
38 #  include <ncursesw/form.h>
39 #elif defined HAVE_NCURSES_FORM_H
40 #  include <ncurses/form.h>
41 #elif defined HAVE_FORM_H
42 #  include <form.h>
43 #else
44 #  error "Curses form.h not found."
45 #endif
46
47 #include <string.h>
48 #include <ctype.h>
49
50 #include <talloc/talloc.h>
51 #include <types/types.h>
52 #include <log/log.h>
53 #include <util/util.h>
54 #include <i18n/i18n.h>
55 #include <fold/fold.h>
56
57 #include "nc-cui.h"
58 #include "nc-widgets.h"
59
60 #undef move
61
62 #define to_checkbox(w) container_of(w, struct nc_widget_checkbox, widget)
63 #define to_textbox(w) container_of(w, struct nc_widget_textbox, widget)
64 #define to_button(w) container_of(w, struct nc_widget_button, widget)
65 #define to_select(w) container_of(w, struct nc_widget_select, widget)
66 #define to_subset(w) container_of(w, struct nc_widget_subset, widget)
67
68 static const char *checkbox_checked_str = "[*]";
69 static const char *checkbox_unchecked_str = "[ ]";
70
71 static const char *select_selected_str = "(*)";
72 static const char *select_unselected_str = "( )";
73
74 struct nc_widgetset {
75         WINDOW  *mainwin;
76         WINDOW  *subwin;
77         FORM    *form;
78         FIELD   **fields;
79         int     n_fields, n_alloc_fields;
80         void    (*widget_focus)(struct nc_widget *, void *);
81         void    *widget_focus_arg;
82         FIELD   *cur_field;
83
84         /* custom validators */
85         FIELDTYPE *ipv4_multi_type;
86 };
87
88 struct nc_widget {
89         FIELD   *field;
90         bool    (*process_key)(struct nc_widget *, FORM *, int);
91         void    (*set_visible)(struct nc_widget *, bool);
92         void    (*move)(struct nc_widget *, int, int);
93         void    (*field_focus)(struct nc_widget *, FIELD *);
94         int     focussed_attr;
95         int     unfocussed_attr;
96         int     height;
97         int     width;
98         int     focus_y;
99         int     x;
100         int     y;
101 };
102
103 struct nc_widget_label {
104         struct nc_widget        widget;
105         const char              *text;
106 };
107
108 struct nc_widget_checkbox {
109         struct nc_widget        widget;
110         bool                    checked;
111 };
112
113 struct nc_widget_textbox {
114         struct nc_widgetset     *set;
115         struct nc_widget        widget;
116 };
117
118 struct nc_widget_subset {
119         struct nc_widget        widget;
120         int                     *active;
121         int                     n_active;
122         struct subset_option {
123                 char            *str;
124                 int             val;
125                 FIELD           *field;
126         } *options;
127         int                     n_options;
128         int                     top, left, size;
129         struct nc_widgetset     *set;
130         void                    (*on_change)(void *, int);
131         void                    *on_change_arg;
132         void                    (*screen_cb)(void *,
133                                         struct nc_widget_subset *, int);
134 };
135
136 struct nc_widget_select {
137         struct nc_widget        widget;
138         struct select_option {
139                 char            *str;
140                 int             val;
141                 FIELD           *field;
142                 int             lines;
143         } *options;
144         int                     top, left, size;
145         int                     n_options, selected_option;
146         struct nc_widgetset     *set;
147         void                    (*on_change)(void *, int);
148         void                    *on_change_arg;
149 };
150
151 struct nc_widget_button {
152         struct nc_widget        widget;
153         void                    (*click)(void *arg);
154         void                    *arg;
155 };
156
157 static void widgetset_add_field(struct nc_widgetset *set, FIELD *field);
158 static void widgetset_remove_field(struct nc_widgetset *set, FIELD *field);
159
160 static bool key_is_select(int key)
161 {
162         return key == ' ' || key == '\r' || key == '\n' || key == KEY_ENTER;
163 }
164
165 static bool process_key_nop(struct nc_widget *widget __attribute__((unused)),
166                 FORM *form __attribute((unused)),
167                 int key __attribute__((unused)))
168 {
169         return false;
170 }
171
172 static void field_set_visible(FIELD *field, bool visible)
173 {
174         int opts = field_opts(field) & ~O_VISIBLE;
175         if (visible)
176                 opts |= O_VISIBLE;
177         set_field_opts(field, opts);
178 }
179
180 static bool field_visible(FIELD *field)
181 {
182         return (field_opts(field) & O_VISIBLE) == O_VISIBLE;
183 }
184
185 static void field_move(FIELD *field, int y, int x)
186 {
187         move_field(field, y, x);
188 }
189
190 static int label_destructor(void *ptr)
191 {
192         struct nc_widget_label *label = ptr;
193         free_field(label->widget.field);
194         return 0;
195 }
196
197
198 struct nc_widget_label *widget_new_label(struct nc_widgetset *set,
199                 int y, int x, char *str)
200 {
201         struct nc_widget_label *label;
202         FIELD *f;
203         int len;
204
205         len = strlen(str);
206
207         label = talloc_zero(set, struct nc_widget_label);
208         label->widget.height = 1;
209         label->widget.width = len;
210         label->widget.x = x;
211         label->widget.y = y;
212         label->widget.process_key = process_key_nop;
213         label->widget.field = f = new_field(1, len, y, x, 0, 0);
214         label->widget.focussed_attr = A_NORMAL;
215         label->widget.unfocussed_attr = A_NORMAL;
216
217         field_opts_off(f, O_ACTIVE);
218         set_field_buffer(f, 0, str);
219         set_field_userptr(f, &label->widget);
220
221         widgetset_add_field(set, label->widget.field);
222         talloc_set_destructor(label, label_destructor);
223
224         return label;
225 }
226
227 bool widget_checkbox_get_value(struct nc_widget_checkbox *checkbox)
228 {
229         return checkbox->checked;
230 }
231
232 static void checkbox_set_buffer(struct nc_widget_checkbox *checkbox)
233 {
234         const char *str;
235         str = checkbox->checked ? checkbox_checked_str : checkbox_unchecked_str;
236         set_field_buffer(checkbox->widget.field, 0, str);
237 }
238
239 static bool checkbox_process_key(struct nc_widget *widget,
240                 FORM *form __attribute__((unused)), int key)
241 {
242         struct nc_widget_checkbox *checkbox = to_checkbox(widget);
243
244         if (!key_is_select(key))
245                 return false;
246
247         checkbox->checked = !checkbox->checked;
248         checkbox_set_buffer(checkbox);
249
250         return true;
251 }
252
253 static int checkbox_destructor(void *ptr)
254 {
255         struct nc_widget_checkbox *checkbox = ptr;
256         free_field(checkbox->widget.field);
257         return 0;
258 }
259
260 struct nc_widget_checkbox *widget_new_checkbox(struct nc_widgetset *set,
261                 int y, int x, bool checked)
262 {
263         struct nc_widget_checkbox *checkbox;
264         FIELD *f;
265
266         checkbox = talloc_zero(set, struct nc_widget_checkbox);
267         checkbox->checked = checked;
268         checkbox->widget.height = 1;
269         checkbox->widget.width = strlen(checkbox_checked_str);
270         checkbox->widget.x = x;
271         checkbox->widget.y = y;
272         checkbox->widget.process_key = checkbox_process_key;
273         checkbox->widget.focussed_attr = A_REVERSE;
274         checkbox->widget.unfocussed_attr = A_NORMAL;
275         checkbox->widget.field = f = new_field(1, strlen(checkbox_checked_str),
276                         y, x, 0, 0);
277
278         field_opts_off(f, O_EDIT);
279         set_field_userptr(f, &checkbox->widget);
280         checkbox_set_buffer(checkbox);
281
282         widgetset_add_field(set, checkbox->widget.field);
283         talloc_set_destructor(checkbox, checkbox_destructor);
284
285         return checkbox;
286 }
287
288 static char *strip_string(char *str)
289 {
290         int len, i;
291
292         len = strlen(str);
293
294         /* clear trailing space */
295         for (i = len - 1; i >= 0; i--) {
296                 if (!isspace(str[i]))
297                         break;
298                 str[i] = '\0';
299         }
300
301         /* increment str past leading space */
302         for (i = 0; i < len; i++) {
303                 if (str[i] == '\0' || !isspace(str[i]))
304                         break;
305         }
306
307         return str + i;
308 }
309
310 char *widget_textbox_get_value(struct nc_widget_textbox *textbox)
311 {
312         char *str = field_buffer(textbox->widget.field, 0);
313         return str ? strip_string(str) : NULL;
314 }
315
316 static bool textbox_process_key(
317                 struct nc_widget *widget __attribute__((unused)),
318                 FORM *form, int key)
319 {
320         switch (key) {
321         case KEY_HOME:
322                 form_driver(form, REQ_BEG_FIELD);
323                 break;
324         case KEY_END:
325                 form_driver(form, REQ_END_FIELD);
326                 break;
327         case KEY_LEFT:
328                 form_driver(form, REQ_LEFT_CHAR);
329                 break;
330         case KEY_RIGHT:
331                 form_driver(form, REQ_RIGHT_CHAR);
332                 break;
333         case KEY_BACKSPACE:
334                 if (form_driver(form, REQ_LEFT_CHAR) != E_OK)
335                         break;
336                 /* fall through */
337         case KEY_DC:
338                 form_driver(form, REQ_DEL_CHAR);
339                 break;
340         default:
341                 form_driver(form, key);
342                 break;
343         }
344
345         return true;
346 }
347
348 static int textbox_destructor(void *ptr)
349 {
350         struct nc_widget_textbox *textbox = ptr;
351         free_field(textbox->widget.field);
352         return 0;
353 }
354
355 struct nc_widget_textbox *widget_new_textbox(struct nc_widgetset *set,
356                 int y, int x, int len, char *str)
357 {
358         struct nc_widget_textbox *textbox;
359         FIELD *f;
360
361         textbox = talloc_zero(set, struct nc_widget_textbox);
362         textbox->set = set;
363         textbox->widget.height = 1;
364         textbox->widget.width = len;
365         textbox->widget.x = x;
366         textbox->widget.y = y;
367         textbox->widget.process_key = textbox_process_key;
368         textbox->widget.field = f = new_field(1, len, y, x, 0, 0);
369         textbox->widget.focussed_attr = A_REVERSE;
370         textbox->widget.unfocussed_attr = A_UNDERLINE;
371
372         field_opts_off(f, O_STATIC | O_WRAP | O_BLANK);
373         set_field_buffer(f, 0, str);
374         set_field_back(f, textbox->widget.unfocussed_attr);
375         set_field_userptr(f, &textbox->widget);
376
377         widgetset_add_field(set, textbox->widget.field);
378         talloc_set_destructor(textbox, textbox_destructor);
379
380         return textbox;
381 }
382
383 void widget_textbox_set_fixed_size(struct nc_widget_textbox *textbox)
384 {
385         field_opts_on(textbox->widget.field, O_STATIC);
386 }
387
388 void widget_textbox_set_validator_integer(struct nc_widget_textbox *textbox,
389                 long min, long max)
390 {
391         set_field_type(textbox->widget.field, TYPE_INTEGER, 1, min, max);
392 }
393
394 void widget_textbox_set_validator_ipv4(struct nc_widget_textbox *textbox)
395 {
396         set_field_type(textbox->widget.field, TYPE_IPV4);
397 }
398
399 static bool check_ipv4_multi_char(int c,
400                 const void *arg __attribute__((unused)))
401 {
402         return isdigit(c) || c == '.' || c == ' ';
403 }
404
405 static bool check_ipv4_multi_field(FIELD *field,
406                 const void *arg __attribute__((unused)))
407 {
408         char *buf = field_buffer(field, 0);
409         unsigned int ip[4];
410         int n, len;
411
412         while (*buf != '\0') {
413                 n = sscanf(buf, "%u.%u.%u.%u%n",
414                                 &ip[0], &ip[1], &ip[2], &ip[3], &len);
415                 if (n != 4)
416                         return false;
417
418                 if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
419                         return false;
420
421                 for (buf += len; *buf != '\0'; buf++) {
422                         if (isspace(*buf))
423                                 continue;
424                         else if (isdigit(*buf))
425                                 break;
426                         else
427                                 return false;
428                 }
429         }
430
431         return true;
432 }
433
434 void widget_textbox_set_validator_ipv4_multi(struct nc_widget_textbox *textbox)
435 {
436         if (!textbox->set->ipv4_multi_type) {
437                 textbox->set->ipv4_multi_type = new_fieldtype(
438                                 check_ipv4_multi_field,
439                                 check_ipv4_multi_char);
440         }
441         set_field_type(textbox->widget.field, textbox->set->ipv4_multi_type);
442 }
443
444 static void subset_update_order(struct nc_widget_subset *subset)
445 {
446         char *str;
447         int i, val;
448
449         for (i = 0; i < subset->n_active; i++) {
450                 val = subset->active[i];
451                 str = talloc_asprintf(subset, "(%d) %s",
452                                       i, subset->options[val].str);
453                 set_field_buffer(subset->options[val].field, 0,
454                                  str);
455                 talloc_free(str);
456         }
457 }
458
459 static void widget_focus_change(struct nc_widget *widget, FIELD *field,
460                 bool focussed);
461
462 static void subset_delete_active(struct nc_widget_subset *subset, int idx)
463 {
464         bool last = idx == (subset->n_active - 1);
465         struct nc_widgetset *set = subset->set;
466         struct nc_widget *widget;
467         size_t rem;
468         int i, val;
469
470         /* Shift field focus to nearest active option or next visible field */
471         if (subset->n_active > 1) {
472                 if (last)
473                         val = subset->active[idx - 1];
474                 else
475                         val = subset->active[idx + 1];
476                 set->cur_field = subset->options[val].field;
477         } else {
478                 for (i = 0; i < set->n_fields; i++)
479                         if (field_visible(set->fields[i])) {
480                                 set->cur_field = set->fields[i];
481                                 break;
482                         }
483         }
484
485         set_current_field(set->form, set->cur_field);
486         widget = field_userptr(set->cur_field);
487         widget_focus_change(widget, set->cur_field, true);
488         if (set->widget_focus)
489                 set->widget_focus(widget, set->widget_focus_arg);
490
491         /* Update active array */
492         rem = sizeof(int) * (subset->n_active - idx - 1);
493         val = subset->active[idx];
494         field_set_visible(subset->options[val].field, false);
495         if (rem)
496                 memmove(&subset->active[idx], &subset->active[idx + 1], rem);
497         subset->n_active--;
498         subset->active = talloc_realloc(subset, subset->active,
499                                          int, subset->n_active);
500
501         subset->widget.height = subset->n_active;
502 }
503
504 static bool subset_process_key(struct nc_widget *w, FORM *form, int key)
505 {
506         struct nc_widget_subset *subset = to_subset(w);
507         int i, val, opt_idx = -1;
508         FIELD *field;
509
510         if (key != '-' && key != '+' && key != KEY_DC && key != KEY_BACKSPACE)
511                 return false;
512
513         field = current_field(form);
514
515         for (i = 0; i < subset->n_active; i++) {
516                 val = subset->active[i];
517                 if (subset->options[val].field == field) {
518                         opt_idx = i;
519                         break;
520                 }
521         }
522
523         if (opt_idx < 0)
524                 return false;
525
526         if (key == KEY_DC || key == KEY_BACKSPACE)
527                 subset_delete_active(subset, opt_idx);
528
529         if (key == '-') {
530                 if (opt_idx == 0)
531                         return true;
532
533                 val = subset->active[opt_idx];
534                 subset->active[opt_idx] = subset->active[opt_idx - 1];
535                 subset->active[opt_idx - 1] = val;
536         }
537
538         if (key == '+') {
539                 if (opt_idx >= subset->n_active - 1)
540                         return true;
541
542                 val = subset->active[opt_idx];
543                 subset->active[opt_idx] = subset->active[opt_idx + 1];
544                 subset->active[opt_idx + 1] = val;
545         }
546
547         subset_update_order(subset);
548
549         if (subset->on_change)
550                 subset->on_change(subset->on_change_arg, 0);
551
552         return true;
553 }
554
555 static void subset_set_visible(struct nc_widget *widget, bool visible)
556 {
557         struct nc_widget_subset *subset = to_subset(widget);
558         int i, val;
559
560         for (i = 0; i < subset->n_active; i++) {
561                 val = subset->active[i];
562                 field_set_visible(subset->options[val].field, visible);
563         }
564 }
565
566 static void subset_move(struct nc_widget *widget, int y, int x)
567 {
568         struct nc_widget_subset *subset = to_subset(widget);
569         int i, val;
570
571         for (i = 0; i < subset->n_active; i++) {
572                 val = subset->active[i];
573                 field_move(subset->options[val].field, y + i , x);
574         }
575 }
576
577 static void subset_field_focus(struct nc_widget *widget, FIELD *field)
578 {
579         struct nc_widget_subset *subset = to_subset(widget);
580         int i, val;
581
582         for (i = 0; i < subset->n_active; i++) {
583                 val = subset->active[i];
584                 if (field == subset->options[val].field) {
585                         widget->focus_y = i;
586                         return;
587                 }
588         }
589 }
590
591 static int subset_destructor(void *ptr)
592 {
593         struct nc_widget_subset *subset = ptr;
594         int i;
595
596         for (i = 0; i < subset->n_options; i++)
597                 free_field(subset->options[i].field);
598
599         return 0;
600 }
601
602 struct nc_widget_subset *widget_new_subset(struct nc_widgetset *set,
603                 int y, int x, int len, void *screen_cb)
604 {
605         struct nc_widget_subset *subset;
606
607         subset = talloc_zero(set, struct nc_widget_subset);
608         subset->widget.width = len;
609         subset->widget.height = 0;
610         subset->widget.x = x;
611         subset->widget.y = y;
612         subset->widget.process_key = subset_process_key;
613         subset->widget.set_visible = subset_set_visible;
614         subset->widget.move = subset_move;
615         subset->widget.field_focus = subset_field_focus;
616         subset->widget.focussed_attr = A_REVERSE;
617         subset->widget.unfocussed_attr = A_NORMAL;
618         subset->top = y;
619         subset->left = x;
620         subset->size = len;
621         subset->set = set;
622         subset->n_active = subset->n_options = 0;
623         subset->active = NULL;
624         subset->options = NULL;
625         subset->screen_cb = screen_cb;
626
627         talloc_set_destructor(subset, subset_destructor);
628
629         return subset;
630 }
631
632 void widget_subset_add_option(struct nc_widget_subset *subset, const char *text)
633 {
634         FIELD *f;
635         int i;
636
637         i = subset->n_options++;
638         subset->options = talloc_realloc(subset, subset->options,
639                                          struct subset_option, i + 1);
640
641         subset->options[i].str = talloc_strdup(subset->options, text);
642
643         subset->options[i].field = f = new_field(1, subset->size, subset->top + i,
644                                                  subset->left, 0, 0);
645
646         field_opts_off(f, O_WRAP | O_EDIT);
647         set_field_userptr(f, &subset->widget);
648         set_field_buffer(f, 0, subset->options[i].str);
649         field_set_visible(f, false);
650         widgetset_add_field(subset->set, f);
651 }
652
653 void widget_subset_make_active(struct nc_widget_subset *subset, int idx)
654 {
655         int i;
656
657         for (i = 0; i < subset->n_active; i++)
658                 if (subset->active[i] == idx) {
659                         pb_debug("%s: Index %d already active\n", __func__, idx);
660                         return;
661                 }
662
663         i = subset->n_active++;
664         subset->widget.height = subset->n_active;
665         subset->active = talloc_realloc(subset, subset->active,
666                                         int, i + 1);
667         subset->active[i] = idx;
668
669         subset_update_order(subset);
670 }
671
672 int widget_subset_get_order(void *ctx, unsigned int **order,
673                 struct nc_widget_subset *subset)
674 {
675         unsigned int *buf = talloc_array(ctx, unsigned int, subset->n_active);
676         int i;
677
678         for (i = 0; i < subset->n_active; i++)
679                 buf[i] = subset->active[i];
680
681         *order = buf;
682         return i;
683 }
684
685 void widget_subset_show_inactive(struct nc_widget_subset *subset,
686                 struct nc_widget_select *select)
687 {
688         bool active = false, first = true;
689         int i, j;
690
691         for (i = 0; i < subset->n_options; i++) {
692                 active = false;
693                 for (j = 0; j < subset->n_active; j++)
694                         if (subset->active[j] == i)
695                                 active = true;
696
697                 if (active)
698                         continue;
699
700                 widget_select_add_option(select, i,
701                                          subset->options[i].str, first);
702                 if (first)
703                         first = false;
704         }
705 }
706
707 int widget_subset_n_inactive(struct nc_widget_subset *subset)
708 {
709         return subset->n_options - subset->n_active;
710 }
711
712 int widget_subset_height(struct nc_widget_subset *subset)
713 {
714         return subset->n_active;
715 }
716
717 void widget_subset_on_change(struct nc_widget_subset *subset,
718                 void (*on_change)(void *, int), void *arg)
719 {
720         subset->on_change = on_change;
721         subset->on_change_arg = arg;
722 }
723
724 void widget_subset_drop_options(struct nc_widget_subset *subset)
725 {
726         struct nc_widgetset *set = subset->set;
727         int i;
728
729         for (i = 0; i < subset->n_options; i++) {
730                 FIELD *field = subset->options[i].field;
731                 widgetset_remove_field(set, field);
732                 if (field == set->cur_field)
733                         set->cur_field = NULL;
734                 free_field(subset->options[i].field);
735         }
736
737         talloc_free(subset->options);
738         talloc_free(subset->active);
739         subset->options = NULL;
740         subset->active = NULL;
741         subset->n_options = 0;
742         subset->n_active = 0;
743         subset->widget.height = 0;
744         subset->widget.focus_y = 0;
745 }
746
747 void widget_subset_clear_active(struct nc_widget_subset *subset)
748 {
749         int i;
750
751         for (i = 0; i < subset->n_options; i++)
752                 field_set_visible(subset->options[i].field, false);
753
754         talloc_free(subset->active);
755         subset->active = NULL;
756         subset->n_active = 0;
757         subset->widget.height = 0;
758         subset->widget.focus_y = 0;
759 }
760
761 void widget_subset_callback(void *arg,
762                 struct nc_widget_subset *subset, int idx)
763 {
764         subset->screen_cb(arg, subset, idx);
765 }
766
767 static void select_option_change(struct select_option *opt, bool selected)
768 {
769         const char *str;
770
771         str = selected ? select_selected_str : select_unselected_str;
772
773         memcpy(opt->str, str, strlen(str));
774         set_field_buffer(opt->field, 0, opt->str);
775 }
776
777 static bool select_process_key(struct nc_widget *w, FORM *form, int key)
778 {
779         struct nc_widget_select *select = to_select(w);
780         struct select_option *new_opt, *old_opt;
781         int i, new_idx;
782         FIELD *field;
783
784         if (!key_is_select(key))
785                 return false;
786
787         field = current_field(form);
788         new_opt = NULL;
789
790         for (i = 0; i < select->n_options; i++) {
791                 if (select->options[i].field == field) {
792                         new_opt = &select->options[i];
793                         new_idx = i;
794                         break;
795                 }
796         }
797
798         if (!new_opt)
799                 return true;
800
801         if (new_idx == select->selected_option)
802                 return true;
803
804         old_opt = &select->options[select->selected_option];
805
806         select_option_change(old_opt, false);
807         select_option_change(new_opt, true);
808
809         select->selected_option = new_idx;
810
811         if (select->on_change)
812                 select->on_change(select->on_change_arg, new_opt->val);
813
814         return true;
815 }
816
817 static void select_set_visible(struct nc_widget *widget, bool visible)
818 {
819         struct nc_widget_select *select = to_select(widget);
820         int i;
821
822         for (i = 0; i < select->n_options; i++)
823                 field_set_visible(select->options[i].field, visible);
824 }
825
826 static void select_move(struct nc_widget *widget, int y, int x)
827 {
828         struct nc_widget_select *select = to_select(widget);
829         int i, cur = 0;
830
831         for (i = 0; i < select->n_options; i++) {
832                 field_move(select->options[i].field, y + cur, x);
833                 cur += select->options[i].lines;
834         }
835 }
836
837 static void select_field_focus(struct nc_widget *widget, FIELD *field)
838 {
839         struct nc_widget_select *select = to_select(widget);
840         int i, cur = 0;
841
842         for (i = 0; i < select->n_options; i++) {
843                 if (field != select->options[i].field) {
844                         cur += select->options[i].lines;
845                         continue;
846                 }
847                 widget->focus_y = cur;
848                 return;
849         }
850 }
851
852 static int select_destructor(void *ptr)
853 {
854         struct nc_widget_select *select = ptr;
855         int i;
856
857         for (i = 0; i < select->n_options; i++)
858                 free_field(select->options[i].field);
859
860         return 0;
861 }
862
863 struct nc_widget_select *widget_new_select(struct nc_widgetset *set,
864                 int y, int x, int len)
865 {
866         struct nc_widget_select *select;
867
868         select = talloc_zero(set, struct nc_widget_select);
869         select->widget.width = len;
870         select->widget.height = 0;
871         select->widget.x = x;
872         select->widget.y = y;
873         select->widget.process_key = select_process_key;
874         select->widget.set_visible = select_set_visible;
875         select->widget.move = select_move;
876         select->widget.field_focus = select_field_focus;
877         select->widget.focussed_attr = A_REVERSE;
878         select->widget.unfocussed_attr = A_NORMAL;
879         select->top = y;
880         select->left = x;
881         select->size = len;
882         select->set = set;
883
884         talloc_set_destructor(select, select_destructor);
885
886         return select;
887 }
888
889 static int widget_select_fold_cb(void *arg, const char *buf, int len)
890 {
891         struct nc_widget_select *select = arg;
892         char *line, *newstr, *padbuf = NULL;
893         int i, pad;
894
895         if (!len)
896                 return 0;
897
898         line = talloc_strndup(select->options, buf, len);
899
900         i = select->n_options - 1;
901         pad = max(0, select->widget.width - strncols(line));
902
903         if (pad) {
904                 padbuf = talloc_array(select->options, char, pad + 1);
905                 memset(padbuf, ' ', pad);
906                 padbuf[pad] = '\0';
907         }
908
909         if (select->options[i].str)
910                 newstr = talloc_asprintf_append(select->options[i].str,
911                                                          "%s%s", line,
912                                                          pad ? padbuf : "");
913         else
914                 newstr = talloc_asprintf(select->options, "%s%s", line,
915                                                          pad ? padbuf : "");
916
917         select->options[i].str = newstr;
918         select->options[i].lines++;
919
920         talloc_free(padbuf);
921         talloc_free(line);
922         return 0;
923 }
924
925 void widget_select_add_option(struct nc_widget_select *select, int value,
926                 const char *text, bool selected)
927 {
928         const char *str;
929         char *full_text;
930         FIELD *f;
931         int i;
932
933         /* if we never see an option with selected set, we want the first
934          * one to be selected */
935         if (select->n_options == 0)
936                 selected = true;
937         else if (selected)
938                 select_option_change(&select->options[select->selected_option],
939                                         false);
940
941         if (selected) {
942                 select->selected_option = select->n_options;
943                 str = select_selected_str;
944         } else
945                 str = select_unselected_str;
946
947         i = select->n_options++;
948         select->options = talloc_realloc(select, select->options,
949                                 struct select_option, i + 2);
950         select->options[i].val = value;
951         select->options[i].lines = 0;
952         select->options[i].str = NULL;
953
954         full_text = talloc_asprintf(select->options, "%s %s", str, text);
955         fold_text(full_text, select->widget.width,
956                   widget_select_fold_cb, select);
957
958         select->options[i].field = f = new_field(select->options[i].lines,
959                                         select->size,
960                                         select->top + select->widget.height,
961                                         select->left, 0, 0);
962
963         select->widget.height += select->options[i].lines;
964
965         field_opts_off(f, O_EDIT);
966         set_field_userptr(f, &select->widget);
967         set_field_buffer(f, 0, select->options[i].str);
968
969         widgetset_add_field(select->set, f);
970         talloc_free(full_text);
971 }
972
973 int widget_select_get_value(struct nc_widget_select *select)
974 {
975         if (!select->n_options)
976                 return -1;
977         return select->options[select->selected_option].val;
978 }
979
980 int widget_select_height(struct nc_widget_select *select)
981 {
982         return select->widget.height;
983 }
984
985 void widget_select_on_change(struct nc_widget_select *select,
986                 void (*on_change)(void *, int), void *arg)
987 {
988         select->on_change = on_change;
989         select->on_change_arg = arg;
990 }
991
992 void widget_select_drop_options(struct nc_widget_select *select)
993 {
994         struct nc_widgetset *set = select->set;
995         int i;
996
997         for (i = 0; i < select->n_options; i++) {
998                 FIELD *field = select->options[i].field;
999                 widgetset_remove_field(set, field);
1000                 if (field == set->cur_field)
1001                         set->cur_field = NULL;
1002                 free_field(select->options[i].field);
1003         }
1004
1005         talloc_free(select->options);
1006         select->options = NULL;
1007         select->n_options = 0;
1008         select->widget.height = 0;
1009         select->widget.focus_y = 0;
1010
1011 }
1012
1013 static bool button_process_key(struct nc_widget *widget,
1014                 FORM *form __attribute__((unused)), int key)
1015 {
1016         struct nc_widget_button *button = to_button(widget);
1017
1018         if (!button->click)
1019                 return false;
1020
1021         if (!key_is_select(key))
1022                 return false;
1023
1024         button->click(button->arg);
1025         return true;
1026 }
1027
1028 static int button_destructor(void *ptr)
1029 {
1030         struct nc_widget_button *button = ptr;
1031         free_field(button->widget.field);
1032         return 0;
1033 }
1034
1035 struct nc_widget_button *widget_new_button(struct nc_widgetset *set,
1036                 int y, int x, int size, const char *str,
1037                 void (*click)(void *), void *arg)
1038 {
1039         struct nc_widget_button *button;
1040         int idx, len, pad1, pad2, bufsz;
1041         char *text;
1042         FIELD *f;
1043
1044         int field_size = size + 2;
1045
1046         button = talloc_zero(set, struct nc_widget_button);
1047         button->widget.height = 1;
1048         button->widget.width = field_size;
1049         button->widget.x = x;
1050         button->widget.y = y;
1051         button->widget.field = f = new_field(1, field_size, y, x, 0, 0);
1052         button->widget.process_key = button_process_key;
1053         button->widget.focussed_attr = A_REVERSE;
1054         button->widget.unfocussed_attr = A_NORMAL;
1055         button->click = click;
1056         button->arg = arg;
1057
1058         field_opts_off(f, O_EDIT);
1059         set_field_userptr(f, &button->widget);
1060
1061         /* Center str in the field. This depends on the number of columns used
1062          * by the string, not the number of chars in str */
1063         len = strncols(str);
1064         if (len <= size) {
1065                 idx = (field_size - len) / 2;
1066         } else {
1067                 idx = 1;
1068                 pb_log("Warning: '%s' %d columns wide "
1069                        "but button is %d columns wide\n",
1070                        str, len, size);
1071         }
1072
1073         pad1 = max(idx - 1, 0);
1074         pad2 = max(size - len - pad1, 0);
1075         bufsz = 1 + pad1 + strlen(str) + pad2 + 2;
1076
1077         text = talloc_array(button, char, bufsz);
1078         memset(text, ' ', bufsz);
1079         memcpy(text + idx, str, strlen(str));
1080         text[0] = '[';
1081         text[bufsz - 2] = ']';
1082         text[bufsz - 1] = '\0';
1083
1084         set_field_buffer(f, 0, text);
1085
1086         widgetset_add_field(set, button->widget.field);
1087         talloc_set_destructor(button, button_destructor);
1088
1089         return button;
1090 }
1091
1092 void widget_focus_change(struct nc_widget *widget, FIELD *field,
1093                 bool focussed)
1094 {
1095         int attr = focussed ? widget->focussed_attr : widget->unfocussed_attr;
1096         set_field_back(field, attr);
1097 }
1098
1099 bool widgetset_process_key(struct nc_widgetset *set, int key)
1100 {
1101         struct nc_widget *widget;
1102         FIELD *field, *tmp;
1103         int req = 0;
1104         bool tab;
1105
1106         field = current_field(set->form);
1107         assert(field);
1108
1109         tab = false;
1110
1111         /* handle field change events */
1112         switch (key) {
1113         case KEY_BTAB:
1114                 tab = true;
1115                 /* fall through */
1116         case KEY_UP:
1117         case KEY_LEFT:
1118                 req = REQ_SPREV_FIELD;
1119                 break;
1120         case '\t':
1121                 tab = true;
1122                 /* fall through */
1123         case KEY_DOWN:
1124         case KEY_RIGHT:
1125                 req = REQ_SNEXT_FIELD;
1126                 break;
1127         case KEY_PPAGE:
1128                 req = REQ_SFIRST_FIELD;
1129                 break;
1130         case KEY_NPAGE:
1131                 req = REQ_SLAST_FIELD;
1132                 break;
1133         }
1134
1135         widget = field_userptr(field);
1136         if (req) {
1137                 widget_focus_change(widget, field, false);
1138                 form_driver(set->form, req);
1139
1140                 /* if we're doing a tabbed-field-change, skip until we
1141                  * see the next widget */
1142                 tmp = field;
1143                 field = current_field(set->form);
1144
1145                 for (; tab && tmp != field && field_userptr(field) == widget;) {
1146                         form_driver(set->form, req);
1147                         field = current_field(set->form);
1148                 }
1149
1150                 form_driver(set->form, REQ_END_FIELD);
1151                 widget = field_userptr(field);
1152                 widget_focus_change(widget, field, true);
1153                 if (widget->field_focus)
1154                         widget->field_focus(widget, field);
1155                 if (set->widget_focus)
1156                         set->widget_focus(widget, set->widget_focus_arg);
1157                 return true;
1158         }
1159
1160         if (!widget->process_key)
1161                 return false;
1162
1163         return widget->process_key(widget, set->form, key);
1164 }
1165
1166 static int widgetset_destructor(void *ptr)
1167 {
1168         struct nc_widgetset *set = ptr;
1169         free_form(set->form);
1170         if (set->ipv4_multi_type)
1171                 free_fieldtype(set->ipv4_multi_type);
1172         return 0;
1173 }
1174
1175 struct nc_widgetset *widgetset_create(void *ctx, WINDOW *main, WINDOW *sub)
1176 {
1177         struct nc_widgetset *set;
1178
1179         set = talloc_zero(ctx, struct nc_widgetset);
1180         set->n_alloc_fields = 8;
1181         set->mainwin = main;
1182         set->subwin = sub;
1183         set->fields = talloc_array(set, FIELD *, set->n_alloc_fields);
1184         talloc_set_destructor(set, widgetset_destructor);
1185
1186         return set;
1187 }
1188
1189 void widgetset_set_windows(struct nc_widgetset *set,
1190                 WINDOW *main, WINDOW *sub)
1191 {
1192         set->mainwin = main;
1193         set->subwin = sub;
1194 }
1195
1196 void widgetset_set_widget_focus(struct nc_widgetset *set,
1197                 widget_focus_cb cb, void *arg)
1198 {
1199         set->widget_focus = cb;
1200         set->widget_focus_arg = arg;
1201 }
1202
1203 void widgetset_post(struct nc_widgetset *set)
1204 {
1205         struct nc_widget *widget;
1206         FIELD *field;
1207
1208         set->form = new_form(set->fields);
1209         set_form_win(set->form, set->mainwin);
1210         set_form_sub(set->form, set->subwin);
1211         post_form(set->form);
1212         form_driver(set->form, REQ_END_FIELD);
1213
1214         if (set->cur_field)
1215                 set_current_field(set->form, set->cur_field);
1216
1217         field = current_field(set->form);
1218         widget = field_userptr(field);
1219         widget_focus_change(widget, field, true);
1220         if (set->widget_focus)
1221                 set->widget_focus(widget, set->widget_focus_arg);
1222 }
1223
1224 void widgetset_unpost(struct nc_widgetset *set)
1225 {
1226         set->cur_field = current_field(set->form);
1227         unpost_form(set->form);
1228         free_form(set->form);
1229         set->form = NULL;
1230 }
1231
1232 static void widgetset_add_field(struct nc_widgetset *set, FIELD *field)
1233 {
1234         if (set->n_fields == set->n_alloc_fields - 1) {
1235                 set->n_alloc_fields *= 2;
1236                 set->fields = talloc_realloc(set, set->fields,
1237                                 FIELD *, set->n_alloc_fields);
1238         }
1239
1240         set->n_fields++;
1241         set->fields[set->n_fields - 1] = field;
1242         set->fields[set->n_fields] = NULL;
1243 }
1244
1245 static void widgetset_remove_field(struct nc_widgetset *set, FIELD *field)
1246 {
1247         int i;
1248
1249         for (i = 0; i < set->n_fields; i++) {
1250                 if (set->fields[i] == field)
1251                         break;
1252         }
1253
1254         if (i == set->n_fields)
1255                 return;
1256
1257         memmove(&set->fields[i], &set->fields[i+i],
1258                         (set->n_fields - i) * sizeof(set->fields[i]));
1259         set->n_fields--;
1260 }
1261
1262 #define DECLARE_BASEFN(type) \
1263         struct nc_widget *widget_ ## type ## _base              \
1264                 (struct nc_widget_ ## type *w)                  \
1265         { return &w->widget; }
1266
1267 DECLARE_BASEFN(textbox);
1268 DECLARE_BASEFN(checkbox);
1269 DECLARE_BASEFN(subset);
1270 DECLARE_BASEFN(select);
1271 DECLARE_BASEFN(label);
1272 DECLARE_BASEFN(button);
1273
1274 void widget_set_visible(struct nc_widget *widget, bool visible)
1275 {
1276         if (widget->set_visible)
1277                 widget->set_visible(widget, visible);
1278         else
1279                 field_set_visible(widget->field, visible);
1280 }
1281
1282 void widget_move(struct nc_widget *widget, int y, int x)
1283 {
1284         if (widget->move)
1285                 widget->move(widget, y, x);
1286         else
1287                 field_move(widget->field, y, x);
1288
1289         widget->x = x;
1290         widget->y = y;
1291
1292         if (x + widget->width > COLS)
1293                 pb_debug("%s: Widget at %d,%d runs over pad! (%d)", __func__,
1294                        y, x, x + widget->width);
1295 }
1296
1297 int widget_height(struct nc_widget *widget)
1298 {
1299         return widget->height;
1300 }
1301
1302 int widget_width(struct nc_widget *widget)
1303 {
1304         return widget->width;
1305 }
1306
1307 int widget_x(struct nc_widget *widget)
1308 {
1309         return widget->x;
1310 }
1311
1312 int widget_y(struct nc_widget *widget)
1313 {
1314         return widget->y;
1315 }
1316
1317 int widget_focus_y(struct nc_widget *widget)
1318 {
1319         return widget->focus_y;
1320 }
1321