better marking of current cursor in Components.Input
[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
41                         para->setGlyphFlag(m_pos, GS_INVERT);
42                         eRect bbox;
43                         bbox = para->getGlyphBBox(m_pos);
44                         printf("BBOX: %d %d %d %d\n", bbox.left(), 0, bbox.width(), size().height());
45                         bbox = eRect(bbox.left(), 0, bbox.width(), size().height());
46                         painter.fill(bbox);
47
48                         painter.renderPara(para, ePoint(0, 0));
49                         return 0;
50                 }
51                 else
52                 {               
53                         painter.setFont(m_font);
54                         style->setStyle(painter, eWindowStyle::styleLabel);
55                         
56                         if (m_have_foreground_color)
57                                 painter.setForegroundColor(m_foreground_color);
58                         
59                         int flags = 0;
60                         if (m_valign == alignTop)
61                                 flags |= gPainter::RT_VALIGN_TOP;
62                         else if (m_valign == alignCenter)
63                                 flags |= gPainter::RT_VALIGN_CENTER;
64                         else if (m_valign == alignBottom)
65                                 flags |= gPainter::RT_VALIGN_BOTTOM;
66         
67                         if (m_halign == alignLeft)
68                                 flags |= gPainter::RT_HALIGN_LEFT;
69                         else if (m_halign == alignCenter)
70                                 flags |= gPainter::RT_HALIGN_CENTER;
71                         else if (m_halign == alignRight)
72                                 flags |= gPainter::RT_HALIGN_RIGHT;
73                         else if (m_halign == alignBlock)
74                                 flags |= gPainter::RT_HALIGN_BLOCK;
75                         
76                         flags |= gPainter::RT_WRAP;
77                         painter.renderText(eRect(0, 0, size().width(), size().height()), m_text, flags);
78                         
79                         return 0;
80                 }
81         }
82         case evtChangedFont:
83         case evtChangedText:
84         case evtChangedAlignment:
85         case evtChangedMarkedPos:
86                 invalidate();
87                 return 0;
88         default:
89                 return eWidget::event(event, data, data2);
90         }
91 }
92
93 void eLabel::setText(const std::string &string)
94 {
95         if (m_text == string)
96                 return;
97         m_text = string;
98         event(evtChangedText);
99 }
100
101 void eLabel::setMarkedPos(int markedPos)
102 {
103         m_pos = markedPos;
104         event(evtChangedMarkedPos);
105 }
106
107 void eLabel::setFont(gFont *font)
108 {
109         m_font = font;
110         event(evtChangedFont);
111 }
112
113 gFont* eLabel::getFont()
114 {
115         return m_font;
116 }
117
118 void eLabel::setVAlign(int align)
119 {
120         m_valign = align;
121         event(evtChangedAlignment);
122 }
123
124 void eLabel::setHAlign(int align)
125 {
126         m_halign = align;
127         event(evtChangedAlignment);
128 }
129
130 void eLabel::setForegroundColor(const gRGB &col)
131 {
132         m_foreground_color = col;
133         m_have_foreground_color = 1;
134 }
135
136 void eLabel::clearForegroundColor()
137 {
138         m_have_foreground_color = 0;
139 }
140
141 eSize eLabel::calculateSize()
142 {
143         ePtr<eTextPara> p = new eTextPara(eRect(0, 0, size().width(), size().height()));
144         
145         p->setFont(m_font);
146         p->renderString(m_text, RS_WRAP);
147         
148         eRect bbox = p->getBoundBox();
149         return bbox.size();
150 }