Add to git.
[monolith.git] / examples / 08_menus.c
1 /* Menus.
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: 08_menus.c,v 1.1 2002/11/30 15:55: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_menu.h"
30 #include "ml_text_label.h"
31
32 /*----- The following standard boilerplate code must appear -----*/
33
34 /* Main entry point to the app. */
35 static void app_main (ml_session);
36
37 int
38 handle_request (rws_request rq)
39 {
40   return ml_entry_point (rq, app_main);
41 }
42
43 /*----- End of standard boilerplate code -----*/
44
45 /* Private per-session data. */
46 struct data
47 {
48   ml_window win;                /* Top-level window. */
49   ml_menubar menubar;           /* Menubar along top of window. */
50   ml_text_label text;           /* Label displayed in the main window. */
51 };
52
53 static void set_red (ml_session session, void *vdata);
54 static void set_green (ml_session session, void *vdata);
55 static void set_blue (ml_session session, void *vdata);
56 static void set_black (ml_session session, void *vdata);
57 static void set_small (ml_session session, void *vdata);
58 static void set_medium (ml_session session, void *vdata);
59 static void set_large (ml_session session, void *vdata);
60
61 static void
62 app_main (ml_session session)
63 {
64   pool pool = ml_session_pool (session);
65   struct data *data;
66   ml_menu menu;
67
68   /* Create the private, per-session data area and save it in the
69    * session object.
70    */
71   data = pmalloc (pool, sizeof *data);
72
73   /* Create the top-level window. */
74   data->win = new_ml_window (session, pool);
75
76   /* Create the menubar. */
77   data->menubar = new_ml_menubar (pool);
78
79   /* Create the "Colours" menu. */
80   menu = new_ml_menu (pool);
81   ml_menu_push_back_button (menu, "Red", set_red, session, data);
82   ml_menu_push_back_button (menu, "Green", set_green, session, data);
83   ml_menu_push_back_button (menu, "Blue", set_blue, session, data);
84   ml_menu_push_back_button (menu, "Black", set_black, session, data);
85
86   /* Pack the "Colours" menu. */
87   ml_menubar_push_back (data->menubar, "Colours", menu);
88
89   /* Create the "Sizes" menu. */
90   menu = new_ml_menu (pool);
91   ml_menu_push_back_button (menu, "Small", set_small, session, data);
92   ml_menu_push_back_button (menu, "Medium", set_medium, session, data);
93   ml_menu_push_back_button (menu, "Large", set_large, session, data);
94
95   /* Pack the "Sizes" menu. */
96   ml_menubar_push_back (data->menubar, "Sizes", menu);
97
98   /* Create the text which appears on the page. (C) Eric Meyer. */
99   data->text = new_ml_text_label
100     (pool,
101      "\n"
102      "In other CSS-aware browsers, like Internet Explorer or Opera, you'll see the toplevel links, and they'll work just fine. The popout menus won't work, that's all. Of course, this means that the browser is downloading the contents of the complete menu, but only able to display a small part of what it downloaded. This is in some respects unfortunate, but at least the basic menu will still function. And the amount of markup involved is probably a lot fewer characters than a similar Javascript-driven menu intended to serve a similar purpose.\n\n"
103      "(And if you're grumbling that you don't see why anyone would deploy a menu system that only worked in one browser, then you should see how many Javascript menus I deal with that don't work in more than one or two browsers-- usually because they're doing browser sniffing as if this were still 1998. But I digress.)\n\n"
104      "If the browser is too old to understand CSS, or doesn't support CSS because (for example) it's a text-mode browser like Lynx, then the user will get the entire menu as a set of nested lists. No problem.\n\n");
105
106   /* Put the text into the menubar widget, so it appears. */
107   ml_menubar_pack (data->menubar, data->text);
108
109   /* Pack the menubar inside the window. */
110   ml_window_pack (data->win, data->menubar);
111 }
112
113 static void
114 set_red (ml_session session, void *vdata)
115 {
116   struct data *data = (struct data *) vdata;
117
118   ml_widget_set_property (data->text, "color", "red");
119 }
120
121 static void
122 set_green (ml_session session, void *vdata)
123 {
124   struct data *data = (struct data *) vdata;
125
126   ml_widget_set_property (data->text, "color", "green");
127 }
128
129 static void
130 set_blue (ml_session session, void *vdata)
131 {
132   struct data *data = (struct data *) vdata;
133
134   ml_widget_set_property (data->text, "color", "blue");
135 }
136
137 static void
138 set_black (ml_session session, void *vdata)
139 {
140   struct data *data = (struct data *) vdata;
141
142   ml_widget_set_property (data->text, "color", "black");
143 }
144
145 static void
146 set_small (ml_session session, void *vdata)
147 {
148   struct data *data = (struct data *) vdata;
149
150   ml_widget_set_property (data->text, "font.size", "small");
151 }
152
153 static void
154 set_medium (ml_session session, void *vdata)
155 {
156   struct data *data = (struct data *) vdata;
157
158   ml_widget_set_property (data->text, "font.size", "medium");
159 }
160
161 static void
162 set_large (ml_session session, void *vdata)
163 {
164   struct data *data = (struct data *) vdata;
165
166   ml_widget_set_property (data->text, "font.size", "large");
167 }