Add to git.
[monolith.git] / examples / 07_toggle_buttons.c
1 /* Toggle buttons.
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: 07_toggle_buttons.c,v 1.2 2002/10/30 21:03:01 rich Exp $
19  */
20
21 #include <string.h>
22
23 #include <pool.h>
24 #include <pstring.h>
25 #include <pthr_cgi.h>
26
27 #include "monolith.h"
28 #include "ml_window.h"
29 #include "ml_toggle_button.h"
30 #include "ml_table_layout.h"
31 #include "ml_text_label.h"
32
33 /*----- The following standard boilerplate code must appear -----*/
34
35 /* Main entry point to the app. */
36 static void app_main (ml_session);
37
38 int
39 handle_request (rws_request rq)
40 {
41   return ml_entry_point (rq, app_main);
42 }
43
44 /*----- End of standard boilerplate code -----*/
45
46 /* Private per-session data. */
47 struct data
48 {
49   /* The toggle buttons. */
50   ml_toggle_button b[4];
51
52   ml_text_label text;
53 };
54
55 static void update_text (ml_session, void *);
56
57 static void
58 app_main (ml_session session)
59 {
60   pool pool = ml_session_pool (session);
61   struct data *data;
62   ml_window win;
63   ml_table_layout tbl;
64
65   /* Create the private, per-session data area and save it in the
66    * session object.
67    */
68   data = pmalloc (pool, sizeof *data);
69
70   /* Create the top-level window. */
71   win = new_ml_window (session, pool);
72
73   /* Create the table layout. */
74   tbl = new_ml_table_layout (pool, 2, 4);
75
76   /* Create the toggle buttons. */
77   data->b[0] = new_ml_toggle_button (pool, session, "A");
78   data->b[1] = new_ml_toggle_button (pool, session, "B");
79   data->b[2] = new_ml_toggle_button (pool, session, "C");
80   data->b[3] = new_ml_toggle_button (pool, session, "D");
81
82   /* Create the status label. */
83   data->text = new_ml_text_label (pool, "(press a button)");
84
85   /* Set the callback on the buttons to a function which will
86    * update the label.
87    */
88   ml_toggle_button_set_callback (data->b[0], update_text, session, data);
89   ml_toggle_button_set_callback (data->b[1], update_text, session, data);
90   ml_toggle_button_set_callback (data->b[2], update_text, session, data);
91   ml_toggle_button_set_callback (data->b[3], update_text, session, data);
92
93   /* Pack everything together. */
94   ml_table_layout_pack (tbl, data->b[0], 0, 0);
95   ml_table_layout_pack (tbl, data->b[1], 0, 1);
96   ml_table_layout_pack (tbl, data->b[2], 0, 2);
97   ml_table_layout_pack (tbl, data->b[3], 0, 3);
98   ml_table_layout_pack (tbl, data->text, 1, 0);
99   ml_table_layout_set_colspan (tbl, 1, 0, 4);
100   ml_window_pack (win, tbl);
101 }
102
103 static void
104 update_text (ml_session session, void *vp)
105 {
106   pool pool = ml_session_pool (session);
107   struct data *data = (struct data *) vp;
108   int a, b, c, d;
109
110   /* Get the state of the four buttons. */
111   a = ml_toggle_button_get_state (data->b[0]);
112   b = ml_toggle_button_get_state (data->b[1]);
113   c = ml_toggle_button_get_state (data->b[2]);
114   d = ml_toggle_button_get_state (data->b[3]);
115
116   ml_widget_set_property
117     (data->text, "text",
118      psprintf (pool,
119                "Button A: %s\n"
120                "Button B: %s\n"
121                "Button C: %s\n"
122                "Button D: %s\n",
123                a ? "pressed" : "not pressed",
124                b ? "pressed" : "not pressed",
125                c ? "pressed" : "not pressed",
126                d ? "pressed" : "not pressed"));
127 }