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