1 /* Monolith menubar, menu, menuitem classes.
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_menu.c,v 1.2 2003/01/11 16:39:10 rich Exp $
29 #include <pthr_iolib.h>
32 #include "ml_widget.h"
33 #include "ml_smarttext.h"
36 static void menubar_repaint (void *, ml_session, const char *, io_handle);
37 static struct ml_widget_property menubar_properties[];
39 struct ml_widget_operations menubar_ops =
41 repaint: menubar_repaint,
42 properties: menubar_properties,
47 struct ml_widget_operations *ops;
48 pool pool; /* Pool for allocations. */
49 vector menus; /* Menus contained here. */
50 ml_widget w; /* Widget for main application area. */
53 static struct ml_widget_property menubar_properties[] =
64 static void menu_repaint (void *, ml_session, const char *, io_handle);
65 static struct ml_widget_property menu_properties[];
67 struct ml_widget_operations menu_ops =
69 repaint: menu_repaint,
70 properties: menu_properties,
75 struct ml_widget_operations *ops;
76 pool pool; /* Pool for allocations. */
77 vector items; /* Menu items. */
80 static struct ml_widget_property menu_properties[] =
85 static void menuitem_button_repaint (void *, ml_session, const char *, io_handle);
86 static struct ml_widget_property menuitem_button_properties[];
88 struct ml_widget_operations menuitem_button_ops =
90 repaint: menuitem_button_repaint,
91 properties: menuitem_button_properties,
94 struct ml_menuitem_button
96 struct ml_widget_operations *ops;
97 pool pool; /* Pool for allocations. */
98 const char *text; /* Text on the button. */
99 const char *action_id; /* Action. */
100 const char *title; /* Tooltip. */
103 static struct ml_widget_property menuitem_button_properties[] =
108 static void menuitem_separator_repaint (void *, ml_session, const char *, io_handle);
109 static struct ml_widget_property menuitem_separator_properties[];
111 struct ml_widget_operations menuitem_separator_ops =
113 repaint: menuitem_separator_repaint,
114 properties: menuitem_separator_properties,
117 struct ml_menuitem_separator
119 struct ml_widget_operations *ops;
120 pool pool; /* Pool for allocations. */
123 static struct ml_widget_property menuitem_separator_properties[] =
129 new_ml_menubar (pool pool)
131 ml_menubar w = pmalloc (pool, sizeof *w);
133 w->ops = &menubar_ops;
135 w->menus = new_vector (pool, struct menu);
142 ml_menubar_push_back (ml_menubar w, const char *name, ml_menu menu)
144 struct menu m = { name, menu };
146 vector_push_back (w->menus, m);
150 ml_menubar_pop_back (ml_menubar w)
154 vector_pop_back (w->menus, m);
159 ml_menubar_push_front (ml_menubar w, const char *name, ml_menu menu)
161 struct menu m = { name, menu };
163 vector_push_front (w->menus, m);
167 ml_menubar_pop_front (ml_menubar w)
171 vector_pop_front (w->menus, m);
176 ml_menubar_get (ml_menubar w, int i)
180 vector_get (w->menus, i, m);
185 ml_menubar_insert (ml_menubar w, int i, const char *name, ml_menu menu)
187 struct menu m = { name, menu };
189 vector_insert (w->menus, i, m);
193 ml_menubar_replace (ml_menubar w, int i, const char *name, ml_menu menu)
195 struct menu m = { name, menu };
197 vector_replace (w->menus, i, m);
201 ml_menubar_erase (ml_menubar w, int i)
203 vector_erase (w->menus, i);
207 ml_menubar_clear (ml_menubar w)
209 vector_clear (w->menus);
213 ml_menubar_size (ml_menubar w)
215 return vector_size (w->menus);
219 ml_menubar_pack (ml_menubar w, ml_widget _w)
225 menubar_repaint (void *vw, ml_session session,
226 const char *windowid, io_handle io)
228 ml_menubar w = (ml_menubar) vw;
232 io_fputs ("<table class=\"ml_menubar\"><tr>", io);
234 for (i = 0; i < vector_size (w->menus); ++i)
236 vector_get (w->menus, i, m);
238 io_fprintf (io, "<td class=\"ml_menubar_item\">%s", m.name);
239 menu_repaint (m.menu, session, windowid, io);
240 io_fputs ("</td>\n", io);
243 io_fputs ("<td width=\"100%\"></td></tr></table>", io);
245 if (w->w) ml_widget_repaint (w->w, session, windowid, io);
249 new_ml_menu (pool pool)
251 ml_menu w = pmalloc (pool, sizeof *w);
255 w->items = new_vector (pool, ml_widget);
261 ml_menu_push_back (ml_menu w, ml_widget menu_or_menuitem)
263 vector_push_back (w->items, menu_or_menuitem);
267 ml_menu_push_back_button (ml_menu w, const char *text, void (*fn) (ml_session, void *), ml_session session, void *data)
269 ml_menuitem_button b = new_ml_menuitem_button (w->pool, text);
271 ml_menuitem_button_set_callback (b, fn, session, data);
272 vector_push_back (w->items, b);
276 ml_menu_push_back_inactive_button (ml_menu w, const char *text)
278 ml_menuitem_button b = new_ml_menuitem_button (w->pool, text);
280 vector_push_back (w->items, b);
284 ml_menu_push_back_separator (ml_menu w)
286 ml_menuitem_separator b = new_ml_menuitem_separator (w->pool);
288 vector_push_back (w->items, b);
292 ml_menu_pop_back (ml_menu w)
294 ml_widget menu_or_menuitem;
296 vector_pop_back (w->items, menu_or_menuitem);
297 return menu_or_menuitem;
301 ml_menu_push_front (ml_menu w, ml_widget menu_or_menuitem)
303 vector_push_front (w->items, menu_or_menuitem);
307 ml_menu_pop_front (ml_menu w)
309 ml_widget menu_or_menuitem;
311 vector_pop_front (w->items, menu_or_menuitem);
312 return menu_or_menuitem;
316 ml_menu_get (ml_menu w, int i)
318 ml_widget menu_or_menuitem;
320 vector_get (w->items, i, menu_or_menuitem);
321 return menu_or_menuitem;
325 ml_menu_insert (ml_menu w, int i, ml_widget menu_or_menuitem)
327 vector_insert (w->items, i, menu_or_menuitem);
331 ml_menu_replace (ml_menu w, int i, ml_widget menu_or_menuitem)
333 vector_replace (w->items, i, menu_or_menuitem);
337 ml_menu_erase (ml_menu w, int i)
339 vector_erase (w->items, i);
343 ml_menu_clear (ml_menu w)
345 vector_clear (w->items);
349 ml_menu_size (ml_menu w)
351 return vector_size (w->items);
355 ml_menu_pack (ml_menu w, ml_widget menu_or_menuitem)
357 vector_push_back (w->items, menu_or_menuitem);
361 menu_repaint (void *vw, ml_session session,
362 const char *windowid, io_handle io)
364 ml_menu w = (ml_menu) vw;
368 io_fputs ("<ul>", io);
370 for (i = 0; i < vector_size (w->items); ++i)
372 vector_get (w->items, i, m);
374 io_fputs ("<li>", io);
375 ml_widget_repaint (m, session, windowid, io);
376 io_fputs ("</li>\n", io);
379 io_fputs ("</ul>\n", io);
383 new_ml_menuitem_button (pool pool, const char *text)
385 ml_menuitem_button w = pmalloc (pool, sizeof *w);
387 w->ops = &menuitem_button_ops;
397 ml_menuitem_button_set_callback (ml_menuitem_button w,
398 void (*fn) (ml_session, void *),
399 ml_session session, void *data)
402 ml_unregister_action (session, w->action_id);
406 w->action_id = ml_register_action (session, fn, data);
410 menuitem_button_repaint (void *vw, ml_session session,
411 const char *windowid, io_handle io)
413 ml_menuitem_button w = (ml_menuitem_button) vw;
419 pool tmp = new_subpool (w->pool);
422 psprintf (tmp, "%s?ml_action=%s&ml_window=%s",
423 ml_session_script_name (session),
427 io_fprintf (io, "<a href=\"%s\"", link);
431 io_fputs (" title=\"", io);
432 ml_plaintext_print (io, w->title);
436 io_fprintf (io, ">%s</a>", w->text);
441 io_fprintf (io, "<span>%s</span>", w->text);
445 ml_menuitem_separator
446 new_ml_menuitem_separator (pool pool)
448 ml_menuitem_separator w = pmalloc (pool, sizeof *w);
450 w->ops = &menuitem_separator_ops;
457 menuitem_separator_repaint (void *vw, ml_session session,
458 const char *windowid, io_handle io)
460 io_fputs ("<hr />", io);