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;
12 m_client_offset = eSize(0, 0);
18 m_parent->getStyle(m_style);
23 m_notify_child_on_position_change = 1;
26 void eWidget::move(ePoint pos)
28 pos = pos + m_client_offset;
29 if (m_position == pos)
32 /* ?? what about native move support? */
36 event(evtChangedPosition);
37 if (m_notify_child_on_position_change)
38 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
39 i->event(evtParentChangedPosition);
40 recalcClipRegionsWhenVisible();
41 /* try native move if supported. */
42 if ((m_vis & wVisShow) && ((!m_desktop) || m_desktop->movedWidget(this)))
46 void eWidget::resize(eSize size)
48 /* same strategy as with move: we first check if
49 the size changed at all, and if it did, we
50 invalidate both the old and new area.
51 TODO: check if either the old or new area
52 fits into the other completely, and invalidate
54 eSize old_size = m_size;
55 eSize old_offset = m_client_offset;
56 m_client_offset = eSize(0, 0);
57 event(evtWillChangeSize, &size, &m_client_offset);
58 if (old_size == m_size)
60 move(position() - old_offset);
62 event(evtChangedSize);
64 if (m_notify_child_on_position_change)
65 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
66 i->event(evtParentChangedPosition); /* position/size is the same here */
68 recalcClipRegionsWhenVisible(); invalidate();
71 void eWidget::invalidate(const gRegion ®ion)
73 /* we determine the area to redraw, and re-position this
74 area to the absolute position, and then call the
75 desktop's invalidate() with that, which adds this
76 area into the dirty region. */
77 gRegion res = m_visible_with_childs;
84 ePoint abspos = position();
85 while (root && !root->m_desktop)
87 root = root->m_parent;
89 abspos += root->position();
92 // eDebug("region to invalidate:");
94 root->m_desktop->invalidate(res);
103 // eDebug("show widget %p", this);
106 /* TODO: optimize here to only recalc what's required. possibly merge with hide. */
107 eWidget *root = this;
108 ePoint abspos = position();
109 while (root && !root->m_desktop)
111 root = root->m_parent;
114 /* oops: our root widget does not have a desktop associated.
115 probably somebody already erased the root, but tries some
116 operations on a child window.
117 ignore them for now. */
121 abspos += root->position();
124 root->m_desktop->recalcClipRegions(root);
126 gRegion abs = m_visible_with_childs;
128 root->m_desktop->invalidate(abs);
133 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
134 /* wVisShow flag (because when the widget is shown again, the widgets must */
135 /* become visible again. */
136 if (!(m_vis & wVisShow))
140 /* this is a workaround to the above problem. when we are in the delete phase,
141 don't hide childs. */
142 if (!(m_parent || m_desktop))
146 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
147 eWidget *root = this;
148 ePoint abspos = position();
149 while (root && !root->m_desktop)
151 root = root->m_parent;
154 abspos += root->position();
156 assert(root->m_desktop);
158 gRegion abs = m_visible_with_childs;
161 root->m_desktop->recalcClipRegions(root);
162 root->m_desktop->invalidate(abs);
165 void eWidget::destruct()
168 m_parent->m_childs.remove(this);
172 void eWidget::setBackgroundColor(const gRGB &col)
174 m_background_color = col;
175 m_have_background_color = 1;
178 void eWidget::clearBackgroundColor()
180 m_have_background_color = 0;
183 void eWidget::setZPosition(int z)
188 m_parent->m_childs.remove(this);
189 insertIntoParent(); /* now at the new Z position */
192 void eWidget::setTransparent(int transp)
194 if (isTransparent() != transp)
197 m_vis |= wVisTransparent;
199 m_vis &=~wVisTransparent;
200 recalcClipRegionsWhenVisible();
204 ePoint eWidget::getAbsolutePosition()
206 eWidget *root = this;
207 ePoint abspos = position();
209 while (root && !root->m_desktop)
211 root = root->m_parent;
213 abspos += root->position();
219 void eWidget::mayKillFocus()
222 /* when we have the focus, remove it first. */
224 m_focus_owner->setFocus(0);
231 m_parent->m_childs.remove(this);
235 /* tell all childs that the parent is not anymore existing */
236 ePtrList<eWidget>::iterator i(m_childs.begin());
237 while (i != m_childs.end())
239 (*i)->parentRemoved();
240 i = m_childs.erase(i);
244 void eWidget::insertIntoParent()
246 ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
249 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
251 m_parent->m_childs.insert(i, this);
258 void eWidget::doPaint(gPainter &painter, const gRegion &r)
260 if (m_visible_with_childs.empty())
262 gRegion region = r, childs = r;
263 /* we were in parent's space, now we are in local space */
264 region.moveBy(-position());
265 painter.moveOffset(position());
266 /* check if there's anything for us to paint */
267 region &= m_visible_region;
270 painter.resetClip(region);
271 event(evtPaint, ®ion, &painter);
274 childs.moveBy(-position());
275 /* walk all childs */
276 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
277 i->doPaint(painter, childs);
278 painter.moveOffset(-position());
281 void eWidget::recalcClipRegionsWhenVisible()
286 if (!(t->m_vis & wVisShow))
290 t->m_desktop->recalcClipRegions(t);
298 void eWidget::parentRemoved()
303 int eWidget::event(int event, void *data, void *data2)
309 gPainter &painter = *(gPainter*)data2;
310 // eDebug("eWidget::evtPaint");
311 // dumpRegion(*(gRegion*)data);
312 if (!isTransparent())
314 if (!m_have_background_color)
316 ePtr<eWindowStyle> style;
317 if (!getStyle(style))
318 style->paintBackground(painter, ePoint(0, 0), size());
321 painter.setBackgroundColor(m_background_color);
327 while (w && !w->m_have_background_color)
331 painter.setBackgroundColor(w->m_background_color);
337 case evtWillChangeSize:
338 m_size = *static_cast<eSize*>(data);
341 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
343 case evtParentChangedPosition:
344 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
345 i->event(evtParentChangedPosition);
348 m_focus_owner = (eWidget*)data;
359 void eWidget::setFocus(eWidget *focus)
362 m_current_focus->event(evtFocusLost, this);
363 m_current_focus = focus;
366 m_current_focus->event(evtFocusGot, this);
369 void eWidget::notifyShowHide()
371 event(evtParentVisibilityChanged);
372 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)