1 #include <lib/gui/ewidget.h>
2 #include <lib/gui/ewidgetdesktop.h>
4 extern void dumpRegion(const gRegion ®ion);
6 eWidget::eWidget(eWidget *parent): m_animation(this), m_parent(parent ? parent->child() : 0)
10 m_have_background_color = 0;
13 m_client_offset = eSize(0, 0);
21 m_parent->getStyle(m_style);
27 m_notify_child_on_position_change = 1;
30 void eWidget::move(ePoint pos)
32 pos = pos + m_client_offset;
34 if (m_position == pos)
37 /* ?? what about native move support? */
42 event(evtChangedPosition);
44 if (m_notify_child_on_position_change)
45 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
46 i->event(evtParentChangedPosition);
48 recalcClipRegionsWhenVisible();
50 /* try native move if supported. */
51 if ((m_vis & wVisShow) && ((!m_desktop) || m_desktop->movedWidget(this)))
55 void eWidget::resize(eSize size)
57 /* same strategy as with move: we first check if
58 the size changed at all, and if it did, we
59 invalidate both the old and new area.
60 TODO: check if either the old or new area
61 fits into the other completely, and invalidate
63 eSize old_size = m_size;
64 eSize old_offset = m_client_offset;
65 m_client_offset = eSize(0, 0);
66 event(evtWillChangeSize, &size, &m_client_offset);
67 if (old_size == m_size)
70 move(position() - old_offset);
73 event(evtChangedSize);
75 if (m_notify_child_on_position_change)
76 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
77 i->event(evtParentChangedPosition); /* position/size is the same here */
79 recalcClipRegionsWhenVisible();
83 void eWidget::invalidate(const gRegion ®ion)
85 /* we determine the area to redraw, and re-position this
86 area to the absolute position, and then call the
87 desktop's invalidate() with that, which adds this
88 area into the dirty region. */
89 gRegion res = m_visible_with_childs;
97 ePoint abspos = position();
98 while (root && !root->m_desktop)
100 root = root->m_parent;
102 abspos += root->position();
106 // eDebug("region to invalidate:");
108 root->m_desktop->invalidate(res);
113 if (m_vis & wVisShow)
118 /* TODO: optimize here to only recalc what's required. possibly merge with hide. */
119 eWidget *root = this;
120 ePoint abspos = position();
121 while (root && !root->m_desktop)
123 root = root->m_parent;
126 /* oops: our root widget does not have a desktop associated.
127 probably somebody already erased the root, but tries some
128 operations on a child window.
130 ignore them for now. */
134 abspos += root->position();
137 root->m_desktop->recalcClipRegions(root);
139 gRegion abs = m_visible_with_childs;
141 root->m_desktop->invalidate(abs);
146 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
147 /* wVisShow flag (because when the widget is shown again, the widgets must */
148 /* become visible again. */
149 if (!(m_vis & wVisShow))
153 /* this is a workaround to the above problem. when we are in the delete phase,
154 don't hide childs. */
155 if (!(m_parent || m_desktop))
158 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
159 eWidget *root = this;
160 ePoint abspos = position();
161 while (root && !root->m_desktop)
163 root = root->m_parent;
166 abspos += root->position();
168 assert(root->m_desktop);
170 gRegion abs = m_visible_with_childs;
173 root->m_desktop->recalcClipRegions(root);
174 root->m_desktop->invalidate(abs);
177 void eWidget::destruct()
180 m_parent->m_childs.remove(this);
184 void eWidget::setBackgroundColor(const gRGB &col)
186 m_background_color = col;
187 m_have_background_color = 1;
190 void eWidget::clearBackgroundColor()
192 m_have_background_color = 0;
195 void eWidget::setZPosition(int z)
201 m_parent->m_childs.remove(this);
203 insertIntoParent(); /* now at the new Z position */
206 void eWidget::setTransparent(int transp)
208 if (isTransparent() != transp)
211 m_vis |= wVisTransparent;
213 m_vis &=~wVisTransparent;
214 recalcClipRegionsWhenVisible();
218 ePoint eWidget::getAbsolutePosition()
220 eWidget *root = this;
221 ePoint abspos = position();
223 while (root && !root->m_desktop)
225 root = root->m_parent;
227 abspos += root->position();
233 void eWidget::mayKillFocus()
236 /* when we have the focus, remove it first. */
238 m_focus_owner->setFocus(0);
246 m_parent->m_childs.remove(this);
250 /* tell all childs that the parent is not anymore existing */
251 ePtrList<eWidget>::iterator i(m_childs.begin());
252 while (i != m_childs.end())
254 (*i)->parentRemoved();
255 i = m_childs.erase(i);
259 void eWidget::insertIntoParent()
261 ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
265 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
267 m_parent->m_childs.insert(i, this);
274 void eWidget::doPaint(gPainter &painter, const gRegion &r)
276 if (m_visible_with_childs.empty())
279 gRegion region = r, childs = r;
280 /* we were in parent's space, now we are in local space */
281 region.moveBy(-position());
283 painter.moveOffset(position());
285 /* check if there's anything for us to paint */
286 region &= m_visible_region;
290 painter.resetClip(region);
291 event(evtPaint, ®ion, &painter);
294 childs.moveBy(-position());
295 /* walk all childs */
296 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
297 i->doPaint(painter, childs);
299 painter.moveOffset(-position());
302 void eWidget::recalcClipRegionsWhenVisible()
307 if (!(t->m_vis & wVisShow))
311 t->m_desktop->recalcClipRegions(t);
319 void eWidget::parentRemoved()
324 int eWidget::event(int event, void *data, void *data2)
330 gPainter &painter = *(gPainter*)data2;
332 // eDebug("eWidget::evtPaint");
333 // dumpRegion(*(gRegion*)data);
334 if (!isTransparent())
336 if (!m_have_background_color)
338 ePtr<eWindowStyle> style;
339 if (!getStyle(style))
340 style->paintBackground(painter, ePoint(0, 0), size());
343 painter.setBackgroundColor(m_background_color);
350 while (w && !w->m_have_background_color)
354 painter.setBackgroundColor(w->m_background_color);
360 case evtWillChangeSize:
361 m_size = *static_cast<eSize*>(data);
364 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
366 case evtParentChangedPosition:
367 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
368 i->event(evtParentChangedPosition);
371 m_focus_owner = (eWidget*)data;
382 void eWidget::setFocus(eWidget *focus)
385 m_current_focus->event(evtFocusLost, this);
387 m_current_focus = focus;
390 m_current_focus->event(evtFocusGot, this);