Add to git.
[monolith.git] / src / ml_form_radio.c
1 /* Monolith form radio 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_radio.c,v 1.1 2002/08/30 14:28:47 rich Exp $
19  */
20
21 #include "config.h"
22
23 #ifdef HAVE_ASSERT_H
24 #include <assert.h>
25 #endif
26
27 #include <pthr_iolib.h>
28
29 #include "monolith.h"
30 #include "ml_widget.h"
31 #include "ml_form_input.h"
32 #include "ml_form_radio.h"
33
34 static void repaint (void *, ml_session, const char *, io_handle);
35 static const char *get_value (void *);
36
37 struct ml_widget_operations form_radio_ops =
38   {
39     repaint: repaint
40   };
41
42 struct ml_form_input_operations form_radio_input_ops =
43   {
44     clear_value: 0,
45     set_value: 0,
46     get_value: get_value
47   };
48
49 struct ml_form_radio
50 {
51   struct ml_widget_operations *ops;
52   struct ml_form_input_operations *fops;
53   pool pool;                    /* Pool for allocations. */
54   const char *name;             /* Name of the input field. */
55   const char *value;            /* Value of the input field. */
56   int is_checked;               /* Radio button checked? */
57 };
58
59 ml_form_radio
60 new_ml_form_radio (pool pool, ml_form_radio_group group, const char *value)
61 {
62   ml_form_radio w = pmalloc (pool, sizeof *w);
63
64   assert (value);
65
66   w->ops = &form_radio_ops;
67   w->fops = &form_radio_input_ops;
68   w->pool = pool;
69   w->value = value;
70   w->is_checked = 0;
71
72   /* Register ourselves with the radio button group widget. */
73   w->name = _ml_form_radio_group_register (group, w, value);
74
75   return w;
76 }
77
78 void
79 ml_form_radio_set_checked (ml_form_radio w, int checked)
80 {
81   w->is_checked = checked;
82 }
83
84 int
85 ml_form_radio_get_checked (ml_form_radio w)
86 {
87   return w->is_checked;
88 }
89
90 static const char *
91 get_value (void *vw)
92 {
93   ml_form_radio w = (ml_form_radio) vw;
94
95   return w->value;
96 }
97
98 static void
99 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
100 {
101   ml_form_radio w = (ml_form_radio) vw;
102
103   io_fprintf (io, "<input class=\"ml_form_radio\" type=\"radio\" "
104               "name=\"%s\" value=\"%s\""
105               "%s />",
106               w->name, w->value,
107               w->is_checked ? " checked=\"1\"" : "");
108 }