1 #include <lib/gui/elabel.h>
2 #include <lib/gdi/font.h>
4 eLabel::eLabel(eWidget *parent, int markedPos): eWidget(parent)
7 ePtr<eWindowStyle> style;
10 style->getFont(eWindowStyle::fontStatic, m_font);
12 /* default to topleft alignment */
16 m_have_foreground_color = 0;
17 m_have_shadow_color = 0;
22 int eLabel::event(int event, void *data, void *data2)
28 ePtr<eWindowStyle> style;
32 eWidget::event(event, data, data2);
34 gPainter &painter = *(gPainter*)data2;
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);
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);
52 int glyphs = para->size();
54 if (m_pos == -2) { /* All glyphs inverted */
56 int left = 0, right = 0;
57 for (int i=0; i<glyphs; i++)
58 para->setGlyphFlag(i, GS_INVERT);
60 bbox = para->getGlyphBBox(0);
62 bbox = para->getGlyphBBox(glyphs-1);
63 right = bbox.left() + bbox.width();
65 bbox = eRect(left, 0, right-left, size().height());
67 } else if ((m_pos < 0) || (m_pos >= glyphs))
68 eWarning("glyph index %d in eLabel out of bounds!", m_pos);
71 para->setGlyphFlag(m_pos, GS_INVERT);
73 bbox = para->getGlyphBBox(m_pos);
74 bbox = eRect(bbox.left(), 0, bbox.width(), size().height());
78 painter.renderPara(para, ePoint(0, 0));
82 painter.setFont(m_font);
83 style->setStyle(painter, eWindowStyle::styleLabel);
85 if (m_have_shadow_color)
86 painter.setForegroundColor(m_shadow_color);
87 else if (m_have_foreground_color)
88 painter.setForegroundColor(m_foreground_color);
91 if (m_valign == alignTop)
92 flags |= gPainter::RT_VALIGN_TOP;
93 else if (m_valign == alignCenter)
94 flags |= gPainter::RT_VALIGN_CENTER;
95 else if (m_valign == alignBottom)
96 flags |= gPainter::RT_VALIGN_BOTTOM;
98 if (m_halign == alignLeft)
99 flags |= gPainter::RT_HALIGN_LEFT;
100 else if (m_halign == alignCenter)
101 flags |= gPainter::RT_HALIGN_CENTER;
102 else if (m_halign == alignRight)
103 flags |= gPainter::RT_HALIGN_RIGHT;
104 else if (m_halign == alignBlock)
105 flags |= gPainter::RT_HALIGN_BLOCK;
108 flags |= gPainter::RT_WRAP;
110 /* if we don't have shadow, m_shadow_offset will be 0,0 */
111 painter.renderText(eRect(-m_shadow_offset.x(), -m_shadow_offset.y(), size().width(), size().height()), m_text, flags);
113 if (m_have_shadow_color)
115 if (!m_have_foreground_color)
116 style->setStyle(painter, eWindowStyle::styleLabel);
118 painter.setForegroundColor(m_foreground_color);
119 painter.setBackgroundColor(m_shadow_color);
120 painter.renderText(eRect(0, 0, size().width(), size().height()), m_text, flags);
128 case evtChangedAlignment:
129 case evtChangedMarkedPos:
133 return eWidget::event(event, data, data2);
137 void eLabel::setText(const std::string &string)
139 if (m_text == string)
142 event(evtChangedText);
145 void eLabel::setMarkedPos(int markedPos)
148 event(evtChangedMarkedPos);
151 void eLabel::setFont(gFont *font)
154 event(evtChangedFont);
157 gFont* eLabel::getFont()
162 void eLabel::setVAlign(int align)
165 event(evtChangedAlignment);
168 void eLabel::setHAlign(int align)
171 event(evtChangedAlignment);
174 void eLabel::setForegroundColor(const gRGB &col)
176 if ((!m_have_foreground_color) || !(m_foreground_color == col))
178 m_foreground_color = col;
179 m_have_foreground_color = 1;
184 void eLabel::setShadowColor(const gRGB &col)
186 if ((!m_have_shadow_color) || !(m_shadow_color == col))
188 m_shadow_color = col;
189 m_have_shadow_color = 1;
194 void eLabel::setShadowOffset(const ePoint &offset)
196 m_shadow_offset = offset;
199 void eLabel::setNoWrap(int nowrap)
201 if (m_nowrap != nowrap)
208 void eLabel::clearForegroundColor()
210 if (m_have_foreground_color)
212 m_have_foreground_color = 0;
217 eSize eLabel::calculateSize()
219 ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
222 p->renderString(m_text.empty()?0:m_text.c_str(), m_nowrap ? 0 : RS_WRAP);
224 eRect bbox = p->getBoundBox();