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