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