Ejemplo n.º 1
0
/*
 * Create a row of widgets in a multiple=true form.
 * Returns the x position of the "Ins" button, if any.
 */
int
curses_form_create_widget_row(struct curses_form *cf, struct curses_widget *cw,
	const struct dfui_dataset *ds, int left, int top, int row)
{
	struct curses_widget *xbox, *button;
	struct dfui_field *fi;
	struct dfui_celldata *cd;
	const char *value;
	int col = 0, ins_x = left;
	struct curses_form_userdata *cfu = cf->userdata;
	const struct dfui_form *f = cfu->f;

	/*
	 * Create one input underneath each field heading.
	 */
	for (fi = dfui_form_field_get_first(f); fi != NULL;
	     fi = dfui_field_get_next(fi)) {
		cd = dfui_dataset_celldata_find(ds, dfui_field_get_id(fi));
		value = dfui_celldata_get_value(cd);
		if (cw == NULL) {
			if (dfui_field_property_is(fi, "control", "checkbox")) {
				xbox = curses_form_widget_add(cf,
				    left, top, 4, CURSES_CHECKBOX, "", 0, 0);
				xbox->amount = (value[0] == 'Y' ? 1 : 0);
			} else {
				xbox = curses_form_widget_add(cf, left, top,
				    cfu->widths[col] - 1, CURSES_TEXTBOX,
				    value, 256, 0);
			}
		} else {
			if (dfui_field_property_is(fi, "control", "checkbox")) {
				xbox = curses_form_widget_insert_after(cw,
				    left, top, 4, CURSES_CHECKBOX, "", 0, 0);
				xbox->amount = (value[0] == 'Y' ? 1 : 0);
			} else {
				xbox = curses_form_widget_insert_after(cw,
				    left, top, cfu->widths[col] - 1,
				    CURSES_TEXTBOX, value, 256, 0);
			}
			cw = xbox;
		}
		curses_widget_tooltip_set(xbox,
		    dfui_info_get_short_desc(dfui_field_get_info(fi)));
		xbox->user_id = row;
		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);
		}

		left += cfu->widths[col++];
	}

	/*
	 * If this is an extensible form,
	 * create buttons for each dataset.
	 */
	if (dfui_form_is_extensible(f)) {
		if (cw == NULL) {
			button = curses_form_widget_add(cf, left,
			    top, 0, CURSES_BUTTON, "Ins", 0,
			    CURSES_WIDGET_WIDEN);
		} else {
			button = curses_form_widget_insert_after(cw, left,
			    top, 0, CURSES_BUTTON, "Ins", 0,
			    CURSES_WIDGET_WIDEN);
			cw = button;
		}
		ins_x = left;
		button->user_id = row;
		curses_widget_set_click_cb(button, cb_click_insert_row);

		left += button->width + 1;

		if (cw == NULL) {
			button = curses_form_widget_add(cf, left,
			    top, 0, CURSES_BUTTON, "Del", 0,
			    CURSES_WIDGET_WIDEN);
		} else {
			button = curses_form_widget_insert_after(cw, left,
			    top, 0, CURSES_BUTTON, "Del", 0,
			    CURSES_WIDGET_WIDEN);
			cw = button;
		}
		button->user_id = row;
		curses_widget_set_click_cb(button, cb_click_remove_row);
	}

	return(ins_x);
}
Ejemplo n.º 2
0
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);
}
Ejemplo n.º 3
0
/*
 * Push a new Lua table representing the given DFUI response
 * onto the Lua stack.
 */
static int
lua_table_from_dfui_response(lua_State *L, struct dfui_response *r)
{
	int table_idx, list_idx, subtable_idx;
	struct dfui_dataset *ds;
	struct dfui_celldata *cd;
	const char *value;
	int counter = 1;
	const char *f_id, *a_id;

	lua_newtable(L);
	table_idx = lua_gettop(L);

	/*
	 * Add response id's to the table.
	 */
	f_id = dfui_response_get_form_id(r);
	a_id = dfui_response_get_action_id(r);

	lua_pushliteral(L, "form_id");
	lua_pushlstring(L, f_id, strlen(f_id));
	lua_settable(L, table_idx);

	lua_pushliteral(L, "action_id");
	lua_pushlstring(L, a_id, strlen(a_id));
	lua_settable(L, table_idx);

	/*
	 * Create 'datasets' lists to the table.
	 */
	lua_pushliteral(L, "datasets");
	lua_newtable(L);
	list_idx = lua_gettop(L);
	
	/*
	 * Add response datasets to the 'datasets' list.
	 */
	for (ds = dfui_response_dataset_get_first(r); ds != NULL;
	     ds = dfui_dataset_get_next(ds)) {
		lua_pushnumber(L, counter++);
		lua_newtable(L);
		subtable_idx = lua_gettop(L);
		/*
		 * Populate this subtable with the celldatas...
		 */
		for (cd = dfui_dataset_celldata_get_first(ds); cd != NULL;
		     cd = dfui_celldata_get_next(cd)) {
			f_id = dfui_celldata_get_field_id(cd);
			value = dfui_celldata_get_value(cd);
			lua_pushlstring(L, f_id, strlen(f_id));
			lua_pushlstring(L, value, strlen(value));
			lua_settable(L, subtable_idx);
		}
		/*
		 * Add this subtable to the list
		 */
		lua_settable(L, list_idx);
	}

	/*
	 * Add the 'datasets' list to the table.
	 */
	lua_settable(L, table_idx);

	return(table_idx);
}