fix warning, redraw on changed foreground color
[openblackhole/openblackhole-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 }
18
19 int eLabel::event(int event, void *data, void *data2)
20 {
21         switch (event)
22         {
23         case evtPaint:
24         {
25                 ePtr<eWindowStyle> style;
26                 
27                 getStyle(style);
28                 
29                 eWidget::event(event, data, data2);
30
31                 gPainter &painter = *(gPainter*)data2;
32                 
33                 if (m_pos != -1)
34                 {
35                         style->setStyle(painter, eWindowStyle::styleLabel);
36                         ePtr<eTextPara> para = new eTextPara(eRect(0, 0, size().width(), size().height()));
37                         para->setFont(m_font);
38                         para->renderString(m_text, 0);
39                         para->realign(eTextPara::dirLeft);
40                         int glyphs = para->size();
41
42                         if ((m_pos < 0) || (m_pos >= glyphs))
43                                 eWarning("glyph index %d in eLabel out of bounds!", m_pos);
44                         else
45                         {
46                                 para->setGlyphFlag(m_pos, GS_INVERT);
47                                 eRect bbox;
48                                 bbox = para->getGlyphBBox(m_pos);
49                                 bbox = eRect(bbox.left(), 0, bbox.width(), size().height());
50                                 painter.fill(bbox);
51                         }
52
53                         painter.renderPara(para, ePoint(0, 0));
54                         return 0;
55                 }
56                 else
57                 {               
58                         painter.setFont(m_font);
59                         style->setStyle(painter, eWindowStyle::styleLabel);
60                         
61                         if (m_have_foreground_color)
62                                 painter.setForegroundColor(m_foreground_color);
63                         
64                         int flags = 0;
65                         if (m_valign == alignTop)
66                                 flags |= gPainter::RT_VALIGN_TOP;
67                         else if (m_valign == alignCenter)
68                                 flags |= gPainter::RT_VALIGN_CENTER;
69                         else if (m_valign == alignBottom)
70                                 flags |= gPainter::RT_VALIGN_BOTTOM;
71         
72                         if (m_halign == alignLeft)
73                                 flags |= gPainter::RT_HALIGN_LEFT;
74                         else if (m_halign == alignCenter)
75                                 flags |= gPainter::RT_HALIGN_CENTER;
76                         else if (m_halign == alignRight)
77                                 flags |= gPainter::RT_HALIGN_RIGHT;
78                         else if (m_halign == alignBlock)
79                                 flags |= gPainter::RT_HALIGN_BLOCK;
80                         
81                         flags |= gPainter::RT_WRAP;
82                         painter.renderText(eRect(0, 0, size().width(), size().height()), m_text, flags);
83                         
84                         return 0;
85                 }
86         }
87         case evtChangedFont:
88         case evtChangedText:
89         case evtChangedAlignment:
90         case evtChangedMarkedPos:
91                 invalidate();
92                 return 0;
93         default:
94                 return eWidget::event(event, data, data2);
95         }
96 }
97
98 void eLabel::setText(const std::string &string)
99 {
100         if (m_text == string)
101                 return;
102         m_text = string;
103         event(evtChangedText);
104 }
105
106 void eLabel::setMarkedPos(int markedPos)
107 {
108         m_pos = markedPos;
109         event(evtChangedMarkedPos);
110 }
111
112 void eLabel::setFont(gFont *font)
113 {
114         m_font = font;
115         event(evtChangedFont);
116 }
117
118 gFont* eLabel::getFont()
119 {
120         return m_font;
121 }
122
123 void eLabel::setVAlign(int align)
124 {
125         m_valign = align;
126         event(evtChangedAlignment);
127 }
128
129 void eLabel::setHAlign(int align)
130 {
131         m_halign = align;
132         event(evtChangedAlignment);
133 }
134
135 void eLabel::setForegroundColor(const gRGB &col)
136 {
137         if ((!m_have_foreground_color) || !(m_foreground_color == col))
138         {
139                 m_foreground_color = col;
140                 m_have_foreground_color = 1;
141                 invalidate();
142         }
143 }
144
145 void eLabel::clearForegroundColor()
146 {
147         if (m_have_foreground_color)
148         {
149                 m_have_foreground_color = 0;
150                 invalidate();
151         }
152 }
153
154 eSize eLabel::calculateSize()
155 {
156         ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
157         
158         p->setFont(m_font);
159         p->renderString(m_text, RS_WRAP);
160         
161         eRect bbox = p->getBoundBox();
162         return bbox.size();
163 }