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)
209 m_vis |= wVisTransparent;
211 m_vis &=~wVisTransparent;
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);
242 m_parent->m_childs.remove(this);
246 /* tell all childs that the parent is not anymore existing */
247 ePtrList<eWidget>::iterator i(m_childs.begin());
248 while (i != m_childs.end())
250 (*i)->parentRemoved();
251 i = m_childs.erase(i);
255 void eWidget::insertIntoParent()
257 ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
261 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
263 m_parent->m_childs.insert(i, this);
270 void eWidget::doPaint(gPainter &painter, const gRegion &r)
272 if (m_visible_with_childs.empty())
275 gRegion region = r, childs = r;
276 /* we were in parent's space, now we are in local space */
277 region.moveBy(-position());
279 painter.moveOffset(position());
281 /* check if there's anything for us to paint */
282 region &= m_visible_region;
286 painter.resetClip(region);
287 event(evtPaint, ®ion, &painter);
290 childs.moveBy(-position());
291 /* walk all childs */
292 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
293 i->doPaint(painter, childs);
295 painter.moveOffset(-position());
298 void eWidget::recalcClipRegionsWhenVisible()
303 if (!(t->m_vis & wVisShow))
307 t->m_desktop->recalcClipRegions(t);
315 void eWidget::parentRemoved()
320 int eWidget::event(int event, void *data, void *data2)
326 gPainter &painter = *(gPainter*)data2;
328 // eDebug("eWidget::evtPaint");
329 // dumpRegion(*(gRegion*)data);
330 if (!isTransparent())
332 if (!m_have_background_color)
334 ePtr<eWindowStyle> style;
335 if (!getStyle(style))
336 style->paintBackground(painter, ePoint(0, 0), size());
339 painter.setBackgroundColor(m_background_color);
344 if (m_have_background_color)
345 painter.setBackgroundColor(m_background_color);
351 case evtWillChangeSize:
352 m_size = *static_cast<eSize*>(data);
355 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
357 case evtParentChangedPosition:
358 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
359 i->event(evtParentChangedPosition);
362 m_focus_owner = (eWidget*)data;
373 void eWidget::setFocus(eWidget *focus)
376 m_current_focus->event(evtFocusLost, this);
378 m_current_focus = focus;
381 m_current_focus->event(evtFocusGot, this);