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 < 0) || (m_pos >= glyphs))
55 eWarning("glyph index %d in eLabel out of bounds!", m_pos);
58 para->setGlyphFlag(m_pos, GS_INVERT);
60 bbox = para->getGlyphBBox(m_pos);
61 bbox = eRect(bbox.left(), 0, bbox.width(), size().height());
65 painter.renderPara(para, ePoint(0, 0));
69 painter.setFont(m_font);
70 style->setStyle(painter, eWindowStyle::styleLabel);
72 if (m_have_shadow_color)
73 painter.setForegroundColor(m_shadow_color);
74 else if (m_have_foreground_color)
75 painter.setForegroundColor(m_foreground_color);
78 if (m_valign == alignTop)
79 flags |= gPainter::RT_VALIGN_TOP;
80 else if (m_valign == alignCenter)
81 flags |= gPainter::RT_VALIGN_CENTER;
82 else if (m_valign == alignBottom)
83 flags |= gPainter::RT_VALIGN_BOTTOM;
85 if (m_halign == alignLeft)
86 flags |= gPainter::RT_HALIGN_LEFT;
87 else if (m_halign == alignCenter)
88 flags |= gPainter::RT_HALIGN_CENTER;
89 else if (m_halign == alignRight)
90 flags |= gPainter::RT_HALIGN_RIGHT;
91 else if (m_halign == alignBlock)
92 flags |= gPainter::RT_HALIGN_BLOCK;
95 flags |= gPainter::RT_WRAP;
97 /* if we don't have shadow, m_shadow_offset will be 0,0 */
98 painter.renderText(eRect(-m_shadow_offset.x(), -m_shadow_offset.y(), size().width(), size().height()), m_text, flags);
100 if (m_have_shadow_color)
102 if (!m_have_foreground_color)
103 style->setStyle(painter, eWindowStyle::styleLabel);
105 painter.setForegroundColor(m_foreground_color);
106 painter.setBackgroundColor(m_shadow_color);
107 painter.renderText(eRect(0, 0, size().width(), size().height()), m_text, flags);
115 case evtChangedAlignment:
116 case evtChangedMarkedPos:
120 return eWidget::event(event, data, data2);
124 void eLabel::setText(const std::string &string)
126 if (m_text == string)
129 event(evtChangedText);
132 void eLabel::setMarkedPos(int markedPos)
135 event(evtChangedMarkedPos);
138 void eLabel::setFont(gFont *font)
141 event(evtChangedFont);
144 gFont* eLabel::getFont()
149 void eLabel::setVAlign(int align)
152 event(evtChangedAlignment);
155 void eLabel::setHAlign(int align)
158 event(evtChangedAlignment);
161 void eLabel::setForegroundColor(const gRGB &col)
163 if ((!m_have_foreground_color) || !(m_foreground_color == col))
165 m_foreground_color = col;
166 m_have_foreground_color = 1;
171 void eLabel::setShadowColor(const gRGB &col)
173 if ((!m_have_shadow_color) || !(m_shadow_color == col))
175 m_shadow_color = col;
176 m_have_shadow_color = 1;
181 void eLabel::setShadowOffset(const ePoint &offset)
183 m_shadow_offset = offset;
186 void eLabel::setNoWrap(int nowrap)
188 if (m_nowrap != nowrap)
195 void eLabel::clearForegroundColor()
197 if (m_have_foreground_color)
199 m_have_foreground_color = 0;
204 eSize eLabel::calculateSize()
206 ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
209 p->renderString(m_text.empty()?0:m_text.c_str(), m_nowrap ? 0 : RS_WRAP);
211 eRect bbox = p->getBoundBox();