Add to git.
[monolith.git] / src / ml_text_label.c
1 /* Monolith text label 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_text_label.c,v 1.6 2002/11/23 17:31:01 rich Exp $
19  */
20
21 #include "config.h"
22
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26
27 #include <pthr_iolib.h>
28
29 #include "monolith.h"
30 #include "ml_widget.h"
31 #include "ml_smarttext.h"
32 #include "ml_text_label.h"
33
34 static void repaint (void *, ml_session, const char *, io_handle);
35 static struct ml_widget_property properties[];
36
37 struct ml_widget_operations text_label_ops =
38   {
39     repaint: repaint,
40     properties: properties,
41   };
42
43 struct ml_text_label
44 {
45   struct ml_widget_operations *ops;
46   pool pool;                    /* Pool for allocations. */
47   const char *text;             /* Text to be displayed. */
48
49   /* Various style information. */
50   const char *text_align;
51   const char *colour;
52   const char *font_weight;
53   const char *font_size;
54 };
55
56 static struct ml_widget_property properties[] =
57   {
58     { name: "text",
59       offset: ml_offsetof (struct ml_text_label, text),
60       type: ML_PROP_STRING },
61     { name: "text.align",
62       offset: ml_offsetof (struct ml_text_label, text_align),
63       type: ML_PROP_STRING },
64     { name: "color",
65       offset: ml_offsetof (struct ml_text_label, colour),
66       type: ML_PROP_STRING },
67     { name: "font.weight",
68       offset: ml_offsetof (struct ml_text_label, font_weight),
69       type: ML_PROP_STRING },
70     { name: "font.size",
71       offset: ml_offsetof (struct ml_text_label, font_size),
72       type: ML_PROP_STRING },
73     { 0 },
74   };
75
76 ml_text_label
77 new_ml_text_label (pool pool, const char *text)
78 {
79   ml_text_label w = pmalloc (pool, sizeof *w);
80
81   w->ops = &text_label_ops;
82   w->pool = pool;
83   w->text = text;
84
85   w->text_align = 0;            /* NULL means default for all these. */
86   w->colour = 0;
87   w->font_weight = 0;
88   w->font_size = 0;
89
90   return w;
91 }
92
93 static void
94 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
95 {
96   ml_text_label w = (ml_text_label) vw;
97
98   if (w->text)
99     {
100       if (w->text_align || w->colour || w->font_weight || w->font_size)
101         {
102           io_fprintf (io, "<span style=\"");
103           if (w->text_align)
104             io_fprintf (io, "text-align: %s;", w->text_align);
105           if (w->colour)
106             io_fprintf (io, "color: %s;", w->colour);
107           if (w->font_weight)
108             io_fprintf (io, "font-weight: %s;", w->font_weight);
109           if (w->font_size)
110             io_fprintf (io, "font-size: %s;", w->font_size);
111           io_fprintf (io, "\">");
112         }
113
114       ml_plaintext_print (io, w->text);
115
116       if (w->text_align || w->colour || w->font_weight || w->font_size)
117         {
118           io_fprintf (io, "</span>");
119         }
120     }
121 }