Add to git.
[monolith.git] / examples / 01_label_and_button.c
1 /* A very simple monolith program.
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: 01_label_and_button.c,v 1.8 2002/11/02 18:53:46 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_flow_layout.h"
30 #include "ml_label.h"
31 #include "ml_button.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   ml_label lb;                  /* Label. */
50   int count;                    /* Count of number of button presses. */
51 };
52
53 static void increment (ml_session, void *);
54 static void update_label (pool pool, ml_label lb, int count);
55
56 static void
57 app_main (ml_session session)
58 {
59   pool pool = ml_session_pool (session);
60   struct data *data;
61   ml_window w;
62   ml_flow_layout lay;
63   ml_label lb;
64   ml_button b;
65
66   /* Create the private, per-session data area. */
67   data = pmalloc (pool, sizeof *data);
68   data->count = 0;
69
70   /* Create the top-level window. */
71   w = new_ml_window (session, pool);
72
73   /* Create the flow layout widget which will be packed into the window. */
74   lay = new_ml_flow_layout (pool);
75
76   /* Create the label and button. */
77   data->lb = lb = new_ml_label (pool, 0);
78   update_label (pool, data->lb, 0);
79
80   b = new_ml_button (pool, "Push me!");
81   ml_button_set_callback (b, increment, session, data);
82
83   /* Pack the label and button into the flow layout widget. */
84   ml_flow_layout_pack (lay, lb);
85   ml_flow_layout_pack (lay, b);
86
87   /* Pack the flow layout widget into the window. */
88   ml_window_pack (w, lay);
89 }
90
91 /* This callback is called whenever the button is pressed. */
92 static void
93 increment (ml_session session, void *vp)
94 {
95   struct data *data = (struct data *) vp;
96
97   update_label (ml_session_pool (session), data->lb, ++data->count);
98 }
99
100 /* This function just updates the label, once from the main function, and
101  * again each time we press the button.
102  */
103 static void
104 update_label (pool pool, ml_label lb, int count)
105 {
106   ml_widget_set_property (lb, "text",
107                           psprintf (pool,
108                                     "Button pressed: <b>%d</b><br>",
109                                     count));
110 }