listbox: after a long discussion, paint the f&/%ing pixels...
[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         m_position = pos;
36         
37         event(evtChangedPosition);
38         recalcClipRegionsWhenVisible();
39         
40                 /* try native move if supported. */
41         if ((m_vis & wVisShow) && ((!m_desktop) || m_desktop->movedWidget(this)))
42                 invalidate();
43 }
44
45 void eWidget::resize(eSize size)
46 {
47                 /* same strategy as with move: we first check if
48                    the size changed at all, and if it did, we
49                    invalidate both the old and new area. 
50                    TODO: check if either the old or new area
51                    fits into the other completely, and invalidate
52                    only once. */
53         eSize old_size = m_size;
54         eSize old_offset = m_client_offset;
55         m_client_offset = eSize(0, 0);
56         event(evtWillChangeSize, &size, &m_client_offset);
57         if (old_size == m_size)
58                 return;
59         
60         move(position() - old_offset);
61         
62         invalidate();
63         event(evtChangedSize);
64         recalcClipRegionsWhenVisible(); 
65         invalidate();
66 }
67
68 void eWidget::invalidate(const gRegion &region)
69 {
70                 /* we determine the area to redraw, and re-position this
71                    area to the absolute position, and then call the
72                    desktop's invalidate() with that, which adds this
73                    area into the dirty region. */
74         gRegion res = m_visible_with_childs;
75         if (region.valid())
76                 res &= region;
77
78         if (res.empty())
79                 return;
80         
81         eWidget *root = this;
82         ePoint abspos = position();
83         while (root && !root->m_desktop)
84         {
85                 root = root->m_parent;
86                 assert(root);
87                 abspos += root->position();
88         }
89         
90         res.moveBy(abspos);
91 //      eDebug("region to invalidate:");
92 //      dumpRegion(res);
93         root->m_desktop->invalidate(res);
94 }
95
96 void eWidget::show()
97 {
98         if (m_vis & wVisShow)
99                 return;
100         
101         m_vis |=  wVisShow;
102
103                 /* TODO: optimize here to only recalc what's required. possibly merge with hide. */
104         eWidget *root = this;
105         ePoint abspos = position();
106         while (root && !root->m_desktop)
107         {
108                 root = root->m_parent;
109                 assert(root);
110                 abspos += root->position();
111         }
112
113         root->m_desktop->recalcClipRegions(root);
114
115         gRegion abs = m_visible_with_childs;
116         abs.moveBy(abspos);
117         root->m_desktop->invalidate(abs);
118 }
119
120 void eWidget::hide()
121 {
122                 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
123                 /* wVisShow flag (because when the widget is shown again, the widgets must */
124                 /* become visible again. */
125         if (!(m_vis & wVisShow))
126                 return;
127         m_vis &= ~wVisShow;
128         
129                 /* this is a workaround to the above problem. when we are in the delete phase, 
130                    don't hide childs. */
131         if (!(m_parent || m_desktop))
132                 return;
133
134                 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
135         eWidget *root = this;
136         ePoint abspos = position();
137         while (root && !root->m_desktop)
138         {
139                 root = root->m_parent;
140                 abspos += root->position();
141         }
142         assert(root->m_desktop);
143
144         gRegion abs = m_visible_with_childs;
145         abs.moveBy(abspos);
146
147         root->m_desktop->recalcClipRegions(root);
148         root->m_desktop->invalidate(abs);
149 }
150
151 void eWidget::destruct()
152 {
153         if (m_parent)
154                 m_parent->m_childs.remove(this);
155         delete this;
156 }
157
158 void eWidget::setBackgroundColor(const gRGB &col)
159 {
160         m_background_color = col;
161         m_have_background_color = 1;
162 }
163
164 void eWidget::clearBackgroundColor()
165 {
166         m_have_background_color = 0;
167 }
168
169 void eWidget::setZPosition(int z)
170 {
171         m_z_position = z;
172         if (!m_parent)
173                 return;
174         
175         m_parent->m_childs.remove(this);
176         
177         insertIntoParent(); /* now at the new Z position */
178 }
179
180 void eWidget::mayKillFocus()
181 {
182         setFocus(0);
183                 /* when we have the focus, remove it first. */
184         if (m_focus_owner)
185                 m_focus_owner->setFocus(0);
186 }
187
188 eWidget::~eWidget()
189 {
190         hide();
191         
192         if (m_parent)
193                 m_parent->m_childs.remove(this);
194
195         m_parent = 0;
196
197                 /* destroy all childs */
198         ePtrList<eWidget>::iterator i(m_childs.begin());
199         while (i != m_childs.end())
200         {
201                 (*i)->m_parent = 0;
202                 delete *i;
203                 i = m_childs.erase(i);
204         }
205 }
206
207 void eWidget::insertIntoParent()
208 {
209         ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
210         
211         for(;;)
212         {
213                 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
214                 {
215                         m_parent->m_childs.insert(i, this);
216                         return;
217                 }
218                 ++i;
219         }
220 }
221
222 void eWidget::doPaint(gPainter &painter, const gRegion &r)
223 {
224         if (m_visible_with_childs.empty())
225                 return;
226         
227         gRegion region = r;
228                         /* we were in parent's space, now we are in local space */
229         region.moveBy(-position());
230         
231         painter.moveOffset(position());
232                 /* walk all childs */
233         for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
234                 i->doPaint(painter, region);
235         
236                 /* check if there's anything for us to paint */
237         region &= m_visible_region;
238         
239         if (!region.empty())
240         {
241                 painter.resetClip(region);
242                 event(evtPaint, &region, &painter);
243         }
244         
245         painter.moveOffset(-position());
246 }
247
248 void eWidget::recalcClipRegionsWhenVisible()
249 {
250         eWidget *t = this;
251         do
252         {
253                 if (!(t->m_vis & wVisShow))
254                         break;
255                 if (t->m_desktop)
256                 {
257                         t->m_desktop->recalcClipRegions(t);
258                         break;
259                 }
260                 t = t->m_parent;
261                 assert(t);
262         } while(1);
263 }
264
265 int eWidget::event(int event, void *data, void *data2)
266 {
267         switch (event)
268         {
269         case evtPaint:
270         {
271                 gPainter &painter = *(gPainter*)data2;
272                 
273 //              eDebug("eWidget::evtPaint");
274 //              dumpRegion(*(gRegion*)data);
275                 if (!m_have_background_color)
276                 {
277                         ePtr<eWindowStyle> style;
278                         if (!getStyle(style))
279                                 style->paintBackground(painter, ePoint(0, 0), size());
280                 } else
281                 {
282                         painter.setBackgroundColor(m_background_color);
283                         painter.clear();
284                 }
285                 break;
286         }
287         case evtKey:
288                 break;
289         case evtWillChangeSize:
290                 m_size = *static_cast<eSize*>(data);
291                 break;
292         case evtChangedSize:
293         {
294                 m_clip_region = gRegion(eRect(ePoint(0, 0),  m_size));
295                 break;
296         }
297         case evtFocusGot:
298                 m_focus_owner = (eWidget*)data;
299                 break;
300         case evtFocusLost:
301                 m_focus_owner = 0;
302                 break;
303         default:
304                 return -1;
305         }
306         return 0;
307 }
308
309 void eWidget::setFocus(eWidget *focus)
310 {
311         if (m_current_focus)
312                 m_current_focus->event(evtFocusLost, this);
313         
314         m_current_focus = focus;
315
316         if (m_current_focus)
317                 m_current_focus->event(evtFocusGot, this);
318 }
319