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