1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* This file is part of the GtkHTML widget. |
---|
3 | |
---|
4 | Copyright (C) 2000 Jonas Borgström <jonas_b@bitsmart.com> |
---|
5 | |
---|
6 | This library is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU Library General Public |
---|
8 | License as published by the Free Software Foundation; either |
---|
9 | version 2 of the License, or (at your option) any later version. |
---|
10 | |
---|
11 | This library is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | Library General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU Library General Public License |
---|
17 | along with this library; see the file COPYING.LIB. If not, write to |
---|
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | #include <string.h> |
---|
24 | #include "htmlform.h" |
---|
25 | #include "htmlengine.h" |
---|
26 | |
---|
27 | |
---|
28 | HTMLForm * |
---|
29 | html_form_new (HTMLEngine *engine, gchar *_action, gchar *_method) |
---|
30 | { |
---|
31 | HTMLForm *new; |
---|
32 | |
---|
33 | new = g_new (HTMLForm, 1); |
---|
34 | new->action = g_strdup(_action); |
---|
35 | new->method = g_strdup(_method); |
---|
36 | |
---|
37 | new->elements = NULL; |
---|
38 | new->hidden = NULL; |
---|
39 | html_form_set_engine (new, engine); |
---|
40 | |
---|
41 | new->radio_group = g_hash_table_new (g_str_hash, g_str_equal); |
---|
42 | |
---|
43 | return new; |
---|
44 | } |
---|
45 | |
---|
46 | void |
---|
47 | html_form_add_element (HTMLForm *form, HTMLEmbedded *element) |
---|
48 | { |
---|
49 | form->elements = g_list_append (form->elements, element); |
---|
50 | |
---|
51 | html_embedded_set_form (element, form); |
---|
52 | } |
---|
53 | |
---|
54 | void |
---|
55 | html_form_add_hidden (HTMLForm *form, HTMLHidden *hidden) |
---|
56 | { |
---|
57 | html_form_add_element (form, HTML_EMBEDDED (hidden)); |
---|
58 | |
---|
59 | form->hidden = g_list_append (form->hidden, hidden); |
---|
60 | } |
---|
61 | |
---|
62 | void |
---|
63 | html_form_add_radio (HTMLForm *form, char *name, GtkRadioButton *button) |
---|
64 | { |
---|
65 | GtkWidget *master; |
---|
66 | GSList *group; |
---|
67 | char *key = name; |
---|
68 | |
---|
69 | /* |
---|
70 | * FIXME a null name makes them all share the same "" group. I doubt this |
---|
71 | * is the correct behaviour but I'm not sure what is. The spec doesn't seem clear. |
---|
72 | */ |
---|
73 | if (key == NULL) key = ""; |
---|
74 | |
---|
75 | master = g_hash_table_lookup (form->radio_group, key); |
---|
76 | if (master == NULL) { |
---|
77 | /* if there is no entry we dup the key because the table will own it */ |
---|
78 | key = g_strdup (key); |
---|
79 | gtk_widget_ref (GTK_WIDGET (button)); |
---|
80 | g_hash_table_insert (form->radio_group, key, button); |
---|
81 | } else { |
---|
82 | group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (master)); |
---|
83 | gtk_radio_button_set_group (button, group); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | static void |
---|
88 | destroy_hidden (gpointer o, gpointer data) |
---|
89 | { |
---|
90 | html_object_destroy (HTML_OBJECT (o)); |
---|
91 | } |
---|
92 | |
---|
93 | static void |
---|
94 | destroy_radio (char *key, gpointer *master) |
---|
95 | { |
---|
96 | g_free (key); |
---|
97 | gtk_widget_unref (GTK_WIDGET (master)); |
---|
98 | } |
---|
99 | |
---|
100 | static void |
---|
101 | reset_element (gpointer o, gpointer data) |
---|
102 | { |
---|
103 | html_embedded_reset (HTML_EMBEDDED (o)); |
---|
104 | } |
---|
105 | |
---|
106 | void |
---|
107 | html_form_destroy (HTMLForm *form) |
---|
108 | { |
---|
109 | g_list_foreach (form->hidden, destroy_hidden, NULL); |
---|
110 | g_list_free (form->elements); |
---|
111 | g_list_free (form->hidden); |
---|
112 | |
---|
113 | g_hash_table_foreach (form->radio_group, (GHFunc)destroy_radio, NULL); |
---|
114 | g_hash_table_destroy (form->radio_group); |
---|
115 | |
---|
116 | g_free (form->action); |
---|
117 | g_free (form->method); |
---|
118 | g_free (form); |
---|
119 | } |
---|
120 | |
---|
121 | void |
---|
122 | html_form_submit (HTMLForm *form) |
---|
123 | { |
---|
124 | GString *encoding = g_string_new (""); |
---|
125 | gint first = TRUE; |
---|
126 | GList *i = form->elements; |
---|
127 | gchar *ptr; |
---|
128 | |
---|
129 | while (i) { |
---|
130 | ptr = html_embedded_encode (HTML_EMBEDDED (i->data)); |
---|
131 | |
---|
132 | if (strlen (ptr)) { |
---|
133 | if(!first) |
---|
134 | encoding = g_string_append_c (encoding, '&'); |
---|
135 | else |
---|
136 | first = FALSE; |
---|
137 | |
---|
138 | encoding = g_string_append (encoding, ptr); |
---|
139 | g_free (ptr); |
---|
140 | } |
---|
141 | i = g_list_next (i); |
---|
142 | } |
---|
143 | |
---|
144 | html_engine_form_submitted (form->engine, form->method, form->action, encoding->str); |
---|
145 | |
---|
146 | g_string_free (encoding, TRUE); |
---|
147 | } |
---|
148 | |
---|
149 | void |
---|
150 | html_form_set_engine (HTMLForm *form, HTMLEngine *engine) |
---|
151 | { |
---|
152 | g_return_if_fail (HTML_IS_ENGINE (engine)); |
---|
153 | form->engine = engine; |
---|
154 | } |
---|
155 | |
---|
156 | void |
---|
157 | html_form_reset (HTMLForm *form) |
---|
158 | { |
---|
159 | g_list_foreach (form->elements, reset_element, NULL); |
---|
160 | } |
---|
161 | |
---|