474e295d0d86830c99413e2d41c784c19032b5fa
[vuplus_dvbapp] / 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         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         m_position = pos + m_client_offset;
30         
31         if (m_position == pos)
32                 return;
33         
34                 /* we invalidate before and after the move to
35                    cause a correct redraw. The area which is
36                    included both before and after isn't redrawn
37                    twice because a invalidate doesn't immediately
38                    redraws the region. */
39         invalidate();
40         event(evtChangedPosition);
41         recalcClipRegionsWhenVisible(); 
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();
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();
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::mayKillFocus()
170 {
171         setFocus(0);
172                 /* when we have the focus, remove it first. */
173         if (m_focus_owner)
174                 m_focus_owner->setFocus(0);
175 }
176
177 eWidget::~eWidget()
178 {
179         hide();
180         
181         if (m_parent)
182                 m_parent->m_childs.remove(this);
183
184         m_parent = 0;
185
186                 /* destroy all childs */
187         ePtrList<eWidget>::iterator i(m_childs.begin());
188         while (i != m_childs.end())
189         {
190                 (*i)->m_parent = 0;
191                 delete *i;
192                 i = m_childs.erase(i);
193         }
194 }
195
196 void eWidget::doPaint(gPainter &painter, const gRegion &r)
197 {
198         if (m_visible_with_childs.empty())
199                 return;
200         
201         gRegion region = r;
202                         /* we were in parent's space, now we are in local space */
203         region.moveBy(-position());
204         
205         painter.moveOffset(position());
206                 /* walk all childs */
207         for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
208                 i->doPaint(painter, region);
209         
210                 /* check if there's anything for us to paint */
211         region &= m_visible_region;
212         
213         if (!region.empty())
214         {
215                 painter.resetClip(region);
216                 event(evtPaint, &region, &painter);
217         }
218         
219         painter.moveOffset(-position());
220 }
221
222 void eWidget::recalcClipRegionsWhenVisible()
223 {
224         eWidget *t = this;
225         do
226         {
227                 if (!(t->m_vis & wVisShow))
228                         break;
229                 if (t->m_desktop)
230                 {
231                         t->m_desktop->recalcClipRegions();
232                         break;
233                 }
234                 t = t->m_parent;
235                 assert(t);
236         } while(1);
237 }
238
239 int eWidget::event(int event, void *data, void *data2)
240 {
241         switch (event)
242         {
243         case evtPaint:
244         {
245                 gPainter &painter = *(gPainter*)data2;
246                 
247 //              eDebug("eWidget::evtPaint");
248 //              dumpRegion(*(gRegion*)data);
249                 if (!m_have_background_color)
250                 {
251                         ePtr<eWindowStyle> style;
252                         if (!getStyle(style))
253                                 style->paintBackground(painter, ePoint(0, 0), size());
254                 } else
255                 {
256                         painter.setBackgroundColor(m_background_color);
257                         painter.clear();
258                 }
259                 break;
260         }
261         case evtKey:
262                 break;
263         case evtWillChangeSize:
264                 m_size = *static_cast<eSize*>(data);
265                 break;
266         case evtChangedSize:
267         {
268                 m_clip_region = gRegion(eRect(ePoint(0, 0),  m_size));
269                 break;
270         }
271         case evtFocusGot:
272                 m_focus_owner = (eWidget*)data;
273                 break;
274         case evtFocusLost:
275                 m_focus_owner = 0;
276                 break;
277         default:
278                 return -1;
279         }
280         return 0;
281 }
282
283 void eWidget::setFocus(eWidget *focus)
284 {
285         if (m_current_focus)
286                 m_current_focus->event(evtFocusLost, this);
287         
288         m_current_focus = focus;
289
290         if (m_current_focus)
291                 m_current_focus->event(evtFocusGot, this);
292 }
293