cbc1a8c05f8e9b9687df89820c671d52714f3eda
[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 void eWidget::mayKillFocus()
203 {
204         setFocus(0);
205                 /* when we have the focus, remove it first. */
206         if (m_focus_owner)
207                 m_focus_owner->setFocus(0);
208 }
209
210 eWidget::~eWidget()
211 {
212         hide();
213         
214         if (m_parent)
215                 m_parent->m_childs.remove(this);
216
217         m_parent = 0;
218
219                 /* tell all childs that the parent is not anymore existing */
220         ePtrList<eWidget>::iterator i(m_childs.begin());
221         while (i != m_childs.end())
222         {
223                 (*i)->parentRemoved();
224                 i = m_childs.erase(i);
225         }
226 }
227
228 void eWidget::insertIntoParent()
229 {
230         ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
231         
232         for(;;)
233         {
234                 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
235                 {
236                         m_parent->m_childs.insert(i, this);
237                         return;
238                 }
239                 ++i;
240         }
241 }
242
243 void eWidget::doPaint(gPainter &painter, const gRegion &r)
244 {
245         if (m_visible_with_childs.empty())
246                 return;
247         
248         gRegion region = r, childs = r;
249                         /* we were in parent's space, now we are in local space */
250         region.moveBy(-position());
251         
252         painter.moveOffset(position());
253         
254                 /* check if there's anything for us to paint */
255         region &= m_visible_region;
256         
257         if (!region.empty())
258         {
259                 painter.resetClip(region);
260                 event(evtPaint, &region, &painter);
261         }
262
263         childs.moveBy(-position());
264                 /* walk all childs */
265         for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
266                 i->doPaint(painter, childs);
267         
268         painter.moveOffset(-position());
269 }
270
271 void eWidget::recalcClipRegionsWhenVisible()
272 {
273         eWidget *t = this;
274         do
275         {
276                 if (!(t->m_vis & wVisShow))
277                         break;
278                 if (t->m_desktop)
279                 {
280                         t->m_desktop->recalcClipRegions(t);
281                         break;
282                 }
283                 t = t->m_parent;
284                 assert(t);
285         } while(1);
286 }
287
288 void eWidget::parentRemoved()
289 {
290         m_parent = 0;
291 }
292
293 int eWidget::event(int event, void *data, void *data2)
294 {
295         switch (event)
296         {
297         case evtPaint:
298         {
299                 gPainter &painter = *(gPainter*)data2;
300                 
301 //              eDebug("eWidget::evtPaint");
302 //              dumpRegion(*(gRegion*)data);
303                 if (!isTransparent())
304                 {
305                         if (!m_have_background_color)
306                         {
307                                 ePtr<eWindowStyle> style;
308                                 if (!getStyle(style))
309                                         style->paintBackground(painter, ePoint(0, 0), size());
310                         } else
311                         {
312                                 painter.setBackgroundColor(m_background_color);
313                                 painter.clear();
314                         }
315                 } else
316                 {
317                         if (m_have_background_color)
318                                 painter.setBackgroundColor(m_background_color);
319                 }
320                 break;
321         }
322         case evtKey:
323                 break;
324         case evtWillChangeSize:
325                 m_size = *static_cast<eSize*>(data);
326                 break;
327         case evtChangedSize:
328         {
329                 m_clip_region = gRegion(eRect(ePoint(0, 0),  m_size));
330                 break;
331         }
332         case evtFocusGot:
333                 m_focus_owner = (eWidget*)data;
334                 break;
335         case evtFocusLost:
336                 m_focus_owner = 0;
337                 break;
338         default:
339                 return -1;
340         }
341         return 0;
342 }
343
344 void eWidget::setFocus(eWidget *focus)
345 {
346         if (m_current_focus)
347                 m_current_focus->event(evtFocusLost, this);
348         
349         m_current_focus = focus;
350
351         if (m_current_focus)
352                 m_current_focus->event(evtFocusGot, this);
353 }
354