1 /* Monolith form class.
2 * - by Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: ml_form.c,v 1.6 2002/11/23 16:46:05 rich Exp $
31 #include <pthr_iolib.h>
35 #include "ml_widget.h"
36 #include "ml_form_input.h"
39 static void repaint (void *, ml_session, const char *, io_handle);
40 static struct ml_widget_property properties[];
42 struct ml_widget_operations form_ops =
45 properties: properties,
50 struct ml_widget_operations *ops;
51 pool pool; /* Pool for allocations. */
52 ml_widget w; /* Packed widget. */
53 const char *action_id; /* Form's callback action. */
54 const char *method; /* Either "GET" or "POST". */
55 const char *name; /* Unique name of the form. */
57 /* This is the user's real callback function. */
58 void (*submit_fn) (ml_session, void *);
61 vector inputs; /* List of input widgets. */
64 static struct ml_widget_property properties[] =
67 offset: ml_offsetof (struct ml_form, method),
68 type: ML_PROP_STRING },
70 offset: ml_offsetof (struct ml_form, name),
72 flags: ML_PROP_READ_ONLY },
78 ml_form_input w; /* Widget pointer. */
79 const char *name; /* Name of input widget. */
82 static void form_callback (ml_session, void *);
85 new_ml_form (pool pool)
87 static int unique = 0;
88 ml_form w = pmalloc (pool, sizeof *w);
95 w->name = psprintf (pool, "ml_form%d", ++unique);
98 w->inputs = new_vector (pool, struct form_input);
104 ml_form_set_callback (ml_form w, void (*callback_fn) (ml_session, void *),
105 ml_session session, void *data)
108 ml_unregister_action (session, w->action_id);
115 w->action_id = ml_register_action (session, form_callback, w);
116 w->submit_fn = callback_fn;
117 w->submit_data = data;
122 ml_form_pack (ml_form w, ml_widget _w)
127 /* We capture the callback function when the submit button is pressed
128 * and allow ourselves to update the values fields of all our input
132 form_callback (ml_session session, void *vw)
134 pool session_pool = ml_session_pool (session);
135 ml_form w = (ml_form) vw;
139 /* Get the arguments passed to this HTTP request. */
140 args = _ml_session_submitted_args (session);
142 /* Update the values of submitted form elements. */
143 for (i = 0; i < vector_size (w->inputs); ++i)
145 struct form_input input;
150 vector_get (w->inputs, i, input);
151 values = cgi_param_list (args, input.name);
153 /* Clear value first. */
154 ml_form_input_clear_value (input.w);
158 for (j = 0; j < vector_size (values); ++j)
160 vector_get (values, j, value);
161 ml_form_input_set_value (input.w, pstrdup (session_pool, value));
165 /* Call the user's real callback function. */
166 assert (w->submit_fn != 0);
167 w->submit_fn (session, w->submit_data);
171 _ml_form_register_widget (ml_form f, ml_form_input w)
173 struct form_input input;
174 static int name_id = 0;
176 input.name = psprintf (f->pool, "ml_f%d", ++name_id);
179 vector_push_back (f->inputs, input);
185 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
187 ml_form w = (ml_form) vw;
193 "<form method=\"%s\" action=\"%s\" name=\"%s\">"
194 "<input type=\"hidden\" name=\"ml_window\" value=\"%s\" />"
195 "<input type=\"hidden\" name=\"ml_action\" value=\"%s\" />"
198 ml_session_script_name (session),
203 io_fputs ("<form>", io);
204 ml_widget_repaint (w->w, session, windowid, io);
205 io_fputs ("</form>", io);