2003-10-25 04:35:49 +04:00
|
|
|
/*
|
2004-08-08 23:13:40 +04:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
2003-10-25 04:35:49 +04:00
|
|
|
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
|
2010-06-04 13:35:08 +04:00
|
|
|
* Copyright 2004 John Tytgat <joty@netsurf-browser.org>
|
2009-02-20 14:39:25 +03:00
|
|
|
* Copyright 2005-9 John-Mark Bell <jmb@netsurf-browser.org>
|
2009-08-14 14:37:33 +04:00
|
|
|
* Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
|
2010-06-04 13:35:08 +04:00
|
|
|
* Copyright 2010 Michael Drake <tlsa@netsurf-browser.org>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-10-25 04:35:49 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Form handling functions (implementation).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2005-06-26 05:55:20 +04:00
|
|
|
#include <ctype.h>
|
2007-03-12 02:48:29 +03:00
|
|
|
#include <limits.h>
|
2005-05-11 06:03:15 +04:00
|
|
|
#include <stdbool.h>
|
2004-03-21 23:07:14 +03:00
|
|
|
#include <stdio.h>
|
2003-10-25 04:35:49 +04:00
|
|
|
#include <string.h>
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/fetch.h"
|
2010-06-04 13:35:08 +04:00
|
|
|
#include "content/hlcache.h"
|
2009-08-14 14:37:33 +04:00
|
|
|
#include "css/css.h"
|
|
|
|
#include "css/utils.h"
|
2010-06-04 13:35:08 +04:00
|
|
|
#include "desktop/browser.h"
|
2009-08-14 14:37:33 +04:00
|
|
|
#include "desktop/gui.h"
|
2010-06-04 13:35:08 +04:00
|
|
|
#include "desktop/mouse.h"
|
2009-08-14 14:37:33 +04:00
|
|
|
#include "desktop/knockout.h"
|
|
|
|
#include "desktop/plot_style.h"
|
|
|
|
#include "desktop/plotters.h"
|
2011-05-10 02:49:17 +04:00
|
|
|
#include "desktop/scrollbar.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "render/box.h"
|
2009-08-14 14:37:33 +04:00
|
|
|
#include "render/font.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "render/form.h"
|
2010-06-04 13:35:08 +04:00
|
|
|
#include "render/html.h"
|
2009-08-14 14:37:33 +04:00
|
|
|
#include "render/layout.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/log.h"
|
2009-08-14 14:37:33 +04:00
|
|
|
#include "utils/messages.h"
|
2010-06-04 13:35:08 +04:00
|
|
|
#include "utils/talloc.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/url.h"
|
|
|
|
#include "utils/utf8.h"
|
|
|
|
#include "utils/utils.h"
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
#define MAX_SELECT_HEIGHT 210
|
|
|
|
#define SELECT_LINE_SPACING 0.2
|
|
|
|
#define SELECT_BORDER_WIDTH 1
|
|
|
|
#define SELECT_SELECTED_COLOUR 0xDB9370
|
|
|
|
|
|
|
|
struct form_select_menu {
|
|
|
|
int line_height;
|
|
|
|
int width, height;
|
2011-05-10 02:49:17 +04:00
|
|
|
struct scrollbar *scrollbar;
|
2009-08-14 14:37:33 +04:00
|
|
|
int f_size;
|
|
|
|
bool scroll_capture;
|
|
|
|
select_menu_redraw_callback callback;
|
|
|
|
void *client_data;
|
|
|
|
struct browser_window *bw;
|
|
|
|
};
|
|
|
|
|
|
|
|
static plot_style_t plot_style_fill_selected = {
|
|
|
|
.fill_type = PLOT_OP_TYPE_SOLID,
|
|
|
|
.fill_colour = SELECT_SELECTED_COLOUR,
|
|
|
|
};
|
|
|
|
|
|
|
|
static plot_font_style_t plot_fstyle_entry = {
|
|
|
|
.family = PLOT_FONT_FAMILY_SANS_SERIF,
|
|
|
|
.weight = 400,
|
|
|
|
.flags = FONTF_NONE,
|
|
|
|
.background = 0xffffff,
|
|
|
|
.foreground = 0x000000,
|
|
|
|
};
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2003-10-26 01:51:15 +04:00
|
|
|
static char *form_textarea_value(struct form_control *textarea);
|
2005-06-26 05:55:20 +04:00
|
|
|
static char *form_acceptable_charset(struct form *form);
|
2007-02-26 03:32:07 +03:00
|
|
|
static char *form_encode_item(const char *item, const char *charset,
|
|
|
|
const char *fallback);
|
2009-08-14 14:37:33 +04:00
|
|
|
static void form_select_menu_clicked(struct form_control *control,
|
|
|
|
int x, int y);
|
|
|
|
static void form_select_menu_scroll_callback(void *client_data,
|
2011-05-10 02:49:17 +04:00
|
|
|
struct scrollbar_msg_data *scrollbar_data);
|
2003-10-26 01:51:15 +04:00
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
/**
|
|
|
|
* Create a struct form.
|
|
|
|
*
|
2009-02-20 14:39:25 +03:00
|
|
|
* \param node DOM node associated with form
|
|
|
|
* \param action URL to submit form to, or NULL for default
|
|
|
|
* \param target Target frame of form, or NULL for default
|
2004-12-13 00:51:01 +03:00
|
|
|
* \param method method and enctype
|
2009-02-20 14:39:25 +03:00
|
|
|
* \param charset acceptable encodings for form submission, or NULL
|
2009-02-20 15:50:34 +03:00
|
|
|
* \param doc_charset encoding of containing document, or NULL
|
2009-02-20 14:39:25 +03:00
|
|
|
* \return a new structure, or NULL on memory exhaustion
|
2004-12-13 00:51:01 +03:00
|
|
|
*/
|
2009-02-20 14:39:25 +03:00
|
|
|
struct form *form_new(void *node, const char *action, const char *target,
|
|
|
|
form_method method, const char *charset,
|
|
|
|
const char *doc_charset)
|
2004-12-13 00:51:01 +03:00
|
|
|
{
|
|
|
|
struct form *form;
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
form = calloc(1, sizeof *form);
|
2004-12-13 00:51:01 +03:00
|
|
|
if (!form)
|
2009-02-20 14:39:25 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
form->action = strdup(action != NULL ? action : "");
|
|
|
|
if (form->action == NULL) {
|
|
|
|
free(form);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
form->target = target != NULL ? strdup(target) : NULL;
|
|
|
|
if (target != NULL && form->target == NULL) {
|
|
|
|
free(form->action);
|
|
|
|
free(form);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
form->method = method;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
|
|
|
form->accept_charsets = charset != NULL ? strdup(charset) : NULL;
|
|
|
|
if (charset != NULL && form->accept_charsets == NULL) {
|
|
|
|
free(form->target);
|
|
|
|
free(form->action);
|
|
|
|
free(form);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-20 15:50:34 +03:00
|
|
|
form->document_charset = doc_charset != NULL ? strdup(doc_charset)
|
|
|
|
: NULL;
|
|
|
|
if (doc_charset && form->document_charset == NULL) {
|
2009-02-20 14:39:25 +03:00
|
|
|
free(form->accept_charsets);
|
|
|
|
free(form->target);
|
|
|
|
free(form->action);
|
|
|
|
free(form);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
form->node = node;
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
return form;
|
|
|
|
}
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
/**
|
|
|
|
* Free a form, and any controls it owns.
|
|
|
|
*
|
|
|
|
* \param form The form to free
|
|
|
|
*
|
|
|
|
* \note There may exist controls attached to box tree nodes which are not
|
|
|
|
* associated with any form. These will leak at present. Ideally, they will
|
|
|
|
* be cleaned up when the box tree is destroyed. As that currently happens
|
|
|
|
* via talloc, this won't happen. These controls are distinguishable, as their
|
|
|
|
* form field will be NULL.
|
|
|
|
*/
|
|
|
|
void form_free(struct form *form)
|
|
|
|
{
|
|
|
|
struct form_control *c, *d;
|
|
|
|
|
|
|
|
for (c = form->controls; c != NULL; c = d) {
|
|
|
|
d = c->next;
|
|
|
|
|
|
|
|
form_free_control(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(form->action);
|
|
|
|
free(form->target);
|
|
|
|
free(form->accept_charsets);
|
|
|
|
free(form->document_charset);
|
|
|
|
|
|
|
|
free(form);
|
|
|
|
}
|
2004-12-13 00:51:01 +03:00
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
/**
|
|
|
|
* Create a struct form_control.
|
|
|
|
*
|
2009-02-20 14:39:25 +03:00
|
|
|
* \param node Associated DOM node
|
2004-05-21 14:25:42 +04:00
|
|
|
* \param type control type
|
2009-02-20 14:39:25 +03:00
|
|
|
* \return a new structure, or NULL on memory exhaustion
|
2004-05-21 14:25:42 +04:00
|
|
|
*/
|
2009-02-20 14:39:25 +03:00
|
|
|
struct form_control *form_new_control(void *node, form_control_type type)
|
2004-05-21 14:25:42 +04:00
|
|
|
{
|
|
|
|
struct form_control *control;
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
control = calloc(1, sizeof *control);
|
|
|
|
if (control == NULL)
|
2004-07-20 00:40:11 +04:00
|
|
|
return NULL;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
|
|
|
control->node = node;
|
2004-05-21 14:25:42 +04:00
|
|
|
control->type = type;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
|
|
|
/* Default max length of input to something insane */
|
2007-03-12 02:48:29 +03:00
|
|
|
control->maxlength = UINT_MAX;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
return control;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* Add a control to the list of controls in a form.
|
2005-06-26 05:55:20 +04:00
|
|
|
*
|
|
|
|
* \param form The form to add the control to
|
|
|
|
* \param control The control to add
|
2003-10-25 04:35:49 +04:00
|
|
|
*/
|
|
|
|
void form_add_control(struct form *form, struct form_control *control)
|
|
|
|
{
|
|
|
|
control->form = form;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
if (form->controls != NULL) {
|
2003-10-25 04:35:49 +04:00
|
|
|
assert(form->last_control);
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
form->last_control->next = control;
|
2004-04-09 03:46:41 +04:00
|
|
|
control->prev = form->last_control;
|
2004-07-20 00:40:11 +04:00
|
|
|
control->next = NULL;
|
2003-10-25 04:35:49 +04:00
|
|
|
form->last_control = control;
|
|
|
|
} else {
|
|
|
|
form->controls = form->last_control = control;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
/**
|
|
|
|
* Free a struct form_control.
|
|
|
|
*
|
|
|
|
* \param control structure to free
|
|
|
|
*/
|
|
|
|
void form_free_control(struct form_control *control)
|
|
|
|
{
|
|
|
|
free(control->name);
|
|
|
|
free(control->value);
|
|
|
|
free(control->initial_value);
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
if (control->type == GADGET_SELECT) {
|
|
|
|
struct form_option *option, *next;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
for (option = control->data.select.items; option;
|
|
|
|
option = next) {
|
|
|
|
next = option->next;
|
|
|
|
free(option->text);
|
|
|
|
free(option->value);
|
|
|
|
free(option);
|
|
|
|
}
|
2009-08-14 14:37:33 +04:00
|
|
|
if (control->data.select.menu != NULL)
|
|
|
|
form_free_select_menu(control);
|
2004-05-21 14:25:42 +04:00
|
|
|
}
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
free(control);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
option = calloc(1, sizeof *option);
|
2004-12-13 00:51:01 +03:00
|
|
|
if (!option)
|
|
|
|
return false;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
option->value = value;
|
|
|
|
option->text = text;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* Identify 'successful' controls.
|
|
|
|
*
|
2007-02-26 03:32:07 +03:00
|
|
|
* All text strings in the successful controls list will be in the charset most
|
|
|
|
* appropriate for submission. Therefore, no utf8_to_* processing should be
|
|
|
|
* performed upon them.
|
|
|
|
*
|
|
|
|
* \todo The chosen charset needs to be made available such that it can be
|
|
|
|
* included in the submission request (e.g. in the fetch's Content-Type header)
|
|
|
|
*
|
2004-08-08 23:13:40 +04:00
|
|
|
* \param form form to search for successful controls
|
|
|
|
* \param submit_button control used to submit the form, if any
|
2006-01-02 16:20:01 +03:00
|
|
|
* \param successful_controls updated to point to linked list of
|
2010-03-28 16:56:39 +04:00
|
|
|
* fetch_multipart_data, 0 if no controls
|
2004-08-08 23:13:40 +04:00
|
|
|
* \return true on success, false on memory exhaustion
|
|
|
|
*
|
2003-10-25 04:35:49 +04:00
|
|
|
* See HTML 4.01 section 17.13.2.
|
|
|
|
*/
|
2004-08-08 23:13:40 +04:00
|
|
|
bool form_successful_controls(struct form *form,
|
|
|
|
struct form_control *submit_button,
|
2010-03-28 16:56:39 +04:00
|
|
|
struct fetch_multipart_data **successful_controls)
|
2003-10-25 04:35:49 +04:00
|
|
|
{
|
|
|
|
struct form_control *control;
|
|
|
|
struct form_option *option;
|
2010-03-28 16:56:39 +04:00
|
|
|
struct fetch_multipart_data sentinel, *last_success, *success_new;
|
2009-03-02 22:47:27 +03:00
|
|
|
char *value = NULL;
|
2005-05-11 06:03:15 +04:00
|
|
|
bool had_submit = false;
|
2007-02-26 03:32:07 +03:00
|
|
|
char *charset;
|
2004-07-20 00:40:11 +04:00
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
last_success = &sentinel;
|
2011-02-25 22:57:23 +03:00
|
|
|
sentinel.next = NULL;
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2007-02-26 03:32:07 +03:00
|
|
|
charset = form_acceptable_charset(form);
|
|
|
|
if (!charset)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#define ENCODE_ITEM(i) form_encode_item((i), charset, form->document_charset)
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
for (control = form->controls; control; control = control->next) {
|
|
|
|
/* ignore disabled controls */
|
|
|
|
if (control->disabled)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore controls with no name */
|
|
|
|
if (!control->name)
|
|
|
|
continue;
|
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
switch (control->type) {
|
|
|
|
case GADGET_HIDDEN:
|
|
|
|
case GADGET_TEXTBOX:
|
|
|
|
case GADGET_PASSWORD:
|
2006-02-04 17:40:01 +03:00
|
|
|
if (control->value)
|
2007-02-26 03:32:07 +03:00
|
|
|
value = ENCODE_ITEM(control->value);
|
2006-02-04 17:40:01 +03:00
|
|
|
else
|
2007-02-26 03:32:07 +03:00
|
|
|
value = ENCODE_ITEM("");
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!value) {
|
|
|
|
LOG(("failed to duplicate value"
|
|
|
|
"'%s' for control %s",
|
|
|
|
control->value,
|
|
|
|
control->name));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_RADIO:
|
|
|
|
case GADGET_CHECKBOX:
|
|
|
|
/* ignore checkboxes and radio buttons which
|
2004-08-08 23:13:40 +04:00
|
|
|
* aren't selected */
|
|
|
|
if (!control->selected)
|
|
|
|
continue;
|
2005-05-11 05:34:41 +04:00
|
|
|
if (control->value)
|
2007-02-26 03:32:07 +03:00
|
|
|
value = ENCODE_ITEM(control->value);
|
2005-05-11 05:34:41 +04:00
|
|
|
else
|
2007-02-26 03:32:07 +03:00
|
|
|
value = ENCODE_ITEM("on");
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!value) {
|
2005-05-11 05:34:41 +04:00
|
|
|
LOG(("failed to duplicate"
|
|
|
|
"value '%s' for"
|
|
|
|
"control %s",
|
|
|
|
control->value,
|
|
|
|
control->name));
|
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_SELECT:
|
|
|
|
/* select */
|
|
|
|
for (option = control->data.select.items;
|
|
|
|
option != NULL;
|
|
|
|
option = option->next) {
|
2004-08-08 23:13:40 +04:00
|
|
|
if (!option->selected)
|
|
|
|
continue;
|
2007-02-26 03:32:07 +03:00
|
|
|
success_new =
|
|
|
|
malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->file = false;
|
2007-02-26 03:32:07 +03:00
|
|
|
success_new->name =
|
|
|
|
ENCODE_ITEM(control->name);
|
|
|
|
success_new->value =
|
|
|
|
ENCODE_ITEM(option->value);
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->next = NULL;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
|
|
|
if (!success_new->name ||
|
2004-08-18 05:36:39 +04:00
|
|
|
!success_new->value) {
|
|
|
|
LOG(("strdup failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_TEXTAREA:
|
2007-02-26 03:32:07 +03:00
|
|
|
{
|
|
|
|
char *v2;
|
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
/* textarea */
|
2004-08-08 23:13:40 +04:00
|
|
|
value = form_textarea_value(control);
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!value) {
|
|
|
|
LOG(("failed handling textarea"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-08-08 23:13:40 +04:00
|
|
|
if (value[0] == 0) {
|
|
|
|
free(value);
|
|
|
|
continue;
|
|
|
|
}
|
2007-02-26 03:32:07 +03:00
|
|
|
|
|
|
|
v2 = ENCODE_ITEM(value);
|
|
|
|
if (!v2) {
|
|
|
|
LOG(("failed handling textarea"));
|
|
|
|
free(value);
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(value);
|
|
|
|
value = v2;
|
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_IMAGE: {
|
|
|
|
/* image */
|
2008-08-12 04:01:46 +04:00
|
|
|
size_t len;
|
2009-02-20 14:39:25 +03:00
|
|
|
char *name;
|
2008-08-12 04:01:46 +04:00
|
|
|
|
2005-09-01 00:29:11 +04:00
|
|
|
if (control != submit_button)
|
|
|
|
/* only the activated submit button
|
|
|
|
* is successful */
|
|
|
|
continue;
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
name = ENCODE_ITEM(control->name);
|
|
|
|
if (name == NULL)
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
len = strlen(name) + 3;
|
2004-07-20 00:40:11 +04:00
|
|
|
|
|
|
|
/* x */
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
2009-02-20 14:39:25 +03:00
|
|
|
free(name);
|
2004-08-18 05:36:39 +04:00
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->file = false;
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->name = malloc(len);
|
|
|
|
success_new->value = malloc(20);
|
|
|
|
if (!success_new->name ||
|
|
|
|
!success_new->value) {
|
|
|
|
free(success_new->name);
|
|
|
|
free(success_new->value);
|
|
|
|
free(success_new);
|
2009-02-20 14:39:25 +03:00
|
|
|
free(name);
|
2004-08-18 05:36:39 +04:00
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
|
|
|
}
|
2009-02-20 14:39:25 +03:00
|
|
|
sprintf(success_new->name, "%s.x", name);
|
2004-08-08 23:13:40 +04:00
|
|
|
sprintf(success_new->value, "%i",
|
|
|
|
control->data.image.mx);
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->next = 0;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
|
|
|
|
|
|
|
/* y */
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
2009-02-20 14:39:25 +03:00
|
|
|
free(name);
|
2004-08-18 05:36:39 +04:00
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->file = false;
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->name = malloc(len);
|
|
|
|
success_new->value = malloc(20);
|
|
|
|
if (!success_new->name ||
|
|
|
|
!success_new->value) {
|
|
|
|
free(success_new->name);
|
|
|
|
free(success_new->value);
|
|
|
|
free(success_new);
|
2009-02-20 14:39:25 +03:00
|
|
|
free(name);
|
2004-08-18 05:36:39 +04:00
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
|
|
|
}
|
2009-02-20 14:39:25 +03:00
|
|
|
sprintf(success_new->name, "%s.y", name);
|
2004-08-08 23:13:40 +04:00
|
|
|
sprintf(success_new->value, "%i",
|
|
|
|
control->data.image.my);
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->next = 0;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
free(name);
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
}
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
case GADGET_SUBMIT:
|
2005-05-11 06:03:15 +04:00
|
|
|
if (!submit_button && !had_submit)
|
|
|
|
/* no submit button specified, so
|
|
|
|
* use first declared in form */
|
|
|
|
had_submit = true;
|
|
|
|
else if (control != submit_button)
|
|
|
|
/* only the activated submit button
|
|
|
|
* is successful */
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2006-02-04 17:40:01 +03:00
|
|
|
if (control->value)
|
2007-02-26 03:32:07 +03:00
|
|
|
value = ENCODE_ITEM(control->value);
|
2006-02-04 17:40:01 +03:00
|
|
|
else
|
2007-02-26 03:32:07 +03:00
|
|
|
value = ENCODE_ITEM("");
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!value) {
|
|
|
|
LOG(("failed to duplicate value"
|
|
|
|
"'%s' for control %s",
|
|
|
|
control->value,
|
|
|
|
control->name));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_RESET:
|
|
|
|
/* ignore reset */
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_FILE:
|
|
|
|
/* file */
|
2007-01-31 02:19:21 +03:00
|
|
|
/* Handling of blank file entries is
|
|
|
|
* implementation defined - we're perfectly
|
|
|
|
* within our rights to treat it as an
|
|
|
|
* unsuccessful control. Unfortunately, every
|
|
|
|
* other browser submits the field with
|
|
|
|
* a blank filename and no content. So,
|
|
|
|
* that's what we have to do, too.
|
|
|
|
*/
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->file = true;
|
2007-02-26 03:32:07 +03:00
|
|
|
success_new->name = ENCODE_ITEM(control->name);
|
2009-02-20 14:39:25 +03:00
|
|
|
success_new->value =
|
|
|
|
ENCODE_ITEM(control->value ?
|
2007-01-31 02:19:21 +03:00
|
|
|
control->value : "");
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->next = 0;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new->name ||
|
|
|
|
!success_new->value) {
|
|
|
|
LOG(("strdup failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
case GADGET_BUTTON:
|
|
|
|
/* Ignore it */
|
2009-02-20 15:55:50 +03:00
|
|
|
continue;
|
2009-02-20 14:39:25 +03:00
|
|
|
break;
|
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
2004-03-21 23:07:14 +03:00
|
|
|
}
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->file = false;
|
2007-02-26 03:32:07 +03:00
|
|
|
success_new->name = ENCODE_ITEM(control->name);
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->value = value;
|
|
|
|
success_new->next = NULL;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new->name) {
|
|
|
|
LOG(("failed to duplicate name '%s'",
|
|
|
|
control->name));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
*successful_controls = sentinel.next;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
warn_user("NoMemory", 0);
|
2010-03-28 16:56:39 +04:00
|
|
|
fetch_multipart_data_destroy(sentinel.next);
|
2004-08-08 23:13:40 +04:00
|
|
|
return false;
|
2007-02-26 03:32:07 +03:00
|
|
|
|
|
|
|
#undef ENCODE_ITEM
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-26 01:51:15 +04:00
|
|
|
/**
|
|
|
|
* Find the value for a textarea control.
|
2004-08-08 23:13:40 +04:00
|
|
|
*
|
|
|
|
* \param textarea control of type GADGET_TEXTAREA
|
|
|
|
* \return the value as a UTF-8 string on heap, or 0 on memory exhaustion
|
2003-10-26 01:51:15 +04:00
|
|
|
*/
|
|
|
|
char *form_textarea_value(struct form_control *textarea)
|
|
|
|
{
|
2004-08-08 23:13:40 +04:00
|
|
|
unsigned int len = 0;
|
2003-10-26 01:51:15 +04:00
|
|
|
char *value, *s;
|
2004-08-08 23:13:40 +04:00
|
|
|
struct box *text_box;
|
2003-10-26 01:51:15 +04:00
|
|
|
|
2011-02-19 17:59:30 +03:00
|
|
|
/* Textarea may have no associated box if styled with display: none */
|
|
|
|
if (textarea->box == NULL) {
|
|
|
|
/* Return the empty string: caller treats this as a
|
|
|
|
* non-successful control. */
|
|
|
|
return strdup("");
|
|
|
|
}
|
|
|
|
|
2003-10-26 01:51:15 +04:00
|
|
|
/* find required length */
|
2004-08-08 23:13:40 +04:00
|
|
|
for (text_box = textarea->box->children->children; text_box;
|
|
|
|
text_box = text_box->next) {
|
2005-05-26 03:06:26 +04:00
|
|
|
if (text_box->type == BOX_TEXT)
|
2003-10-26 01:51:15 +04:00
|
|
|
len += text_box->length + 1;
|
2004-08-08 23:13:40 +04:00
|
|
|
else /* BOX_BR */
|
|
|
|
len += 2;
|
2003-10-26 01:51:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* construct value */
|
2004-08-08 23:13:40 +04:00
|
|
|
s = value = malloc(len + 1);
|
|
|
|
if (!s)
|
2011-02-19 17:59:30 +03:00
|
|
|
return NULL;
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
for (text_box = textarea->box->children->children; text_box;
|
|
|
|
text_box = text_box->next) {
|
2005-05-26 03:06:26 +04:00
|
|
|
if (text_box->type == BOX_TEXT) {
|
2003-10-26 01:51:15 +04:00
|
|
|
strncpy(s, text_box->text, text_box->length);
|
|
|
|
s += text_box->length;
|
2005-05-16 04:55:43 +04:00
|
|
|
if (text_box->next && text_box->next->type != BOX_BR)
|
|
|
|
/* only add space if this isn't
|
|
|
|
* the last box on a line (or in the area) */
|
2005-04-16 23:34:30 +04:00
|
|
|
*s++ = ' ';
|
2004-08-08 23:13:40 +04:00
|
|
|
} else { /* BOX_BR */
|
|
|
|
*s++ = '\r';
|
|
|
|
*s++ = '\n';
|
2003-10-26 01:51:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* Encode controls using application/x-www-form-urlencoded.
|
2004-08-08 23:13:40 +04:00
|
|
|
*
|
2005-04-16 09:09:33 +04:00
|
|
|
* \param form form to which successful controls relate
|
2010-03-28 16:56:39 +04:00
|
|
|
* \param control linked list of fetch_multipart_data
|
2004-08-08 23:13:40 +04:00
|
|
|
* \return URL-encoded form, or 0 on memory exhaustion
|
2003-10-25 04:35:49 +04:00
|
|
|
*/
|
|
|
|
|
2005-04-16 09:09:33 +04:00
|
|
|
char *form_url_encode(struct form *form,
|
2010-03-28 16:56:39 +04:00
|
|
|
struct fetch_multipart_data *control)
|
2003-10-25 04:35:49 +04:00
|
|
|
{
|
2007-02-26 03:32:07 +03:00
|
|
|
char *name, *value;
|
2004-08-08 23:13:40 +04:00
|
|
|
char *s = malloc(1), *s2;
|
2003-10-26 01:51:15 +04:00
|
|
|
unsigned int len = 0, len1;
|
2005-06-27 02:18:37 +04:00
|
|
|
url_func_result url_err;
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
s[0] = 0;
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
for (; control; control = control->next) {
|
2008-04-02 04:43:51 +04:00
|
|
|
url_err = url_escape(control->name, 0, true, NULL, &name);
|
2005-06-27 02:18:37 +04:00
|
|
|
if (url_err == URL_FUNC_NOMEM) {
|
|
|
|
free(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(url_err == URL_FUNC_OK);
|
|
|
|
|
2008-04-02 04:43:51 +04:00
|
|
|
url_err = url_escape(control->value, 0, true, NULL, &value);
|
2005-06-27 02:18:37 +04:00
|
|
|
if (url_err == URL_FUNC_NOMEM) {
|
|
|
|
free(name);
|
2005-04-16 09:09:33 +04:00
|
|
|
free(s);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-27 02:18:37 +04:00
|
|
|
|
|
|
|
assert(url_err == URL_FUNC_OK);
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
len1 = len + strlen(name) + strlen(value) + 2;
|
2004-08-08 23:13:40 +04:00
|
|
|
s2 = realloc(s, len1 + 1);
|
|
|
|
if (!s2) {
|
2005-06-27 02:18:37 +04:00
|
|
|
free(value);
|
|
|
|
free(name);
|
2004-08-08 23:13:40 +04:00
|
|
|
free(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s = s2;
|
2003-10-25 04:35:49 +04:00
|
|
|
sprintf(s + len, "%s=%s&", name, value);
|
|
|
|
len = len1;
|
2005-06-27 02:18:37 +04:00
|
|
|
free(name);
|
|
|
|
free(value);
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
2005-06-26 05:55:20 +04:00
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
if (len)
|
|
|
|
s[len - 1] = 0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-06-26 05:55:20 +04:00
|
|
|
/**
|
|
|
|
* Find an acceptable character set encoding with which to submit the form
|
|
|
|
*
|
|
|
|
* \param form The form
|
|
|
|
* \return Pointer to charset name (on heap, caller should free) or NULL
|
|
|
|
*/
|
|
|
|
char *form_acceptable_charset(struct form *form)
|
|
|
|
{
|
|
|
|
char *temp, *c;
|
|
|
|
|
|
|
|
if (!form)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!form->accept_charsets) {
|
|
|
|
/* no accept-charsets attribute for this form */
|
|
|
|
if (form->document_charset)
|
|
|
|
/* document charset present, so use it */
|
|
|
|
return strdup(form->document_charset);
|
|
|
|
else
|
|
|
|
/* no document charset, so default to 8859-1 */
|
|
|
|
return strdup("ISO-8859-1");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make temporary copy of accept-charsets attribute */
|
|
|
|
temp = strdup(form->accept_charsets);
|
|
|
|
if (!temp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* make it upper case */
|
|
|
|
for (c = temp; *c; c++)
|
2005-07-18 18:22:26 +04:00
|
|
|
*c = toupper(*c);
|
2005-06-26 05:55:20 +04:00
|
|
|
|
|
|
|
/* is UTF-8 specified? */
|
|
|
|
c = strstr(temp, "UTF-8");
|
|
|
|
if (c) {
|
|
|
|
free(temp);
|
|
|
|
return strdup("UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dispense with temporary copy */
|
|
|
|
free(temp);
|
|
|
|
|
|
|
|
/* according to RFC2070, the accept-charsets attribute of the
|
|
|
|
* form element contains a space and/or comma separated list */
|
|
|
|
c = form->accept_charsets;
|
|
|
|
|
|
|
|
/* What would be an improvement would be to choose an encoding
|
|
|
|
* acceptable to the server which covers as much of the input
|
|
|
|
* values as possible. Additionally, we need to handle the case
|
|
|
|
* where none of the acceptable encodings cover all the textual
|
|
|
|
* input values.
|
|
|
|
* For now, we just extract the first element of the charset list
|
|
|
|
*/
|
|
|
|
while (*c && !isspace(*c)) {
|
|
|
|
if (*c == ',')
|
|
|
|
break;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strndup(form->accept_charsets, c - form->accept_charsets);
|
|
|
|
}
|
2007-02-26 03:32:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a string from UTF-8 to the specified charset
|
|
|
|
* As a final fallback, this will attempt to convert to ISO-8859-1.
|
|
|
|
*
|
|
|
|
* \todo Return charset used?
|
|
|
|
*
|
|
|
|
* \param item String to convert
|
|
|
|
* \param charset Destination charset
|
|
|
|
* \param fallback Fallback charset (may be NULL),
|
|
|
|
* used iff converting to charset fails
|
|
|
|
* \return Pointer to converted string (on heap, caller frees), or NULL
|
|
|
|
*/
|
|
|
|
char *form_encode_item(const char *item, const char *charset,
|
|
|
|
const char *fallback)
|
|
|
|
{
|
|
|
|
utf8_convert_ret err;
|
|
|
|
char *ret = NULL;
|
2007-03-11 16:58:15 +03:00
|
|
|
char cset[256];
|
2007-02-26 03:32:07 +03:00
|
|
|
|
|
|
|
if (!item || !charset)
|
|
|
|
return NULL;
|
|
|
|
|
2007-03-11 16:58:15 +03:00
|
|
|
snprintf(cset, sizeof cset, "%s//TRANSLIT", charset);
|
2007-02-26 03:32:07 +03:00
|
|
|
|
2007-03-11 16:58:15 +03:00
|
|
|
err = utf8_to_enc(item, cset, 0, &ret);
|
2007-02-26 03:32:07 +03:00
|
|
|
if (err == UTF8_CONVERT_BADENC) {
|
2007-09-27 17:57:29 +04:00
|
|
|
/* charset not understood, try without transliteration */
|
|
|
|
snprintf(cset, sizeof cset, "%s", charset);
|
|
|
|
err = utf8_to_enc(item, cset, 0, &ret);
|
|
|
|
|
|
|
|
if (err == UTF8_CONVERT_BADENC) {
|
|
|
|
/* nope, try fallback charset (if any) */
|
|
|
|
if (fallback) {
|
|
|
|
snprintf(cset, sizeof cset,
|
|
|
|
"%s//TRANSLIT", fallback);
|
|
|
|
err = utf8_to_enc(item, cset, 0, &ret);
|
|
|
|
|
|
|
|
if (err == UTF8_CONVERT_BADENC) {
|
|
|
|
/* and without transliteration */
|
|
|
|
snprintf(cset, sizeof cset,
|
|
|
|
"%s", fallback);
|
|
|
|
err = utf8_to_enc(item, cset, 0, &ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == UTF8_CONVERT_BADENC) {
|
|
|
|
/* that also failed, use 8859-1 */
|
|
|
|
err = utf8_to_enc(item, "ISO-8859-1//TRANSLIT",
|
|
|
|
0, &ret);
|
|
|
|
if (err == UTF8_CONVERT_BADENC) {
|
|
|
|
/* and without transliteration */
|
|
|
|
err = utf8_to_enc(item, "ISO-8859-1",
|
|
|
|
0, &ret);
|
|
|
|
}
|
|
|
|
}
|
2007-03-11 16:58:15 +03:00
|
|
|
}
|
2007-02-26 03:32:07 +03:00
|
|
|
}
|
|
|
|
if (err == UTF8_CONVERT_NOMEM) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-09-27 17:57:29 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
/**
|
|
|
|
* Open a select menu for a select form control, creating it if necessary.
|
|
|
|
*
|
|
|
|
* \param client_data data passed to the redraw callback
|
|
|
|
* \param control the select form control for which the menu is being
|
|
|
|
* opened
|
|
|
|
* \param callback redraw callback for the select menu
|
|
|
|
* \param bw the browser window in which the select menu is being
|
|
|
|
* opened
|
|
|
|
* \return false on memory exhaustion, true otherwise
|
|
|
|
*/
|
|
|
|
bool form_open_select_menu(void *client_data,
|
|
|
|
struct form_control *control,
|
|
|
|
select_menu_redraw_callback callback,
|
|
|
|
struct browser_window *bw)
|
|
|
|
{
|
|
|
|
int i, line_height_with_spacing, scroll;
|
|
|
|
struct form_option *option;
|
|
|
|
struct box *box;
|
|
|
|
plot_font_style_t fstyle;
|
|
|
|
int total_height;
|
|
|
|
struct form_select_menu *menu;
|
|
|
|
|
|
|
|
|
|
|
|
/* if the menu is opened for the first time */
|
|
|
|
if (control->data.select.menu == NULL) {
|
|
|
|
|
|
|
|
menu = calloc(1, sizeof (struct form_select_menu));
|
|
|
|
if (menu == NULL) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
control->data.select.menu = menu;
|
|
|
|
|
|
|
|
box = control->box;
|
|
|
|
|
|
|
|
menu->width = box->width +
|
|
|
|
box->border[RIGHT].width +
|
|
|
|
box->border[LEFT].width +
|
|
|
|
box->padding[RIGHT] + box->padding[LEFT];
|
|
|
|
|
|
|
|
font_plot_style_from_css(control->box->style,
|
|
|
|
&fstyle);
|
|
|
|
menu->f_size = fstyle.size;
|
|
|
|
|
2011-03-12 20:46:11 +03:00
|
|
|
menu->line_height = FIXTOINT(FDIV((FMUL(FLTTOFIX(1.2), FMUL(nscss_screen_dpi, INTTOFIX((fstyle.size / FONT_SIZE_SCALE))))), F_72));
|
2009-08-14 14:37:33 +04:00
|
|
|
|
|
|
|
line_height_with_spacing = menu->line_height +
|
|
|
|
menu->line_height *
|
|
|
|
SELECT_LINE_SPACING;
|
|
|
|
|
|
|
|
total_height = control->data.select.num_items *
|
|
|
|
line_height_with_spacing;
|
|
|
|
menu->height = total_height;
|
|
|
|
|
|
|
|
scroll = 0;
|
|
|
|
if (menu->height > MAX_SELECT_HEIGHT) {
|
|
|
|
|
|
|
|
menu->height = MAX_SELECT_HEIGHT;
|
|
|
|
|
|
|
|
if (control->data.select.num_selected > 0) {
|
|
|
|
i = 0;
|
|
|
|
option = control->data.select.items;
|
|
|
|
while (!option->selected) {
|
|
|
|
option = option->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((i + 1) * line_height_with_spacing >
|
|
|
|
MAX_SELECT_HEIGHT)
|
|
|
|
scroll = (i + 1) *
|
|
|
|
line_height_with_spacing
|
|
|
|
- MAX_SELECT_HEIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu->client_data = client_data;
|
|
|
|
menu->callback = callback;
|
2011-05-10 02:49:17 +04:00
|
|
|
if (!scrollbar_create(false,
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->height,
|
|
|
|
total_height,
|
|
|
|
menu->height,
|
|
|
|
control,
|
|
|
|
form_select_menu_scroll_callback,
|
2011-05-10 02:49:17 +04:00
|
|
|
&(menu->scrollbar))) {
|
2009-08-14 14:37:33 +04:00
|
|
|
free(menu);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
menu->bw = bw;
|
|
|
|
}
|
|
|
|
else menu = control->data.select.menu;
|
|
|
|
|
|
|
|
menu->callback(client_data, 0, 0, menu->width, menu->height);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a select menu and free allocated memory.
|
|
|
|
*
|
|
|
|
* \param control the select form control owning the select menu being
|
|
|
|
* destroyed
|
|
|
|
*/
|
|
|
|
void form_free_select_menu(struct form_control *control)
|
|
|
|
{
|
2011-05-10 02:49:17 +04:00
|
|
|
if (control->data.select.menu->scrollbar != NULL)
|
|
|
|
scrollbar_destroy(control->data.select.menu->scrollbar);
|
2009-08-14 14:37:33 +04:00
|
|
|
free(control->data.select.menu);
|
|
|
|
control->data.select.menu = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redraw an opened select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu being redrawn
|
|
|
|
* \param x the X coordinate to draw the menu at
|
|
|
|
* \param x the Y coordinate to draw the menu at
|
|
|
|
* \param scale current redraw scale
|
|
|
|
* \param clip_x0 minimum x of clipping rectangle
|
|
|
|
* \param clip_y0 minimum y of clipping rectangle
|
|
|
|
* \param clip_x1 maximum x of clipping rectangle
|
|
|
|
* \param clip_y1 maximum y of clipping rectangle
|
|
|
|
* \return true on success, false otherwise
|
|
|
|
*/
|
|
|
|
bool form_redraw_select_menu(struct form_control *control, int x, int y,
|
2011-02-15 01:05:39 +03:00
|
|
|
float scale, const struct rect *clip)
|
2009-08-14 14:37:33 +04:00
|
|
|
{
|
|
|
|
struct box *box;
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
|
|
|
struct form_option *option;
|
|
|
|
int line_height, line_height_with_spacing;
|
|
|
|
int width, height;
|
|
|
|
int x0, y0, x1, scrollbar_x, y1, y2, y3;
|
|
|
|
int item_y;
|
|
|
|
int text_pos_offset, text_x;
|
|
|
|
int scrollbar_width = SCROLLBAR_WIDTH;
|
|
|
|
int i;
|
|
|
|
int scroll;
|
|
|
|
int x_cp, y_cp;
|
2011-02-15 01:05:39 +03:00
|
|
|
struct rect r;
|
2009-08-14 14:37:33 +04:00
|
|
|
|
|
|
|
box = control->box;
|
|
|
|
|
|
|
|
x_cp = x;
|
|
|
|
y_cp = y;
|
|
|
|
width = menu->width;
|
|
|
|
height = menu->height;
|
|
|
|
line_height = menu->line_height;
|
|
|
|
|
|
|
|
line_height_with_spacing = line_height +
|
|
|
|
line_height * SELECT_LINE_SPACING;
|
2011-05-10 02:49:17 +04:00
|
|
|
scroll = scrollbar_get_offset(menu->scrollbar);
|
2009-08-14 14:37:33 +04:00
|
|
|
|
|
|
|
if (scale != 1.0) {
|
|
|
|
x *= scale;
|
|
|
|
y *= scale;
|
|
|
|
width *= scale;
|
|
|
|
height *= scale;
|
|
|
|
scrollbar_width *= scale;
|
|
|
|
|
|
|
|
i = scroll / line_height_with_spacing;
|
|
|
|
scroll -= i * line_height_with_spacing;
|
|
|
|
line_height *= scale;
|
|
|
|
line_height_with_spacing *= scale;
|
|
|
|
scroll *= scale;
|
|
|
|
scroll += i * line_height_with_spacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
x0 = x;
|
|
|
|
y0 = y;
|
|
|
|
x1 = x + width - 1;
|
|
|
|
y1 = y + height - 1;
|
|
|
|
scrollbar_x = x1 - scrollbar_width;
|
2011-02-15 01:05:39 +03:00
|
|
|
|
|
|
|
r.x0 = x0;
|
|
|
|
r.y0 = y0;
|
|
|
|
r.x1 = x1 + 1;
|
|
|
|
r.y1 = y1 + 1;
|
|
|
|
if (!plot.clip(&r))
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
|
|
|
if (!plot.rectangle(x0, y0, x1, y1 ,plot_style_stroke_darkwbasec))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
x0 = x0 + SELECT_BORDER_WIDTH;
|
|
|
|
y0 = y0 + SELECT_BORDER_WIDTH;
|
|
|
|
x1 = x1 - SELECT_BORDER_WIDTH;
|
|
|
|
y1 = y1 - SELECT_BORDER_WIDTH;
|
|
|
|
height = height - 2 * SELECT_BORDER_WIDTH;
|
|
|
|
|
2011-02-15 01:05:39 +03:00
|
|
|
r.x0 = x0;
|
|
|
|
r.y0 = y0;
|
|
|
|
r.x1 = x1 + 1;
|
|
|
|
r.y1 = y1 + 1;
|
|
|
|
if (!plot.clip(&r))
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
|
|
|
if (!plot.rectangle(x0, y0, x1 + 1, y1 + 1,
|
|
|
|
plot_style_fill_lightwbasec))
|
|
|
|
return false;
|
|
|
|
option = control->data.select.items;
|
|
|
|
item_y = line_height_with_spacing;
|
|
|
|
|
|
|
|
while (item_y < scroll) {
|
|
|
|
option = option->next;
|
|
|
|
item_y += line_height_with_spacing;
|
|
|
|
}
|
|
|
|
item_y -= line_height_with_spacing;
|
|
|
|
text_pos_offset = y - scroll +
|
|
|
|
(int) (line_height * (0.75 + SELECT_LINE_SPACING));
|
|
|
|
text_x = x + (box->border[LEFT].width + box->padding[LEFT]) * scale;
|
|
|
|
|
|
|
|
plot_fstyle_entry.size = menu->f_size;
|
|
|
|
|
|
|
|
while (option && item_y - scroll < height) {
|
|
|
|
|
|
|
|
if (option->selected) {
|
|
|
|
y2 = y + item_y - scroll;
|
|
|
|
y3 = y + item_y + line_height_with_spacing - scroll;
|
|
|
|
if (!plot.rectangle(x0, (y0 > y2 ? y0 : y2),
|
|
|
|
scrollbar_x + 1,
|
|
|
|
(y3 < y1 + 1 ? y3 : y1 + 1),
|
|
|
|
&plot_style_fill_selected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
y2 = text_pos_offset + item_y;
|
|
|
|
if (!plot.text(text_x, y2, option->text,
|
|
|
|
strlen(option->text), &plot_fstyle_entry))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
item_y += line_height_with_spacing;
|
|
|
|
option = option->next;
|
|
|
|
}
|
|
|
|
|
2011-05-10 02:49:17 +04:00
|
|
|
if (!scrollbar_redraw(menu->scrollbar,
|
2009-08-14 14:37:33 +04:00
|
|
|
x_cp + menu->width - SCROLLBAR_WIDTH,
|
|
|
|
y_cp,
|
2011-02-15 01:05:39 +03:00
|
|
|
clip, scale))
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether a clipping rectangle is completely contained in the
|
|
|
|
* select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu to check the clipping rectangle for
|
|
|
|
* \param scale the current browser window scale
|
|
|
|
* \param clip_x0 minimum x of clipping rectangle
|
|
|
|
* \param clip_y0 minimum y of clipping rectangle
|
|
|
|
* \param clip_x1 maximum x of clipping rectangle
|
|
|
|
* \param clip_y1 maximum y of clipping rectangle
|
|
|
|
* \return true if inside false otherwise
|
|
|
|
*/
|
|
|
|
bool form_clip_inside_select_menu(struct form_control *control, float scale,
|
2011-02-15 01:05:39 +03:00
|
|
|
const struct rect *clip)
|
2009-08-14 14:37:33 +04:00
|
|
|
{
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
|
|
|
int width, height;
|
|
|
|
|
|
|
|
|
|
|
|
width = menu->width;
|
|
|
|
height = menu->height;
|
|
|
|
|
|
|
|
if (scale != 1.0) {
|
|
|
|
width *= scale;
|
|
|
|
height *= scale;
|
|
|
|
}
|
|
|
|
|
2011-02-15 01:05:39 +03:00
|
|
|
if (clip->x0 >= 0 && clip->x1 <= width &&
|
|
|
|
clip->y0 >= 0 && clip->y1 <= height)
|
2009-08-14 14:37:33 +04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a click on the area of the currently opened select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu which received the click
|
|
|
|
* \param x X coordinate of click
|
|
|
|
* \param y Y coordinate of click
|
|
|
|
*/
|
|
|
|
void form_select_menu_clicked(struct form_control *control, int x, int y)
|
|
|
|
{
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
|
|
|
struct form_option *option;
|
|
|
|
int line_height, line_height_with_spacing;
|
|
|
|
int item_bottom_y;
|
|
|
|
int scroll, i;
|
|
|
|
|
2011-05-10 02:49:17 +04:00
|
|
|
scroll = scrollbar_get_offset(menu->scrollbar);
|
2009-08-14 14:37:33 +04:00
|
|
|
|
|
|
|
line_height = menu->line_height;
|
|
|
|
line_height_with_spacing = line_height +
|
|
|
|
line_height * SELECT_LINE_SPACING;
|
|
|
|
|
|
|
|
option = control->data.select.items;
|
|
|
|
item_bottom_y = line_height_with_spacing;
|
|
|
|
i = 0;
|
|
|
|
while (option && item_bottom_y < scroll + y) {
|
|
|
|
item_bottom_y += line_height_with_spacing;
|
|
|
|
option = option->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option != NULL)
|
2010-06-04 13:35:08 +04:00
|
|
|
form_select_process_selection(menu->bw->current_content,
|
|
|
|
control, i);
|
2009-08-14 14:37:33 +04:00
|
|
|
|
|
|
|
menu->callback(menu->client_data, 0, 0, menu->width, menu->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse action for the currently opened select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu which received the mouse action
|
|
|
|
* \param mouse current mouse state
|
|
|
|
* \param x X coordinate of click
|
|
|
|
* \param y Y coordinate of click
|
|
|
|
* \return text for the browser status bar or NULL if the menu has
|
|
|
|
* to be closed
|
|
|
|
*/
|
|
|
|
const char *form_select_mouse_action(struct form_control *control,
|
|
|
|
browser_mouse_state mouse, int x, int y)
|
|
|
|
{
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
|
|
|
int x0, y0, x1, y1, scrollbar_x;
|
|
|
|
const char *status = NULL;
|
|
|
|
bool multiple = control->data.select.multiple;
|
|
|
|
|
|
|
|
x0 = 0;
|
|
|
|
y0 = 0;
|
|
|
|
x1 = menu->width;
|
|
|
|
y1 = menu->height;
|
|
|
|
scrollbar_x = x1 - SCROLLBAR_WIDTH;
|
|
|
|
|
|
|
|
if (menu->scroll_capture ||
|
|
|
|
(x > scrollbar_x && x < x1 && y > y0 && y < y1)) {
|
|
|
|
/* The scroll is currently capturing all events or the mouse
|
|
|
|
* event is taking place on the scrollbar widget area
|
|
|
|
*/
|
|
|
|
x -= scrollbar_x;
|
2011-05-10 02:49:17 +04:00
|
|
|
return scrollbar_mouse_action(menu->scrollbar,
|
2009-08-14 14:37:33 +04:00
|
|
|
mouse, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (x > x0 && x < scrollbar_x && y > y0 && y < y1) {
|
|
|
|
/* over option area */
|
|
|
|
|
|
|
|
if (mouse & (BROWSER_MOUSE_CLICK_1 | BROWSER_MOUSE_CLICK_2))
|
|
|
|
/* button 1 or 2 click */
|
|
|
|
form_select_menu_clicked(control, x, y);
|
|
|
|
|
|
|
|
if (!(mouse & BROWSER_MOUSE_CLICK_1 && !multiple))
|
|
|
|
/* anything but a button 1 click over a single select
|
|
|
|
menu */
|
|
|
|
status = messages_get(control->data.select.multiple ?
|
|
|
|
"SelectMClick" : "SelectClick");
|
|
|
|
|
|
|
|
} else if (!(mouse & (BROWSER_MOUSE_CLICK_1 | BROWSER_MOUSE_CLICK_2)))
|
|
|
|
/* if not a button 1 or 2 click*/
|
|
|
|
status = messages_get("SelectClose");
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse drag end for the currently opened select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu which received the mouse drag end
|
|
|
|
* \param mouse current mouse state
|
|
|
|
* \param x X coordinate of drag end
|
|
|
|
* \param y Y coordinate of drag end
|
|
|
|
*/
|
|
|
|
void form_select_mouse_drag_end(struct form_control *control,
|
|
|
|
browser_mouse_state mouse, int x, int y)
|
|
|
|
{
|
|
|
|
int x0, y0, x1, y1;
|
2010-06-04 13:35:08 +04:00
|
|
|
int box_x, box_y;
|
|
|
|
struct box *box;
|
2009-08-14 14:37:33 +04:00
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
2010-06-04 13:35:08 +04:00
|
|
|
|
|
|
|
box = control->box;
|
|
|
|
|
|
|
|
/* Get global coords of scrollbar */
|
|
|
|
box_coords(box, &box_x, &box_y);
|
|
|
|
box_x -= box->border[LEFT].width;
|
|
|
|
box_y += box->height + box->border[BOTTOM].width +
|
|
|
|
box->padding[BOTTOM] + box->padding[TOP];
|
|
|
|
|
|
|
|
/* Get drag end coords relative to scrollbar */
|
|
|
|
x = x - box_x;
|
|
|
|
y = y - box_y;
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (menu->scroll_capture) {
|
|
|
|
x -= menu->width - SCROLLBAR_WIDTH;
|
2011-05-10 02:49:17 +04:00
|
|
|
scrollbar_mouse_drag_end(menu->scrollbar, mouse, x, y);
|
2009-08-14 14:37:33 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
x0 = 0;
|
|
|
|
y0 = 0;
|
|
|
|
x1 = menu->width;
|
|
|
|
y1 = menu->height;
|
|
|
|
|
|
|
|
|
|
|
|
if (x > x0 && x < x1 - SCROLLBAR_WIDTH && y > y0 && y < y1)
|
|
|
|
/* handle drag end above the option area like a regular click */
|
|
|
|
form_select_menu_clicked(control, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for the select menus scroll
|
|
|
|
*/
|
|
|
|
void form_select_menu_scroll_callback(void *client_data,
|
2011-05-10 02:49:17 +04:00
|
|
|
struct scrollbar_msg_data *scrollbar_data)
|
2009-08-14 14:37:33 +04:00
|
|
|
{
|
|
|
|
struct form_control *control = client_data;
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
|
|
|
|
2011-05-10 02:49:17 +04:00
|
|
|
switch (scrollbar_data->msg) {
|
|
|
|
case SCROLLBAR_MSG_REDRAW:
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->callback(menu->client_data,
|
|
|
|
menu->width -
|
2011-05-10 02:49:17 +04:00
|
|
|
SCROLLBAR_WIDTH + scrollbar_data->x0,
|
|
|
|
scrollbar_data->y0,
|
|
|
|
scrollbar_data->x1 - scrollbar_data->x0,
|
|
|
|
scrollbar_data->y1 - scrollbar_data->y0);
|
2009-08-14 14:37:33 +04:00
|
|
|
break;
|
2011-05-10 02:49:17 +04:00
|
|
|
case SCROLLBAR_MSG_MOVED:
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->callback(menu->client_data,
|
|
|
|
0, 0,
|
|
|
|
menu->width - SCROLLBAR_WIDTH,
|
|
|
|
menu->height);
|
|
|
|
break;
|
2011-05-10 02:49:17 +04:00
|
|
|
case SCROLLBAR_MSG_SCROLL_START:
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->scroll_capture = true;
|
|
|
|
gui_window_box_scroll_start(menu->bw->window,
|
2011-05-10 02:49:17 +04:00
|
|
|
scrollbar_data->x0, scrollbar_data->y0,
|
|
|
|
scrollbar_data->x1, scrollbar_data->y1);
|
2009-08-14 14:37:33 +04:00
|
|
|
break;
|
2011-05-10 02:49:17 +04:00
|
|
|
case SCROLLBAR_MSG_SCROLL_FINISHED:
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->scroll_capture = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the dimensions of a select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu to get the dimensions of
|
|
|
|
* \param width gets updated to menu width
|
|
|
|
* \param height gets updated to menu height
|
|
|
|
*/
|
|
|
|
void form_select_get_dimensions(struct form_control *control,
|
|
|
|
int *width, int *height)
|
|
|
|
{
|
|
|
|
*width = control->data.select.menu->width;
|
|
|
|
*height = control->data.select.menu->height;
|
|
|
|
}
|
2010-06-04 13:35:08 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a selection from a form select menu.
|
|
|
|
*
|
|
|
|
* \param bw browser window with menu
|
|
|
|
* \param control form control with menu
|
|
|
|
* \param item index of item selected from the menu
|
|
|
|
*/
|
|
|
|
|
|
|
|
void form_select_process_selection(hlcache_handle *h,
|
|
|
|
struct form_control *control, int item)
|
|
|
|
{
|
|
|
|
struct box *inline_box;
|
|
|
|
struct form_option *o;
|
|
|
|
int count;
|
|
|
|
struct content *current_content;
|
|
|
|
|
|
|
|
assert(control != NULL);
|
|
|
|
assert(h != NULL);
|
|
|
|
|
|
|
|
/** \todo Even though the form code is effectively part of the html
|
|
|
|
* content handler, poking around inside contents is not good */
|
|
|
|
current_content = hlcache_handle_get_content(h);
|
|
|
|
|
|
|
|
inline_box = control->box->children->children;
|
|
|
|
|
|
|
|
for (count = 0, o = control->data.select.items;
|
|
|
|
o != NULL;
|
|
|
|
count++, o = o->next) {
|
|
|
|
if (!control->data.select.multiple)
|
|
|
|
o->selected = false;
|
|
|
|
if (count == item) {
|
|
|
|
if (control->data.select.multiple) {
|
|
|
|
if (o->selected) {
|
|
|
|
o->selected = false;
|
|
|
|
control->data.select.num_selected--;
|
|
|
|
} else {
|
|
|
|
o->selected = true;
|
|
|
|
control->data.select.num_selected++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
o->selected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (o->selected)
|
|
|
|
control->data.select.current = o;
|
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(inline_box->text);
|
|
|
|
inline_box->text = 0;
|
|
|
|
if (control->data.select.num_selected == 0)
|
|
|
|
inline_box->text = talloc_strdup(current_content,
|
|
|
|
messages_get("Form_None"));
|
|
|
|
else if (control->data.select.num_selected == 1)
|
|
|
|
inline_box->text = talloc_strdup(current_content,
|
|
|
|
control->data.select.current->text);
|
|
|
|
else
|
|
|
|
inline_box->text = talloc_strdup(current_content,
|
|
|
|
messages_get("Form_Many"));
|
|
|
|
if (!inline_box->text) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
inline_box->length = 0;
|
|
|
|
} else
|
|
|
|
inline_box->length = strlen(inline_box->text);
|
|
|
|
inline_box->width = control->box->width;
|
|
|
|
|
|
|
|
html_redraw_a_box(h, control->box);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for the core select menu.
|
|
|
|
*/
|
|
|
|
void form_select_menu_callback(void *client_data,
|
|
|
|
int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
struct browser_window *bw = client_data;
|
|
|
|
int menu_x, menu_y;
|
|
|
|
struct box *box;
|
|
|
|
|
|
|
|
box = bw->visible_select_menu->box;
|
|
|
|
box_coords(box, &menu_x, &menu_y);
|
|
|
|
|
|
|
|
menu_x -= box->border[LEFT].width;
|
|
|
|
menu_y += box->height + box->border[BOTTOM].width +
|
|
|
|
box->padding[BOTTOM] +
|
|
|
|
box->padding[TOP];
|
|
|
|
browser_window_redraw_rect(bw, menu_x + x, menu_y + y,
|
|
|
|
width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a radio form control and clear the others in the group.
|
|
|
|
*
|
|
|
|
* \param content content containing the form, of type CONTENT_TYPE
|
|
|
|
* \param radio form control of type GADGET_RADIO
|
|
|
|
*/
|
|
|
|
|
|
|
|
void form_radio_set(hlcache_handle *content,
|
|
|
|
struct form_control *radio)
|
|
|
|
{
|
|
|
|
struct form_control *control;
|
|
|
|
|
|
|
|
assert(content);
|
|
|
|
assert(radio);
|
|
|
|
if (!radio->form)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (radio->selected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (control = radio->form->controls; control;
|
|
|
|
control = control->next) {
|
|
|
|
if (control->type != GADGET_RADIO)
|
|
|
|
continue;
|
|
|
|
if (control == radio)
|
|
|
|
continue;
|
|
|
|
if (strcmp(control->name, radio->name) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (control->selected) {
|
|
|
|
control->selected = false;
|
|
|
|
html_redraw_a_box(content, control->box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
radio->selected = true;
|
|
|
|
html_redraw_a_box(content, radio->box);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collect controls and submit a form.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void form_submit(hlcache_handle *h, struct browser_window *target,
|
|
|
|
struct form *form, struct form_control *submit_button)
|
|
|
|
{
|
2011-02-25 22:57:23 +03:00
|
|
|
char *data = NULL, *url = NULL;
|
2010-06-04 13:35:08 +04:00
|
|
|
struct fetch_multipart_data *success;
|
2011-02-25 22:57:23 +03:00
|
|
|
struct url_components components;
|
|
|
|
url_func_result res;
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2011-02-25 22:57:23 +03:00
|
|
|
assert(form != NULL);
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2011-02-25 22:57:23 +03:00
|
|
|
if (form_successful_controls(form, submit_button, &success) == false) {
|
2010-06-04 13:35:08 +04:00
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (form->method) {
|
2011-02-25 22:57:23 +03:00
|
|
|
case method_GET:
|
|
|
|
data = form_url_encode(form, success);
|
|
|
|
if (data == NULL) {
|
|
|
|
fetch_multipart_data_destroy(success);
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return;
|
|
|
|
}
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2011-02-25 22:57:23 +03:00
|
|
|
/* Decompose action */
|
|
|
|
res = url_get_components(form->action, &components);
|
|
|
|
if (res != URL_FUNC_OK) {
|
|
|
|
free(data);
|
|
|
|
fetch_multipart_data_destroy(success);
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return;
|
|
|
|
}
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2011-02-25 22:57:23 +03:00
|
|
|
/* Replace query segment */
|
|
|
|
components.query = data;
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2011-02-25 22:57:23 +03:00
|
|
|
/* Construct submit url */
|
|
|
|
url = url_reform_components(&components);
|
|
|
|
if (url == NULL) {
|
|
|
|
free(data);
|
|
|
|
fetch_multipart_data_destroy(success);
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
url_destroy_components(&components);
|
|
|
|
|
|
|
|
browser_window_go(target, url, content_get_url(h), true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case method_POST_URLENC:
|
|
|
|
data = form_url_encode(form, success);
|
|
|
|
if (data == NULL) {
|
|
|
|
fetch_multipart_data_destroy(success);
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
browser_window_go_post(target, form->action, data, 0,
|
|
|
|
true, content_get_url(h),
|
|
|
|
false, true, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case method_POST_MULTIPART:
|
|
|
|
browser_window_go_post(target, form->action, 0,
|
|
|
|
success, true, content_get_url(h),
|
|
|
|
false, true, 0);
|
|
|
|
break;
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fetch_multipart_data_destroy(success);
|
|
|
|
free(data);
|
|
|
|
free(url);
|
|
|
|
}
|