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;
12 m_client_offset = eSize(0, 0);
19 m_parent->m_childs.push_back(this);
20 m_parent->getStyle(m_style);
27 void eWidget::move(ePoint pos)
29 m_position = pos + m_client_offset;
31 if (m_position == pos)
34 /* we invalidate before and after the move to
35 cause a correct redraw. The area which is
36 included both before and after isn't redrawn
37 twice because a invalidate doesn't immediately
38 redraws the region. */
40 event(evtChangedPosition);
41 recalcClipRegionsWhenVisible();
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();
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();
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::mayKillFocus()
172 /* when we have the focus, remove it first. */
174 m_focus_owner->setFocus(0);
182 m_parent->m_childs.remove(this);
186 /* destroy all childs */
187 ePtrList<eWidget>::iterator i(m_childs.begin());
188 while (i != m_childs.end())
192 i = m_childs.erase(i);
196 void eWidget::doPaint(gPainter &painter, const gRegion &r)
198 if (m_visible_with_childs.empty())
202 /* we were in parent's space, now we are in local space */
203 region.moveBy(-position());
205 painter.moveOffset(position());
206 /* walk all childs */
207 for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
208 i->doPaint(painter, region);
210 /* check if there's anything for us to paint */
211 region &= m_visible_region;
215 painter.resetClip(region);
216 event(evtPaint, ®ion, &painter);
219 painter.moveOffset(-position());
222 void eWidget::recalcClipRegionsWhenVisible()
227 if (!(t->m_vis & wVisShow))
231 t->m_desktop->recalcClipRegions();
239 int eWidget::event(int event, void *data, void *data2)
245 gPainter &painter = *(gPainter*)data2;
247 // eDebug("eWidget::evtPaint");
248 // dumpRegion(*(gRegion*)data);
249 if (!m_have_background_color)
251 ePtr<eWindowStyle> style;
252 if (!getStyle(style))
253 style->paintBackground(painter, ePoint(0, 0), size());
256 painter.setBackgroundColor(m_background_color);
263 case evtWillChangeSize:
264 m_size = *static_cast<eSize*>(data);
268 m_clip_region = gRegion(eRect(ePoint(0, 0), m_size));
272 m_focus_owner = (eWidget*)data;
283 void eWidget::setFocus(eWidget *focus)
286 m_current_focus->event(evtFocusLost, this);
288 m_current_focus = focus;
291 m_current_focus->event(evtFocusGot, this);