Add to git.
[monolith.git] / src / ml_form_password.c
1 /* Monolith form password input 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_password.c,v 1.2 2003/01/11 16:39:09 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_smarttext.h"
32 #include "ml_form_input.h"
33 #include "ml_form_password.h"
34
35 static void repaint (void *, ml_session, const char *, io_handle);
36 static void clear_value (void *);
37 static void set_value (void *, const char *value);
38 static const char *get_value (void *);
39
40 struct ml_widget_operations form_password_ops =
41   {
42     repaint: repaint
43   };
44
45 struct ml_form_input_operations form_password_input_ops =
46   {
47     clear_value: clear_value,
48     set_value: set_value,
49     get_value: get_value
50   };
51
52 struct ml_form_password
53 {
54   struct ml_widget_operations *ops;
55   struct ml_form_input_operations *fops;
56   pool pool;                    /* Pool for allocations. */
57   const char *name;             /* Name of the input field. */
58   const char *value;            /* Value of the input field. */
59 };
60
61 ml_form_password
62 new_ml_form_password (pool pool, ml_form form)
63 {
64   ml_form_password w = pmalloc (pool, sizeof *w);
65
66   w->ops = &form_password_ops;
67   w->fops = &form_password_input_ops;
68   w->pool = pool;
69   w->value = 0;
70
71   /* Register ourselves with the form. */
72   w->name = _ml_form_register_widget (form, w);
73
74   return w;
75 }
76
77 static void
78 clear_value (void *vw)
79 {
80   ml_form_password w = (ml_form_password) vw;
81
82   w->value = 0;
83 }
84
85 static void
86 set_value (void *vw, const char *value)
87 {
88   ml_form_password w = (ml_form_password) vw;
89
90   assert (value);
91   w->value = value;
92 }
93
94 static const char *
95 get_value (void *vw)
96 {
97   ml_form_password w = (ml_form_password) vw;
98
99   return w->value;
100 }
101
102 static void
103 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
104 {
105   ml_form_password w = (ml_form_password) vw;
106
107   io_fprintf (io, "<input class=\"ml_form_password\" type=\"password\" "
108               "name=\"%s\" value=\"",
109               w->name);
110   if (w->value) ml_plaintext_print (io, w->value);
111   io_fputs ("\" />", io);
112 }