/* Exported interface, documented in box_textarea.h */ bool box_textarea_create_textarea(html_content *html, struct box *box, struct dom_node *node) { dom_string *dom_text = NULL; dom_exception err; textarea_setup ta_setup; textarea_flags ta_flags; plot_font_style_t fstyle; bool read_only = false; struct form_control *gadget = box->gadget; const char *text; assert(gadget != NULL); assert(gadget->type == GADGET_TEXTAREA || gadget->type == GADGET_TEXTBOX || gadget->type == GADGET_PASSWORD); if (gadget->type == GADGET_TEXTAREA) { dom_html_text_area_element *textarea = (dom_html_text_area_element *) node; ta_flags = TEXTAREA_MULTILINE; err = dom_html_text_area_element_get_read_only( textarea, &read_only); if (err != DOM_NO_ERR) return false; /* Get the textarea's initial content */ err = dom_html_text_area_element_get_value(textarea, &dom_text); if (err != DOM_NO_ERR) return false; } else { dom_html_input_element *input = (dom_html_input_element *) node; err = dom_html_input_element_get_read_only( input, &read_only); if (err != DOM_NO_ERR) return false; if (gadget->type == GADGET_PASSWORD) ta_flags = TEXTAREA_PASSWORD; else ta_flags = TEXTAREA_DEFAULT; /* Get initial text */ err = dom_html_input_element_get_value(input, &dom_text); if (err != DOM_NO_ERR) return false; } if (dom_text != NULL) { text = dom_string_data(dom_text); } else { /* No initial text, or failed reading it; * use a blank string */ text = ""; } if (read_only) ta_flags |= TEXTAREA_READONLY; gadget->data.text.data.gadget = gadget; font_plot_style_from_css(gadget->box->style, &fstyle); /* Reset to correct values by layout */ ta_setup.width = 200; ta_setup.height = 20; ta_setup.pad_top = 4; ta_setup.pad_right = 4; ta_setup.pad_bottom = 4; ta_setup.pad_left = 4; /* Set remaining data */ ta_setup.border_width = 0; ta_setup.border_col = 0x000000; ta_setup.text = fstyle; ta_setup.text.background = NS_TRANSPARENT; /* Make selected text either black or white, as gives greatest contrast * with background colour. */ ta_setup.selected_bg = fstyle.foreground; ta_setup.selected_text = colour_to_bw_furthest(ta_setup.selected_bg); /* Hand reference to dom text over to gadget */ gadget->data.text.initial = dom_text; gadget->data.text.ta = textarea_create(ta_flags, &ta_setup, box_textarea_callback, &gadget->data.text.data); if (gadget->data.text.ta == NULL) { return false; } if (!textarea_set_text(gadget->data.text.ta, text)) return false; return true; }
static struct form_control * parse_input_element(struct form *forms, dom_html_input_element *input) { struct form_control *control = NULL; dom_html_form_element *form = NULL; dom_string *ds_type = NULL; dom_string *ds_name = NULL; dom_string *ds_value = NULL; char *name = NULL; if (dom_html_input_element_get_form(input, &form) != DOM_NO_ERR) goto out; if (dom_html_input_element_get_type(input, &ds_type) != DOM_NO_ERR) goto out; if (dom_html_input_element_get_name(input, &ds_name) != DOM_NO_ERR) goto out; if (ds_name != NULL) name = strndup(dom_string_data(ds_name), dom_string_byte_length(ds_name)); if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_password)) { control = form_new_control(input, GADGET_PASSWORD); } else if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_file)) { control = form_new_control(input, GADGET_FILE); } else if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_hidden)) { control = form_new_control(input, GADGET_HIDDEN); } else if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_checkbox)) { control = form_new_control(input, GADGET_CHECKBOX); } else if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_radio)) { control = form_new_control(input, GADGET_RADIO); } else if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_submit)) { control = form_new_control(input, GADGET_SUBMIT); } else if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_reset)) { control = form_new_control(input, GADGET_RESET); } else if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_button)) { control = form_new_control(input, GADGET_BUTTON); } else if (ds_type != NULL && dom_string_caseless_lwc_isequal(ds_type, corestring_lwc_image)) { control = form_new_control(input, GADGET_IMAGE); } else { control = form_new_control(input, GADGET_TEXTBOX); } if (control == NULL) goto out; if (name != NULL) { /* Hand the name string over */ control->name = name; name = NULL; } if (control->type == GADGET_CHECKBOX || control->type == GADGET_RADIO) { bool selected; if (dom_html_input_element_get_checked( input, &selected) == DOM_NO_ERR) { control->selected = selected; } } if (control->type == GADGET_PASSWORD || control->type == GADGET_TEXTBOX) { int32_t maxlength; if (dom_html_input_element_get_max_length( input, &maxlength) != DOM_NO_ERR) { maxlength = -1; } if (maxlength >= 0) { /* Got valid maxlength */ control->maxlength = maxlength; } else { /* Input has no maxlength attr, or * dom_html_input_element_get_max_length failed. * * Set it to something insane. */ control->maxlength = UINT_MAX; } } if (control->type != GADGET_FILE && control->type != GADGET_IMAGE) { if (dom_html_input_element_get_value( input, &ds_value) == DOM_NO_ERR) { if (ds_value != NULL) { control->value = strndup( dom_string_data(ds_value), dom_string_byte_length(ds_value)); if (control->value == NULL) { form_free_control(control); control = NULL; goto out; } control->length = strlen(control->value); } } if (control->type == GADGET_TEXTBOX || control->type == GADGET_PASSWORD) { if (control->value == NULL) { control->value = strdup(""); if (control->value == NULL) { form_free_control(control); control = NULL; goto out; } control->length = 0; } control->initial_value = strdup(control->value); if (control->initial_value == NULL) { form_free_control(control); control = NULL; goto out; } } } if (form != NULL && control != NULL) form_add_control(find_form(forms, form), control); out: if (form != NULL) dom_node_unref(form); if (ds_type != NULL) dom_string_unref(ds_type); if (ds_name != NULL) dom_string_unref(ds_name); if (ds_value != NULL) dom_string_unref(ds_value); if (name != NULL) free(name); return control; }