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;
113 abspos += root->position();
116 root->m_desktop->recalcClipRegions(root);
118 gRegion abs = m_visible_with_childs;
120 root->m_desktop->invalidate(abs);
125 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
126 /* wVisShow flag (because when the widget is shown again, the widgets must */
127 /* become visible again. */
128 if (!(m_vis & wVisShow))
132 /* this is a workaround to the above problem. when we are in the delete phase,
133 don't hide childs. */
134 if (!(m_parent || m_desktop))
137 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
138 eWidget *root = this;
139 ePoint abspos = position();
140 while (root && !root->m_desktop)
142 root = root->m_parent;
145 abspos += root->position();
147 assert(root->m_desktop);
149 gRegion abs = m_visible_with_childs;
152 root->m_desktop->recalcClipRegions(root);
153 root->m_desktop->invalidate(abs);
156 void eWidget::destruct()
159 m_parent->m_childs.remove(this);
163 void eWidget::setBackgroundColor(const gRGB &col)
165 m_background_color = col;
166 m_have_background_color = 1;
169 void eWidget::clearBackgroundColor()
171 m_have_background_color = 0;
174 void eWidget::setZPosition(int z)
180 m_parent->m_childs.remove(this);
182 insertIntoParent(); /* now at the new Z position */
185 void eWidget::setTransparent(int transp)
188 m_vis |= wVisTransparent;
190 m_vis &=~wVisTransparent;
193 void eWidget::mayKillFocus()
196 /* when we have the focus, remove it first. */
198 m_focus_owner->setFocus(0);
206 m_parent->m_childs.remove(this);
210 /* tell all childs that the parent is not anymore existing */
211 ePtrList<eWidget>::iterator i(m_childs.begin());
212 while (i != m_childs.end())
214 (*i)->parentRemoved();
215 i = m_childs.erase(i);
219 void eWidget::insertIntoParent()
221 ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
225 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
227 m_parent->m_childs.insert(i, this);
234 void eWidget::doPaint(gPainter &painter, const gRegion &r)
236 if (m_visible_with_childs.empty())
239 gRegion region = r, childs = r;
240 /* we were in parent's space, now we are in local space */
241 region.moveBy(-position());
243 painter.moveOffset(position());
245 /* check if there's anything for us to paint */
246 region &= m_visible_region;
250 painter.resetClip(region);
251 event(evtPaint, ®ion, &painter);
254 childs.moveBy(-position());
255 /* walk all childs */
256 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
257 i->doPaint(painter, childs);
259 painter.moveOffset(-position());
262 void eWidget::recalcClipRegionsWhenVisible()
267 if (!(t->m_vis & wVisShow))
271 t->m_desktop->recalcClipRegions(t);
279 void eWidget::parentRemoved()
284 int eWidget::event(int event, void *data, void *data2)
290 gPainter &painter = *(gPainter*)data2;
292 // eDebug("eWidget::evtPaint");
293 // dumpRegion(*(gRegion*)data);
294 if (!isTransparent())
296 if (!m_have_background_color)
298 ePtr<eWindowStyle> style;
299 if (!getStyle(style))
300 style->paintBackground(painter, ePoint(0, 0), size());
303 painter.setBackgroundColor(m_background_color);
308 if (m_have_background_color)
309 painter.setBackgroundColor(m_background_color);
315 case evtWillChangeSize:
316 m_size = *static_cast<eSize*>(data);
320 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
324 m_focus_owner = (eWidget*)data;
335 void eWidget::setFocus(eWidget *focus)
338 m_current_focus->event(evtFocusLost, this);
340 m_current_focus = focus;
343 m_current_focus->event(evtFocusGot, this);