Add to git.
[monolith.git] / src / ml_form_text.c
1 /* Monolith form text 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_text.c,v 1.5 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_input.h"
33 #include "ml_form_text.h"
34
35 static void repaint (void *, ml_session, const char *, io_handle);
36 static struct ml_widget_property properties[];
37 static void clear_value (void *);
38 static void set_value (void *, const char *value);
39 static const char *get_value (void *);
40
41 struct ml_widget_operations form_text_ops =
42   {
43     repaint: repaint,
44     properties: properties,
45   };
46
47 struct ml_form_input_operations form_text_input_ops =
48   {
49     clear_value: clear_value,
50     set_value: set_value,
51     get_value: get_value
52   };
53
54 struct ml_form_text
55 {
56   struct ml_widget_operations *ops;
57   struct ml_form_input_operations *fops;
58   pool pool;                    /* Pool for allocations. */
59   const char *name;             /* Name of the input field. */
60   const char *value;            /* Value of the input field. */
61   int size;                     /* Displayed width. */
62   int maxlength;                /* Maximum number of characters. */
63   ml_form form;                 /* The form. */
64   int focus;                    /* If set, grab focus. */
65 };
66
67 static struct ml_widget_property properties[] =
68   {
69     { name: "form.text.size",
70       offset: ml_offsetof (struct ml_form_text, size),
71       type: ML_PROP_INT },
72     { name: "form.text.maxlength",
73       offset: ml_offsetof (struct ml_form_text, maxlength),
74       type: ML_PROP_INT },
75     { 0 }
76   };
77
78 ml_form_text
79 new_ml_form_text (pool pool, ml_form form)
80 {
81   ml_form_text w = pmalloc (pool, sizeof *w);
82
83   w->ops = &form_text_ops;
84   w->fops = &form_text_input_ops;
85   w->pool = pool;
86   w->value = 0;
87   w->size = w->maxlength = -1;
88   w->form = form;
89   w->focus = 0;
90
91   /* Register ourselves with the form. */
92   w->name = _ml_form_register_widget (form, w);
93
94   return w;
95 }
96
97 void
98 ml_form_text_focus (ml_form_text w)
99 {
100   w->focus = 1;
101 }
102
103 static void
104 clear_value (void *vw)
105 {
106   ml_form_text w = (ml_form_text) vw;
107
108   w->value = 0;
109 }
110
111 static void
112 set_value (void *vw, const char *value)
113 {
114   ml_form_text w = (ml_form_text) vw;
115
116   assert (value);
117   w->value = value;
118 }
119
120 static const char *
121 get_value (void *vw)
122 {
123   ml_form_text w = (ml_form_text) vw;
124
125   return w->value;
126 }
127
128 static void
129 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
130 {
131   ml_form_text w = (ml_form_text) vw;
132
133   io_fprintf (io, "<input class=\"ml_form_text\" name=\"%s\" value=\"",
134               w->name);
135   if (w->value) ml_plaintext_print (io, w->value);
136   io_fputc ('"', io);
137   if (w->size >= 0)
138     io_fprintf (io, " size=\"%d\"", w->size);
139   if (w->maxlength >= 0)
140     io_fprintf (io, " maxlength=\"%d\"", w->maxlength);
141   io_fputs (" />", io);
142
143   /* XXX It is quite likely this won't work. It looks like we need to
144    * provide an onLoad function in the window to do this reliably.
145    */
146   if (w->focus)
147     {
148       const char *form_name;
149
150       ml_widget_get_property (w->form, "form.name", form_name);
151
152       io_fprintf (io, "<script language=\"javascript\"><!--\n"
153                   "  document.%s.%s.focus ();\n"
154                   "//--></script>\n",
155                   form_name, w->name);
156     }
157 }