widget: don't crash when deallocating in wrong order
[enigma2.git] / lib / gui / ewidget.cpp
index 8cb7d4ae0f4fdd15dbd000df2655207cf83b9ee7..3bc6e663841e940e1d7506f7baf4722c90f01a24 100644 (file)
@@ -8,6 +8,7 @@ eWidget::eWidget(eWidget *parent): m_animation(this), m_parent(parent ? parent->
        m_vis = 0;
        m_desktop = 0;
        m_have_background_color = 0;
        m_vis = 0;
        m_desktop = 0;
        m_have_background_color = 0;
+       m_z_position = 0;
        
        m_client_offset = eSize(0, 0);
        
        
        m_client_offset = eSize(0, 0);
        
@@ -16,7 +17,7 @@ eWidget::eWidget(eWidget *parent): m_animation(this), m_parent(parent ? parent->
        
        if (m_parent)
        {
        
        if (m_parent)
        {
-               m_parent->m_childs.push_back(this);
+               insertIntoParent();
                m_parent->getStyle(m_style);
        }
 
                m_parent->getStyle(m_style);
        }
 
@@ -31,6 +32,9 @@ void eWidget::move(ePoint pos)
        if (m_position == pos)
                return;
 
        if (m_position == pos)
                return;
 
+                       /* ?? what about native move support? */
+       invalidate();
+
        m_position = pos;
        
        event(evtChangedPosition);
        m_position = pos;
        
        event(evtChangedPosition);
@@ -136,6 +140,8 @@ void eWidget::hide()
        while (root && !root->m_desktop)
        {
                root = root->m_parent;
        while (root && !root->m_desktop)
        {
                root = root->m_parent;
+               if (!root)
+                       return;
                abspos += root->position();
        }
        assert(root->m_desktop);
                abspos += root->position();
        }
        assert(root->m_desktop);
@@ -165,6 +171,25 @@ void eWidget::clearBackgroundColor()
        m_have_background_color = 0;
 }
 
        m_have_background_color = 0;
 }
 
+void eWidget::setZPosition(int z)
+{
+       m_z_position = z;
+       if (!m_parent)
+               return;
+       
+       m_parent->m_childs.remove(this);
+       
+       insertIntoParent(); /* now at the new Z position */
+}
+
+void eWidget::setTransparent(int transp)
+{
+       if (transp)
+               m_vis |= wVisTransparent;
+       else
+               m_vis &=~wVisTransparent;
+}
+
 void eWidget::mayKillFocus()
 {
        setFocus(0);
 void eWidget::mayKillFocus()
 {
        setFocus(0);
@@ -182,29 +207,40 @@ eWidget::~eWidget()
 
        m_parent = 0;
 
 
        m_parent = 0;
 
-               /* destroy all childs */
+               /* tell all childs that the parent is not anymore existing */
        ePtrList<eWidget>::iterator i(m_childs.begin());
        while (i != m_childs.end())
        {
        ePtrList<eWidget>::iterator i(m_childs.begin());
        while (i != m_childs.end())
        {
-               (*i)->m_parent = 0;
-               delete *i;
+               (*i)->parentRemoved();
                i = m_childs.erase(i);
        }
 }
 
                i = m_childs.erase(i);
        }
 }
 
+void eWidget::insertIntoParent()
+{
+       ePtrList<eWidget>::iterator i = m_parent->m_childs.begin();
+       
+       for(;;)
+       {
+               if ((i == m_parent->m_childs.end()) || (i->m_z_position > m_z_position))
+               {
+                       m_parent->m_childs.insert(i, this);
+                       return;
+               }
+               ++i;
+       }
+}
+
 void eWidget::doPaint(gPainter &painter, const gRegion &r)
 {
        if (m_visible_with_childs.empty())
                return;
        
 void eWidget::doPaint(gPainter &painter, const gRegion &r)
 {
        if (m_visible_with_childs.empty())
                return;
        
-       gRegion region = r;
+       gRegion region = r, childs = r;
                        /* we were in parent's space, now we are in local space */
        region.moveBy(-position());
        
        painter.moveOffset(position());
                        /* we were in parent's space, now we are in local space */
        region.moveBy(-position());
        
        painter.moveOffset(position());
-               /* walk all childs */
-       for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
-               i->doPaint(painter, region);
        
                /* check if there's anything for us to paint */
        region &= m_visible_region;
        
                /* check if there's anything for us to paint */
        region &= m_visible_region;
@@ -214,6 +250,11 @@ void eWidget::doPaint(gPainter &painter, const gRegion &r)
                painter.resetClip(region);
                event(evtPaint, &region, &painter);
        }
                painter.resetClip(region);
                event(evtPaint, &region, &painter);
        }
+
+       childs.moveBy(-position());
+               /* walk all childs */
+       for (ePtrList<eWidget>::iterator i(m_childs.begin()); i != m_childs.end(); ++i)
+               i->doPaint(painter, childs);
        
        painter.moveOffset(-position());
 }
        
        painter.moveOffset(-position());
 }
@@ -235,6 +276,11 @@ void eWidget::recalcClipRegionsWhenVisible()
        } while(1);
 }
 
        } while(1);
 }
 
+void eWidget::parentRemoved()
+{
+       m_parent = 0;
+}
+
 int eWidget::event(int event, void *data, void *data2)
 {
        switch (event)
 int eWidget::event(int event, void *data, void *data2)
 {
        switch (event)
@@ -245,15 +291,22 @@ int eWidget::event(int event, void *data, void *data2)
                
 //             eDebug("eWidget::evtPaint");
 //             dumpRegion(*(gRegion*)data);
                
 //             eDebug("eWidget::evtPaint");
 //             dumpRegion(*(gRegion*)data);
-               if (!m_have_background_color)
+               if (!isTransparent())
                {
                {
-                       ePtr<eWindowStyle> style;
-                       if (!getStyle(style))
-                               style->paintBackground(painter, ePoint(0, 0), size());
+                       if (!m_have_background_color)
+                       {
+                               ePtr<eWindowStyle> style;
+                               if (!getStyle(style))
+                                       style->paintBackground(painter, ePoint(0, 0), size());
+                       } else
+                       {
+                               painter.setBackgroundColor(m_background_color);
+                               painter.clear();
+                       }
                } else
                {
                } else
                {
-                       painter.setBackgroundColor(m_background_color);
-                       painter.clear();
+                       if (m_have_background_color)
+                               painter.setBackgroundColor(m_background_color);
                }
                break;
        }
                }
                break;
        }