a bit more/nicer fakes
[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 void eLabel::setVAlign(int align)
84 {
85         m_valign = align;
86         event(evtChangedAlignment);
87 }
88
89 void eLabel::setHAlign(int align)
90 {
91         m_halign = align;
92         event(evtChangedAlignment);
93 }
94
95 void eLabel::setForegroundColor(const gRGB &col)
96 {
97         m_foreground_color = col;
98         m_have_foreground_color = 1;
99 }
100
101 void eLabel::clearForegroundColor()
102 {
103         m_have_foreground_color = 0;
104 }
105
106 eSize eLabel::calculateSize()
107 {
108         ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
109         
110         p->setFont(m_font);
111         p->renderString(m_text, RS_WRAP);
112         
113         eRect bbox = p->getBoundBox();
114         return bbox.size();
115 }