ce4efaf17f02b505bbb3c76e4d2fb504d11695de
[vuplus_dvbapp] / lib / gui / elabel.cpp
1 #include <lib/gui/elabel.h>
2
3 #include <lib/gdi/fb.h>
4 #include <lib/gdi/font.h>
5 #include <lib/gdi/lcd.h>
6 #include <lib/gui/eskin.h>
7 #include <lib/base/init.h>
8 #include <lib/base/init_num.h>
9
10 eLabel::eLabel(eWidget *parent, int flags, int takefocus, const char *deco ):
11         eDecoWidget(parent, takefocus, deco), blitFlags(0), flags(flags),
12         para(0), align( eTextPara::dirLeft ), shortcutPixmap(0)
13 {
14 }
15
16 eLabel::~eLabel()
17 {
18         if (para)
19         {
20                 para->destroy();
21                 para=0;
22         }
23 }
24
25 void eLabel::setPixmapPosition( const ePoint &p )
26 {
27         pixmap_position = p;
28         invalidate();
29 }
30
31 void eLabel::validate( const eSize* s )
32 {
33         if (!para)
34         {
35                 if (s)
36                         para=new eTextPara( eRect(text_position.x(), text_position.y(), s->width() - text_position.x(), s->height() - text_position.y()));
37                 else
38                         para=new eTextPara( eRect(text_position.x(), text_position.y(), size.width() - text_position.x(), size.height() - text_position.y()));
39
40                 para->setFont(font);
41                 para->renderString(text, flags);
42                 para->realign(align);
43   }
44 }
45
46 void eLabel::invalidate()
47 {
48         if (para)
49         {
50                 para->destroy();
51                 para=0;
52         }
53         if (isVisible())
54                 eDecoWidget::invalidate();  // we must redraw...
55 }
56
57 void eLabel::setFlags(int flag)
58 {
59         flags|=flag;
60         if (flag)
61                 invalidate();
62 }
63
64 void eLabel::setBlitFlags( int flags )
65 {
66         blitFlags |= flags;
67 }
68
69 void eLabel::removeFlags(int flag)
70 {
71         flags &= ~flag;
72         if (flag)
73                 invalidate();
74 }
75
76 void eLabel::setAlign(int align)
77 {
78         this->align = align;
79         invalidate();
80 }
81
82 void eLabel::redrawWidget(gPainter *target, const eRect &rc)
83 {
84 /*      eDebug("decoStr = %s, text=%s, name=%s, %p left = %d, top = %d, width=%d, height = %d", strDeco?strDeco.c_str():"no", text?text.c_str():"no" , name?name.c_str():"no", this, this->getPosition().x(), this->getPosition().y(), this->getSize().width(), this->getSize().height() ); 
85         eDebug("renderContext left = %d, top = %d, width = %d, height = %d", rc.left(), rc.top(), rc.width(), rc.height() );*/
86
87         target->clip( gRegion(rc) );
88         eRect area=eRect(ePoint(0, 0), ePoint(width(), height()));
89 /*      eDebug("area left = %d, top = %d, width = %d, height = %d",
90                 area.left(), area.top(),
91                 area.width(), area.height() );*/
92
93         if (deco_selected && have_focus)
94         {
95                 deco_selected.drawDecoration(target, ePoint(width(), height()));
96                 area=crect_selected;
97         } else if (deco)
98         {
99                 deco.drawDecoration(target, ePoint(width(), height()));
100                 area=crect;
101         }
102 /*      eDebug("area left = %d, top = %d, width = %d, height = %d",
103                 area.left(), area.top(),
104                 area.width(), area.height() );*/
105
106         if (shortcutPixmap)
107         {
108                 //area.setWidth(area.width()-area.height());
109                 area.setX(area.height());
110         }
111
112         if (text.length())
113         {
114                 if ( area.size().height() < size.height() ||
115                                 area.size().width() < size.width() )
116                 {
117                 // then deco is drawed
118                         eSize s=area.size();
119                         validate( &s );
120                 } else
121                         validate();
122
123                 if (flags & flagVCenter)
124                         yOffs = ( (area.height() - para->getBoundBox().height() ) / 2 + 0) - para->getBoundBox().top();
125                 else
126                         yOffs = 0;
127
128                 eWidget *w;
129                 if ((blitFlags & BF_ALPHATEST) && (transparentBackgroundColor >= 0))
130                 {
131                         w=this;
132                         target->setBackgroundColor(transparentBackgroundColor);
133                 } else
134                 {
135                         w=getNonTransparentBackground();
136                         target->setBackgroundColor(w->getBackgroundColor());
137                 }
138                 target->setFont(font);
139                 target->renderPara(para, ePoint( area.left(), area.top()+yOffs) );
140         }
141         if (pixmap)
142         {
143 //              eDebug("blit pixmap area left=%d, top=%d, right=%d, bottom=%d", rc.left(), rc.top(), rc.right(), rc.bottom() );
144 //              eDebug("pixmap_pos x = %d, y = %d, xsize=%d, ysize=%d", pixmap_position.x(), pixmap_position.y(), pixmap->x, pixmap->y );
145                 target->blit(pixmap, shortcutPixmap?pixmap_position+ePoint( area.left(), 0):pixmap_position, area, (blitFlags & BF_ALPHATEST) ? gPixmap::blitAlphaTest : 0);
146         }
147         if (shortcutPixmap)
148                 target->blit(shortcutPixmap, 
149                                 ePoint((area.height()-shortcutPixmap->getSize().width())/2, area.top()+(area.height()-shortcutPixmap->getSize().height())/2),
150                                 eRect(),
151                                 gPixmap::blitAlphaTest);
152         target->clippop();
153 }
154
155 int eLabel::eventHandler(const eWidgetEvent &event)
156 {
157         switch (event.type)
158         {
159                 case eWidgetEvent::changedFont:
160                 case eWidgetEvent::changedText:
161                 if (para)
162                 {
163                         para->destroy();
164                         para=0;
165                 }
166                 if ( have_focus && deco_selected )
167                         eDecoWidget::invalidate( crect_selected );
168                 else if ( deco )
169                         eDecoWidget::invalidate( crect );
170                 else
171                         eDecoWidget::invalidate();
172         break;
173
174         case eWidgetEvent::changedSize:
175                 invalidate();
176         break;
177
178         default:
179                 return eDecoWidget::eventHandler(event);
180                 break;
181         }
182         return 1;
183 }
184
185 eSize eLabel::getExtend()
186 {
187         validate();
188         return eSize(para->getBoundBox().width()+(shortcutPixmap?shortcutPixmap->x*2:0), para->getBoundBox().height());
189 }
190
191 ePoint eLabel::getLeftTop()
192 {
193         validate();
194         return ePoint(para->getBoundBox().left(), para->getBoundBox().top());
195 }
196
197 int eLabel::setProperty(const eString &prop, const eString &value)
198 {
199         if (prop=="wrap" && value == "on")
200                 setFlags(RS_WRAP);
201         else if (prop=="alphatest" && value == "on")
202         {
203                 transparentBackgroundColor=getBackgroundColor();
204                 setBackgroundColor(-1);
205                 blitFlags |= BF_ALPHATEST;
206         } else if (prop=="align")
207         {
208                 if (value=="left")
209                         setAlign(eTextPara::dirLeft);
210                 else if (value=="center")
211                         setAlign(eTextPara::dirCenter);
212                 else if (value=="right")
213                         setAlign(eTextPara::dirRight);
214                 else if (value=="block")
215                         setAlign(eTextPara::dirBlock);
216                 else
217                         setAlign(eTextPara::dirLeft);
218         }
219         else if (prop=="vcenter")
220                 setFlags( flagVCenter );
221         else if (prop == "shortcut")
222         {
223                 setShortcutPixmap(value);
224                 return eWidget::setProperty(prop, value);
225         } else
226                 return eDecoWidget::setProperty(prop, value);
227         return 0;
228 }
229
230 void eLabel::setShortcutPixmap(const eString &shortcut)
231 {
232         eSkin::getActive()->queryImage(shortcutPixmap, "shortcut." + shortcut);
233 }
234
235 static eWidget *create_eLabel(eWidget *parent)
236 {
237         return new eLabel(parent);
238 }
239
240 class eLabelSkinInit
241 {
242 public:
243         eLabelSkinInit()
244         {
245                 eSkin::addWidgetCreator("eLabel", create_eLabel);
246         }
247         ~eLabelSkinInit()
248         {
249                 eSkin::removeWidgetCreator("eLabel", create_eLabel);
250         }
251 };
252
253 eAutoInitP0<eLabelSkinInit> init_eLabelSkinInit(eAutoInitNumbers::guiobject, "eLabel");