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