2 * Copyright (C) 2013 IBM Corporation
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.
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.
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
18 #if defined(HAVE_CONFIG_H)
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
31 #elif defined HAVE_CURSES_H
34 # error "Curses header file not found."
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
44 # error "Curses form.h not found."
50 #include <talloc/talloc.h>
51 #include <types/types.h>
53 #include <util/util.h>
54 #include <i18n/i18n.h>
55 #include <fold/fold.h>
58 #include "nc-widgets.h"
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)
68 static const char *checkbox_checked_str = "[*]";
69 static const char *checkbox_unchecked_str = "[ ]";
71 static const char *select_selected_str = "(*)";
72 static const char *select_unselected_str = "( )";
79 int n_fields, n_alloc_fields;
80 void (*widget_focus)(struct nc_widget *, void *);
81 void *widget_focus_arg;
84 /* custom validators */
85 FIELDTYPE *ipv4_multi_type;
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 *);
103 struct nc_widget_label {
104 struct nc_widget widget;
108 struct nc_widget_checkbox {
109 struct nc_widget widget;
113 struct nc_widget_textbox {
114 struct nc_widgetset *set;
115 struct nc_widget widget;
118 struct nc_widget_subset {
119 struct nc_widget widget;
122 struct subset_option {
129 struct nc_widgetset *set;
130 void (*on_change)(void *, int);
132 void (*screen_cb)(void *,
133 struct nc_widget_subset *, int);
136 struct nc_widget_select {
137 struct nc_widget widget;
138 struct select_option {
145 int n_options, selected_option;
146 struct nc_widgetset *set;
147 void (*on_change)(void *, int);
151 struct nc_widget_button {
152 struct nc_widget widget;
153 void (*click)(void *arg);
157 static void widgetset_add_field(struct nc_widgetset *set, FIELD *field);
158 static void widgetset_remove_field(struct nc_widgetset *set, FIELD *field);
160 static bool key_is_select(int key)
162 return key == ' ' || key == '\r' || key == '\n' || key == KEY_ENTER;
165 static bool process_key_nop(struct nc_widget *widget __attribute__((unused)),
166 FORM *form __attribute((unused)),
167 int key __attribute__((unused)))
172 static void field_set_visible(FIELD *field, bool visible)
174 int opts = field_opts(field) & ~O_VISIBLE;
177 set_field_opts(field, opts);
180 static bool field_visible(FIELD *field)
182 return (field_opts(field) & O_VISIBLE) == O_VISIBLE;
185 static void field_move(FIELD *field, int y, int x)
187 move_field(field, y, x);
190 static int label_destructor(void *ptr)
192 struct nc_widget_label *label = ptr;
193 free_field(label->widget.field);
198 struct nc_widget_label *widget_new_label(struct nc_widgetset *set,
199 int y, int x, char *str)
201 struct nc_widget_label *label;
207 label = talloc_zero(set, struct nc_widget_label);
208 label->widget.height = 1;
209 label->widget.width = len;
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;
217 field_opts_off(f, O_ACTIVE);
218 set_field_buffer(f, 0, str);
219 set_field_userptr(f, &label->widget);
221 widgetset_add_field(set, label->widget.field);
222 talloc_set_destructor(label, label_destructor);
227 bool widget_checkbox_get_value(struct nc_widget_checkbox *checkbox)
229 return checkbox->checked;
232 static void checkbox_set_buffer(struct nc_widget_checkbox *checkbox)
235 str = checkbox->checked ? checkbox_checked_str : checkbox_unchecked_str;
236 set_field_buffer(checkbox->widget.field, 0, str);
239 static bool checkbox_process_key(struct nc_widget *widget,
240 FORM *form __attribute__((unused)), int key)
242 struct nc_widget_checkbox *checkbox = to_checkbox(widget);
244 if (!key_is_select(key))
247 checkbox->checked = !checkbox->checked;
248 checkbox_set_buffer(checkbox);
253 static int checkbox_destructor(void *ptr)
255 struct nc_widget_checkbox *checkbox = ptr;
256 free_field(checkbox->widget.field);
260 struct nc_widget_checkbox *widget_new_checkbox(struct nc_widgetset *set,
261 int y, int x, bool checked)
263 struct nc_widget_checkbox *checkbox;
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),
278 field_opts_off(f, O_EDIT);
279 set_field_userptr(f, &checkbox->widget);
280 checkbox_set_buffer(checkbox);
282 widgetset_add_field(set, checkbox->widget.field);
283 talloc_set_destructor(checkbox, checkbox_destructor);
288 static char *strip_string(char *str)
294 /* clear trailing space */
295 for (i = len - 1; i >= 0; i--) {
296 if (!isspace(str[i]))
301 /* increment str past leading space */
302 for (i = 0; i < len; i++) {
303 if (str[i] == '\0' || !isspace(str[i]))
310 char *widget_textbox_get_value(struct nc_widget_textbox *textbox)
312 char *str = field_buffer(textbox->widget.field, 0);
313 return str ? strip_string(str) : NULL;
316 static bool textbox_process_key(
317 struct nc_widget *widget __attribute__((unused)),
322 form_driver(form, REQ_BEG_FIELD);
325 form_driver(form, REQ_END_FIELD);
328 form_driver(form, REQ_LEFT_CHAR);
331 form_driver(form, REQ_RIGHT_CHAR);
334 if (form_driver(form, REQ_LEFT_CHAR) != E_OK)
338 form_driver(form, REQ_DEL_CHAR);
346 /* Don't catch navigational keys */
349 form_driver(form, key);
356 static int textbox_destructor(void *ptr)
358 struct nc_widget_textbox *textbox = ptr;
359 free_field(textbox->widget.field);
363 struct nc_widget_textbox *widget_new_textbox(struct nc_widgetset *set,
364 int y, int x, int len, char *str)
366 struct nc_widget_textbox *textbox;
369 textbox = talloc_zero(set, struct nc_widget_textbox);
371 textbox->widget.height = 1;
372 textbox->widget.width = len;
373 textbox->widget.x = x;
374 textbox->widget.y = y;
375 textbox->widget.process_key = textbox_process_key;
376 textbox->widget.field = f = new_field(1, len, y, x, 0, 0);
377 textbox->widget.focussed_attr = A_REVERSE;
378 textbox->widget.unfocussed_attr = A_UNDERLINE;
380 field_opts_off(f, O_STATIC | O_WRAP | O_BLANK);
381 set_field_buffer(f, 0, str);
382 set_field_back(f, textbox->widget.unfocussed_attr);
383 set_field_userptr(f, &textbox->widget);
385 widgetset_add_field(set, textbox->widget.field);
386 talloc_set_destructor(textbox, textbox_destructor);
391 void widget_textbox_set_fixed_size(struct nc_widget_textbox *textbox)
393 field_opts_on(textbox->widget.field, O_STATIC);
396 void widget_textbox_set_validator_integer(struct nc_widget_textbox *textbox,
399 set_field_type(textbox->widget.field, TYPE_INTEGER, 1, min, max);
402 void widget_textbox_set_validator_ipv4(struct nc_widget_textbox *textbox)
404 set_field_type(textbox->widget.field, TYPE_IPV4);
407 static bool check_ipv4_multi_char(int c,
408 const void *arg __attribute__((unused)))
410 return isdigit(c) || c == '.' || c == ' ';
413 static bool check_ipv4_multi_field(FIELD *field,
414 const void *arg __attribute__((unused)))
416 char *buf = field_buffer(field, 0);
420 while (*buf != '\0') {
421 n = sscanf(buf, "%u.%u.%u.%u%n",
422 &ip[0], &ip[1], &ip[2], &ip[3], &len);
426 if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
429 for (buf += len; *buf != '\0'; buf++) {
432 else if (isdigit(*buf))
442 void widget_textbox_set_validator_ipv4_multi(struct nc_widget_textbox *textbox)
444 if (!textbox->set->ipv4_multi_type) {
445 textbox->set->ipv4_multi_type = new_fieldtype(
446 check_ipv4_multi_field,
447 check_ipv4_multi_char);
449 set_field_type(textbox->widget.field, textbox->set->ipv4_multi_type);
452 static void subset_update_order(struct nc_widget_subset *subset)
457 for (i = 0; i < subset->n_active; i++) {
458 val = subset->active[i];
459 str = talloc_asprintf(subset, "(%d) %s",
460 i, subset->options[val].str);
461 set_field_buffer(subset->options[val].field, 0,
467 static void widget_focus_change(struct nc_widget *widget, FIELD *field,
470 static void subset_delete_active(struct nc_widget_subset *subset, int idx)
472 bool last = idx == (subset->n_active - 1);
473 struct nc_widgetset *set = subset->set;
474 struct nc_widget *widget;
479 /* Shift field focus to nearest active option or next visible field */
480 if (subset->n_active > 1) {
482 val = subset->active[idx - 1];
484 val = subset->active[idx + 1];
485 set->cur_field = subset->options[val].field;
487 for (i = 0; i < set->n_fields; i++)
488 if (field_visible(set->fields[i])) {
489 opts = field_opts(set->fields[i]);
490 if ((opts & O_ACTIVE) == O_ACTIVE) {
491 set->cur_field = set->fields[i];
497 set_current_field(set->form, set->cur_field);
498 widget = field_userptr(set->cur_field);
499 widget_focus_change(widget, set->cur_field, true);
500 if (set->widget_focus)
501 set->widget_focus(widget, set->widget_focus_arg);
503 /* Update active array */
504 rem = sizeof(int) * (subset->n_active - idx - 1);
505 val = subset->active[idx];
506 field_set_visible(subset->options[val].field, false);
508 memmove(&subset->active[idx], &subset->active[idx + 1], rem);
510 subset->active = talloc_realloc(subset, subset->active,
511 int, subset->n_active);
513 subset->widget.height = subset->n_active;
516 static bool subset_process_key(struct nc_widget *w, FORM *form, int key)
518 struct nc_widget_subset *subset = to_subset(w);
519 int i, val, opt_idx = -1;
522 if (key != '-' && key != '+' && key != KEY_DC && key != KEY_BACKSPACE)
525 field = current_field(form);
527 for (i = 0; i < subset->n_active; i++) {
528 val = subset->active[i];
529 if (subset->options[val].field == field) {
538 if (key == KEY_DC || key == KEY_BACKSPACE)
539 subset_delete_active(subset, opt_idx);
545 val = subset->active[opt_idx];
546 subset->active[opt_idx] = subset->active[opt_idx - 1];
547 subset->active[opt_idx - 1] = val;
551 if (opt_idx >= subset->n_active - 1)
554 val = subset->active[opt_idx];
555 subset->active[opt_idx] = subset->active[opt_idx + 1];
556 subset->active[opt_idx + 1] = val;
559 subset_update_order(subset);
561 if (subset->on_change)
562 subset->on_change(subset->on_change_arg, 0);
567 static void subset_set_visible(struct nc_widget *widget, bool visible)
569 struct nc_widget_subset *subset = to_subset(widget);
572 for (i = 0; i < subset->n_active; i++) {
573 val = subset->active[i];
574 field_set_visible(subset->options[val].field, visible);
578 static void subset_move(struct nc_widget *widget, int y, int x)
580 struct nc_widget_subset *subset = to_subset(widget);
583 for (i = 0; i < subset->n_active; i++) {
584 val = subset->active[i];
585 field_move(subset->options[val].field, y + i , x);
589 static void subset_field_focus(struct nc_widget *widget, FIELD *field)
591 struct nc_widget_subset *subset = to_subset(widget);
594 for (i = 0; i < subset->n_active; i++) {
595 val = subset->active[i];
596 if (field == subset->options[val].field) {
603 static int subset_destructor(void *ptr)
605 struct nc_widget_subset *subset = ptr;
608 for (i = 0; i < subset->n_options; i++)
609 free_field(subset->options[i].field);
614 struct nc_widget_subset *widget_new_subset(struct nc_widgetset *set,
615 int y, int x, int len, void *screen_cb)
617 struct nc_widget_subset *subset;
619 subset = talloc_zero(set, struct nc_widget_subset);
620 subset->widget.width = len;
621 subset->widget.height = 0;
622 subset->widget.x = x;
623 subset->widget.y = y;
624 subset->widget.process_key = subset_process_key;
625 subset->widget.set_visible = subset_set_visible;
626 subset->widget.move = subset_move;
627 subset->widget.field_focus = subset_field_focus;
628 subset->widget.focussed_attr = A_REVERSE;
629 subset->widget.unfocussed_attr = A_NORMAL;
634 subset->n_active = subset->n_options = 0;
635 subset->active = NULL;
636 subset->options = NULL;
637 subset->screen_cb = screen_cb;
639 talloc_set_destructor(subset, subset_destructor);
644 void widget_subset_add_option(struct nc_widget_subset *subset, const char *text)
649 i = subset->n_options++;
650 subset->options = talloc_realloc(subset, subset->options,
651 struct subset_option, i + 1);
653 subset->options[i].str = talloc_strdup(subset->options, text);
655 subset->options[i].field = f = new_field(1, subset->size, subset->top + i,
658 field_opts_off(f, O_WRAP | O_EDIT);
659 set_field_userptr(f, &subset->widget);
660 set_field_buffer(f, 0, subset->options[i].str);
661 field_set_visible(f, false);
662 widgetset_add_field(subset->set, f);
665 void widget_subset_make_active(struct nc_widget_subset *subset, int idx)
669 for (i = 0; i < subset->n_active; i++)
670 if (subset->active[i] == idx) {
671 pb_debug("%s: Index %d already active\n", __func__, idx);
675 i = subset->n_active++;
676 subset->widget.height = subset->n_active;
677 subset->active = talloc_realloc(subset, subset->active,
679 subset->active[i] = idx;
681 subset_update_order(subset);
684 int widget_subset_get_order(void *ctx, unsigned int **order,
685 struct nc_widget_subset *subset)
687 unsigned int *buf = talloc_array(ctx, unsigned int, subset->n_active);
690 for (i = 0; i < subset->n_active; i++)
691 buf[i] = subset->active[i];
697 void widget_subset_show_inactive(struct nc_widget_subset *subset,
698 struct nc_widget_select *select)
700 bool active = false, first = true;
703 for (i = 0; i < subset->n_options; i++) {
705 for (j = 0; j < subset->n_active; j++)
706 if (subset->active[j] == i)
712 widget_select_add_option(select, i,
713 subset->options[i].str, first);
719 int widget_subset_n_inactive(struct nc_widget_subset *subset)
721 return subset->n_options - subset->n_active;
724 int widget_subset_height(struct nc_widget_subset *subset)
726 return subset->n_active;
729 void widget_subset_on_change(struct nc_widget_subset *subset,
730 void (*on_change)(void *, int), void *arg)
732 subset->on_change = on_change;
733 subset->on_change_arg = arg;
736 void widget_subset_drop_options(struct nc_widget_subset *subset)
738 struct nc_widgetset *set = subset->set;
741 for (i = 0; i < subset->n_options; i++) {
742 FIELD *field = subset->options[i].field;
743 widgetset_remove_field(set, field);
744 if (field == set->cur_field)
745 set->cur_field = NULL;
746 free_field(subset->options[i].field);
749 talloc_free(subset->options);
750 talloc_free(subset->active);
751 subset->options = NULL;
752 subset->active = NULL;
753 subset->n_options = 0;
754 subset->n_active = 0;
755 subset->widget.height = 0;
756 subset->widget.focus_y = 0;
759 void widget_subset_clear_active(struct nc_widget_subset *subset)
763 for (i = 0; i < subset->n_options; i++)
764 field_set_visible(subset->options[i].field, false);
766 talloc_free(subset->active);
767 subset->active = NULL;
768 subset->n_active = 0;
769 subset->widget.height = 0;
770 subset->widget.focus_y = 0;
773 void widget_subset_callback(void *arg,
774 struct nc_widget_subset *subset, int idx)
776 subset->screen_cb(arg, subset, idx);
779 static void select_option_change(struct select_option *opt, bool selected)
783 str = selected ? select_selected_str : select_unselected_str;
785 memcpy(opt->str, str, strlen(str));
786 set_field_buffer(opt->field, 0, opt->str);
789 static bool select_process_key(struct nc_widget *w, FORM *form, int key)
791 struct nc_widget_select *select = to_select(w);
792 struct select_option *new_opt, *old_opt;
796 if (!key_is_select(key))
799 field = current_field(form);
802 for (i = 0; i < select->n_options; i++) {
803 if (select->options[i].field == field) {
804 new_opt = &select->options[i];
813 if (new_idx == select->selected_option)
816 old_opt = &select->options[select->selected_option];
818 select_option_change(old_opt, false);
819 select_option_change(new_opt, true);
821 select->selected_option = new_idx;
823 if (select->on_change)
824 select->on_change(select->on_change_arg, new_opt->val);
829 static void select_set_visible(struct nc_widget *widget, bool visible)
831 struct nc_widget_select *select = to_select(widget);
834 for (i = 0; i < select->n_options; i++)
835 field_set_visible(select->options[i].field, visible);
838 static void select_move(struct nc_widget *widget, int y, int x)
840 struct nc_widget_select *select = to_select(widget);
843 for (i = 0; i < select->n_options; i++) {
844 field_move(select->options[i].field, y + cur, x);
845 cur += select->options[i].lines;
849 static void select_field_focus(struct nc_widget *widget, FIELD *field)
851 struct nc_widget_select *select = to_select(widget);
854 for (i = 0; i < select->n_options; i++) {
855 if (field != select->options[i].field) {
856 cur += select->options[i].lines;
859 widget->focus_y = cur;
864 static int select_destructor(void *ptr)
866 struct nc_widget_select *select = ptr;
869 for (i = 0; i < select->n_options; i++)
870 free_field(select->options[i].field);
875 struct nc_widget_select *widget_new_select(struct nc_widgetset *set,
876 int y, int x, int len)
878 struct nc_widget_select *select;
880 select = talloc_zero(set, struct nc_widget_select);
881 select->widget.width = len;
882 select->widget.height = 0;
883 select->widget.x = x;
884 select->widget.y = y;
885 select->widget.process_key = select_process_key;
886 select->widget.set_visible = select_set_visible;
887 select->widget.move = select_move;
888 select->widget.field_focus = select_field_focus;
889 select->widget.focussed_attr = A_REVERSE;
890 select->widget.unfocussed_attr = A_NORMAL;
896 talloc_set_destructor(select, select_destructor);
901 static int widget_select_fold_cb(void *arg, const char *buf, int len)
903 struct nc_widget_select *select = arg;
904 char *line, *newstr, *padbuf = NULL;
910 line = talloc_strndup(select->options, buf, len);
912 i = select->n_options - 1;
913 pad = max(0, select->widget.width - strncols(line));
916 padbuf = talloc_array(select->options, char, pad + 1);
917 memset(padbuf, ' ', pad);
921 if (select->options[i].str)
922 newstr = talloc_asprintf_append(select->options[i].str,
926 newstr = talloc_asprintf(select->options, "%s%s", line,
929 select->options[i].str = newstr;
930 select->options[i].lines++;
937 void widget_select_add_option(struct nc_widget_select *select, int value,
938 const char *text, bool selected)
945 /* if we never see an option with selected set, we want the first
946 * one to be selected */
947 if (select->n_options == 0)
950 select_option_change(&select->options[select->selected_option],
954 select->selected_option = select->n_options;
955 str = select_selected_str;
957 str = select_unselected_str;
959 i = select->n_options++;
960 select->options = talloc_realloc(select, select->options,
961 struct select_option, i + 2);
962 select->options[i].val = value;
963 select->options[i].lines = 0;
964 select->options[i].str = NULL;
966 full_text = talloc_asprintf(select->options, "%s %s", str, text);
967 fold_text(full_text, select->widget.width,
968 widget_select_fold_cb, select);
970 select->options[i].field = f = new_field(select->options[i].lines,
972 select->top + select->widget.height,
975 select->widget.height += select->options[i].lines;
977 field_opts_off(f, O_EDIT);
978 set_field_userptr(f, &select->widget);
979 set_field_buffer(f, 0, select->options[i].str);
981 widgetset_add_field(select->set, f);
982 talloc_free(full_text);
985 int widget_select_get_value(struct nc_widget_select *select)
987 if (!select->n_options)
989 return select->options[select->selected_option].val;
992 int widget_select_height(struct nc_widget_select *select)
994 return select->widget.height;
997 void widget_select_on_change(struct nc_widget_select *select,
998 void (*on_change)(void *, int), void *arg)
1000 select->on_change = on_change;
1001 select->on_change_arg = arg;
1004 void widget_select_drop_options(struct nc_widget_select *select)
1006 struct nc_widgetset *set = select->set;
1009 for (i = 0; i < select->n_options; i++) {
1010 FIELD *field = select->options[i].field;
1011 widgetset_remove_field(set, field);
1012 if (field == set->cur_field)
1013 set->cur_field = NULL;
1014 free_field(select->options[i].field);
1017 talloc_free(select->options);
1018 select->options = NULL;
1019 select->n_options = 0;
1020 select->widget.height = 0;
1021 select->widget.focus_y = 0;
1025 static bool button_process_key(struct nc_widget *widget,
1026 FORM *form __attribute__((unused)), int key)
1028 struct nc_widget_button *button = to_button(widget);
1033 if (!key_is_select(key))
1036 button->click(button->arg);
1040 static int button_destructor(void *ptr)
1042 struct nc_widget_button *button = ptr;
1043 free_field(button->widget.field);
1047 struct nc_widget_button *widget_new_button(struct nc_widgetset *set,
1048 int y, int x, int size, const char *str,
1049 void (*click)(void *), void *arg)
1051 struct nc_widget_button *button;
1052 int idx, len, pad1, pad2, bufsz;
1056 int field_size = size + 2;
1058 button = talloc_zero(set, struct nc_widget_button);
1059 button->widget.height = 1;
1060 button->widget.width = field_size;
1061 button->widget.x = x;
1062 button->widget.y = y;
1063 button->widget.field = f = new_field(1, field_size, y, x, 0, 0);
1064 button->widget.process_key = button_process_key;
1065 button->widget.focussed_attr = A_REVERSE;
1066 button->widget.unfocussed_attr = A_NORMAL;
1067 button->click = click;
1070 field_opts_off(f, O_EDIT);
1071 set_field_userptr(f, &button->widget);
1073 /* Center str in the field. This depends on the number of columns used
1074 * by the string, not the number of chars in str */
1075 len = strncols(str);
1077 idx = (field_size - len) / 2;
1080 pb_log("Warning: '%s' %d columns wide "
1081 "but button is %d columns wide\n",
1085 pad1 = max(idx - 1, 0);
1086 pad2 = max(size - len - pad1, 0);
1087 bufsz = 1 + pad1 + strlen(str) + pad2 + 2;
1089 text = talloc_array(button, char, bufsz);
1090 memset(text, ' ', bufsz);
1091 memcpy(text + idx, str, strlen(str));
1093 text[bufsz - 2] = ']';
1094 text[bufsz - 1] = '\0';
1096 set_field_buffer(f, 0, text);
1098 widgetset_add_field(set, button->widget.field);
1099 talloc_set_destructor(button, button_destructor);
1104 void widget_focus_change(struct nc_widget *widget, FIELD *field,
1107 int attr = focussed ? widget->focussed_attr : widget->unfocussed_attr;
1108 set_field_back(field, attr);
1111 bool widgetset_process_key(struct nc_widgetset *set, int key)
1113 struct nc_widget *widget;
1118 field = current_field(set->form);
1121 widget = field_userptr(field);
1123 if (widget->process_key)
1124 if (widget->process_key(widget, set->form, key))
1129 /* handle field change events */
1135 req = REQ_SPREV_FIELD;
1141 req = REQ_SNEXT_FIELD;
1144 req = REQ_SFIRST_FIELD;
1147 req = REQ_SLAST_FIELD;
1150 req = REQ_LEFT_FIELD;
1153 req = REQ_RIGHT_FIELD;
1158 widget_focus_change(widget, field, false);
1159 form_driver(set->form, req);
1161 /* if we're doing a tabbed-field-change, skip until we
1162 * see the next widget */
1164 field = current_field(set->form);
1166 for (; tab && tmp != field && field_userptr(field) == widget;) {
1167 form_driver(set->form, req);
1168 field = current_field(set->form);
1171 form_driver(set->form, REQ_END_FIELD);
1172 widget = field_userptr(field);
1173 widget_focus_change(widget, field, true);
1174 if (widget->field_focus)
1175 widget->field_focus(widget, field);
1176 if (set->widget_focus)
1177 set->widget_focus(widget, set->widget_focus_arg);
1184 static int widgetset_destructor(void *ptr)
1186 struct nc_widgetset *set = ptr;
1187 free_form(set->form);
1188 if (set->ipv4_multi_type)
1189 free_fieldtype(set->ipv4_multi_type);
1193 struct nc_widgetset *widgetset_create(void *ctx, WINDOW *main, WINDOW *sub)
1195 struct nc_widgetset *set;
1197 set = talloc_zero(ctx, struct nc_widgetset);
1198 set->n_alloc_fields = 8;
1199 set->mainwin = main;
1201 set->fields = talloc_array(set, FIELD *, set->n_alloc_fields);
1202 talloc_set_destructor(set, widgetset_destructor);
1207 void widgetset_set_windows(struct nc_widgetset *set,
1208 WINDOW *main, WINDOW *sub)
1210 set->mainwin = main;
1214 void widgetset_set_widget_focus(struct nc_widgetset *set,
1215 widget_focus_cb cb, void *arg)
1217 set->widget_focus = cb;
1218 set->widget_focus_arg = arg;
1221 void widgetset_post(struct nc_widgetset *set)
1223 struct nc_widget *widget;
1226 set->form = new_form(set->fields);
1227 set_form_win(set->form, set->mainwin);
1228 set_form_sub(set->form, set->subwin);
1229 post_form(set->form);
1230 form_driver(set->form, REQ_END_FIELD);
1233 set_current_field(set->form, set->cur_field);
1235 field = current_field(set->form);
1236 widget = field_userptr(field);
1237 widget_focus_change(widget, field, true);
1238 if (set->widget_focus)
1239 set->widget_focus(widget, set->widget_focus_arg);
1242 void widgetset_unpost(struct nc_widgetset *set)
1244 set->cur_field = current_field(set->form);
1245 unpost_form(set->form);
1246 free_form(set->form);
1250 static void widgetset_add_field(struct nc_widgetset *set, FIELD *field)
1252 if (set->n_fields == set->n_alloc_fields - 1) {
1253 set->n_alloc_fields *= 2;
1254 set->fields = talloc_realloc(set, set->fields,
1255 FIELD *, set->n_alloc_fields);
1259 set->fields[set->n_fields - 1] = field;
1260 set->fields[set->n_fields] = NULL;
1263 static void widgetset_remove_field(struct nc_widgetset *set, FIELD *field)
1267 for (i = 0; i < set->n_fields; i++) {
1268 if (set->fields[i] == field)
1272 if (i == set->n_fields)
1275 memmove(&set->fields[i], &set->fields[i+i],
1276 (set->n_fields - i) * sizeof(set->fields[i]));
1280 #define DECLARE_BASEFN(type) \
1281 struct nc_widget *widget_ ## type ## _base \
1282 (struct nc_widget_ ## type *w) \
1283 { return &w->widget; }
1285 DECLARE_BASEFN(textbox);
1286 DECLARE_BASEFN(checkbox);
1287 DECLARE_BASEFN(subset);
1288 DECLARE_BASEFN(select);
1289 DECLARE_BASEFN(label);
1290 DECLARE_BASEFN(button);
1292 void widget_set_visible(struct nc_widget *widget, bool visible)
1294 if (widget->set_visible)
1295 widget->set_visible(widget, visible);
1297 field_set_visible(widget->field, visible);
1300 void widget_move(struct nc_widget *widget, int y, int x)
1303 widget->move(widget, y, x);
1305 field_move(widget->field, y, x);
1310 if (x + widget->width > COLS)
1311 pb_debug("%s: Widget at %d,%d runs over pad! (%d)", __func__,
1312 y, x, x + widget->width);
1315 int widget_height(struct nc_widget *widget)
1317 return widget->height;
1320 int widget_width(struct nc_widget *widget)
1322 return widget->width;
1325 int widget_x(struct nc_widget *widget)
1330 int widget_y(struct nc_widget *widget)
1335 int widget_focus_y(struct nc_widget *widget)
1337 return widget->focus_y;