1 /* Monolith toggle button 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_toggle_button.c,v 1.4 2002/11/07 10:49:02 rich Exp $
25 #include <pthr_iolib.h>
28 #include "ml_widget.h"
29 #include "ml_toggle_button.h"
31 static void repaint (void *, ml_session, const char *, io_handle);
32 static struct ml_widget_property properties[];
34 struct ml_widget_operations toggle_button_ops =
37 properties: properties
40 struct ml_toggle_button
42 struct ml_widget_operations *ops;
43 pool pool; /* Pool for allocations. */
44 const char *text; /* HTML printed for the button. */
45 int state; /* 0 = out, non-zero = pushed in */
46 const char *action_id; /* Action. */
47 void (*fn) (ml_session, void *); /* Callback. */
49 int is_key; /* Key button property. */
52 static struct ml_widget_property properties[] =
55 offset: ml_offsetof (struct ml_toggle_button, text),
56 type: ML_PROP_STRING },
57 { name: "button.key", /* XXX Replace with button.style. */
58 offset: ml_offsetof (struct ml_toggle_button, is_key),
63 static void toggle_me (ml_session session, void *vp);
66 new_ml_toggle_button (pool pool, ml_session session, const char *text)
68 ml_toggle_button w = pmalloc (pool, sizeof *w);
70 w->ops = &toggle_button_ops;
78 /* Register the action for this toggle button. */
79 w->action_id = ml_register_action (session, toggle_me, w);
84 /* This function is called whenever the button is toggled. */
86 toggle_me (ml_session session, void *vp)
88 ml_toggle_button w = (ml_toggle_button) vp;
92 if (w->fn) /* Call the user's callback function. */
93 w->fn (session, w->data);
97 ml_toggle_button_set_callback (ml_toggle_button w,
98 void (*fn)(ml_session, void *),
99 ml_session session, void *data)
106 ml_toggle_button_set_state (ml_toggle_button w, int state)
112 ml_toggle_button_get_state (ml_toggle_button w)
118 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
120 ml_toggle_button w = (ml_toggle_button) vw;
121 pool pool = ml_session_pool (session);
125 const char *clazz, *link;
130 clazz = "ml_toggle_button";
132 clazz = "ml_toggle_button_pressed";
137 clazz = "ml_toggle_button_key";
139 clazz = "ml_toggle_button_key_pressed";
142 link = psprintf (pool, "%s?ml_action=%s&ml_window=%s",
143 ml_session_script_name (session),
147 io_fprintf (io, "<a class=\"%s\" href=\"%s\">%s</a>",
148 clazz, link, w->text);