Add to git.
[monolith.git] / src / ml_form_submit.c
1 /* Monolith form submit button class.
2  * - by Richard W.M. Jones <rich@annexia.org>
3  *
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.
8  *
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.
13  *
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.
17  *
18  * $Id: ml_form_submit.c,v 1.4 2003/01/11 16:39:10 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <pthr_iolib.h>
24
25 #include "monolith.h"
26 #include "ml_widget.h"
27 #include "ml_smarttext.h"
28 #include "ml_form_submit.h"
29
30 static void repaint (void *, ml_session, const char *, io_handle);
31 static void clear_value (void *);
32 static void set_value (void *, const char *value);
33 static const char *get_value (void *);
34
35 struct ml_widget_operations form_submit_ops =
36   {
37     repaint: repaint
38   };
39
40 struct ml_form_input_operations form_submit_input_ops =
41   {
42     clear_value: clear_value,
43     set_value: set_value,
44     get_value: get_value
45   };
46
47 struct ml_form_submit
48 {
49   struct ml_widget_operations *ops;
50   struct ml_form_input_operations *fops;
51   pool pool;                    /* Pool for allocations. */
52   const char *name;             /* Name of the input field. */
53   const char *value;            /* Value of the input field. */
54 };
55
56 ml_form_submit
57 new_ml_form_submit (pool pool, ml_form form, const char *value)
58 {
59   ml_form_submit w = pmalloc (pool, sizeof *w);
60
61   w->ops = &form_submit_ops;
62   w->fops = &form_submit_input_ops;
63   w->pool = pool;
64   w->value = value;
65
66   /* Register ourselves with the form. */
67   w->name = _ml_form_register_widget (form, w);
68
69   return w;
70 }
71
72 static void
73 clear_value (void *vw)
74 {
75   ml_form_submit w = (ml_form_submit) vw;
76
77   w->value = 0;
78 }
79
80 static void
81 set_value (void *vw, const char *value)
82 {
83   ml_form_submit w = (ml_form_submit) vw;
84
85   w->value = value;
86 }
87
88 static const char *
89 get_value (void *vw)
90 {
91   ml_form_submit w = (ml_form_submit) vw;
92
93   return w->value;
94 }
95
96 static void
97 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
98 {
99   ml_form_submit w = (ml_form_submit) vw;
100
101   io_fprintf (io, "<input class=\"ml_form_submit\" "
102               "type=\"submit\" name=\"%s\" value=\"",
103               w->name);
104   if (w->value) ml_plaintext_print (io, w->value);
105   io_fputs ("\" />", io);
106 }