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;
58 m_client_offset = eSize(0, 0);
59 event(evtWillChangeSize, &size, &m_client_offset);
60 if (old_size == m_size)
62 move(position() - old_offset);
64 event(evtChangedSize);
66 if (m_notify_child_on_position_change)
67 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
68 i->event(evtParentChangedPosition); /* position/size is the same here */
70 recalcClipRegionsWhenVisible(); invalidate();
73 void eWidget::invalidate(const gRegion ®ion)
75 /* we determine the area to redraw, and re-position this
76 area to the absolute position, and then call the
77 desktop's invalidate() with that, which adds this
78 area into the dirty region. */
79 gRegion res = m_visible_with_childs;
86 ePoint abspos = position();
87 int target_layer = m_layer;
89 while (root && !root->m_desktop)
91 root = root->m_parent;
93 if (root->m_layer != -1)
94 target_layer = root->m_layer;
95 abspos += root->position();
98 // eDebug("region to invalidate:");
100 root->m_desktop->invalidate(res, this, target_layer);
105 if (m_vis & wVisShow)
109 // eDebug("show widget %p", this);
112 /* TODO: optimize here to only recalc what's required. possibly merge with hide. */
113 eWidget *root = this;
114 ePoint abspos = position();
115 int target_layer = m_layer;
117 while (root && !root->m_desktop)
119 root = root->m_parent;
122 /* oops: our root widget does not have a desktop associated.
123 probably somebody already erased the root, but tries some
124 operations on a child window.
125 ignore them for now. */
129 if (root->m_layer != -1)
130 target_layer = root->m_layer;
131 abspos += root->position();
134 root->m_desktop->recalcClipRegions(root);
136 gRegion abs = m_visible_with_childs;
138 root->m_desktop->invalidate(abs, this, target_layer);
143 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
144 /* wVisShow flag (because when the widget is shown again, the widgets must */
145 /* become visible again. */
146 if (!(m_vis & wVisShow))
150 /* this is a workaround to the above problem. when we are in the delete phase,
151 don't hide childs. */
152 if (!(m_parent || m_desktop))
156 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
157 eWidget *root = this;
158 ePoint abspos = position();
159 while (root && !root->m_desktop)
161 root = root->m_parent;
164 abspos += root->position();
166 ASSERT(root->m_desktop);
168 gRegion abs = m_visible_with_childs;
171 root->m_desktop->recalcClipRegions(root);
172 root->m_desktop->invalidate(abs);
175 void eWidget::destruct()
178 m_parent->m_childs.remove(this);
182 void eWidget::setBackgroundColor(const gRGB &col)
184 m_background_color = col;
185 m_have_background_color = 1;
188 void eWidget::clearBackgroundColor()
190 m_have_background_color = 0;
193 void eWidget::setZPosition(int z)
198 m_parent->m_childs.remove(this);
199 insertIntoParent(); /* now at the new Z position */
202 void eWidget::setTransparent(int transp)
204 if (isTransparent() != transp)
207 m_vis |= wVisTransparent;
209 m_vis &=~wVisTransparent;
210 recalcClipRegionsWhenVisible();
214 ePoint eWidget::getAbsolutePosition()
216 eWidget *root = this;
217 ePoint abspos = position();
219 while (root && !root->m_desktop)
221 root = root->m_parent;
223 abspos += root->position();
229 void eWidget::mayKillFocus()
232 /* when we have the focus, remove it first. */
234 m_focus_owner->setFocus(0);
241 m_parent->m_childs.remove(this);
245 /* tell all childs that the parent is not anymore existing */
246 ePtrList<eWidget>::iterator i(m_childs.begin());
247 while (i != m_childs.end())
249 (*i)->parentRemoved();
250 i = m_childs.erase(i);
254 void eWidget::insertIntoParent()
256 ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
259 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
261 m_parent->m_childs.insert(i, this);
268 void eWidget::doPaint(gPainter &painter, const gRegion &r, int layer)
270 if (m_visible_with_childs.empty())
272 gRegion region = r, childs = r;
273 /* we were in parent's space, now we are in local space */
274 region.moveBy(-position());
275 painter.moveOffset(position());
276 /* check if there's anything for us to paint */
277 if (layer == m_layer)
279 region &= m_visible_region;
282 painter.resetClip(region);
283 event(evtPaint, ®ion, &painter);
287 childs.moveBy(-position());
288 /* walk all childs */
289 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
290 i->doPaint(painter, childs, layer);
291 painter.moveOffset(-position());
294 void eWidget::recalcClipRegionsWhenVisible()
299 if (!(t->m_vis & wVisShow))
303 t->m_desktop->recalcClipRegions(t);
311 void eWidget::parentRemoved()
316 int eWidget::event(int event, void *data, void *data2)
322 gPainter &painter = *(gPainter*)data2;
323 // eDebug("eWidget::evtPaint");
324 // dumpRegion(*(gRegion*)data);
325 if (!isTransparent())
327 if (!m_have_background_color)
329 ePtr<eWindowStyle> style;
330 if (!getStyle(style))
331 style->paintBackground(painter, ePoint(0, 0), size());
334 painter.setBackgroundColor(m_background_color);
340 while (w && !w->m_have_background_color)
344 painter.setBackgroundColor(w->m_background_color);
350 case evtWillChangeSize:
351 m_size = *static_cast<eSize*>(data);
354 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
356 case evtParentChangedPosition:
357 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
358 i->event(evtParentChangedPosition);
361 m_focus_owner = (eWidget*)data;
372 void eWidget::setFocus(eWidget *focus)
375 m_current_focus->event(evtFocusLost, this);
376 m_current_focus = focus;
379 m_current_focus->event(evtFocusGot, this);
382 void eWidget::notifyShowHide()
384 event(evtParentVisibilityChanged);
385 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)