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_parent(parent ? parent->child() : 0)
10 m_have_background_color = 0;
17 m_parent->m_childs.push_back(this);
18 m_parent->getStyle(m_style);
25 void eWidget::move(ePoint pos)
27 if (m_position == pos)
32 /* we invalidate before and after the move to
33 cause a correct redraw. The area which is
34 included both before and after isn't redrawn
35 twice because a invalidate doesn't immediately
36 redraws the region. */
38 event(evtChangedPosition);
39 recalcClipRegionsWhenVisible();
43 void eWidget::resize(eSize size)
45 /* same strategy as with move: we first check if
46 the size changed at all, and if it did, we
47 invalidate both the old and new area.
48 TODO: check if either the old or new area
49 fits into the other completely, and invalidate
51 eSize old_size = m_size;
52 eSize offset = eSize(0, 0);
53 event(evtWillChangeSize, &size, &offset);
54 if (old_size == m_size)
56 move(position() + offset);
59 event(evtChangedSize);
60 recalcClipRegionsWhenVisible();
64 void eWidget::invalidate(const gRegion ®ion)
66 /* we determine the area to redraw, and re-position this
67 area to the absolute position, and then call the
68 desktop's invalidate() with that, which adds this
69 area into the dirty region. */
70 gRegion res = m_visible_with_childs;
78 ePoint abspos = position();
79 while (root && !root->m_desktop)
81 root = root->m_parent;
83 abspos += root->position();
87 // eDebug("region to invalidate:");
89 root->m_desktop->invalidate(res);
99 /* TODO: optimize here to only recalc what's required. possibly merge with hide. */
100 eWidget *root = this;
101 ePoint abspos = position();
102 while (root && !root->m_desktop)
104 root = root->m_parent;
106 abspos += root->position();
109 root->m_desktop->recalcClipRegions();
111 gRegion abs = m_visible_with_childs;
113 root->m_desktop->invalidate(abs);
118 /* TODO: when hiding an upper level widget, widgets get hidden but keep the */
119 /* wVisShow flag (because when the widget is shown again, the widgets must */
120 /* become visible again. */
121 if (!(m_vis & wVisShow))
125 /* this is a workaround to the above problem. when we are in the delete phase,
126 don't hide childs. */
127 if (!(m_parent || m_desktop))
130 /* TODO: optimize here to only recalc what's required. possibly merge with show. */
131 eWidget *root = this;
132 ePoint abspos = position();
133 while (root && !root->m_desktop)
135 root = root->m_parent;
136 abspos += root->position();
138 assert(root->m_desktop);
140 gRegion abs = m_visible_with_childs;
143 root->m_desktop->recalcClipRegions();
144 root->m_desktop->invalidate(abs);
147 void eWidget::destruct()
150 m_parent->m_childs.remove(this);
154 void eWidget::setBackgroundColor(const gRGB &col)
156 m_background_color = col;
157 m_have_background_color = 1;
160 void eWidget::clearBackgroundColor()
162 m_have_background_color = 0;
165 void eWidget::mayKillFocus()
168 /* when we have the focus, remove it first. */
170 m_focus_owner->setFocus(0);
178 m_parent->m_childs.remove(this);
182 /* destroy all childs */
183 ePtrList<eWidget>::iterator i(m_childs.begin());
184 while (i != m_childs.end())
188 i = m_childs.erase(i);
192 void eWidget::doPaint(gPainter &painter, const gRegion &r)
194 if (m_visible_with_childs.empty())
198 /* we were in parent's space, now we are in local space */
199 region.moveBy(-position());
201 painter.moveOffset(position());
202 /* walk all childs */
203 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
204 i->doPaint(painter, region);
206 /* check if there's anything for us to paint */
207 region &= m_visible_region;
211 painter.resetClip(region);
212 event(evtPaint, ®ion, &painter);
215 painter.moveOffset(-position());
218 void eWidget::recalcClipRegionsWhenVisible()
223 if (!(t->m_vis & wVisShow))
227 t->m_desktop->recalcClipRegions();
235 int eWidget::event(int event, void *data, void *data2)
241 gPainter &painter = *(gPainter*)data2;
243 // eDebug("eWidget::evtPaint");
244 // dumpRegion(*(gRegion*)data);
245 if (!m_have_background_color)
247 ePtr<eWindowStyle> style;
248 if (!getStyle(style))
249 style->paintBackground(painter, ePoint(0, 0), size());
252 painter.setBackgroundColor(m_background_color);
259 case evtWillChangeSize:
260 m_size = *static_cast<eSize*>(data);
264 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
268 m_focus_owner = (eWidget*)data;
279 void eWidget::setFocus(eWidget *focus)
281 eDebug("setFocus in %p to %p, was %p", this, focus, m_current_focus);
283 m_current_focus->event(evtFocusLost, this);
285 m_current_focus = focus;
288 m_current_focus->event(evtFocusGot, this);