]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/controls/ctrl_tree.cpp
skins2: fix arrows short of 1
[vlc] / modules / gui / skins2 / controls / ctrl_tree.cpp
index c44c888fef4edbf0391643e759a6094fad5af121..b8be9b453f9cf4f8f7cb7b31d7e42392265cc0b4 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * ctrl_tree.cpp
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
+ * Copyright (C) 2003 the VideoLAN team
  * $Id$
  *
  * Authors: Antoine Cellerier <dionoea@videolan.org>
+ *          ClĂ©ment Stenac <zorglub@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #include <math.h>
+#include "../utils/var_bool.hpp"
 #include "ctrl_tree.hpp"
 #include "../src/os_factory.hpp"
 #include "../src/os_graphics.hpp"
 #include "../events/evt_key.hpp"
 #include "../events/evt_mouse.hpp"
 #include "../events/evt_scroll.hpp"
-#include "vlc_keys.h"
-#ifdef sun
-#   include "solaris_specific.h" // for lrint
-#endif
+#include <vlc_keys.h>
 
 #define SCROLL_STEP 0.05
 #define LINE_INTERVAL 1  // Number of pixels inserted between 2 lines
@@ -55,19 +54,22 @@ CtrlTree::CtrlTree( intf_thread_t *pIntf,
                     uint32_t bgColor2,
                     uint32_t selColor,
                     const UString &rHelp,
-                    VarBool *pVisible ):
+                    VarBool *pVisible,
+                    VarBool *pFlat ):
     CtrlGeneric( pIntf,rHelp, pVisible), m_rTree( rTree), m_rFont( rFont ),
     m_pBgBitmap( pBgBitmap ), m_pItemBitmap( pItemBitmap ),
     m_pOpenBitmap( pOpenBitmap ), m_pClosedBitmap( pClosedBitmap ),
     m_fgColor( fgColor ), m_playColor( playColor ), m_bgColor1( bgColor1 ),
     m_bgColor2( bgColor2 ), m_selColor( selColor ),
-    m_pLastSelected( NULL ), m_pImage( NULL )
+    m_pLastSelected( NULL ), m_pImage( NULL ), m_dontMove( false )
 {
     // Observe the tree and position variables
     m_rTree.addObserver( this );
     m_rTree.getPositionVar().addObserver( this );
 
-    m_lastPos = m_rTree.begin();
+    m_flat = pFlat->get();
+
+    m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
 
     makeImage();
 }
@@ -76,22 +78,22 @@ CtrlTree::~CtrlTree()
 {
     m_rTree.getPositionVar().delObserver( this );
     m_rTree.delObserver( this );
-    if( m_pImage )
-    {
-        delete m_pImage;
-    }
+    delete m_pImage;
 }
 
 int CtrlTree::itemHeight()
 {
     int itemHeight = m_rFont.getSize();
-    if( m_pClosedBitmap )
-    {
-        itemHeight = __MAX( m_pClosedBitmap->getHeight(), itemHeight );
-    }
-    if( m_pOpenBitmap )
+    if( !m_flat )
     {
-        itemHeight = __MAX( m_pOpenBitmap->getHeight(), itemHeight );
+        if( m_pClosedBitmap )
+        {
+            itemHeight = __MAX( m_pClosedBitmap->getHeight(), itemHeight );
+        }
+        if( m_pOpenBitmap )
+        {
+            itemHeight = __MAX( m_pOpenBitmap->getHeight(), itemHeight );
+        }
     }
     if( m_pItemBitmap )
     {
@@ -104,13 +106,16 @@ int CtrlTree::itemHeight()
 int CtrlTree::itemImageWidth()
 {
     int bitmapWidth = 5;
-    if( m_pClosedBitmap )
-    {
-        bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
-    }
-    if( m_pOpenBitmap )
+    if( !m_flat )
     {
-        bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
+        if( m_pClosedBitmap )
+        {
+            bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
+        }
+        if( m_pOpenBitmap )
+        {
+            bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
+        }
     }
     if( m_pItemBitmap )
     {
@@ -130,21 +135,68 @@ int CtrlTree::maxItems()
 }
 
 
-void CtrlTree::onUpdate( Subject<VarTree> &rTree )
+void CtrlTree::onUpdate( Subject<VarTree, tree_update> &rTree,
+                         tree_update *arg )
 {
-    // Invalidate the position when the tree is updated
-    m_lastPos = m_rTree.begin();
+    m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
 
-    autoScroll();
-    m_pLastSelected = NULL;
+    if( arg->i_type == 0 ) // Item update
+    {
+        if( arg->b_active_item )
+        {
+            autoScroll();
+            ///\todo We should make image if we are visible in the view
+            makeImage();
+        }
+    }
+    /// \todo handle delete in a more clever way
+    else if ( arg->i_type == 1 ) // Global change or deletion
+    {
+        makeImage();
+    }
+    else if ( arg->i_type == 2 ) // Item-append
+    {
+        if( m_flat && m_firstPos->size() )
+            m_firstPos = m_rTree.getNextLeaf( m_firstPos );
+        /// \todo Check if the item is really visible in the view
+        // (we only check if it in the document)
+        if( arg->b_visible == true )
+        {
+            makeImage();
+        }
+    }
+    else if( arg->i_type == 3 ) // item-del
+    {
+        /* Make sure firstPos and lastSelected are still valid */
+        while( m_firstPos->m_deleted && m_firstPos != m_rTree.root()->begin() )
+        {
+            m_firstPos = m_flat ? m_rTree.getPrevLeaf( m_firstPos )
+                                : m_rTree.getPrevVisibleItem( m_firstPos );
+        }
+        if( m_firstPos->m_deleted )
+            m_firstPos = m_flat ? m_rTree.firstLeaf()
+                                : m_rTree.root()->begin();
+
+        if( arg->b_visible == true )
+        {
+            makeImage();
+        }
+    }
+    notifyLayout();
 }
 
-void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
+void CtrlTree::onUpdate( Subject<VarPercent> &rPercent, void* arg)
 {
     // Determine what is the first item to display
-    VarTree::Iterator it = m_rTree.begin();
+    VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
 
-    int excessItems = m_rTree.visibleItems() - maxItems();
+    if( m_dontMove ) return;
+
+    int excessItems;
+    if( m_flat )
+        excessItems = m_rTree.countLeafs() - maxItems();
+    else
+        excessItems = m_rTree.visibleItems() - maxItems();
 
     if( excessItems > 0)
     {
@@ -153,12 +205,15 @@ void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
 #ifdef _MSC_VER
 #   define lrint (int)
 #endif
-        it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
+        if( m_flat )
+            it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
+        else
+            it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
     }
-    if( m_lastPos != it )
+    if( m_firstPos != it )
     {
         // Redraw the control if the position has changed
-        m_lastPos = it;
+        m_firstPos = it;
         makeImage();
         notifyLayout();
     }
@@ -166,11 +221,14 @@ void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
 
 void CtrlTree::onResize()
 {
-// FIXME : shouldn't be the same as the onUpdate function ... but i'm lazy
     // Determine what is the first item to display
-    VarTree::Iterator it = m_rTree.begin();
+    VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
 
-    int excessItems = m_rTree.visibleItems() - maxItems();
+    int excessItems;
+    if( m_flat )
+        excessItems = m_rTree.countLeafs() - maxItems();
+    else
+        excessItems = m_rTree.visibleItems() - maxItems();
 
     if( excessItems > 0)
     {
@@ -179,147 +237,229 @@ void CtrlTree::onResize()
 #ifdef _MSC_VER
 #   define lrint (int)
 #endif
-        it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
-    }
-    // Redraw the control if the position has changed
-    m_lastPos = it;
-    makeImage();
-    notifyLayout();
-#if 0
-    // Determine what is the first item to display
-    VarTree::Iterator it = m_rTree.begin();
-
-    int excessItems = m_rTree.visibleItems() - maxItems();
-
-    if( excessItems > 0)
-    {
-        /* FIXME VarPercent &rVarPos = m_rTree.getPositionVar();
-        double newVal = 1.0 - (double)m_lastPos / excessItems;
-        if( newVal >= 0 )
-        {
-            // Change the position to keep the same first displayed item
-            rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
-        }
+        if( m_flat )
+            it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
         else
-        {
-            // We cannot keep the current first item
-            m_lastPos = excessItems;
-        }*/
-        it = m_rTree.getVisibleItem( excessItems );
+            it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
     }
+    // Redraw the control if the position has changed
+    m_firstPos = it;
     makeImage();
-    notifyLayout();
-#endif
 }
 
 void CtrlTree::onPositionChange()
 {
     makeImage();
-    notifyLayout();
 }
 
 void CtrlTree::handleEvent( EvtGeneric &rEvent )
 {
+    bool bChangedPosition = false;
+    VarTree::Iterator toShow; bool needShow = false;
     if( rEvent.getAsString().find( "key:down" ) != string::npos )
     {
         int key = ((EvtKey&)rEvent).getKey();
         VarTree::Iterator it;
         bool previousWasSelected = false;
-        for( it = m_rTree.begin(); it != m_rTree.end();
-             it = m_rTree.getNextVisibleItem( it ) )
+
+        /* Delete the selection */
+        if( key == KEY_DELETE )
         {
-            VarTree::Iterator next = m_rTree.getNextVisibleItem( it );
-            if( key == KEY_UP )
+            /* Find first non selected item before m_pLastSelected */
+            VarTree::Iterator it_sel = m_flat ? m_rTree.firstLeaf()
+                                              : m_rTree.begin();
+            for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+                 it != m_rTree.end();
+                 it = m_flat ? m_rTree.getNextLeaf( it )
+                             : m_rTree.getNextVisibleItem( it ) )
             {
-                // Scroll up one item
-                if( ( it->parent()
-                      && it != it->parent()->begin() )
-                    || &*it != m_pLastSelected )
-                {
-                    bool nextWasSelected = ( &*next == m_pLastSelected );
-                    it->m_selected = nextWasSelected;
-                    if( nextWasSelected )
-                    {
-                        m_pLastSelected = &*it;
-                    }
-                }
+                if( &*it == m_pLastSelected ) break;
+                if( !it->m_selected ) it_sel = it;
             }
-            else if( key == KEY_DOWN )
+
+            /* Delete selected stuff */
+            m_rTree.delSelected();
+
+            /* Select it_sel */
+            it_sel->m_selected = true;
+            m_pLastSelected = &*it_sel;
+
+            // Redraw the control
+            makeImage();
+            notifyLayout();
+        }
+        else if( key == KEY_PAGEDOWN )
+        {
+            it = m_firstPos;
+            int i = (int)(maxItems()*1.5);
+            while( i >= 0 )
             {
-                // Scroll down one item
-                if( ( it->parent()
-                      && next != it->parent()->end() )
-                    || &*it != m_pLastSelected )
-                {
-                    (*it).m_selected = previousWasSelected;
-                }
-                if( previousWasSelected )
+                VarTree::Iterator it_old = it;
+                it = m_flat ? m_rTree.getNextLeaf( it )
+                            : m_rTree.getNextVisibleItem( it );
+                /* End is already visible, dont' scroll */
+                if( it == m_rTree.end() )
                 {
-                    m_pLastSelected = &*it;
-                    previousWasSelected = false;
+                    it = it_old;
+                    break;
                 }
-                else
+                needShow = true;
+                i--;
+            }
+            if( needShow )
+            {
+                ensureVisible( it );
+                makeImage();
+                notifyLayout();
+            }
+        }
+        else if (key == KEY_PAGEUP )
+        {
+            it = m_firstPos;
+            int i = maxItems();
+            while( i >= maxItems()/2 )
+            {
+                it = m_flat ? m_rTree.getPrevLeaf( it )
+                            : m_rTree.getPrevVisibleItem( it );
+                /* End is already visible, dont' scroll */
+                if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
                 {
-                    previousWasSelected = ( &*it == m_pLastSelected );
+                    break;
                 }
+                i--;
             }
-            else if( key == KEY_RIGHT )
+            ensureVisible( it );
+            makeImage();
+            notifyLayout();
+        }
+        else if ( key == KEY_UP ||
+                  key == KEY_DOWN ||
+                  key == KEY_LEFT ||
+                  key == KEY_RIGHT ||
+                  key == KEY_ENTER ||
+                  key == ' ' )
+        {
+            for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+                 it != m_rTree.end();
+                 it = m_flat ? m_rTree.getNextLeaf( it )
+                             : m_rTree.getNextVisibleItem( it ) )
             {
-                // Go down one level (and expand node)
-                if( &*it == m_pLastSelected )
+                VarTree::Iterator next = m_flat ?
+                                         m_rTree.getNextLeaf( it ) :
+                                         m_rTree.getNextVisibleItem( it );
+                if( key == KEY_UP )
                 {
-                    if( it->m_expanded )
+                    // Scroll up one item
+                    if( ( it->parent()
+                          && it != it->parent()->begin() )
+                        || &*it != m_pLastSelected )
                     {
-                        if( it->size() )
+                        bool nextWasSelected = ( &*next == m_pLastSelected );
+                        it->m_selected = nextWasSelected;
+                        if( nextWasSelected )
                         {
-                            it->m_selected = false;
-                            it->begin()->m_selected = true;
-                            m_pLastSelected = &*(it->begin());
-                        }
-                        else
-                        {
-                            m_rTree.action( &*it );
+                            m_pLastSelected = &*it;
+                            needShow = true; toShow = it;
                         }
                     }
+                }
+                else if( key == KEY_DOWN )
+                {
+                    // Scroll down one item
+                    if( ( it->parent()
+                          && next != it->parent()->end() )
+                        || &*it != m_pLastSelected )
+                    {
+                        (*it).m_selected = previousWasSelected;
+                    }
+                    if( previousWasSelected )
+                    {
+                        m_pLastSelected = &*it;
+                        needShow = true; toShow = it;
+                        previousWasSelected = false;
+                    }
                     else
                     {
-                        it->m_expanded = true;
+                        previousWasSelected = ( &*it == m_pLastSelected );
+                    }
+
+                    // Fix last tree item selection
+                    if( ( m_flat ? m_rTree.getNextLeaf( it )
+                        : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
+                     && &*it == m_pLastSelected )
+                    {
+                        (*it).m_selected = true;
                     }
                 }
-            }
-            else if( key == KEY_LEFT )
-            {
-                // Go up one level (and close node)
-                if( &*it == m_pLastSelected )
+                else if( key == KEY_RIGHT )
                 {
-                    if( it->m_expanded && it->size() )
+                    // Go down one level (and expand node)
+                    if( &*it == m_pLastSelected )
                     {
-                        it->m_expanded = false;
+                        if( it->m_expanded )
+                        {
+                            if( it->size() )
+                            {
+                                it->m_selected = false;
+                                it->begin()->m_selected = true;
+                                m_pLastSelected = &*(it->begin());
+                            }
+                            else
+                            {
+                                m_rTree.action( &*it );
+                            }
+                        }
+                        else
+                        {
+                            it->m_expanded = true;
+                            bChangedPosition = true;
+                        }
                     }
-                    else
+                }
+                else if( key == KEY_LEFT )
+                {
+                    // Go up one level (and close node)
+                    if( &*it == m_pLastSelected )
                     {
-                        if( it->parent() && it->parent() != &m_rTree)
+                        if( it->m_expanded && it->size() )
                         {
-                            it->m_selected = false;
-                            m_pLastSelected = it->parent();
-                            m_pLastSelected->m_selected = true;
+                            it->m_expanded = false;
+                            bChangedPosition = true;
+                        }
+                        else
+                        {
+                            if( it->parent() && it->parent() != &m_rTree)
+                            {
+                                it->m_selected = false;
+                                m_pLastSelected = it->parent();
+                                m_pLastSelected->m_selected = true;
+                            }
                         }
                     }
                 }
-            }
-            else if( key == KEY_ENTER || key == KEY_SPACE )
-            {
-                // Go up one level (and close node)
-                if( &*it == m_pLastSelected )
+                else if( key == KEY_ENTER || key == ' ' )
                 {
-                    m_rTree.action( &*it );
+                    // Go up one level (and close node)
+                    if( &*it == m_pLastSelected )
+                    {
+                        m_rTree.action( &*it );
+                    }
                 }
             }
+            if( needShow )
+                ensureVisible( toShow );
+            // Redraw the control
+            makeImage();
+            notifyLayout();
+        }
+        else
+        {
+            // other keys to be forwarded to vlc core
+            EvtKey& rEvtKey = (EvtKey&)rEvent;
+            var_SetInteger( getIntf()->p_libvlc, "key-pressed",
+                            rEvtKey.getModKey() );
         }
 
-        // Redraw the control
-        makeImage();
-        notifyLayout();
     }
 
     else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
@@ -336,8 +476,10 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
             VarTree::Iterator itClicked = findItemAtPos( yPos );
             // Flag to know if the current item must be selected
             bool select = false;
-            for( it = m_rTree.begin(); it != m_rTree.end();
-                 it = m_rTree.getNextVisibleItem( it ) )
+            for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+                 it != m_rTree.end();
+                 it = m_flat ? m_rTree.getNextLeaf( it )
+                             : m_rTree.getNextVisibleItem( it ) )
             {
                 bool nextSelect = select;
                 if( it == itClicked || &*it == m_pLastSelected )
@@ -373,8 +515,10 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
             VarTree::Iterator itClicked = findItemAtPos( yPos );
             // Flag to know if the current item must be selected
             bool select = false;
-            for( it = m_rTree.begin(); it != m_rTree.end();
-                 it = m_rTree.getNextVisibleItem( it ) )
+            for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+                 it != m_rTree.end();
+                 it = m_flat ? m_rTree.getNextLeaf( it )
+                             : m_rTree.getNextVisibleItem( it ) )
             {
                 bool nextSelect = select;
                 if( it == itClicked || &*it == m_pLastSelected )
@@ -397,25 +541,33 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
                  string::npos )
         {
             it = findItemAtPos(yPos);
-            if( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
-                && xPos < it->depth() * itemImageWidth() )
-            {
-                // Fold/unfold the item
-                it->m_expanded = !it->m_expanded;
-            }
-            else
+            if( it != m_rTree.end() )
             {
-                // Unselect any previously selected item
-                for( it = m_rTree.begin(); it != m_rTree.end();
-                     it = m_rTree.getNextVisibleItem( it ) )
+                if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
+                      && xPos < it->depth() * itemImageWidth() )
+                 && !m_flat )
                 {
-                    it->m_selected = false;
+                    // Fold/unfold the item
+                    it->m_expanded = !it->m_expanded;
+                    bChangedPosition = true;
                 }
-                // Select the new item
-                if( it != m_rTree.end() )
+                else
                 {
-                    it->m_selected = true;
-                    m_pLastSelected = &*it;
+                    // Unselect any previously selected item
+                    VarTree::Iterator it2;
+                    for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+                         it2 != m_rTree.end();
+                         it2 = m_flat ? m_rTree.getNextLeaf( it2 )
+                                      : m_rTree.getNextVisibleItem( it2 ) )
+                    {
+                        it2->m_selected = false;
+                    }
+                    // Select the new item
+                    if( it != m_rTree.end() )
+                    {
+                        it->m_selected = true;
+                        m_pLastSelected = &*it;
+                    }
                 }
             }
         }
@@ -430,7 +582,6 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
                m_rTree.action( &*it );
             }
         }
-
         // Redraw the control
         makeImage();
         notifyLayout();
@@ -438,10 +589,14 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
 
     else if( rEvent.getAsString().find( "scroll" ) != string::npos )
     {
-        int direction = ((EvtScroll&)rEvent).getDirection();
+        // XXX ctrl_slider.cpp has two more (but slightly different)
+        // XXX implementations of `scroll'. Figure out where it belongs.
+
+        int direction = static_cast<EvtScroll&>(rEvent).getDirection();
 
         double percentage = m_rTree.getPositionVar().get();
-        double step = 2.0 / (double)m_rTree.visibleItems();
+        double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
+                                             : m_rTree.visibleItems() );
         if( direction == EvtScroll::kUp )
         {
             percentage += step;
@@ -452,76 +607,153 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
         }
         m_rTree.getPositionVar().set( percentage );
     }
+
+    /* We changed the nodes, let's fix teh position var */
+    if( bChangedPosition )
+    {
+        VarTree::Iterator it;
+        int i = 0;
+        int iFirst = 0;
+        for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+             it != m_rTree.end();
+             it = m_flat ? m_rTree.getNextLeaf( it )
+                         : m_rTree.getNextVisibleItem( it ) )
+        {
+            i++;
+            if( it == m_firstPos )
+            {
+                iFirst = i;
+                break;
+            }
+        }
+        iFirst += maxItems();
+        if( iFirst >= (m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems()) )
+            iFirst = m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems();
+        float f_new = (float)iFirst / (float)( m_flat ? m_rTree.countLeafs()
+                                                      :m_rTree.visibleItems() );
+        m_dontMove = true;
+        m_rTree.getPositionVar().set( 1.0 - f_new );
+        m_dontMove = false;
+    }
 }
 
 bool CtrlTree::mouseOver( int x, int y ) const
 {
     const Position *pPos = getPosition();
-    return ( pPos
-       ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
-       : false);
+    return !pPos ? false :
+        x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight();
 }
 
-void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
+void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h)
 {
-    if( m_pImage )
-    {
-        rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
-    }
+    const Position *pPos = getPosition();
+    rect region( pPos->getLeft(), pPos->getTop(),
+                 pPos->getWidth(), pPos->getHeight() );
+    rect clip( xDest, yDest, w, h );
+    rect inter;
+
+    if( rect::intersect( region, clip, &inter ) && m_pImage )
+        rImage.drawGraphics( *m_pImage,
+                      inter.x - pPos->getLeft(),
+                      inter.y - pPos->getTop(),
+                      inter.x, inter.y, inter.width, inter.height );
 }
 
-void CtrlTree::autoScroll()
+bool CtrlTree::ensureVisible( VarTree::Iterator item )
 {
-    // Find the current playing stream
-    int playIndex = 0;
+    // Find the item to focus
+    int focusItemIndex = 0;
     VarTree::Iterator it;
-    for( it = m_rTree.begin(); it != m_rTree.end();
-         it = m_rTree.getNextVisibleItem( it ) )
+
+    m_rTree.ensureExpanded( item );
+
+    for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+         it != m_rTree.end();
+         it = m_flat ? m_rTree.getNextLeaf( it )
+                     : m_rTree.getNextVisibleItem( it ) )
     {
-        if( it->m_playing ) break;
-        playIndex++;
+        if( it->m_id == item->m_id ) break;
+        focusItemIndex++;
     }
+   return ensureVisible( focusItemIndex );
+}
 
-    if( it == m_rTree.end() ) return;
-
-    // Find  m_lastPos
-    int lastPosIndex = 0;
-    for( it = m_rTree.begin(); it != m_rTree.end();
-         it = m_rTree.getNextVisibleItem( it ) )
+bool CtrlTree::ensureVisible( int focusItemIndex )
+{
+    // Find  m_firstPos
+    VarTree::Iterator it;
+    int firstPosIndex = 0;
+    for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+         it != m_rTree.end();
+         it = m_flat ? m_rTree.getNextLeaf( it )
+                     : m_rTree.getNextVisibleItem( it ) )
     {
-        if( it == m_lastPos ) break;
-        lastPosIndex++;
+        if( it == m_firstPos ) break;
+        firstPosIndex++;
     }
 
-    if( it == m_rTree.end() ) return;
+    if( it == m_rTree.end() ) return false;
 
 
     if( it != m_rTree.end()
-        && ( playIndex < lastPosIndex
-           || playIndex > lastPosIndex + maxItems() ) )
+        && ( focusItemIndex < firstPosIndex
+           || focusItemIndex > firstPosIndex + maxItems() - 1 ) )
     {
-        // Scroll to have the playing stream visible
+        // Scroll to have the wanted stream visible
         VarPercent &rVarPos = m_rTree.getPositionVar();
-        rVarPos.set( 1.0 - (double)playIndex / (double)m_rTree.visibleItems() );
+        int indexMax = ( m_flat ? m_rTree.countLeafs()
+                                : m_rTree.visibleItems() ) - 1;
+        rVarPos.set( 1.0 - (double)focusItemIndex / (double)indexMax );
+        return true;
     }
-    else
+    return false;
+}
+
+void CtrlTree::autoScroll()
+{
+    // Find the current playing stream
+    int playIndex = 0;
+    VarTree::Iterator it;
+
+    for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+         it != m_rTree.end();
+         it = m_flat ? m_rTree.getNextLeaf( it )
+                     : m_rTree.getNextItem( it ) )
     {
-        makeImage();
-        notifyLayout();
+        if( it->m_playing )
+        {
+           m_rTree.ensureExpanded( it );
+           break;
+        }
+    }
+    for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
+         it != m_rTree.end();
+         it = m_flat ? m_rTree.getNextLeaf( it )
+                     : m_rTree.getNextVisibleItem( it ) )
+    {
+        if( it->m_playing )
+           break;
+        playIndex++;
     }
+
+    if( it == m_rTree.end() ) return;
+
+
+    ensureVisible( playIndex );
 }
 
+
 void CtrlTree::makeImage()
 {
-    if( m_pImage )
-    {
-        delete m_pImage;
-    }
+    stats_TimerStart( getIntf(), "[Skins] Playlist image",
+                      STATS_TIMER_SKINS_PLAYTREE_IMAGE );
+    delete m_pImage;
 
     // Get the size of the control
     const Position *pPos = getPosition();
     if( !pPos )
     {
+        stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
         return;
     }
     int width = pPos->getWidth();
@@ -533,7 +765,7 @@ void CtrlTree::makeImage()
     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
     m_pImage = pOsFactory->createOSGraphics( width, height );
 
-    VarTree::Iterator it = m_lastPos;
+    VarTree::Iterator it = m_firstPos;
 
     if( m_pBgBitmap )
     {
@@ -551,7 +783,11 @@ void CtrlTree::makeImage()
                     m_pImage->fillRect( 0, yPos, width, rectHeight,
                                         m_selColor );
                 }
-                it = m_rTree.getNextVisibleItem( it );
+                do
+                {
+                    it = m_flat ? m_rTree.getNextLeaf( it )
+                                : m_rTree.getNextVisibleItem( it );
+                } while( it != m_rTree.end() && it->m_deleted );
             }
         }
     }
@@ -564,37 +800,40 @@ void CtrlTree::makeImage()
         for( int yPos = 0; yPos < height; yPos += i_itemHeight )
         {
             int rectHeight = __MIN( i_itemHeight, height - yPos );
-            if( it != m_rTree.end() )
+            if( it == m_rTree.end() )
+                m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
+            else
             {
                 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
                 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
-                it = m_rTree.getNextVisibleItem( it );
-            }
-            else
-            {
-                m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
+                do
+                {
+                    it = m_flat ? m_rTree.getNextLeaf( it )
+                                : m_rTree.getNextVisibleItem( it );
+                } while( it != m_rTree.end() && it->m_deleted );
             }
             bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
         }
     }
-//    fprintf( stderr, "done\n");
 
     int bitmapWidth = itemImageWidth();
 
     int yPos = 0;
-    it = m_lastPos;
+    it = m_firstPos;
     while( it != m_rTree.end() && yPos < height )
     {
         const GenericBitmap *m_pCurBitmap;
         UString *pStr = (UString*)(it->m_cString.get());
         uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
+
         // Draw the text
         if( pStr != NULL )
         {
-            int depth = it->depth();
+            int depth = m_flat ? 1 : it->depth();
             GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
             if( !pText )
             {
+                stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
                 return;
             }
             if( it->size() )
@@ -604,17 +843,19 @@ void CtrlTree::makeImage()
 
             if( m_pCurBitmap )
             {
+                // Make sure we are centered on the line
                 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
+                if( yPos2 >= height )
+                {
+                    delete pText;
+                    break;
+                }
                 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
                                       bitmapWidth * (depth - 1 ), yPos2,
                                       m_pCurBitmap->getWidth(),
                                       __MIN( m_pCurBitmap->getHeight(),
                                              height -  yPos2), true );
             }
-            else
-            {
-                /* it would be nice to draw something */
-            }
             yPos += i_itemHeight - pText->getHeight();
             int ySrc = 0;
             if( yPos < 0 )
@@ -629,21 +870,26 @@ void CtrlTree::makeImage()
             yPos += (pText->getHeight() - ySrc );
             delete pText;
         }
-        it = m_rTree.getNextVisibleItem( it );
+        do
+        {
+            it = m_flat ? m_rTree.getNextLeaf( it )
+                : m_rTree.getNextVisibleItem( it );
+        } while( it != m_rTree.end() && it->m_deleted );
     }
+    stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
 }
 
 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
 {
-    // The first item is m_lastPos.
+    // The first item is m_firstPos.
     // We decrement pos as we try the other items, until pos == 0.
     VarTree::Iterator it;
-    for( it = m_lastPos; it != m_rTree.end() && pos != 0;
-         it = m_rTree.getNextVisibleItem( it ) )
+    for( it = m_firstPos; it != m_rTree.end() && pos != 0;
+         it = m_flat ? m_rTree.getNextLeaf( it )
+                     : m_rTree.getNextVisibleItem( it ) )
     {
         pos--;
     }
 
     return it;
 }
-