add video widget
[enigma2.git] / lib / gui / ewidget.cpp
1 #include <lib/gui/ewidget.h>
2 #include <lib/gui/ewidgetdesktop.h>
3
4 extern void dumpRegion(const gRegion &region);
5
6 eWidget::eWidget(eWidget *parent): m_animation(this), m_parent(parent ? parent->child() : 0)
7 {
8         m_vis = 0;
9         m_desktop = 0;
10         m_have_background_color = 0;
11         m_z_position = 0;
12         
13         m_client_offset = eSize(0, 0);
14         
15         if (m_parent)
16                 m_vis = wVisShow;
17         
18         if (m_parent)
19         {
20                 insertIntoParent();
21                 m_parent->getStyle(m_style);
22         }
23
24         m_current_focus = 0;
25         m_focus_owner = 0;
26 }
27
28 void eWidget::move(ePoint pos)
29 {
30         pos = pos + m_client_offset;
31         
32         if (m_position == pos)
33                 return;
34
35                         /* ?? what about native move support? */
36         invalidate();
37
38         m_position = pos;
39         
40         event(evtChangedPosition);
41         recalcClipRegionsWhenVisible();
42         
43                 /* try native move if supported. */
44         if ((m_vis & wVisShow) && ((!m_desktop) || m_desktop->movedWidget(this)))
45                 invalidate();
46 }
47
48 void eWidget::resize(eSize size)
49 {
50                 /* same strategy as with move: we first check if
51                    the size changed at all, and if it did, we
52                    invalidate both the old and new area. 
53                    TODO: check if either the old or new area
54                    fits into the other completely, and invalidate
55                    only once. */
56         eSize old_size = m_size;
57         eSize old_offset = m_client_offset;
58         m_client_offset = eSize(0, 0);
59         event(evtWillChangeSize, &size, &m_client_offset);
60         if (old_size == m_size)
61                 return;
62         
63         move(position() - old_offset);
64         
65         invalidate();
66         event(evtChangedSize);
67         recalcClipRegionsWhenVisible(); 
68         invalidate();
69 }
70
71 void eWidget::invalidate(const gRegion &region)
72 {
73                 /* we determine the area to redraw, and re-position this
74                    area to the absolute position, and then call the
75                    desktop's invalidate() with that, which adds this
76                    area into the dirty region. */
77         gRegion res = m_visible_with_childs;
78         if (region.valid())
79                 res &= region;
80
81         if (res.empty())
82                 return;
83         
84         eWidget *root = this;
85         ePoint abspos = position();
86         while (root && !root->m_desktop)
87         {
88                 root = root->m_parent;
89                 assert(root);
90                 abspos += root->position();
91         }
92         
93         res.moveBy(abspos);
94 //      eDebug("region to invalidate:");
95 //      dumpRegion(res);
96         root->m_desktop->invalidate(res);
97 }
98
99 void eWidget::show()
100 {
101         if (m_vis & wVisShow)
102                 return;
103
104         m_vis |= wVisShow;
105         
106                 /* TODO: optimize here to only recalc what's required. possibly merge with hide. */
107         eWidget *root = this;
108         ePoint abspos = position();
109         while (root && !root->m_desktop)
110         {
111                 root = root->m_parent;
112                 if (!root)
113                 {
114                                 /* oops: our root widget does not have a desktop associated. 
115                                         probably somebody already erased the root, but tries some
116                                         operations on a child window. 
117                                         
118                                         ignore them for now. */
119                         /* assert(root); */
120                         return;
121                 }
122                 abspos += root->position();
123         }
124
125         root->m_desktop->recalcClipRegions(root);
126
127         gRegion abs = m_visible_with_childs;
128         abs.moveBy(abspos);
129         root->m_desktop->invalidate(abs);
130 }
131
132 void eWidget::hide()
133 {
134                 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
135                 /* wVisShow flag (because when the widget is shown again, the widgets must */
136                 /* become visible again. */
137         if (!(m_vis & wVisShow))
138                 return;
139         m_vis &= ~wVisShow;
140         
141                 /* this is a workaround to the above problem. when we are in the delete phase, 
142                    don't hide childs. */
143         if (!(m_parent || m_desktop))
144                 return;
145
146                 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
147         eWidget *root = this;
148         ePoint abspos = position();
149         while (root && !root->m_desktop)
150         {
151                 root = root->m_parent;
152                 if (!root)
153                         return;
154                 abspos += root->position();
155         }
156         assert(root->m_desktop);
157
158         gRegion abs = m_visible_with_childs;
159         abs.moveBy(abspos);
160
161         root->m_desktop->recalcClipRegions(root);
162         root->m_desktop->invalidate(abs);
163 }
164
165 void eWidget::destruct()
166 {
167         if (m_parent)
168                 m_parent->m_childs.remove(this);
169         delete this;
170 }
171
172 void eWidget::setBackgroundColor(const gRGB &col)
173 {
174         m_background_color = col;
175         m_have_background_color = 1;
176 }
177
178 void eWidget::clearBackgroundColor()
179 {
180         m_have_background_color = 0;
181 }
182
183 void eWidget::setZPosition(int z)
184 {
185         m_z_position = z;
186         if (!m_parent)
187                 return;
188         
189         m_parent->m_childs.remove(this);
190         
191         insertIntoParent(); /* now at the new Z position */
192 }
193
194 void eWidget::setTransparent(int transp)
195 {
196         if (transp)
197                 m_vis |= wVisTransparent;
198         else
199                 m_vis &=~wVisTransparent;
200 }
201
202 ePoint eWidget::getAbsolutePosition()
203 {
204         eWidget *root = this;
205         ePoint abspos = position();
206
207         while (root && !root->m_desktop)
208         {
209                 root = root->m_parent;
210                 assert(root);
211                 abspos += root->position();
212         }
213
214         return abspos;
215 }
216
217 void eWidget::mayKillFocus()
218 {
219         setFocus(0);
220                 /* when we have the focus, remove it first. */
221         if (m_focus_owner)
222                 m_focus_owner->setFocus(0);
223 }
224
225 eWidget::~eWidget()
226 {
227         hide();
228         
229         if (m_parent)
230                 m_parent->m_childs.remove(this);
231
232         m_parent = 0;
233
234                 /* tell all childs that the parent is not anymore existing */
235         ePtrList<eWidget>::iterator i(m_childs.begin());
236         while (i != m_childs.end())
237         {
238                 (*i)->parentRemoved();
239                 i = m_childs.erase(i);
240         }
241 }
242
243 void eWidget::insertIntoParent()
244 {
245         ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
246         
247         for(;;)
248         {
249                 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
250                 {
251                         m_parent->m_childs.insert(i, this);
252                         return;
253                 }
254                 ++i;
255         }
256 }
257
258 void eWidget::doPaint(gPainter &painter, const gRegion &r)
259 {
260         if (m_visible_with_childs.empty())
261                 return;
262         
263         gRegion region = r, childs = r;
264                         /* we were in parent's space, now we are in local space */
265         region.moveBy(-position());
266         
267         painter.moveOffset(position());
268         
269                 /* check if there's anything for us to paint */
270         region &= m_visible_region;
271         
272         if (!region.empty())
273         {
274                 painter.resetClip(region);
275                 event(evtPaint, &region, &painter);
276         }
277
278         childs.moveBy(-position());
279                 /* walk all childs */
280         for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
281                 i->doPaint(painter, childs);
282         
283         painter.moveOffset(-position());
284 }
285
286 void eWidget::recalcClipRegionsWhenVisible()
287 {
288         eWidget *t = this;
289         do
290         {
291                 if (!(t->m_vis & wVisShow))
292                         break;
293                 if (t->m_desktop)
294                 {
295                         t->m_desktop->recalcClipRegions(t);
296                         break;
297                 }
298                 t = t->m_parent;
299                 assert(t);
300         } while(1);
301 }
302
303 void eWidget::parentRemoved()
304 {
305         m_parent = 0;
306 }
307
308 int eWidget::event(int event, void *data, void *data2)
309 {
310         switch (event)
311         {
312         case evtPaint:
313         {
314                 gPainter &painter = *(gPainter*)data2;
315                 
316 //              eDebug("eWidget::evtPaint");
317 //              dumpRegion(*(gRegion*)data);
318                 if (!isTransparent())
319                 {
320                         if (!m_have_background_color)
321                         {
322                                 ePtr<eWindowStyle> style;
323                                 if (!getStyle(style))
324                                         style->paintBackground(painter, ePoint(0, 0), size());
325                         } else
326                         {
327                                 painter.setBackgroundColor(m_background_color);
328                                 painter.clear();
329                         }
330                 } else
331                 {
332                         if (m_have_background_color)
333                                 painter.setBackgroundColor(m_background_color);
334                 }
335                 break;
336         }
337         case evtKey:
338                 break;
339         case evtWillChangeSize:
340                 m_size = *static_cast<eSize*>(data);
341                 break;
342         case evtChangedSize:
343         {
344                 m_clip_region = gRegion(eRect(ePoint(0, 0),  m_size));
345                 break;
346         }
347         case evtFocusGot:
348                 m_focus_owner = (eWidget*)data;
349                 break;
350         case evtFocusLost:
351                 m_focus_owner = 0;
352                 break;
353         default:
354                 return -1;
355         }
356         return 0;
357 }
358
359 void eWidget::setFocus(eWidget *focus)
360 {
361         if (m_current_focus)
362                 m_current_focus->event(evtFocusLost, this);
363         
364         m_current_focus = focus;
365
366         if (m_current_focus)
367                 m_current_focus->event(evtFocusGot, this);
368 }
369