summaryrefslogtreecommitdiff
path: root/render/form.h
blob: c1e01ae0a85bffa4423ddf58ba4c683218cd2990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
 * Copyright 2003 James Bursa <bursa@users.sourceforge.net>
 */

/** \file
 * Form handling functions (interface).
 */

#ifndef _NETSURF_RENDER_FORM_H_
#define _NETSURF_RENDER_FORM_H_

#include <stdbool.h>
#include "netsurf/utils/config.h"

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. */
	form_method method;			/**< Method and enctype. */
	char *charset;				/**< Charset to submit form in */
	struct form_control *controls;		/**< Linked list of controls. */
	struct form_control *last_control;	/**< Last control in list. */
	struct form *prev;			/**< Previous form in doc. */
};

/** Type of a struct form_control. */
typedef enum {
	GADGET_HIDDEN,
	GADGET_TEXTBOX,
	GADGET_RADIO,
	GADGET_CHECKBOX,
	GADGET_SELECT,
	GADGET_TEXTAREA,
	GADGET_IMAGE,
	GADGET_PASSWORD,
	GADGET_SUBMIT,
	GADGET_RESET,
	GADGET_FILE
} form_control_type;

/** Form control. */
struct form_control {
	form_control_type type;
	char *name;
	char *value;
	char *initial_value;
	bool disabled;
	struct form *form;
	struct box *box;
	struct box *caret_inline_container;
	struct box *caret_text_box;
	size_t caret_box_offset, caret_form_offset;
	unsigned int length;
	int caret_pixel_offset;
	unsigned int maxlength;
	bool selected;
	union {
		struct {
			int mx, my;
		} image;
		struct {
			int num_items;
			struct form_option *items, *last_item;
			bool multiple;
			int num_selected;
			/** Currently selected item, if num_selected == 1. */
			struct form_option *current;
		} select;
	} data;
	struct form_control *prev;      /**< Previous control in this form */
	struct form_control *next;	/**< Next control in this form. */
};

/** Option in a select. */
struct form_option {
	bool selected;
	bool initial_selected;
	char *value;
	char *text; /**< NUL terminated. */
	struct form_option* next;
};

/** Successful control, as defined by HTML 4.01 17.13. */
struct form_successful_control {
        bool file;                              /**< It's a file */
	char *name;				/**< Control name. */
	char *value;				/**< Current value. */
	struct form_successful_control *next;	/**< Next in linked list. */
};

struct form *form_new(char *action, form_method method, char *charset);
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);
char *form_url_encode(struct form *form,
		struct form_successful_control *control);
void form_free_successful(struct form_successful_control *control);

#endif