2 * Copyright (c)2004 Cat's Eye Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * Neither the name of Cat's Eye Technologies nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Translate DFUI forms to curses forms.
37 * $Id: curses_xlat.c,v 1.20 2005/02/08 21:39:42 cpressey Exp $
46 #include "libaura/mem.h"
48 #include "libdfui/dfui.h"
49 #include "libdfui/dump.h"
51 #include "curses_form.h"
52 #include "curses_widget.h"
53 #include "curses_util.h"
54 #include "curses_xlat.h"
56 #define MAX(a, b) (a > b ? a : b)
57 #define MIN(a, b) (a < b ? a : b)
61 static struct timeval last_update;
62 static unsigned int last_y;
65 * Callback to give to curses_widget_set_click_cb, for buttons
66 * that remove the same row of widgets that they are on.
69 cb_click_remove_row(struct curses_widget *w)
71 struct curses_form *cf = w->form;
72 struct curses_widget *few;
76 * Since we're going to be deleting the widget with
77 * the focus, first move the focus onto a widget
78 * that we won't be deleting.
81 if (cf->widget_focus == NULL)
82 cf->widget_focus = cf->widget_head;
83 while (cf->widget_focus->user_id == id)
84 cf->widget_focus = cf->widget_focus->prev;
85 } while (cf->widget_focus == NULL);
88 * Delete all widgets with the same id as the focused one.
90 for (few = cf->widget_head; few != NULL; few = few->next) {
91 if (few->user_id == id) {
92 curses_form_widget_remove(few);
94 * Reset the iterator, as the previous command
95 * may have obliterated the current widget.
97 few = cf->widget_head;
102 * Slide the remaining widgets up a row.
104 for (few = cf->widget_head; few != NULL; few = few->next) {
105 if (few->user_id > id) {
107 * Slide the rows below the deleted row up one row.
111 } else if (few->user_id == -1) {
113 * Slide the buttons, too.
122 * Now that the widgets are deleted, make sure the focus is
123 * on a usable widget (not a label.)
125 curses_form_focus_skip_forward(cf);
126 cf->want_y = cf->widget_focus->y;
129 * Repaint the form. XXX Might not be necessary anymore?
131 curses_form_draw(cf);
132 curses_form_refresh(cf);
137 * Callback to give to curses_widget_set_click_cb, for textboxes
138 * that pop up a list of options from which the user can select.
141 cb_click_select_option(struct curses_widget *w)
143 struct dfui_field *fi = w->userdata;
144 struct dfui_option *o;
145 struct curses_form *cf;
146 struct curses_widget *button, *cw;
148 cf = curses_form_new("* select *");
150 for (o = dfui_field_option_get_first(fi); o != NULL;
151 o = dfui_option_get_next(o)) {
152 button = curses_form_widget_add(cf, 1,
153 cf->height++, 0, CURSES_BUTTON,
154 dfui_option_get_value(o), 0, CURSES_WIDGET_WIDEN);
155 curses_widget_set_click_cb(button, cb_click_close_form);
158 curses_form_finalize(cf);
160 curses_form_draw(cf);
161 curses_form_refresh(cf);
162 cw = curses_form_frob(cf);
164 curses_textbox_set_text(w, cw->text);
166 curses_form_free(cf);
167 curses_form_refresh(NULL);
173 * XXX this should maybe be in libdfui.
175 static struct dfui_dataset *
176 create_default_dataset(const struct dfui_form *f)
178 struct dfui_dataset *ds;
179 struct dfui_field *fi;
181 ds = dfui_dataset_new();
182 for (fi = dfui_form_field_get_first(f); fi != NULL;
183 fi = dfui_field_get_next(fi)) {
184 dfui_dataset_celldata_add(ds,
185 dfui_field_get_id(fi), "");
192 * Callback to give to curses_widget_set_click_cb, for buttons
193 * that insert a row of widgets before the row that they are on.
196 cb_click_insert_row(struct curses_widget *w)
198 struct curses_form *cf = w->form;
199 struct curses_widget *few, *lw;
202 struct dfui_dataset *ds;
203 struct curses_form_userdata *cfu = cf->userdata;
206 * Find the last widget in the tab order that is of the prev row.
209 for (lw = w; lw != NULL; lw = lw->prev) {
210 if (lw->user_id == id - 1)
215 * Slide widgets below the row we're going to insert, down.
217 for (few = cf->widget_head; few != NULL; few = few->next) {
218 if (few->user_id >= id) {
220 * Slide the rows below the deleted row up one row.
224 } else if (few->user_id == -1) {
226 * Slide the buttons, too.
234 * Insert a new row of widgets.
236 ds = create_default_dataset(cfu->f);
237 curses_form_create_widget_row(cf, lw, ds, 1, top, id);
238 dfui_dataset_free(ds);
243 curses_form_widget_ensure_visible(cf->widget_focus);
244 cf->want_y = cf->widget_focus->y;
245 curses_form_draw(cf);
246 curses_form_refresh(cf);
251 * Create a row of widgets in a multiple=true form.
252 * Returns the x position of the "Ins" button, if any.
255 curses_form_create_widget_row(struct curses_form *cf, struct curses_widget *cw,
256 const struct dfui_dataset *ds, int left, int top, int row)
258 struct curses_widget *xbox, *button;
259 struct dfui_field *fi;
260 struct dfui_celldata *cd;
262 int col = 0, ins_x = left;
263 struct curses_form_userdata *cfu = cf->userdata;
264 const struct dfui_form *f = cfu->f;
267 * Create one input underneath each field heading.
269 for (fi = dfui_form_field_get_first(f); fi != NULL;
270 fi = dfui_field_get_next(fi)) {
271 cd = dfui_dataset_celldata_find(ds, dfui_field_get_id(fi));
272 value = dfui_celldata_get_value(cd);
274 if (dfui_field_property_is(fi, "control", "checkbox")) {
275 xbox = curses_form_widget_add(cf,
276 left, top, 4, CURSES_CHECKBOX, "", 0, 0);
277 xbox->amount = (value[0] == 'Y' ? 1 : 0);
279 xbox = curses_form_widget_add(cf, left, top,
280 cfu->widths[col] - 1, CURSES_TEXTBOX,
284 if (dfui_field_property_is(fi, "control", "checkbox")) {
285 xbox = curses_form_widget_insert_after(cw,
286 left, top, 4, CURSES_CHECKBOX, "", 0, 0);
287 xbox->amount = (value[0] == 'Y' ? 1 : 0);
289 xbox = curses_form_widget_insert_after(cw,
290 left, top, cfu->widths[col] - 1,
291 CURSES_TEXTBOX, value, 256, 0);
295 curses_widget_tooltip_set(xbox,
296 dfui_info_get_short_desc(dfui_field_get_info(fi)));
300 if (dfui_field_property_is(fi, "editable", "false"))
302 if (dfui_field_property_is(fi, "obscured", "true"))
305 if (dfui_field_option_get_first(fi) != NULL) {
306 curses_widget_set_click_cb(xbox, cb_click_select_option);
309 left += cfu->widths[col++];
313 * If this is an extensible form,
314 * create buttons for each dataset.
316 if (dfui_form_is_extensible(f)) {
318 button = curses_form_widget_add(cf, left,
319 top, 0, CURSES_BUTTON, "Ins", 0,
320 CURSES_WIDGET_WIDEN);
322 button = curses_form_widget_insert_after(cw, left,
323 top, 0, CURSES_BUTTON, "Ins", 0,
324 CURSES_WIDGET_WIDEN);
328 button->user_id = row;
329 curses_widget_set_click_cb(button, cb_click_insert_row);
331 left += button->width + 1;
334 button = curses_form_widget_add(cf, left,
335 top, 0, CURSES_BUTTON, "Del", 0,
336 CURSES_WIDGET_WIDEN);
338 button = curses_form_widget_insert_after(cw, left,
339 top, 0, CURSES_BUTTON, "Del", 0,
340 CURSES_WIDGET_WIDEN);
343 button->user_id = row;
344 curses_widget_set_click_cb(button, cb_click_remove_row);
350 static struct curses_widget *
351 center_buttons(struct curses_form *cf, struct curses_widget *row_start, int is_menu)
353 struct curses_widget *w;
354 int row_width, row_offset;
357 * Center the previous row of buttons on the form
358 * if this is not a menu.
361 /* Find the width of all buttons on the previous row. */
363 for (w = row_start; w != NULL; w = w->next) {
364 row_width += w->width + 2;
368 * Adjust the x position of each of button by
369 * a calculated offset.
371 row_offset = (cf->width - row_width) / 2;
372 for (w = row_start; w != NULL; w = w->next) {
377 * Mark the next button we will create
378 * as the first button of a row.
387 * Create a row of buttons, one for each action, at
388 * the bottom of a curses_form.
391 create_buttons(const struct dfui_form *f, struct curses_form *cf, int is_menu)
393 struct curses_widget *w;
395 struct dfui_action *a;
396 struct curses_widget *row_start = NULL;
400 for (a = dfui_form_action_get_first(f); a != NULL;
401 a = dfui_action_get_next(a)) {
402 strlcpy(name, dfui_info_get_name(dfui_action_get_info(a)), 70);
404 dfui_debug("creating button `%s' (%d) @ %d / %d\n",
405 name, strlen(name), left_acc, cf->width);
408 * Check for overflow. If the next button would appear
409 * off the right side of the form, start putting buttons
410 * on the next row. Or, if this is a menu, always put the
411 * next button on the next line.
414 ((left_acc + strlen(name) + 6) > cf->width &&
416 row_start = center_buttons(cf, row_start, is_menu);
421 w = curses_form_widget_add(cf, left_acc,
422 cf->height, 0, CURSES_BUTTON, name, 0, CURSES_WIDGET_WIDEN);
423 curses_widget_tooltip_set(w,
424 dfui_info_get_short_desc(dfui_action_get_info(a)));
426 accel = dfui_action_property_get(a, "accelerator");
427 if (strlen(accel) > 0) {
428 if (strcmp(accel, "ESC") == 0) {
431 w->accel = toupper(accel[0]);
435 left_acc += (w->width + 2);
438 curses_widget_set_click_cb(w, cb_click_close_form);
439 if (row_start == NULL)
443 center_buttons(cf, row_start, is_menu);
447 set_help(const struct dfui_form *f, struct curses_form *cf)
449 const char *help_text;
451 help_text = dfui_info_get_long_desc(dfui_form_get_info(f));
452 if (cf->help_text != NULL) {
455 if (strlen(help_text) > 0) {
456 cf->help_text = aura_strdup(help_text);
458 cf->help_text = NULL;
462 /*** FORM TRANSLATORS ***/
464 static struct curses_form *
465 curses_form_construct_from_dfui_form_single(const struct dfui_form *f)
467 struct curses_form *cf;
468 struct curses_form_userdata *cfu;
469 const char *min_width_str;
470 unsigned int desc_width, min_width = 0;
471 unsigned int len, max_label_width, total_label_width;
472 unsigned int max_button_width, total_button_width;
473 struct dfui_field *fi;
474 struct dfui_action *a;
475 struct curses_widget *xbox;
476 struct dfui_celldata *cd;
480 dfui_debug("-----\nconstructing single form: %s\n",
481 dfui_info_get_name(dfui_form_get_info(f)));
483 is_menu = dfui_form_property_is(f, "role", "menu");
484 cf = curses_form_new(dfui_info_get_name(dfui_form_get_info(f)));
485 AURA_MALLOC(cfu, curses_form_userdata);
492 /* Calculate offsets for nice positioning of labels and buttons. */
495 * Determine the widths of the widest field and the widest
496 * button, and the total widths of all fields and all buttons.
500 total_label_width = 0;
501 max_button_width = 0;
502 total_button_width = 0;
504 for (fi = dfui_form_field_get_first(f); fi != NULL;
505 fi = dfui_field_get_next(fi)) {
506 len = MIN(60, strlen(dfui_info_get_name(dfui_field_get_info(fi))));
507 if (len > max_label_width)
508 max_label_width = len;
509 total_label_width += (len + 2);
511 for (a = dfui_form_action_get_first(f); a != NULL;
512 a = dfui_action_get_next(a)) {
513 len = strlen(dfui_info_get_name(dfui_action_get_info(a)));
514 if (len > max_button_width)
515 max_button_width = len;
516 total_button_width += (len + 6);
519 if (total_label_width > (xmax - 2))
520 total_label_width = (xmax - 2); /* XXX scroll/wrap? */
522 /* Take the short description and turn it into a set of labels. */
524 if ((min_width_str = dfui_form_property_get(f, "minimum_width")) != NULL)
525 min_width = atoi(min_width_str);
528 desc_width = MAX(desc_width, min_width);
530 desc_width = MAX(desc_width, max_button_width);
532 desc_width = MAX(desc_width, total_button_width);
534 desc_width = MAX(desc_width, max_label_width); /* XXX + max_field_width */
535 desc_width = MIN(desc_width, xmax - 4); /* -2 for borders, -2 for spaces */
537 dfui_debug("min width: %d\n", min_width);
538 dfui_debug("button width: %d\n", total_button_width);
539 dfui_debug("label width: %d\n", total_label_width);
540 dfui_debug("resulting width: %d\n", desc_width);
541 dfui_debug("form width: %d\n", cf->width);
543 cf->height = curses_form_descriptive_labels_add(cf,
544 dfui_info_get_short_desc(dfui_form_get_info(f)),
545 1, cf->height + 1, desc_width);
547 dfui_debug("form width now: %d\n", cf->width);
553 * Add one label and one textbox (or other control) to a
554 * curses_form for each field in the dfui_form. Each set of
555 * labels and controls is added one row below the previous set.
557 for (fi = dfui_form_field_get_first(f); fi != NULL;
558 fi = dfui_field_get_next(fi)) {
559 curses_form_widget_add(cf, 1,
560 cf->height, max_label_width, CURSES_LABEL,
561 dfui_info_get_name(dfui_field_get_info(fi)), 0, 0);
563 cd = dfui_dataset_celldata_find(dfui_form_dataset_get_first(f),
564 dfui_field_get_id(fi));
566 value = dfui_celldata_get_value(cd);
568 if (dfui_field_property_is(fi, "control", "checkbox")) {
569 xbox = curses_form_widget_add(cf,
571 cf->height, 4, CURSES_CHECKBOX, "", 0, 0);
572 xbox->amount = (value[0] == 'Y' ? 1 : 0);
574 xbox = curses_form_widget_add(cf,
576 cf->height, 20, CURSES_TEXTBOX, value, 256, 0);
578 curses_widget_tooltip_set(xbox,
579 dfui_info_get_short_desc(dfui_field_get_info(fi)));
583 if (dfui_field_property_is(fi, "editable", "false"))
585 if (dfui_field_property_is(fi, "obscured", "true"))
588 if (dfui_field_option_get_first(fi) != NULL) {
589 curses_widget_set_click_cb(xbox, cb_click_select_option);
595 if (dfui_form_field_get_first(f) != NULL)
598 create_buttons(f, cf, is_menu);
602 curses_form_finalize(cf);
607 static struct curses_form *
608 curses_form_construct_from_dfui_form_multiple(const struct dfui_form *f)
610 struct curses_form *cf;
611 struct curses_form_userdata *cfu;
612 const char *min_width_str;
613 unsigned int desc_width, min_width = 0;
614 unsigned int len, max_label_width, total_label_width;
615 unsigned int max_button_width, total_button_width;
616 struct dfui_field *fi;
617 struct dfui_action *a;
618 struct curses_widget *label, *button;
619 struct dfui_dataset *ds;
621 int left_acc, top_acc;
622 int row = 1, col = 0, ins_x = 1, is_menu = 0;
624 dfui_debug("-----\nconstructing multiple form: %s\n",
625 dfui_info_get_name(dfui_form_get_info(f)));
627 cf = curses_form_new(dfui_info_get_name(dfui_form_get_info(f)));
628 AURA_MALLOC(cfu, curses_form_userdata);
635 /* Calculate offsets for nice positioning of labels and buttons. */
638 * Determine the widths of the widest field and the widest
639 * button, and the total widths of all fields and all buttons.
643 total_label_width = 0;
644 max_button_width = 0;
645 total_button_width = 0;
647 for (fi = dfui_form_field_get_first(f); fi != NULL;
648 fi = dfui_field_get_next(fi)) {
649 len = MIN(60, strlen(dfui_info_get_name(dfui_field_get_info(fi))));
650 if (len > max_label_width)
651 max_label_width = len;
652 total_label_width += (len + 2);
654 for (a = dfui_form_action_get_first(f); a != NULL;
655 a = dfui_action_get_next(a)) {
656 len = strlen(dfui_info_get_name(dfui_action_get_info(a)));
657 if (len > max_button_width)
658 max_button_width = len;
659 total_button_width += (len + 6);
662 /* Take the short description and turn it into a set of labels. */
664 if ((min_width_str = dfui_form_property_get(f, "minimum_width")) != NULL)
665 min_width = atoi(min_width_str);
668 desc_width = MAX(desc_width, min_width);
669 desc_width = MAX(desc_width, total_button_width);
670 desc_width = MAX(desc_width, total_label_width);
671 desc_width = MIN(desc_width, xmax - 3);
673 dfui_debug("min width: %d\n", min_width);
674 dfui_debug("button width: %d\n", total_button_width);
675 dfui_debug("label width: %d\n", total_label_width);
676 dfui_debug("resulting width: %d\n", desc_width);
677 dfui_debug("form width: %d\n", cf->width);
679 cf->height = curses_form_descriptive_labels_add(cf,
680 dfui_info_get_short_desc(dfui_form_get_info(f)),
681 1, cf->height + 1, desc_width);
683 dfui_debug("form width now: %d\n", cf->width);
685 /* Add the fields. */
687 top_acc = cf->height + 1;
688 cf->height += dfui_form_dataset_count(f) + 2;
691 * Create the widgets for a multiple=true form. For each field
692 * in the form, a label containing the field's name, which serves
693 * as a heading, is created. Underneath these labels, for each
694 * dataset in the form, a row of input widgets (typically textboxes)
695 * is added. Non-action, manipulation buttons are also added to
696 * the right of each row.
699 for (fi = dfui_form_field_get_first(f); fi != NULL;
700 fi = dfui_field_get_next(fi)) {
702 * Create a label to serve as a heading for the column.
704 name = dfui_info_get_name(dfui_field_get_info(fi));
705 label = curses_form_widget_add(cf, left_acc,
706 top_acc, 0, CURSES_LABEL, name, 0,
707 CURSES_WIDGET_WIDEN);
708 cfu->widths[col++] = label->width + 2;
709 left_acc += (label->width + 2);
713 * Create a row of widgets for each dataset.
716 for (ds = dfui_form_dataset_get_first(f); ds != NULL;
717 ds = dfui_dataset_get_next(ds)) {
718 ins_x = curses_form_create_widget_row(cf, NULL, ds,
719 1, top_acc++, row++);
723 * Finally, create an 'Add' button to add a new row
724 * if this is an extensible form.
726 if (dfui_form_is_extensible(f)) {
727 button = curses_form_widget_add(cf,
729 CURSES_BUTTON, "Add", 0, CURSES_WIDGET_WIDEN);
730 button->user_id = row;
731 curses_widget_set_click_cb(button, cb_click_insert_row);
737 /* Add the buttons. */
739 create_buttons(f, cf, is_menu);
743 curses_form_finalize(cf);
749 curses_form_construct_from_dfui_form(const struct dfui_form *f)
751 if (dfui_form_is_multiple(f))
752 return(curses_form_construct_from_dfui_form_multiple(f));
754 return(curses_form_construct_from_dfui_form_single(f));
757 #define FIFTY_EIGHT_SPACES " "
760 strcpy_max(char *dest, const char *src, unsigned int max)
764 strncpy(dest, src, max);
765 if (strlen(src) > max) {
766 strcpy(dest + (max - 3), "...");
768 strncpy(dest + strlen(src),
769 FIFTY_EIGHT_SPACES, max - strlen(src));
771 for (i = 0; i < strlen(dest); i++) {
772 if (isspace(dest[i]))
778 curses_form_construct_from_dfui_progress(const struct dfui_progress *pr,
779 struct curses_widget **pbar,
780 struct curses_widget **plab,
781 struct curses_widget **pcan)
783 struct curses_form *cf;
786 desc = dfui_info_get_short_desc(dfui_progress_get_info(pr));
788 cf = curses_form_new(dfui_info_get_name(dfui_progress_get_info(pr)));
793 if (dfui_progress_get_streaming(pr)) {
797 *plab = curses_form_widget_add(cf, 0, 1, 58,
798 CURSES_LABEL, FIFTY_EIGHT_SPACES, 0, CURSES_WIDGET_CENTER);
799 strcpy_max((*plab)->text, desc, 58);
800 *pbar = curses_form_widget_add(cf, 0, 3, 40,
801 CURSES_PROGRESS, "", 0, CURSES_WIDGET_CENTER);
802 *pcan = curses_form_widget_add(cf, 0, 5, 0,
803 CURSES_BUTTON, "Cancel", 0,
804 CURSES_WIDGET_CENTER | CURSES_WIDGET_WIDEN);
805 (*pbar)->amount = dfui_progress_get_amount(pr);
807 last_y = (*pbar)->y + 2;
809 curses_form_finalize(cf);
811 gettimeofday(&last_update, NULL);
817 curses_widgets_update_from_dfui_progress(const struct dfui_progress *pr,
818 struct curses_widget *pbar,
819 struct curses_widget *plab,
820 struct curses_widget *pcan)
822 const char *short_desc;
825 struct curses_widget *w;
826 int short_desc_changed;
828 gettimeofday(&now, NULL);
829 msec_diff = (now.tv_sec - last_update.tv_sec) * 1000 +
830 (now.tv_usec - last_update.tv_usec) / 1000;
832 short_desc = dfui_info_get_short_desc(dfui_progress_get_info(pr));
833 short_desc_changed = (strncmp(plab->text, short_desc, MIN(55, strlen(short_desc))) != 0);
835 if (msec_diff < 100 && !dfui_progress_get_streaming(pr) && !short_desc_changed)
838 if (dfui_progress_get_amount(pr) != pbar->amount ||
839 short_desc_changed ||
840 dfui_progress_get_streaming(pr)) {
841 strcpy_max(plab->text, short_desc, 58);
842 curses_widget_draw(plab);
843 pbar->amount = dfui_progress_get_amount(pr);
844 curses_widget_draw(pbar);
845 if (dfui_progress_get_streaming(pr)) {
846 /* add a label with the text */
847 w = curses_form_widget_add(pbar->form, 0, ++last_y, 58,
848 CURSES_LABEL, FIFTY_EIGHT_SPACES, 0, CURSES_WIDGET_CENTER);
849 strcpy_max(w->text, dfui_progress_get_msg_line(pr), 58);
850 if (last_y >= pbar->form->int_height) {
851 pbar->form->int_height = last_y + 1;
853 curses_form_widget_ensure_visible(w);
854 curses_widget_draw(w);
857 curses_progress_spin(pbar);
859 wmove(pcan->form->win, pcan->y + 1, pcan->x + pcan->width + 1);
860 curses_form_refresh(NULL);
865 curses_widget_xlat_value(const struct curses_widget *cw)
867 if (cw->type == CURSES_TEXTBOX)
869 else if (cw->type == CURSES_CHECKBOX)
870 return(cw->amount ? "Y" : "N");
875 static struct dfui_response *
876 response_construct_from_curses_form_single(const struct dfui_form *f,
877 const struct curses_form *cf,
878 const struct curses_widget *cw)
880 struct dfui_response *r = NULL;
881 struct dfui_action *selected = NULL;
882 struct dfui_dataset *ds = NULL;
886 selected = cw->userdata;
887 r = dfui_response_new(dfui_form_get_id(f),
888 dfui_action_get_id(selected));
889 ds = dfui_dataset_new();
890 for (cw = cf->widget_head; cw != NULL; cw = cw->next) {
891 if (cw->user_id > 0) {
892 id = dfui_field_get_id((struct dfui_field *)cw->userdata);
893 value = curses_widget_xlat_value(cw);
894 dfui_dataset_celldata_add(ds, id, value);
897 dfui_response_dataset_add(r, ds);
902 static struct dfui_response *
903 response_construct_from_curses_form_multiple(const struct dfui_form *f,
904 const struct curses_form *cf,
905 const struct curses_widget *cw)
907 struct dfui_response *r = NULL;
908 struct dfui_action *selected = NULL;
909 struct dfui_dataset *ds = NULL;
913 int rows = 100; /* XXX obviously we'd prefer something more efficient here! */
916 selected = cw->userdata;
917 r = dfui_response_new(dfui_form_get_id(f),
918 dfui_action_get_id(selected));
920 /* Create one dataset per row. */
921 for (row = 1; row < rows; row++) {
922 ds = dfui_dataset_new();
924 for (cw = cf->widget_head; cw != NULL; cw = cw->next) {
925 if (cw->user_id == row &&
926 (cw->type == CURSES_TEXTBOX || cw->type == CURSES_CHECKBOX)) {
927 id = dfui_field_get_id((struct dfui_field *)cw->userdata);
928 value = curses_widget_xlat_value(cw);
929 dfui_dataset_celldata_add(ds, id, value);
934 dfui_response_dataset_add(r, ds);
936 dfui_dataset_free(ds);
943 struct dfui_response *
944 response_construct_from_curses_form(const struct dfui_form *f,
945 const struct curses_form *cf,
946 const struct curses_widget *cw)
948 if (dfui_form_is_multiple(f))
949 return(response_construct_from_curses_form_multiple(f, cf, cw));
951 return(response_construct_from_curses_form_single(f, cf, cw));