add missing files, add ability to specify table_id mask and table_id ext mask
[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         m_background_color = col;
157         m_have_background_color = 1;
158 }
159
160 void eWidget::clearBackgroundColor()
161 {
162         m_have_background_color = 0;
163 }
164
165 void eWidget::mayKillFocus()
166 {
167         setFocus(0);
168                 /* when we have the focus, remove it first. */
169         if (m_focus_owner)
170                 m_focus_owner->setFocus(0);
171 }
172
173 eWidget::~eWidget()
174 {
175         hide();
176         
177         if (m_parent)
178                 m_parent->m_childs.remove(this);
179
180         m_parent = 0;
181
182                 /* destroy all childs */
183         ePtrList<eWidget>::iterator i(m_childs.begin());
184         while (i != m_childs.end())
185         {
186                 (*i)->m_parent = 0;
187                 delete *i;
188                 i = m_childs.erase(i);
189         }
190 }
191
192 void eWidget::doPaint(gPainter &painter, const gRegion &r)
193 {
194         if (m_visible_with_childs.empty())
195                 return;
196         
197         gRegion region = r;
198                         /* we were in parent's space, now we are in local space */
199         region.moveBy(-position());
200         
201         painter.moveOffset(position());
202                 /* walk all childs */
203         for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
204                 i->doPaint(painter, region);
205         
206                 /* check if there's anything for us to paint */
207         region &= m_visible_region;
208         
209         if (!region.empty())
210         {
211                 painter.resetClip(region);
212                 event(evtPaint, &region, &painter);
213         }
214         
215         painter.moveOffset(-position());
216 }
217
218 void eWidget::recalcClipRegionsWhenVisible()
219 {
220         eWidget *t = this;
221         do
222         {
223                 if (!(t->m_vis & wVisShow))
224                         break;
225                 if (t->m_desktop)
226                 {
227                         t->m_desktop->recalcClipRegions();
228                         break;
229                 }
230                 t = t->m_parent;
231                 assert(t);
232         } while(1);
233 }
234
235 int eWidget::event(int event, void *data, void *data2)
236 {
237         switch (event)
238         {
239         case evtPaint:
240         {
241                 gPainter &painter = *(gPainter*)data2;
242                 
243 //              eDebug("eWidget::evtPaint");
244 //              dumpRegion(*(gRegion*)data);
245                 if (!m_have_background_color)
246                 {
247                         ePtr<eWindowStyle> style;
248                         if (!getStyle(style))
249                                 style->paintBackground(painter, ePoint(0, 0), size());
250                 } else
251                 {
252                         painter.setBackgroundColor(m_background_color);
253                         painter.clear();
254                 }
255                 break;
256         }
257         case evtKey:
258                 break;
259         case evtWillChangeSize:
260                 m_size = *static_cast<eSize*>(data);
261                 break;
262         case evtChangedSize:
263         {
264                 m_clip_region = gRegion(eRect(ePoint(0, 0),  m_size));
265                 break;
266         }
267         case evtFocusGot:
268                 m_focus_owner = (eWidget*)data;
269                 break;
270         case evtFocusLost:
271                 m_focus_owner = 0;
272                 break;
273         default:
274                 return -1;
275         }
276         return 0;
277 }
278
279 void eWidget::setFocus(eWidget *focus)
280 {
281         eDebug("setFocus in %p to %p, was %p", this, focus, m_current_focus);
282         if (m_current_focus)
283                 m_current_focus->event(evtFocusLost, this);
284         
285         m_current_focus = focus;
286
287         if (m_current_focus)
288                 m_current_focus->event(evtFocusGot, this);
289 }
290