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