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;
20 int eLabel::event(int event, void *data, void *data2)
26 ePtr<eWindowStyle> style;
30 eWidget::event(event, data, data2);
32 gPainter &painter = *(gPainter*)data2;
36 style->setStyle(painter, eWindowStyle::styleLabel);
37 ePtr<eTextPara> para = new eTextPara(eRect(0, 0, size().width(), size().height()));
38 para->setFont(m_font);
39 para->renderString(m_text.empty()?0:m_text.c_str(), 0);
41 if (m_halign == alignLeft)
42 para->realign(eTextPara::dirLeft);
43 else if (m_halign == alignCenter)
44 para->realign(eTextPara::dirCenter);
45 else if (m_halign == alignRight)
46 para->realign(eTextPara::dirRight);
47 else if (m_halign == alignBlock)
48 para->realign(eTextPara::dirBlock);
50 int glyphs = para->size();
52 if ((m_pos < 0) || (m_pos >= glyphs))
53 eWarning("glyph index %d in eLabel out of bounds!", m_pos);
56 para->setGlyphFlag(m_pos, GS_INVERT);
58 bbox = para->getGlyphBBox(m_pos);
59 bbox = eRect(bbox.left(), 0, bbox.width(), size().height());
63 painter.renderPara(para, ePoint(0, 0));
67 painter.setFont(m_font);
68 style->setStyle(painter, eWindowStyle::styleLabel);
70 if (m_have_shadow_color)
71 painter.setForegroundColor(m_shadow_color);
72 else if (m_have_foreground_color)
73 painter.setForegroundColor(m_foreground_color);
76 if (m_valign == alignTop)
77 flags |= gPainter::RT_VALIGN_TOP;
78 else if (m_valign == alignCenter)
79 flags |= gPainter::RT_VALIGN_CENTER;
80 else if (m_valign == alignBottom)
81 flags |= gPainter::RT_VALIGN_BOTTOM;
83 if (m_halign == alignLeft)
84 flags |= gPainter::RT_HALIGN_LEFT;
85 else if (m_halign == alignCenter)
86 flags |= gPainter::RT_HALIGN_CENTER;
87 else if (m_halign == alignRight)
88 flags |= gPainter::RT_HALIGN_RIGHT;
89 else if (m_halign == alignBlock)
90 flags |= gPainter::RT_HALIGN_BLOCK;
92 flags |= gPainter::RT_WRAP;
94 /* if we don't have shadow, m_shadow_offset will be 0,0 */
95 painter.renderText(eRect(-m_shadow_offset.x(), -m_shadow_offset.y(), size().width(), size().height()), m_text, flags);
97 if (m_have_shadow_color)
99 if (!m_have_foreground_color)
100 style->setStyle(painter, eWindowStyle::styleLabel);
102 painter.setForegroundColor(m_foreground_color);
103 painter.setBackgroundColor(m_shadow_color);
104 painter.renderText(eRect(0, 0, size().width(), size().height()), m_text, flags);
112 case evtChangedAlignment:
113 case evtChangedMarkedPos:
117 return eWidget::event(event, data, data2);
121 void eLabel::setText(const std::string &string)
123 if (m_text == string)
126 event(evtChangedText);
129 void eLabel::setMarkedPos(int markedPos)
132 event(evtChangedMarkedPos);
135 void eLabel::setFont(gFont *font)
138 event(evtChangedFont);
141 gFont* eLabel::getFont()
146 void eLabel::setVAlign(int align)
149 event(evtChangedAlignment);
152 void eLabel::setHAlign(int align)
155 event(evtChangedAlignment);
158 void eLabel::setForegroundColor(const gRGB &col)
160 if ((!m_have_foreground_color) || !(m_foreground_color == col))
162 m_foreground_color = col;
163 m_have_foreground_color = 1;
168 void eLabel::setShadowColor(const gRGB &col)
170 if ((!m_have_shadow_color) || !(m_shadow_color == col))
172 m_shadow_color = col;
173 m_have_shadow_color = 1;
178 void eLabel::setShadowOffset(const ePoint &offset)
180 m_shadow_offset = offset;
183 void eLabel::clearForegroundColor()
185 if (m_have_foreground_color)
187 m_have_foreground_color = 0;
192 eSize eLabel::calculateSize()
194 ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
197 p->renderString(m_text.empty()?0:m_text.c_str(), RS_WRAP);
199 eRect bbox = p->getBoundBox();