]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/controls/ctrl_tree.cpp
Playtree: handle delete nicely
[vlc] / modules / gui / skins2 / controls / ctrl_tree.cpp
index 17bc5f539409997b4dd5090e271f0d7a8a13fb7c..86863a6c75a1ca115bc5161bb5556664bb75c524 100644 (file)
@@ -2,7 +2,7 @@
  * ctrl_tree.cpp
  *****************************************************************************
  * Copyright (C) 2003 VideoLAN
- * $Id: ctrl_list.cpp 11009 2005-05-14 14:39:05Z ipkiss $
+ * $Id$
  *
  * Authors: Antoine Cellerier <dionoea@videolan.org>
  *
@@ -18,7 +18,7 @@
  *
  * 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>
 #define SCROLL_STEP 0.05
 #define LINE_INTERVAL 1  // Number of pixels inserted between 2 lines
 
+
 CtrlTree::CtrlTree( intf_thread_t *pIntf,
-                  VarTree &rTree,
-                  const GenericFont &rFont,
-                  const GenericBitmap *pBgBitmap,
-                  const GenericBitmap *pItemBitmap,
-                  const GenericBitmap *pOpenBitmap,
-                  const GenericBitmap *pClosedBitmap,
-                  uint32_t fgColor,
-                  uint32_t playColor,
-                  uint32_t bgColor1,
-                  uint32_t bgColor2,
-                  uint32_t selColor,
-                  const UString &rHelp,
-                  VarBool *pVisible ):
+                    VarTree &rTree,
+                    const GenericFont &rFont,
+                    const GenericBitmap *pBgBitmap,
+                    const GenericBitmap *pItemBitmap,
+                    const GenericBitmap *pOpenBitmap,
+                    const GenericBitmap *pClosedBitmap,
+                    uint32_t fgColor,
+                    uint32_t playColor,
+                    uint32_t bgColor1,
+                    uint32_t bgColor2,
+                    uint32_t selColor,
+                    const UString &rHelp,
+                    VarBool *pVisible ):
     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_firstPos = m_rTree.begin();
 
     makeImage();
 }
@@ -129,20 +130,58 @@ int CtrlTree::maxItems()
 }
 
 
-void CtrlTree::onUpdate( Subject<VarTree> &rTree )
+void CtrlTree::onUpdate( Subject<VarTree, tree_update*> &rTree,
+                         tree_update *arg )
 {
-    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
+    {
+        m_firstPos = m_rTree.begin();
+        makeImage();
+    }
+    else if ( arg->i_type == 2 ) // Item-append
+    {
+        /// \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_rTree.getPrevVisibleItem( m_firstPos );
+        }
+        if( m_firstPos->m_deleted ) m_firstPos = m_rTree.root()->begin();
+
+        if( arg->b_visible == true )
+        {
+            makeImage();
+        }
+    }
+    notifyLayout();
 }
 
-void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
+void CtrlTree::onUpdate( Subject<VarPercent, void*> &rPercent, void* arg)
 {
     // Determine what is the first item to display
     VarTree::Iterator it = m_rTree.begin();
 
-    int excessItems = m_rTree.visibleItems() - maxItems();
+    if( m_dontMove ) return;
 
-    fprintf( stderr, "Hullo\n");
+    int excessItems = m_rTree.visibleItems() - maxItems();
 
     if( excessItems > 0)
     {
@@ -151,23 +190,19 @@ void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
 #ifdef _MSC_VER
 #   define lrint (int)
 #endif
-        it = m_rTree.visibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
+        it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
     }
-    if( m_lastPos != it )
+    if( m_firstPos != it )
     {
-        fprintf( stderr, "updating\n" );
         // Redraw the control if the position has changed
-        m_lastPos = it;
+        m_firstPos = it;
         makeImage();
         notifyLayout();
     }
-    else
-        fprintf( stderr, "not updating\n" );
 }
 
 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();
 
@@ -180,37 +215,12 @@ void CtrlTree::onResize()
 #ifdef _MSC_VER
 #   define lrint (int)
 #endif
-        it = m_rTree.visibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
+        it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
     }
     // Redraw the control if the position has changed
-    m_lastPos = it;
+    m_firstPos = 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 );
-        }
-        else
-        {
-            // We cannot keep the current first item
-            m_lastPos = excessItems;
-        }*/
-        it = m_rTree.visibleItem( excessItems );
-    }
-    makeImage();
-    notifyLayout();
-#endif
 }
 
 void CtrlTree::onPositionChange()
@@ -219,35 +229,74 @@ void CtrlTree::onPositionChange()
     notifyLayout();
 }
 
-#define IT_DISP_LOOP_END( a )  \
-                if( a ->m_expanded && a ->size() ) \
-                { \
-                    a = a ->begin(); \
-                } \
-                else \
-                { \
-                    VarTree::Iterator it_old = a; \
-                    a ++; \
-                    if( it_old->parent() && it_old->parent()->end() == a ) \
-                    { \
-                        a = it_old->uncle(); \
-                    } \
-                }
 void CtrlTree::handleEvent( EvtGeneric &rEvent )
 {
-    // TODO TODO FIXME TODO TODO
+    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 = m_rTree.begin();
+        VarTree::Iterator it;
         bool previousWasSelected = false;
-        while( it != m_rTree.end() )
+
+        /* Delete the selection */
+        if( key == KEY_DELETE )
         {
-            VarTree::Iterator next = it;
-            IT_DISP_LOOP_END( next );
+            m_rTree.delSelected();
+        }
+        else if( key == KEY_PAGEDOWN )
+        {
+            it = m_firstPos;
+            int i = (int)(maxItems()*1.5);
+            while( i >= 0 )
+            {
+                VarTree::Iterator it_old = it;
+                it = m_rTree.getNextVisibleItem( it );
+                /* End is already visible, dont' scroll */
+                if( it == m_rTree.end() )
+                {
+                    it = it_old;
+                    break;
+                }
+                needShow = true;
+                i--;
+            }
+            if( needShow )
+            {
+                ensureVisible( it );
+                makeImage();
+                notifyLayout();
+                return;
+            }
+        }
+        else if (key == KEY_PAGEUP )
+        {
+            it = m_firstPos;
+            int i = maxItems();
+            while( i >= maxItems()/2 )
+            {
+                it = m_rTree.getPrevVisibleItem( it );
+                /* End is already visible, dont' scroll */
+                if( it == m_rTree.begin() )
+                {
+                    break;
+                }
+                i--;
+            }
+            ensureVisible( it );
+            makeImage();
+            notifyLayout();
+            return;
+        }
+
+
+        for( it = m_rTree.begin(); it != m_rTree.end();
+             it = m_rTree.getNextVisibleItem( it ) )
+        {
+            VarTree::Iterator next = m_rTree.getNextVisibleItem( it );
             if( key == KEY_UP )
             {
-                //Scroll up one item
+                // Scroll up one item
                 if( ( it->parent()
                       && it != it->parent()->begin() )
                     || &*it != m_pLastSelected )
@@ -257,6 +306,7 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
                     if( nextWasSelected )
                     {
                         m_pLastSelected = &*it;
+                        needShow = true; toShow = it;
                     }
                 }
             }
@@ -272,6 +322,7 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
                 if( previousWasSelected )
                 {
                     m_pLastSelected = &*it;
+                    needShow = true; toShow = it;
                     previousWasSelected = false;
                 }
                 else
@@ -281,28 +332,61 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
             }
             else if( key == KEY_RIGHT )
             {
-                // Go down one level
-                if( it->m_expanded )
+                // Go down one level (and expand node)
+                if( &*it == m_pLastSelected )
                 {
-                    if( it->size() )
+                    if( it->m_expanded )
                     {
-                        /* FIXME : finir */
-                        m_pLastSelected = &*(it->begin());
+                        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 )
                 {
-                    it->m_expanded = true;
+                    if( it->m_expanded && it->size() )
+                    {
+                        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_LEFT )
+            else if( key == KEY_ENTER || key == KEY_SPACE )
             {
                 // Go up one level (and close node)
-                // TODO
-                it->m_expanded = false;
+                if( &*it == m_pLastSelected )
+                {
+                    m_rTree.action( &*it );
+                }
             }
-            it = next;
         }
+        if( needShow )
+            ensureVisible( toShow );
 
         // Redraw the control
         makeImage();
@@ -316,19 +400,18 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
         int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
         int xPos = rEvtMouse.getXPos() - pos->getLeft();
         VarTree::Iterator it;
-        int index = 0;
 
         if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
             string::npos )
         {
-            // Flag to know if the currend item must be selected
+            VarTree::Iterator itClicked = findItemAtPos( yPos );
+            // Flag to know if the current item must be selected
             bool select = false;
-            index = -1;
-            for( it = m_rTree.begin(); it != m_rTree.end(); )
+            for( it = m_rTree.begin(); it != m_rTree.end();
+                 it = m_rTree.getNextVisibleItem( it ) )
             {
                 bool nextSelect = select;
-                if( it == m_lastPos ) index = 0;
-                if( index == yPos || &*it == m_pLastSelected )
+                if( it == itClicked || &*it == m_pLastSelected )
                 {
                     if( select )
                     {
@@ -342,37 +425,30 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
                 }
                 it->m_selected = (*it).m_selected || select;
                 select = nextSelect;
-                if( index != -1 )
-                    index++;
-                IT_DISP_LOOP_END( it );
             }
         }
         else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
                  string::npos )
         {
-            for( it = m_lastPos; it != m_rTree.end(); )
+            // Invert the selection of the item
+            it = findItemAtPos( yPos );
+            if( it != m_rTree.end() )
             {
-                if( index == yPos )
-                {
-                    it->m_selected = !it->m_selected;
-                    m_pLastSelected = &*it;
-                    break;
-                }
-                index++;
-                IT_DISP_LOOP_END( it );
+                it->m_selected = !it->m_selected;
+                m_pLastSelected = &*it;
             }
         }
         else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
                  string::npos )
         {
-            // Flag to know if the currend item must be selected
+            VarTree::Iterator itClicked = findItemAtPos( yPos );
+            // Flag to know if the current item must be selected
             bool select = false;
-            index = -1;
-            for( it = m_rTree.begin(); it != m_rTree.end(); )
+            for( it = m_rTree.begin(); it != m_rTree.end();
+                 it = m_rTree.getNextVisibleItem( it ) )
             {
                 bool nextSelect = select;
-                if( it == m_lastPos ) index = 0;
-                if( index == yPos || &*it == m_pLastSelected )
+                if( it == itClicked || &*it == m_pLastSelected )
                 {
                     if( select )
                     {
@@ -386,58 +462,50 @@ void CtrlTree::handleEvent( EvtGeneric &rEvent )
                 }
                 it->m_selected = select;
                 select = nextSelect;
-                if( index != -1 )
-                    index++;
-                IT_DISP_LOOP_END( it );
             }
         }
         else if( rEvent.getAsString().find( "mouse:left:down" ) !=
                  string::npos )
         {
-            for( it = m_lastPos; it != m_rTree.end(); )
+            it = findItemAtPos(yPos);
+            if( it != m_rTree.end() )
             {
-                if( index == yPos )
+                if( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
+                    && xPos < it->depth() * itemImageWidth() )
                 {
-                    it->m_selected = true;
-                    m_pLastSelected = &*it;
+                    // Fold/unfold the item
+                    it->m_expanded = !it->m_expanded;
+                    bChangedPosition = true;
                 }
                 else
                 {
-                    it->m_selected = false;
-                }
-                index ++;
-                IT_DISP_LOOP_END( it );
-            }
-        }
-
-        else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
-                 string::npos )
-        {
-            for( it = m_lastPos; it != m_rTree.end(); )
-            {
-                if( index == yPos )
-                {
-                    if( it->size() && xPos < it->depth() * itemImageWidth() )
+                    // Unselect any previously selected item
+                    VarTree::Iterator it2;
+                    for( it2 = m_rTree.begin(); it2 != m_rTree.end();
+                         it2 = m_rTree.getNextVisibleItem( it2 ) )
                     {
-                        it->m_expanded = !it->m_expanded;
+                        it2->m_selected = false;
                     }
-                    else
+                    // Select the new item
+                    if( it != m_rTree.end() )
                     {
                         it->m_selected = true;
                         m_pLastSelected = &*it;
-                        // Execute the action associated to this item
-                        m_rTree.action( &*it );
                     }
                 }
-                else
-                {
-                    it->m_selected = false;
-                }
-                index ++;
-                IT_DISP_LOOP_END( it );
             }
         }
 
+        else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
+                 string::npos )
+        {
+            it = findItemAtPos(yPos);
+            if( it != m_rTree.end() )
+            {
+               // Execute the action associated to this item
+               m_rTree.action( &*it );
+            }
+        }
         // Redraw the control
         makeImage();
         notifyLayout();
@@ -459,6 +527,30 @@ 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_rTree.begin(); it != m_rTree.end();
+             it = m_rTree.getNextVisibleItem( it ) )
+        {
+            i++;
+            if( it == m_firstPos )
+            {
+                iFirst = i;
+                break;
+            }
+        }
+        iFirst += maxItems();
+        if( iFirst >= m_rTree.visibleItems() ) iFirst = m_rTree.visibleItems();
+        float f_new = (float)iFirst / (float)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
@@ -477,50 +569,83 @@ void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
     }
 }
 
-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(); )
+
+    m_rTree.ensureExpanded( item );
+
+    for( it = m_rTree.begin(); it != m_rTree.end();
+         it = m_rTree.getNextVisibleItem( it ) )
     {
-        if( it->m_playing ) break;
-        playIndex++;
-        IT_DISP_LOOP_END( it );
+        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(); )
+bool CtrlTree::ensureVisible( int focusItemIndex )
+{
+    // Find  m_firstPos
+    VarTree::Iterator it;
+    int firstPosIndex = 0;
+    for( it = m_rTree.begin(); it != m_rTree.end();
+         it = m_rTree.getNextVisibleItem( it ) )
     {
-        if( it == m_lastPos ) break;
-        lastPosIndex++;
-        IT_DISP_LOOP_END( it );
+        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() ) )
     {
-        // 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() );
+        rVarPos.set( 1.0 - (double)focusItemIndex /
+                           (double)m_rTree.visibleItems() );
+        return true;
     }
-    else
+    return false;
+}
+
+void CtrlTree::autoScroll()
+{
+    // Find the current playing stream
+    int playIndex = 0;
+    VarTree::Iterator it;
+
+    for( it = m_rTree.begin(); it != m_rTree.end();
+         it = m_rTree.getNextItem( it ) )
     {
-        makeImage();
-        notifyLayout();
+        if( it->m_playing )
+        {
+           m_rTree.ensureExpanded( it );
+           break;
+        }
+    }
+    for( it = m_rTree.begin(); it != m_rTree.end();
+         it = m_rTree.getNextVisibleItem( it ) )
+    {
+        if( it->m_playing )
+           break;
+        playIndex++;
     }
+
+    if( it == m_rTree.end() ) return;
+
+
+    ensureVisible( playIndex );
 }
 
+
 void CtrlTree::makeImage()
 {
-    fprintf( stderr, "CtrlTree::makeImage()\n");
     if( m_pImage )
     {
         delete m_pImage;
@@ -541,7 +666,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,15 +676,18 @@ 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_selected )
                 {
+                    int rectHeight = __MIN( i_itemHeight, height - yPos );
                     m_pImage->fillRect( 0, yPos, width, rectHeight,
                                         m_selColor );
                 }
-                IT_DISP_LOOP_END( it );
+                do
+                {
+                    it = m_rTree.getNextVisibleItem( it );
+                } while( it->m_deleted );
             }
         }
     }
@@ -576,7 +704,10 @@ void CtrlTree::makeImage()
             {
                 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
                 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
-                IT_DISP_LOOP_END( it );
+                do
+                {
+                    it = m_rTree.getNextVisibleItem( it );
+                } while( it->m_deleted );
             }
             else
             {
@@ -585,34 +716,45 @@ void CtrlTree::makeImage()
             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 ){
+        if( pStr != NULL )
+        {
             int depth = it->depth();
             GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
             if( !pText )
             {
                 return;
             }
-            m_pCurBitmap = it->size() ? ( it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap ) : m_pItemBitmap ;
+            if( it->size() )
+                m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
+            else
+                m_pCurBitmap = m_pItemBitmap;
+
             if( m_pCurBitmap )
             {
+                // Make sure we are centered on the line
                 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
-                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 */
+                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 );
             }
             yPos += i_itemHeight - pText->getHeight();
             int ySrc = 0;
@@ -621,13 +763,29 @@ void CtrlTree::makeImage()
                 ySrc = - yPos;
                 yPos = 0;
             }
-            int lineHeight =  __MIN( pText->getHeight() - ySrc, height - yPos );
+            int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
             m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
                                   pText->getWidth(),
                                   lineHeight, true );
             yPos += (pText->getHeight() - ySrc );
             delete pText;
         }
-        IT_DISP_LOOP_END( it );
+        do {
+        it = m_rTree.getNextVisibleItem( it );
+        } while( it->m_deleted );
+    }
+}
+
+VarTree::Iterator CtrlTree::findItemAtPos( int pos )
+{
+    // The first item is m_firstPos.
+    // We decrement pos as we try the other items, until pos == 0.
+    VarTree::Iterator it;
+    for( it = m_firstPos; it != m_rTree.end() && pos != 0;
+         it = m_rTree.getNextVisibleItem( it ) )
+    {
+        pos--;
     }
+
+    return it;
 }