From 9995dbad578947656f08e58ec873de85a90b606d Mon Sep 17 00:00:00 2001 From: James Bursa Date: Sun, 12 Dec 2004 21:51:01 +0000 Subject: [project @ 2004-12-12 21:49:23 by bursa] Add form_new() and form_add_option(). svn path=/import/netsurf/; revision=1400 --- render/form.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ render/form.h | 22 ++++++++++++------- 2 files changed, 84 insertions(+), 8 deletions(-) (limited to 'render') diff --git a/render/form.c b/render/form.c index b52e5add5..119fa968b 100644 --- a/render/form.c +++ b/render/form.c @@ -23,6 +23,29 @@ static char *form_textarea_value(struct form_control *textarea); +/** + * Create a struct form. + * + * \param action URL to submit form to, used directly (not copied) + * \param method method and enctype + * \return a new structure, or 0 on memory exhaustion + */ + +struct form *form_new(char *action, form_method method) +{ + struct form *form; + + form = malloc(sizeof *form); + if (!form) + return 0; + form->action = action; + form->method = method; + form->controls = 0; + form->last_control = 0; + return form; +} + + /** * Create a struct form_control. * @@ -98,6 +121,53 @@ void form_free_control(struct form_control *control) } +/** + * Add an option to a form select control. + * + * \param control form control of type GADGET_SELECT + * \param value value of option, used directly (not copied) + * \param text text for option, used directly (not copied) + * \param selected this option is selected + * \return true on success, false on memory exhaustion + */ + +bool form_add_option(struct form_control *control, char *value, char *text, + bool selected) +{ + struct form_option *option; + + assert(control); + assert(control->type == GADGET_SELECT); + + option = malloc(sizeof *option); + if (!option) + return false; + option->selected = option->initial_selected = false; + option->value = value; + option->text = text; + option->next = 0; + + /* add to linked list */ + if (control->data.select.items == 0) + control->data.select.items = option; + else + control->data.select.last_item->next = option; + control->data.select.last_item = option; + + /* set selected */ + if (selected && (control->data.select.num_selected == 0 || + control->data.select.multiple)) { + option->selected = option->initial_selected = true; + control->data.select.num_selected++; + control->data.select.current = option; + } + + control->data.select.num_items++; + + return true; +} + + /** * Identify 'successful' controls. * diff --git a/render/form.h b/render/form.h index b5f57cf3c..00d8eb565 100644 --- a/render/form.h +++ b/render/form.h @@ -20,14 +20,17 @@ struct box; struct form_control; struct form_option; +/** Form submit method. */ +typedef enum { + method_GET, /**< GET, always url encoded. */ + method_POST_URLENC, /**< POST, url encoded. */ + method_POST_MULTIPART /**< POST, multipart/form-data. */ +} form_method; + /** HTML form. */ struct form { - char *action; /**< Url to submit to. */ - enum { - method_GET, /**< GET, always url encoded. */ - method_POST_URLENC, /**< POST, url encoded. */ - method_POST_MULTIPART /**< POST, multipart/form-data. */ - } method; /**< Method and enctype. */ + char *action; /**< URL to submit to. */ + form_method method; /**< Method and enctype. */ struct form_control *controls; /**< Linked list of controls. */ struct form_control *last_control; /**< Last control in list. */ }; @@ -84,8 +87,8 @@ struct form_control { struct form_option { bool selected; bool initial_selected; - char* value; - char* text; /**< NUL terminated. */ + char *value; + char *text; /**< NUL terminated. */ struct form_option* next; }; @@ -97,9 +100,12 @@ struct form_successful_control { struct form_successful_control *next; /**< Next in linked list. */ }; +struct form *form_new(char *action, form_method method); struct form_control *form_new_control(form_control_type type); void form_add_control(struct form *form, struct form_control *control); void form_free_control(struct form_control *control); +bool form_add_option(struct form_control *control, char *value, char *text, + bool selected); bool form_successful_controls(struct form *form, struct form_control *submit_button, struct form_successful_control **successful_controls); -- cgit v1.2.3