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);
28 void eWidget::move(ePoint pos)
30 pos = pos + m_client_offset;
32 if (m_position == pos)
35 /* ?? what about native move support? */
40 event(evtChangedPosition);
41 recalcClipRegionsWhenVisible();
43 /* try native move if supported. */
44 if ((m_vis & wVisShow) && ((!m_desktop) || m_desktop->movedWidget(this)))
48 void eWidget::resize(eSize size)
50 /* same strategy as with move: we first check if
51 the size changed at all, and if it did, we
52 invalidate both the old and new area.
53 TODO: check if either the old or new area
54 fits into the other completely, and invalidate
56 eSize old_size = m_size;
57 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)
63 move(position() - old_offset);
66 event(evtChangedSize);
67 recalcClipRegionsWhenVisible();
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;
85 ePoint abspos = position();
86 while (root && !root->m_desktop)
88 root = root->m_parent;
90 abspos += root->position();
94 // eDebug("region to invalidate:");
96 root->m_desktop->invalidate(res);
101 if (m_vis & wVisShow)
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.
118 ignore them for now. */
122 abspos += root->position();
125 root->m_desktop->recalcClipRegions(root);
127 gRegion abs = m_visible_with_childs;
129 root->m_desktop->invalidate(abs);
134 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
135 /* wVisShow flag (because when the widget is shown again, the widgets must */
136 /* become visible again. */
137 if (!(m_vis & wVisShow))
141 /* this is a workaround to the above problem. when we are in the delete phase,
142 don't hide childs. */
143 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)
189 m_parent->m_childs.remove(this);
191 insertIntoParent(); /* now at the new Z position */
194 void eWidget::setTransparent(int transp)
197 m_vis |= wVisTransparent;
199 m_vis &=~wVisTransparent;
202 ePoint eWidget::getAbsolutePosition()
204 eWidget *root = this;
205 ePoint abspos = position();
207 while (root && !root->m_desktop)
209 root = root->m_parent;
211 abspos += root->position();
217 void eWidget::mayKillFocus()
220 /* when we have the focus, remove it first. */
222 m_focus_owner->setFocus(0);
230 m_parent->m_childs.remove(this);
234 /* tell all childs that the parent is not anymore existing */
235 ePtrList<eWidget>::iterator i(m_childs.begin());
236 while (i != m_childs.end())
238 (*i)->parentRemoved();
239 i = m_childs.erase(i);
243 void eWidget::insertIntoParent()
245 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())
263 gRegion region = r, childs = r;
264 /* we were in parent's space, now we are in local space */
265 region.moveBy(-position());
267 painter.moveOffset(position());
269 /* check if there's anything for us to paint */
270 region &= m_visible_region;
274 painter.resetClip(region);
275 event(evtPaint, ®ion, &painter);
278 childs.moveBy(-position());
279 /* walk all childs */
280 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
281 i->doPaint(painter, childs);
283 painter.moveOffset(-position());
286 void eWidget::recalcClipRegionsWhenVisible()
291 if (!(t->m_vis & wVisShow))
295 t->m_desktop->recalcClipRegions(t);
303 void eWidget::parentRemoved()
308 int eWidget::event(int event, void *data, void *data2)
314 gPainter &painter = *(gPainter*)data2;
316 // eDebug("eWidget::evtPaint");
317 // dumpRegion(*(gRegion*)data);
318 if (!isTransparent())
320 if (!m_have_background_color)
322 ePtr<eWindowStyle> style;
323 if (!getStyle(style))
324 style->paintBackground(painter, ePoint(0, 0), size());
327 painter.setBackgroundColor(m_background_color);
332 if (m_have_background_color)
333 painter.setBackgroundColor(m_background_color);
339 case evtWillChangeSize:
340 m_size = *static_cast<eSize*>(data);
344 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
348 m_focus_owner = (eWidget*)data;
359 void eWidget::setFocus(eWidget *focus)
362 m_current_focus->event(evtFocusLost, this);
364 m_current_focus = focus;
367 m_current_focus->event(evtFocusGot, this);