1 /* Monolith form select box input class.
2 * - by Richard W.M. Jones <rich@annexia.org>
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.
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.
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.
18 * $Id: ml_form_select.c,v 1.4 2002/11/13 20:46:37 rich Exp $
23 #include <pthr_iolib.h>
26 #include "ml_widget.h"
27 #include "ml_form_input.h"
28 #include "ml_form_select.h"
30 static void repaint (void *, ml_session, const char *, io_handle);
31 static struct ml_widget_property properties[];
32 static void set_value (void *, const char *value);
33 static void clear_value (void *);
35 struct ml_widget_operations form_select_ops =
38 properties: properties,
41 struct ml_form_input_operations form_select_input_ops =
45 clear_value: clear_value
50 struct ml_widget_operations *ops;
51 struct ml_form_input_operations *fops;
52 pool pool; /* Pool for allocations. */
53 const char *name; /* Name of the input field. */
54 int size; /* Size property. */
55 int multiple; /* Multiple property. */
56 vector options; /* Options (vector of strings). */
57 vector selections; /* Selected options (vector of int). */
58 int selected; /* Single selection. */
61 static struct ml_widget_property properties[] =
63 { name: "form.select.size",
64 offset: ml_offsetof (struct ml_form_select, size),
66 { name: "form.select.multiple",
67 offset: ml_offsetof (struct ml_form_select, multiple),
73 new_ml_form_select (pool pool, ml_form form)
75 ml_form_select w = pmalloc (pool, sizeof *w);
77 w->ops = &form_select_ops;
78 w->fops = &form_select_input_ops;
82 w->options = new_vector (pool, const char *);
86 /* Register ourselves with the form. */
87 w->name = _ml_form_register_widget (form, w);
93 ml_form_select_push_back (ml_form_select w, const char *option)
95 vector_push_back (w->options, option);
99 ml_form_select_pop_back (ml_form_select w)
103 vector_pop_back (w->options, option);
108 ml_form_select_push_front (ml_form_select w, const char *option)
110 vector_push_front (w->options, option);
114 ml_form_select_pop_front (ml_form_select w)
118 vector_pop_front (w->options, option);
123 ml_form_select_get (ml_form_select w, int option_index)
127 vector_get (w->options, option_index, option);
132 ml_form_select_insert (ml_form_select w, int option_index, const char *option)
134 vector_insert (w->options, option_index, option);
138 ml_form_select_replace (ml_form_select w, int option_index, const char *option)
140 vector_replace (w->options, option_index, option);
144 ml_form_select_erase (ml_form_select w, int option_index)
146 vector_erase (w->options, option_index);
150 ml_form_select_clear (ml_form_select w)
152 vector_clear (w->options);
156 ml_form_select_size (ml_form_select w)
158 return vector_size (w->options);
162 ml_form_select_set_selection (ml_form_select w, int option_index)
165 w->selected = option_index;
171 ml_form_select_set_selections (ml_form_select w, vector selected)
174 w->selections = selected;
180 ml_form_select_get_selection (ml_form_select w)
189 ml_form_select_get_selections (ml_form_select w)
192 return w->selections;
198 clear_value (void *vw)
200 ml_form_select w = (ml_form_select) vw;
210 w->selections = new_vector (w->pool, int);
211 vector_fill (w->selections, zero, vector_size (w->options));
216 set_value (void *vw, const char *value)
218 ml_form_select w = (ml_form_select) vw;
220 sscanf (value, "%d", &w->selected);
222 if (w->multiple && w->selections &&
223 w->selected >= 0 && w->selected < vector_size (w->selections))
227 vector_replace (w->selections, w->selected, one);
232 is_selected (ml_form_select w, int index)
235 return index == w->selected;
238 if (w->selections && index < vector_size (w->selections))
242 vector_get (w->selections, index, i);
251 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
253 ml_form_select w = (ml_form_select) vw;
257 io_fprintf (io, "<select class=\"ml_form_select\" name=\"%s\"", w->name);
258 if (w->size) io_fprintf (io, " size=\"%d\"", w->size);
259 if (w->multiple) io_fprintf (io, " multiple=\"1\"");
260 io_fprintf (io, ">");
262 for (i = 0; i < vector_size (w->options); ++i)
264 vector_get (w->options, i, option);
266 io_fprintf (io, "<option value=\"%d\"", i);
267 if (is_selected (w, i))
268 io_fprintf (io, " selected=\"1\"");
269 io_fprintf (io, ">%s</option>\n", option);
272 io_fprintf (io, "</select>");