fix
[enigma2.git] / lib / gui / elabel.cpp
1 #include <lib/gui/elabel.h>
2 #include <lib/gdi/font.h>
3
4 eLabel::eLabel(eWidget *parent, int markedPos): eWidget(parent)
5 {
6         m_pos = markedPos;
7         ePtr<eWindowStyle> style;
8         getStyle(style);
9         
10         style->getFont(eWindowStyle::fontStatic, m_font);
11         
12                 /* default to topleft alignment */
13         m_valign = alignTop;
14         m_halign = alignLeft;
15         
16         m_have_foreground_color = 0;
17         m_have_shadow_color = 0;
18 }
19
20 int eLabel::event(int event, void *data, void *data2)
21 {
22         switch (event)
23         {
24         case evtPaint:
25         {
26                 ePtr<eWindowStyle> style;
27                 
28                 getStyle(style);
29                 
30                 eWidget::event(event, data, data2);
31
32                 gPainter &painter = *(gPainter*)data2;
33                 
34                 if (m_pos != -1)
35                 {
36                         style->setStyle(painter, eWindowStyle::styleLabel);
37                         ePtr<eTextPara> para = new eTextPara(eRect(0, 0, size().width(), size().height()));
38                         para->setFont(m_font);
39                         para->renderString(m_text, 0);
40                         para->realign(eTextPara::dirLeft);
41                         int glyphs = para->size();
42
43                         if ((m_pos < 0) || (m_pos >= glyphs))
44                                 eWarning("glyph index %d in eLabel out of bounds!", m_pos);
45                         else
46                         {
47                                 para->setGlyphFlag(m_pos, GS_INVERT);
48                                 eRect bbox;
49                                 bbox = para->getGlyphBBox(m_pos);
50                                 bbox = eRect(bbox.left(), 0, bbox.width(), size().height());
51                                 painter.fill(bbox);
52                         }
53
54                         painter.renderPara(para, ePoint(0, 0));
55                         return 0;
56                 } else
57                 {
58                         painter.setFont(m_font);
59                         style->setStyle(painter, eWindowStyle::styleLabel);
60                         
61                         if (m_have_shadow_color)
62                                 painter.setForegroundColor(m_shadow_color);
63                         else if (m_have_foreground_color)
64                                 painter.setForegroundColor(m_foreground_color);
65                         
66                         int flags = 0;
67                         if (m_valign == alignTop)
68                                 flags |= gPainter::RT_VALIGN_TOP;
69                         else if (m_valign == alignCenter)
70                                 flags |= gPainter::RT_VALIGN_CENTER;
71                         else if (m_valign == alignBottom)
72                                 flags |= gPainter::RT_VALIGN_BOTTOM;
73         
74                         if (m_halign == alignLeft)
75                                 flags |= gPainter::RT_HALIGN_LEFT;
76                         else if (m_halign == alignCenter)
77                                 flags |= gPainter::RT_HALIGN_CENTER;
78                         else if (m_halign == alignRight)
79                                 flags |= gPainter::RT_HALIGN_RIGHT;
80                         else if (m_halign == alignBlock)
81                                 flags |= gPainter::RT_HALIGN_BLOCK;
82                         
83                         flags |= gPainter::RT_WRAP;
84                         
85                                 /* if we don't have shadow, m_shadow_offset will be 0,0 */
86                         painter.renderText(eRect(-m_shadow_offset.x(), -m_shadow_offset.y(), size().width(), size().height()), m_text, flags);
87                         
88                         if (m_have_shadow_color)
89                         {
90                                 if (!m_have_foreground_color)
91                                         style->setStyle(painter, eWindowStyle::styleLabel);
92                                 else
93                                         painter.setForegroundColor(m_foreground_color);
94                                 painter.setBackgroundColor(m_shadow_color);
95                                 painter.renderText(eRect(0, 0, size().width(), size().height()), m_text, flags);
96                         }
97                         
98                         return 0;
99                 }
100         }
101         case evtChangedFont:
102         case evtChangedText:
103         case evtChangedAlignment:
104         case evtChangedMarkedPos:
105                 invalidate();
106                 return 0;
107         default:
108                 return eWidget::event(event, data, data2);
109         }
110 }
111
112 void eLabel::setText(const std::string &string)
113 {
114         if (m_text == string)
115                 return;
116         m_text = string;
117         event(evtChangedText);
118 }
119
120 void eLabel::setMarkedPos(int markedPos)
121 {
122         m_pos = markedPos;
123         event(evtChangedMarkedPos);
124 }
125
126 void eLabel::setFont(gFont *font)
127 {
128         m_font = font;
129         event(evtChangedFont);
130 }
131
132 gFont* eLabel::getFont()
133 {
134         return m_font;
135 }
136
137 void eLabel::setVAlign(int align)
138 {
139         m_valign = align;
140         event(evtChangedAlignment);
141 }
142
143 void eLabel::setHAlign(int align)
144 {
145         m_halign = align;
146         event(evtChangedAlignment);
147 }
148
149 void eLabel::setForegroundColor(const gRGB &col)
150 {
151         if ((!m_have_foreground_color) || !(m_foreground_color == col))
152         {
153                 m_foreground_color = col;
154                 m_have_foreground_color = 1;
155                 invalidate();
156         }
157 }
158
159 void eLabel::setShadowColor(const gRGB &col)
160 {
161         if ((!m_have_shadow_color) || !(m_shadow_color == col))
162         {
163                 m_shadow_color = col;
164                 m_have_shadow_color = 1;
165                 invalidate();
166         }
167 }
168
169 void eLabel::setShadowOffset(const ePoint &offset)
170 {
171         m_shadow_offset = offset;
172 }
173
174 void eLabel::clearForegroundColor()
175 {
176         if (m_have_foreground_color)
177         {
178                 m_have_foreground_color = 0;
179                 invalidate();
180         }
181 }
182
183 eSize eLabel::calculateSize()
184 {
185         ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
186         
187         p->setFont(m_font);
188         p->renderString(m_text, RS_WRAP);
189         
190         eRect bbox = p->getBoundBox();
191         return bbox.size();
192 }