/* * Create a row of buttons, one for each action, at * the bottom of a curses_form. */ static void create_buttons(const struct dfui_form *f, struct curses_form *cf, int is_menu) { struct curses_widget *w; char name[80]; struct dfui_action *a; struct curses_widget *row_start = NULL; int left_acc = 1; const char *accel; for (a = dfui_form_action_get_first(f); a != NULL; a = dfui_action_get_next(a)) { strlcpy(name, dfui_info_get_name(dfui_action_get_info(a)), 70); dfui_debug("creating button `%s' (%d) @ %d / %d\n", name, strlen(name), left_acc, cf->width); /* * Check for overflow. If the next button would appear * off the right side of the form, start putting buttons * on the next row. Or, if this is a menu, always put the * next button on the next line. */ if (is_menu || ((left_acc + strlen(name) + 6) > cf->width && left_acc > 1)) { row_start = center_buttons(cf, row_start, is_menu); cf->height++; left_acc = 1; } w = curses_form_widget_add(cf, left_acc, cf->height, 0, CURSES_BUTTON, name, 0, CURSES_WIDGET_WIDEN); curses_widget_tooltip_set(w, dfui_info_get_short_desc(dfui_action_get_info(a))); accel = dfui_action_property_get(a, "accelerator"); if (strlen(accel) > 0) { if (strcmp(accel, "ESC") == 0) { w->accel = '\e'; } else { w->accel = toupper(accel[0]); } } left_acc += (w->width + 2); w->user_id = -1; w->userdata = a; curses_widget_set_click_cb(w, cb_click_close_form); if (row_start == NULL) row_start = w; } center_buttons(cf, row_start, is_menu); }
static void display_form(struct dfui_form *f) { struct dfui_field *fi; struct dfui_action *a; printf("," BAR "\n"); for (fi = dfui_form_field_get_first(f); fi != NULL; fi = dfui_field_get_next(fi)) { printf("| %30s %c[ %-30s ]\n", dfui_info_get_name(dfui_field_get_info(fi)), dfui_field_property_is(fi, "control", "checkbox") ? '+' : ' ', "(value)" ); } printf("+" BAR "\n"); printf("| Actions: "); for (a = dfui_form_action_get_first(f); a != NULL; a = dfui_action_get_next(a)) { printf("[ %s ] ", dfui_info_get_name(dfui_action_get_info(a)) ); } printf("\n"); printf("`" BAR "\n"); }
static struct curses_form * curses_form_construct_from_dfui_form_multiple(const struct dfui_form *f) { struct curses_form *cf; struct curses_form_userdata *cfu; const char *min_width_str; unsigned int desc_width, min_width = 0; unsigned int len, max_label_width, total_label_width; unsigned int max_button_width, total_button_width; struct dfui_field *fi; struct dfui_action *a; struct curses_widget *label, *button; struct dfui_dataset *ds; const char *name; int left_acc, top_acc; int row = 1, col = 0, ins_x = 1, is_menu = 0; dfui_debug("-----\nconstructing multiple form: %s\n", dfui_info_get_name(dfui_form_get_info(f))); cf = curses_form_new(dfui_info_get_name(dfui_form_get_info(f))); AURA_MALLOC(cfu, curses_form_userdata); cfu->f = f; cf->userdata = cfu; cf->cleanup = 1; set_help(f, cf); /* Calculate offsets for nice positioning of labels and buttons. */ /* * Determine the widths of the widest field and the widest * button, and the total widths of all fields and all buttons. */ max_label_width = 0; total_label_width = 0; max_button_width = 0; total_button_width = 0; for (fi = dfui_form_field_get_first(f); fi != NULL; fi = dfui_field_get_next(fi)) { len = MIN(60, strlen(dfui_info_get_name(dfui_field_get_info(fi)))); if (len > max_label_width) max_label_width = len; total_label_width += (len + 2); } for (a = dfui_form_action_get_first(f); a != NULL; a = dfui_action_get_next(a)) { len = strlen(dfui_info_get_name(dfui_action_get_info(a))); if (len > max_button_width) max_button_width = len; total_button_width += (len + 6); } /* Take the short description and turn it into a set of labels. */ if ((min_width_str = dfui_form_property_get(f, "minimum_width")) != NULL) min_width = atoi(min_width_str); desc_width = 40; desc_width = MAX(desc_width, min_width); desc_width = MAX(desc_width, total_button_width); desc_width = MAX(desc_width, total_label_width); desc_width = MIN(desc_width, xmax - 3); dfui_debug("min width: %d\n", min_width); dfui_debug("button width: %d\n", total_button_width); dfui_debug("label width: %d\n", total_label_width); dfui_debug("resulting width: %d\n", desc_width); dfui_debug("form width: %d\n", cf->width); cf->height = curses_form_descriptive_labels_add(cf, dfui_info_get_short_desc(dfui_form_get_info(f)), 1, cf->height + 1, desc_width); dfui_debug("form width now: %d\n", cf->width); /* Add the fields. */ top_acc = cf->height + 1; cf->height += dfui_form_dataset_count(f) + 2; /* * Create the widgets for a multiple=true form. For each field * in the form, a label containing the field's name, which serves * as a heading, is created. Underneath these labels, for each * dataset in the form, a row of input widgets (typically textboxes) * is added. Non-action, manipulation buttons are also added to * the right of each row. */ left_acc = 1; for (fi = dfui_form_field_get_first(f); fi != NULL; fi = dfui_field_get_next(fi)) { /* * Create a label to serve as a heading for the column. */ name = dfui_info_get_name(dfui_field_get_info(fi)); label = curses_form_widget_add(cf, left_acc, top_acc, 0, CURSES_LABEL, name, 0, CURSES_WIDGET_WIDEN); cfu->widths[col++] = label->width + 2; left_acc += (label->width + 2); } /* * Create a row of widgets for each dataset. */ top_acc++; for (ds = dfui_form_dataset_get_first(f); ds != NULL; ds = dfui_dataset_get_next(ds)) { ins_x = curses_form_create_widget_row(cf, NULL, ds, 1, top_acc++, row++); } /* * Finally, create an 'Add' button to add a new row * if this is an extensible form. */ if (dfui_form_is_extensible(f)) { button = curses_form_widget_add(cf, ins_x, top_acc, 0, CURSES_BUTTON, "Add", 0, CURSES_WIDGET_WIDEN); button->user_id = row; curses_widget_set_click_cb(button, cb_click_insert_row); cf->height++; } cf->height++; /* Add the buttons. */ create_buttons(f, cf, is_menu); cf->height++; curses_form_finalize(cf); return(cf); }
static struct curses_form * curses_form_construct_from_dfui_form_single(const struct dfui_form *f) { struct curses_form *cf; struct curses_form_userdata *cfu; const char *min_width_str; unsigned int desc_width, min_width = 0; unsigned int len, max_label_width, total_label_width; unsigned int max_button_width, total_button_width; struct dfui_field *fi; struct dfui_action *a; struct curses_widget *label, *xbox; struct dfui_celldata *cd; const char *value; int is_menu; dfui_debug("-----\nconstructing single form: %s\n", dfui_info_get_name(dfui_form_get_info(f))); is_menu = dfui_form_property_is(f, "role", "menu"); cf = curses_form_new(dfui_info_get_name(dfui_form_get_info(f))); AURA_MALLOC(cfu, curses_form_userdata); cfu->f = f; cf->userdata = cfu; cf->cleanup = 1; set_help(f, cf); /* Calculate offsets for nice positioning of labels and buttons. */ /* * Determine the widths of the widest field and the widest * button, and the total widths of all fields and all buttons. */ max_label_width = 0; total_label_width = 0; max_button_width = 0; total_button_width = 0; for (fi = dfui_form_field_get_first(f); fi != NULL; fi = dfui_field_get_next(fi)) { len = MIN(60, strlen(dfui_info_get_name(dfui_field_get_info(fi)))); if (len > max_label_width) max_label_width = len; total_label_width += (len + 2); } for (a = dfui_form_action_get_first(f); a != NULL; a = dfui_action_get_next(a)) { len = strlen(dfui_info_get_name(dfui_action_get_info(a))); if (len > max_button_width) max_button_width = len; total_button_width += (len + 6); } if (total_label_width > (xmax - 2)) total_label_width = (xmax - 2); /* XXX scroll/wrap? */ /* Take the short description and turn it into a set of labels. */ if ((min_width_str = dfui_form_property_get(f, "minimum_width")) != NULL) min_width = atoi(min_width_str); desc_width = 40; desc_width = MAX(desc_width, min_width); if (is_menu) { desc_width = MAX(desc_width, max_button_width); } else { desc_width = MAX(desc_width, total_button_width); } desc_width = MAX(desc_width, max_label_width); /* XXX + max_field_width */ desc_width = MIN(desc_width, xmax - 4); /* -2 for borders, -2 for spaces */ dfui_debug("min width: %d\n", min_width); dfui_debug("button width: %d\n", total_button_width); dfui_debug("label width: %d\n", total_label_width); dfui_debug("resulting width: %d\n", desc_width); dfui_debug("form width: %d\n", cf->width); cf->height = curses_form_descriptive_labels_add(cf, dfui_info_get_short_desc(dfui_form_get_info(f)), 1, cf->height + 1, desc_width); dfui_debug("form width now: %d\n", cf->width); if (!is_menu) cf->height++; /* * Add one label and one textbox (or other control) to a * curses_form for each field in the dfui_form. Each set of * labels and controls is added one row below the previous set. */ for (fi = dfui_form_field_get_first(f); fi != NULL; fi = dfui_field_get_next(fi)) { label = curses_form_widget_add(cf, 1, cf->height, max_label_width, CURSES_LABEL, dfui_info_get_name(dfui_field_get_info(fi)), 0, 0); cd = dfui_dataset_celldata_find(dfui_form_dataset_get_first(f), dfui_field_get_id(fi)); value = dfui_celldata_get_value(cd); if (dfui_field_property_is(fi, "control", "checkbox")) { xbox = curses_form_widget_add(cf, max_label_width + 3, cf->height, 4, CURSES_CHECKBOX, "", 0, 0); xbox->amount = (value[0] == 'Y' ? 1 : 0); } else { xbox = curses_form_widget_add(cf, max_label_width + 3, cf->height, 20, CURSES_TEXTBOX, value, 256, 0); } curses_widget_tooltip_set(xbox, dfui_info_get_short_desc(dfui_field_get_info(fi))); xbox->user_id = 1; xbox->userdata = fi; if (dfui_field_property_is(fi, "editable", "false")) xbox->editable = 0; if (dfui_field_property_is(fi, "obscured", "true")) xbox->obscured = 1; if (dfui_field_option_get_first(fi) != NULL) { curses_widget_set_click_cb(xbox, cb_click_select_option); } cf->height++; } if (dfui_form_field_get_first(f) != NULL) cf->height++; create_buttons(f, cf, is_menu); cf->height++; curses_form_finalize(cf); return(cf); }