Add to git.
[monolith.git] / src / ml_close_button.c
1 /* Monolith close 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_close_button.c,v 1.3 2002/11/13 21:41:01 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_close_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 close_button_ops =
35   {
36     repaint: repaint,
37     properties: properties,
38   };
39
40 struct ml_close_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 reload_opener;            /* If set, reload the opener window. */
46 };
47
48 static struct ml_widget_property properties[] =
49   {
50     { name: "text",
51       offset: ml_offsetof (struct ml_close_button, text),
52       type: ML_PROP_STRING },
53     { name: "button.reload-opener",
54       offset: ml_offsetof (struct ml_close_button, reload_opener),
55       type: ML_PROP_BOOL },
56     { 0 }
57   };
58
59 ml_close_button
60 new_ml_close_button (pool pool, const char *text)
61 {
62   ml_close_button w = pmalloc (pool, sizeof *w);
63
64   w->ops = &close_button_ops;
65   w->pool = pool;
66   w->text = text;
67   w->reload_opener = 0;
68
69   return w;
70 }
71
72 static void
73 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
74 {
75   ml_close_button w = (ml_close_button) vw;
76
77   if (w->text)
78     {
79       const char *js;
80
81       if (!w->reload_opener)
82         js = "top.close()";
83       else
84         js = "window.opener.location.reload(); top.close()";
85
86       io_fprintf (io,
87                   "<a class=\"ml_button\" href=\"javascript:%s\">%s</a>",
88                   js, w->text);
89     }
90 }