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;
19 int eLabel::event(int event, void *data, void *data2)
25 ePtr<eWindowStyle> style;
29 eWidget::event(event, data, data2);
31 gPainter &painter = *(gPainter*)data2;
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();
42 if ((m_pos < 0) || (m_pos >= glyphs))
43 eWarning("glyph index %d in eLabel out of bounds!", m_pos);
46 para->setGlyphFlag(m_pos, GS_INVERT);
48 bbox = para->getGlyphBBox(m_pos);
49 bbox = eRect(bbox.left(), 0, bbox.width(), size().height());
53 painter.renderPara(para, ePoint(0, 0));
58 painter.setFont(m_font);
59 style->setStyle(painter, eWindowStyle::styleLabel);
61 if (m_have_foreground_color)
62 painter.setForegroundColor(m_foreground_color);
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;
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;
81 flags |= gPainter::RT_WRAP;
82 painter.renderText(eRect(0, 0, size().width(), size().height()), m_text, flags);
89 case evtChangedAlignment:
90 case evtChangedMarkedPos:
94 return eWidget::event(event, data, data2);
98 void eLabel::setText(const std::string &string)
100 if (m_text == string)
103 event(evtChangedText);
106 void eLabel::setMarkedPos(int markedPos)
109 event(evtChangedMarkedPos);
112 void eLabel::setFont(gFont *font)
115 event(evtChangedFont);
118 gFont* eLabel::getFont()
123 void eLabel::setVAlign(int align)
126 event(evtChangedAlignment);
129 void eLabel::setHAlign(int align)
132 event(evtChangedAlignment);
135 void eLabel::setForegroundColor(const gRGB &col)
137 if ((!m_have_foreground_color) || !(m_foreground_color == col))
139 m_foreground_color = col;
140 m_have_foreground_color = 1;
145 void eLabel::clearForegroundColor()
147 if (m_have_foreground_color)
149 m_have_foreground_color = 0;
154 eSize eLabel::calculateSize()
156 ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
159 p->renderString(m_text, RS_WRAP);
161 eRect bbox = p->getBoundBox();