audio selection - fake
[enigma2.git] / lib / gui / elabel.cpp
1 #include <lib/gui/elabel.h>
2 #include <lib/gdi/font.h>
3
4 eLabel::eLabel(eWidget *parent): eWidget(parent)
5 {
6         ePtr<eWindowStyle> style;
7         getStyle(style);
8         
9         style->getFont(eWindowStyle::fontStatic, m_font);
10         
11                 /* default to topleft alignment */
12         m_valign = alignTop;
13         m_halign = alignLeft;
14         
15         m_have_foreground_color = 0;
16 }
17
18 int eLabel::event(int event, void *data, void *data2)
19 {
20         switch (event)
21         {
22         case evtPaint:
23         {
24                 ePtr<eWindowStyle> style;
25                 
26                 getStyle(style);
27                 
28                 eWidget::event(event, data, data2);
29
30                 gPainter &painter = *(gPainter*)data2;
31                 painter.setFont(m_font);
32                 style->setStyle(painter, eWindowStyle::styleLabel);
33                 
34                 if (m_have_foreground_color)
35                         painter.setForegroundColor(m_foreground_color);
36                 
37                 int flags = 0;
38                 if (m_valign == alignTop)
39                         flags |= gPainter::RT_VALIGN_TOP;
40                 else if (m_valign == alignCenter)
41                         flags |= gPainter::RT_VALIGN_CENTER;
42                 else if (m_valign == alignBottom)
43                         flags |= gPainter::RT_VALIGN_BOTTOM;
44
45                 if (m_halign == alignLeft)
46                         flags |= gPainter::RT_HALIGN_LEFT;
47                 else if (m_halign == alignCenter)
48                         flags |= gPainter::RT_HALIGN_CENTER;
49                 else if (m_halign == alignRight)
50                         flags |= gPainter::RT_HALIGN_RIGHT;
51                 else if (m_halign == alignBlock)
52                         flags |= gPainter::RT_HALIGN_BLOCK;
53                 
54                 flags |= gPainter::RT_WRAP;
55                 painter.renderText(eRect(0, 0, size().width(), size().height()), m_text, flags);
56                 
57                 return 0;
58         }
59         case evtChangedFont:
60         case evtChangedText:
61         case evtChangedAlignment:
62                 invalidate();
63                 return 0;
64         default:
65                 return eWidget::event(event, data, data2);
66         }
67 }
68
69 void eLabel::setText(const std::string &string)
70 {
71         if (m_text == string)
72                 return;
73         m_text = string;
74         event(evtChangedText);
75 }
76
77 void eLabel::setFont(gFont *font)
78 {
79         m_font = font;
80         event(evtChangedFont);
81 }
82
83 gFont* eLabel::getFont()
84 {
85         return m_font;
86 }
87
88 void eLabel::setVAlign(int align)
89 {
90         m_valign = align;
91         event(evtChangedAlignment);
92 }
93
94 void eLabel::setHAlign(int align)
95 {
96         m_halign = align;
97         event(evtChangedAlignment);
98 }
99
100 void eLabel::setForegroundColor(const gRGB &col)
101 {
102         m_foreground_color = col;
103         m_have_foreground_color = 1;
104 }
105
106 void eLabel::clearForegroundColor()
107 {
108         m_have_foreground_color = 0;
109 }
110
111 eSize eLabel::calculateSize()
112 {
113         ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
114         
115         p->setFont(m_font);
116         p->renderString(m_text, RS_WRAP);
117         
118         eRect bbox = p->getBoundBox();
119         return bbox.size();
120 }