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