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)
37 event(evtChangedPosition);
38 recalcClipRegionsWhenVisible();
40 /* try native move if supported. */
41 if ((m_vis & wVisShow) && ((!m_desktop) || m_desktop->movedWidget(this)))
45 void eWidget::resize(eSize size)
47 /* same strategy as with move: we first check if
48 the size changed at all, and if it did, we
49 invalidate both the old and new area.
50 TODO: check if either the old or new area
51 fits into the other completely, and invalidate
53 eSize old_size = m_size;
54 eSize old_offset = m_client_offset;
55 m_client_offset = eSize(0, 0);
56 event(evtWillChangeSize, &size, &m_client_offset);
57 if (old_size == m_size)
60 move(position() - old_offset);
63 event(evtChangedSize);
64 recalcClipRegionsWhenVisible();
68 void eWidget::invalidate(const gRegion ®ion)
70 /* we determine the area to redraw, and re-position this
71 area to the absolute position, and then call the
72 desktop's invalidate() with that, which adds this
73 area into the dirty region. */
74 gRegion res = m_visible_with_childs;
82 ePoint abspos = position();
83 while (root && !root->m_desktop)
85 root = root->m_parent;
87 abspos += root->position();
91 // eDebug("region to invalidate:");
93 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;
110 abspos += root->position();
113 root->m_desktop->recalcClipRegions(root);
115 gRegion abs = m_visible_with_childs;
117 root->m_desktop->invalidate(abs);
122 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
123 /* wVisShow flag (because when the widget is shown again, the widgets must */
124 /* become visible again. */
125 if (!(m_vis & wVisShow))
129 /* this is a workaround to the above problem. when we are in the delete phase,
130 don't hide childs. */
131 if (!(m_parent || m_desktop))
134 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
135 eWidget *root = this;
136 ePoint abspos = position();
137 while (root && !root->m_desktop)
139 root = root->m_parent;
140 abspos += root->position();
142 assert(root->m_desktop);
144 gRegion abs = m_visible_with_childs;
147 root->m_desktop->recalcClipRegions(root);
148 root->m_desktop->invalidate(abs);
151 void eWidget::destruct()
154 m_parent->m_childs.remove(this);
158 void eWidget::setBackgroundColor(const gRGB &col)
160 m_background_color = col;
161 m_have_background_color = 1;
164 void eWidget::clearBackgroundColor()
166 m_have_background_color = 0;
169 void eWidget::setZPosition(int z)
175 m_parent->m_childs.remove(this);
177 insertIntoParent(); /* now at the new Z position */
180 void eWidget::setTransparent(int transp)
183 m_vis |= wVisTransparent;
185 m_vis &=~wVisTransparent;
188 void eWidget::mayKillFocus()
191 /* when we have the focus, remove it first. */
193 m_focus_owner->setFocus(0);
201 m_parent->m_childs.remove(this);
205 /* destroy all childs */
206 ePtrList<eWidget>::iterator i(m_childs.begin());
207 while (i != m_childs.end())
211 i = m_childs.erase(i);
215 void eWidget::insertIntoParent()
217 ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
221 if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
223 m_parent->m_childs.insert(i, this);
230 void eWidget::doPaint(gPainter &painter, const gRegion &r)
232 if (m_visible_with_childs.empty())
235 gRegion region = r, childs = r;
236 /* we were in parent's space, now we are in local space */
237 region.moveBy(-position());
239 painter.moveOffset(position());
241 /* check if there's anything for us to paint */
242 region &= m_visible_region;
246 painter.resetClip(region);
247 event(evtPaint, ®ion, &painter);
250 childs.moveBy(-position());
251 /* walk all childs */
252 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
253 i->doPaint(painter, childs);
255 painter.moveOffset(-position());
258 void eWidget::recalcClipRegionsWhenVisible()
263 if (!(t->m_vis & wVisShow))
267 t->m_desktop->recalcClipRegions(t);
275 int eWidget::event(int event, void *data, void *data2)
281 gPainter &painter = *(gPainter*)data2;
283 // eDebug("eWidget::evtPaint");
284 // dumpRegion(*(gRegion*)data);
285 if (!isTransparent())
287 if (!m_have_background_color)
289 ePtr<eWindowStyle> style;
290 if (!getStyle(style))
291 style->paintBackground(painter, ePoint(0, 0), size());
294 painter.setBackgroundColor(m_background_color);
299 if (m_have_background_color)
300 painter.setBackgroundColor(m_background_color);
306 case evtWillChangeSize:
307 m_size = *static_cast<eSize*>(data);
311 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
315 m_focus_owner = (eWidget*)data;
326 void eWidget::setFocus(eWidget *focus)
329 m_current_focus->event(evtFocusLost, this);
331 m_current_focus = focus;
334 m_current_focus->event(evtFocusGot, this);