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 /* TODO: optimize here to only recalc what's required. possibly merge with hide. */
104 eWidget *root = this;
105 ePoint abspos = position();
106 while (root && !root->m_desktop)
108 root = root->m_parent;
111 /* oops: our root widget does not have a desktop associated.
112 probably somebody already erased the root, but tries some
113 operations on a child window.
114 ignore them for now. */
118 abspos += root->position();
121 root->m_desktop->recalcClipRegions(root);
123 gRegion abs = m_visible_with_childs;
125 root->m_desktop->invalidate(abs);
130 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
131 /* wVisShow flag (because when the widget is shown again, the widgets must */
132 /* become visible again. */
133 if (!(m_vis & wVisShow))
136 /* this is a workaround to the above problem. when we are in the delete phase,
137 don't hide childs. */
138 if (!(m_parent || m_desktop))
141 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
142 eWidget *root = this;
143 ePoint abspos = position();
144 while (root && !root->m_desktop)
146 root = root->m_parent;
149 abspos += root->position();
151 assert(root->m_desktop);
153 gRegion abs = m_visible_with_childs;
156 root->m_desktop->recalcClipRegions(root);
157 root->m_desktop->invalidate(abs);
160 void eWidget::destruct()
163 m_parent->m_childs.remove(this);
167 void eWidget::setBackgroundColor(const gRGB &col)
169 m_background_color = col;
170 m_have_background_color = 1;
173 void eWidget::clearBackgroundColor()
175 m_have_background_color = 0;
178 void eWidget::setZPosition(int z)
183 m_parent->m_childs.remove(this);
184 insertIntoParent(); /* now at the new Z position */
187 void eWidget::setTransparent(int transp)
189 if (isTransparent() != transp)
192 m_vis |= wVisTransparent;
194 m_vis &=~wVisTransparent;
195 recalcClipRegionsWhenVisible();
199 ePoint eWidget::getAbsolutePosition()
201 eWidget *root = this;
202 ePoint abspos = position();
204 while (root && !root->m_desktop)
206 root = root->m_parent;
208 abspos += root->position();
214 void eWidget::mayKillFocus()
217 /* when we have the focus, remove it first. */
219 m_focus_owner->setFocus(0);
226 m_parent->m_childs.remove(this);
230 /* tell all childs that the parent is not anymore existing */
231 ePtrList<eWidget>::iterator i(m_childs.begin());
232 while (i != m_childs.end())
234 (*i)->parentRemoved();
235 i = m_childs.erase(i);
239 void eWidget::insertIntoParent()
241 ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
244 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
246 m_parent->m_childs.insert(i, this);
253 void eWidget::doPaint(gPainter &painter, const gRegion &r)
255 if (m_visible_with_childs.empty())
257 gRegion region = r, childs = r;
258 /* we were in parent's space, now we are in local space */
259 region.moveBy(-position());
260 painter.moveOffset(position());
261 /* check if there's anything for us to paint */
262 region &= m_visible_region;
265 painter.resetClip(region);
266 event(evtPaint, ®ion, &painter);
269 childs.moveBy(-position());
270 /* walk all childs */
271 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
272 i->doPaint(painter, childs);
273 painter.moveOffset(-position());
276 void eWidget::recalcClipRegionsWhenVisible()
281 if (!(t->m_vis & wVisShow))
285 t->m_desktop->recalcClipRegions(t);
293 void eWidget::parentRemoved()
298 int eWidget::event(int event, void *data, void *data2)
304 gPainter &painter = *(gPainter*)data2;
305 // eDebug("eWidget::evtPaint");
306 // dumpRegion(*(gRegion*)data);
307 if (!isTransparent())
309 if (!m_have_background_color)
311 ePtr<eWindowStyle> style;
312 if (!getStyle(style))
313 style->paintBackground(painter, ePoint(0, 0), size());
316 painter.setBackgroundColor(m_background_color);
322 while (w && !w->m_have_background_color)
326 painter.setBackgroundColor(w->m_background_color);
332 case evtWillChangeSize:
333 m_size = *static_cast<eSize*>(data);
336 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
338 case evtParentChangedPosition:
339 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
340 i->event(evtParentChangedPosition);
343 m_focus_owner = (eWidget*)data;
354 void eWidget::setFocus(eWidget *focus)
357 m_current_focus->event(evtFocusLost, this);
358 m_current_focus = focus;
361 m_current_focus->event(evtFocusGot, this);