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)
11 m_have_background_color = 0;
13 m_client_offset = eSize(0, 0);
19 m_parent->getStyle(m_style);
24 m_notify_child_on_position_change = 1;
27 void eWidget::move(ePoint pos)
29 pos = pos + m_client_offset;
30 if (m_position == pos)
33 /* ?? what about native move support? */
37 event(evtChangedPosition);
38 if (m_notify_child_on_position_change)
39 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
40 i->event(evtParentChangedPosition);
41 recalcClipRegionsWhenVisible();
42 /* try native move if supported. */
43 if ((m_vis & wVisShow) && ((!m_desktop) || m_desktop->movedWidget(this)))
47 void eWidget::resize(eSize size)
49 /* same strategy as with move: we first check if
50 the size changed at all, and if it did, we
51 invalidate both the old and new area.
52 TODO: check if either the old or new area
53 fits into the other completely, and invalidate
55 eSize old_size = m_size;
56 eSize old_offset = m_client_offset;
57 m_client_offset = eSize(0, 0);
58 event(evtWillChangeSize, &size, &m_client_offset);
59 if (old_size == m_size)
61 move(position() - old_offset);
63 event(evtChangedSize);
65 if (m_notify_child_on_position_change)
66 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
67 i->event(evtParentChangedPosition); /* position/size is the same here */
69 recalcClipRegionsWhenVisible(); invalidate();
72 void eWidget::invalidate(const gRegion ®ion)
74 /* we determine the area to redraw, and re-position this
75 area to the absolute position, and then call the
76 desktop's invalidate() with that, which adds this
77 area into the dirty region. */
78 gRegion res = m_visible_with_childs;
85 ePoint abspos = position();
86 int target_layer = m_layer;
88 while (root && !root->m_desktop)
90 root = root->m_parent;
92 if (root->m_layer != -1)
93 target_layer = root->m_layer;
94 abspos += root->position();
97 // eDebug("region to invalidate:");
99 root->m_desktop->invalidate(res, this, target_layer);
104 if (m_vis & wVisShow)
108 // eDebug("show widget %p", this);
111 /* TODO: optimize here to only recalc what's required. possibly merge with hide. */
112 eWidget *root = this;
113 ePoint abspos = position();
114 int target_layer = m_layer;
116 while (root && !root->m_desktop)
118 root = root->m_parent;
121 /* oops: our root widget does not have a desktop associated.
122 probably somebody already erased the root, but tries some
123 operations on a child window.
124 ignore them for now. */
128 if (root->m_layer != -1)
129 target_layer = root->m_layer;
130 abspos += root->position();
133 root->m_desktop->recalcClipRegions(root);
135 gRegion abs = m_visible_with_childs;
137 root->m_desktop->invalidate(abs, this, target_layer);
142 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
143 /* wVisShow flag (because when the widget is shown again, the widgets must */
144 /* become visible again. */
145 if (!(m_vis & wVisShow))
149 /* this is a workaround to the above problem. when we are in the delete phase,
150 don't hide childs. */
151 if (!(m_parent || m_desktop))
155 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
156 eWidget *root = this;
157 ePoint abspos = position();
158 while (root && !root->m_desktop)
160 root = root->m_parent;
163 abspos += root->position();
165 assert(root->m_desktop);
167 gRegion abs = m_visible_with_childs;
170 root->m_desktop->recalcClipRegions(root);
171 root->m_desktop->invalidate(abs);
174 void eWidget::destruct()
177 m_parent->m_childs.remove(this);
181 void eWidget::setBackgroundColor(const gRGB &col)
183 m_background_color = col;
184 m_have_background_color = 1;
187 void eWidget::clearBackgroundColor()
189 m_have_background_color = 0;
192 void eWidget::setZPosition(int z)
197 m_parent->m_childs.remove(this);
198 insertIntoParent(); /* now at the new Z position */
201 void eWidget::setTransparent(int transp)
203 if (isTransparent() != transp)
206 m_vis |= wVisTransparent;
208 m_vis &=~wVisTransparent;
209 recalcClipRegionsWhenVisible();
213 ePoint eWidget::getAbsolutePosition()
215 eWidget *root = this;
216 ePoint abspos = position();
218 while (root && !root->m_desktop)
220 root = root->m_parent;
222 abspos += root->position();
228 void eWidget::mayKillFocus()
231 /* when we have the focus, remove it first. */
233 m_focus_owner->setFocus(0);
240 m_parent->m_childs.remove(this);
244 /* tell all childs that the parent is not anymore existing */
245 ePtrList<eWidget>::iterator i(m_childs.begin());
246 while (i != m_childs.end())
248 (*i)->parentRemoved();
249 i = m_childs.erase(i);
253 void eWidget::insertIntoParent()
255 ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
258 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
260 m_parent->m_childs.insert(i, this);
267 void eWidget::doPaint(gPainter &painter, const gRegion &r, int layer)
269 if (m_visible_with_childs.empty())
271 gRegion region = r, childs = r;
272 /* we were in parent's space, now we are in local space */
273 region.moveBy(-position());
274 painter.moveOffset(position());
275 /* check if there's anything for us to paint */
276 if (layer == m_layer)
278 region &= m_visible_region;
281 painter.resetClip(region);
282 event(evtPaint, ®ion, &painter);
286 childs.moveBy(-position());
287 /* walk all childs */
288 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
289 i->doPaint(painter, childs, layer);
290 painter.moveOffset(-position());
293 void eWidget::recalcClipRegionsWhenVisible()
298 if (!(t->m_vis & wVisShow))
302 t->m_desktop->recalcClipRegions(t);
310 void eWidget::parentRemoved()
315 int eWidget::event(int event, void *data, void *data2)
321 gPainter &painter = *(gPainter*)data2;
322 // eDebug("eWidget::evtPaint");
323 // dumpRegion(*(gRegion*)data);
324 if (!isTransparent())
326 if (!m_have_background_color)
328 ePtr<eWindowStyle> style;
329 if (!getStyle(style))
330 style->paintBackground(painter, ePoint(0, 0), size());
333 painter.setBackgroundColor(m_background_color);
339 while (w && !w->m_have_background_color)
343 painter.setBackgroundColor(w->m_background_color);
349 case evtWillChangeSize:
350 m_size = *static_cast<eSize*>(data);
353 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
355 case evtParentChangedPosition:
356 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
357 i->event(evtParentChangedPosition);
360 m_focus_owner = (eWidget*)data;
371 void eWidget::setFocus(eWidget *focus)
374 m_current_focus->event(evtFocusLost, this);
375 m_current_focus = focus;
378 m_current_focus->event(evtFocusGot, this);
381 void eWidget::notifyShowHide()
383 event(evtParentVisibilityChanged);
384 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)