Add to git.
[monolith.git] / src / ml_toggle_button.c
1 /* Monolith toggle button 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_toggle_button.c,v 1.4 2002/11/07 10:49:02 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <pool.h>
24 #include <pstring.h>
25 #include <pthr_iolib.h>
26
27 #include "monolith.h"
28 #include "ml_widget.h"
29 #include "ml_toggle_button.h"
30
31 static void repaint (void *, ml_session, const char *, io_handle);
32 static struct ml_widget_property properties[];
33
34 struct ml_widget_operations toggle_button_ops =
35   {
36     repaint: repaint,
37     properties: properties
38   };
39
40 struct ml_toggle_button
41 {
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. */
48   void *data;
49   int is_key;                   /* Key button property. */
50 };
51
52 static struct ml_widget_property properties[] =
53   {
54     { name: "text",
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),
59       type: ML_PROP_BOOL },
60     { 0 }
61   };
62
63 static void toggle_me (ml_session session, void *vp);
64
65 ml_toggle_button
66 new_ml_toggle_button (pool pool, ml_session session, const char *text)
67 {
68   ml_toggle_button w = pmalloc (pool, sizeof *w);
69
70   w->ops = &toggle_button_ops;
71   w->pool = pool;
72   w->text = text;
73   w->state = 0;
74   w->is_key = 0;
75   w->fn = 0;
76   w->data = 0;
77
78   /* Register the action for this toggle button. */
79   w->action_id = ml_register_action (session, toggle_me, w);
80
81   return w;
82 }
83
84 /* This function is called whenever the button is toggled. */
85 static void
86 toggle_me (ml_session session, void *vp)
87 {
88   ml_toggle_button w = (ml_toggle_button) vp;
89
90   w->state = !w->state;
91
92   if (w->fn)                    /* Call the user's callback function. */
93     w->fn (session, w->data);
94 }
95
96 void
97 ml_toggle_button_set_callback (ml_toggle_button w,
98                                void (*fn)(ml_session, void *),
99                                ml_session session, void *data)
100 {
101   w->fn = fn;
102   w->data = data;
103 }
104
105 void
106 ml_toggle_button_set_state (ml_toggle_button w, int state)
107 {
108   w->state = state;
109 }
110
111 int
112 ml_toggle_button_get_state (ml_toggle_button w)
113 {
114   return w->state;
115 }
116
117 static void
118 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
119 {
120   ml_toggle_button w = (ml_toggle_button) vw;
121   pool pool = ml_session_pool (session);
122
123   if (w->text)
124     {
125       const char *clazz, *link;
126
127       if (!w->is_key)
128         {
129           if (!w->state)
130             clazz = "ml_toggle_button";
131           else
132             clazz = "ml_toggle_button_pressed";
133         }
134       else
135         {
136           if (!w->state)
137             clazz = "ml_toggle_button_key";
138           else
139             clazz = "ml_toggle_button_key_pressed";
140         }
141
142       link = psprintf (pool, "%s?ml_action=%s&ml_window=%s",
143                        ml_session_script_name (session),
144                        w->action_id,
145                        windowid);
146
147       io_fprintf (io, "<a class=\"%s\" href=\"%s\">%s</a>",
148                   clazz, link, w->text);
149     }
150 }