return None when there is no list or invalid cursor
[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                 assert(root);
113                 abspos += root->position();
114         }
115
116         root->m_desktop->recalcClipRegions(root);
117
118         gRegion abs = m_visible_with_childs;
119         abs.moveBy(abspos);
120         root->m_desktop->invalidate(abs);
121 }
122
123 void eWidget::hide()
124 {
125                 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
126                 /* wVisShow flag (because when the widget is shown again, the widgets must */
127                 /* become visible again. */
128         if (!(m_vis & wVisShow))
129                 return;
130         m_vis &= ~wVisShow;
131         
132                 /* this is a workaround to the above problem. when we are in the delete phase, 
133                    don't hide childs. */
134         if (!(m_parent || m_desktop))
135                 return;
136
137                 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
138         eWidget *root = this;
139         ePoint abspos = position();
140         while (root && !root->m_desktop)
141         {
142                 root = root->m_parent;
143                 if (!root)
144                         return;
145                 abspos += root->position();
146         }
147         assert(root->m_desktop);
148
149         gRegion abs = m_visible_with_childs;
150         abs.moveBy(abspos);
151
152         root->m_desktop->recalcClipRegions(root);
153         root->m_desktop->invalidate(abs);
154 }
155
156 void eWidget::destruct()
157 {
158         if (m_parent)
159                 m_parent->m_childs.remove(this);
160         delete this;
161 }
162
163 void eWidget::setBackgroundColor(const gRGB &col)
164 {
165         m_background_color = col;
166         m_have_background_color = 1;
167 }
168
169 void eWidget::clearBackgroundColor()
170 {
171         m_have_background_color = 0;
172 }
173
174 void eWidget::setZPosition(int z)
175 {
176         m_z_position = z;
177         if (!m_parent)
178                 return;
179         
180         m_parent->m_childs.remove(this);
181         
182         insertIntoParent(); /* now at the new Z position */
183 }
184
185 void eWidget::setTransparent(int transp)
186 {
187         if (transp)
188                 m_vis |= wVisTransparent;
189         else
190                 m_vis &=~wVisTransparent;
191 }
192
193 void eWidget::mayKillFocus()
194 {
195         setFocus(0);
196                 /* when we have the focus, remove it first. */
197         if (m_focus_owner)
198                 m_focus_owner->setFocus(0);
199 }
200
201 eWidget::~eWidget()
202 {
203         hide();
204         
205         if (m_parent)
206                 m_parent->m_childs.remove(this);
207
208         m_parent = 0;
209
210                 /* tell all childs that the parent is not anymore existing */
211         ePtrList<eWidget>::iterator i(m_childs.begin());
212         while (i != m_childs.end())
213         {
214                 (*i)->parentRemoved();
215                 i = m_childs.erase(i);
216         }
217 }
218
219 void eWidget::insertIntoParent()
220 {
221         ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
222         
223         for(;;)
224         {
225                 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
226                 {
227                         m_parent->m_childs.insert(i, this);
228                         return;
229                 }
230                 ++i;
231         }
232 }
233
234 void eWidget::doPaint(gPainter &painter, const gRegion &r)
235 {
236         if (m_visible_with_childs.empty())
237                 return;
238         
239         gRegion region = r, childs = r;
240                         /* we were in parent's space, now we are in local space */
241         region.moveBy(-position());
242         
243         painter.moveOffset(position());
244         
245                 /* check if there's anything for us to paint */
246         region &= m_visible_region;
247         
248         if (!region.empty())
249         {
250                 painter.resetClip(region);
251                 event(evtPaint, &region, &painter);
252         }
253
254         childs.moveBy(-position());
255                 /* walk all childs */
256         for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
257                 i->doPaint(painter, childs);
258         
259         painter.moveOffset(-position());
260 }
261
262 void eWidget::recalcClipRegionsWhenVisible()
263 {
264         eWidget *t = this;
265         do
266         {
267                 if (!(t->m_vis & wVisShow))
268                         break;
269                 if (t->m_desktop)
270                 {
271                         t->m_desktop->recalcClipRegions(t);
272                         break;
273                 }
274                 t = t->m_parent;
275                 assert(t);
276         } while(1);
277 }
278
279 void eWidget::parentRemoved()
280 {
281         m_parent = 0;
282 }
283
284 int eWidget::event(int event, void *data, void *data2)
285 {
286         switch (event)
287         {
288         case evtPaint:
289         {
290                 gPainter &painter = *(gPainter*)data2;
291                 
292 //              eDebug("eWidget::evtPaint");
293 //              dumpRegion(*(gRegion*)data);
294                 if (!isTransparent())
295                 {
296                         if (!m_have_background_color)
297                         {
298                                 ePtr<eWindowStyle> style;
299                                 if (!getStyle(style))
300                                         style->paintBackground(painter, ePoint(0, 0), size());
301                         } else
302                         {
303                                 painter.setBackgroundColor(m_background_color);
304                                 painter.clear();
305                         }
306                 } else
307                 {
308                         if (m_have_background_color)
309                                 painter.setBackgroundColor(m_background_color);
310                 }
311                 break;
312         }
313         case evtKey:
314                 break;
315         case evtWillChangeSize:
316                 m_size = *static_cast<eSize*>(data);
317                 break;
318         case evtChangedSize:
319         {
320                 m_clip_region = gRegion(eRect(ePoint(0, 0),  m_size));
321                 break;
322         }
323         case evtFocusGot:
324                 m_focus_owner = (eWidget*)data;
325                 break;
326         case evtFocusLost:
327                 m_focus_owner = 0;
328                 break;
329         default:
330                 return -1;
331         }
332         return 0;
333 }
334
335 void eWidget::setFocus(eWidget *focus)
336 {
337         if (m_current_focus)
338                 m_current_focus->event(evtFocusLost, this);
339         
340         m_current_focus = focus;
341
342         if (m_current_focus)
343                 m_current_focus->event(evtFocusGot, this);
344 }
345