widget: add - untested, as usual - z ordering
[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         }
219 }
220
221 void eWidget::doPaint(gPainter &painter, const gRegion &r)
222 {
223         if (m_visible_with_childs.empty())
224                 return;
225         
226         gRegion region = r;
227                         /* we were in parent's space, now we are in local space */
228         region.moveBy(-position());
229         
230         painter.moveOffset(position());
231                 /* walk all childs */
232         for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
233                 i->doPaint(painter, region);
234         
235                 /* check if there's anything for us to paint */
236         region &= m_visible_region;
237         
238         if (!region.empty())
239         {
240                 painter.resetClip(region);
241                 event(evtPaint, &region, &painter);
242         }
243         
244         painter.moveOffset(-position());
245 }
246
247 void eWidget::recalcClipRegionsWhenVisible()
248 {
249         eWidget *t = this;
250         do
251         {
252                 if (!(t->m_vis & wVisShow))
253                         break;
254                 if (t->m_desktop)
255                 {
256                         t->m_desktop->recalcClipRegions(t);
257                         break;
258                 }
259                 t = t->m_parent;
260                 assert(t);
261         } while(1);
262 }
263
264 int eWidget::event(int event, void *data, void *data2)
265 {
266         switch (event)
267         {
268         case evtPaint:
269         {
270                 gPainter &painter = *(gPainter*)data2;
271                 
272 //              eDebug("eWidget::evtPaint");
273 //              dumpRegion(*(gRegion*)data);
274                 if (!m_have_background_color)
275                 {
276                         ePtr<eWindowStyle> style;
277                         if (!getStyle(style))
278                                 style->paintBackground(painter, ePoint(0, 0), size());
279                 } else
280                 {
281                         painter.setBackgroundColor(m_background_color);
282                         painter.clear();
283                 }
284                 break;
285         }
286         case evtKey:
287                 break;
288         case evtWillChangeSize:
289                 m_size = *static_cast<eSize*>(data);
290                 break;
291         case evtChangedSize:
292         {
293                 m_clip_region = gRegion(eRect(ePoint(0, 0),  m_size));
294                 break;
295         }
296         case evtFocusGot:
297                 m_focus_owner = (eWidget*)data;
298                 break;
299         case evtFocusLost:
300                 m_focus_owner = 0;
301                 break;
302         default:
303                 return -1;
304         }
305         return 0;
306 }
307
308 void eWidget::setFocus(eWidget *focus)
309 {
310         if (m_current_focus)
311                 m_current_focus->event(evtFocusLost, this);
312         
313         m_current_focus = focus;
314
315         if (m_current_focus)
316                 m_current_focus->event(evtFocusGot, this);
317 }
318